library/smarty/libs/sysplugins/smarty_internal_compile_insert.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Smarty Internal Plugin Compile Insert
       
     5  * Compiles the {insert} tag
       
     6  *
       
     7  * @package    Smarty
       
     8  * @subpackage Compiler
       
     9  * @author     Uwe Tews
       
    10  */
       
    11 
       
    12 /**
       
    13  * Smarty Internal Plugin Compile Insert Class
       
    14  *
       
    15  * @package    Smarty
       
    16  * @subpackage Compiler
       
    17  */
       
    18 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase {
       
    19     /**
       
    20      * Attribute definition: Overwrites base class.
       
    21      *
       
    22      * @var array
       
    23      * @see Smarty_Internal_CompileBase
       
    24      */
       
    25     public $required_attributes = array('name');
       
    26     /**
       
    27      * Attribute definition: Overwrites base class.
       
    28      *
       
    29      * @var array
       
    30      * @see Smarty_Internal_CompileBase
       
    31      */
       
    32     public $shorttag_order = array('name');
       
    33     /**
       
    34      * Attribute definition: Overwrites base class.
       
    35      *
       
    36      * @var array
       
    37      * @see Smarty_Internal_CompileBase
       
    38      */
       
    39     public $optional_attributes = array('_any');
       
    40 
       
    41     /**
       
    42      * Compiles code for the {insert} tag
       
    43      *
       
    44      * @param  array $args array with attributes from parser
       
    45      * @param  object $compiler compiler object
       
    46      *
       
    47      * @return string compiled code
       
    48      */
       
    49     public function compile($args, $compiler) {
       
    50         // check and get attributes
       
    51         $_attr = $this->getAttributes($compiler, $args);
       
    52         $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
       
    53         if (!$nocacheParam) {
       
    54             // do not compile as nocache code
       
    55             $compiler->suppressNocacheProcessing = true;
       
    56         }
       
    57         $compiler->tag_nocache = true;
       
    58         $_smarty_tpl = $compiler->template;
       
    59         $_name = null;
       
    60         $_script = null;
       
    61 
       
    62         $_output = '<?php ';
       
    63         // save possible attributes
       
    64         eval('$_name = ' . $_attr['name'] . ';');
       
    65         if (isset($_attr['assign'])) {
       
    66             // output will be stored in a smarty variable instead of being displayed
       
    67             $_assign = $_attr['assign'];
       
    68             // create variable to make sure that the compiler knows about its nocache status
       
    69             $var = trim($_attr['assign'], "'");
       
    70             if (isset($compiler->template->tpl_vars[$var])) {
       
    71                 $compiler->template->tpl_vars[$var]->nocache = true;
       
    72             } else {
       
    73                 $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
       
    74             }
       
    75         }
       
    76         if (isset($_attr['script'])) {
       
    77             // script which must be included
       
    78             $_function = "smarty_insert_{$_name}";
       
    79             $_smarty_tpl = $compiler->template;
       
    80             $_filepath = false;
       
    81             eval('$_script = ' . $_attr['script'] . ';');
       
    82             if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
       
    83                 $_filepath = $_script;
       
    84             } else {
       
    85                 if (isset($compiler->smarty->security_policy)) {
       
    86                     $_dir = $compiler->smarty->security_policy->trusted_dir;
       
    87                 } else {
       
    88                     $_dir = $compiler->smarty->trusted_dir;
       
    89                 }
       
    90                 if (!empty($_dir)) {
       
    91                     foreach ((array)$_dir as $_script_dir) {
       
    92                         $_script_dir = rtrim($_script_dir, '/\\') . DS;
       
    93                         if (file_exists($_script_dir . $_script)) {
       
    94                             $_filepath = $_script_dir . $_script;
       
    95                             break;
       
    96                         }
       
    97                     }
       
    98                 }
       
    99             }
       
   100             if ($_filepath == false) {
       
   101                 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", $compiler->lex->taglineno);
       
   102             }
       
   103             // code for script file loading
       
   104             $_output .= "require_once '{$_filepath}' ;";
       
   105             require_once $_filepath;
       
   106             if (!is_callable($_function)) {
       
   107                 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $compiler->lex->taglineno);
       
   108             }
       
   109         } else {
       
   110             $_filepath = 'null';
       
   111             $_function = "insert_{$_name}";
       
   112             // function in PHP script ?
       
   113             if (!is_callable($_function)) {
       
   114                 // try plugin
       
   115                 if (!$_function = $compiler->getPlugin($_name, 'insert')) {
       
   116                     $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $compiler->lex->taglineno);
       
   117                 }
       
   118             }
       
   119         }
       
   120         // delete {insert} standard attributes
       
   121         unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']);
       
   122         // convert attributes into parameter array string
       
   123         $_paramsArray = array();
       
   124         foreach ($_attr as $_key => $_value) {
       
   125             $_paramsArray[] = "'$_key' => $_value";
       
   126         }
       
   127         $_params = 'array(' . implode(", ", $_paramsArray) . ')';
       
   128         // call insert
       
   129         if (isset($_assign)) {
       
   130             if ($_smarty_tpl->caching && !$nocacheParam) {
       
   131                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
       
   132             } else {
       
   133                 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
       
   134             }
       
   135         } else {
       
   136             $compiler->has_output = true;
       
   137             if ($_smarty_tpl->caching && !$nocacheParam) {
       
   138                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
       
   139             } else {
       
   140                 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
       
   141             }
       
   142         }
       
   143 
       
   144         return $_output;
       
   145     }
       
   146 }