You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_private_modifier.php 7.6KB

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