No Description

class-wc-eval-math.php 13KB

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