core.write_cache_file.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Prepend the cache information to the cache file
  9. * and write it
  10. *
  11. * @param string $tpl_file
  12. * @param string $cache_id
  13. * @param string $compile_id
  14. * @param string $results
  15. * @return true|null
  16. */
  17. // $tpl_file, $cache_id, $compile_id, $results
  18. function smarty_core_write_cache_file($params, &$smarty)
  19. {
  20. // put timestamp in cache header
  21. $smarty->_cache_info['timestamp'] = time();
  22. if ($smarty->cache_lifetime > -1){
  23. // expiration set
  24. $smarty->_cache_info['expires'] = $smarty->_cache_info['timestamp'] + $smarty->cache_lifetime;
  25. } else {
  26. // cache will never expire
  27. $smarty->_cache_info['expires'] = -1;
  28. }
  29. // collapse nocache.../nocache-tags
  30. if (preg_match_all('!\{(/?)nocache\:[0-9a-f]{32}#\d+\}!', $params['results'], $match, PREG_PATTERN_ORDER)) {
  31. // remove everything between every pair of outermost noache.../nocache-tags
  32. // and replace it by a single nocache-tag
  33. // this new nocache-tag will be replaced by dynamic contents in
  34. // smarty_core_process_compiled_includes() on a cache-read
  35. $match_count = count($match[0]);
  36. $results = preg_split('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!', $params['results'], -1, PREG_SPLIT_DELIM_CAPTURE);
  37. $level = 0;
  38. $j = 0;
  39. for ($i=0, $results_count = count($results); $i < $results_count && $j < $match_count; $i++) {
  40. if ($results[$i] == $match[0][$j]) {
  41. // nocache tag
  42. if ($match[1][$j]) { // closing tag
  43. $level--;
  44. unset($results[$i]);
  45. } else { // opening tag
  46. if ($level++ > 0) unset($results[$i]);
  47. }
  48. $j++;
  49. } elseif ($level > 0) {
  50. unset($results[$i]);
  51. }
  52. }
  53. $params['results'] = implode('', $results);
  54. }
  55. $smarty->_cache_info['cache_serials'] = $smarty->_cache_serials;
  56. // prepend the cache header info into cache file
  57. $_cache_info = serialize($smarty->_cache_info);
  58. $params['results'] = strlen($_cache_info) . "\n" . $_cache_info . $params['results'];
  59. if (!empty($smarty->cache_handler_func)) {
  60. // use cache_handler function
  61. call_user_func_array($smarty->cache_handler_func,
  62. array('write', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
  63. } else {
  64. // use local cache file
  65. if(!@is_writable($smarty->cache_dir)) {
  66. // cache_dir not writable, see if it exists
  67. if(!@is_dir($smarty->cache_dir)) {
  68. $smarty->trigger_error('the $cache_dir \'' . $smarty->cache_dir . '\' does not exist, or is not a directory.', E_USER_ERROR);
  69. return false;
  70. }
  71. $smarty->trigger_error('unable to write to $cache_dir \'' . realpath($smarty->cache_dir) . '\'. Be sure $cache_dir is writable by the web server user.', E_USER_ERROR);
  72. return false;
  73. }
  74. $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
  75. $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
  76. $_params = array('filename' => $_cache_file, 'contents' => $params['results'], 'create_dirs' => true);
  77. require_once(SMARTY_CORE_DIR . 'core.write_file.php');
  78. smarty_core_write_file($_params, $smarty);
  79. return true;
  80. }
  81. }
  82. /* vim: set expandtab: */
  83. ?>