better image caching

This commit is contained in:
zotlabs 2020-03-04 20:07:26 -08:00
parent e0276892b9
commit cdd02dc54b
5 changed files with 78 additions and 9 deletions

1
.gitignore vendored
View file

@ -26,6 +26,7 @@ addon/
widget/
custom/
/store/
cache/
# site apps
apps/
!doc/context/*/apps

View file

@ -51,6 +51,23 @@ class Cron_weekly {
// Check for dead sites
Master::Summon(array('Checksites'));
// clean up image cache - use site expiration or 180 days if not set
$files = glob('cache/img/*/*');
$expire_days = intval(get_config('system','default_expire_days', 180);
$now = time();
if ($files) {
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= 60 * 60 * 24 * $expire_days) {
unlink($file);
}
}
}
}
// update searchable doc indexes
// disabled until help system regenerated
// Master::Summon(array('Importdoc'));

30
Zotlabs/Module/Ca.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace Zotlabs\Module;
use Zotlabs\Web\Controller;
class Ca extends Controller {
function get() {
if (argc() > 1) {
$path = 'cache/img/' . substr(argv(1),0,2) . '/' . argv(1);
if (file_exists($path)) {
$x = @getimagesize($path);
if ($x) {
header('Content-Type: ' . $x['mime']);
}
$infile = fopen($path,'rb');
$outfile = fopen('php://output','wb');
pipe_streams($infile,$outfile);
fclose($infile);
fclose($outfile);
killme();
}
if ($_GET['url']) {
goaway($url);
}
}
http_status_exit(404,'Not found');
}
}

View file

@ -993,18 +993,38 @@ function linkify($s, $me = false) {
}
/**
* @brief Replace media element using http url with https to a local redirector
* if using https locally.
* @brief implement image caching
*
* Looks for HTML tags containing src elements that are http when we're viewing an https page
* Typically this throws an insecure content violation in the browser. So we redirect them
* to a local redirector which uses https and which redirects to the selected content
* Note: This is named sslify because the old behaviour was only to proxy image fetches to
* non-SSL resources. Now all images are cached.
*
* @param string $s
* @returns string
*/
function sslify($s) {
if (! intval(get_config('system','cache_images', 1))) {
if (strpos(z_root(),'https:') === false) {
return $s;
}
// we'll only sslify img tags because media files will probably choke.
$pattern = "/\<img(.*?)src=\"(http\:.*?)\"(.*?)\>/";
$matches = null;
$cnt = preg_match_all($pattern,$s,$matches,PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $match) {
$filename = basename( parse_url($match[2], PHP_URL_PATH) );
$s = str_replace($match[2],z_root() . '/sslify/' . $filename . '?f=&url=' . urlencode($match[2]),$s);
}
}
return $s;
}
$pattern = "/\<img(.*?)src=\"(https?\:.*?)\"(.*?)\>/ism";
$matches = null;
@ -1016,7 +1036,7 @@ function sslify($s) {
}
$cached = Img_cache::check($match[2],'cache/img');
if ($cached) {
$s = str_replace($match[2],z_root() . '/' . Img_cache::get_filename($match[2],'cache/img') . '?url=' . urlencode($match[2]),$s);
$s = str_replace($match[2],z_root() . '/ca/' . basename(Img_cache::get_filename($match[2],'cache/img')) . '?url=' . urlencode($match[2]),$s);
}
}
}

View file

@ -105,9 +105,10 @@ web server platforms.
- make sure folders *cache/smarty3* and *store* exist and are
writable by the webserver
mkdir -p "store"
mkdir -p "cache/smarty3"
chmod -R 777 store
chmod -R 777 store cache
[This permission (777) is very dangerous and if you have sufficient
privilege and knowledge you should make these directories writeable