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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / libraries / class-wc-eval-math.php
woocommerce / includes / libraries Last commit date
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