smarty_internal_compile_private_print_expression.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Print Expression
  4. *
  5. * Compiles any tag which will output an expression or variable
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Print Expression Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('assign');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $option_flags = array('nocache', 'nofilter');
  32. /**
  33. * Compiles code for gererting output from any expression
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param object $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. * @return string compiled code
  39. */
  40. public function compile($args, $compiler, $parameter)
  41. {
  42. // check and get attributes
  43. $_attr = $this->getAttributes($compiler, $args);
  44. // nocache option
  45. if ($_attr['nocache'] === true) {
  46. $compiler->tag_nocache = true;
  47. }
  48. // filter handling
  49. if ($_attr['nofilter'] === true) {
  50. $_filter = 'false';
  51. } else {
  52. $_filter = 'true';
  53. }
  54. if (isset($_attr['assign'])) {
  55. // assign output to variable
  56. $output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
  57. } else {
  58. // display value
  59. $output = $parameter['value'];
  60. // tag modifier
  61. if (!empty($parameter['modifierlist'])) {
  62. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $parameter['modifierlist'], 'value' => $output));
  63. }
  64. if (!$_attr['nofilter']) {
  65. // default modifier
  66. if (!empty($compiler->smarty->default_modifiers)) {
  67. if (empty($compiler->default_modifier_list)) {
  68. $modifierlist = array();
  69. foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
  70. preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/', $single_default_modifier, $mod_array);
  71. for ($i = 0, $count = count($mod_array[0]);$i < $count;$i++) {
  72. if ($mod_array[0][$i] != ':') {
  73. $modifierlist[$key][] = $mod_array[0][$i];
  74. }
  75. }
  76. }
  77. $compiler->default_modifier_list = $modifierlist;
  78. }
  79. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $compiler->default_modifier_list, 'value' => $output));
  80. }
  81. // autoescape html
  82. if ($compiler->template->smarty->escape_html) {
  83. $output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
  84. }
  85. // loop over registerd filters
  86. if (!empty($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE])) {
  87. foreach ($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE] as $key => $function) {
  88. if (!is_array($function)) {
  89. $output = "{$function}({$output},\$_smarty_tpl)";
  90. } else if (is_object($function[0])) {
  91. $output = "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE][{$key}][0]->{$function[1]}({$output},\$_smarty_tpl)";
  92. } else {
  93. $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
  94. }
  95. }
  96. }
  97. // auto loaded filters
  98. if (isset($compiler->smarty->autoload_filters[Smarty::FILTER_VARIABLE])) {
  99. foreach ((array)$compiler->template->smarty->autoload_filters[Smarty::FILTER_VARIABLE] as $name) {
  100. $result = $this->compile_output_filter($compiler, $name, $output);
  101. if ($result !== false) {
  102. $output = $result;
  103. } else {
  104. // not found, throw exception
  105. throw new SmartyException("Unable to load filter '{$name}'");
  106. }
  107. }
  108. }
  109. if (isset($compiler->template->variable_filters)) {
  110. foreach ($compiler->template->variable_filters as $filter) {
  111. if (count($filter) == 1 && ($result = $this->compile_output_filter($compiler, $filter[0], $output)) !== false) {
  112. $output = $result;
  113. } else {
  114. $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => array($filter), 'value' => $output));
  115. }
  116. }
  117. }
  118. }
  119. $compiler->has_output = true;
  120. $output = "<?php echo {$output};?>";
  121. }
  122. return $output;
  123. }
  124. /**
  125. * @param object $compiler compiler object
  126. * @param string $name name of variable filter
  127. * @param type $output embedded output
  128. * @return string
  129. */
  130. private function compile_output_filter($compiler, $name, $output)
  131. {
  132. $plugin_name = "smarty_variablefilter_{$name}";
  133. $path = $compiler->smarty->loadPlugin($plugin_name, false);
  134. if ($path) {
  135. if ($compiler->template->caching) {
  136. $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
  137. $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
  138. } else {
  139. $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
  140. $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
  141. }
  142. } else {
  143. // not found
  144. return false;
  145. }
  146. return "{$plugin_name}({$output},\$_smarty_tpl)";
  147. }
  148. }
  149. ?>