Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

function.math.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_Internal_Template $template template object
  21. *
  22. * @return string|null
  23. */
  24. function smarty_function_math($params, $template)
  25. {
  26. static $_allowed_funcs = array(
  27. '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,
  29. 'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true
  30. );
  31. // be sure equation parameter is present
  32. if (empty($params['equation'])) {
  33. trigger_error("math: missing equation parameter", E_USER_WARNING);
  34. return;
  35. }
  36. $equation = $params['equation'];
  37. // make sure parenthesis are balanced
  38. if (substr_count($equation, "(") != substr_count($equation, ")")) {
  39. trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
  40. return;
  41. }
  42. // match all vars in equation, make sure all are passed
  43. preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!", $equation, $match);
  44. foreach ($match[1] as $curr_var) {
  45. if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) {
  46. trigger_error("math: function call $curr_var not allowed", E_USER_WARNING);
  47. return;
  48. }
  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. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  62. }
  63. }
  64. $smarty_math_result = null;
  65. eval("\$smarty_math_result = " . $equation . ";");
  66. if (empty($params['format'])) {
  67. if (empty($params['assign'])) {
  68. return $smarty_math_result;
  69. } else {
  70. $template->assign($params['assign'], $smarty_math_result);
  71. }
  72. } else {
  73. if (empty($params['assign'])) {
  74. printf($params['format'], $smarty_math_result);
  75. } else {
  76. $template->assign($params['assign'], sprintf($params['format'], $smarty_math_result));
  77. }
  78. }
  79. }