smarty_internal_compile_private_modifier.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 object $compiler compiler object
  23. * @param array $parameter array with compilation parameter
  24. *
  25. * @return string compiled code
  26. */
  27. public function compile($args, $compiler, $parameter)
  28. {
  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. return $output;
  135. }
  136. }