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