PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/shipping/flat-rate/includes/class-wc-eval-math.php

https://gitlab.com/webkod3r/tripolis
PHP | 367 lines | 227 code | 25 blank | 115 comment | 71 complexity | 22dc67f284d570945b536b8df0b8d14d MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class WC_Eval_Math. Supports basic math only (removed eval function).
  7. *
  8. * Based on EvalMath by Miles Kaufman Copyright (C) 2005 Miles Kaufmann http://www.twmagic.com/.
  9. */
  10. class WC_Eval_Math {
  11. /**
  12. * Last error.
  13. *
  14. * @var string
  15. */
  16. public static $last_error = null;
  17. /**
  18. * Variables (and constants).
  19. *
  20. * @var array
  21. */
  22. public static $v = array( 'e' => 2.71, 'pi' => 3.14 );
  23. /**
  24. * User-defined functions.
  25. *
  26. * @var array
  27. */
  28. public static $f = array();
  29. /**
  30. * Constants.
  31. *
  32. * @var array
  33. */
  34. public static $vb = array( 'e', 'pi' );
  35. /**
  36. * Built-in functions.
  37. *
  38. * @var array
  39. */
  40. public static $fb = array();
  41. /**
  42. * Evaluate maths string.
  43. *
  44. * @param string $expr
  45. * @return mixed
  46. */
  47. public static function evaluate( $expr ) {
  48. self::$last_error = null;
  49. $expr = trim( $expr );
  50. if ( substr( $expr, -1, 1 ) == ';' ) $expr = substr( $expr, 0, strlen( $expr )-1 ); // strip semicolons at the end
  51. //===============
  52. // is it a variable assignment?
  53. if ( preg_match( '/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches ) ) {
  54. if ( in_array( $matches[1], self::$vb ) ) { // make sure we're not assigning to a constant
  55. return self::trigger( "cannot assign to constant '$matches[1]'" );
  56. }
  57. if ( ( $tmp = self::pfx( self::nfx( $matches[2] ) ) ) === false ) return false; // get the result and make sure it's good
  58. self::$v[$matches[1]] = $tmp; // if so, stick it in the variable array
  59. return self::$v[$matches[1]]; // and return the resulting value
  60. //===============
  61. // is it a function assignment?
  62. } elseif ( preg_match( '/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches ) ) {
  63. $fnn = $matches[1]; // get the function name
  64. if ( in_array( $matches[1], self::$fb ) ) { // make sure it isn't built in
  65. return self::trigger( "cannot redefine built-in function '$matches[1]()'" );
  66. }
  67. $args = explode( ",", preg_replace( "/\s+/", "", $matches[2] ) ); // get the arguments
  68. if ( ( $stack = self::nfx( $matches[3] ) ) === false ) return false; // see if it can be converted to postfix
  69. for ( $i = 0; $i<count( $stack ); $i++ ) { // freeze the state of the non-argument variables
  70. $token = $stack[$i];
  71. if ( preg_match( '/^[a-z]\w*$/', $token ) and !in_array( $token, $args ) ) {
  72. if ( array_key_exists( $token, self::$v ) ) {
  73. $stack[$i] = self::$v[$token];
  74. } else {
  75. return self::trigger( "undefined variable '$token' in function definition" );
  76. }
  77. }
  78. }
  79. self::$f[$fnn] = array( 'args'=>$args, 'func'=>$stack );
  80. return true;
  81. //===============
  82. } else {
  83. return self::pfx( self::nfx( $expr ) ); // straight up evaluation, woo
  84. }
  85. }
  86. /**
  87. * Convert infix to postfix notation.
  88. *
  89. * @param string $expr
  90. * @return string
  91. */
  92. private static function nfx( $expr ) {
  93. $index = 0;
  94. $stack = new WC_Eval_Math_Stack;
  95. $output = array(); // postfix form of expression, to be passed to pfx()
  96. // $expr = trim(strtolower($expr));
  97. $expr = trim( $expr );
  98. $ops = array( '+', '-', '*', '/', '^', '_' );
  99. $ops_r = array( '+'=>0, '-'=>0, '*'=>0, '/'=>0, '^'=>1 ); // right-associative operator?
  100. $ops_p = array( '+'=>0, '-'=>0, '*'=>1, '/'=>1, '_'=>1, '^'=>2 ); // operator precedence
  101. $expecting_op = false; // we use this in syntax-checking the expression
  102. // and determining when a - is a negation
  103. if ( preg_match( "/[^\w\s+*^\/()\.,-]/", $expr, $matches ) ) { // make sure the characters are all good
  104. return self::trigger( "illegal character '{$matches[0]}'" );
  105. }
  106. while ( 1 ) { // 1 Infinite Loop ;)
  107. $op = substr( $expr, $index, 1 ); // get the first character at the current index
  108. // find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
  109. $ex = preg_match( '/^([A-Za-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr( $expr, $index ), $match );
  110. //===============
  111. if ( $op == '-' and !$expecting_op ) { // is it a negation instead of a minus?
  112. $stack->push( '_' ); // put a negation on the stack
  113. $index++;
  114. } elseif ( $op == '_' ) { // we have to explicitly deny this, because it's legal on the stack
  115. return self::trigger( "illegal character '_'" ); // but not in the input expression
  116. //===============
  117. } elseif ( ( in_array( $op, $ops ) or $ex ) and $expecting_op ) { // are we putting an operator on the stack?
  118. if ( $ex ) { // are we expecting an operator but have a number/variable/function/opening parethesis?
  119. $op = '*'; $index--; // it's an implicit multiplication
  120. }
  121. // heart of the algorithm:
  122. while ( $stack->count > 0 and ( $o2 = $stack->last() ) and in_array( $o2, $ops ) and ( $ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2] ) ) {
  123. $output[] = $stack->pop(); // pop stuff off the stack into the output
  124. }
  125. // many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
  126. $stack->push( $op ); // finally put OUR operator onto the stack
  127. $index++;
  128. $expecting_op = false;
  129. //===============
  130. } elseif ( $op == ')' and $expecting_op ) { // ready to close a parenthesis?
  131. while ( ( $o2 = $stack->pop() ) != '(' ) { // pop off the stack back to the last (
  132. if ( is_null( $o2 ) ) return self::trigger( "unexpected ')'" );
  133. else $output[] = $o2;
  134. }
  135. if ( preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) ) { // did we just close a function?
  136. $fnn = $matches[1]; // get the function name
  137. $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
  138. $output[] = $stack->pop(); // pop the function and push onto the output
  139. if ( in_array( $fnn, self::$fb ) ) { // check the argument count
  140. if ( $arg_count > 1 )
  141. return self::trigger( "too many arguments ($arg_count given, 1 expected)" );
  142. } elseif ( array_key_exists( $fnn, self::$f ) ) {
  143. if ( $arg_count != count( self::$f[$fnn]['args'] ) )
  144. return self::trigger( "wrong number of arguments ($arg_count given, " . count( self::$f[$fnn]['args'] ) . " expected)" );
  145. } else { // did we somehow push a non-function on the stack? this should never happen
  146. return self::trigger( "internal error" );
  147. }
  148. }
  149. $index++;
  150. //===============
  151. } elseif ( $op == ',' and $expecting_op ) { // did we just finish a function argument?
  152. while ( ( $o2 = $stack->pop() ) != '(' ) {
  153. if ( is_null( $o2 ) ) return self::trigger( "unexpected ','" ); // oops, never had a (
  154. else $output[] = $o2; // pop the argument expression stuff and push onto the output
  155. }
  156. // make sure there was a function
  157. if ( !preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) )
  158. return self::trigger( "unexpected ','" );
  159. $stack->push( $stack->pop()+1 ); // increment the argument count
  160. $stack->push( '(' ); // put the ( back on, we'll need to pop back to it again
  161. $index++;
  162. $expecting_op = false;
  163. //===============
  164. } elseif ( $op == '(' and !$expecting_op ) {
  165. $stack->push( '(' ); // that was easy
  166. $index++;
  167. //===============
  168. } elseif ( $ex and !$expecting_op ) { // do we now have a function/variable/number?
  169. $expecting_op = true;
  170. $val = $match[1];
  171. if ( preg_match( "/^([A-Za-z]\w*)\($/", $val, $matches ) ) { // may be func, or variable w/ implicit multiplication against parentheses...
  172. if ( in_array( $matches[1], self::$fb ) or array_key_exists( $matches[1], self::$f ) ) { // it's a func
  173. $stack->push( $val );
  174. $stack->push( 1 );
  175. $stack->push( '(' );
  176. $expecting_op = false;
  177. } else { // it's a var w/ implicit multiplication
  178. $val = $matches[1];
  179. $output[] = $val;
  180. }
  181. } else { // it's a plain old var or num
  182. $output[] = $val;
  183. }
  184. $index += strlen( $val );
  185. //===============
  186. } elseif ( $op == ')' ) { // miscellaneous error checking
  187. return self::trigger( "unexpected ')'" );
  188. } elseif ( in_array( $op, $ops ) and !$expecting_op ) {
  189. return self::trigger( "unexpected operator '$op'" );
  190. } else { // I don't even want to know what you did to get here
  191. return self::trigger( "an unexpected error occured" );
  192. }
  193. if ( $index == strlen( $expr ) ) {
  194. if ( in_array( $op, $ops ) ) { // did we end with an operator? bad.
  195. return self::trigger( "operator '$op' lacks operand" );
  196. } else {
  197. break;
  198. }
  199. }
  200. while ( substr( $expr, $index, 1 ) == ' ' ) { // step the index past whitespace (pretty much turns whitespace
  201. $index++; // into implicit multiplication if no operator is there)
  202. }
  203. }
  204. while ( !is_null( $op = $stack->pop() ) ) { // pop everything off the stack and push onto output
  205. if ( $op == '(' ) return self::trigger( "expecting ')'" ); // if there are (s on the stack, ()s were unbalanced
  206. $output[] = $op;
  207. }
  208. return $output;
  209. }
  210. /**
  211. * Evaluate postfix notation.
  212. *
  213. * @param mixed $tokens
  214. * @param array $vars
  215. *
  216. * @return mixed
  217. */
  218. private static function pfx( $tokens, $vars = array() ) {
  219. if ( $tokens == false ) return false;
  220. $stack = new WC_Eval_Math_Stack;
  221. foreach ( $tokens as $token ) { // nice and easy
  222. // if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
  223. if ( in_array( $token, array( '+', '-', '*', '/', '^' ) ) ) {
  224. if ( is_null( $op2 = $stack->pop() ) ) return self::trigger( "internal error" );
  225. if ( is_null( $op1 = $stack->pop() ) ) return self::trigger( "internal error" );
  226. switch ( $token ) {
  227. case '+':
  228. $stack->push( $op1+$op2 ); break;
  229. case '-':
  230. $stack->push( $op1-$op2 ); break;
  231. case '*':
  232. $stack->push( $op1*$op2 ); break;
  233. case '/':
  234. if ( $op2 == 0 ) return self::trigger( "division by zero" );
  235. $stack->push( $op1/$op2 ); break;
  236. case '^':
  237. $stack->push( pow( $op1, $op2 ) ); break;
  238. }
  239. // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
  240. } elseif ( $token == "_" ) {
  241. $stack->push( -1*$stack->pop() );
  242. // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
  243. } elseif ( ! preg_match( "/^([a-z]\w*)\($/", $token, $matches ) ) {
  244. if ( is_numeric( $token ) ) {
  245. $stack->push( $token );
  246. } elseif ( array_key_exists( $token, self::$v ) ) {
  247. $stack->push( self::$v[$token] );
  248. } elseif ( array_key_exists( $token, $vars ) ) {
  249. $stack->push( $vars[$token] );
  250. } else {
  251. return self::trigger( "undefined variable '$token'" );
  252. }
  253. }
  254. }
  255. // when we're out of tokens, the stack should have a single element, the final result
  256. if ( $stack->count != 1 ) return self::trigger( "internal error" );
  257. return $stack->pop();
  258. }
  259. /**
  260. * Trigger an error, but nicely, if need be.
  261. *
  262. * @param string $msg
  263. *
  264. * @return bool
  265. */
  266. private static function trigger( $msg ) {
  267. self::$last_error = $msg;
  268. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  269. echo "\nError found in:";
  270. self::debugPrintCallingFunction();
  271. trigger_error( $msg, E_USER_WARNING );
  272. }
  273. return false;
  274. }
  275. /**
  276. * Prints the file name, function name, and
  277. * line number which called your function
  278. * (not this function, then one that called
  279. * it to begin with)
  280. */
  281. private static function debugPrintCallingFunction() {
  282. $file = 'n/a';
  283. $func = 'n/a';
  284. $line = 'n/a';
  285. $debugTrace = debug_backtrace();
  286. if ( isset( $debugTrace[1] ) ) {
  287. $file = $debugTrace[1]['file'] ? $debugTrace[1]['file'] : 'n/a';
  288. $line = $debugTrace[1]['line'] ? $debugTrace[1]['line'] : 'n/a';
  289. }
  290. if ( isset( $debugTrace[2] ) ) $func = $debugTrace[2]['function'] ? $debugTrace[2]['function'] : 'n/a';
  291. echo "\n$file, $func, $line\n";
  292. }
  293. }
  294. /**
  295. * Class WC_Eval_Math_Stack.
  296. */
  297. class WC_Eval_Math_Stack {
  298. /**
  299. * Stack array.
  300. *
  301. * @var array
  302. */
  303. public $stack = array();
  304. /**
  305. * Stack counter.
  306. *
  307. * @var integer
  308. */
  309. public $count = 0;
  310. /**
  311. * Push value into stack.
  312. *
  313. * @param mixed $val
  314. */
  315. public function push( $val ) {
  316. $this->stack[ $this->count ] = $val;
  317. $this->count++;
  318. }
  319. /**
  320. * Pop value from stack.
  321. *
  322. * @return mixed
  323. */
  324. public function pop() {
  325. if ( $this->count > 0 ) {
  326. $this->count--;
  327. return $this->stack[ $this->count ];
  328. }
  329. return null;
  330. }
  331. /**
  332. * Get last value from stack.
  333. *
  334. * @param int $n
  335. *
  336. * @return mixed
  337. */
  338. public function last( $n = 1 ) {
  339. $key = $this->count - $n;
  340. return array_key_exists( $key, $this->stack ) ? $this->stack[ $key ] : null;
  341. }
  342. }