/sites/all/modules/contrib/civicrm/packages/Smarty/plugins/function.math.php

https://gitlab.com/virtualrealms/d7civicrm · PHP · 108 lines · 63 code · 17 blank · 28 comment · 29 complexity · 503bf196c22171fa05b6051ee3e52864 MD5 · raw file

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