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

/modules/quicky/classes/plugins/function.math.php

https://bitbucket.org/seyar/parshin.local
PHP | 63 lines | 49 code | 9 blank | 5 comment | 20 complexity | 5ea234159602a6045ba3dc1d21b9338d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. function quicky_function_math($params,$quicky)
  3. {
  4. // be sure equation parameter is present
  5. if (empty($params['equation'])) {
  6. $quicky->warning("math: missing equation parameter");
  7. return;
  8. }
  9. $equation = $params['equation'];
  10. // make sure parenthesis are balanced
  11. if (substr_count($equation,"(") != substr_count($equation,")")) {
  12. $quicky->warning("math: unbalanced parenthesis");
  13. return;
  14. }
  15. // match all vars in equation, make sure all are passed
  16. preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!",$equation, $match);
  17. $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
  18. 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
  19. foreach($match[1] as $curr_var) {
  20. if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
  21. $quicky->warning("math: function call $curr_var not allowed");
  22. return;
  23. }
  24. }
  25. foreach($params as $key => $val) {
  26. if ($key != "equation" && $key != "format" && $key != "assign") {
  27. // make sure value is not empty
  28. if (strlen($val)==0) {
  29. $quicky->warning("math: parameter $key is empty");
  30. return;
  31. }
  32. if (!is_numeric($val)) {
  33. $quicky->warning("math: parameter $key: is not numeric");
  34. return;
  35. }
  36. $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
  37. }
  38. }
  39. eval("\$quicky_math_result = ".$equation.";");
  40. if (empty($params['format'])) {
  41. if (empty($params['assign'])) {
  42. return $quicky_math_result;
  43. } else {
  44. $quicky->assign($params['assign'],$quicky_math_result);
  45. }
  46. } else {
  47. if (empty($params['assign'])){
  48. printf($params['format'],$quicky_math_result);
  49. } else {
  50. $quicky->assign($params['assign'],sprintf($params['format'],$quicky_math_result));
  51. }
  52. }
  53. }
  54. /* vim: set expandtab: */