You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_cacheresource_file.php 7.9KB

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