function.math.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * This plugin is only for Smarty2 BC
  5. *
  6. * @package Smarty
  7. * @subpackage PluginsFunction
  8. */
  9. /**
  10. * Smarty {math} function plugin
  11. * Type: function
  12. * Name: math
  13. * Purpose: handle math computations in template
  14. *
  15. * @link http://www.smarty.net/manual/en/language.function.math.php {math}
  16. * (Smarty online manual)
  17. * @author Monte Ohrt <monte at ohrt dot com>
  18. *
  19. * @param array $params parameters
  20. * @param Smarty_Internal_Template $template template object
  21. *
  22. * @return string|null
  23. */
  24. function smarty_function_math($params, $template)
  25. {
  26. static $_allowed_funcs =
  27. array(
  28. 'int' => true,
  29. 'abs' => true,
  30. 'ceil' => true,
  31. 'cos' => true,
  32. 'exp' => true,
  33. 'floor' => true,
  34. 'log' => true,
  35. 'log10' => true,
  36. 'max' => true,
  37. 'min' => true,
  38. 'pi' => true,
  39. 'pow' => true,
  40. 'rand' => true,
  41. 'round' => true,
  42. 'sin' => true,
  43. 'sqrt' => true,
  44. 'srand' => true,
  45. 'tan' => true
  46. );
  47. // be sure equation parameter is present
  48. if (empty($params[ 'equation' ])) {
  49. trigger_error("math: missing equation parameter", E_USER_WARNING);
  50. return;
  51. }
  52. $equation = $params[ 'equation' ];
  53. // make sure parenthesis are balanced
  54. if (substr_count($equation, '(') !== substr_count($equation, ')')) {
  55. trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
  56. return;
  57. }
  58. // disallow backticks
  59. if (strpos($equation, '`') !== false) {
  60. trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
  61. return;
  62. }
  63. // also disallow dollar signs
  64. if (strpos($equation, '$') !== false) {
  65. trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
  66. return;
  67. }
  68. foreach ($params as $key => $val) {
  69. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  70. // make sure value is not empty
  71. if (strlen($val) === 0) {
  72. trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
  73. return;
  74. }
  75. if (!is_numeric($val)) {
  76. trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
  77. return;
  78. }
  79. }
  80. }
  81. // match all vars in equation, make sure all are passed
  82. preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
  83. foreach ($match[ 1 ] as $curr_var) {
  84. if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
  85. trigger_error(
  86. "math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'",
  87. E_USER_WARNING
  88. );
  89. return;
  90. }
  91. }
  92. foreach ($params as $key => $val) {
  93. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  94. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  95. }
  96. }
  97. $smarty_math_result = null;
  98. eval("\$smarty_math_result = " . $equation . ";");
  99. if (empty($params[ 'format' ])) {
  100. if (empty($params[ 'assign' ])) {
  101. return $smarty_math_result;
  102. } else {
  103. $template->assign($params[ 'assign' ], $smarty_math_result);
  104. }
  105. } else {
  106. if (empty($params[ 'assign' ])) {
  107. printf($params[ 'format' ], $smarty_math_result);
  108. } else {
  109. $template->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
  110. }
  111. }
  112. }