summaryrefslogtreecommitdiff
path: root/lib/smarty3/sysplugins/smarty_internal_runtime_updatecache.php
blob: c1abbb321a0e2ee13dcaca52e2f1442ec7bd13ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

/**
 * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
 *
 * @package    Smarty
 * @subpackage PluginsInternal
 * @author     Uwe Tews
 **/
class Smarty_Internal_Runtime_UpdateCache
{
    /**
     * check client side cache
     *
     * @param \Smarty_Template_Cached  $cached
     * @param Smarty_Internal_Template $_template
     * @param string                   $content
     */
    public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
    {
    }

    /**
     * Cache was invalid , so render from compiled and write to cache
     *
     * @param \Smarty_Template_Cached   $cached
     * @param \Smarty_Internal_Template $_template
     * @param                           $no_output_filter
     *
     * @throws \Exception
     */
    public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
    {
        ob_start();
        if (!isset($_template->compiled)) {
            $_template->loadCompiled();
        }
        $_template->compiled->render($_template);
        if ($_template->smarty->debugging) {
            $_template->smarty->_debug->start_cache($_template);
        }
        $this->removeNoCacheHash($cached, $_template, $no_output_filter);
        $compile_check = (int)$_template->compile_check;
        $_template->compile_check = Smarty::COMPILECHECK_OFF;
        if ($_template->_isSubTpl()) {
            $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
        }
        if (!$_template->cached->processed) {
            $_template->cached->process($_template, true);
        }
        $_template->compile_check = $compile_check;
        $cached->getRenderedTemplateCode($_template);
        if ($_template->smarty->debugging) {
            $_template->smarty->_debug->end_cache($_template);
        }
    }

    /**
     * Sanitize content and write it to cache resource
     *
     * @param \Smarty_Template_Cached  $cached
     * @param Smarty_Internal_Template $_template
     * @param bool                     $no_output_filter
     *
     * @throws \SmartyException
     */
    public function removeNoCacheHash(
        Smarty_Template_Cached $cached,
        Smarty_Internal_Template $_template,
        $no_output_filter
    ) {
        $php_pattern = '/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/';
        $content = ob_get_clean();
        $hash_array = $cached->hashes;
        $hash_array[ $_template->compiled->nocache_hash ] = true;
        $hash_array = array_keys($hash_array);
        $nocache_hash = '(' . implode('|', $hash_array) . ')';
        $_template->cached->has_nocache_code = false;
        // get text between non-cached items
        $cache_split =
            preg_split(
                "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
                $content
            );
        // get non-cached items
        preg_match_all(
            "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
            $content,
            $cache_parts
        );
        $content = '';
        // loop over items, stitch back together
        foreach ($cache_split as $curr_idx => $curr_split) {
            if (preg_match($php_pattern, $curr_split)) {
                // escape PHP tags in template content
                $php_split = preg_split(
                    $php_pattern,
                    $curr_split
                );
                preg_match_all(
                    $php_pattern,
                    $curr_split,
                    $php_parts
                );
                foreach ($php_split as $idx_php => $curr_php) {
                    $content .= $curr_php;
                    if (isset($php_parts[ 0 ][ $idx_php ])) {
                        $content .= "<?php echo '{$php_parts[ 1 ][ $idx_php ]}'; ?>\n";
                    }
                }
            } else {
                $content .= $curr_split;
            }
            if (isset($cache_parts[ 0 ][ $curr_idx ])) {
                $_template->cached->has_nocache_code = true;
                $content .= $cache_parts[ 2 ][ $curr_idx ];
            }
        }
        if (!$no_output_filter && !$_template->cached->has_nocache_code
            && (isset($_template->smarty->autoload_filters[ 'output' ])
                || isset($_template->smarty->registered_filters[ 'output' ]))
        ) {
            $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
        }
        // write cache file content
        $this->writeCachedContent($_template, $content);
    }

    /**
     * Writes the content to cache resource
     *
     * @param Smarty_Internal_Template $_template
     * @param string                   $content
     *
     * @return bool
     */
    public function writeCachedContent(Smarty_Internal_Template $_template, $content)
    {
        if ($_template->source->handler->recompiled || !$_template->caching
        ) {
            // don't write cache file
            return false;
        }
        if (!isset($_template->cached)) {
            $_template->loadCached();
        }
        $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
        return $this->write($_template, $content);
    }

    /**
     * Write this cache object to handler
     *
     * @param Smarty_Internal_Template $_template template object
     * @param string                   $content   content to cache
     *
     * @return bool success
     */
    public function write(Smarty_Internal_Template $_template, $content)
    {
        if (!$_template->source->handler->recompiled) {
            $cached = $_template->cached;
            if ($cached->handler->writeCachedContent($_template, $content)) {
                $cached->content = null;
                $cached->timestamp = time();
                $cached->exists = true;
                $cached->valid = true;
                $cached->cache_lifetime = $_template->cache_lifetime;
                $cached->processed = false;
                if ($_template->smarty->cache_locking) {
                    $cached->handler->releaseLock($_template->smarty, $cached);
                }
                return true;
            }
            $cached->content = null;
            $cached->timestamp = false;
            $cached->exists = false;
            $cached->valid = false;
            $cached->processed = false;
        }
        return false;
    }
}