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