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