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 */ 18class 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 = &$_template->source; 31 $smarty = &$_template->smarty; 32 $_compile_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^'; 33 $_filepath = sha1($source->uid . $smarty->_joined_template_dir); 34 $cached->filepath = $smarty->getCacheDir(); 35 if (isset($_template->cache_id)) { 36 $cached->filepath .= preg_replace(array('![^\w|]+!', '![|]+!'), array('_', $_compile_dir_sep), 37 $_template->cache_id) . $_compile_dir_sep; 38 } 39 if (isset($_template->compile_id)) { 40 $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep; 41 } 42 // if use_sub_dirs, break file into directories 43 if ($smarty->use_sub_dirs) { 44 $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $smarty->ds . 45 $_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds; 46 } 47 $cached->filepath .= $_filepath; 48 $basename = $source->handler->getBasename($source); 49 if (!empty($basename)) { 50 $cached->filepath .= '.' . $basename; 51 } 52 if ($smarty->cache_locking) { 53 $cached->lock_id = $cached->filepath . '.lock'; 54 } 55 $cached->filepath .= '.php'; 56 $cached->timestamp = $cached->exists = is_file($cached->filepath); 57 if ($cached->exists) { 58 $cached->timestamp = filemtime($cached->filepath); 59 } 60 } 61 62 /** 63 * populate Cached Object with timestamp and exists from Resource 64 * 65 * @param Smarty_Template_Cached $cached cached object 66 * 67 * @return void 68 */ 69 public function populateTimestamp(Smarty_Template_Cached $cached) 70 { 71 $cached->timestamp = $cached->exists = is_file($cached->filepath); 72 if ($cached->exists) { 73 $cached->timestamp = filemtime($cached->filepath); 74 } 75 } 76 77 /** 78 * Read the cached template and process its header 79 * 80 * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template 81 * @param Smarty_Template_Cached $cached cached object 82 * @param bool $update flag if called because cache update 83 * 84 * @return boolean true or false if the cached content does not exist 85 */ 86 public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, 87 $update = false) 88 { 89 $_smarty_tpl->cached->valid = false; 90 if ($update && defined('HHVM_VERSION')) { 91 eval("?>" . file_get_contents($_smarty_tpl->cached->filepath)); 92 return true; 93 } else { 94 return @include $_smarty_tpl->cached->filepath; 95 } 96 } 97 98 /** 99 * Write the rendered template output to cache 100 * 101 * @param Smarty_Internal_Template $_template template object 102 * @param string $content content to cache 103 * 104 * @return boolean success 105 */ 106 public function writeCachedContent(Smarty_Internal_Template $_template, $content) 107 { 108 if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content, 109 $_template->smarty) === true 110 ) { 111 if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) { 112 opcache_invalidate($_template->cached->filepath, true); 113 } elseif (function_exists('apc_compile_file')) { 114 apc_compile_file($_template->cached->filepath); 115 } 116 $cached = $_template->cached; 117 $cached->timestamp = $cached->exists = is_file($cached->filepath); 118 if ($cached->exists) { 119 $cached->timestamp = filemtime($cached->filepath); 120 return true; 121 } 122 } 123 return false; 124 } 125 126 /** 127 * Read cached template from cache 128 * 129 * @param Smarty_Internal_Template $_template template object 130 * 131 * @return string content 132 */ 133 public function readCachedContent(Smarty_Internal_Template $_template) 134 { 135 if (is_file($_template->cached->filepath)) { 136 return file_get_contents($_template->cached->filepath); 137 } 138 return false; 139 } 140 141 /** 142 * Empty cache 143 * 144 * @param Smarty $smarty 145 * @param integer $exp_time expiration time (number of seconds, not timestamp) 146 * 147 * @return integer number of cache files deleted 148 */ 149 public function clearAll(Smarty $smarty, $exp_time = null) 150 { 151 return $smarty->ext->_cacheResourceFile->clear($smarty, null, null, null, $exp_time); 152 } 153 154 /** 155 * Empty cache for a specific template 156 * 157 * @param Smarty $smarty 158 * @param string $resource_name template name 159 * @param string $cache_id cache id 160 * @param string $compile_id compile id 161 * @param integer $exp_time expiration time (number of seconds, not timestamp) 162 * 163 * @return integer number of cache files deleted 164 */ 165 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) 166 { 167 return $smarty->ext->_cacheResourceFile->clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time); 168 } 169 170 /** 171 * Check is cache is locked for this template 172 * 173 * @param Smarty $smarty Smarty object 174 * @param Smarty_Template_Cached $cached cached object 175 * 176 * @return boolean true or false if cache is locked 177 */ 178 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) 179 { 180 if (version_compare(PHP_VERSION, '5.3.0', '>=')) { 181 clearstatcache(true, $cached->lock_id); 182 } else { 183 clearstatcache(); 184 } 185 if (is_file($cached->lock_id)) { 186 $t = filemtime($cached->lock_id); 187 return $t && (time() - $t < $smarty->locking_timeout); 188 } else { 189 return false; 190 } 191 } 192 193 /** 194 * Lock cache for this template 195 * 196 * @param Smarty $smarty Smarty object 197 * @param Smarty_Template_Cached $cached cached object 198 * 199 * @return bool|void 200 */ 201 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) 202 { 203 $cached->is_locked = true; 204 touch($cached->lock_id); 205 } 206 207 /** 208 * Unlock cache for this template 209 * 210 * @param Smarty $smarty Smarty object 211 * @param Smarty_Template_Cached $cached cached object 212 * 213 * @return bool|void 214 */ 215 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) 216 { 217 $cached->is_locked = false; 218 @unlink($cached->lock_id); 219 } 220} 221