smarty_internal_compile_extends.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile extend
  4. *
  5. * Compiles the {extends} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile extend Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('file');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('file');
  32. /**
  33. * mbstring.overload flag
  34. *
  35. * @var int
  36. */
  37. public $mbstring_overload = 0;
  38. /**
  39. * Compiles code for the {extends} tag
  40. *
  41. * @param array $args array with attributes from parser
  42. * @param object $compiler compiler object
  43. * @return string compiled code
  44. */
  45. public function compile($args, $compiler)
  46. {
  47. static $_is_stringy = array('string' => true, 'eval' => true);
  48. $this->_rdl = preg_quote($compiler->smarty->right_delimiter);
  49. $this->_ldl = preg_quote($compiler->smarty->left_delimiter);
  50. $filepath = $compiler->template->source->filepath;
  51. $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
  52. // check and get attributes
  53. $_attr = $this->getAttributes($compiler, $args);
  54. if ($_attr['nocache'] === true) {
  55. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  56. }
  57. $_smarty_tpl = $compiler->template;
  58. $include_file = null;
  59. if (strpos($_attr['file'], '$_tmp') !== false) {
  60. $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
  61. }
  62. eval('$include_file = ' . $_attr['file'] . ';');
  63. // create template object
  64. $_template = new $compiler->smarty->template_class($include_file, $compiler->smarty, $compiler->template);
  65. // save file dependency
  66. if (isset($_is_stringy[$_template->source->type])) {
  67. $template_sha1 = sha1($include_file);
  68. } else {
  69. $template_sha1 = sha1($_template->source->filepath);
  70. }
  71. if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
  72. $compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"", $compiler->lex->line - 1);
  73. }
  74. $compiler->template->properties['file_dependency'][$template_sha1] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
  75. $_content = ($this->mbstring_overload ? mb_substr($compiler->template->source->content, $compiler->lex->counter - 1, 20000000, 'latin1') : substr($compiler->template->source->content, $compiler->lex->counter - 1));
  76. if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
  77. preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $c)) {
  78. $compiler->trigger_template_error('unmatched {block} {/block} pairs');
  79. }
  80. preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}|{$this->_ldl}\*([\S\s]*?)\*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  81. $_result_count = count($_result[0]);
  82. $_start = 0;
  83. while ($_start+1 < $_result_count) {
  84. $_end = 0;
  85. $_level = 1;
  86. if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($compiler->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($compiler->smarty->left_delimiter)+1)) == $compiler->smarty->left_delimiter.'*') {
  87. $_start++;
  88. continue;
  89. }
  90. while ($_level != 0) {
  91. $_end++;
  92. if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($compiler->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($compiler->smarty->left_delimiter)+1)) == $compiler->smarty->left_delimiter.'*') {
  93. continue;
  94. }
  95. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  96. $_level++;
  97. } else {
  98. $_level--;
  99. }
  100. }
  101. $_block_content = str_replace($compiler->smarty->left_delimiter . '$smarty.block.parent' . $compiler->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  102. ($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]))));
  103. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template, $filepath);
  104. $_start = $_start + $_end + 1;
  105. }
  106. if ($_template->source->type == 'extends') {
  107. $_template->block_data = $compiler->template->block_data;
  108. }
  109. $compiler->template->source->content = $_template->source->content;
  110. if ($_template->source->type == 'extends') {
  111. $compiler->template->block_data = $_template->block_data;
  112. foreach ($_template->source->components as $key => $component) {
  113. $compiler->template->properties['file_dependency'][$key] = array($component->filepath, $component->timestamp, $component->type);
  114. }
  115. }
  116. $compiler->template->source->filepath = $_template->source->filepath;
  117. $compiler->abort_and_recompile = true;
  118. return '';
  119. }
  120. }
  121. ?>