smarty_internal_compile_insert.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {insert} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('name');
  26. $this->optional_attributes = array('_any');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. // never compile as nocache code
  30. $this->compiler->suppressNocacheProcessing = true;
  31. $this->compiler->tag_nocache = true;
  32. $_smarty_tpl = $compiler->template;
  33. $_output = '<?php ';
  34. // save posible attributes
  35. eval('$_name = ' . $_attr['name'] . ';');
  36. if (isset($_attr['assign'])) {
  37. // output will be stored in a smarty variable instead of beind displayed
  38. $_assign = $_attr['assign'];
  39. // create variable to make shure that the compiler knows about its nocache status
  40. $this->compiler->template->tpl_vars[trim($_attr['assign'], "'")] = new Smarty_Variable(null, true);
  41. }
  42. if (isset($_attr['script'])) {
  43. // script which must be included
  44. $_function = "smarty_insert_{$_name}";
  45. $_smarty_tpl = $compiler->template;
  46. $_filepath = false;
  47. eval('$_script = ' . $_attr['script'] . ';');
  48. if (!$this->compiler->smarty->security && file_exists($_script)) {
  49. $_filepath = $_script;
  50. } else {
  51. if ($this->compiler->smarty->security) {
  52. $_dir = $this->compiler->smarty->security_policy->trusted_dir;
  53. } else {
  54. $_dir = $this->compiler->smarty->trusted_dir;
  55. }
  56. if (!empty($_dir)) {
  57. foreach((array)$_dir as $_script_dir) {
  58. if (strpos('/\\', substr($_script_dir, -1)) === false) {
  59. $_script_dir .= DS;
  60. }
  61. if (file_exists($_script_dir . $_script)) {
  62. $_filepath = $_script_dir . $_script;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. if ($_filepath == false) {
  69. $this->compiler->trigger_template_error("{insert} missing script file '{$_script}'", $this->compiler->lex->taglineno);
  70. }
  71. // code for script file loading
  72. $_output .= "require_once '{$_filepath}' ;";
  73. require_once $_filepath;
  74. if (!is_callable($_function)) {
  75. $this->compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $this->compiler->lex->taglineno);
  76. }
  77. } else {
  78. $_filepath = 'null';
  79. $_function = "insert_{$_name}";
  80. // function in PHP script ?
  81. if (!is_callable($_function)) {
  82. // try plugin
  83. if (!$_function = $this->compiler->getPlugin($_name, 'insert')) {
  84. $this->compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $this->compiler->lex->taglineno);
  85. }
  86. }
  87. }
  88. // delete {insert} standard attributes
  89. unset($_attr['name'], $_attr['assign'], $_attr['script']);
  90. // convert attributes into parameter array string
  91. $_paramsArray = array();
  92. foreach ($_attr as $_key => $_value) {
  93. $_paramsArray[] = "'$_key' => $_value";
  94. }
  95. $_params = 'array(' . implode(", ", $_paramsArray) . ')';
  96. // call insert
  97. if (isset($_assign)) {
  98. if ($_smarty_tpl->caching) {
  99. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
  100. } else {
  101. $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl->smarty,\$_smarty_tpl), true);?>";
  102. }
  103. } else {
  104. $this->compiler->has_output = true;
  105. if ($_smarty_tpl->caching) {
  106. $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
  107. } else {
  108. $_output .= "echo {$_function}({$_params},\$_smarty_tpl->smarty,\$_smarty_tpl);?>";
  109. }
  110. }
  111. return $_output;
  112. }
  113. }
  114. ?>