smarty_internal_compile_insert.php 5.1 KB

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