/external/smarty-4.0.4/libs/plugins/function.math.php

https://github.com/exponentcms/exponent-cms · PHP · 142 lines · 104 code · 8 blank · 30 comment · 22 complexity · 576d583291cdd6a642525ae2f616f6ef 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
  12. * Name: math
  13. * Purpose: handle math computations in template
  14. *
  15. * @link https://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. 'acos' => true,
  32. 'acosh' => true,
  33. 'cos' => true,
  34. 'cosh' => true,
  35. 'deg2rad' => true,
  36. 'rad2deg' => true,
  37. 'exp' => true,
  38. 'floor' => true,
  39. 'log' => true,
  40. 'log10' => true,
  41. 'max' => true,
  42. 'min' => true,
  43. 'pi' => true,
  44. 'pow' => true,
  45. 'rand' => true,
  46. 'round' => true,
  47. 'asin' => true,
  48. 'asinh' => true,
  49. 'sin' => true,
  50. 'sinh' => true,
  51. 'sqrt' => true,
  52. 'srand' => true,
  53. 'atan' => true,
  54. 'atanh' => true,
  55. 'tan' => true,
  56. 'tanh' => true
  57. );
  58. // be sure equation parameter is present
  59. if (empty($params[ 'equation' ])) {
  60. trigger_error("math: missing equation parameter", E_USER_WARNING);
  61. return;
  62. }
  63. $equation = $params[ 'equation' ];
  64. // Remove whitespaces
  65. $equation = preg_replace('/\s+/', '', $equation);
  66. // Adapted from https://www.php.net/manual/en/function.eval.php#107377
  67. $number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
  68. $functionsOrVars = '((?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))';
  69. $operators = '[+\/*\^%-]'; // Allowed math operators
  70. $regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)+\)|\((?1)+\)))(?:'.$operators.'(?1))?)+$/';
  71. if (!preg_match($regexp, $equation)) {
  72. trigger_error("math: illegal characters", E_USER_WARNING);
  73. return;
  74. }
  75. // make sure parenthesis are balanced
  76. if (substr_count($equation, '(') !== substr_count($equation, ')')) {
  77. trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
  78. return;
  79. }
  80. // disallow backticks
  81. if (strpos($equation, '`') !== false) {
  82. trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
  83. return;
  84. }
  85. // also disallow dollar signs
  86. if (strpos($equation, '$') !== false) {
  87. trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
  88. return;
  89. }
  90. foreach ($params as $key => $val) {
  91. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  92. // make sure value is not empty
  93. if (strlen($val) === 0) {
  94. trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
  95. return;
  96. }
  97. if (!is_numeric($val)) {
  98. trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
  99. return;
  100. }
  101. }
  102. }
  103. // match all vars in equation, make sure all are passed
  104. preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
  105. foreach ($match[ 1 ] as $curr_var) {
  106. if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
  107. trigger_error(
  108. "math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'",
  109. E_USER_WARNING
  110. );
  111. return;
  112. }
  113. }
  114. foreach ($params as $key => $val) {
  115. if ($key !== 'equation' && $key !== 'format' && $key !== 'assign') {
  116. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  117. }
  118. }
  119. $smarty_math_result = null;
  120. eval("\$smarty_math_result = " . $equation . ";");
  121. if (empty($params[ 'format' ])) {
  122. if (empty($params[ 'assign' ])) {
  123. return $smarty_math_result;
  124. } else {
  125. $template->assign($params[ 'assign' ], $smarty_math_result);
  126. }
  127. } else {
  128. if (empty($params[ 'assign' ])) {
  129. printf($params[ 'format' ], $smarty_math_result);
  130. } else {
  131. $template->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
  132. }
  133. }
  134. }