library/smarty/libs/sysplugins/smarty_internal_compile_private_modifier.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 
       
     3 /**
       
     4  * Smarty Internal Plugin Compile Modifier
       
     5  * Compiles code for modifier execution
       
     6  *
       
     7  * @package    Smarty
       
     8  * @subpackage Compiler
       
     9  * @author     Uwe Tews
       
    10  */
       
    11 
       
    12 /**
       
    13  * Smarty Internal Plugin Compile Modifier Class
       
    14  *
       
    15  * @package    Smarty
       
    16  * @subpackage Compiler
       
    17  */
       
    18 class Smarty_Internal_Compile_Private_Modifier extends Smarty_Internal_CompileBase {
       
    19     /**
       
    20      * Compiles code for modifier execution
       
    21      *
       
    22      * @param  array $args array with attributes from parser
       
    23      * @param  object $compiler compiler object
       
    24      * @param  array $parameter array with compilation parameter
       
    25      *
       
    26      * @return string compiled code
       
    27      */
       
    28     public function compile($args, $compiler, $parameter) {
       
    29         // check and get attributes
       
    30         $_attr = $this->getAttributes($compiler, $args);
       
    31         $output = $parameter['value'];
       
    32         // loop over list of modifiers
       
    33         foreach ($parameter['modifierlist'] as $single_modifier) {
       
    34             $modifier = $single_modifier[0];
       
    35             $single_modifier[0] = $output;
       
    36             $params = implode(',', $single_modifier);
       
    37             // check if we know already the type of modifier
       
    38             if (isset($compiler->known_modifier_type[$modifier])) {
       
    39                 $modifier_types = array($compiler->known_modifier_type[$modifier]);
       
    40             } else {
       
    41                 $modifier_types = array(1, 2, 3, 4, 5, 6);
       
    42             }
       
    43             foreach ($modifier_types as $type) {
       
    44                 switch ($type) {
       
    45                     case 1:
       
    46                         // registered modifier
       
    47                         if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier])) {
       
    48                             $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier][0];
       
    49                             if (!is_array($function)) {
       
    50                                 $output = "{$function}({$params})";
       
    51                             } else {
       
    52                                 if (is_object($function[0])) {
       
    53                                     $output = '$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' . $modifier . '\'][0][0]->' . $function[1] . '(' . $params . ')';
       
    54                                 } else {
       
    55                                     $output = $function[0] . '::' . $function[1] . '(' . $params . ')';
       
    56                                 }
       
    57                             }
       
    58                             $compiler->known_modifier_type[$modifier] = $type;
       
    59                             break 2;
       
    60                         }
       
    61                         break;
       
    62                     case 2:
       
    63                         // registered modifier compiler
       
    64                         if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0])) {
       
    65                             $output = call_user_func($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0], $single_modifier, $compiler->smarty);
       
    66                             $compiler->known_modifier_type[$modifier] = $type;
       
    67                             break 2;
       
    68                         }
       
    69                         break;
       
    70                     case 3:
       
    71                         // modifiercompiler plugin
       
    72                         if ($compiler->smarty->loadPlugin('smarty_modifiercompiler_' . $modifier)) {
       
    73                             // check if modifier allowed
       
    74                             if (!is_object($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)) {
       
    75                                 $plugin = 'smarty_modifiercompiler_' . $modifier;
       
    76                                 $output = $plugin($single_modifier, $compiler);
       
    77                             }
       
    78                             $compiler->known_modifier_type[$modifier] = $type;
       
    79                             break 2;
       
    80                         }
       
    81                         break;
       
    82                     case 4:
       
    83                         // modifier plugin
       
    84                         if ($function = $compiler->getPlugin($modifier, Smarty::PLUGIN_MODIFIER)) {
       
    85                             // check if modifier allowed
       
    86                             if (!is_object($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)) {
       
    87                                 $output = "{$function}({$params})";
       
    88                             }
       
    89                             $compiler->known_modifier_type[$modifier] = $type;
       
    90                             break 2;
       
    91                         }
       
    92                         break;
       
    93                     case 5:
       
    94                         // PHP function
       
    95                         if (is_callable($modifier)) {
       
    96                             // check if modifier allowed
       
    97                             if (!is_object($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedPhpModifier($modifier, $compiler)) {
       
    98                                 $output = "{$modifier}({$params})";
       
    99                             }
       
   100                             $compiler->known_modifier_type[$modifier] = $type;
       
   101                             break 2;
       
   102                         }
       
   103                         break;
       
   104                     case 6:
       
   105                         // default plugin handler
       
   106                         if (isset($compiler->default_handler_plugins[Smarty::PLUGIN_MODIFIER][$modifier]) || (is_callable($compiler->smarty->default_plugin_handler_func) && $compiler->getPluginFromDefaultHandler($modifier, Smarty::PLUGIN_MODIFIER))) {
       
   107                             $function = $compiler->default_handler_plugins[Smarty::PLUGIN_MODIFIER][$modifier][0];
       
   108                             // check if modifier allowed
       
   109                             if (!is_object($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)) {
       
   110                                 if (!is_array($function)) {
       
   111                                     $output = "{$function}({$params})";
       
   112                                 } else {
       
   113                                     if (is_object($function[0])) {
       
   114                                         $output = '$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' . $modifier . '\'][0][0]->' . $function[1] . '(' . $params . ')';
       
   115                                     } else {
       
   116                                         $output = $function[0] . '::' . $function[1] . '(' . $params . ')';
       
   117                                     }
       
   118                                 }
       
   119                             }
       
   120                             if (isset($compiler->template->required_plugins['nocache'][$modifier][Smarty::PLUGIN_MODIFIER]['file']) || isset($compiler->template->required_plugins['compiled'][$modifier][Smarty::PLUGIN_MODIFIER]['file'])) {
       
   121                                 // was a plugin
       
   122                                 $compiler->known_modifier_type[$modifier] = 4;
       
   123                             } else {
       
   124                                 $compiler->known_modifier_type[$modifier] = $type;
       
   125                             }
       
   126                             break 2;
       
   127                         }
       
   128                 }
       
   129             }
       
   130             if (!isset($compiler->known_modifier_type[$modifier])) {
       
   131                 $compiler->trigger_template_error("unknown modifier \"" . $modifier . "\"", $compiler->lex->taglineno);
       
   132             }
       
   133         }
       
   134 
       
   135         return $output;
       
   136     }
       
   137 }