smarty_internal_cacheresource_file.php (11969B)
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 * Implements the file system as resource for the HTML cache Version ussing nocache inserts. 14 * 15 * @package Smarty 16 * @subpackage Cacher 17 */ 18 class Smarty_Internal_CacheResource_File extends Smarty_CacheResource 19 { 20 /** 21 * populate Cached Object with meta data from Resource 22 * 23 * @param Smarty_Template_Cached $cached cached object 24 * @param Smarty_Internal_Template $_template template object 25 * 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 = $cached->exists = is_file($cached->filepath); 65 if ($cached->exists) { 66 $cached->timestamp = filemtime($cached->filepath); 67 } 68 } 69 70 /** 71 * populate Cached Object with timestamp and exists from Resource 72 * 73 * @param Smarty_Template_Cached $cached cached object 74 * 75 * @return void 76 */ 77 public function populateTimestamp(Smarty_Template_Cached $cached) 78 { 79 $cached->timestamp = $cached->exists = is_file($cached->filepath); 80 if ($cached->exists) { 81 $cached->timestamp = filemtime($cached->filepath); 82 } 83 } 84 85 /** 86 * Read the cached template and process its header 87 * 88 * @param Smarty_Internal_Template $_template template object 89 * @param Smarty_Template_Cached $cached cached object 90 * 91 * @return booleantrue or false if the cached content does not exist 92 */ 93 public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null) 94 { 95 /** @var Smarty_Internal_Template $_smarty_tpl 96 * used in included file 97 */ 98 $_smarty_tpl = $_template; 99 100 return @include $_template->cached->filepath; 101 } 102 103 /** 104 * Write the rendered template output to cache 105 * 106 * @param Smarty_Internal_Template $_template template object 107 * @param string $content content to cache 108 * 109 * @return boolean success 110 */ 111 public function writeCachedContent(Smarty_Internal_Template $_template, $content) 112 { 113 $obj = new Smarty_Internal_Write_File(); 114 if ($obj->writeFile($_template->cached->filepath, $content, $_template->smarty) === true) { 115 $cached = $_template->cached; 116 $cached->timestamp = $cached->exists = is_file($cached->filepath); 117 if ($cached->exists) { 118 $cached->timestamp = filemtime($cached->filepath); 119 return true; 120 } 121 } 122 return false; 123 } 124 125 /** 126 * Read cached template from cache 127 * 128 * @param Smarty_Internal_Template $_template template object 129 * 130 * @return string content 131 */ 132 public function readCachedContent(Smarty_Internal_Template $_template) 133 { 134 if (is_file($_template->cached->filepath)) { 135 return file_get_contents($_template->cached->filepath); 136 } 137 return false; 138 } 139 140 /** 141 * Empty cache 142 * 143 * @param Smarty $smarty 144 * @param integer $exp_time expiration time (number of seconds, not timestamp) 145 * 146 * @return integer number of cache files deleted 147 */ 148 public function clearAll(Smarty $smarty, $exp_time = null) 149 { 150 return $this->clear($smarty, null, null, null, $exp_time); 151 } 152 153 /** 154 * Empty cache for a specific template 155 * 156 * @param Smarty $smarty 157 * @param string $resource_name template name 158 * @param string $cache_id cache id 159 * @param string $compile_id compile id 160 * @param integer $exp_time expiration time (number of seconds, not timestamp) 161 * 162 * @return integer number of cache files deleted 163 */ 164 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) 165 { 166 $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; 167 $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; 168 $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; 169 $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0; 170 $_dir = realpath($smarty->getCacheDir()) . '/'; 171 if ($_dir == '/') { //We should never want to delete this! 172 return 0; 173 } 174 $_dir_length = strlen($_dir); 175 if (isset($_cache_id)) { 176 $_cache_id_parts = explode('|', $_cache_id); 177 $_cache_id_parts_count = count($_cache_id_parts); 178 if ($smarty->use_sub_dirs) { 179 foreach ($_cache_id_parts as $id_part) { 180 $_dir .= $id_part . DS; 181 } 182 } 183 } 184 if (isset($resource_name)) { 185 $_save_stat = $smarty->caching; 186 $smarty->caching = true; 187 $tpl = new $smarty->template_class($resource_name, $smarty); 188 $smarty->caching = $_save_stat; 189 190 // remove from template cache 191 $tpl->source; // have the template registered before unset() 192 if ($smarty->allow_ambiguous_resources) { 193 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; 194 } else { 195 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; 196 } 197 if (isset($_templateId[150])) { 198 $_templateId = sha1($_templateId); 199 } 200 unset($smarty->template_objects[$_templateId]); 201 202 if ($tpl->source->exists) { 203 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); 204 } else { 205 return 0; 206 } 207 } 208 $_count = 0; 209 $_time = time(); 210 if (file_exists($_dir)) { 211 $_cacheDirs = new RecursiveDirectoryIterator($_dir); 212 $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); 213 foreach ($_cache as $_file) { 214 if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { 215 continue; 216 } 217 // directory ? 218 if ($_file->isDir()) { 219 if (!$_cache->isDot()) { 220 // delete folder if empty 221 @rmdir($_file->getPathname()); 222 } 223 } else { 224 $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length))); 225 $_parts_count = count($_parts); 226 // check name 227 if (isset($resource_name)) { 228 if ($_parts[$_parts_count - 1] != $_resourcename_parts) { 229 continue; 230 } 231 } 232 // check compile id 233 if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) { 234 continue; 235 } 236 // check cache id 237 if (isset($_cache_id)) { 238 // count of cache id parts 239 $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset; 240 if ($_parts_count < $_cache_id_parts_count) { 241 continue; 242 } 243 for ($i = 0; $i < $_cache_id_parts_count; $i ++) { 244 if ($_parts[$i] != $_cache_id_parts[$i]) { 245 continue 2; 246 } 247 } 248 } 249 // expired ? 250 if (isset($exp_time)) { 251 if ($exp_time < 0) { 252 preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match); 253 if ($_time < (@filemtime($_file) + $match[1])) { 254 continue; 255 } 256 } else { 257 if ($_time - @filemtime($_file) < $exp_time) { 258 continue; 259 } 260 } 261 } 262 $_count += @unlink((string) $_file) ? 1 : 0; 263 } 264 } 265 } 266 267 return $_count; 268 } 269 270 /** 271 * Check is cache is locked for this template 272 * 273 * @param Smarty $smarty Smarty object 274 * @param Smarty_Template_Cached $cached cached object 275 * 276 * @return boolean true or false if cache is locked 277 */ 278 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) 279 { 280 if (version_compare(PHP_VERSION, '5.3.0', '>=')) { 281 clearstatcache(true, $cached->lock_id); 282 } else { 283 clearstatcache(); 284 } 285 if (is_file($cached->lock_id)) { 286 $t = @filemtime($cached->lock_id); 287 return $t && (time() - $t < $smarty->locking_timeout); 288 } else { 289 return false; 290 } 291 } 292 293 /** 294 * Lock cache for this template 295 * 296 * @param Smarty $smarty Smarty object 297 * @param Smarty_Template_Cached $cached cached object 298 * 299 * @return bool|void 300 */ 301 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) 302 { 303 $cached->is_locked = true; 304 touch($cached->lock_id); 305 } 306 307 /** 308 * Unlock cache for this template 309 * 310 * @param Smarty $smarty Smarty object 311 * @param Smarty_Template_Cached $cached cached object 312 * 313 * @return bool|void 314 */ 315 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) 316 { 317 $cached->is_locked = false; 318 @unlink($cached->lock_id); 319 } 320 }