1 <?php
3 /**
4 * Smarty Internal Plugin Resource Extends
5 *
6 * Implements the file system as resource for Smarty which does extend a chain of template files templates
7 *
8 * @package Smarty
9 * @subpackage TemplateResources
10 * @author Uwe Tews
11 */
12 /**
13 * Smarty Internal Plugin Resource Extends
14 */
15 class Smarty_Internal_Resource_Extends {
16 public function __construct($smarty)
17 {
18 $this->smarty = $smarty;
19 $this->_rdl = preg_quote($smarty->right_delimiter);
20 $this->_ldl = preg_quote($smarty->left_delimiter);
21 }
22 // classes used for compiling Smarty templates from file resource
23 public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
24 public $template_lexer_class = 'Smarty_Internal_Templatelexer';
25 public $template_parser_class = 'Smarty_Internal_Templateparser';
26 // properties
27 public $usesCompiler = true;
28 public $isEvaluated = false;
29 public $allFilepaths = array();
31 /**
32 * Return flag if template source is existing
33 *
34 * @param object $_template template object
35 * @return boolean result
36 */
37 public function isExisting($_template)
38 {
39 if ($_template->getTemplateFilepath() === false) {
40 return false;
41 } else {
42 return true;
43 }
44 }
45 /**
46 * Get filepath to template source
47 *
48 * @param object $_template template object
49 * @return string filepath to template source file
50 */
51 public function getTemplateFilepath($_template)
52 {
53 $sha1String = '';
54 $_files = explode('|', $_template->resource_name);
55 foreach ($_files as $_file) {
56 $_filepath = $_template->buildTemplateFilepath ($_file);
57 if ($_filepath !== false) {
58 if ($_template->security) {
59 $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
60 }
61 }
62 $sha1String .= $_filepath;
63 $this->allFilepaths[] = $_filepath;
64 }
65 $_template->templateUid = sha1($sha1String);
66 return $_filepath;
67 }
69 /**
70 * Get timestamp to template source
71 *
72 * @param object $_template template object
73 * @return integer timestamp of template source file
74 */
75 public function getTemplateTimestamp($_template)
76 {
77 return filemtime($_template->getTemplateFilepath());
78 }
80 /**
81 * Read template source from file
82 *
83 * @param object $_template template object
84 * @return string content of template source file
85 */
86 public function getTemplateSource($_template)
87 {
88 $this->template = $_template;
89 $_files = array_reverse($this->allFilepaths);
90 foreach ($_files as $_filepath) {
91 // read template file
92 if ($_filepath === false) {
93 throw new Exception("Unable to load template 'file : {$_file}'");
94 }
95 if ($_filepath != $_files[0]) {
96 $_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));
97 }
98 $_template->template_filepath = $_filepath;
99 $_content = file_get_contents($_filepath);
100 if ($_filepath != $_files[count($_files)-1]) {
101 if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $_open) !=
102 preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close)) {
103 $this->smarty->trigger_error("unmatched {block} {/block} pairs in file '$_filepath'");
104 }
105 preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
106 $_result_count = count($_result[0]);
107 $_start = 0;
108 while ($_start < $_result_count) {
109 $_end = 0;
110 $_level = 1;
111 while ($_level != 0) {
112 $_end++;
113 if (!strpos($_result[0][$_start + $_end][0], '/')) {
114 $_level++;
115 } else {
116 $_level--;
117 }
118 }
119 $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
120 substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
121 $this->saveBlockData($_block_content, $_result[0][$_start][0], $_filepath);
122 $_start = $_start + $_end + 1;
123 }
124 } else {
125 $_template->template_source = $_content;
126 return true;
127 }
128 }
129 // $_template->template_filepath = $saved_filepath;
130 }
131 protected function saveBlockData($block_content, $block_tag, $_filepath)
132 {
133 if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
134 $this->smarty->trigger_error("'{$block_tag}' missing name attribute in file '$_filepath'");
135 } else {
136 $_name = trim($_match[3], '\'"');
137 // replace {$smarty.block.child}
138 if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) {
139 if (isset($this->smarty->block_data[$_name])) {
140 $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
141 $this->smarty->block_data[$_name]['source'], $block_content);
142 unset($this->smarty->block_data[$_name]);
143 } else {
144 $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
145 '', $block_content);
146 }
147 }
148 if (isset($this->smarty->block_data[$_name])) {
149 if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
150 $this->smarty->block_data[$_name]['source'] =
151 str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']);
152 } elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
153 $this->smarty->block_data[$_name]['source'] .= $block_content;
154 } elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
155 $this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source'];
156 }
157 } else {
158 $this->smarty->block_data[$_name]['source'] = $block_content;
159 }
160 if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
161 $this->smarty->block_data[$_name]['mode'] = 'append';
162 } elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
163 $this->smarty->block_data[$_name]['mode'] = 'prepend';
164 } else {
165 $this->smarty->block_data[$_name]['mode'] = 'replace';
166 }
167 $this->smarty->block_data[$_name]['file'] = $_filepath;
168 }
169 }
171 /**
172 * Get filepath to compiled template
173 *
174 * @param object $_template template object
175 * @return string return path to compiled template
176 */
177 public function getCompiledFilepath($_template)
178 {
179 $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
180 $_files = explode('|', $_template->resource_name);
181 // calculate Uid if not already done
182 if ($_template->templateUid == '') {
183 $_template->getTemplateFilepath();
184 }
185 $_filepath = $_template->templateUid;
186 // if use_sub_dirs, break file into directories
187 if ($_template->smarty->use_sub_dirs) {
188 $_filepath = substr($_filepath, 0, 2) . DS
189 . substr($_filepath, 2, 2) . DS
190 . substr($_filepath, 4, 2) . DS
191 . $_filepath;
192 }
193 $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
194 if (isset($_compile_id)) {
195 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
196 }
197 if ($_template->caching) {
198 $_cache = '.cache';
199 } else {
200 $_cache = '';
201 }
202 $_compile_dir = $_template->smarty->compile_dir;
203 if (substr($_compile_dir, -1) != DS) {
204 $_compile_dir .= DS;
205 }
206 return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
207 }
208 }
210 ?>