library/smarty/libs/sysplugins/smarty_internal_compile_private_special_variable.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 Special Smarty Variable
       
     4  * Compiles the special $smarty variables
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile special Smarty Variable Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_Private_Special_Variable extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Compiles code for the special $smarty variables
       
    20      *
       
    21      * @param  array $args array with attributes from parser
       
    22      * @param  object $compiler compiler object
       
    23      * @param         $parameter
       
    24      *
       
    25      * @return string compiled code
       
    26      */
       
    27     public function compile($args, $compiler, $parameter) {
       
    28         $_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
       
    29         $compiled_ref = ' ';
       
    30         $variable = trim($_index[0], "'");
       
    31         if (!isset($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)) {
       
    32             switch ($variable) {
       
    33                 case 'foreach':
       
    34                     $name = trim($_index[1], "'");
       
    35                     $foreachVar = "'__foreach_{$name}'";
       
    36                     return "(isset(\$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}]) ? \$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}] : null)";
       
    37                 case 'section':
       
    38                     return "\$_smarty_tpl->getVariable('smarty')->value$parameter";
       
    39                 case 'capture':
       
    40                     return "Smarty::\$_smarty_vars$parameter";
       
    41                 case 'now':
       
    42                     return 'time()';
       
    43                 case 'cookies':
       
    44                     if (isset($compiler->smarty->security_policy) && !$compiler->smarty->security_policy->allow_super_globals) {
       
    45                         $compiler->trigger_template_error("(secure mode) super globals not permitted");
       
    46                         break;
       
    47                     }
       
    48                     $compiled_ref = '$_COOKIE';
       
    49                     break;
       
    50 
       
    51                 case 'get':
       
    52                 case 'post':
       
    53                 case 'env':
       
    54                 case 'server':
       
    55                 case 'session':
       
    56                 case 'request':
       
    57                     if (isset($compiler->smarty->security_policy) && !$compiler->smarty->security_policy->allow_super_globals) {
       
    58                         $compiler->trigger_template_error("(secure mode) super globals not permitted");
       
    59                         break;
       
    60                     }
       
    61                     $compiled_ref = '$_' . strtoupper($variable);
       
    62                     break;
       
    63 
       
    64                 case 'template':
       
    65                     return 'basename($_smarty_tpl->source->filepath)';
       
    66 
       
    67                 case 'template_object':
       
    68                     return '$_smarty_tpl';
       
    69 
       
    70                 case 'current_dir':
       
    71                     return 'dirname($_smarty_tpl->source->filepath)';
       
    72 
       
    73                 case 'version':
       
    74                     $_version = Smarty::SMARTY_VERSION;
       
    75 
       
    76                     return "'$_version'";
       
    77 
       
    78                 case 'const':
       
    79                     if (isset($compiler->smarty->security_policy) && !$compiler->smarty->security_policy->allow_constants) {
       
    80                         $compiler->trigger_template_error("(secure mode) constants not permitted");
       
    81                         break;
       
    82                     }
       
    83                     if (strpos($_index[1], '$') === false && strpos($_index[1], '\'') === false) {
       
    84                         return "@constant('{$_index[1]}')";
       
    85                     } else {
       
    86                         return "@constant({$_index[1]})";
       
    87                     }
       
    88 
       
    89                 case 'config':
       
    90                     if (isset($_index[2])) {
       
    91                         return "(is_array(\$tmp = \$_smarty_tpl->getConfigVariable($_index[1])) ? \$tmp[$_index[2]] : null)";
       
    92                     } else {
       
    93                         return "\$_smarty_tpl->getConfigVariable($_index[1])";
       
    94                     }
       
    95                 case 'ldelim':
       
    96                     $_ldelim = $compiler->smarty->left_delimiter;
       
    97 
       
    98                     return "'$_ldelim'";
       
    99 
       
   100                 case 'rdelim':
       
   101                     $_rdelim = $compiler->smarty->right_delimiter;
       
   102 
       
   103                     return "'$_rdelim'";
       
   104 
       
   105                 default:
       
   106                     $compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is invalid');
       
   107                     break;
       
   108             }
       
   109             if (isset($_index[1])) {
       
   110                 array_shift($_index);
       
   111                 foreach ($_index as $_ind) {
       
   112                     $compiled_ref = $compiled_ref . "[$_ind]";
       
   113                 }
       
   114             }
       
   115         }
       
   116         return $compiled_ref;
       
   117     }
       
   118 }