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