smarty_internal_compile_private_modifier.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. */
  28. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  29. {
  30. // check and get attributes
  31. $_attr = $this->getAttributes($compiler, $args);
  32. $output = $parameter[ 'value' ];
  33. // loop over list of modifiers
  34. foreach ($parameter[ 'modifierlist' ] as $single_modifier) {
  35. $modifier = $single_modifier[ 0 ];
  36. $single_modifier[ 0 ] = $output;
  37. $params = implode(',', $single_modifier);
  38. // check if we know already the type of modifier
  39. if (isset($compiler->known_modifier_type[ $modifier ])) {
  40. $modifier_types = array($compiler->known_modifier_type[ $modifier ]);
  41. } else {
  42. $modifier_types = array(1, 2, 3, 4, 5, 6);
  43. }
  44. foreach ($modifier_types as $type) {
  45. switch ($type) {
  46. case 1:
  47. // registered modifier
  48. if (isset($compiler->smarty->registered_plugins[ Smarty::PLUGIN_MODIFIER ][ $modifier ])) {
  49. $function =
  50. $compiler->smarty->registered_plugins[ Smarty::PLUGIN_MODIFIER ][ $modifier ][ 0 ];
  51. if (!is_array($function)) {
  52. $output = "{$function}({$params})";
  53. } else {
  54. if (is_object($function[ 0 ])) {
  55. $output = '$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' .
  56. $modifier . '\'][0][0]->' . $function[ 1 ] . '(' . $params . ')';
  57. } else {
  58. $output = $function[ 0 ] . '::' . $function[ 1 ] . '(' . $params . ')';
  59. }
  60. }
  61. $compiler->known_modifier_type[ $modifier ] = $type;
  62. break 2;
  63. }
  64. break;
  65. case 2:
  66. // registered modifier compiler
  67. if (isset($compiler->smarty->registered_plugins[ Smarty::PLUGIN_MODIFIERCOMPILER ][ $modifier ][ 0 ])) {
  68. $output =
  69. call_user_func($compiler->smarty->registered_plugins[ Smarty::PLUGIN_MODIFIERCOMPILER ][ $modifier ][ 0 ],
  70. $single_modifier, $compiler->smarty);
  71. $compiler->known_modifier_type[ $modifier ] = $type;
  72. break 2;
  73. }
  74. break;
  75. case 3:
  76. // modifiercompiler plugin
  77. if ($compiler->smarty->loadPlugin('smarty_modifiercompiler_' . $modifier)) {
  78. // check if modifier allowed
  79. if (!is_object($compiler->smarty->security_policy) ||
  80. $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)
  81. ) {
  82. $plugin = 'smarty_modifiercompiler_' . $modifier;
  83. $output = $plugin($single_modifier, $compiler);
  84. }
  85. $compiler->known_modifier_type[ $modifier ] = $type;
  86. break 2;
  87. }
  88. break;
  89. case 4:
  90. // modifier plugin
  91. if ($function = $compiler->getPlugin($modifier, Smarty::PLUGIN_MODIFIER)) {
  92. // check if modifier allowed
  93. if (!is_object($compiler->smarty->security_policy) ||
  94. $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)
  95. ) {
  96. $output = "{$function}({$params})";
  97. }
  98. $compiler->known_modifier_type[ $modifier ] = $type;
  99. break 2;
  100. }
  101. break;
  102. case 5:
  103. // PHP function
  104. if (is_callable($modifier)) {
  105. // check if modifier allowed
  106. if (!is_object($compiler->smarty->security_policy) ||
  107. $compiler->smarty->security_policy->isTrustedPhpModifier($modifier, $compiler)
  108. ) {
  109. $output = "{$modifier}({$params})";
  110. }
  111. $compiler->known_modifier_type[ $modifier ] = $type;
  112. break 2;
  113. }
  114. break;
  115. case 6:
  116. // default plugin handler
  117. if (isset($compiler->default_handler_plugins[ Smarty::PLUGIN_MODIFIER ][ $modifier ]) ||
  118. (is_callable($compiler->smarty->default_plugin_handler_func) &&
  119. $compiler->getPluginFromDefaultHandler($modifier, Smarty::PLUGIN_MODIFIER))
  120. ) {
  121. $function = $compiler->default_handler_plugins[ Smarty::PLUGIN_MODIFIER ][ $modifier ][ 0 ];
  122. // check if modifier allowed
  123. if (!is_object($compiler->smarty->security_policy) ||
  124. $compiler->smarty->security_policy->isTrustedModifier($modifier, $compiler)
  125. ) {
  126. if (!is_array($function)) {
  127. $output = "{$function}({$params})";
  128. } else {
  129. if (is_object($function[ 0 ])) {
  130. $output = $function[ 0 ] . '->'. $function[ 1 ] . '(' . $params . ')';
  131. } else {
  132. $output = $function[ 0 ] . '::' . $function[ 1 ] . '(' . $params . ')';
  133. }
  134. }
  135. }
  136. if (isset($compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $modifier ][ Smarty::PLUGIN_MODIFIER ][ 'file' ]) ||
  137. isset($compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $modifier ][ Smarty::PLUGIN_MODIFIER ][ 'file' ])
  138. ) {
  139. // was a plugin
  140. $compiler->known_modifier_type[ $modifier ] = 4;
  141. } else {
  142. $compiler->known_modifier_type[ $modifier ] = $type;
  143. }
  144. break 2;
  145. }
  146. }
  147. }
  148. if (!isset($compiler->known_modifier_type[ $modifier ])) {
  149. $compiler->trigger_template_error("unknown modifier \"" . $modifier . "\"", null, true);
  150. }
  151. }
  152. return $output;
  153. }
  154. }