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