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
|
|
|
|
{
|
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');
|
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');
|
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
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param object $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
|
|
|
*
|
2011-09-16 14:19:56 +00:00
|
|
|
* @return boolean true
|
2010-05-25 19:33:55 +00:00
|
|
|
*/
|
2010-11-11 21:34:36 +00:00
|
|
|
public function compile($args, $compiler, $parameter)
|
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
|
|
|
|
|
|
|
if ($_attr['nocache'] === true) {
|
2011-09-16 14:19:56 +00:00
|
|
|
$compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
|
2010-11-11 21:34:36 +00:00
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
unset($_attr['nocache']);
|
2014-11-01 22:42:34 +01:00
|
|
|
$_name = trim($_attr['name'], "'\"");
|
|
|
|
|
2010-07-14 22:47:37 +00:00
|
|
|
$save = array($_attr, $compiler->parser->current_buffer,
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->template->has_nocache_code, $compiler->template->required_plugins, $compiler->template->caching);
|
2011-09-16 14:19:56 +00:00
|
|
|
$this->openTag($compiler, 'function', $save);
|
2011-09-28 15:56:01 +00:00
|
|
|
// set flag that we are compiling a template function
|
|
|
|
$compiler->compiles_template_function = true;
|
2014-06-06 02:40:04 +00:00
|
|
|
// Init temporary context
|
2010-03-25 20:43:56 +00:00
|
|
|
$compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
|
2010-07-14 22:47:37 +00:00
|
|
|
$compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
|
2009-12-29 20:12:11 +00:00
|
|
|
$compiler->template->has_nocache_code = false;
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->template->caching = true;
|
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
|
|
|
|
{
|
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
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param object $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
|
|
|
*
|
2010-05-25 19:33:55 +00:00
|
|
|
* @return boolean true
|
|
|
|
*/
|
2010-11-11 21:34:36 +00:00
|
|
|
public function compile($args, $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
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'));
|
2014-11-01 22:42:34 +01:00
|
|
|
$_attr = $saved_data[0];
|
|
|
|
$_name = trim($_attr['name'], "'\"");
|
|
|
|
// reset flag that we are compiling a template function
|
|
|
|
$compiler->compiles_template_function = false;
|
|
|
|
$compiler->parent_compiler->templateProperties['tpl_function']['param'][$_name]['called_functions'] = $compiler->called_functions;
|
|
|
|
$compiler->parent_compiler->templateProperties['tpl_function']['param'][$_name]['compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
|
|
|
|
$compiler->called_functions = array();
|
|
|
|
$_parameter = $_attr;
|
|
|
|
unset($_parameter['name']);
|
|
|
|
// 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) . ')';
|
|
|
|
$_paramsCode = "\$params = array_merge(($_params), \$params);\n";
|
|
|
|
} else {
|
|
|
|
$_paramsCode = '';
|
|
|
|
}
|
|
|
|
$_functionCode = $compiler->parser->current_buffer->to_smarty_php();
|
|
|
|
// setup buffer for template function code
|
|
|
|
$compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
|
|
|
|
|
|
|
|
$_funcName = "smarty_template_function_{$_name}_{$compiler->template->properties['nocache_hash']}";
|
|
|
|
$_funcNameCaching = $_funcName . '_nocache';
|
|
|
|
if ($compiler->template->has_nocache_code) {
|
|
|
|
$compiler->parent_compiler->templateProperties['tpl_function']['param'][$_name]['call_name_caching'] = $_funcNameCaching;
|
|
|
|
$output = "<?php\n";
|
|
|
|
$output .= "/* {$_funcNameCaching} */\n";
|
|
|
|
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
|
|
|
|
$output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
|
|
|
|
// build plugin include code
|
|
|
|
if (!empty($compiler->template->required_plugins['compiled'])) {
|
|
|
|
foreach ($compiler->template->required_plugins['compiled'] as $tmp) {
|
|
|
|
foreach ($tmp as $data) {
|
|
|
|
$output .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-01 22:42:34 +01:00
|
|
|
if (!empty($compiler->template->required_plugins['nocache'])) {
|
|
|
|
$output .= "echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
|
|
|
|
foreach ($compiler->template->required_plugins['nocache'] as $tmp) {
|
|
|
|
foreach ($tmp as $data) {
|
|
|
|
$output .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';\n";
|
|
|
|
}
|
|
|
|
$output .= "\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
|
|
|
|
$output .= $_paramsCode;
|
|
|
|
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);\n}\n";
|
|
|
|
$output .= "ob_start();\n?>";
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $_functionCode));
|
|
|
|
$output = "<?php echo str_replace('{$compiler->template->properties['nocache_hash']}', \"{\$_smarty_tpl->properties['nocache_hash']}\", ob_get_clean());\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars = \$saved_tpl_vars;
|
|
|
|
foreach (Smarty::\$global_tpl_vars as \$key => \$value) if(!isset(\$_smarty_tpl->tpl_vars[\$key])) \$_smarty_tpl->tpl_vars[\$key] = \$value;\n}\n}\n";
|
|
|
|
$output .= "/*/ {$_funcName}_nocache */\n";
|
|
|
|
$output .= "?>\n";
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
|
|
|
|
$_functionCode = preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/", array($this, 'removeNocache'), $_functionCode);
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->parent_compiler->templateProperties['tpl_function']['param'][$_name]['call_name'] = $_funcName;
|
|
|
|
$output = "<?php\n";
|
|
|
|
$output .= "/* {$_funcName} */\n";
|
|
|
|
$output .= "if (!function_exists('{$_funcName}')) {\n";
|
|
|
|
$output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
|
|
|
|
// build plugin include code
|
2010-03-25 20:43:56 +00:00
|
|
|
if (!empty($compiler->template->required_plugins['nocache'])) {
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->template->required_plugins['compiled'] = array_merge($compiler->template->required_plugins['compiled'], $compiler->template->required_plugins['nocache']);
|
|
|
|
}
|
|
|
|
if (!empty($compiler->template->required_plugins['compiled'])) {
|
|
|
|
foreach ($compiler->template->required_plugins['compiled'] as $tmp) {
|
2013-07-14 22:15:45 +00:00
|
|
|
foreach ($tmp as $data) {
|
2014-11-01 22:42:34 +01:00
|
|
|
$output .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-01 22:42:34 +01:00
|
|
|
$output .= "\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
|
|
|
|
$output .= $_paramsCode;
|
|
|
|
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);\n}?>";
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $_functionCode));
|
|
|
|
$output = "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;
|
|
|
|
foreach (Smarty::\$global_tpl_vars as \$key => \$value) if(!isset(\$_smarty_tpl->tpl_vars[\$key])) \$_smarty_tpl->tpl_vars[\$key] = \$value;\n}\n}\n";
|
|
|
|
$output .= "/*/ {$_funcName} */\n";
|
|
|
|
$output .= "?>\n";
|
|
|
|
$compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
|
|
|
|
$compiler->parent_compiler->templateFunctionCode .= $compiler->parser->current_buffer->to_smarty_php();
|
|
|
|
// restore old buffer
|
2010-07-14 22:47:37 +00:00
|
|
|
$compiler->parser->current_buffer = $saved_data[1];
|
2014-11-01 22:42:34 +01:00
|
|
|
// restore old status
|
|
|
|
$compiler->template->has_nocache_code = $saved_data[2];
|
2010-07-14 22:47:37 +00:00
|
|
|
$compiler->template->required_plugins = $saved_data[3];
|
2014-11-01 22:42:34 +01:00
|
|
|
$compiler->template->caching = $saved_data[4];
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$code = preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/", '', $match[0]);
|
|
|
|
$code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
|
|
|
|
return $code;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|