smarty_internal_compile_private_modifier.php 7.3 KB

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