smarty_internal_compile_insert.php (5618B)
1 <?php 2 3 /** 4 * Smarty Internal Plugin Compile Insert 5 * Compiles the {insert} tag 6 * 7 * @package Smarty 8 * @subpackage Compiler 9 * @author Uwe Tews 10 */ 11 12 /** 13 * Smarty Internal Plugin Compile Insert Class 14 * 15 * @package Smarty 16 * @subpackage Compiler 17 */ 18 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase 19 { 20 /** 21 * Attribute definition: Overwrites base class. 22 * 23 * @var array 24 * @see Smarty_Internal_CompileBase 25 */ 26 public $required_attributes = array('name'); 27 /** 28 * Attribute definition: Overwrites base class. 29 * 30 * @var array 31 * @see Smarty_Internal_CompileBase 32 */ 33 public $shorttag_order = array('name'); 34 /** 35 * Attribute definition: Overwrites base class. 36 * 37 * @var array 38 * @see Smarty_Internal_CompileBase 39 */ 40 public $optional_attributes = array('_any'); 41 42 /** 43 * Compiles code for the {insert} tag 44 * 45 * @param array $args array with attributes from parser 46 * @param object $compiler compiler object 47 * 48 * @return string compiled code 49 */ 50 public function compile($args, $compiler) 51 { 52 // check and get attributes 53 $_attr = $this->getAttributes($compiler, $args); 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 64 $_output = '<?php '; 65 // save possible attributes 66 eval('$_name = ' . $_attr['name'] . ';'); 67 if (isset($_attr['assign'])) { 68 // output will be stored in a smarty variable instead of being displayed 69 $_assign = $_attr['assign']; 70 // create variable to make sure that the compiler knows about its nocache status 71 $var = trim($_attr['assign'], "'"); 72 if (isset($compiler->template->tpl_vars[$var])) { 73 $compiler->template->tpl_vars[$var]->nocache = true; 74 } else { 75 $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true); 76 } 77 } 78 if (isset($_attr['script'])) { 79 // script which must be included 80 $_function = "smarty_insert_{$_name}"; 81 $_smarty_tpl = $compiler->template; 82 $_filepath = false; 83 eval('$_script = ' . $_attr['script'] . ';'); 84 if (!isset($compiler->smarty->security_policy) && file_exists($_script)) { 85 $_filepath = $_script; 86 } else { 87 if (isset($compiler->smarty->security_policy)) { 88 $_dir = $compiler->smarty->security_policy->trusted_dir; 89 } else { 90 $_dir = $compiler->smarty->trusted_dir; 91 } 92 if (!empty($_dir)) { 93 foreach ((array) $_dir as $_script_dir) { 94 $_script_dir = rtrim($_script_dir, '/\\') . DS; 95 if (file_exists($_script_dir . $_script)) { 96 $_filepath = $_script_dir . $_script; 97 break; 98 } 99 } 100 } 101 } 102 if ($_filepath == false) { 103 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", $compiler->lex->taglineno); 104 } 105 // code for script file loading 106 $_output .= "require_once '{$_filepath}' ;"; 107 require_once $_filepath; 108 if (!is_callable($_function)) { 109 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $compiler->lex->taglineno); 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}'", $compiler->lex->taglineno); 119 } 120 } 121 } 122 // delete {insert} standard attributes 123 unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']); 124 // convert attributes into parameter array string 125 $_paramsArray = array(); 126 foreach ($_attr as $_key => $_value) { 127 $_paramsArray[] = "'$_key' => $_value"; 128 } 129 $_params = 'array(' . implode(", ", $_paramsArray) . ')'; 130 // call insert 131 if (isset($_assign)) { 132 if ($_smarty_tpl->caching && !$nocacheParam) { 133 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>"; 134 } else { 135 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>"; 136 } 137 } else { 138 $compiler->has_output = true; 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 146 return $_output; 147 } 148 }