]> git.mxchange.org Git - friendica-addons.git/blobdiff - privacy_image_cache/privacy_image_cache.php
privacy_image_cache: A file cache is implemented that should be much faster than...
[friendica-addons.git] / privacy_image_cache / privacy_image_cache.php
index b5cfb13e6d1c87319144a0ee2bb814e82f214bee..f0d8e39ac55b24b2a5d4b31a1900d61911cc2bcc 100644 (file)
@@ -43,6 +43,24 @@ function privacy_image_cache_init() {
        // Double encoded url - happens with Diaspora
        $urlhash2 = 'pic:' . sha1(urldecode($_REQUEST['url']));
 
+       $cache = get_config('system','itemcache');
+       if (($cache != '') and is_dir($cache)) {
+               $cachefile = $cache."/".hash("md5", $urlhash);
+               if (file_exists($cachefile)) {
+                       $img_str = file_get_contents($cachefile);
+
+                       $mime = image_type_to_mime_type(exif_imagetype($cachefile));
+
+                       header("Content-type: $mime");
+                       header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
+                       header("Cache-Control: max-age=" . (3600*24));
+
+                       echo $img_str;
+
+                       killme();
+               }
+       }
+
        $r = q("SELECT * FROM `photo` WHERE `resource-id` in ('%s', '%s') LIMIT 1", $urlhash, $urlhash2);
        if (count($r)) {
                $img_str = $r[0]['data'];
@@ -51,6 +69,9 @@ function privacy_image_cache_init() {
        } else {
                require_once("Photo.php");
 
+               // It shouldn't happen but it does - spaces in URL
+               $_REQUEST['url'] = str_replace(" ", "+", $_REQUEST['url']);
+
                $img_str = fetch_url($_REQUEST['url'],true);
 
                $tempfile = tempnam("", "cache");
@@ -95,6 +116,10 @@ function privacy_image_cache_init() {
                }
        }
 
+       // Writing in cachefile
+       if (isset($cachefile) && $cachefile != '')
+               file_put_contents($cachefile, $img_str);
+
        header("Content-type: $mime");
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
        header("Cache-Control: max-age=" . (3600*24));
@@ -127,7 +152,7 @@ function privacy_image_cache_img_cb($matches) {
        if (privacy_image_cache_is_local_image($matches[2]))
                return $matches[1] . $matches[2] . $matches[3];
 
-       return $matches[1] . get_app()->get_baseurl() . "/privacy_image_cache/?url=" . escape_tags(addslashes(rawurlencode($matches[2]))) . $matches[3];
+       return $matches[1] . get_app()->get_baseurl() . "/privacy_image_cache/?url=" . addslashes(rawurlencode(htmlspecialchars_decode($matches[2]))) . $matches[3];
 }
 
 /**