2008-08-31 05:56:49 +00:00
|
|
|
<?php
|
2012-09-02 11:07:02 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
/**
|
2015-09-28 18:03:35 +00:00
|
|
|
* Abuse filter parser.
|
|
|
|
* Copyright © Victor Vasiliev, 2008.
|
|
|
|
* Based on ideas by Andrew Garrett
|
|
|
|
* Distributed under GNU GPL v2 terms.
|
|
|
|
*
|
|
|
|
* Types of token:
|
|
|
|
* * T_NONE - special-purpose token
|
|
|
|
* * T_BRACE - ( or )
|
|
|
|
* * T_COMMA - ,
|
|
|
|
* * T_OP - operator like + or ^
|
|
|
|
* * T_NUMBER - number
|
|
|
|
* * T_STRING - string, in "" or ''
|
|
|
|
* * T_KEYWORD - keyword
|
|
|
|
* * T_ID - identifier
|
|
|
|
* * T_STATEMENT_SEPARATOR - ;
|
|
|
|
* * T_SQUARE_BRACKETS - [ or ]
|
|
|
|
*
|
|
|
|
* Levels of parsing:
|
|
|
|
* * Entry - catches unexpected characters
|
|
|
|
* * Semicolon - ;
|
|
|
|
* * Set - :=
|
|
|
|
* * Conditionls (IF) - if-then-else-end, cond ? a :b
|
|
|
|
* * BoolOps (BO) - &, |, ^
|
|
|
|
* * CompOps (CO) - ==, !=, ===, !==, >, <, >=, <=
|
|
|
|
* * SumRel (SR) - +, -
|
|
|
|
* * MulRel (MR) - *, /, %
|
|
|
|
* * Pow (P) - **
|
|
|
|
* * BoolNeg (BN) - ! operation
|
|
|
|
* * SpecialOperators (SO) - in and like
|
|
|
|
* * Unarys (U) - plus and minus in cases like -5 or -(2 * +2)
|
|
|
|
* * ListElement (LE) - list[number]
|
|
|
|
* * Braces (B) - ( and )
|
|
|
|
* * Functions (F)
|
|
|
|
* * Atom (A) - return value
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
|
|
|
|
class AFPToken {
|
2009-10-07 13:57:06 +00:00
|
|
|
// Types of tken
|
2015-09-28 18:03:35 +00:00
|
|
|
const TNONE = 'T_NONE';
|
2008-08-31 05:56:49 +00:00
|
|
|
const TID = 'T_ID';
|
2015-09-28 18:03:35 +00:00
|
|
|
const TKEYWORD = 'T_KEYWORD';
|
|
|
|
const TSTRING = 'T_STRING';
|
|
|
|
const TINT = 'T_INT';
|
|
|
|
const TFLOAT = 'T_FLOAT';
|
|
|
|
const TOP = 'T_OP';
|
|
|
|
const TBRACE = 'T_BRACE';
|
|
|
|
const TSQUAREBRACKET = 'T_SQUARE_BRACKET';
|
|
|
|
const TCOMMA = 'T_COMMA';
|
|
|
|
const TSTATEMENTSEPARATOR = 'T_STATEMENT_SEPARATOR';
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-10-15 12:35:03 +00:00
|
|
|
public $type;
|
|
|
|
public $value;
|
|
|
|
public $pos;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
public function __construct( $type = self::TNONE, $value = null, $pos = 0 ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->type = $type;
|
|
|
|
$this->value = $value;
|
|
|
|
$this->pos = $pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AFPData {
|
2010-02-13 14:10:36 +00:00
|
|
|
// Datatypes
|
2015-09-28 18:03:35 +00:00
|
|
|
const DINT = 'int';
|
|
|
|
const DSTRING = 'string';
|
|
|
|
const DNULL = 'null';
|
|
|
|
const DBOOL = 'bool';
|
|
|
|
const DFLOAT = 'float';
|
|
|
|
const DLIST = 'list';
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2014-06-23 18:25:11 +00:00
|
|
|
// Translation table mapping shell-style wildcards to PCRE equivalents.
|
|
|
|
// Derived from <http://www.php.net/manual/en/function.fnmatch.php#100207>
|
|
|
|
private static $wildcardMap = array(
|
2015-09-28 18:03:35 +00:00
|
|
|
'\*' => '.*',
|
|
|
|
'\+' => '\+',
|
|
|
|
'\-' => '\-',
|
|
|
|
'\.' => '\.',
|
|
|
|
'\?' => '.',
|
|
|
|
'\[' => '[',
|
2014-06-23 18:25:11 +00:00
|
|
|
'\[\!' => '[^',
|
2015-09-28 18:03:35 +00:00
|
|
|
'\\' => '\\\\',
|
|
|
|
'\]' => ']',
|
2014-06-23 18:25:11 +00:00
|
|
|
);
|
|
|
|
|
2013-10-15 12:35:03 +00:00
|
|
|
public $type;
|
|
|
|
public $data;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 21:01:29 +00:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param null $val
|
|
|
|
*/
|
2015-09-28 18:03:35 +00:00
|
|
|
public function __construct( $type = self::DNULL, $val = null ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->type = $type;
|
|
|
|
$this->data = $val;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $var
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function newFromPHPVar( $var ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( is_string( $var ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DSTRING, $var );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( is_int( $var ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DINT, $var );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( is_float( $var ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DFLOAT, $var );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( is_bool( $var ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $var );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( is_array( $var ) ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$result = array();
|
2010-08-19 21:12:09 +00:00
|
|
|
foreach ( $var as $item ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$result[] = self::newFromPHPVar( $item );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( self::DLIST, $result );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( is_null( $var ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
return new AFPData();
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-02-11 01:41:51 +00:00
|
|
|
throw new AFPException(
|
2010-08-19 21:12:09 +00:00
|
|
|
'Data type ' . gettype( $var ) . ' is not supported by AbuseFilter'
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function dup() {
|
|
|
|
return new AFPData( $this->type, $this->data );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $orig AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $target
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function castTypes( $orig, $target ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $orig->type == $target ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
return $orig->dup();
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DNULL ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
return new AFPData();
|
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $orig->type == self::DLIST ) {
|
|
|
|
if ( $target == self::DBOOL ) {
|
|
|
|
return new AFPData( self::DBOOL, (bool)count( $orig->data ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DFLOAT ) {
|
|
|
|
return new AFPData( self::DFLOAT, floatval( count( $orig->data ) ) );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DINT ) {
|
|
|
|
return new AFPData( self::DINT, intval( count( $orig->data ) ) );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DSTRING ) {
|
2009-05-26 13:08:15 +00:00
|
|
|
$s = '';
|
2010-08-19 21:12:09 +00:00
|
|
|
foreach ( $orig->data as $item ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$s .= $item->toString() . "\n";
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( self::DSTRING, $s );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DBOOL ) {
|
|
|
|
return new AFPData( self::DBOOL, (bool)$orig->data );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DFLOAT ) {
|
|
|
|
return new AFPData( self::DFLOAT, floatval( $orig->data ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DINT ) {
|
|
|
|
return new AFPData( self::DINT, intval( $orig->data ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DSTRING ) {
|
|
|
|
return new AFPData( self::DSTRING, strval( $orig->data ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $target == self::DLIST ) {
|
|
|
|
return new AFPData( self::DLIST, array( $orig ) );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $value AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function boolInvert( $value ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, !$value->toBool() );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $base AFPData
|
|
|
|
* @param $exponent AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function pow( $base, $exponent ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DFLOAT, pow( $base->toFloat(), $exponent->toFloat() ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function keywordIn( $a, $b ) {
|
|
|
|
$a = $a->toString();
|
|
|
|
$b = $b->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $a == '' || $b == '' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, false );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, strpos( $b, $a ) !== false );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function keywordContains( $a, $b ) {
|
|
|
|
$a = $a->toString();
|
|
|
|
$b = $b->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $a == '' || $b == '' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, false );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, strpos( $a, $b ) !== false );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @param $list
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-04-05 17:11:17 +00:00
|
|
|
public static function listContains( $value, $list ) {
|
|
|
|
// Should use built-in PHP function somehow
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $list->data as $item ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( self::equals( $value, $item ) ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
return true;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-04-05 17:11:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $d1 AFPData
|
|
|
|
* @param $d2 AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2009-04-05 17:11:17 +00:00
|
|
|
public static function equals( $d1, $d2 ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return $d1->type != self::DLIST && $d2->type != self::DLIST &&
|
|
|
|
$d1->toString() === $d2->toString();
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $str AFPData
|
|
|
|
* @param $pattern AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function keywordLike( $str, $pattern ) {
|
|
|
|
$str = $str->toString();
|
2014-06-23 18:25:11 +00:00
|
|
|
$pattern = '#^' . strtr( preg_quote( $pattern->toString(), '#' ), self::$wildcardMap ) . '$#u';
|
2008-08-31 05:56:49 +00:00
|
|
|
wfSuppressWarnings();
|
2014-06-23 18:25:11 +00:00
|
|
|
$result = preg_match( $pattern, $str );
|
2008-08-31 05:56:49 +00:00
|
|
|
wfRestoreWarnings();
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( self::DBOOL, (bool)$result );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $str AFPData
|
|
|
|
* @param $regex AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $pos
|
|
|
|
* @param $insensitive bool
|
|
|
|
* @return AFPData
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2010-03-28 00:50:51 +00:00
|
|
|
public static function keywordRegex( $str, $regex, $pos, $insensitive = false ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$str = $str->toString();
|
2011-02-10 17:25:25 +00:00
|
|
|
$pattern = $regex->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-25 12:43:53 +00:00
|
|
|
$pattern = preg_replace( '!(\\\\\\\\)*(\\\\)?/!', '$1\/', $pattern );
|
2009-03-22 10:34:54 +00:00
|
|
|
$pattern = "/$pattern/u";
|
2010-08-19 21:12:09 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $insensitive ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
$pattern .= 'i';
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-10-21 23:20:07 +00:00
|
|
|
$result = preg_match( $pattern, $str );
|
|
|
|
if ( $result === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'regexfailure',
|
|
|
|
$pos,
|
|
|
|
array( 'unspecified error in preg_match()', $pattern )
|
|
|
|
);
|
2009-06-18 20:13:52 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( self::DBOOL, (bool)$result );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $str
|
|
|
|
* @param $regex
|
|
|
|
* @param $pos
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2010-03-28 00:50:51 +00:00
|
|
|
public static function keywordRegexInsensitive( $str, $regex, $pos ) {
|
|
|
|
return self::keywordRegex( $str, $regex, $pos, true );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $data AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function unaryMinus( $data ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $data->type == self::DINT ) {
|
|
|
|
return new AFPData( $data->type, -$data->toInt() );
|
2008-08-31 05:56:49 +00:00
|
|
|
} else {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( $data->type, -$data->toFloat() );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
|
|
|
* @param $op string
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function boolOp( $a, $b, $op ) {
|
|
|
|
$a = $a->toBool();
|
|
|
|
$b = $b->toBool();
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $op == '|' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a || $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '&' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a && $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '^' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a xor $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-01-26 23:32:46 +00:00
|
|
|
throw new AFPException( "Invalid boolean operation: {$op}" ); // Should never happen.
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
|
|
|
* @param $op string
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function compareOp( $a, $b, $op ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $op == '==' || $op == '=' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, self::equals( $a, $b ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '!=' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, !self::equals( $a, $b ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '===' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a->type == $b->type && self::equals( $a, $b ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '!==' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a->type != $b->type || !self::equals( $a, $b ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$a = $a->toString();
|
|
|
|
$b = $b->toString();
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $op == '>' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a > $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '<' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a < $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '>=' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a >= $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '<=' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DBOOL, $a <= $b );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-01-26 23:32:46 +00:00
|
|
|
throw new AFPException( "Invalid comparison operation: {$op}" ); // Should never happen
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
|
|
|
* @param $op string
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $pos
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2010-02-13 14:10:36 +00:00
|
|
|
public static function mulRel( $a, $b, $op, $pos ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
// Figure out the type.
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $a->type == self::DFLOAT || $b->type == self::DFLOAT ||
|
|
|
|
$a->toFloat() != $a->toString() || $b->toFloat() != $b->toString()
|
|
|
|
) {
|
|
|
|
$type = self::DFLOAT;
|
2008-08-31 05:56:49 +00:00
|
|
|
$a = $a->toFloat();
|
|
|
|
$b = $b->toFloat();
|
|
|
|
} else {
|
2015-09-28 18:03:35 +00:00
|
|
|
$type = self::DINT;
|
2008-08-31 05:56:49 +00:00
|
|
|
$a = $a->toInt();
|
|
|
|
$b = $b->toInt();
|
|
|
|
}
|
2009-01-25 05:25:33 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $op != '*' && $b == 0 ) {
|
|
|
|
throw new AFPUserVisibleException( 'dividebyzero', $pos, array( $a ) );
|
2009-01-25 05:54:49 +00:00
|
|
|
}
|
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $op == '*' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$data = $a * $b;
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( $op == '/' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$data = $a / $b;
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( $op == '%' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$data = $a % $b;
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2015-09-28 18:03:35 +00:00
|
|
|
// Should never happen
|
|
|
|
throw new AFPException( "Invalid multiplication-related operation: {$op}" );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $type == self::DINT ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$data = intval( $data );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2012-03-11 20:40:04 +00:00
|
|
|
$data = floatval( $data );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
return new AFPData( $type, $data );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function sum( $a, $b ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $a->type == self::DSTRING || $b->type == self::DSTRING ) {
|
|
|
|
return new AFPData( self::DSTRING, $a->toString() . $b->toString() );
|
|
|
|
} elseif ( $a->type == self::DLIST && $b->type == self::DLIST ) {
|
|
|
|
return new AFPData( self::DLIST, array_merge( $a->toList(), $b->toList() ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DFLOAT, $a->toFloat() + $b->toFloat() );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $a AFPData
|
|
|
|
* @param $b AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public static function sub( $a, $b ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( self::DFLOAT, $a->toFloat() - $b->toFloat() );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
/** Convert shorteners */
|
2011-08-24 22:11:52 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
/**
|
2012-09-02 11:07:02 +00:00
|
|
|
* @throws MWException
|
2012-02-10 23:41:05 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function toNative() {
|
2015-09-28 18:03:35 +00:00
|
|
|
switch ( $this->type ) {
|
|
|
|
case self::DBOOL:
|
2012-02-10 23:41:05 +00:00
|
|
|
return $this->toBool();
|
2015-09-28 18:03:35 +00:00
|
|
|
case self::DSTRING:
|
2012-02-10 23:41:05 +00:00
|
|
|
return $this->toString();
|
2015-09-28 18:03:35 +00:00
|
|
|
case self::DFLOAT:
|
2012-02-10 23:41:05 +00:00
|
|
|
return $this->toFloat();
|
2015-09-28 18:03:35 +00:00
|
|
|
case self::DINT:
|
2012-02-10 23:41:05 +00:00
|
|
|
return $this->toInt();
|
2015-09-28 18:03:35 +00:00
|
|
|
case self::DLIST:
|
2012-02-10 23:41:05 +00:00
|
|
|
$input = $this->toList();
|
|
|
|
$output = array();
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $input as $item ) {
|
2012-02-10 23:41:05 +00:00
|
|
|
$output[] = $item->toNative();
|
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
return $output;
|
2015-09-28 18:03:35 +00:00
|
|
|
case self::DNULL:
|
2012-02-10 23:41:05 +00:00
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
throw new MWException( "Unknown type" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function toBool() {
|
2015-09-28 18:03:35 +00:00
|
|
|
return self::castTypes( $this, self::DBOOL )->data;
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function toString() {
|
2015-09-28 18:03:35 +00:00
|
|
|
return self::castTypes( $this, self::DSTRING )->data;
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function toFloat() {
|
2015-09-28 18:03:35 +00:00
|
|
|
return self::castTypes( $this, self::DFLOAT )->data;
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function toInt() {
|
2015-09-28 18:03:35 +00:00
|
|
|
return self::castTypes( $this, self::DINT )->data;
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
|
|
|
|
public function toList() {
|
2015-09-28 18:03:35 +00:00
|
|
|
return self::castTypes( $this, self::DLIST )->data;
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 11:47:42 +00:00
|
|
|
class AFPParserState {
|
2015-08-25 17:27:15 +00:00
|
|
|
public $pos, $token;
|
2009-04-05 11:47:42 +00:00
|
|
|
|
|
|
|
public function __construct( $token, $pos ) {
|
|
|
|
$this->token = $token;
|
|
|
|
$this->pos = $pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
class AFPException extends MWException {
|
|
|
|
}
|
2009-01-26 23:32:46 +00:00
|
|
|
|
|
|
|
// Exceptions that we might conceivably want to report to ordinary users
|
|
|
|
// (i.e. exceptions that don't represent bugs in the extension itself)
|
|
|
|
class AFPUserVisibleException extends AFPException {
|
2012-09-02 11:07:02 +00:00
|
|
|
/**
|
|
|
|
* @param string $exception_id
|
|
|
|
* @param int $position
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2009-10-07 13:57:06 +00:00
|
|
|
function __construct( $exception_id, $position, $params ) {
|
2013-03-07 00:04:39 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// abusefilter-exception-unexpectedatend, abusefilter-exception-expectednotfound
|
|
|
|
// abusefilter-exception-unrecognisedkeyword, abusefilter-exception-unexpectedtoken
|
|
|
|
// abusefilter-exception-unclosedstring, abusefilter-exception-invalidoperator
|
|
|
|
// abusefilter-exception-unrecognisedtoken, abusefilter-exception-noparams
|
|
|
|
// abusefilter-exception-dividebyzero, abusefilter-exception-unrecognisedvar
|
|
|
|
// abusefilter-exception-notenoughargs, abusefilter-exception-regexfailure
|
|
|
|
// abusefilter-exception-overridebuiltin, abusefilter-exception-outofbounds
|
|
|
|
// abusefilter-exception-notlist
|
2012-09-02 11:07:02 +00:00
|
|
|
$msg = wfMessage(
|
|
|
|
'abusefilter-exception-' . $exception_id,
|
|
|
|
array_merge( array( $position ), $params )
|
|
|
|
)->text();
|
2009-01-26 23:32:46 +00:00
|
|
|
parent::__construct( $msg );
|
|
|
|
|
|
|
|
$this->mExceptionID = $exception_id;
|
|
|
|
$this->mPosition = $position;
|
|
|
|
$this->mParams = $params;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
class AbuseFilterParser {
|
2015-08-21 20:58:41 +00:00
|
|
|
public $mCode, $mTokens, $mPos, $mCur, $mShortCircuit, $mAllowShort, $mLen;
|
2012-03-11 20:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbuseFilterVariableHolder
|
|
|
|
*/
|
2013-10-15 12:35:03 +00:00
|
|
|
public $mVars;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-04-17 16:36:10 +00:00
|
|
|
// length,lcase,ucase,ccnorm,rmdoubles,specialratio,rmspecials,norm,count
|
2015-09-28 18:03:35 +00:00
|
|
|
public static $mFunctions = array(
|
2009-03-07 01:26:42 +00:00
|
|
|
'lcase' => 'funcLc',
|
2013-04-17 16:36:10 +00:00
|
|
|
'ucase' => 'funcUc',
|
2009-03-07 01:26:42 +00:00
|
|
|
'length' => 'funcLen',
|
|
|
|
'string' => 'castString',
|
|
|
|
'int' => 'castInt',
|
|
|
|
'float' => 'castFloat',
|
|
|
|
'bool' => 'castBool',
|
|
|
|
'norm' => 'funcNorm',
|
|
|
|
'ccnorm' => 'funcCCNorm',
|
|
|
|
'specialratio' => 'funcSpecialRatio',
|
|
|
|
'rmspecials' => 'funcRMSpecials',
|
|
|
|
'rmdoubles' => 'funcRMDoubles',
|
|
|
|
'rmwhitespace' => 'funcRMWhitespace',
|
|
|
|
'count' => 'funcCount',
|
|
|
|
'rcount' => 'funcRCount',
|
2009-03-09 12:39:52 +00:00
|
|
|
'ip_in_range' => 'funcIPInRange',
|
2009-03-26 02:03:32 +00:00
|
|
|
'contains_any' => 'funcContainsAny',
|
2009-04-01 05:05:23 +00:00
|
|
|
'substr' => 'funcSubstr',
|
|
|
|
'strlen' => 'funcLen',
|
|
|
|
'strpos' => 'funcStrPos',
|
|
|
|
'str_replace' => 'funcStrReplace',
|
2011-10-18 17:57:33 +00:00
|
|
|
'rescape' => 'funcStrRegexEscape',
|
2009-04-01 06:53:18 +00:00
|
|
|
'set' => 'funcSetVar',
|
|
|
|
'set_var' => 'funcSetVar',
|
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 06:53:18 +00:00
|
|
|
// Functions that affect parser state, and shouldn't be cached.
|
2015-09-28 18:03:35 +00:00
|
|
|
public static $ActiveFunctions = array(
|
2009-04-01 06:53:18 +00:00
|
|
|
'funcSetVar',
|
2008-08-31 05:56:49 +00:00
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
public static $funcCache = array();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
/**
|
|
|
|
* Create a new instance
|
|
|
|
*
|
|
|
|
* @param $vars AbuseFilterVariableHolder
|
|
|
|
*/
|
|
|
|
public function __construct( $vars = null ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->resetState();
|
2013-01-07 00:02:41 +00:00
|
|
|
if ( $vars instanceof AbuseFilterVariableHolder ) {
|
|
|
|
$this->mVars = $vars;
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
public function resetState() {
|
|
|
|
$this->mCode = '';
|
|
|
|
$this->mTokens = array();
|
2009-02-26 12:15:14 +00:00
|
|
|
$this->mVars = new AbuseFilterVariableHolder;
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->mPos = 0;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = false;
|
|
|
|
$this->mAllowShort = true;
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function checkSyntax( $filter ) {
|
|
|
|
try {
|
2009-03-25 11:48:33 +00:00
|
|
|
$origAS = $this->mAllowShort;
|
|
|
|
$this->mAllowShort = false;
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->parse( $filter );
|
|
|
|
} catch ( AFPUserVisibleException $excep ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mAllowShort = $origAS;
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
return array( $excep->getMessage(), $excep->mPosition );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mAllowShort = $origAS;
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
*/
|
2009-02-26 12:15:14 +00:00
|
|
|
public function setVar( $name, $value ) {
|
|
|
|
$this->mVars->setVar( $name, $value );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $vars
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function setVars( $vars ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
if ( is_array( $vars ) ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $vars as $name => $var ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$this->setVar( $name, $var );
|
|
|
|
}
|
|
|
|
} elseif ( $vars instanceof AbuseFilterVariableHolder ) {
|
2013-03-06 06:21:55 +00:00
|
|
|
$this->mVars->addHolders( $vars );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return AFPToken
|
|
|
|
*/
|
2015-02-04 18:25:21 +00:00
|
|
|
protected function move() {
|
2015-08-25 19:57:23 +00:00
|
|
|
list( $this->mCur, $this->mPos ) = $this->mTokens[$this->mPos];
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-04-05 11:47:42 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* getState() function allows parser state to be rollbacked to several tokens back
|
|
|
|
* @return AFPParserState
|
|
|
|
*/
|
2009-04-05 11:47:42 +00:00
|
|
|
protected function getState() {
|
|
|
|
return new AFPParserState( $this->mCur, $this->mPos );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* setState() function allows parser state to be rollbacked to several tokens back
|
|
|
|
* @param AFPParserState $state
|
|
|
|
*/
|
2009-04-05 11:47:42 +00:00
|
|
|
protected function setState( AFPParserState $state ) {
|
|
|
|
$this->mCur = $state->token;
|
|
|
|
$this->mPos = $state->pos;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-03-25 11:48:33 +00:00
|
|
|
protected function skipOverBraces() {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == '(' ) ||
|
|
|
|
!$this->mShortCircuit
|
|
|
|
) {
|
2009-03-25 11:48:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$braces = 1;
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type != AFPToken::TNONE && $braces > 0 ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TBRACE ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $this->mCur->value == '(' ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$braces++;
|
2010-02-13 14:10:36 +00:00
|
|
|
} elseif ( $this->mCur->value == ')' ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$braces--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == ')' ) )
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound', $this->mCur->pos, array( ')' ) );
|
2009-03-25 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $code
|
|
|
|
* @return bool
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function parse( $code ) {
|
|
|
|
return $this->intEval( $code )->toBool();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
public function evaluateExpression( $filter ) {
|
|
|
|
return $this->intEval( $filter )->toString();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $code
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
function intEval( $code ) {
|
2009-02-11 20:00:33 +00:00
|
|
|
// Setup, resetting
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->mCode = $code;
|
2015-08-25 19:57:23 +00:00
|
|
|
$this->mTokens = AbuseFilterTokenizer::tokenize( $code );
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->mPos = 0;
|
2009-02-11 20:00:33 +00:00
|
|
|
$this->mLen = strlen( $code );
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = false;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = new AFPData();
|
|
|
|
$this->doLevelEntry( $result );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $a
|
|
|
|
* @param $b
|
|
|
|
* @return int
|
|
|
|
*/
|
2009-02-11 01:41:51 +00:00
|
|
|
static function lengthCompare( $a, $b ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( strlen( $a ) == strlen( $b ) ) {
|
2009-02-11 01:41:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return ( strlen( $a ) < strlen( $b ) ) ? -1 : 1;
|
2009-02-11 01:41:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
/* Levels */
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Handles unexpected characters after the expression
|
|
|
|
*
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $result AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @throws AFPUserVisibleException
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelEntry( &$result ) {
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->doLevelSemicolon( $result );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type != AFPToken::TNONE ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'unexpectedatend',
|
|
|
|
$this->mCur->pos, array( $this->mCur->type )
|
|
|
|
);
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Handles multiple expressions
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $result AFPData
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2009-04-05 11:47:42 +00:00
|
|
|
protected function doLevelSemicolon( &$result ) {
|
2009-04-01 06:53:18 +00:00
|
|
|
do {
|
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type != AFPToken::TSTATEMENTSEPARATOR ) {
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->doLevelSet( $result );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
} while ( $this->mCur->type == AFPToken::TSTATEMENTSEPARATOR );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Handles multiple expressions
|
|
|
|
*
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $result AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @throws AFPUserVisibleException
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2009-04-05 11:47:42 +00:00
|
|
|
protected function doLevelSet( &$result ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TID ) {
|
2009-04-05 11:47:42 +00:00
|
|
|
$varname = $this->mCur->value;
|
|
|
|
$prev = $this->getState();
|
|
|
|
$this->move();
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TOP && $this->mCur->value == ':=' ) {
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->move();
|
|
|
|
$this->doLevelSet( $result );
|
|
|
|
$this->setUserVariable( $varname, $result );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-04-05 11:47:42 +00:00
|
|
|
return;
|
2015-09-28 18:03:35 +00:00
|
|
|
} elseif ( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == '[' ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( !$this->mVars->varIsSet( $varname ) ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
throw new AFPUserVisibleException( 'unrecognisedvar',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
2011-02-10 17:32:57 +00:00
|
|
|
array( $varname )
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
$list = $this->mVars->getVar( $varname );
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $list->type != AFPData::DLIST ) {
|
2009-05-22 06:42:10 +00:00
|
|
|
throw new AFPUserVisibleException( 'notlist', $this->mCur->pos, array() );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
$list = $list->toList();
|
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == ']' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$idx = 'new';
|
|
|
|
} else {
|
2009-05-22 06:42:10 +00:00
|
|
|
$this->setState( $prev );
|
|
|
|
$this->move();
|
2009-04-05 17:11:17 +00:00
|
|
|
$idx = new AFPData();
|
|
|
|
$this->doLevelSemicolon( $idx );
|
|
|
|
$idx = $idx->toInt();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == ']' ) ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound', $this->mCur->pos,
|
|
|
|
array( ']', $this->mCur->type, $this->mCur->value ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( count( $list ) <= $idx ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
throw new AFPUserVisibleException( 'outofbounds', $this->mCur->pos,
|
|
|
|
array( $idx, count( $result->data ) ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TOP && $this->mCur->value == ':=' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$this->move();
|
|
|
|
$this->doLevelSet( $result );
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $idx === 'new' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$list[] = $result;
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-04-05 17:11:17 +00:00
|
|
|
$list[$idx] = $result;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
$this->setUserVariable( $varname, new AFPData( AFPData::DLIST, $list ) );
|
|
|
|
|
2009-04-05 17:11:17 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$this->setState( $prev );
|
|
|
|
}
|
2009-04-05 11:47:42 +00:00
|
|
|
} else {
|
|
|
|
$this->setState( $prev );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->doLevelConditions( $result );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $result AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 15:12:55 +00:00
|
|
|
protected function doLevelConditions( &$result ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TKEYWORD && $this->mCur->value == 'if' ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->move();
|
|
|
|
$this->doLevelBoolOps( $result );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TKEYWORD && $this->mCur->value == 'then' ) )
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
'then',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->move();
|
2009-03-18 23:28:35 +00:00
|
|
|
|
2008-08-31 15:12:55 +00:00
|
|
|
$r1 = new AFPData();
|
|
|
|
$r2 = new AFPData();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-18 23:28:35 +00:00
|
|
|
$isTrue = $result->toBool();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( !$isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$scOrig = $this->mShortCircuit;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = $this->mAllowShort;
|
2009-03-18 23:28:35 +00:00
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelConditions( $r1 );
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( !$isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->mShortCircuit = $scOrig;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TKEYWORD && $this->mCur->value == 'else' ) )
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
'else',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->move();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( $isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$scOrig = $this->mShortCircuit;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = $this->mAllowShort;
|
2009-03-18 23:28:35 +00:00
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelConditions( $r2 );
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( $isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->mShortCircuit = $scOrig;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TKEYWORD && $this->mCur->value == 'end' ) )
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
'end',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->move();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $result->toBool() ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$result = $r1;
|
|
|
|
} else {
|
|
|
|
$result = $r2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->doLevelBoolOps( $result );
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TOP && $this->mCur->value == '?' ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->move();
|
|
|
|
$r1 = new AFPData();
|
|
|
|
$r2 = new AFPData();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-18 23:28:35 +00:00
|
|
|
$isTrue = $result->toBool();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( !$isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$scOrig = $this->mShortCircuit;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = $this->mAllowShort;
|
2009-03-18 23:28:35 +00:00
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelConditions( $r1 );
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( !$isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->mShortCircuit = $scOrig;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TOP && $this->mCur->value == ':' ) )
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
':',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->move();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( $isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$scOrig = $this->mShortCircuit;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = $this->mAllowShort;
|
2009-03-18 23:28:35 +00:00
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelConditions( $r2 );
|
2013-03-23 05:39:27 +00:00
|
|
|
if ( $isTrue ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->mShortCircuit = $scOrig;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $isTrue ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$result = $r1;
|
|
|
|
} else {
|
|
|
|
$result = $r2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $result AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelBoolOps( &$result ) {
|
|
|
|
$this->doLevelCompares( $result );
|
|
|
|
$ops = array( '&', '|', '^' );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TOP && in_array( $this->mCur->value, $ops ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$op = $this->mCur->value;
|
|
|
|
$this->move();
|
|
|
|
$r2 = new AFPData();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-12-20 01:34:28 +00:00
|
|
|
// We can go on quickly as either one statement with | is true or on with & is false
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( ( $op == '&' && !$result->toBool() ) || ( $op == '|' && $result->toBool() ) ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-shortcircuit' );
|
2009-03-18 23:28:35 +00:00
|
|
|
$orig = $this->mShortCircuit;
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->mShortCircuit = $this->mAllowShort;
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->doLevelCompares( $r2 );
|
2009-03-19 00:07:29 +00:00
|
|
|
$this->mShortCircuit = $orig;
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = new AFPData( AFPData::DBOOL, $result->toBool() );
|
2009-10-07 13:57:06 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-shortcircuit' );
|
2009-03-19 00:18:03 +00:00
|
|
|
continue;
|
2009-03-18 23:28:35 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->doLevelCompares( $r2 );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = AFPData::boolOp( $result, $r2, $op );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelCompares( &$result ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelSumRels( $result );
|
2008-08-31 05:56:49 +00:00
|
|
|
$ops = array( '==', '===', '!=', '!==', '<', '>', '<=', '>=', '=' );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TOP && in_array( $this->mCur->value, $ops ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$op = $this->mCur->value;
|
|
|
|
$this->move();
|
|
|
|
$r2 = new AFPData();
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelSumRels( $r2 );
|
2016-04-09 13:00:02 +00:00
|
|
|
AbuseFilter::triggerLimiter();
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = AFPData::compareOp( $result, $r2, $op );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 15:12:55 +00:00
|
|
|
protected function doLevelSumRels( &$result ) {
|
|
|
|
$this->doLevelMulRels( $result );
|
|
|
|
$ops = array( '+', '-' );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TOP && in_array( $this->mCur->value, $ops ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$op = $this->mCur->value;
|
|
|
|
$this->move();
|
|
|
|
$r2 = new AFPData();
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelMulRels( $r2 );
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $op == '+' ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$result = AFPData::sum( $result, $r2 );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $op == '-' ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$result = AFPData::sub( $result, $r2 );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 15:12:55 +00:00
|
|
|
protected function doLevelMulRels( &$result ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->doLevelPow( $result );
|
2008-08-31 15:12:55 +00:00
|
|
|
$ops = array( '*', '/', '%' );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TOP && in_array( $this->mCur->value, $ops ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$op = $this->mCur->value;
|
|
|
|
$this->move();
|
|
|
|
$r2 = new AFPData();
|
|
|
|
$this->doLevelPow( $r2 );
|
2009-02-11 01:41:51 +00:00
|
|
|
$result = AFPData::mulRel( $result, $r2, $op, $this->mCur->pos );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-31 15:12:55 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelPow( &$result ) {
|
|
|
|
$this->doLevelBoolInvert( $result );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TOP && $this->mCur->value == '**' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->move();
|
|
|
|
$expanent = new AFPData();
|
|
|
|
$this->doLevelBoolInvert( $expanent );
|
|
|
|
$result = AFPData::pow( $result, $expanent );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelBoolInvert( &$result ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TOP && $this->mCur->value == '!' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->move();
|
|
|
|
$this->doLevelSpecialWords( $result );
|
|
|
|
$result = AFPData::boolInvert( $result );
|
|
|
|
} else {
|
|
|
|
$this->doLevelSpecialWords( $result );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelSpecialWords( &$result ) {
|
|
|
|
$this->doLevelUnarys( $result );
|
2009-10-07 13:57:06 +00:00
|
|
|
$keyword = strtolower( $this->mCur->value );
|
|
|
|
$specwords = array(
|
|
|
|
'in' => 'keywordIn',
|
|
|
|
'like' => 'keywordLike',
|
|
|
|
'matches' => 'keywordLike',
|
|
|
|
'contains' => 'keywordContains',
|
|
|
|
'rlike' => 'keywordRegex',
|
2010-03-28 00:50:51 +00:00
|
|
|
'irlike' => 'keywordRegexInsensitive',
|
2009-10-07 13:57:06 +00:00
|
|
|
'regex' => 'keywordRegex'
|
|
|
|
);
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TKEYWORD && in_array( $keyword, array_keys( $specwords ) ) ) {
|
2009-01-30 23:46:25 +00:00
|
|
|
$func = $specwords[$keyword];
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->move();
|
|
|
|
$r2 = new AFPData();
|
|
|
|
$this->doLevelUnarys( $r2 );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $this->mShortCircuit ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
return; // The result doesn't matter.
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-04-09 13:00:02 +00:00
|
|
|
AbuseFilter::triggerLimiter();
|
2009-10-07 13:57:06 +00:00
|
|
|
wfProfileIn( __METHOD__ . "-$func" );
|
2009-04-01 05:56:24 +00:00
|
|
|
$result = AFPData::$func( $result, $r2, $this->mCur->pos );
|
2009-10-07 13:57:06 +00:00
|
|
|
wfProfileOut( __METHOD__ . "-$func" );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelUnarys( &$result ) {
|
|
|
|
$op = $this->mCur->value;
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TOP && ( $op == "+" || $op == "-" ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->move();
|
2009-04-05 17:11:17 +00:00
|
|
|
$this->doLevelListElements( $result );
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $op == '-' ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = AFPData::unaryMinus( $result );
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-05 17:11:17 +00:00
|
|
|
$this->doLevelListElements( $result );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-05 17:11:17 +00:00
|
|
|
protected function doLevelListElements( &$result ) {
|
|
|
|
$this->doLevelBraces( $result );
|
2015-09-28 18:03:35 +00:00
|
|
|
while ( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == '[' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$idx = new AFPData();
|
|
|
|
$this->doLevelSemicolon( $idx );
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == ']' ) ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound', $this->mCur->pos,
|
2015-09-28 18:03:35 +00:00
|
|
|
array( ']', $this->mCur->type, $this->mCur->value ) );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
$idx = $idx->toInt();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $result->type == AFPData::DLIST ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( count( $result->data ) <= $idx ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
throw new AFPUserVisibleException( 'outofbounds', $this->mCur->pos,
|
|
|
|
array( $idx, count( $result->data ) ) );
|
|
|
|
}
|
|
|
|
$result = $result->data[$idx];
|
|
|
|
} else {
|
|
|
|
throw new AFPUserVisibleException( 'notlist', $this->mCur->pos, array() );
|
|
|
|
}
|
|
|
|
$this->move();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelBraces( &$result ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == '(' ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->skipOverBraces();
|
|
|
|
} else {
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->doLevelSemicolon( $result );
|
2009-03-25 11:48:33 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == ')' ) )
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'expectednotfound',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( ')', $this->mCur->type, $this->mCur->value )
|
|
|
|
);
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->move();
|
|
|
|
} else {
|
|
|
|
$this->doLevelFunction( $result );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelFunction( &$result ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TID && isset( self::$mFunctions[$this->mCur->value] ) ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$func = self::$mFunctions[$this->mCur->value];
|
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type != AFPToken::TBRACE || $this->mCur->value != '(' ) {
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
'(',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-03-25 11:48:33 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
2009-03-25 11:48:33 +00:00
|
|
|
$this->skipOverBraces();
|
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-03-25 11:48:33 +00:00
|
|
|
return; // The result doesn't matter.
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ . '-loadargs' );
|
2008-08-31 05:56:49 +00:00
|
|
|
$args = array();
|
2009-01-23 19:23:19 +00:00
|
|
|
do {
|
|
|
|
$r = new AFPData();
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->doLevelSemicolon( $r );
|
2009-01-23 19:23:19 +00:00
|
|
|
$args[] = $r;
|
2015-09-28 18:03:35 +00:00
|
|
|
} while ( $this->mCur->type == AFPToken::TCOMMA );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type != AFPToken::TBRACE || $this->mCur->value != ')' ) {
|
2009-03-25 11:36:38 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound',
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
')',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-03-18 23:28:35 +00:00
|
|
|
$this->move();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-loadargs' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ . "-$func" );
|
|
|
|
|
|
|
|
$funcHash = md5( $func . serialize( $args ) );
|
|
|
|
|
|
|
|
if ( isset( self::$funcCache[$funcHash] ) &&
|
2015-09-28 18:03:35 +00:00
|
|
|
!in_array( $func, self::$ActiveFunctions )
|
|
|
|
) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = self::$funcCache[$funcHash];
|
|
|
|
} else {
|
2009-01-23 19:23:19 +00:00
|
|
|
AbuseFilter::triggerLimiter();
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = self::$funcCache[$funcHash] = $this->$func( $args );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( count( self::$funcCache ) > 1000 ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
self::$funcCache = array();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ . "-$func" );
|
2008-08-31 05:56:49 +00:00
|
|
|
} else {
|
|
|
|
$this->doLevelAtom( $result );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelAtom( &$result ) {
|
|
|
|
$tok = $this->mCur->value;
|
2015-09-28 18:03:35 +00:00
|
|
|
switch ( $this->mCur->type ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
case AFPToken::TID:
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
2009-03-18 23:28:35 +00:00
|
|
|
break;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
$var = strtolower( $tok );
|
2009-02-26 12:15:14 +00:00
|
|
|
$result = $this->getVarValue( $var );
|
2008-08-31 05:56:49 +00:00
|
|
|
break;
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TSTRING:
|
|
|
|
$result = new AFPData( AFPData::DSTRING, $tok );
|
2008-08-31 05:56:49 +00:00
|
|
|
break;
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TFLOAT:
|
|
|
|
$result = new AFPData( AFPData::DFLOAT, $tok );
|
2008-08-31 05:56:49 +00:00
|
|
|
break;
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TINT:
|
|
|
|
$result = new AFPData( AFPData::DINT, $tok );
|
2008-08-31 05:56:49 +00:00
|
|
|
break;
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TKEYWORD:
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $tok == "true" ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = new AFPData( AFPData::DBOOL, true );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( $tok == "false" ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = new AFPData( AFPData::DBOOL, false );
|
2010-08-19 21:12:09 +00:00
|
|
|
} elseif ( $tok == "null" ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = new AFPData();
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'unrecognisedkeyword',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( $tok )
|
|
|
|
);
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
break;
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TNONE:
|
2009-04-01 06:53:18 +00:00
|
|
|
return; // Handled at entry level
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TBRACE:
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $this->mCur->value == ')' ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
return; // Handled at the entry level
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
case AFPToken::TSQUAREBRACKET:
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $this->mCur->value == '[' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
$list = array();
|
2015-09-28 18:03:35 +00:00
|
|
|
// @codingStandardsIgnoreStart Space beforsemi-colon ignored.
|
2010-02-13 14:10:36 +00:00
|
|
|
for ( ; ; ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2009-04-05 17:11:17 +00:00
|
|
|
$this->move();
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == ']' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
break;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
$item = new AFPData();
|
|
|
|
$this->doLevelSet( $item );
|
|
|
|
$list[] = $item;
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TSQUAREBRACKET && $this->mCur->value == ']' ) {
|
2009-04-05 17:11:17 +00:00
|
|
|
break;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $this->mCur->type != AFPToken::TCOMMA ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'expectednotfound',
|
2009-04-05 17:11:17 +00:00
|
|
|
$this->mCur->pos,
|
2010-08-19 21:12:09 +00:00
|
|
|
array( ', or ]', $this->mCur->type, $this->mCur->value )
|
|
|
|
);
|
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = new AFPData( AFPData::DLIST, $list );
|
2009-04-05 17:11:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
default:
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'unexpectedtoken',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array(
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
|
|
|
)
|
|
|
|
);
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
$this->move();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-05 11:47:42 +00:00
|
|
|
/* End of levels */
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $var
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-02-26 12:15:14 +00:00
|
|
|
protected function getVarValue( $var ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
$var = strtolower( $var );
|
2009-03-05 02:43:05 +00:00
|
|
|
$builderValues = AbuseFilter::getBuilderValues();
|
2009-04-01 06:53:18 +00:00
|
|
|
if ( !( array_key_exists( $var, $builderValues['vars'] )
|
2015-09-28 18:03:35 +00:00
|
|
|
|| $this->mVars->varIsSet( $var ) )
|
|
|
|
) {
|
2009-02-26 12:15:14 +00:00
|
|
|
// If the variable is invalid, throw an exception
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'unrecognisedvar',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( $var )
|
|
|
|
);
|
2009-02-26 12:15:14 +00:00
|
|
|
} else {
|
2015-02-04 18:25:21 +00:00
|
|
|
return $this->mVars->getVar( $var );
|
2009-02-26 12:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-05 11:47:42 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-05 11:47:42 +00:00
|
|
|
protected function setUserVariable( $name, $value ) {
|
|
|
|
$builderValues = AbuseFilter::getBuilderValues();
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( array_key_exists( $name, $builderValues['vars'] ) ) {
|
2009-04-05 11:47:42 +00:00
|
|
|
throw new AFPUserVisibleException( 'overridebuiltin', $this->mCur->pos, array( $name ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->mVars->setVar( $name, $value );
|
|
|
|
}
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Built-in functions
|
2012-03-11 20:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcLc( $args ) {
|
|
|
|
global $wgContLang;
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'lc', 2, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( AFPData::DSTRING, $wgContLang->lc( $s ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-04-17 16:36:10 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcUc( $args ) {
|
|
|
|
global $wgContLang;
|
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'uc', 2, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$s = $args[0]->toString();
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $wgContLang->uc( $s ) );
|
|
|
|
}
|
2013-04-17 16:36:10 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcLen( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'len', 2, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $args[0]->type == AFPData::DLIST ) {
|
2012-12-20 01:19:55 +00:00
|
|
|
// Don't use toString on lists, but count
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DINT, count( $args[0]->data ) );
|
2012-12-20 01:19:55 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( AFPData::DINT, mb_strlen( $s, 'utf-8' ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcSimpleNorm( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'simplenorm', 2, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = preg_replace( '/[\d\W]+/', '', $s );
|
2011-02-10 17:25:25 +00:00
|
|
|
$s = strtolower( $s );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcSpecialRatio( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'specialratio', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !strlen( $s ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DFLOAT, 0 );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$nospecials = $this->rmspecials( $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$val = 1. - ( ( mb_strlen( $nospecials ) / mb_strlen( $s ) ) );
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DFLOAT, $val );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcCount( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'count', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $args[0]->type == AFPData::DLIST && count( $args ) == 1 ) {
|
|
|
|
return new AFPData( AFPData::DINT, count( $args[0]->data ) );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
$offset = -1;
|
2009-04-05 17:11:17 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( count( $args ) == 1 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
$count = count( explode( ',', $args[0]->toString() ) );
|
2008-08-31 05:56:49 +00:00
|
|
|
} else {
|
|
|
|
$needle = $args[0]->toString();
|
|
|
|
$haystack = $args[1]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$count = 0;
|
2014-01-18 17:05:03 +00:00
|
|
|
|
|
|
|
// Bug #60203: Keep empty parameters from causing PHP warnings
|
|
|
|
if ( $needle !== '' ) {
|
|
|
|
while ( ( $offset = strpos( $haystack, $needle, $offset + 1 ) ) !== false ) {
|
|
|
|
$count++;
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DINT, $count );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2009-03-07 01:26:42 +00:00
|
|
|
protected function funcRCount( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'rcount', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( count( $args ) == 1 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
$count = count( explode( ',', $args[0]->toString() ) );
|
2009-03-07 01:26:42 +00:00
|
|
|
} else {
|
2011-02-10 17:25:25 +00:00
|
|
|
$needle = $args[0]->toString();
|
2009-03-07 01:26:42 +00:00
|
|
|
$haystack = $args[1]->toString();
|
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
# Munge the regex
|
2009-03-25 12:43:53 +00:00
|
|
|
$needle = preg_replace( '!(\\\\\\\\)*(\\\\)?/!', '$1\/', $needle );
|
2009-03-22 10:31:26 +00:00
|
|
|
$needle = "/$needle/u";
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-07 01:26:42 +00:00
|
|
|
$matches = array();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-10-21 23:20:07 +00:00
|
|
|
$count = preg_match_all( $needle, $haystack, $matches );
|
|
|
|
if ( $count === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'regexfailure',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'unspecified error in preg_match_all()', $needle )
|
|
|
|
);
|
2009-06-18 20:13:52 +00:00
|
|
|
}
|
2009-03-07 01:26:42 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DINT, $count );
|
2009-03-07 01:26:42 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-03-09 12:39:52 +00:00
|
|
|
protected function funcIPInRange( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'ip_in_range', 2, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-09 12:39:52 +00:00
|
|
|
$ip = $args[0]->toString();
|
|
|
|
$range = $args[1]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-09 12:39:52 +00:00
|
|
|
$result = IP::isInRange( $ip, $range );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DBOOL, $result );
|
2009-03-09 12:39:52 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcCCNorm( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'ccnorm', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$s = html_entity_decode( $s, ENT_QUOTES, 'UTF-8' );
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $this->ccnorm( $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $args array
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-03-26 02:03:32 +00:00
|
|
|
protected function funcContainsAny( $args ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'contains_any', 2, count( $args ) )
|
|
|
|
);
|
2009-03-26 02:03:32 +00:00
|
|
|
}
|
2009-04-05 17:11:17 +00:00
|
|
|
|
2009-03-26 02:03:32 +00:00
|
|
|
$s = array_shift( $args );
|
|
|
|
$s = $s->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-26 02:03:32 +00:00
|
|
|
$searchStrings = array();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $args as $arg ) {
|
2009-03-26 02:03:32 +00:00
|
|
|
$searchStrings[] = $arg->toString();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-26 02:03:32 +00:00
|
|
|
if ( function_exists( 'fss_prep_search' ) ) {
|
|
|
|
$fss = fss_prep_search( $searchStrings );
|
|
|
|
$result = fss_exec_search( $fss, $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$ok = is_array( $result );
|
2009-03-26 02:03:32 +00:00
|
|
|
} else {
|
|
|
|
$ok = false;
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $searchStrings as $needle ) {
|
2014-01-18 17:05:03 +00:00
|
|
|
// Bug #60203: Keep empty parameters from causing PHP warnings
|
|
|
|
if ( $needle !== '' && strpos( $s, $needle ) !== false ) {
|
2009-03-26 02:03:32 +00:00
|
|
|
$ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DBOOL, $ok );
|
2009-03-26 02:03:32 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $s
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function ccnorm( $s ) {
|
2009-05-26 13:08:15 +00:00
|
|
|
static $replacementArray = null;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-07-02 01:55:53 +00:00
|
|
|
if ( is_null( $replacementArray ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
// @codingStandardsIgnoreStart Ignore global without wg prefix.
|
2009-04-23 03:37:51 +00:00
|
|
|
global $IP;
|
2015-09-28 18:03:35 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
2013-07-02 01:55:53 +00:00
|
|
|
if ( is_readable( "$IP/extensions/AntiSpoof/equivset.php" ) ) {
|
2013-10-14 19:45:02 +00:00
|
|
|
// Satisfy analyzer.
|
|
|
|
$equivset = null;
|
|
|
|
// Contains a map of characters in $equivset.
|
2013-07-02 01:55:53 +00:00
|
|
|
require "$IP/extensions/AntiSpoof/equivset.php";
|
2014-05-30 05:47:23 +00:00
|
|
|
|
|
|
|
// strtr in ReplacementArray->replace() doesn't like this.
|
|
|
|
if ( isset( $equivset[''] ) ) {
|
|
|
|
unset( $equivset[''] );
|
|
|
|
}
|
|
|
|
|
2013-07-02 01:55:53 +00:00
|
|
|
$replacementArray = new ReplacementArray( $equivset );
|
|
|
|
} else {
|
|
|
|
// AntiSpoof isn't available, so just create a dummy
|
|
|
|
wfDebugLog(
|
|
|
|
'AbuseFilter',
|
2014-05-30 05:47:23 +00:00
|
|
|
"Can't compute normalized string (ccnorm) as the AntiSpoof Extension isn't installed."
|
2013-07-02 01:55:53 +00:00
|
|
|
);
|
|
|
|
$replacementArray = new ReplacementArray( array() );
|
|
|
|
}
|
2009-04-23 03:37:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
return $replacementArray->replace( $s );
|
2010-02-13 14:10:36 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $s string
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function rmspecials( $s ) {
|
2012-03-11 20:40:04 +00:00
|
|
|
return preg_replace( '/[^\p{L}\p{N}]/u', '', $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $s string
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function rmdoubles( $s ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
return preg_replace( '/(.)\1+/us', '\1', $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $s string
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2009-02-18 19:42:01 +00:00
|
|
|
protected function rmwhitespace( $s ) {
|
|
|
|
return preg_replace( '/\s+/u', '', $s );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcRMSpecials( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'rmspecials', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $this->rmspecials( $s );
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-02-18 19:42:01 +00:00
|
|
|
protected function funcRMWhitespace( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'rmwhitespace', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2009-02-18 19:42:01 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-18 19:42:01 +00:00
|
|
|
$s = $this->rmwhitespace( $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2009-02-18 19:42:01 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcRMDoubles( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'rmdoubles', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $this->rmdoubles( $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function funcNorm( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'norm', 1, count( $args ) )
|
|
|
|
);
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $args[0]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$s = $this->ccnorm( $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
$s = $this->rmdoubles( $s );
|
2008-09-22 14:03:45 +00:00
|
|
|
$s = $this->rmspecials( $s );
|
2009-02-18 19:42:01 +00:00
|
|
|
$s = $this->rmwhitespace( $s );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $s );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-01 05:05:23 +00:00
|
|
|
protected function funcSubstr( $args ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'substr', 2, count( $args ) )
|
|
|
|
);
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 05:05:23 +00:00
|
|
|
$s = $args[0]->toString();
|
|
|
|
$offset = $args[1]->toInt();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( isset( $args[2] ) ) {
|
2009-04-01 05:05:23 +00:00
|
|
|
$length = $args[2]->toInt();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-07-31 11:26:30 +00:00
|
|
|
$result = mb_substr( $s, $offset, $length );
|
2009-04-01 05:05:23 +00:00
|
|
|
} else {
|
2009-07-31 11:26:30 +00:00
|
|
|
$result = mb_substr( $s, $offset );
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, $result );
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-01 05:05:23 +00:00
|
|
|
protected function funcStrPos( $args ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'strpos', 2, count( $args ) )
|
|
|
|
);
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 05:05:23 +00:00
|
|
|
$haystack = $args[0]->toString();
|
|
|
|
$needle = $args[1]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2014-01-18 17:05:03 +00:00
|
|
|
// Bug #60203: Keep empty parameters from causing PHP warnings
|
|
|
|
if ( $needle === '' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DINT, -1 );
|
2014-01-18 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( isset( $args[2] ) ) {
|
2009-04-01 05:05:23 +00:00
|
|
|
$offset = $args[2]->toInt();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-07-31 11:26:30 +00:00
|
|
|
$result = mb_strpos( $haystack, $needle, $offset );
|
2009-04-01 05:05:23 +00:00
|
|
|
} else {
|
2009-07-31 11:26:30 +00:00
|
|
|
$result = mb_strpos( $haystack, $needle );
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $result === false )
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = -1;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DINT, $result );
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-01 05:05:23 +00:00
|
|
|
protected function funcStrReplace( $args ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $args ) < 3 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'str_replace', 3, count( $args ) )
|
|
|
|
);
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 05:05:23 +00:00
|
|
|
$subject = $args[0]->toString();
|
|
|
|
$search = $args[1]->toString();
|
|
|
|
$replace = $args[2]->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPData( AFPData::DSTRING, str_replace( $search, $replace, $subject ) );
|
2009-04-01 05:05:23 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2011-10-18 17:57:33 +00:00
|
|
|
protected function funcStrRegexEscape( $args ) {
|
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException( 'notenoughargs', $this->mCur->pos,
|
2015-09-28 18:03:35 +00:00
|
|
|
array( 'rescape', 1, count( $args ) ) );
|
2011-10-18 17:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$string = $args[0]->toString();
|
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
// preg_quote does not need the second parameter, since rlike takes
|
|
|
|
// care of the delimiter symbol itself
|
|
|
|
return new AFPData( AFPData::DSTRING, preg_quote( $string ) );
|
2011-10-18 17:57:33 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return mixed
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2009-04-01 06:53:18 +00:00
|
|
|
protected function funcSetVar( $args ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
array( 'set_var', 2, count( $args ) )
|
|
|
|
);
|
2009-04-01 06:53:18 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 06:53:18 +00:00
|
|
|
$varName = $args[0]->toString();
|
|
|
|
$value = $args[1];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-05 11:47:42 +00:00
|
|
|
$this->setUserVariable( $varName, $value );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 06:53:18 +00:00
|
|
|
return $value;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function castString( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, array( __METHOD__ ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$val = $args[0];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return AFPData::castTypes( $val, AFPData::DSTRING );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function castInt( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, array( __METHOD__ ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$val = $args[0];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return AFPData::castTypes( $val, AFPData::DINT );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function castFloat( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, array( __METHOD__ ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$val = $args[0];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return AFPData::castTypes( $val, AFPData::DFLOAT );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function castBool( $args ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( count( $args ) < 1 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, array( __METHOD__ ) );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$val = $args[0];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
return AFPData::castTypes( $val, AFPData::DBOOL );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|