fusionpbx/resources/templates/engine/smarty/sysplugins/smarty_internal_compile_ins...

157 lines
5.8 KiB
PHP
Raw Normal View History

2013-11-09 21:02:56 +01:00
<?php
/**
* Smarty Internal Plugin Compile Insert
* Compiles the {insert} tag
*
2018-11-07 08:18:14 +01:00
* @package Smarty
2013-11-09 21:02:56 +01:00
* @subpackage Compiler
2018-11-07 08:18:14 +01:00
* @author Uwe Tews
2013-11-09 21:02:56 +01:00
*/
/**
* Smarty Internal Plugin Compile Insert Class
*
2018-11-07 08:18:14 +01:00
* @package Smarty
2013-11-09 21:02:56 +01:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $required_attributes = array('name');
2018-11-07 08:18:14 +01:00
2013-11-09 21:02:56 +01:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $shorttag_order = array('name');
2018-11-07 08:18:14 +01:00
2013-11-09 21:02:56 +01:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('_any');
/**
* Compiles code for the {insert} tag
*
2018-11-07 08:18:14 +01:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
2013-11-09 21:02:56 +01:00
* @return string compiled code
2018-11-07 08:18:14 +01:00
* @throws \SmartyCompilerException
* @throws \SmartyException
2013-11-09 21:02:56 +01:00
*/
2018-11-07 08:18:14 +01:00
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
2013-11-09 21:02:56 +01:00
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
2018-11-07 08:18:14 +01:00
$nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
if (!$nocacheParam) {
// do not compile as nocache code
$compiler->suppressNocacheProcessing = true;
}
2013-11-09 21:02:56 +01:00
$compiler->tag_nocache = true;
$_smarty_tpl = $compiler->template;
$_name = null;
$_script = null;
$_output = '<?php ';
2018-11-07 08:18:14 +01:00
// save possible attributes
eval('$_name = @' . $_attr[ 'name' ] . ';');
if (isset($_attr[ 'assign' ])) {
2013-11-09 21:02:56 +01:00
// output will be stored in a smarty variable instead of being displayed
2018-11-07 08:18:14 +01:00
$_assign = $_attr[ 'assign' ];
// create variable to make sure that the compiler knows about its nocache status
$var = trim($_attr[ 'assign' ], '\'');
if (isset($compiler->template->tpl_vars[ $var ])) {
$compiler->template->tpl_vars[ $var ]->nocache = true;
} else {
$compiler->template->tpl_vars[ $var ] = new Smarty_Variable(null, true);
}
2013-11-09 21:02:56 +01:00
}
2018-11-07 08:18:14 +01:00
if (isset($_attr[ 'script' ])) {
2013-11-09 21:02:56 +01:00
// script which must be included
$_function = "smarty_insert_{$_name}";
$_smarty_tpl = $compiler->template;
$_filepath = false;
2018-11-07 08:18:14 +01:00
eval('$_script = @' . $_attr[ 'script' ] . ';');
2013-11-09 21:02:56 +01:00
if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
$_filepath = $_script;
} else {
if (isset($compiler->smarty->security_policy)) {
$_dir = $compiler->smarty->security_policy->trusted_dir;
} else {
2018-11-07 08:18:14 +01:00
$_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
2013-11-09 21:02:56 +01:00
}
if (!empty($_dir)) {
2018-11-07 08:18:14 +01:00
foreach ((array)$_dir as $_script_dir) {
$_script_dir = rtrim($_script_dir, '/\\') . DIRECTORY_SEPARATOR;
2013-11-09 21:02:56 +01:00
if (file_exists($_script_dir . $_script)) {
$_filepath = $_script_dir . $_script;
break;
}
}
}
}
2018-11-07 08:18:14 +01:00
if ($_filepath === false) {
$compiler->trigger_template_error("{insert} missing script file '{$_script}'", null, true);
2013-11-09 21:02:56 +01:00
}
// code for script file loading
$_output .= "require_once '{$_filepath}' ;";
2018-11-07 08:18:14 +01:00
include_once $_filepath;
2013-11-09 21:02:56 +01:00
if (!is_callable($_function)) {
2018-11-07 08:18:14 +01:00
$compiler->trigger_template_error(
" {insert} function '{$_function}' is not callable in script file '{$_script}'",
null,
true
);
2013-11-09 21:02:56 +01:00
}
} else {
$_filepath = 'null';
$_function = "insert_{$_name}";
// function in PHP script ?
if (!is_callable($_function)) {
// try plugin
if (!$_function = $compiler->getPlugin($_name, 'insert')) {
2018-11-07 08:18:14 +01:00
$compiler->trigger_template_error(
"{insert} no function or plugin found for '{$_name}'",
null,
true
);
2013-11-09 21:02:56 +01:00
}
}
}
// delete {insert} standard attributes
2018-11-07 08:18:14 +01:00
unset($_attr[ 'name' ], $_attr[ 'assign' ], $_attr[ 'script' ], $_attr[ 'nocache' ]);
2013-11-09 21:02:56 +01:00
// convert attributes into parameter array string
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
$_paramsArray[] = "'$_key' => $_value";
}
$_params = 'array(' . implode(", ", $_paramsArray) . ')';
// call insert
if (isset($_assign)) {
2018-11-07 08:18:14 +01:00
if ($_smarty_tpl->caching && !$nocacheParam) {
2013-11-09 21:02:56 +01:00
$_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
} else {
$_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
}
} else {
2018-11-07 08:18:14 +01:00
if ($_smarty_tpl->caching && !$nocacheParam) {
2013-11-09 21:02:56 +01:00
$_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
} else {
$_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
}
}
return $_output;
}
}