1<?php
2
3/**
4 * Smarty Template Resource Base Object
5 *
6 * @package    Smarty
7 * @subpackage TemplateResources
8 * @author     Rodney Rehm
9 */
10abstract class Smarty_Template_Resource_Base
11{
12    /**
13     * Compiled Filepath
14     *
15     * @var string
16     */
17    public $filepath = null;
18
19    /**
20     * Compiled Timestamp
21     *
22     * @var integer|bool
23     */
24    public $timestamp = false;
25
26    /**
27     * Compiled Existence
28     *
29     * @var boolean
30     */
31    public $exists = false;
32
33    /**
34     * Template Compile Id (Smarty_Internal_Template::$compile_id)
35     *
36     * @var string
37     */
38    public $compile_id = null;
39
40    /**
41     * Compiled Content Loaded
42     *
43     * @var boolean
44     */
45    public $processed = false;
46
47    /**
48     * unique function name for compiled template code
49     *
50     * @var string
51     */
52    public $unifunc = '';
53
54    /**
55     * flag if template does contain nocache code sections
56     *
57     * @var bool
58     */
59    public $has_nocache_code = false;
60
61    /**
62     * resource file dependency
63     *
64     * @var array
65     */
66    public $file_dependency = array();
67
68    /**
69     * Content buffer
70     *
71     * @var string
72     */
73    public $content = null;
74
75    /**
76     * Included sub templates
77     * - index name
78     * - value use count
79     *
80     * @var int[]
81     */
82    public $includes = array();
83
84    /**
85     * Flag if this is a cache resource
86     *
87     * @var bool
88     */
89    public $isCache = false;
90
91    /**
92     * Process resource
93     *
94     * @param Smarty_Internal_Template $_template template object
95     */
96    abstract public function process(Smarty_Internal_Template $_template);
97
98    /**
99     * get rendered template content by calling compiled or cached template code
100     *
101     * @param \Smarty_Internal_Template $_template
102     * @param string                    $unifunc function with template code
103     *
104     * @throws \Exception
105     */
106    public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null)
107    {
108        $smarty = &$_template->smarty;
109        $_template->isRenderingCache = $this->isCache;
110        $level = ob_get_level();
111        try {
112            if (!isset($unifunc)) {
113                $unifunc = $this->unifunc;
114            }
115            if (empty($unifunc) || !function_exists($unifunc)) {
116                throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
117            }
118            if ($_template->startRenderCallbacks) {
119                foreach ($_template->startRenderCallbacks as $callback) {
120                    call_user_func($callback, $_template);
121                }
122            }
123            $unifunc($_template);
124            foreach ($_template->endRenderCallbacks as $callback) {
125                call_user_func($callback, $_template);
126            }
127            $_template->isRenderingCache = false;
128        } catch (Exception $e) {
129            $_template->isRenderingCache = false;
130            while (ob_get_level() > $level) {
131                ob_end_clean();
132            }
133            if (isset($smarty->security_policy)) {
134                $smarty->security_policy->endTemplate();
135            }
136            throw $e;
137        }
138    }
139
140    /**
141     * Get compiled time stamp
142     *
143     * @return int
144     */
145    public function getTimeStamp()
146    {
147        if ($this->exists && !$this->timestamp) {
148            $this->timestamp = filemtime($this->filepath);
149        }
150        return $this->timestamp;
151    }
152}
153