1 : <?php
2 : /**
3 : * Smarty Internal Plugin Compile Print Expression
4 : *
5 : * Compiles any tag which will output an expression or variable
6 : *
7 : * @package Smarty
8 : * @subpackage Compiler
9 : * @author Uwe Tews
10 : */
11 :
12 : /**
13 : * Smarty Internal Plugin Compile Print Expression Class
14 : *
15 : * @package Smarty
16 : * @subpackage Compiler
17 : */
18 : class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase {
19 :
20 : /**
21 : * Attribute definition: Overwrites base class.
22 : *
23 : * @var array
24 : * @see Smarty_Internal_CompileBase
25 : */
26 : public $optional_attributes = array('assign');
27 : /**
28 : * Attribute definition: Overwrites base class.
29 : *
30 : * @var array
31 : * @see Smarty_Internal_CompileBase
32 : */
33 : public $option_flags = array('nocache', 'nofilter');
34 :
35 : /**
36 : * Compiles code for gererting output from any expression
37 : *
38 : * @param array $args array with attributes from parser
39 : * @param object $compiler compiler object
40 : * @param array $parameter array with compilation parameter
41 : * @return string compiled code
42 : */
43 : public function compile($args, $compiler, $parameter)
44 : {
45 : // check and get attributes
46 0 : $_attr = $this->getAttributes($compiler, $args);
47 : // nocache option
48 0 : if ($_attr['nocache'] === true) {
49 0 : $compiler->tag_nocache = true;
50 0 : }
51 : // filter handling
52 0 : if ($_attr['nofilter'] === true) {
53 0 : $_filter = 'false';
54 0 : } else {
55 0 : $_filter = 'true';
56 : }
57 0 : if (isset($_attr['assign'])) {
58 : // assign output to variable
59 0 : $output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
60 0 : } else {
61 : // display value
62 0 : $output = $parameter['value'];
63 : // tag modifier
64 0 : if (!empty($parameter['modifierlist'])) {
65 0 : $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $parameter['modifierlist'], 'value' => $output));
66 0 : }
67 0 : if (!$_attr['nofilter']) {
68 : // default modifier
69 0 : if (!empty($compiler->smarty->default_modifiers)) {
70 0 : if (empty($compiler->default_modifier_list)) {
71 0 : $modifierlist = array();
72 0 : foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
73 0 : preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/', $single_default_modifier, $mod_array);
74 0 : for ($i = 0, $count = count($mod_array[0]);$i < $count;$i++) {
75 0 : if ($mod_array[0][$i] != ':') {
76 0 : $modifierlist[$key][] = $mod_array[0][$i];
77 0 : }
78 0 : }
79 0 : }
80 0 : $compiler->default_modifier_list = $modifierlist;
81 0 : }
82 0 : $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $compiler->default_modifier_list, 'value' => $output));
83 0 : }
84 : // autoescape html
85 0 : if ($compiler->template->smarty->escape_html) {
86 0 : $output = "htmlspecialchars({$output}, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET)";
87 0 : }
88 : // loop over registerd filters
89 0 : if (!empty($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE])) {
90 0 : foreach ($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE] as $key => $function) {
91 0 : if (!is_array($function)) {
92 0 : $output = "{$function}({$output},\$_smarty_tpl)";
93 0 : } else if (is_object($function[0])) {
94 0 : $output = "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE][{$key}][0]->{$function[1]}({$output},\$_smarty_tpl)";
95 0 : } else {
96 0 : $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
97 : }
98 0 : }
99 0 : }
100 : // auto loaded filters
101 0 : if (isset($compiler->smarty->autoload_filters[Smarty::FILTER_VARIABLE])) {
102 0 : foreach ((array)$compiler->template->smarty->autoload_filters[Smarty::FILTER_VARIABLE] as $name) {
103 0 : $result = $this->compile_output_filter($compiler, $name, $output);
104 0 : if ($result !== false) {
105 0 : $output = $result;
106 0 : } else {
107 : // not found, throw exception
108 0 : throw new SmartyException("Unable to load filter '{$name}'");
109 : }
110 0 : }
111 0 : }
112 0 : if (isset($compiler->template->variable_filters)) {
113 0 : foreach ($compiler->template->variable_filters as $filter) {
114 0 : if (count($filter) == 1 && ($result = $this->compile_output_filter($compiler, $filter[0], $output)) !== false) {
115 0 : $output = $result;
116 0 : } else {
117 0 : $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => array($filter), 'value' => $output));
118 : }
119 0 : }
120 0 : }
121 0 : }
122 :
123 0 : $compiler->has_output = true;
124 0 : $output = "<?php echo {$output};?>";
125 : }
126 0 : return $output;
127 : }
128 :
129 : /**
130 : * @param object $compiler compiler object
131 : * @param string $name name of variable filter
132 : * @param type $output embedded output
133 : * @return string
134 : */
135 : private function compile_output_filter($compiler, $name, $output)
136 : {
137 0 : $plugin_name = "smarty_variablefilter_{$name}";
138 0 : $path = $compiler->smarty->loadPlugin($plugin_name, false);
139 0 : if ($path) {
140 0 : if ($compiler->template->caching) {
141 0 : $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
142 0 : $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
143 0 : } else {
144 0 : $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
145 0 : $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
146 : }
147 0 : } else {
148 : // not found
149 0 : return false;
150 : }
151 0 : return "{$plugin_name}({$output},\$_smarty_tpl)";
152 : }
153 :
154 : }
155 :
|