Files
smarty/libs/sysplugins/smarty_internal_compile_insert.php

142 lines
5.1 KiB
PHP
Raw Normal View History

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