<?php
abstract class Smarty_Internal_TemplateCompilerBase {
private $nocache_hash = null;
public $suppressNocacheProcessing = false;
public $suppressMergedTemplates = false;
public static $_tag_objects = array();
public $_tag_stack = array();
public $template = null;
public $merged_templates = array();
public $inheritance = false;
public $default_handler_plugins = array();
public $default_modifier_list = null;
public $forceNocache = false;
public $suppressHeader = false;
public $suppressTemplatePropertyHeader = false;
public $write_compiled_code = true;
public $compiles_template_function = false;
public $called_functions = array();
public $modifier_plugins = array();
public function __construct()
{
$this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
}
public function compileTemplate(Smarty_Internal_Template $template)
{
if (empty($template->properties['nocache_hash'])) {
$template->properties['nocache_hash'] = $this->nocache_hash;
} else {
$this->nocache_hash = $template->properties['nocache_hash'];
}
$this->nocache = false;
$this->tag_nocache = false;
$this->template = $template;
$this->template->has_nocache_code = false;
$this->smarty->_current_file = $saved_filepath = $this->template->source->filepath;
$template_header = '';
if (!$this->suppressHeader) {
$template_header .= "<?php ?>\n";
}
do {
$this->abort_and_recompile = false;
$_content = $template->source->content;
if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
$template->source->content = $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
}
if ($_content == '') {
if ($this->suppressTemplatePropertyHeader) {
$code = '';
} else {
$code = $template_header . $template->createTemplateCodeFrame();
}
return $code;
}
$_compiled_code = $this->doCompile($_content);
} while ($this->abort_and_recompile);
$this->template->source->filepath = $saved_filepath;
unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex, $this->template);
self::$_tag_objects = array();
$merged_code = '';
if (!$this->suppressMergedTemplates) {
foreach ($this->merged_templates as $code) {
$merged_code .= $code;
}
}
if ($this->suppressTemplatePropertyHeader) {
$code = $_compiled_code . $merged_code;
} else {
$code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code;
}
if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
$code = Smarty_Internal_Filter_Handler::runFilter('post', $code, $template);
}
return $code;
}
public function compileTag($tag, $args, $parameter = array())
{
$this->has_code = true;
$this->has_output = false;
if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
$this->template->used_tags[] = array($tag, $args);
}
if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args)
|| in_array(array('nocache'=>'"true"'),$args) || in_array(array('nocache'=>"'true'"),$args)) {
$this->tag_nocache = true;
}
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
if (isset($this->smarty->template_functions[$tag])) {
$args['_attr']['name'] = "'" . $tag . "'";
$_output = $this->callTagCompiler('call', $args, $parameter);
}
}
if ($_output !== false) {
if ($_output !== true) {
if ($this->has_code) {
if ($this->has_output) {
$_output .= "\n";
}
return $_output;
}
}
return '';
} else {
if (isset($args['_attr'])) {
foreach ($args['_attr'] as $key => $attribute) {
if (is_array($attribute)) {
$args = array_merge($args, $attribute);
}
}
}
if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
$methode = $parameter['object_methode'];
if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
(empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
} elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
} else {
return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
}
}
foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) {
if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) {
if ($plugin_type == Smarty::PLUGIN_COMPILER) {
$new_args = array();
foreach ($args as $key => $mixed) {
if (is_array($mixed)) {
$new_args = array_merge($new_args, $mixed);
} else {
$new_args[$key] = $mixed;
}
}
if (!$this->smarty->registered_plugins[$plugin_type][$tag][1]) {
$this->tag_nocache = true;
}
$function = $this->smarty->registered_plugins[$plugin_type][$tag][0];
if (!is_array($function)) {
return $function($new_args, $this);
} else if (is_object($function[0])) {
return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
} else {
return call_user_func_array($function, array($new_args, $this));
}
}
if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) {
return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
}
}
}
foreach ($this->smarty->plugin_search_order as $plugin_type) {
if ($plugin_type == Smarty::PLUGIN_BLOCK && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))) {
$plugin = 'smarty_compiler_' . $tag;
if (is_callable($plugin)) {
$new_args = array();
foreach ($args as $key => $mixed) {
if (is_array($mixed)) {
$new_args = array_merge($new_args, $mixed);
} else {
$new_args[$key] = $mixed;
}
}
return $plugin($new_args, $this->smarty);
}
if (class_exists($plugin, false)) {
$plugin_object = new $plugin;
if (method_exists($plugin_object, 'compile')) {
return $plugin_object->compile($args, $this);
}
}
throw new SmartyException("Plugin \"{$tag}\" not callable");
} else {
if ($function = $this->getPlugin($tag, $plugin_type)) {
if(!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
}
}
}
}
if (is_callable($this->smarty->default_plugin_handler_func)) {
$found = false;
foreach ($this->smarty->plugin_search_order as $plugin_type) {
if (isset($this->default_handler_plugins[$plugin_type][$tag])) {
$found = true;
break;
}
}
if (!$found) {
foreach ($this->smarty->plugin_search_order as $plugin_type) {
if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) {
$found = true;
break;
}
}
}
if ($found) {
if ($plugin_type == Smarty::PLUGIN_COMPILER) {
$new_args = array();
foreach ($args as $mixed) {
$new_args = array_merge($new_args, $mixed);
}
$function = $this->default_handler_plugins[$plugin_type][$tag][0];
if (!is_array($function)) {
return $function($new_args, $this);
} else if (is_object($function[0])) {
return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
} else {
return call_user_func_array($function, array($new_args, $this));
}
} else {
return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
}
}
}
} else {
$base_tag = substr($tag, 0, -5);
if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
$methode = $parameter['object_methode'];
if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
} else {
return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
}
}
if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
}
if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
}
if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
$plugin = 'smarty_compiler_' . $tag;
if (is_callable($plugin)) {
return $plugin($args, $this->smarty);
}
if (class_exists($plugin, false)) {
$plugin_object = new $plugin;
if (method_exists($plugin_object, 'compile')) {
return $plugin_object->compile($args, $this);
}
}
throw new SmartyException("Plugin \"{$tag}\" not callable");
}
}
$this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
}
}
public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
{
if (isset(self::$_tag_objects[$tag])) {
return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
}
$class_name = 'Smarty_Internal_Compile_' . $tag;
if ($this->smarty->loadPlugin($class_name)) {
if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
self::$_tag_objects[$tag] = new $class_name;
return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
}
}
return false;
}
public function getPlugin($plugin_name, $plugin_type)
{
$function = null;
if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
$function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
} else if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
$this->template->required_plugins['nocache'][$plugin_name][$plugin_type] = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type];
$function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
}
} else {
if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
$function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
} else if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
$this->template->required_plugins['compiled'][$plugin_name][$plugin_type] = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type];
$function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
}
}
if (isset($function)) {
if ($plugin_type == 'modifier') {
$this->modifier_plugins[$plugin_name] = true;
}
return $function;
}
$function = 'smarty_' . $plugin_type . '_' . $plugin_name;
$file = $this->smarty->loadPlugin($function, false);
if (is_string($file)) {
if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
$this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = $file;
$this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = $function;
} else {
$this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = $file;
$this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = $function;
}
if ($plugin_type == 'modifier') {
$this->modifier_plugins[$plugin_name] = true;
}
return $function;
}
if (is_callable($function)) {
return $function;
}
return false;
}
public function getPluginFromDefaultHandler($tag, $plugin_type)
{
$callback = null;
$script = null;
$result = call_user_func_array(
$this->smarty->default_plugin_handler_func,
array($tag, $plugin_type, $this->template, &$callback, &$script)
);
if ($result) {
if ($script !== null) {
if (is_file($script)) {
if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
$this->template->required_plugins['nocache'][$tag][$plugin_type]['file'] = $script;
$this->template->required_plugins['nocache'][$tag][$plugin_type]['function'] = $callback;
} else {
$this->template->required_plugins['compiled'][$tag][$plugin_type]['file'] = $script;
$this->template->required_plugins['compiled'][$tag][$plugin_type]['function'] = $callback;
}
include_once $script;
} else {
$this->trigger_template_error("Default plugin handler: Returned script file \"{$script}\" for \"{$tag}\" not found");
}
}
if (!is_string($callback) && !(is_array($callback) && is_string($callback[0]) && is_string($callback[1]))) {
$this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" must be a static function name or array of class and function name");
}
if (is_callable($callback)) {
$this->default_handler_plugins[$plugin_type][$tag] = array($callback, true, array());
return true;
} else {
$this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" not callable");
}
}
return false;
}
public function processNocacheCode($content, $is_code)
{
if ($is_code && !empty($content)) {
if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
($this->nocache || $this->tag_nocache || $this->forceNocache == 2)) {
$this->template->has_nocache_code = true;
$_output = str_replace("'", "\'", $content);
$_output = str_replace('\\\\', '\\\\\\\\', $_output);
$_output = str_replace("^#^", "'", $_output);
$_output = "<?php echo '" . $_output . "';?>\n";
foreach ($this->modifier_plugins as $plugin_name => $dummy) {
if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
$this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
}
}
} else {
$_output = $content;
}
} else {
$_output = $content;
}
$this->modifier_plugins = array();
$this->suppressNocacheProcessing = false;
$this->tag_nocache = false;
return $_output;
}
public function trigger_template_error($args = null, $line = null)
{
if (!isset($line)) {
$line = $this->lex->line;
}
$match = preg_split("/\n/", $this->lex->data);
$error_text = 'Syntax Error in template "' . $this->template->source->filepath . '" on line ' . $line . ' "' . htmlspecialchars(trim(preg_replace('![\t\r\n]+!',' ',$match[$line-1]))) . '" ';
if (isset($args)) {
$error_text .= $args;
} else {
$error_text .= ' - Unexpected "' . $this->lex->value.'"';
if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4 ) {
foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
$exp_token = $this->parser->yyTokenName[$token];
if (isset($this->lex->smarty_token_names[$exp_token])) {
$expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
} else {
$expect[] = $this->parser->yyTokenName[$token];
}
}
$error_text .= ', expected one of: ' . implode(' , ', $expect);
}
}
throw new SmartyCompilerException($error_text);
}
}
?>