2020-03-05 02:55:25 +00:00
|
|
|
<?php
|
|
|
|
namespace Zotlabs\Lib;
|
2020-03-11 22:22:34 +00:00
|
|
|
|
2020-06-15 07:29:56 +00:00
|
|
|
use Zotlabs\Daemon\Run;
|
2020-03-05 02:55:25 +00:00
|
|
|
|
|
|
|
class Img_cache {
|
|
|
|
|
|
|
|
static $cache_life = 18600 * 7;
|
|
|
|
|
|
|
|
static function get_filename($url, $prefix = EMPTY_STR) {
|
|
|
|
$hash = hash('sha256', $url);
|
|
|
|
$slug = substr($hash,0,2);
|
|
|
|
$path = $prefix . '/' . $slug;
|
|
|
|
os_mkdir($path, STORAGE_DEFAULT_PERMISSIONS, true);
|
|
|
|
return $path . '/' . $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function check($url, $prefix = EMPTY_STR) {
|
|
|
|
|
|
|
|
if (strpos($url,z_root()) !== false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = self::get_filename($url,$prefix);
|
|
|
|
if (file_exists($path)) {
|
2020-03-06 04:00:21 +00:00
|
|
|
$t = filemtime($path);
|
2020-03-05 02:55:25 +00:00
|
|
|
if ($t && time() - $t >= self::$cache_life) {
|
|
|
|
if (self::url_to_cache($url,$path)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::url_to_cache($url,$path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function url_to_cache($url,$file) {
|
|
|
|
|
2020-03-05 04:51:20 +00:00
|
|
|
$fp = fopen($file,'wb');
|
2020-03-05 02:55:25 +00:00
|
|
|
|
|
|
|
if (! $fp) {
|
2020-03-06 04:00:21 +00:00
|
|
|
logger('failed to open storage file: ' . $file,LOGGER_NORMAL,LOG_ERR);
|
2020-03-05 02:55:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$redirects = 0;
|
|
|
|
$x = z_fetch_url($url,true,$redirects,[ 'filep' => $fp, 'novalidate' => true ]);
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
if ($x['success']) {
|
2020-05-07 23:23:31 +00:00
|
|
|
$i = getimagesize($file);
|
|
|
|
if ($i && $i[2]) { // looking for non-zero imagetype
|
2020-06-15 07:29:56 +00:00
|
|
|
Run::Summon( [ 'CacheThumb' , basename($file) ] );
|
2020-05-07 23:23:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-05 02:55:25 +00:00
|
|
|
}
|
|
|
|
unlink($file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|