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.7KB

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_file_path = str_replace(':', '.', $_template->source->filepath);
  30. $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
  31. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w]+!', '_', $_template->compile_id) : null;
  32. $_filepath = $_template->source->uid;
  33. // if use_sub_dirs, break file into directories
  34. if ($_template->smarty->use_sub_dirs) {
  35. $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS .
  36. $_filepath;
  37. }
  38. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  39. if (isset($_cache_id)) {
  40. $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
  41. } else {
  42. $_cache_id = '';
  43. }
  44. if (isset($_compile_id)) {
  45. $_compile_id = $_compile_id . $_compile_dir_sep;
  46. } else {
  47. $_compile_id = '';
  48. }
  49. $_cache_dir = $_template->smarty->getCacheDir();
  50. if ($_template->smarty->cache_locking) {
  51. // create locking file name
  52. // relative file name?
  53. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {
  54. $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;
  55. } else {
  56. $_lock_dir = $_cache_dir;
  57. }
  58. $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock';
  59. }
  60. $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) .
  61. '.php';
  62. $cached->timestamp = $cached->exists = is_file($cached->filepath);
  63. if ($cached->exists) {
  64. $cached->timestamp = filemtime($cached->filepath);
  65. }
  66. }
  67. /**
  68. * populate Cached Object with timestamp and exists from Resource
  69. *
  70. * @param Smarty_Template_Cached $cached cached object
  71. *
  72. * @return void
  73. */
  74. public function populateTimestamp(Smarty_Template_Cached $cached)
  75. {
  76. $cached->timestamp = $cached->exists = is_file($cached->filepath);
  77. if ($cached->exists) {
  78. $cached->timestamp = filemtime($cached->filepath);
  79. }
  80. }
  81. /**
  82. * Read the cached template and process its header
  83. *
  84. * @param Smarty_Internal_Template $_template template object
  85. * @param Smarty_Template_Cached $cached cached object
  86. * @param bool $update flag if called because cache update
  87. *
  88. * @return boolean true or false if the cached content does not exist
  89. */
  90. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false)
  91. {
  92. /** @var Smarty_Internal_Template $_smarty_tpl
  93. * used in included file
  94. */
  95. $_smarty_tpl = $_template;
  96. $_template->cached->valid = false;
  97. if ($update && defined('HHVM_VERSION')) {
  98. return $_template->smarty->ext->_hhvm->includeHhvm($_template, $_template->cached->filepath);
  99. } else {
  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. if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
  114. if (function_exists('opcache_invalidate')) {
  115. opcache_invalidate($_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_Internal_Extension_Clear::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_Internal_Extension_Clear::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. }