function.math.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * This plugin is only for Smarty2 BC
  6. * @package Smarty
  7. * @subpackage PluginsFunction
  8. */
  9. /**
  10. * Smarty {math} function plugin
  11. *
  12. * Type: function<br>
  13. * Name: math<br>
  14. * Purpose: handle math computations in template
  15. *
  16. * @link http://www.smarty.net/manual/en/language.function.math.php {math}
  17. * (Smarty online manual)
  18. * @author Monte Ohrt <monte at ohrt dot com>
  19. * @param array $params parameters
  20. * @param Smarty_Internal_Template $template template object
  21. * @return string|null
  22. */
  23. function smarty_function_math($params, $template)
  24. {
  25. static $_allowed_funcs = array(
  26. 'int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
  27. 'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true,
  28. 'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true ,'tan' => true
  29. );
  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. // match all vars in equation, make sure all are passed
  42. preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
  43. foreach($match[1] as $curr_var) {
  44. if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) {
  45. trigger_error("math: function call $curr_var not allowed",E_USER_WARNING);
  46. return;
  47. }
  48. }
  49. foreach($params as $key => $val) {
  50. if ($key != "equation" && $key != "format" && $key != "assign") {
  51. // make sure value is not empty
  52. if (strlen($val)==0) {
  53. trigger_error("math: parameter $key is empty",E_USER_WARNING);
  54. return;
  55. }
  56. if (!is_numeric($val)) {
  57. trigger_error("math: parameter $key: is not numeric",E_USER_WARNING);
  58. return;
  59. }
  60. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  61. }
  62. }
  63. $smarty_math_result = null;
  64. eval("\$smarty_math_result = ".$equation.";");
  65. if (empty($params['format'])) {
  66. if (empty($params['assign'])) {
  67. return $smarty_math_result;
  68. } else {
  69. $template->assign($params['assign'],$smarty_math_result);
  70. }
  71. } else {
  72. if (empty($params['assign'])){
  73. printf($params['format'],$smarty_math_result);
  74. } else {
  75. $template->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
  76. }
  77. }
  78. }
  79. ?>