smarty_internal_resource_extends.php 8.8 KB

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