You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_insert.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Insert
  4. * Compiles the {insert} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Insert Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('_any');
  39. /**
  40. * Compiles code for the {insert} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  44. *
  45. * @return string compiled code
  46. * @throws \SmartyCompilerException
  47. */
  48. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  49. {
  50. // check and get attributes
  51. $_attr = $this->getAttributes($compiler, $args);
  52. $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
  53. if (!$nocacheParam) {
  54. // do not compile as nocache code
  55. $compiler->suppressNocacheProcessing = true;
  56. }
  57. $compiler->tag_nocache = true;
  58. $_smarty_tpl = $compiler->template;
  59. $_name = null;
  60. $_script = null;
  61. $_output = '<?php ';
  62. // save possible attributes
  63. eval('$_name = @' . $_attr['name'] . ';');
  64. if (isset($_attr['assign'])) {
  65. // output will be stored in a smarty variable instead of being displayed
  66. $_assign = $_attr['assign'];
  67. // create variable to make sure that the compiler knows about its nocache status
  68. $var = trim($_attr['assign'], "'");
  69. if (isset($compiler->template->tpl_vars[$var])) {
  70. $compiler->template->tpl_vars[$var]->nocache = true;
  71. } else {
  72. $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
  73. }
  74. }
  75. if (isset($_attr['script'])) {
  76. // script which must be included
  77. $_function = "smarty_insert_{$_name}";
  78. $_smarty_tpl = $compiler->template;
  79. $_filepath = false;
  80. eval('$_script = @' . $_attr['script'] . ';');
  81. if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
  82. $_filepath = $_script;
  83. } else {
  84. if (isset($compiler->smarty->security_policy)) {
  85. $_dir = $compiler->smarty->security_policy->trusted_dir;
  86. } else {
  87. $_dir = $compiler->smarty->trusted_dir;
  88. }
  89. if (!empty($_dir)) {
  90. foreach ((array) $_dir as $_script_dir) {
  91. $_script_dir = rtrim($_script_dir, '/\\') . DS;
  92. if (file_exists($_script_dir . $_script)) {
  93. $_filepath = $_script_dir . $_script;
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. if ($_filepath == false) {
  100. $compiler->trigger_template_error("{insert} missing script file '{$_script}'", null, true);
  101. }
  102. // code for script file loading
  103. $_output .= "require_once '{$_filepath}' ;";
  104. require_once $_filepath;
  105. if (!is_callable($_function)) {
  106. $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", null, true);
  107. }
  108. } else {
  109. $_filepath = 'null';
  110. $_function = "insert_{$_name}";
  111. // function in PHP script ?
  112. if (!is_callable($_function)) {
  113. // try plugin
  114. if (!$_function = $compiler->getPlugin($_name, 'insert')) {
  115. $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null, true);
  116. }
  117. }
  118. }
  119. // delete {insert} standard attributes
  120. unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']);
  121. // convert attributes into parameter array string
  122. $_paramsArray = array();
  123. foreach ($_attr as $_key => $_value) {
  124. $_paramsArray[] = "'$_key' => $_value";
  125. }
  126. $_params = 'array(' . implode(", ", $_paramsArray) . ')';
  127. // call insert
  128. if (isset($_assign)) {
  129. if ($_smarty_tpl->caching && !$nocacheParam) {
  130. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
  131. } else {
  132. $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
  133. }
  134. } else {
  135. $compiler->has_output = true;
  136. if ($_smarty_tpl->caching && !$nocacheParam) {
  137. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
  138. } else {
  139. $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
  140. }
  141. }
  142. return $_output;
  143. }
  144. }