PEEL Shopping
Open source ecommerce : PEEL Shopping
smarty_internal_templatecompilerbase.php
Go to the documentation of this file.
1 <?php
2 
20 
26  private $nocache_hash = null;
27 
34 
40  public $suppressMergedTemplates = false;
41 
47  public static $_tag_objects = array();
48 
54  public $_tag_stack = array();
55 
61  public $template = null;
62 
68  public $merged_templates = array();
69 
75  public $inheritance = false;
76 
82  public $default_handler_plugins = array();
83 
89  public $default_modifier_list = null;
90 
95  public $forceNocache = false;
96 
101  public $suppressHeader = false;
102 
108 
113  public $suppressFilter = false;
114 
119  public $write_compiled_code = true;
120 
126 
131  public $called_functions = array();
132 
137  public $modifier_plugins = array();
138 
143  public $known_modifier_type = array();
144 
151  abstract protected function doCompile($_content);
152 
156  public function __construct() {
157  $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
158  }
159 
167  if (empty($template->properties['nocache_hash'])) {
168  $template->properties['nocache_hash'] = $this->nocache_hash;
169  } else {
170  $this->nocache_hash = $template->properties['nocache_hash'];
171  }
172  // flag for nochache sections
173  $this->nocache = false;
174  $this->tag_nocache = false;
175  // save template object in compiler class
176  $this->template = $template;
177  // reset has noche code flag
178  $this->template->has_nocache_code = false;
179  $this->smarty->_current_file = $saved_filepath = $this->template->source->filepath;
180  // template header code
181  $template_header = '';
182  if (!$this->suppressHeader) {
183  $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
184  $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
185  }
186 
187  do {
188  // flag for aborting current and start recompile
189  $this->abort_and_recompile = false;
190  // get template source
191  $_content = $template->source->content;
192  // run prefilter if required
193  if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) {
194  $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
195  }
196  // on empty template just return header
197  if ($_content == '') {
198  if ($this->suppressTemplatePropertyHeader) {
199  $code = '';
200  } else {
201  $code = $template_header . $template->createTemplateCodeFrame();
202  }
203  return $code;
204  }
205  // call compiler
206  $_compiled_code = $this->doCompile($_content);
207  } while ($this->abort_and_recompile);
208  $this->template->source->filepath = $saved_filepath;
209  // free memory
210  unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex, $this->template);
211  self::$_tag_objects = array();
212  // return compiled code to template object
213  $merged_code = '';
214  if (!$this->suppressMergedTemplates && !empty($this->merged_templates)) {
215  foreach ($this->merged_templates as $code) {
216  $merged_code .= $code;
217  }
218  }
219  // run postfilter if required on compiled template code
220  if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter) {
221  $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template);
222  }
223  if ($this->suppressTemplatePropertyHeader) {
224  $code = $_compiled_code . $merged_code;
225  } else {
226  $code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code;
227  }
228  // unset content because template inheritance could have replace source with parent code
229  unset ($template->source->content);
230  return $code;
231  }
232 
244  public function compileTag($tag, $args, $parameter = array()) {
245  // $args contains the attributes parsed and compiled by the lexer/parser
246  // assume that tag does compile into code, but creates no HTML output
247  $this->has_code = true;
248  $this->has_output = false;
249  // log tag/attributes
250  if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
251  $this->template->used_tags[] = array($tag, $args);
252  }
253  // check nocache option flag
254  if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args)
255  || in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args)) {
256  $this->tag_nocache = true;
257  }
258  // compile the smarty tag (required compile classes to compile the tag are autoloaded)
259  if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
260  if (isset($this->smarty->template_functions[$tag])) {
261  // template defined by {template} tag
262  $args['_attr']['name'] = "'" . $tag . "'";
263  $_output = $this->callTagCompiler('call', $args, $parameter);
264  }
265  }
266  if ($_output !== false) {
267  if ($_output !== true) {
268  // did we get compiled code
269  if ($this->has_code) {
270  // Does it create output?
271  if ($this->has_output) {
272  $_output .= "\n";
273  }
274  // return compiled code
275  return $_output;
276  }
277  }
278  // tag did not produce compiled code
279  return '';
280  } else {
281  // map_named attributes
282  if (isset($args['_attr'])) {
283  foreach ($args['_attr'] as $key => $attribute) {
284  if (is_array($attribute)) {
285  $args = array_merge($args, $attribute);
286  }
287  }
288  }
289  // not an internal compiler tag
290  if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
291  // check if tag is a registered object
292  if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
293  $methode = $parameter['object_methode'];
294  if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
295  (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
296  return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
297  } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
298  return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
299  } else {
300  return $this->trigger_template_error('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
301  }
302  }
303  // check if tag is registered
304  foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) {
305  if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) {
306  // if compiler function plugin call it now
307  if ($plugin_type == Smarty::PLUGIN_COMPILER) {
308  $new_args = array();
309  foreach ($args as $key => $mixed) {
310  if (is_array($mixed)) {
311  $new_args = array_merge($new_args, $mixed);
312  } else {
313  $new_args[$key] = $mixed;
314  }
315  }
316  if (!$this->smarty->registered_plugins[$plugin_type][$tag][1]) {
317  $this->tag_nocache = true;
318  }
319  $function = $this->smarty->registered_plugins[$plugin_type][$tag][0];
320  if (!is_array($function)) {
321  return $function($new_args, $this);
322  } else if (is_object($function[0])) {
323  return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
324  } else {
325  return call_user_func_array($function, array($new_args, $this));
326  }
327  }
328  // compile registered function or block function
329  if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) {
330  return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
331  }
332  }
333  }
334  // check plugins from plugins folder
335  foreach ($this->smarty->plugin_search_order as $plugin_type) {
336  if ($plugin_type == Smarty::PLUGIN_COMPILER && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))) {
337  $plugin = 'smarty_compiler_' . $tag;
338  if (is_callable($plugin)) {
339  // convert arguments format for old compiler plugins
340  $new_args = array();
341  foreach ($args as $key => $mixed) {
342  if (is_array($mixed)) {
343  $new_args = array_merge($new_args, $mixed);
344  } else {
345  $new_args[$key] = $mixed;
346  }
347  }
348  return $plugin($new_args, $this->smarty);
349  }
350  if (class_exists($plugin, false)) {
351  $plugin_object = new $plugin;
352  if (method_exists($plugin_object, 'compile')) {
353  return $plugin_object->compile($args, $this);
354  }
355  }
356  throw new SmartyException("Plugin \"{$tag}\" not callable");
357  } else {
358  if ($function = $this->getPlugin($tag, $plugin_type)) {
359  if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
360  return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
361  }
362  }
363  }
364  }
365  if (is_callable($this->smarty->default_plugin_handler_func)) {
366  $found = false;
367  // look for already resolved tags
368  foreach ($this->smarty->plugin_search_order as $plugin_type) {
369  if (isset($this->default_handler_plugins[$plugin_type][$tag])) {
370  $found = true;
371  break;
372  }
373  }
374  if (!$found) {
375  // call default handler
376  foreach ($this->smarty->plugin_search_order as $plugin_type) {
377  if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) {
378  $found = true;
379  break;
380  }
381  }
382  }
383  if ($found) {
384  // if compiler function plugin call it now
385  if ($plugin_type == Smarty::PLUGIN_COMPILER) {
386  $new_args = array();
387  foreach ($args as $mixed) {
388  $new_args = array_merge($new_args, $mixed);
389  }
390  $function = $this->default_handler_plugins[$plugin_type][$tag][0];
391  if (!is_array($function)) {
392  return $function($new_args, $this);
393  } else if (is_object($function[0])) {
394  return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
395  } else {
396  return call_user_func_array($function, array($new_args, $this));
397  }
398  } else {
399  return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
400  }
401  }
402  }
403  } else {
404  // compile closing tag of block function
405  $base_tag = substr($tag, 0, -5);
406  // check if closing tag is a registered object
407  if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
408  $methode = $parameter['object_methode'];
409  if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
410  return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
411  } else {
412  return $this->trigger_template_error('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
413  }
414  }
415  // registered block tag ?
416  if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
417  return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
418  }
419  // block plugin?
420  if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
421  return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
422  }
423  // registered compiler plugin ?
424  if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag])) {
425  // if compiler function plugin call it now
426  $args = array();
427  if (!$this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][1]) {
428  $this->tag_nocache = true;
429  }
430  $function = $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0];
431  if (!is_array($function)) {
432  return $function($args, $this);
433  } else if (is_object($function[0])) {
434  return $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0][0]->$function[1]($args, $this);
435  } else {
436  return call_user_func_array($function, array($args, $this));
437  }
438  }
439  if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
440  $plugin = 'smarty_compiler_' . $tag;
441  if (is_callable($plugin)) {
442  return $plugin($args, $this->smarty);
443  }
444  if (class_exists($plugin, false)) {
445  $plugin_object = new $plugin;
446  if (method_exists($plugin_object, 'compile')) {
447  return $plugin_object->compile($args, $this);
448  }
449  }
450  throw new SmartyException("Plugin \"{$tag}\" not callable");
451  }
452  }
453  $this->trigger_template_error("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
454  }
455  }
456 
471  public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) {
472  // re-use object if already exists
473  if (isset(self::$_tag_objects[$tag])) {
474  // compile this tag
475  return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
476  }
477  // lazy load internal compiler plugin
478  $class_name = 'Smarty_Internal_Compile_' . $tag;
479  if ($this->smarty->loadPlugin($class_name)) {
480  // check if tag allowed by security
481  if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
482  // use plugin if found
483  self::$_tag_objects[$tag] = new $class_name;
484  // compile this tag
485  return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
486  }
487  }
488  // no internal compile plugin for this tag
489  return false;
490  }
491 
499  public function getPlugin($plugin_name, $plugin_type) {
500  $function = null;
501  if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
502  if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
503  $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
504  } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
505  $this->template->required_plugins['nocache'][$plugin_name][$plugin_type] = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type];
506  $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
507  }
508  } else {
509  if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
510  $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
511  } else if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
512  $this->template->required_plugins['compiled'][$plugin_name][$plugin_type] = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type];
513  $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
514  }
515  }
516  if (isset($function)) {
517  if ($plugin_type == 'modifier') {
518  $this->modifier_plugins[$plugin_name] = true;
519  }
520  return $function;
521  }
522  // loop through plugin dirs and find the plugin
523  $function = 'smarty_' . $plugin_type . '_' . $plugin_name;
524  $file = $this->smarty->loadPlugin($function, false);
525 
526  if (is_string($file)) {
527  if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
528  $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = $file;
529  $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = $function;
530  } else {
531  $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = $file;
532  $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = $function;
533  }
534  if ($plugin_type == 'modifier') {
535  $this->modifier_plugins[$plugin_name] = true;
536  }
537  return $function;
538  }
539  if (is_callable($function)) {
540  // plugin function is defined in the script
541  return $function;
542  }
543  return false;
544  }
545 
553  public function getPluginFromDefaultHandler($tag, $plugin_type) {
554  $callback = null;
555  $script = null;
556  $cacheable = true;
557  $result = call_user_func_array(
558  $this->smarty->default_plugin_handler_func, array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable)
559  );
560  if ($result) {
561  $this->tag_nocache = $this->tag_nocache || !$cacheable;
562  if ($script !== null) {
563  if (is_file($script)) {
564  if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
565  $this->template->required_plugins['nocache'][$tag][$plugin_type]['file'] = $script;
566  $this->template->required_plugins['nocache'][$tag][$plugin_type]['function'] = $callback;
567  } else {
568  $this->template->required_plugins['compiled'][$tag][$plugin_type]['file'] = $script;
569  $this->template->required_plugins['compiled'][$tag][$plugin_type]['function'] = $callback;
570  }
571  include_once $script;
572  } else {
573  $this->trigger_template_error("Default plugin handler: Returned script file \"{$script}\" for \"{$tag}\" not found");
574  }
575  }
576  if (!is_string($callback) && !(is_array($callback) && is_string($callback[0]) && is_string($callback[1]))) {
577  $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" must be a static function name or array of class and function name");
578  }
579  if (is_callable($callback)) {
580  $this->default_handler_plugins[$plugin_type][$tag] = array($callback, true, array());
581  return true;
582  } else {
583  $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" not callable");
584  }
585  }
586  return false;
587  }
588 
600  public function processNocacheCode($content, $is_code) {
601  // If the template is not evaluated and we have a nocache section and or a nocache tag
602  if ($is_code && !empty($content)) {
603  // generate replacement code
604  if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
605  ($this->nocache || $this->tag_nocache || $this->forceNocache == 2)) {
606  $this->template->has_nocache_code = true;
607  $_output = addcslashes($content,'\'\\');
608  $_output = str_replace("^#^", "'", $_output);
609  $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n";
610  // make sure we include modifier plugins for nocache code
611  foreach ($this->modifier_plugins as $plugin_name => $dummy) {
612  if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
613  $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
614  }
615  }
616  } else {
617  $_output = $content;
618  }
619  } else {
620  $_output = $content;
621  }
622  $this->modifier_plugins = array();
623  $this->suppressNocacheProcessing = false;
624  $this->tag_nocache = false;
625  return $_output;
626  }
627 
640  public function trigger_template_error($args = null, $line = null) {
641  // get template source line which has error
642  if (!isset($line)) {
643  $line = $this->lex->line;
644  }
645  $match = preg_split("/\n/", $this->lex->data);
646  $error_text = 'Syntax Error in template "' . $this->template->source->filepath . '" on line ' . $line . ' "' . trim(preg_replace('![\t\r\n]+!', ' ', $match[$line - 1])) . '" ';
647  if (isset($args)) {
648  // individual error message
649  $error_text .= $args;
650  } else {
651  // expected token from parser
652  $error_text .= ' - Unexpected "' . $this->lex->value . '"';
653  if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4) {
654  foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
655  $exp_token = $this->parser->yyTokenName[$token];
656  if (isset($this->lex->smarty_token_names[$exp_token])) {
657  // token type from lexer
658  $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
659  } else {
660  // otherwise internal token name
661  $expect[] = $this->parser->yyTokenName[$token];
662  }
663  }
664  $error_text .= ', expected one of: ' . implode(' , ', $expect);
665  }
666  }
667  throw new SmartyCompilerException($error_text);
668  }
669 
670 }
671 
672 ?>
static runFilter($type, $content, Smarty_Internal_Template $template)
Run filters over content.
$result
getPlugin($plugin_name, $plugin_type)
Check for plugins and return function name.
const PLUGIN_FUNCTION
plugin types
processNocacheCode($content, $is_code)
Inject inline code for nocache template sections.
const PLUGIN_COMPILER
compileTemplate(Smarty_Internal_Template $template)
Method to compile a Smarty template.
compileTag($tag, $args, $parameter=array())
Compile Tag.
createTemplateCodeFrame($content= '', $cache=false)
Create code frame for compiled and cached templates.
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
getPluginFromDefaultHandler($tag, $plugin_type)
Check for plugins by default plugin handler.
trigger_template_error($args=null, $line=null)
display compiler error messages without dying
doCompile($_content)
Methode to compile a Smarty template.
callTagCompiler($tag, $args, $param1=null, $param2=null, $param3=null)
lazy loads internal compile plugin for tag and calls the compile methode
const PLUGIN_BLOCK
const SMARTY_VERSION
#@+ constant definitions

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:17 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.