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