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