1: <?php
2:
3: /**
4: * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
5: *
6: * @package Smarty
7: * @subpackage PluginsInternal
8: * @author Uwe Tews
9: **/
10: class Smarty_Internal_Runtime_UpdateCache
11: {
12: /**
13: * check client side cache
14: *
15: * @param \Smarty_Template_Cached $cached
16: * @param Smarty_Internal_Template $_template
17: * @param string $content
18: */
19: public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
20: {
21: }
22:
23: /**
24: * Cache was invalid , so render from compiled and write to cache
25: *
26: * @param \Smarty_Template_Cached $cached
27: * @param \Smarty_Internal_Template $_template
28: * @param $no_output_filter
29: *
30: * @throws \Exception
31: */
32: public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
33: {
34: ob_start();
35: if (!isset($_template->compiled)) {
36: $_template->loadCompiled();
37: }
38: $_template->compiled->render($_template);
39: if ($_template->smarty->debugging) {
40: $_template->smarty->_debug->start_cache($_template);
41: }
42: $this->removeNoCacheHash($cached, $_template, $no_output_filter);
43: $compile_check = (int)$_template->compile_check;
44: $_template->compile_check = Smarty::COMPILECHECK_OFF;
45: if ($_template->_isSubTpl()) {
46: $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
47: }
48: if (!$_template->cached->processed) {
49: $_template->cached->process($_template, true);
50: }
51: $_template->compile_check = $compile_check;
52: $cached->getRenderedTemplateCode($_template);
53: if ($_template->smarty->debugging) {
54: $_template->smarty->_debug->end_cache($_template);
55: }
56: }
57:
58: /**
59: * Sanitize content and write it to cache resource
60: *
61: * @param \Smarty_Template_Cached $cached
62: * @param Smarty_Internal_Template $_template
63: * @param bool $no_output_filter
64: *
65: * @throws \SmartyException
66: */
67: public function removeNoCacheHash(
68: Smarty_Template_Cached $cached,
69: Smarty_Internal_Template $_template,
70: $no_output_filter
71: ) {
72: $php_pattern = '/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/';
73: $content = ob_get_clean();
74: $hash_array = $cached->hashes;
75: $hash_array[ $_template->compiled->nocache_hash ] = true;
76: $hash_array = array_keys($hash_array);
77: $nocache_hash = '(' . implode('|', $hash_array) . ')';
78: $_template->cached->has_nocache_code = false;
79: // get text between non-cached items
80: $cache_split =
81: preg_split(
82: "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
83: $content
84: );
85: // get non-cached items
86: preg_match_all(
87: "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
88: $content,
89: $cache_parts
90: );
91: $content = '';
92: // loop over items, stitch back together
93: foreach ($cache_split as $curr_idx => $curr_split) {
94: if (preg_match($php_pattern, $curr_split)) {
95: // escape PHP tags in template content
96: $php_split = preg_split(
97: $php_pattern,
98: $curr_split
99: );
100: preg_match_all(
101: $php_pattern,
102: $curr_split,
103: $php_parts
104: );
105: foreach ($php_split as $idx_php => $curr_php) {
106: $content .= $curr_php;
107: if (isset($php_parts[ 0 ][ $idx_php ])) {
108: $content .= "<?php echo '{$php_parts[ 1 ][ $idx_php ]}'; ?>\n";
109: }
110: }
111: } else {
112: $content .= $curr_split;
113: }
114: if (isset($cache_parts[ 0 ][ $curr_idx ])) {
115: $_template->cached->has_nocache_code = true;
116: $content .= $cache_parts[ 2 ][ $curr_idx ];
117: }
118: }
119: if (!$no_output_filter && !$_template->cached->has_nocache_code
120: && (isset($_template->smarty->autoload_filters[ 'output' ])
121: || isset($_template->smarty->registered_filters[ 'output' ]))
122: ) {
123: $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
124: }
125: // write cache file content
126: $this->writeCachedContent($_template, $content);
127: }
128:
129: /**
130: * Writes the content to cache resource
131: *
132: * @param Smarty_Internal_Template $_template
133: * @param string $content
134: *
135: * @return bool
136: */
137: public function writeCachedContent(Smarty_Internal_Template $_template, $content)
138: {
139: if ($_template->source->handler->recompiled || !$_template->caching
140: ) {
141: // don't write cache file
142: return false;
143: }
144: if (!isset($_template->cached)) {
145: $_template->loadCached();
146: }
147: $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
148: return $this->write($_template, $content);
149: }
150:
151: /**
152: * Write this cache object to handler
153: *
154: * @param Smarty_Internal_Template $_template template object
155: * @param string $content content to cache
156: *
157: * @return bool success
158: */
159: public function write(Smarty_Internal_Template $_template, $content)
160: {
161: if (!$_template->source->handler->recompiled) {
162: $cached = $_template->cached;
163: if ($cached->handler->writeCachedContent($_template, $content)) {
164: $cached->content = null;
165: $cached->timestamp = time();
166: $cached->exists = true;
167: $cached->valid = true;
168: $cached->cache_lifetime = $_template->cache_lifetime;
169: $cached->processed = false;
170: if ($_template->smarty->cache_locking) {
171: $cached->handler->releaseLock($_template->smarty, $cached);
172: }
173: return true;
174: }
175: $cached->content = null;
176: $cached->timestamp = false;
177: $cached->exists = false;
178: $cached->valid = false;
179: $cached->processed = false;
180: }
181: return false;
182: }
183: }
184: