Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

smarty_internal_compile_insert.php 5.9KB

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