core.read_cache_file.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * read a cache file, determine if it needs to be
  9. * regenerated or not
  10. *
  11. * @param string $tpl_file
  12. * @param string $cache_id
  13. * @param string $compile_id
  14. * @param string $results
  15. * @return boolean
  16. */
  17. // $tpl_file, $cache_id, $compile_id, &$results
  18. function smarty_core_read_cache_file(&$params, &$smarty)
  19. {
  20. static $content_cache = array();
  21. if ($smarty->force_compile) {
  22. // force compile enabled, always regenerate
  23. return false;
  24. }
  25. if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) {
  26. list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']];
  27. return true;
  28. }
  29. if (!empty($smarty->cache_handler_func)) {
  30. // use cache_handler function
  31. call_user_func_array($smarty->cache_handler_func,
  32. array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
  33. } else {
  34. // use local cache file
  35. $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
  36. $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
  37. $params['results'] = $smarty->_read_file($_cache_file);
  38. }
  39. if (empty($params['results'])) {
  40. // nothing to parse (error?), regenerate cache
  41. return false;
  42. }
  43. $cache_split = explode("\n", $params['results'], 2);
  44. $cache_header = $cache_split[0];
  45. $_cache_info = unserialize($cache_header);
  46. if ($smarty->caching == 2 && isset ($_cache_info['expires'])){
  47. // caching by expiration time
  48. if ($_cache_info['expires'] > -1 && (time() > $_cache_info['expires'])) {
  49. // cache expired, regenerate
  50. return false;
  51. }
  52. } else {
  53. // caching by lifetime
  54. if ($smarty->cache_lifetime > -1 && (time() - $_cache_info['timestamp'] > $smarty->cache_lifetime)) {
  55. // cache expired, regenerate
  56. return false;
  57. }
  58. }
  59. if ($smarty->compile_check) {
  60. $_params = array('get_source' => false, 'quiet'=>true);
  61. foreach (array_keys($_cache_info['template']) as $_template_dep) {
  62. $_params['resource_name'] = $_template_dep;
  63. if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
  64. // template file has changed, regenerate cache
  65. return false;
  66. }
  67. }
  68. if (isset($_cache_info['config'])) {
  69. $_params = array('resource_base_path' => $smarty->config_dir, 'get_source' => false, 'quiet'=>true);
  70. foreach (array_keys($_cache_info['config']) as $_config_dep) {
  71. $_params['resource_name'] = $_config_dep;
  72. if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
  73. // config file has changed, regenerate cache
  74. return false;
  75. }
  76. }
  77. }
  78. }
  79. foreach ($_cache_info['cache_serials'] as $_include_file_path=>$_cache_serial) {
  80. if (empty($smarty->_cache_serials[$_include_file_path])) {
  81. $smarty->_include($_include_file_path, true);
  82. }
  83. if ($smarty->_cache_serials[$_include_file_path] != $_cache_serial) {
  84. /* regenerate */
  85. return false;
  86. }
  87. }
  88. $params['results'] = $cache_split[1];
  89. $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info);
  90. $smarty->_cache_info = $_cache_info;
  91. return true;
  92. }
  93. /* vim: set expandtab: */
  94. ?>