/includes/smarty/sysplugins/smarty_internal_resource_extends.php

https://github.com/ACCORD5/TrellisDesk · PHP · 186 lines · 132 code · 6 blank · 48 comment · 32 complexity · 5f5e78d5c55217b1d8b90b8067bc2c13 MD5 · raw file

  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(.+?){$this->_rdl})!", $_content, $_open, PREG_OFFSET_CAPTURE) !=
  98. preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close, PREG_OFFSET_CAPTURE)) {
  99. $this->smarty->trigger_error('unmatched {block} {/block} pairs');
  100. }
  101. $_block_count = count($_open[0]);
  102. for ($_i = 0; $_i < $_block_count; $_i++) {
  103. $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  104. substr($_content, $_open[0][$_i][1] + strlen($_open[0][$_i][0]), $_close[0][$_i][1] - $_open[0][$_i][1] - strlen($_open[0][$_i][0])));
  105. $this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath);
  106. }
  107. } else {
  108. $_template->template_source = $_content;
  109. return true;
  110. }
  111. }
  112. // $_template->template_filepath = $saved_filepath;
  113. }
  114. protected function saveBlockData($block_content, $block_tag, $_filepath)
  115. {
  116. if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
  117. $this->smarty->trigger_error("'{$block_tag}' missing name attribute");
  118. } else {
  119. $_name = trim($_match[3], '\'"');
  120. if (isset($this->smarty->block_data[$_name])) {
  121. if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  122. $this->smarty->block_data[$_name]['source'] =
  123. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']);
  124. } elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
  125. $this->smarty->block_data[$_name]['source'] .= $block_content;
  126. } elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
  127. $this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source'];
  128. }
  129. } else {
  130. $this->smarty->block_data[$_name]['source'] = $block_content;
  131. }
  132. if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
  133. $this->smarty->block_data[$_name]['mode'] = 'append';
  134. } elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
  135. $this->smarty->block_data[$_name]['mode'] = 'prepend';
  136. } else {
  137. $this->smarty->block_data[$_name]['mode'] = 'replace';
  138. }
  139. $this->smarty->block_data[$_name]['file'] = $_filepath;
  140. }
  141. }
  142. /**
  143. * Get filepath to compiled template
  144. *
  145. * @param object $_template template object
  146. * @return string return path to compiled template
  147. */
  148. public function getCompiledFilepath($_template)
  149. {
  150. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  151. $_files = explode('|', $_template->resource_name);
  152. // calculate Uid if not already done
  153. if ($_template->templateUid == '') {
  154. $_template->getTemplateFilepath();
  155. }
  156. $_filepath = $_template->templateUid;
  157. // if use_sub_dirs, break file into directories
  158. if ($_template->smarty->use_sub_dirs) {
  159. $_filepath = substr($_filepath, 0, 2) . DS
  160. . substr($_filepath, 2, 2) . DS
  161. . substr($_filepath, 4, 2) . DS
  162. . $_filepath;
  163. }
  164. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  165. if (isset($_compile_id)) {
  166. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  167. }
  168. if ($_template->caching) {
  169. $_cache = '.cache';
  170. } else {
  171. $_cache = '';
  172. }
  173. $_compile_dir = $_template->smarty->compile_dir;
  174. if (substr($_compile_dir, -1) != DS) {
  175. $_compile_dir .= DS;
  176. }
  177. return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
  178. }
  179. }
  180. ?>