]> git.mxchange.org Git - friendica.git/commitdiff
New cache system with subdirectories
authorMichael Vogel <icarus@dabo.de>
Fri, 16 Nov 2012 23:50:39 +0000 (00:50 +0100)
committerMichael Vogel <icarus@dabo.de>
Fri, 16 Nov 2012 23:50:39 +0000 (00:50 +0100)
boot.php
include/network.php
include/poller.php
include/text.php
mod/photo.php
view/theme/vier/prv_message.tpl [deleted file]
view/theme/vier/style.css

index 8e79d6d9be62807187f28fee21f661d5a91c191c..f69f6fe820d62a942283487e8ef47c1ce8a6f923 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -1847,3 +1847,54 @@ function random_digits($digits) {
        }
        return $rn;
 }
+
+function get_cachefile($file, $writemode = true) {
+       $cache = get_config("system","itemcache");
+
+       if ($cache == "")
+               return("");
+
+       if (!is_dir($cache))
+               return("");
+
+       $subfolder = $cache."/".substr($file, 0, 2);
+
+       $cachepath = $subfolder."/".$file;
+
+       if ($writemode) {
+               if (!is_dir($subfolder)) {
+                       mkdir($subfolder);
+                       chmod($subfolder, 0777);
+               }
+       }
+
+       return($cachepath);
+}
+
+function clear_cache($basepath = "", $path = "") {
+       if ($path == "") {
+               $basepath = get_config('system','itemcache');
+               $path = $basepath;
+       }
+
+       if (($path == "") OR (!is_dir($path)))
+               return;
+
+       if (substr(realpath($path), 0, strlen($basepath)) != $basepath)
+               return;
+
+       $cachetime = (int)get_config('system','itemcache_duration');
+       if ($cachetime == 0)
+               $cachetime = 86400;
+
+       if ($dh = opendir($path)) {
+               while (($file = readdir($dh)) !== false) {
+                       $fullpath = $path."/".$file;
+                       if ((filetype($fullpath) == "dir") and ($file != ".") and ($file != ".."))
+                               clear_cache($basepath, $fullpath);
+                       if ((filetype($fullpath) == "file") and filectime($fullpath) < (time() - $cachetime))
+                               unlink($fullpath);
+               }
+               closedir($dh);
+       }
+}
index 0e1a63792f5a04676cf3f23b00123c5cd7aad12b..edd03d557acc2bb28066dab678e71cf1c671ade7 100644 (file)
@@ -823,15 +823,13 @@ function scale_external_images($s, $include_link = true, $scale_replace = false)
                                $scaled = $mtch[1];
                        $i = fetch_url($scaled);
 
-                       $cache = get_config('system','itemcache');
-                       if (($cache != '') and is_dir($cache)) {
-                               $cachefile = $cache."/".hash("md5", $scaled);
+                       $cachefile = get_cachefile(hash("md5", $scaled));
+                       if ($cachefile != '')
                                file_put_contents($cachefile, $i);
-                       }
 
                        // guess mimetype from headers or filename
                        $type = guess_image_type($mtch[1],true);
-                       
+
                        if($i) {
                                $ph = new Photo($i, $type);
                                if($ph->is_valid()) {
index d5efa36a832b42544b9840ba134589d14286c75b..6f2eeed822b80f757adafaf269caac0c656ee91e 100644 (file)
@@ -102,18 +102,8 @@ function poller_run(&$argv, &$argc){
        // clear old cache
        Cache::clear();
 
-       // clear item cache files if they are older than one day
-       $cache = get_config('system','itemcache');
-       if (($cache != '') and is_dir($cache)) {
-               if ($dh = opendir($cache)) {
-                       while (($file = readdir($dh)) !== false) {
-                               $fullpath = $cache."/".$file;
-                               if ((filetype($fullpath) == "file") and filectime($fullpath) < (time() - 86400))
-                                       unlink($fullpath);
-                       }
-                       closedir($dh);
-               }
-       }
+       // clear old item cache files
+       clear_cache();
 
        $manual_id  = 0;
        $generation = 0;
@@ -128,7 +118,7 @@ function poller_run(&$argv, &$argc){
                $restart = true;
                $generation = intval($argv[2]);
                if(! $generation)
-                       killme();               
+                       killme();
        }
 
        if(($argc > 1) && intval($argv[1])) {
index 044a1f8926984fc96950d8073fe71540b9f991a6..a326412411703947bc1c827c5853c0f4bf4a6c47 100644 (file)
@@ -962,13 +962,11 @@ if(! function_exists('prepare_body')) {
 function prepare_body($item,$attach = false) {
 
        $a = get_app();
-       call_hooks('prepare_body_init', $item); 
+       call_hooks('prepare_body_init', $item);
 
-       $cache = get_config('system','itemcache');
-
-       if (($cache != '')) {
-               $cachefile = $cache."/".$item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']);
+       $cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
 
+       if (($cachefile != '')) {
                if (file_exists($cachefile))
                        $s = file_get_contents($cachefile);
                else {
index dee483d835bbda6479376d21a0c5a603f9cf29b1..bd49437e26687a69147b874119a3e96956886728 100644 (file)
@@ -4,30 +4,7 @@ require_once('include/security.php');
 require_once('include/Photo.php');
 
 function photo_init(&$a) {
-
-       // To-Do:
-       // - checking with realpath
-       // - checking permissions
-       /*
-       $cache = get_config('system','itemcache');
-        if (($cache != '') and is_dir($cache)) {
-               $cachefile = $cache."/".$a->argc."-".$a->argv[1]."-".$a->argv[2]."-".$a->argv[3];
-               if (file_exists($cachefile)) {
-                       $data = file_get_contents($cachefile);
-
-                       if(function_exists('header_remove')) {
-                               header_remove('Pragma');
-                               header_remove('pragma');
-                       }
-
-                       header("Content-type: image/jpeg");
-                       header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
-                       header("Cache-Control: max-age=" . (3600*24));
-                       echo $data;
-                       killme();
-                       // NOTREACHED
-               }
-       }*/
+       global $_SERVER;
 
        $prvcachecontrol = false;
 
@@ -50,6 +27,22 @@ function photo_init(&$a) {
                        // NOTREACHED
        }
 
+       //      strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
+       if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
+               header('HTTP/1.1 304 Not Modified');
+               header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
+               header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
+               header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
+               header("Cache-Control: max-age=31536000");
+               if(function_exists('header_remove')) {
+                       header_remove('Last-Modified');
+                       header_remove('Expires');
+                       header_remove('Cache-Control');
+               }
+               exit;
+       }
+
+
        $default = 'images/person-175.jpg';
 
        if(isset($type)) {
@@ -203,10 +196,10 @@ function photo_init(&$a) {
 
        }
        else {
-
-               header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
-               header("Cache-Control: max-age=" . (3600*24));
-
+               header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
+               header('Etag: "'.md5($data).'"');
+               header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
+               header("Cache-Control: max-age=31536000");
        }
        echo $data;
        killme();
diff --git a/view/theme/vier/prv_message.tpl b/view/theme/vier/prv_message.tpl
deleted file mode 100644 (file)
index 23a64f4..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-
-<h3>$header</h3>
-
-<div id="prvmail-wrapper" >
-<form id="prvmail-form" action="message" method="post" >
-
-$parent
-
-<div id="prvmail-to-label">$to</div>
-$select
-
-<div id="prvmail-subject-label">$subject</div>
-<input type="text" size="64" maxlength="255" id="prvmail-subject" name="subject" value="$subjtxt" $readonly tabindex="11" />
-
-<div id="prvmail-message-label">$yourmessage</div>
-<textarea rows="8" cols="72" class="prvmail-text" id="prvmail-text" name="body" tabindex="12">$text</textarea>
-
-
-<div id="prvmail-submit-wrapper" >
-       <input type="submit" id="prvmail-submit" name="submit" value="$submit" tabindex="13" />
-       <div id="prvmail-upload-wrapper" >
-               <div id="prvmail-upload" title="$upload" ><i class="icon-camera-retro icon-large"></i></div>
-       </div> 
-       <div id="prvmail-link-wrapper" >
-               <div id="prvmail-link" title="$insert" onclick="jotGetLink();" ><i class="icon-link icon-large"></i></div>
-       </div> 
-       <div id="prvmail-rotator-wrapper" >
-               <img id="prvmail-rotator" src="images/rotator.gif" alt="$wait" title="$wait" style="display: none;" />
-       </div> 
-</div>
-<div id="prvmail-end"></div>
-</form>
-</div>
index b39422a4aef126e4cea620bece8a7d4321e90b4b..2c4a7c7049618171a04de50c093f1ee035ec2b8f 100644 (file)
@@ -887,8 +887,8 @@ aside #search-text {
   margin: 0px 2px 2px 0px;
 }
 #contact-block .contact-block-link img {
-  widht: 48px;
-  height: 58px;
+  width: 48px;
+  height: 48px;
 }
 .group_selected {
   background: url("../../../view/theme/diabook/icons/selected.png") no-repeat left center;