smarty_internal_cacheresource_file.php 7.5 KB

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