From f17377e6ded9540d31c57166aedd2d177b519331 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 17 Nov 2012 00:50:39 +0100 Subject: [PATCH] New cache system with subdirectories --- boot.php | 51 +++++++++++++++++++++++++++++++++ include/network.php | 8 ++---- include/poller.php | 16 ++--------- include/text.php | 8 ++---- mod/photo.php | 49 ++++++++++++++----------------- view/theme/vier/prv_message.tpl | 33 --------------------- view/theme/vier/style.css | 4 +-- 7 files changed, 83 insertions(+), 86 deletions(-) delete mode 100644 view/theme/vier/prv_message.tpl diff --git a/boot.php b/boot.php index 8e79d6d9be..f69f6fe820 100644 --- 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); + } +} diff --git a/include/network.php b/include/network.php index 0e1a63792f..edd03d557a 100644 --- a/include/network.php +++ b/include/network.php @@ -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()) { diff --git a/include/poller.php b/include/poller.php index d5efa36a83..6f2eeed822 100644 --- a/include/poller.php +++ b/include/poller.php @@ -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])) { diff --git a/include/text.php b/include/text.php index 044a1f8926..a326412411 100644 --- a/include/text.php +++ b/include/text.php @@ -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 { diff --git a/mod/photo.php b/mod/photo.php index dee483d835..bd49437e26 100644 --- a/mod/photo.php +++ b/mod/photo.php @@ -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 index 23a64f4f6a..0000000000 --- a/view/theme/vier/prv_message.tpl +++ /dev/null @@ -1,33 +0,0 @@ - -

$header

- -
-
- -$parent - -
$to
-$select - -
$subject
- - -
$yourmessage
- - - -
- -
-
-
- -
- -
-
-
-
-
diff --git a/view/theme/vier/style.css b/view/theme/vier/style.css index b39422a4ae..2c4a7c7049 100644 --- a/view/theme/vier/style.css +++ b/view/theme/vier/style.css @@ -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;