2009-11-06 14:35:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-25 19:33:55 +00:00
|
|
|
* Smarty Internal Plugin Compile Function
|
|
|
|
* Compiles the {function} {/function} tags
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2010-05-25 19:33:55 +00:00
|
|
|
* @subpackage Compiler
|
2014-06-06 02:40:04 +00:00
|
|
|
* @author Uwe Tews
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2010-08-17 15:39:51 +00:00
|
|
|
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-05-25 19:33:55 +00:00
|
|
|
* Smarty Internal Plugin Compile Function Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2015-05-14 15:11:38 +02:00
|
|
|
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2010-11-11 21:34:36 +00:00
|
|
|
public $required_attributes = array('name');
|
2015-05-14 15:11:38 +02:00
|
|
|
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2010-11-11 21:34:36 +00:00
|
|
|
public $shorttag_order = array('name');
|
2015-05-14 15:11:38 +02:00
|
|
|
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
|
|
|
public $optional_attributes = array('_any');
|
2010-11-11 21:34:36 +00:00
|
|
|
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-05-25 19:33:55 +00:00
|
|
|
* Compiles code for the {function} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2015-08-09 20:43:04 +02:00
|
|
|
* @param array $args array with attributes from parser
|
2015-08-06 01:19:11 +02:00
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
2015-08-09 20:43:04 +02:00
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-08-06 01:19:11 +02:00
|
|
|
* @return bool true
|
|
|
|
* @throws \SmartyCompilerException
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2015-08-06 01:19:11 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->loopNesting ++;
|
2009-11-06 14:35:00 +00:00
|
|
|
// check and get attributes
|
2011-09-16 14:19:56 +00:00
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
2010-11-11 21:34:36 +00:00
|
|
|
|
2016-02-09 01:27:15 +01:00
|
|
|
if ($_attr[ 'nocache' ] === true) {
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->trigger_template_error('nocache option not allowed', null, true);
|
2010-11-11 21:34:36 +00:00
|
|
|
}
|
2016-02-09 01:27:15 +01:00
|
|
|
unset($_attr[ 'nocache' ]);
|
|
|
|
$_name = trim($_attr[ 'name' ], "'\"");
|
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ] = array();
|
2015-08-09 20:43:04 +02:00
|
|
|
$save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
|
|
|
|
$compiler->template->caching);
|
2011-09-16 14:19:56 +00:00
|
|
|
$this->openTag($compiler, 'function', $save);
|
2014-06-06 02:40:04 +00:00
|
|
|
// Init temporary context
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
2015-08-09 20:43:04 +02:00
|
|
|
$compiler->template->compiled->has_nocache_code = false;
|
2009-11-06 14:35:00 +00:00
|
|
|
return true;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
|
|
|
|
/**
|
2010-05-25 19:33:55 +00:00
|
|
|
* Smarty Internal Plugin Compile Functionclose Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2015-05-14 15:11:38 +02:00
|
|
|
|
2014-11-01 22:42:34 +01:00
|
|
|
/**
|
|
|
|
* Compiler object
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $compiler = null;
|
|
|
|
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-05-25 19:33:55 +00:00
|
|
|
* Compiles code for the {/function} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2015-05-14 15:11:38 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param object|\Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-05-14 15:11:38 +02:00
|
|
|
* @return bool true
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2014-11-10 23:57:30 +01:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->loopNesting --;
|
2014-11-01 22:42:34 +01:00
|
|
|
$this->compiler = $compiler;
|
2011-09-16 14:19:56 +00:00
|
|
|
$saved_data = $this->closeTag($compiler, array('function'));
|
2016-02-09 01:27:15 +01:00
|
|
|
$_attr = $saved_data[ 0 ];
|
|
|
|
$_name = trim($_attr[ 'name' ], "'\"");
|
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ][ 'called_functions' ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'called_functions' ] =
|
|
|
|
$compiler->called_functions;
|
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'compiled_filepath' ] =
|
|
|
|
$compiler->parent_compiler->template->compiled->filepath;
|
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->called_functions = array();
|
|
|
|
$_parameter = $_attr;
|
2016-02-09 01:27:15 +01:00
|
|
|
unset($_parameter[ 'name' ]);
|
2014-11-01 22:42:34 +01:00
|
|
|
// default parameter
|
|
|
|
$_paramsArray = array();
|
|
|
|
foreach ($_parameter as $_key => $_value) {
|
|
|
|
if (is_int($_key)) {
|
|
|
|
$_paramsArray[] = "$_key=>$_value";
|
|
|
|
} else {
|
|
|
|
$_paramsArray[] = "'$_key'=>$_value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($_paramsArray)) {
|
|
|
|
$_params = 'array(' . implode(",", $_paramsArray) . ')';
|
2014-11-10 23:57:30 +01:00
|
|
|
$_paramsCode = "\$params = array_merge($_params, \$params);\n";
|
2014-11-01 22:42:34 +01:00
|
|
|
} else {
|
|
|
|
$_paramsCode = '';
|
|
|
|
}
|
2014-11-10 23:57:30 +01:00
|
|
|
$_functionCode = $compiler->parser->current_buffer;
|
2014-11-01 22:42:34 +01:00
|
|
|
// setup buffer for template function code
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
2014-11-01 22:42:34 +01:00
|
|
|
|
2015-08-09 20:43:04 +02:00
|
|
|
$_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
|
2014-11-01 22:42:34 +01:00
|
|
|
$_funcNameCaching = $_funcName . '_nocache';
|
2015-08-09 20:43:04 +02:00
|
|
|
if ($compiler->template->compiled->has_nocache_code) {
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
|
2014-11-01 22:42:34 +01:00
|
|
|
$output = "<?php\n";
|
|
|
|
$output .= "/* {$_funcNameCaching} */\n";
|
|
|
|
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
|
|
|
|
$output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
|
2014-11-10 23:57:30 +01:00
|
|
|
$output .= "ob_start();\n";
|
2015-08-09 20:43:04 +02:00
|
|
|
$output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
|
2014-12-30 16:16:54 +01:00
|
|
|
$output .= $_paramsCode;
|
2015-08-09 20:43:04 +02:00
|
|
|
$output .= "\$_smarty_tpl->_cache['saved_tpl_vars'][] = \$_smarty_tpl->tpl_vars;\n";
|
2015-01-06 01:18:58 +01:00
|
|
|
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}";
|
2014-11-10 23:57:30 +01:00
|
|
|
$output .= "\$params = var_export(\$params, true);\n";
|
2015-08-09 20:43:04 +02:00
|
|
|
$output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
|
2015-01-06 01:18:58 +01:00
|
|
|
$output .= "\\\$saved_tpl_vars = \\\$_smarty_tpl->tpl_vars;\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value);\n}\n?>";
|
2015-08-09 20:43:04 +02:00
|
|
|
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
2015-08-09 20:43:04 +02:00
|
|
|
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
|
2015-03-20 18:25:59 +01:00
|
|
|
$output .= "foreach (Smarty::\\\$global_tpl_vars as \\\$key => \\\$value){\n";
|
2015-10-29 21:41:21 +01:00
|
|
|
$output .= "if (!isset(\\\$_smarty_tpl->tpl_vars[\\\$key]) || \\\$_smarty_tpl->tpl_vars[\\\$key] === \\\$value) \\\$saved_tpl_vars[\\\$key] = \\\$value;\n}\n";
|
2015-03-20 18:25:59 +01:00
|
|
|
$output .= "\\\$_smarty_tpl->tpl_vars = \\\$saved_tpl_vars;?>\n";
|
2015-08-09 20:43:04 +02:00
|
|
|
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
|
|
|
|
$output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars = array_pop(\$_smarty_tpl->_cache['saved_tpl_vars']);\n}\n}\n";
|
2014-11-10 23:57:30 +01:00
|
|
|
$output .= "/*/ {$_funcName}_nocache */\n\n";
|
2014-11-01 22:42:34 +01:00
|
|
|
$output .= "?>\n";
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
|
|
|
$_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
|
|
|
|
array($this, 'removeNocache'),
|
|
|
|
$_functionCode->to_smarty_php($compiler->parser)));
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] =
|
|
|
|
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
|
2014-11-01 22:42:34 +01:00
|
|
|
$output = "<?php\n";
|
|
|
|
$output .= "/* {$_funcName} */\n";
|
|
|
|
$output .= "if (!function_exists('{$_funcName}')) {\n";
|
|
|
|
$output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
|
|
|
|
$output .= "\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
|
|
|
|
$output .= $_paramsCode;
|
2015-01-06 01:18:58 +01:00
|
|
|
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}?>";
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
2015-03-20 18:25:59 +01:00
|
|
|
$output = "<?php foreach (Smarty::\$global_tpl_vars as \$key => \$value){\n";
|
2015-10-29 21:41:21 +01:00
|
|
|
$output .= "if (!isset(\$_smarty_tpl->tpl_vars[\$key]) || \$_smarty_tpl->tpl_vars[\$key] === \$value) \$saved_tpl_vars[\$key] = \$value;\n}\n";
|
2015-03-20 18:25:59 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars = \$saved_tpl_vars;\n}\n}\n";
|
2014-11-10 23:57:30 +01:00
|
|
|
$output .= "/*/ {$_funcName} */\n\n";
|
2014-11-01 22:42:34 +01:00
|
|
|
$output .= "?>\n";
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-09-16 16:23:38 +02:00
|
|
|
$compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
|
2015-08-09 20:53:32 +02:00
|
|
|
// nocache plugins must be copied
|
2016-02-09 01:27:15 +01:00
|
|
|
if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
|
|
|
|
foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
|
2015-08-09 20:53:32 +02:00
|
|
|
foreach ($tmp as $type => $data) {
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
|
|
|
|
$data;
|
2015-08-09 20:53:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-01 22:42:34 +01:00
|
|
|
// restore old buffer
|
2015-08-09 20:43:04 +02:00
|
|
|
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->parser->current_buffer = $saved_data[ 1 ];
|
2014-11-01 22:42:34 +01:00
|
|
|
// restore old status
|
2016-02-09 01:27:15 +01:00
|
|
|
$compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
|
|
|
|
$compiler->template->caching = $saved_data[ 3 ];
|
2014-11-01 22:42:34 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-14 22:15:45 +00:00
|
|
|
|
2014-11-01 22:42:34 +01:00
|
|
|
/**
|
|
|
|
* @param $match
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function removeNocache($match)
|
|
|
|
{
|
2016-02-09 01:27:15 +01:00
|
|
|
$code =
|
|
|
|
preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
|
|
|
|
'', $match[ 0 ]);
|
2014-11-01 22:42:34 +01:00
|
|
|
$code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
|
|
|
|
return $code;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|