1<?php
2/**
3 * Smarty Internal Plugin
4 *
5 * @package    Smarty
6 * @subpackage Cacher
7 */
8
9/**
10 * Cache Handler API
11 *
12 * @package    Smarty
13 * @subpackage Cacher
14 * @author     Rodney Rehm
15 */
16abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
17{
18    /**
19     * fetch cached content and its modification time from data source
20     *
21     * @param string  $id         unique cache content identifier
22     * @param string  $name       template name
23     * @param string  $cache_id   cache id
24     * @param string  $compile_id compile id
25     * @param string  $content    cached content
26     * @param integer $mtime      cache modification timestamp (epoch)
27     *
28     * @return void
29     */
30    abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
31
32    /**
33     * Fetch cached content's modification timestamp from data source
34     * {@internal implementing this method is optional.
35     *  Only implement it if modification times can be accessed faster than loading the complete cached content.}}
36     *
37     * @param string $id         unique cache content identifier
38     * @param string $name       template name
39     * @param string $cache_id   cache id
40     * @param string $compile_id compile id
41     *
42     * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
43     */
44    protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
45    {
46        return false;
47    }
48
49    /**
50     * Save content to cache
51     *
52     * @param string       $id         unique cache content identifier
53     * @param string       $name       template name
54     * @param string       $cache_id   cache id
55     * @param string       $compile_id compile id
56     * @param integer|null $exp_time   seconds till expiration or null
57     * @param string       $content    content to cache
58     *
59     * @return boolean      success
60     */
61    abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
62
63    /**
64     * Delete content from cache
65     *
66     * @param string|null  $name       template name
67     * @param string|null  $cache_id   cache id
68     * @param string|null  $compile_id compile id
69     * @param integer|null $exp_time   seconds till expiration time in seconds or null
70     *
71     * @return integer      number of deleted caches
72     */
73    abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
74
75    /**
76     * populate Cached Object with meta data from Resource
77     *
78     * @param Smarty_Template_Cached   $cached    cached object
79     * @param Smarty_Internal_Template $_template template object
80     *
81     * @return void
82     */
83    public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
84    {
85        $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
86        $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
87        $path = $cached->source->uid . $_cache_id . $_compile_id;
88        $cached->filepath = sha1($path);
89        if ($_template->smarty->cache_locking) {
90            $cached->lock_id = sha1('lock.' . $path);
91        }
92        $this->populateTimestamp($cached);
93    }
94
95    /**
96     * populate Cached Object with timestamp and exists from Resource
97     *
98     * @param Smarty_Template_Cached $cached
99     *
100     * @return void
101     */
102    public function populateTimestamp(Smarty_Template_Cached $cached)
103    {
104        $mtime =
105            $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
106        if ($mtime !== null) {
107            $cached->timestamp = $mtime;
108            $cached->exists = !!$cached->timestamp;
109            return;
110        }
111        $timestamp = null;
112        $this->fetch(
113            $cached->filepath,
114            $cached->source->name,
115            $cached->cache_id,
116            $cached->compile_id,
117            $cached->content,
118            $timestamp
119        );
120        $cached->timestamp = isset($timestamp) ? $timestamp : false;
121        $cached->exists = !!$cached->timestamp;
122    }
123
124    /**
125     * Read the cached template and process the header
126     *
127     * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
128     * @param Smarty_Template_Cached    $cached      cached object
129     * @param boolean                   $update      flag if called because cache update
130     *
131     * @return boolean                 true or false if the cached content does not exist
132     */
133    public function process(
134        Smarty_Internal_Template $_smarty_tpl,
135        Smarty_Template_Cached $cached = null,
136        $update = false
137    ) {
138        if (!$cached) {
139            $cached = $_smarty_tpl->cached;
140        }
141        $content = $cached->content ? $cached->content : null;
142        $timestamp = $cached->timestamp ? $cached->timestamp : null;
143        if ($content === null || !$timestamp) {
144            $this->fetch(
145                $_smarty_tpl->cached->filepath,
146                $_smarty_tpl->source->name,
147                $_smarty_tpl->cache_id,
148                $_smarty_tpl->compile_id,
149                $content,
150                $timestamp
151            );
152        }
153        if (isset($content)) {
154            eval('?>' . $content);
155            $cached->content = null;
156            return true;
157        }
158        return false;
159    }
160
161    /**
162     * Write the rendered template output to cache
163     *
164     * @param Smarty_Internal_Template $_template template object
165     * @param string                   $content   content to cache
166     *
167     * @return boolean                  success
168     */
169    public function writeCachedContent(Smarty_Internal_Template $_template, $content)
170    {
171        return $this->save(
172            $_template->cached->filepath,
173            $_template->source->name,
174            $_template->cache_id,
175            $_template->compile_id,
176            $_template->cache_lifetime,
177            $content
178        );
179    }
180
181    /**
182     * Read cached template from cache
183     *
184     * @param Smarty_Internal_Template $_template template object
185     *
186     * @return string|boolean  content
187     */
188    public function readCachedContent(Smarty_Internal_Template $_template)
189    {
190        $content = $_template->cached->content ? $_template->cached->content : null;
191        $timestamp = null;
192        if ($content === null) {
193            $timestamp = null;
194            $this->fetch(
195                $_template->cached->filepath,
196                $_template->source->name,
197                $_template->cache_id,
198                $_template->compile_id,
199                $content,
200                $timestamp
201            );
202        }
203        if (isset($content)) {
204            return $content;
205        }
206        return false;
207    }
208
209    /**
210     * Empty cache
211     *
212     * @param Smarty  $smarty   Smarty object
213     * @param integer $exp_time expiration time (number of seconds, not timestamp)
214     *
215     * @return integer number of cache files deleted
216     */
217    public function clearAll(Smarty $smarty, $exp_time = null)
218    {
219        return $this->delete(null, null, null, $exp_time);
220    }
221
222    /**
223     * Empty cache for a specific template
224     *
225     * @param Smarty  $smarty        Smarty object
226     * @param string  $resource_name template name
227     * @param string  $cache_id      cache id
228     * @param string  $compile_id    compile id
229     * @param integer $exp_time      expiration time (number of seconds, not timestamp)
230     *
231     * @return int number of cache files deleted
232     * @throws \SmartyException
233     */
234    public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
235    {
236        $cache_name = null;
237        if (isset($resource_name)) {
238            $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
239            if ($source->exists) {
240                $cache_name = $source->name;
241            } else {
242                return 0;
243            }
244        }
245        return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
246    }
247
248    /**
249     * Check is cache is locked for this template
250     *
251     * @param Smarty                 $smarty Smarty object
252     * @param Smarty_Template_Cached $cached cached object
253     *
254     * @return boolean               true or false if cache is locked
255     */
256    public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
257    {
258        $id = $cached->lock_id;
259        $name = $cached->source->name . '.lock';
260        $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
261        if ($mtime === null) {
262            $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
263        }
264        return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
265    }
266
267    /**
268     * Lock cache for this template
269     *
270     * @param Smarty                 $smarty Smarty object
271     * @param Smarty_Template_Cached $cached cached object
272     *
273     * @return bool|void
274     */
275    public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
276    {
277        $cached->is_locked = true;
278        $id = $cached->lock_id;
279        $name = $cached->source->name . '.lock';
280        $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
281    }
282
283    /**
284     * Unlock cache for this template
285     *
286     * @param Smarty                 $smarty Smarty object
287     * @param Smarty_Template_Cached $cached cached object
288     *
289     * @return bool|void
290     */
291    public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
292    {
293        $cached->is_locked = false;
294        $name = $cached->source->name . '.lock';
295        $this->delete($name, $cached->cache_id, $cached->compile_id, null);
296    }
297}
298