smarty_internal_compile_insert.php 5.1 KB

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