smarty_internal_compile_insert.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //Does tag create output
  53. $compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
  54. $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
  55. if (!$nocacheParam) {
  56. // do not compile as nocache code
  57. $compiler->suppressNocacheProcessing = true;
  58. }
  59. $compiler->tag_nocache = true;
  60. $_smarty_tpl = $compiler->template;
  61. $_name = null;
  62. $_script = null;
  63. $_output = '<?php ';
  64. // save possible attributes
  65. eval('$_name = @' . $_attr[ 'name' ] . ';');
  66. if (isset($_attr[ 'assign' ])) {
  67. // output will be stored in a smarty variable instead of being displayed
  68. $_assign = $_attr[ 'assign' ];
  69. // create variable to make sure that the compiler knows about its nocache status
  70. $var = trim($_attr[ 'assign' ], "'");
  71. if (isset($compiler->template->tpl_vars[ $var ])) {
  72. $compiler->template->tpl_vars[ $var ]->nocache = true;
  73. } else {
  74. $compiler->template->tpl_vars[ $var ] = new Smarty_Variable(null, true);
  75. }
  76. }
  77. if (isset($_attr[ 'script' ])) {
  78. // script which must be included
  79. $_function = "smarty_insert_{$_name}";
  80. $_smarty_tpl = $compiler->template;
  81. $_filepath = false;
  82. eval('$_script = @' . $_attr[ 'script' ] . ';');
  83. if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
  84. $_filepath = $_script;
  85. } else {
  86. if (isset($compiler->smarty->security_policy)) {
  87. $_dir = $compiler->smarty->security_policy->trusted_dir;
  88. } else {
  89. $_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
  90. }
  91. if (!empty($_dir)) {
  92. foreach ((array) $_dir as $_script_dir) {
  93. $_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
  94. if (file_exists($_script_dir . $_script)) {
  95. $_filepath = $_script_dir . $_script;
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. if ($_filepath == false) {
  102. $compiler->trigger_template_error("{insert} missing script file '{$_script}'", null, true);
  103. }
  104. // code for script file loading
  105. $_output .= "require_once '{$_filepath}' ;";
  106. require_once $_filepath;
  107. if (!is_callable($_function)) {
  108. $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'",
  109. null, true);
  110. }
  111. } else {
  112. $_filepath = 'null';
  113. $_function = "insert_{$_name}";
  114. // function in PHP script ?
  115. if (!is_callable($_function)) {
  116. // try plugin
  117. if (!$_function = $compiler->getPlugin($_name, 'insert')) {
  118. $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null,
  119. true);
  120. }
  121. }
  122. }
  123. // delete {insert} standard attributes
  124. unset($_attr[ 'name' ], $_attr[ 'assign' ], $_attr[ 'script' ], $_attr[ 'nocache' ]);
  125. // convert attributes into parameter array string
  126. $_paramsArray = array();
  127. foreach ($_attr as $_key => $_value) {
  128. $_paramsArray[] = "'$_key' => $_value";
  129. }
  130. $_params = 'array(' . implode(", ", $_paramsArray) . ')';
  131. // call insert
  132. if (isset($_assign)) {
  133. if ($_smarty_tpl->caching && !$nocacheParam) {
  134. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
  135. } else {
  136. $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
  137. }
  138. } else {
  139. if ($_smarty_tpl->caching && !$nocacheParam) {
  140. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
  141. } else {
  142. $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
  143. }
  144. }
  145. return $_output;
  146. }
  147. }