Merge branch 'dev' of /home/macgirvin/roadhouse into dev

This commit is contained in:
nobody 2021-04-20 20:22:57 -07:00
commit 1acd4fc8e2
4 changed files with 68 additions and 7 deletions

View file

@ -0,0 +1,18 @@
<?php
namespace Zotlabs\Daemon;
use Zotlabs\Lib\Img_cache;
class Cache_image {
static public function run($argc,$argv) {
cli_startup();
if ($argc === 3) {
Img_cache::url_to_cache($argv[1],$argv[2]);
}
}
}

View file

@ -7,11 +7,16 @@ use Zotlabs\Daemon\Run;
class Img_cache { class Img_cache {
static $cache_life = 18600 * 7; static $cache_life = 18600 * 7;
static function get_filename($url, $prefix = '.') { static function get_filename($url, $prefix = '.') {
return Hashpath::path($url,$prefix); return Hashpath::path($url,$prefix);
} }
// Check to see if we have this url in our cache
// If we have it return true.
// If we do not, or the cache file is empty or expired, return false
// but attempt to fetch the entry in the background
static function check($url, $prefix = '.') { static function check($url, $prefix = '.') {
if (strpos($url,z_root()) !== false) { if (strpos($url,z_root()) !== false) {
@ -22,15 +27,16 @@ class Img_cache {
if (file_exists($path)) { if (file_exists($path)) {
$t = filemtime($path); $t = filemtime($path);
if ($t && time() - $t >= self::$cache_life) { if ($t && time() - $t >= self::$cache_life) {
if (self::url_to_cache($url,$path)) { Run::Summon( [ 'Cache_image', $url, $path ] );
return true;
}
return false; return false;
} }
return true; else {
return ((filesize($path)) ? true : false);
}
} }
return self::url_to_cache($url,$path); Run::Summon( [ 'Cache_image', $url, $path ] );
return false;
} }
static function url_to_cache($url,$file) { static function url_to_cache($url,$file) {

View file

@ -9,7 +9,7 @@ class Ca extends Controller {
if (argc() > 1) { if (argc() > 1) {
$path = 'cache/img/' . substr(argv(1),0,2) . '/' . argv(1); $path = 'cache/img/' . substr(argv(1),0,2) . '/' . argv(1);
if (file_exists($path)) { if (file_exists($path) && filesize($path)) {
$x = @getimagesize($path); $x = @getimagesize($path);
if ($x) { if ($x) {
header('Content-Type: ' . $x['mime']); header('Content-Type: ' . $x['mime']);

View file

@ -1023,6 +1023,17 @@ function linkify($s, $me = false) {
*/ */
function sslify($s, $cache_enable = true) { function sslify($s, $cache_enable = true) {
if (! $cache_enable) {
// we're fetching an old item and we are no longer
// caching photos for it. Remove any existing cached photos.
// Cron_weekly tasks will also remove these, but if the cache
// entry was updated recently they might not get removed for
// another couple of months.
uncache($s);
}
if ((! $cache_enable) || (! intval(get_config('system','cache_images', 1)))) { if ((! $cache_enable) || (! intval(get_config('system','cache_images', 1)))) {
// if caching is prevented for whatever reason, proxy any non-SSL photos // if caching is prevented for whatever reason, proxy any non-SSL photos
@ -1067,6 +1078,30 @@ function sslify($s, $cache_enable = true) {
return $s; return $s;
} }
// clean out the image cache
function uncache($s) {
$pattern = "/\<img(.*?)src=\"(https?\:.*?)\"(.*?)\>/ism";
$matches = null;
$cnt = preg_match_all($pattern,$s,$matches,PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $match) {
// repeat the filename generation procedure we used when creating the cache entry
$clean = strip_zids(strip_query_param($match[2],'f'));
$file = Img_cache::get_filename($clean,'cache/img');
if (file_exists($file)) {
unlink($file);
}
}
}
return $s;
}
/** /**
* @brief Get an array of poke verbs. * @brief Get an array of poke verbs.
* *
@ -1782,6 +1817,8 @@ function prepare_body(&$item,$attach = false,$opts = false) {
} }
$cache_enable = ((($cache_expire) && ($item['created'] < datetime_convert('UTC','UTC', 'now - ' . $cache_expire . ' days'))) ? false : true); $cache_enable = ((($cache_expire) && ($item['created'] < datetime_convert('UTC','UTC', 'now - ' . $cache_expire . ' days'))) ? false : true);
if($s) if($s)
$s = sslify($s, $cache_enable); $s = sslify($s, $cache_enable);
if($photo) if($photo)