PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/libraries/smarty/plugins/function.math.php

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 83 lines | 51 code | 8 blank | 24 comment | 20 complexity | a875f349e9a8e87f5b6682147bf76e11 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  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<br>
  15. * @link http://smarty.php.net/manual/en/language.function.math.php {math}
  16. * (Smarty online manual)
  17. * @author Monte Ohrt <monte at ohrt dot com>
  18. * @param array $params parameters
  19. * @param object $template template object
  20. * @return string|null
  21. */
  22. function smarty_function_math($params, $template)
  23. {
  24. // be sure equation parameter is present
  25. if (empty($params['equation'])) {
  26. trigger_error("math: missing equation parameter",E_USER_WARNING);
  27. return;
  28. }
  29. $equation = $params['equation'];
  30. // make sure parenthesis are balanced
  31. if (substr_count($equation,"(") != substr_count($equation,")")) {
  32. trigger_error("math: unbalanced parenthesis",E_USER_WARNING);
  33. return;
  34. }
  35. // match all vars in equation, make sure all are passed
  36. preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
  37. $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
  38. 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
  39. foreach($match[1] as $curr_var) {
  40. if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
  41. trigger_error("math: function call $curr_var not allowed",E_USER_WARNING);
  42. return;
  43. }
  44. }
  45. foreach($params as $key => $val) {
  46. if ($key != "equation" && $key != "format" && $key != "assign") {
  47. // make sure value is not empty
  48. if (strlen($val)==0) {
  49. trigger_error("math: parameter $key is empty",E_USER_WARNING);
  50. return;
  51. }
  52. if (!is_numeric($val)) {
  53. trigger_error("math: parameter $key: is not numeric",E_USER_WARNING);
  54. return;
  55. }
  56. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  57. }
  58. }
  59. $smarty_math_result = null;
  60. eval("\$smarty_math_result = ".$equation.";");
  61. if (empty($params['format'])) {
  62. if (empty($params['assign'])) {
  63. return $smarty_math_result;
  64. } else {
  65. $template->assign($params['assign'],$smarty_math_result);
  66. }
  67. } else {
  68. if (empty($params['assign'])){
  69. printf($params['format'],$smarty_math_result);
  70. } else {
  71. $template->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
  72. }
  73. }
  74. }
  75. ?>