You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.math.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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('int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
  28. 'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true, 'rand' => true,
  29. 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true);
  30. // be sure equation parameter is present
  31. if (empty($params[ 'equation' ])) {
  32. trigger_error("math: missing equation parameter", E_USER_WARNING);
  33. return;
  34. }
  35. $equation = $params[ 'equation' ];
  36. // make sure parenthesis are balanced
  37. if (substr_count($equation, '(') !== substr_count($equation, ')')) {
  38. trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
  39. return;
  40. }
  41. // disallow backticks
  42. if (strpos($equation, '`') !== false) {
  43. trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
  44. return;
  45. }
  46. // also disallow dollar signs
  47. if (strpos($equation, '$') !== false) {
  48. trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
  49. return;
  50. }
  51. foreach ($params as $key => $val) {
  52. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  53. // make sure value is not empty
  54. if (strlen($val) === 0) {
  55. trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
  56. return;
  57. }
  58. if (!is_numeric($val)) {
  59. trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
  60. return;
  61. }
  62. }
  63. }
  64. // match all vars in equation, make sure all are passed
  65. preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
  66. foreach ($match[ 1 ] as $curr_var) {
  67. if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
  68. trigger_error("math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'", E_USER_WARNING);
  69. return;
  70. }
  71. }
  72. foreach ($params as $key => $val) {
  73. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  74. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  75. }
  76. }
  77. $smarty_math_result = null;
  78. eval("\$smarty_math_result = " . $equation . ";");
  79. if (empty($params[ 'format' ])) {
  80. if (empty($params[ 'assign' ])) {
  81. return $smarty_math_result;
  82. } else {
  83. $template->assign($params[ 'assign' ], $smarty_math_result);
  84. }
  85. } else {
  86. if (empty($params[ 'assign' ])) {
  87. printf($params[ 'format' ], $smarty_math_result);
  88. } else {
  89. $template->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
  90. }
  91. }
  92. }