PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/seyar/parshin.local
PHP | 83 lines | 67 code | 9 blank | 7 comment | 23 complexity | 8b330d60bfb637500064f3793b953ed9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. function quicky_compiler_math($params, $compiler)
  3. {
  4. $params = $compiler->_parse_params($params);
  5. $outoff = FALSE;
  6. // be sure equation parameter is present
  7. if (empty($params['equation'])) {
  8. $compiler->parent->warning("math: missing equation parameter");
  9. return;
  10. }
  11. $equation = $compiler->_dequote($params['equation']);
  12. if (strpos($equation,'$') !== FALSE)
  13. {
  14. $p = 'array(';
  15. $i = 0;
  16. $f = $compiler->parent->fetch_plugin('function.math');
  17. if (!in_array($f,$compiler->load_plugins)) {$compiler->load_plugins[] = $f;}
  18. foreach ($params as $k => $v) {$p .= ($i++ > 0?',':'').'\''.$k.'\' => '.$v;}
  19. $p .= ')';
  20. $r = '<?php '.($outoff?'':($compiler->_write_out_to !== ''?$compiler->_write_out_to.' .=':'echo').' ').
  21. 'quicky_function_math('.$p.',$this,TRUE); ?>';
  22. //var_dump($p);
  23. //exit;
  24. return $r;
  25. }
  26. // make sure parenthesis are balanced
  27. if (substr_count($equation,"(") != substr_count($equation,")")) {
  28. $this->parent->warning("math: unbalanced parenthesis");
  29. return;
  30. }
  31. // match all vars in equation, make sure all are passed
  32. preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!",$equation, $match);
  33. $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
  34. 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
  35. foreach($match[1] as $curr_var) {
  36. if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
  37. $this->parent->warning("math: function call $curr_var not allowed");
  38. return;
  39. }
  40. }
  41. foreach($params as $key => $val) {
  42. if ($key != "equation" && $key != "format" && $key != "assign") {
  43. // make sure value is not empty
  44. if (strlen($val)==0) {
  45. $this->parent->warning("math: parameter $key is empty");
  46. return;
  47. }
  48. $equation = preg_replace("/\b$key\b/", $params[$key], $equation);
  49. }
  50. }
  51. if (isset($params['assign']) && ($params['assign'] !== ''))
  52. {
  53. $part = $compiler->_varname($params['assign']).' =';
  54. }
  55. else
  56. {
  57. $part = ($compiler->_write_out_to !== ''?$compiler->_write_out_to.' .=':'echo');
  58. }
  59. return '<?php '.($outoff?'':$part.' ').$equation.'; ?>';
  60. if (empty($params['format'])) {
  61. if (empty($params['assign'])) {
  62. return $this->parent_math_result;
  63. } else {
  64. $this->parent->assign($params['assign'],$this->parent_math_result);
  65. }
  66. } else {
  67. if (empty($params['assign'])){
  68. printf($params['format'],$this->parent_math_result);
  69. } else {
  70. $this->parent->assign($params['assign'],sprintf($params['format'],$this->parent_math_result));
  71. }
  72. }
  73. }
  74. /* vim: set expandtab: */