smarty_internal_resource_extends.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Extends
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource Extends
  12. *
  13. * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
  14. *
  15. * @package Smarty
  16. * @subpackage TemplateResources
  17. */
  18. class Smarty_Internal_Resource_Extends extends Smarty_Resource {
  19. /**
  20. * mbstring.overload flag
  21. *
  22. * @var int
  23. */
  24. public $mbstring_overload = 0;
  25. /**
  26. * populate Source Object with meta data from Resource
  27. *
  28. * @param Smarty_Template_Source $source source object
  29. * @param Smarty_Internal_Template $_template template object
  30. */
  31. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
  32. {
  33. $uid = '';
  34. $sources = array();
  35. $components = explode('|', $source->name);
  36. $exists = true;
  37. foreach ($components as $component) {
  38. $s = Smarty_Resource::source(null, $source->smarty, $component);
  39. if ($s->type == 'php') {
  40. throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
  41. }
  42. $sources[$s->uid] = $s;
  43. $uid .= $s->filepath;
  44. if ($_template && $_template->smarty->compile_check) {
  45. $exists = $exists && $s->exists;
  46. }
  47. }
  48. $source->components = $sources;
  49. $source->filepath = $s->filepath;
  50. $source->uid = sha1($uid);
  51. if ($_template && $_template->smarty->compile_check) {
  52. $source->timestamp = $s->timestamp;
  53. $source->exists = $exists;
  54. }
  55. // need the template at getContent()
  56. $source->template = $_template;
  57. }
  58. /**
  59. * populate Source Object with timestamp and exists from Resource
  60. *
  61. * @param Smarty_Template_Source $source source object
  62. */
  63. public function populateTimestamp(Smarty_Template_Source $source)
  64. {
  65. $source->exists = true;
  66. foreach ($source->components as $s) {
  67. $source->exists = $source->exists && $s->exists;
  68. }
  69. $source->timestamp = $s->timestamp;
  70. }
  71. /**
  72. * Load template's source from files into current template object
  73. *
  74. * @param Smarty_Template_Source $source source object
  75. * @return string template source
  76. * @throws SmartyException if source cannot be loaded
  77. */
  78. public function getContent(Smarty_Template_Source $source)
  79. {
  80. if (!$source->exists) {
  81. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  82. }
  83. $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
  84. $_rdl = preg_quote($source->smarty->right_delimiter);
  85. $_ldl = preg_quote($source->smarty->left_delimiter);
  86. $_components = array_reverse($source->components);
  87. $_first = reset($_components);
  88. $_last = end($_components);
  89. foreach ($_components as $_component) {
  90. // register dependency
  91. if ($_component != $_first) {
  92. $source->template->properties['file_dependency'][$_component->uid] = array($_component->filepath, $_component->timestamp, $_component->type);
  93. }
  94. // read content
  95. $source->filepath = $_component->filepath;
  96. $_content = $_component->content;
  97. // extend sources
  98. if ($_component != $_last) {
  99. if (preg_match_all("!({$_ldl}block\s(.+?){$_rdl})!", $_content, $_open) !=
  100. preg_match_all("!({$_ldl}/block{$_rdl})!", $_content, $_close)) {
  101. throw new SmartyException("unmatched {block} {/block} pairs in template {$_component->type} '{$_component->name}'");
  102. }
  103. preg_match_all("!{$_ldl}block\s(.+?){$_rdl}|{$_ldl}/block{$_rdl}|{$_ldl}\*([\S\s]*?)\*{$_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  104. $_result_count = count($_result[0]);
  105. $_start = 0;
  106. while ($_start+1 < $_result_count) {
  107. $_end = 0;
  108. $_level = 1;
  109. if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
  110. $_start++;
  111. continue;
  112. }
  113. while ($_level != 0) {
  114. $_end++;
  115. if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
  116. continue;
  117. }
  118. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  119. $_level++;
  120. } else {
  121. $_level--;
  122. }
  123. }
  124. $_block_content = str_replace($source->smarty->left_delimiter . '$smarty.block.parent' . $source->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  125. ($this->mbstring_overload ? mb_substr($_content, $_result[0][$_start][1] + mb_strlen($_result[0][$_start][0], 'latin1'), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + mb_strlen($_result[0][$_start][0], 'latin1'), 'latin1') : 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]))));
  126. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $source->template, $_component->filepath);
  127. $_start = $_start + $_end + 1;
  128. }
  129. } else {
  130. return $_content;
  131. }
  132. }
  133. }
  134. /**
  135. * Determine basename for compiled filename
  136. *
  137. * @param Smarty_Template_Source $source source object
  138. * @return string resource's basename
  139. */
  140. public function getBasename(Smarty_Template_Source $source)
  141. {
  142. return str_replace(':', '.', basename($source->filepath));
  143. }
  144. }
  145. ?>