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