library/smarty/libs/sysplugins/smarty_internal_compile_include_php.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Compile Include PHP
       
     4  * Compiles the {include_php} tag
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile Insert Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Attribute definition: Overwrites base class.
       
    20      *
       
    21      * @var array
       
    22      * @see Smarty_Internal_CompileBase
       
    23      */
       
    24     public $required_attributes = array('file');
       
    25     /**
       
    26      * Attribute definition: Overwrites base class.
       
    27      *
       
    28      * @var array
       
    29      * @see Smarty_Internal_CompileBase
       
    30      */
       
    31     public $shorttag_order = array('file');
       
    32     /**
       
    33      * Attribute definition: Overwrites base class.
       
    34      *
       
    35      * @var array
       
    36      * @see Smarty_Internal_CompileBase
       
    37      */
       
    38     public $optional_attributes = array('once', 'assign');
       
    39 
       
    40     /**
       
    41      * Compiles code for the {include_php} tag
       
    42      *
       
    43      * @param  array $args array with attributes from parser
       
    44      * @param  object $compiler compiler object
       
    45      *
       
    46      * @throws SmartyException
       
    47      * @return string compiled code
       
    48      */
       
    49     public function compile($args, $compiler) {
       
    50         if (!($compiler->smarty instanceof SmartyBC)) {
       
    51             throw new SmartyException("{include_php} is deprecated, use SmartyBC class to enable");
       
    52         }
       
    53         // check and get attributes
       
    54         $_attr = $this->getAttributes($compiler, $args);
       
    55 
       
    56         /** @var Smarty_Internal_Template $_smarty_tpl
       
    57          * used in evaluated code
       
    58          */
       
    59         $_smarty_tpl = $compiler->template;
       
    60         $_filepath = false;
       
    61         eval('$_file = ' . $_attr['file'] . ';');
       
    62         if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
       
    63             $_filepath = $_file;
       
    64         } else {
       
    65             if (isset($compiler->smarty->security_policy)) {
       
    66                 $_dir = $compiler->smarty->security_policy->trusted_dir;
       
    67             } else {
       
    68                 $_dir = $compiler->smarty->trusted_dir;
       
    69             }
       
    70             if (!empty($_dir)) {
       
    71                 foreach ((array)$_dir as $_script_dir) {
       
    72                     $_script_dir = rtrim($_script_dir, '/\\') . DS;
       
    73                     if (file_exists($_script_dir . $_file)) {
       
    74                         $_filepath = $_script_dir . $_file;
       
    75                         break;
       
    76                     }
       
    77                 }
       
    78             }
       
    79         }
       
    80         if ($_filepath == false) {
       
    81             $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", $compiler->lex->taglineno);
       
    82         }
       
    83 
       
    84         if (isset($compiler->smarty->security_policy)) {
       
    85             $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
       
    86         }
       
    87 
       
    88         if (isset($_attr['assign'])) {
       
    89             // output will be stored in a smarty variable instead of being displayed
       
    90             $_assign = $_attr['assign'];
       
    91         }
       
    92         $_once = '_once';
       
    93         if (isset($_attr['once'])) {
       
    94             if ($_attr['once'] == 'false') {
       
    95                 $_once = '';
       
    96             }
       
    97         }
       
    98 
       
    99         if (isset($_assign)) {
       
   100             return "<?php ob_start(); include{$_once} ('{$_filepath}'); \$_smarty_tpl->assign({$_assign},ob_get_contents()); ob_end_clean();?>";
       
   101         } else {
       
   102             return "<?php include{$_once} ('{$_filepath}');?>\n";
       
   103         }
       
   104     }
       
   105 }