smarty_internal_cacheresource_file.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 ? $smarty->ds : '^';
  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. '![|]+!'), array('_',
  37. $_compile_dir_sep),
  38. $_template->cache_id) . $_compile_dir_sep;
  39. }
  40. if (isset($_template->compile_id)) {
  41. $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
  42. }
  43. // if use_sub_dirs, break file into directories
  44. if ($smarty->use_sub_dirs) {
  45. $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] .
  46. $smarty->ds .
  47. $_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
  48. }
  49. $cached->filepath .= $_filepath;
  50. $basename = $source->handler->getBasename($source);
  51. if (!empty($basename)) {
  52. $cached->filepath .= '.' . $basename;
  53. }
  54. if ($smarty->cache_locking) {
  55. $cached->lock_id = $cached->filepath . '.lock';
  56. }
  57. $cached->filepath .= '.php';
  58. $cached->timestamp = $cached->exists = is_file($cached->filepath);
  59. if ($cached->exists) {
  60. $cached->timestamp = filemtime($cached->filepath);
  61. }
  62. }
  63. /**
  64. * populate Cached Object with timestamp and exists from Resource
  65. *
  66. * @param Smarty_Template_Cached $cached cached object
  67. *
  68. * @return void
  69. */
  70. public function populateTimestamp(Smarty_Template_Cached $cached)
  71. {
  72. $cached->timestamp = $cached->exists = is_file($cached->filepath);
  73. if ($cached->exists) {
  74. $cached->timestamp = filemtime($cached->filepath);
  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. * Write the rendered template output to cache
  99. *
  100. * @param Smarty_Internal_Template $_template template object
  101. * @param string $content content to cache
  102. *
  103. * @return boolean success
  104. */
  105. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  106. {
  107. if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
  108. $_template->smarty) === true
  109. ) {
  110. if (function_exists('opcache_invalidate') &&
  111. (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
  112. ) {
  113. opcache_invalidate($_template->cached->filepath, true);
  114. } elseif (function_exists('apc_compile_file')) {
  115. apc_compile_file($_template->cached->filepath);
  116. }
  117. $cached = $_template->cached;
  118. $cached->timestamp = $cached->exists = is_file($cached->filepath);
  119. if ($cached->exists) {
  120. $cached->timestamp = filemtime($cached->filepath);
  121. return true;
  122. }
  123. }
  124. return false;
  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. * 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 $smarty->ext->_cacheResourceFile->clear($smarty, null, null, null, $exp_time);
  151. }
  152. /**
  153. * Empty cache for a specific template
  154. *
  155. * @param Smarty $smarty
  156. * @param string $resource_name template name
  157. * @param string $cache_id cache id
  158. * @param string $compile_id compile id
  159. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  160. *
  161. * @return integer number of cache files deleted
  162. */
  163. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  164. {
  165. return $smarty->ext->_cacheResourceFile->clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time);
  166. }
  167. /**
  168. * Check is cache is locked for this template
  169. *
  170. * @param Smarty $smarty Smarty object
  171. * @param Smarty_Template_Cached $cached cached object
  172. *
  173. * @return boolean true or false if cache is locked
  174. */
  175. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  176. {
  177. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  178. clearstatcache(true, $cached->lock_id);
  179. } else {
  180. clearstatcache();
  181. }
  182. if (is_file($cached->lock_id)) {
  183. $t = filemtime($cached->lock_id);
  184. return $t && (time() - $t < $smarty->locking_timeout);
  185. } else {
  186. return false;
  187. }
  188. }
  189. /**
  190. * Lock cache for this template
  191. *
  192. * @param Smarty $smarty Smarty object
  193. * @param Smarty_Template_Cached $cached cached object
  194. *
  195. * @return bool|void
  196. */
  197. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  198. {
  199. $cached->is_locked = true;
  200. touch($cached->lock_id);
  201. }
  202. /**
  203. * Unlock cache for this template
  204. *
  205. * @param Smarty $smarty Smarty object
  206. * @param Smarty_Template_Cached $cached cached object
  207. *
  208. * @return bool|void
  209. */
  210. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  211. {
  212. $cached->is_locked = false;
  213. @unlink($cached->lock_id);
  214. }
  215. }