1<?php 2 /** 3 * Smarty Internal Plugin CacheResource File 4 * 5 * @package Smarty 6 * @subpackage Cacher 7 * @author Uwe Tews 8 * @author Rodney Rehm 9 */ 10 11 /** 12 * This class does contain all necessary methods for the HTML cache on file system 13 * 14 * Implements the file system as resource for the HTML cache Version ussing nocache inserts. 15 * 16 * @package Smarty 17 * @subpackage Cacher 18 */ 19 class Smarty_Internal_CacheResource_File extends Smarty_CacheResource 20 { 21 /** 22 * populate Cached Object with meta data from Resource 23 * 24 * @param Smarty_Template_Cached $cached cached object 25 * @param Smarty_Internal_Template $_template template object 26 * @return void 27 */ 28 public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) 29 { 30 $_source_file_path = str_replace(':', '.', $_template->source->filepath); 31 $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null; 32 $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null; 33 $_filepath = $_template->source->uid; 34 // if use_sub_dirs, break file into directories 35 if ($_template->smarty->use_sub_dirs) { 36 $_filepath = substr($_filepath, 0, 2) . DS 37 . substr($_filepath, 2, 2) . DS 38 . substr($_filepath, 4, 2) . DS 39 . $_filepath; 40 } 41 $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; 42 if (isset($_cache_id)) { 43 $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep; 44 } else { 45 $_cache_id = ''; 46 } 47 if (isset($_compile_id)) { 48 $_compile_id = $_compile_id . $_compile_dir_sep; 49 } else { 50 $_compile_id = ''; 51 } 52 $_cache_dir = $_template->smarty->getCacheDir(); 53 if ($_template->smarty->cache_locking) { 54 // create locking file name 55 // relative file name? 56 if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) { 57 $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir; 58 } else { 59 $_lock_dir = $_cache_dir; 60 } 61 $cached->lock_id = $_lock_dir.sha1($_cache_id.$_compile_id.$_template->source->uid).'.lock'; 62 } 63 $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php'; 64 $cached->timestamp = @filemtime($cached->filepath); 65 $cached->exists = !!$cached->timestamp; 66 } 67 68 /** 69 * populate Cached Object with timestamp and exists from Resource 70 * 71 * @param Smarty_Template_Cached $cached cached object 72 * @return void 73 */ 74 public function populateTimestamp(Smarty_Template_Cached $cached) 75 { 76 $cached->timestamp = @filemtime($cached->filepath); 77 $cached->exists = !!$cached->timestamp; 78 } 79 80 /** 81 * Read the cached template and process its header 82 * 83 * @param Smarty_Internal_Template $_template template object 84 * @param Smarty_Template_Cached $cached cached object 85 * @return booelan true or false if the cached content does not exist 86 */ 87 public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null) 88 { 89 $_smarty_tpl = $_template; 90 91 return @include $_template->cached->filepath; 92 } 93 94 /** 95 * Write the rendered template output to cache 96 * 97 * @param Smarty_Internal_Template $_template template object 98 * @param string $content content to cache 99 * @return boolean success 100 */ 101 public function writeCachedContent(Smarty_Internal_Template $_template, $content) 102 { 103 if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) { 104 $_template->cached->timestamp = @filemtime($_template->cached->filepath); 105 $_template->cached->exists = !!$_template->cached->timestamp; 106 if ($_template->cached->exists) { 107 return true; 108 } 109 } 110 111 return false; 112 } 113 114 /** 115 * Empty cache 116 * 117 * @param Smarty_Internal_Template $_template template object 118 * @param integer $exp_time expiration time (number of seconds, not timestamp) 119 * @return integer number of cache files deleted 120 */ 121 public function clearAll(Smarty $smarty, $exp_time = null) 122 { 123 return $this->clear($smarty, null, null, null, $exp_time); 124 } 125 126 /** 127 * Empty cache for a specific template 128 * 129 * @param Smarty $_template template object 130 * @param string $resource_name template name 131 * @param string $cache_id cache id 132 * @param string $compile_id compile id 133 * @param integer $exp_time expiration time (number of seconds, not timestamp) 134 * @return integer number of cache files deleted 135 */ 136 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) 137 { 138 $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; 139 $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; 140 $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; 141 $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0; 142 $_dir = $smarty->getCacheDir(); 143 $_dir_length = strlen($_dir); 144 if (isset($_cache_id)) { 145 $_cache_id_parts = explode('|', $_cache_id); 146 $_cache_id_parts_count = count($_cache_id_parts); 147 if ($smarty->use_sub_dirs) { 148 foreach ($_cache_id_parts as $id_part) { 149 $_dir .= $id_part . DS; 150 } 151 } 152 } 153 if (isset($resource_name)) { 154 $_save_stat = $smarty->caching; 155 $smarty->caching = true; 156 $tpl = new $smarty->template_class($resource_name, $smarty); 157 $smarty->caching = $_save_stat; 158 159 // remove from template cache 160 $tpl->source; // have the template registered before unset() 161 if ($smarty->allow_ambiguous_resources) { 162 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; 163 } else { 164 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; 165 } 166 if (isset($_templateId[150])) { 167 $_templateId = sha1($_templateId); 168 } 169 unset($smarty->template_objects[$_templateId]); 170 171 if ($tpl->source->exists) { 172 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); 173 } else { 174 return 0; 175 } 176 } 177 $_count = 0; 178 $_time = time(); 179 if (file_exists($_dir)) { 180 $_cacheDirs = new RecursiveDirectoryIterator($_dir); 181 $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); 182 foreach ($_cache as $_file) { 183 if (substr(basename($_file->getPathname()),0,1) == '.' || strpos($_file, '.svn') !== false || strpos($_file, '.git') !== false) continue; 184 // directory ? 185 if ($_file->isDir()) { 186 if (!$_cache->isDot()) { 187 // delete folder if empty 188 @rmdir($_file->getPathname()); 189 } 190 } else { 191 $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length))); 192 $_parts_count = count($_parts); 193 // check name 194 if (isset($resource_name)) { 195 if ($_parts[$_parts_count-1] != $_resourcename_parts) { 196 continue; 197 } 198 } 199 // check compile id 200 if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) { 201 continue; 202 } 203 // check cache id 204 if (isset($_cache_id)) { 205 // count of cache id parts 206 $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset; 207 if ($_parts_count < $_cache_id_parts_count) { 208 continue; 209 } 210 for ($i = 0; $i < $_cache_id_parts_count; $i++) { 211 if ($_parts[$i] != $_cache_id_parts[$i]) continue 2; 212 } 213 } 214 // expired ? 215 if (isset($exp_time)) { 216 if ($exp_time < 0) { 217 preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match); 218 if ($_time < (@filemtime($_file) + $match[1])) { 219 continue; 220 } 221 } else { 222 if ($_time - @filemtime($_file) < $exp_time) { 223 continue; 224 } 225 } 226 } 227 $_count += @unlink((string) $_file) ? 1 : 0; 228 } 229 } 230 } 231 232 return $_count; 233 } 234 235 /** 236 * Check is cache is locked for this template 237 * 238 * @param Smarty $smarty Smarty object 239 * @param Smarty_Template_Cached $cached cached object 240 * @return booelan true or false if cache is locked 241 */ 242 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) 243 { 244 if (version_compare(PHP_VERSION, '5.3.0', '>=')) { 245 clearstatcache(true, $cached->lock_id); 246 } else { 247 clearstatcache(); 248 } 249 $t = @filemtime($cached->lock_id); 250 251 return $t && (time() - $t < $smarty->locking_timeout); 252 } 253 254 /** 255 * Lock cache for this template 256 * 257 * @param Smarty $smarty Smarty object 258 * @param Smarty_Template_Cached $cached cached object 259 */ 260 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) 261 { 262 $cached->is_locked = true; 263 touch($cached->lock_id); 264 } 265 266 /** 267 * Unlock cache for this template 268 * 269 * @param Smarty $smarty Smarty object 270 * @param Smarty_Template_Cached $cached cached object 271 */ 272 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) 273 { 274 $cached->is_locked = false; 275 @unlink($cached->lock_id); 276 } 277 } 278