Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_templatecompilerbase.php
1 <?php
2 /**
3  * Smarty Internal Plugin Smarty Template Compiler Base
4  * 
5  * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
6  * 
7  * @package Smarty
8  * @subpackage Compiler
9  * @author Uwe Tews 
10  */
11
12 /**
13  * Main compiler class
14  */
15 class Smarty_Internal_TemplateCompilerBase {
16     // hash for nocache sections
17     private $nocache_hash = null; 
18     // suppress generation of nocache code
19     public $suppressNocacheProcessing = false; 
20     // compile tag objects
21     static $_tag_objects = array(); 
22     // tag stack
23     public $_tag_stack = array(); 
24     // current template
25     public $template = null;
26     // optional log of tag/attributes
27     public $used_tags = array();
28
29     /**
30      * Initialize compiler
31      */
32     public function __construct()
33     {
34         $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
35     } 
36
37     /**
38      * Methode to compile a Smarty template
39      * 
40      * @param  $template template object to compile
41      * @return bool true if compiling succeeded, false if it failed
42      */
43     public function compileTemplate($template)
44     {
45         if (empty($template->properties['nocache_hash'])) {
46             $template->properties['nocache_hash'] = $this->nocache_hash;
47         } else {
48             $this->nocache_hash = $template->properties['nocache_hash'];
49         } 
50         // flag for nochache sections
51         $this->nocache = false;
52         $this->tag_nocache = false; 
53         // save template object in compiler class
54         $this->template = $template;
55         $this->smarty->_current_file = $this->template->getTemplateFilepath(); 
56         // template header code
57         $template_header = '';
58         if (!$template->suppressHeader) {
59             $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
60             $template_header .= "         compiled from \"" . $this->template->getTemplateFilepath() . "\" */ ?>\n";
61         } 
62
63         do {
64             // flag for aborting current and start recompile
65             $this->abort_and_recompile = false; 
66             // get template source
67             $_content = $template->getTemplateSource(); 
68             // run prefilter if required
69             if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
70                 $template->template_source = $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
71             } 
72             // on empty template just return header
73             if ($_content == '') {
74                 if ($template->suppressFileDependency) {
75                     $template->compiled_template = '';
76                 } else {
77                     $template->compiled_template = $template_header . $template->createPropertyHeader();
78                 } 
79                 return true;
80             } 
81             // call compiler
82             $_compiled_code = $this->doCompile($_content);
83         } while ($this->abort_and_recompile); 
84         // return compiled code to template object
85         if ($template->suppressFileDependency) {
86             $template->compiled_template = $_compiled_code;
87         } else {
88             $template->compiled_template = $template_header . $template->createPropertyHeader() . $_compiled_code;
89         } 
90         // run postfilter if required
91         if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
92             $template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $template);
93         } 
94     } 
95
96     /**
97      * Compile Tag
98      * 
99      * This is a call back from the lexer/parser
100      * It executes the required compile plugin for the Smarty tag
101      * 
102      * @param string $tag tag name
103      * @param array $args array with tag attributes
104      * @param array $parameter array with compilation parameter
105      * @return string compiled code
106      */
107     public function compileTag($tag, $args, $parameter = array())
108     { 
109         // $args contains the attributes parsed and compiled by the lexer/parser
110         // assume that tag does compile into code, but creates no HTML output
111         $this->has_code = true;
112         $this->has_output = false;
113         // log tag/attributes
114         if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
115                 $this->used_tags[] = array($tag,$args);
116         } 
117                 // check nocache option flag
118         if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args)
119                         || in_array(array('nocache'=>'"true"'),$args) || in_array(array('nocache'=>"'true'"),$args)) {
120                 $this->tag_nocache = true;
121         }
122         // compile the smarty tag (required compile classes to compile the tag are autoloaded)
123         if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
124             if (isset($this->smarty->template_functions[$tag])) {
125                 // template defined by {template} tag
126                 $args['_attr']['name'] = "'" . $tag . "'";
127                 $_output = $this->callTagCompiler('call', $args, $parameter);
128             } 
129         } 
130         if ($_output !== false) {
131             if ($_output !== true) {
132                 // did we get compiled code
133                 if ($this->has_code) {
134                     // Does it create output?
135                     if ($this->has_output) {
136                         $_output .= "\n";
137                     } 
138                     // return compiled code
139                     return $_output;
140                 } 
141             } 
142             // tag did not produce compiled code
143             return '';
144         } else {
145             // map_named attributes
146             if (isset($args['_attr'])) {
147                 foreach ($args['_attr'] as $key => $attribute) {
148                     if (is_array($attribute)) {
149                         $args = array_merge($args, $attribute);
150                     } 
151                 } 
152             } 
153             // not an internal compiler tag
154             if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
155                 // check if tag is a registered object
156                 if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
157                     $methode = $parameter['object_methode'];
158                     if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
159                             (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
160                         return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
161                     } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
162                         return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
163                     } else {
164                         return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
165                     } 
166                 } 
167                 // check if tag is registered
168                 foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $type) {
169                     if (isset($this->smarty->registered_plugins[$type][$tag])) {
170                         // if compiler function plugin call it now
171                         if ($type == Smarty::PLUGIN_COMPILER) {
172                             $new_args = array();
173                             foreach ($args as $mixed) {
174                                 $new_args = array_merge($new_args, $mixed);
175                             } 
176                             if (!$this->smarty->registered_plugins[$type][$tag][1]) {
177                                 $this->tag_nocache = true;
178                             } 
179                             $function = $this->smarty->registered_plugins[$type][$tag][0];
180                             if (!is_array($function)) {
181                                 return $function($new_args, $this);
182                             } else if (is_object($function[0])) {
183                                 return $this->smarty->registered_plugins[$type][$tag][0][0]->$function[1]($new_args, $this);
184                             } else {
185                                 return call_user_func_array($this->smarty->registered_plugins[$type][$tag][0], array($new_args, $this));
186                             } 
187                         } 
188                         // compile registered function or block function
189                         if ($type == Smarty::PLUGIN_FUNCTION || $type == Smarty::PLUGIN_BLOCK) {
190                             return $this->callTagCompiler('private_registered_' . $type, $args, $parameter, $tag);
191                         } 
192                     } 
193                 } 
194                 // check plugins from plugins folder
195                 foreach ($this->smarty->plugin_search_order as $plugin_type) {
196                     if ($plugin_type == Smarty::PLUGIN_BLOCK && $this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
197                         $plugin = 'smarty_compiler_' . $tag;
198                         if (is_callable($plugin)) {
199                                 // convert arguments format for old compiler plugins
200                             $new_args = array();
201                             foreach ($args as $mixed) {
202                                 $new_args = array_merge($new_args, $mixed);
203                             } 
204                             return $plugin($new_args, $this->smarty);
205                         } 
206                         if (class_exists($plugin, false)) {
207                             $plugin_object = new $plugin;
208                             if (method_exists($plugin_object, 'compile')) {
209                                 return $plugin_object->compile($args, $this);
210                             } 
211                         } 
212                         throw new SmartyException("Plugin \"{$tag}\" not callable");
213                     } else {
214                         if ($function = $this->getPlugin($tag, $plugin_type)) {
215                             return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
216                         } 
217                     } 
218                 } 
219             } else {
220                 // compile closing tag of block function
221                 $base_tag = substr($tag, 0, -5); 
222                 // check if closing tag is a registered object
223                 if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
224                     $methode = $parameter['object_methode'];
225                     if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
226                         return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
227                     } else {
228                         return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
229                     } 
230                 } 
231                 // registered block tag ?
232                 if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
233                     return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
234                 } 
235                 // block plugin?
236                 if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
237                     return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
238                 } 
239                 if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
240                     $plugin = 'smarty_compiler_' . $tag;
241                     if (is_callable($plugin)) {
242                         return $plugin($args, $this->smarty);
243                     } 
244                     if (class_exists($plugin, false)) {
245                         $plugin_object = new $plugin;
246                         if (method_exists($plugin_object, 'compile')) {
247                             return $plugin_object->compile($args, $this);
248                         } 
249                     } 
250                     throw new SmartyException("Plugin \"{$tag}\" not callable");
251                 } 
252             } 
253             $this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
254         } 
255     } 
256
257     /**
258      * lazy loads internal compile plugin for tag and calls the compile methode
259      * 
260      * compile objects cached for reuse.
261      * class name format:  Smarty_Internal_Compile_TagName
262      * plugin filename format: Smarty_Internal_Tagname.php
263      * 
264      * @param  $tag string tag name
265      * @param  $args array with tag attributes
266      * @param  $param1 optional parameter
267      * @param  $param2 optional parameter
268      * @param  $param3 optional parameter
269      * @return string compiled code
270      */
271     public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
272     { 
273         // re-use object if already exists
274         if (isset(self::$_tag_objects[$tag])) {
275             // compile this tag
276             return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
277         } 
278         // lazy load internal compiler plugin
279         $class_name = 'Smarty_Internal_Compile_' . $tag;
280         if ($this->smarty->loadPlugin($class_name)) {
281             // use plugin if found
282             self::$_tag_objects[$tag] = new $class_name; 
283             // compile this tag
284             return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
285         } 
286         // no internal compile plugin for this tag
287         return false;
288     } 
289
290     /**
291      * Check for plugins and return function name
292      * 
293      * @param  $pugin_name string name of plugin or function
294      * @param  $type string type of plugin
295      * @return string call name of function
296      */
297     public function getPlugin($plugin_name, $type)
298     {
299         $function = null;
300         if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
301             if (isset($this->template->required_plugins['nocache'][$plugin_name][$type])) {
302                 $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
303             } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
304                 $this->template->required_plugins['nocache'][$plugin_name][$type] = $this->template->required_plugins['compiled'][$plugin_name][$type];
305                 $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
306             } 
307         } else {
308             if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
309                 $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
310             } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
311                 $this->template->required_plugins['compiled'][$plugin_name][$type] = $this->template->required_plugins['nocache'][$plugin_name][$type];
312                 $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
313             } 
314         } 
315         if (isset($function)) {
316             if ($type == 'modifier') {
317                 $this->template->saved_modifier[$plugin_name] = true;
318             } 
319             return $function;
320         } 
321         // loop through plugin dirs and find the plugin
322         $function = 'smarty_' . $type . '_' . $plugin_name;
323         $found = false;
324         foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {
325             $file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';
326             if (file_exists($file)) {
327                 // require_once($file);
328                 $found = true;
329                 break;
330             } 
331         } 
332         if ($found) {
333             if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
334                 $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;
335                 $this->template->required_plugins['nocache'][$plugin_name][$type]['function'] = $function;
336             } else {
337                 $this->template->required_plugins['compiled'][$plugin_name][$type]['file'] = $file;
338                 $this->template->required_plugins['compiled'][$plugin_name][$type]['function'] = $function;
339             } 
340             if ($type == 'modifier') {
341                 $this->template->saved_modifier[$plugin_name] = true;
342             } 
343             return $function;
344         } 
345         if (is_callable($function)) {
346             // plugin function is defined in the script
347             return $function;
348         } 
349         return false;
350     } 
351     /**
352      * Inject inline code for nocache template sections
353      * 
354      * This method gets the content of each template element from the parser.
355      * If the content is compiled code and it should be not cached the code is injected
356      * into the rendered output.
357      * 
358      * @param string $content content of template element
359      * @param boolean $tag_nocache true if the parser detected a nocache situation
360      * @param boolean $is_code true if content is compiled code
361      * @return string content
362      */
363     public function processNocacheCode ($content, $is_code)
364     { 
365         // If the template is not evaluated and we have a nocache section and or a nocache tag
366         if ($is_code && !empty($content)) {
367             // generate replacement code
368             if ((!$this->template->resource_object->isEvaluated || $this->template->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
369                     ($this->nocache || $this->tag_nocache || $this->template->forceNocache == 2)) {
370                 $this->template->has_nocache_code = true;
371                 $_output = str_replace("'", "\'", $content);
372                 $_output = str_replace("^#^", "'", $_output);
373                 $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>"; 
374                 // make sure we include modifer plugins for nocache code
375                 if (isset($this->template->saved_modifier)) {
376                     foreach ($this->template->saved_modifier as $plugin_name => $dummy) {
377                         if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
378                             $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
379                         } 
380                     } 
381                     $this->template->saved_modifier = null;
382                 } 
383             } else {
384                 $_output = $content;
385             } 
386         } else {
387             $_output = $content;
388         } 
389         $this->suppressNocacheProcessing = false;
390         $this->tag_nocache = false;
391         return $_output;
392     } 
393     /**
394      * display compiler error messages without dying
395      * 
396      * If parameter $args is empty it is a parser detected syntax error.
397      * In this case the parser is called to obtain information about expected tokens.
398      * 
399      * If parameter $args contains a string this is used as error message
400      * 
401      * @param  $args string individual error message or null
402      */
403     public function trigger_template_error($args = null, $line = null)
404     { 
405         // get template source line which has error
406         if (!isset($line)) {
407             $line = $this->lex->line;
408         } 
409         $match = preg_split("/\n/", $this->lex->data);
410         $error_text = 'Syntax Error in template "' . $this->template->getTemplateFilepath() . '"  on line ' . $line . ' "' . htmlspecialchars(trim(preg_replace('![\t\r\n]+!',' ',$match[$line-1]))) . '" ';
411         if (isset($args)) {
412             // individual error message
413             $error_text .= $args;
414         } else {
415             // expected token from parser
416             $error_text .= ' - Unexpected "' . $this->lex->value.'"';
417             if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4 ) {
418                 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
419                     $exp_token = $this->parser->yyTokenName[$token];
420                     if (isset($this->lex->smarty_token_names[$exp_token])) {
421                         // token type from lexer
422                         $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
423                     } else {
424                         // otherwise internal token name
425                         $expect[] = $this->parser->yyTokenName[$token];
426                     } 
427                 } 
428                 $error_text .= ', expected one of: ' . implode(' , ', $expect);
429                 }
430         } 
431         throw new SmartyCompilerException($error_text);
432     } 
433
434
435 ?>