From bd0fc8a2d7f71e9b9d6fa768fa30484bcb7e22f2 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 10:38:25 +0100 Subject: Add photo cache hooks --- Zotlabs/Module/Photo.php | 90 ++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 33 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Photo.php b/Zotlabs/Module/Photo.php index 30e8340e2..55e92ca04 100644 --- a/Zotlabs/Module/Photo.php +++ b/Zotlabs/Module/Photo.php @@ -1,19 +1,19 @@ false, // Cache off + 'age' => 86400, // max-age + 'exp' => true, // Use own max-age + 'leak' => false // Do not allow non members use our cache + ); + call_hooks('cache_mode_hook', $cache_mode); + $observer_xchan = get_observer_hash(); $ismodified = $_SERVER['HTTP_IF_MODIFIED_SINCE']; @@ -106,13 +114,14 @@ class Photo extends \Zotlabs\Web\Controller { License link: http://creativecommons.org/licenses/by/3.0/ */ + // @FIXME It seems this part doesn't work because we are not setting such cookie $cookie_value = false; if (isset($_COOKIE['devicePixelRatio'])) { $cookie_value = intval($_COOKIE['devicePixelRatio']); } else { // Force revalidation of cache on next request - $cache_directive = 'no-cache'; + // $prvcachecontrol = 'no-cache'; $status = 'no cookie'; } @@ -129,27 +138,44 @@ class Photo extends \Zotlabs\Web\Controller { $resolution = 1; } - $r = q("SELECT uid, photo_usage FROM photo WHERE resource_id = '%s' AND imgscale = %d LIMIT 1", + $r = q("SELECT uid, photo_usage, expires, display_path FROM photo WHERE resource_id = '%s' AND imgscale = %d LIMIT 1", dbesc($photo), intval($resolution) ); if($r) { - $allowed = (-1); - if(intval($r[0]['photo_usage'])) { + $u = intval($r[0]['photo_usage']); + if($u) { $allowed = 1; - if(intval($r[0]['photo_usage']) === PHOTO_COVER) + if($u === PHOTO_COVER) if($resolution < PHOTO_RES_COVER_1200) $allowed = (-1); - if(intval($r[0]['photo_usage']) === PHOTO_PROFILE) + if($u === PHOTO_PROFILE) if(! in_array($resolution,[4,5,6])) $allowed = (-1); + if($u === PHOTO_CACHE) { + $origurl = $r[0]['display_path']; + // Cached image leak protection + if(! (local_channel() || $cache_mode['leak'])) { + header("Location: " . $origurl); + killme(); + } + // Revalidate cache + if($cache_mode['on'] && strtotime($r[0]['expires']) - 60 < time()) { + $cache = array( + 'url' => $origurl, + 'uid' => $r[0]['uid'] + ); + call_hooks('cache_url_hook', $cache); + if(! $cache['status']) + http_status_exit(404,'not found'); + } + } } - if($allowed === (-1)) { + if($allowed === (-1)) $allowed = attach_can_view($r[0]['uid'],$observer_xchan,$photo); - } $channel = channelx_by_n($r[0]['uid']); @@ -160,16 +186,19 @@ class Photo extends \Zotlabs\Web\Controller { ); $exists = (($e) ? true : false); - + if($exists && $allowed) { + $expires = strtotime($e[0]['expires'] . 'Z'); $data = dbunescbin($e[0]['content']); $filesize = $e[0]['filesize']; $mimetype = $e[0]['mimetype']; $modified = strtotime($e[0]['edited'] . 'Z'); - if(intval($e[0]['os_storage'])) + + if(intval($e[0]['os_storage'])) { $streaming = $data; + } if($e[0]['allow_cid'] != '' || $e[0]['allow_gid'] != '' || $e[0]['deny_gid'] != '' || $e[0]['deny_gid'] != '') - $prvcachecontrol = true; + $prvcachecontrol = 'no-store, no-cache, must-revalidate'; } else { if(! $allowed) { @@ -180,9 +209,9 @@ class Photo extends \Zotlabs\Web\Controller { } } - } else { + } + else http_status_exit(404,'not found'); - } } header_remove('Pragma'); @@ -225,24 +254,14 @@ class Photo extends \Zotlabs\Web\Controller { $mimetype = $ph->getType(); } } - - // @FIXME Seems never invoked - // Writing in cachefile - if (isset($cachefile) && $cachefile != '') { - file_put_contents($cachefile, $data); - $modified = filemtime($cachefile); - } - - header("Content-type: " . $mimetype); - - if($prvcachecontrol) { + if(isset($prvcachecontrol)) { // it is a private photo that they have no permission to view. // tell the browser not to cache it, in case they authenticate // and subsequently have permission to see it - header("Cache-Control: no-store, no-cache, must-revalidate"); + header("Cache-Control: " . $prvcachecontrol); } else { @@ -255,18 +274,23 @@ class Photo extends \Zotlabs\Web\Controller { // This has performance considerations but we highly recommend you // leave it alone. - $cache = get_config('system','photo_cache_time', 86400); // 1 day by default + $maxage = $cache_mode['age']; - header("Expires: " . gmdate("D, d M Y H:i:s", time() + $cache) . " GMT"); - header("Cache-Control: max-age=" . $cache); + if($cache_mode['exp'] || (! isset($expires)) || (isset($expires) && $expires - 60 < time())) + $expires = time() + $maxage; + else + $maxage = $expires - time(); + + header("Expires: " . gmdate("D, d M Y H:i:s", $expires) . " GMT"); + header("Cache-Control: max-age=" . $maxage); } + header("Content-type: " . $mimetype); header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modified) . " GMT"); header("Content-Length: " . (isset($filesize) ? $filesize : strlen($data))); // If it's a file resource, stream it. - if($streaming && $channel) { if(strpos($streaming,'store') !== false) $istream = fopen($streaming,'rb'); -- cgit v1.2.3 From 6aa99a903fd221d306c62a807cf3c1a4564f0b4d Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 11:56:56 +0100 Subject: Code cleanup --- Zotlabs/Module/Photo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Photo.php b/Zotlabs/Module/Photo.php index 55e92ca04..b8d1c36f2 100644 --- a/Zotlabs/Module/Photo.php +++ b/Zotlabs/Module/Photo.php @@ -5,6 +5,7 @@ namespace Zotlabs\Module; require_once('include/security.php'); require_once('include/attach.php'); +require_once('include/photo/photo_driver.php'); class Photo extends \Zotlabs\Web\Controller { @@ -155,16 +156,15 @@ class Photo extends \Zotlabs\Web\Controller { if(! in_array($resolution,[4,5,6])) $allowed = (-1); if($u === PHOTO_CACHE) { - $origurl = $r[0]['display_path']; // Cached image leak protection if(! (local_channel() || $cache_mode['leak'])) { - header("Location: " . $origurl); + header("Location: " . $r[0]['display_path']); killme(); } // Revalidate cache if($cache_mode['on'] && strtotime($r[0]['expires']) - 60 < time()) { $cache = array( - 'url' => $origurl, + 'url' => $r[0]['display_path'], 'uid' => $r[0]['uid'] ); call_hooks('cache_url_hook', $cache); -- cgit v1.2.3 From f3831fa77fe62d8696f867259ae9f007a0a3d2ea Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 21:27:06 +0100 Subject: Better cache settings logic --- Zotlabs/Module/Photo.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Photo.php b/Zotlabs/Module/Photo.php index b8d1c36f2..d85db7307 100644 --- a/Zotlabs/Module/Photo.php +++ b/Zotlabs/Module/Photo.php @@ -32,10 +32,10 @@ class Photo extends \Zotlabs\Web\Controller { } $cache_mode = array( - 'on' => false, // Cache off - 'age' => 86400, // max-age - 'exp' => true, // Use own max-age - 'leak' => false // Do not allow non members use our cache + 'on' => get_config('system','photo_cache_enable', 0), + 'age' => 86400, + 'exp' => true, + 'leak' => false ); call_hooks('cache_mode_hook', $cache_mode); -- cgit v1.2.3