2008-08-31 05:56:49 +00:00
|
|
|
<?php
|
2012-09-02 11:07:02 +00:00
|
|
|
|
2017-10-11 22:07:48 +00:00
|
|
|
use Wikimedia\Equivset\Equivset;
|
|
|
|
|
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
|
|
|
|
2017-11-07 18:44:10 +00:00
|
|
|
// length,lcase,ucase,ccnorm,rmdoubles,specialratio,rmspecials,norm,count,get_matches
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $mFunctions = [
|
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',
|
2017-09-19 23:54:03 +00:00
|
|
|
'ccnorm_contains_any' => 'funcCCNormContainsAny',
|
2017-11-13 19:13:07 +00:00
|
|
|
'ccnorm_contains_all' => 'funcCCNormContainsAll',
|
2009-03-07 01:26:42 +00:00
|
|
|
'specialratio' => 'funcSpecialRatio',
|
|
|
|
'rmspecials' => 'funcRMSpecials',
|
|
|
|
'rmdoubles' => 'funcRMDoubles',
|
|
|
|
'rmwhitespace' => 'funcRMWhitespace',
|
|
|
|
'count' => 'funcCount',
|
|
|
|
'rcount' => 'funcRCount',
|
2017-11-07 18:44:10 +00:00
|
|
|
'get_matches' => 'funcGetMatches',
|
2009-03-09 12:39:52 +00:00
|
|
|
'ip_in_range' => 'funcIPInRange',
|
2009-03-26 02:03:32 +00:00
|
|
|
'contains_any' => 'funcContainsAny',
|
2017-11-13 19:13:07 +00:00
|
|
|
'contains_all' => 'funcContainsAll',
|
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',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
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.
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $ActiveFunctions = [
|
2009-04-01 06:53:18 +00:00
|
|
|
'funcSetVar',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $mKeywords = [
|
2016-08-24 04:52:58 +00:00
|
|
|
'in' => 'keywordIn',
|
|
|
|
'like' => 'keywordLike',
|
|
|
|
'matches' => 'keywordLike',
|
|
|
|
'contains' => 'keywordContains',
|
|
|
|
'rlike' => 'keywordRegex',
|
|
|
|
'irlike' => 'keywordRegexInsensitive',
|
2017-09-19 23:54:03 +00:00
|
|
|
'regex' => 'keywordRegex',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2016-08-24 04:52:58 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $funcCache = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-10-11 22:07:48 +00:00
|
|
|
/**
|
|
|
|
* @var Equivset
|
|
|
|
*/
|
|
|
|
protected static $equivset;
|
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
/**
|
|
|
|
* Create a new instance
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2013-01-07 00:02:41 +00:00
|
|
|
*/
|
|
|
|
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 = '';
|
2017-06-15 14:23:34 +00:00
|
|
|
$this->mTokens = [];
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $filter
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
return [ $excep->getMessageObj()->text(), $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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param mixed $vars
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 20:17:41 +00:00
|
|
|
if ( !( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == ')' ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'expectednotfound', $this->mCur->pos, [ ')' ] );
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
2009-03-25 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $code
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $filter
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $code
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2017-11-07 18:44:10 +00:00
|
|
|
public 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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData &$result
|
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',
|
2017-06-15 14:23:34 +00:00
|
|
|
$this->mCur->pos, [ $this->mCur->type ]
|
2015-09-28 18:03:35 +00:00
|
|
|
);
|
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
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData &$result
|
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
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData &$result
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ $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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'notlist', $this->mCur->pos, [] );
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ ']', $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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ $idx, count( $result->data ) ] );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData &$result
|
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
|
|
|
|
2016-01-06 20:17:41 +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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
'then',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
|
2016-01-06 20:17:41 +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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
'else',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
|
2016-01-06 20:17:41 +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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
'end',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
|
2016-01-06 20:17:41 +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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
':',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelBoolOps( &$result ) {
|
|
|
|
$this->doLevelCompares( $result );
|
2017-06-15 14:23:34 +00:00
|
|
|
$ops = [ '&', '|', '^' ];
|
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-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-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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-31 05:56:49 +00:00
|
|
|
protected function doLevelCompares( &$result ) {
|
2008-08-31 15:12:55 +00:00
|
|
|
$this->doLevelSumRels( $result );
|
2017-06-15 14:23:34 +00:00
|
|
|
$ops = [ '==', '===', '!=', '!==', '<', '>', '<=', '>=', '=' ];
|
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:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
break; // The result doesn't matter.
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-31 15:12:55 +00:00
|
|
|
protected function doLevelSumRels( &$result ) {
|
|
|
|
$this->doLevelMulRels( $result );
|
2017-06-15 14:23:34 +00:00
|
|
|
$ops = [ '+', '-' ];
|
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 );
|
2016-04-09 13:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
break; // The result doesn't matter.
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-31 15:12:55 +00:00
|
|
|
protected function doLevelMulRels( &$result ) {
|
2008-08-31 05:56:49 +00:00
|
|
|
$this->doLevelPow( $result );
|
2017-06-15 14:23:34 +00:00
|
|
|
$ops = [ '*', '/', '%' ];
|
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 );
|
2016-04-09 13:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
break; // The result doesn't matter.
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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 );
|
2016-04-09 13:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
break; // The result doesn't matter.
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = AFPData::pow( $result, $expanent );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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 );
|
2016-04-09 13:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
return; // The result doesn't matter.
|
|
|
|
}
|
2008-08-31 05:56:49 +00:00
|
|
|
$result = AFPData::boolInvert( $result );
|
|
|
|
} else {
|
|
|
|
$this->doLevelSpecialWords( $result );
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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 );
|
2016-08-24 04:52:58 +00:00
|
|
|
if ( $this->mCur->type == AFPToken::TKEYWORD
|
|
|
|
&& in_array( $keyword, array_keys( self::$mKeywords ) )
|
|
|
|
) {
|
|
|
|
$func = self::$mKeywords[$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();
|
2017-08-07 23:14:31 +00:00
|
|
|
|
2009-04-01 05:56:24 +00:00
|
|
|
$result = AFPData::$func( $result, $r2, $this->mCur->pos );
|
2008-08-31 05:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
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 );
|
2016-04-09 13:53:16 +00:00
|
|
|
if ( $this->mShortCircuit ) {
|
|
|
|
return; // The result doesn't matter.
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ ']', $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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ $idx, count( $result->data ) ] );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
$result = $result->data[$idx];
|
|
|
|
} else {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'notlist', $this->mCur->pos, [] );
|
2009-04-05 17:11:17 +00:00
|
|
|
}
|
|
|
|
$this->move();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
}
|
2016-01-06 20:17:41 +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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ ')', $this->mCur->type, $this->mCur->value ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
'(',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
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
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$args = [];
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
')',
|
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
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
|
|
|
|
|
|
|
$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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
self::$funcCache = [];
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string &$result
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ $tok ]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
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 == '[' ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$list = [];
|
2016-04-09 13:35:35 +00:00
|
|
|
while ( true ) {
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ ', or ]', $this->mCur->type, $this->mCur->value ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->mCur->type,
|
|
|
|
$this->mCur->value
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $var
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ $var ]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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'] ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'overridebuiltin', $this->mCur->pos, [ $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
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'lc', 2, count( $args ) ]
|
2010-08-19 21:12:09 +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::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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2013-04-17 16:36:10 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcUc( $args ) {
|
|
|
|
global $wgContLang;
|
|
|
|
if ( count( $args ) < 1 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'uc', 2, count( $args ) ]
|
2013-04-17 16:36:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'len', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'simplenorm', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'specialratio', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'count', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
// T62203: Keep empty parameters from causing PHP warnings
|
2016-04-17 06:32:27 +00:00
|
|
|
if ( $needle === '' ) {
|
|
|
|
$count = 0;
|
|
|
|
} else {
|
|
|
|
$count = substr_count( $haystack, $needle );
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'rcount', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
2016-07-28 22:35:40 +00:00
|
|
|
// Omit the '$matches' argument to avoid computing them, just count.
|
|
|
|
$count = preg_match_all( $needle, $haystack );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-10-21 23:20:07 +00:00
|
|
|
if ( $count === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'regexfailure',
|
|
|
|
$this->mCur->pos,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'unspecified error in preg_match_all()', $needle ]
|
2015-10-21 23:20:07 +00:00
|
|
|
);
|
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
|
|
|
|
2017-11-07 18:44:10 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of matches of needle in the haystack, the first one for the whole regex,
|
|
|
|
* the other ones for every capturing group.
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return AFPData A list of matches.
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcGetMatches( $args ) {
|
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
[ 'get_matches', 2, count( $args ) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$needle = $args[0]->toString();
|
|
|
|
$haystack = $args[1]->toString();
|
|
|
|
|
|
|
|
// Count the amount of capturing groups in the submitted pattern.
|
|
|
|
// This way we can return a fixed-dimension array, much easier to manage.
|
|
|
|
// First, strip away escaped parentheses
|
|
|
|
$sanitized = preg_replace( '/(\\\\\\\\)*\\\\\(/', '', $needle );
|
|
|
|
// Then strip starting parentheses of non-capturing groups
|
|
|
|
// (also atomics, lookahead and so on, even if not every of them is supported)
|
|
|
|
$sanitized = preg_replace( '/\(\?/', '', $sanitized );
|
|
|
|
// Finally create an array of falses with dimension = # of capturing groups
|
|
|
|
$groupscount = substr_count( $sanitized, '(' ) + 1;
|
|
|
|
$falsy = array_fill( 0, $groupscount, false );
|
|
|
|
|
|
|
|
// Munge the regex by escaping slashes
|
|
|
|
$needle = preg_replace( '!(\\\\\\\\)*(\\\\)?/!', '$1\/', $needle );
|
|
|
|
$needle = "/$needle/u";
|
|
|
|
|
|
|
|
// Suppress and restore are here for the same reason as T177744
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2017-11-07 18:44:10 +00:00
|
|
|
$check = preg_match( $needle, $haystack, $matches );
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2017-11-07 18:44:10 +00:00
|
|
|
|
|
|
|
if ( $check === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'regexfailure',
|
|
|
|
$this->mCur->pos,
|
|
|
|
[ 'unspecified error in preg_match()', $needle ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returned array has non-empty positions identical to the ones returned
|
|
|
|
// by the third parameter of a standard preg_match call ($matches in this case).
|
|
|
|
// We want an union with falsy to return a fixed-dimention array.
|
|
|
|
return AFPData::newFromPHPVar( $matches + $falsy );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'ip_in_range', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'ccnorm', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'contains_any', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
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 );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
return new AFPData( AFPData::DBOOL, self::contains( true, $s, $args ) );
|
2017-09-19 23:54:03 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-09-19 23:54:03 +00:00
|
|
|
/**
|
2017-11-13 19:13:07 +00:00
|
|
|
* @param array $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcContainsAll( $args ) {
|
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
[ 'contains_all', 2, count( $args ) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = array_shift( $args );
|
|
|
|
|
|
|
|
return new AFPData( AFPData::DBOOL, self::contains( false, $s, $args, false ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize and search a string for multiple substrings in OR mode
|
2017-09-19 23:54:03 +00:00
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2017-09-19 23:54:03 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcCCNormContainsAny( $args ) {
|
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
[ 'ccnorm_contains_any', 2, count( $args ) ]
|
|
|
|
);
|
2009-03-26 02:03:32 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-09-19 23:54:03 +00:00
|
|
|
$s = array_shift( $args );
|
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
return new AFPData( AFPData::DBOOL, self::contains( true, $s, $args, true ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize and search a string for multiple substrings in AND mode
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected function funcCCNormContainsAll( $args ) {
|
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'notenoughargs',
|
|
|
|
$this->mCur->pos,
|
|
|
|
[ 'ccnorm_contains_all', 2, count( $args ) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = array_shift( $args );
|
|
|
|
|
|
|
|
return new AFPData( AFPData::DBOOL, self::contains( false, $s, $args, true ) );
|
2017-09-19 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for substrings in a string
|
|
|
|
*
|
2017-11-13 19:13:07 +00:00
|
|
|
* Use is_any to determine wether to use logic OR (true) or AND (false).
|
|
|
|
*
|
2017-09-19 23:54:03 +00:00
|
|
|
* Use normalize = true to make use of ccnorm and
|
|
|
|
* normalize both sides of the search.
|
|
|
|
*
|
2017-11-13 19:13:07 +00:00
|
|
|
* @param bool $is_any
|
2017-09-19 23:54:03 +00:00
|
|
|
* @param AFData $string
|
|
|
|
* @param AFData[] $values
|
|
|
|
* @param bool $normalize
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-11-13 19:13:07 +00:00
|
|
|
protected static function contains( $is_any = true, $string, $values, $normalize = false ) {
|
2017-09-19 23:54:03 +00:00
|
|
|
$string = $string->toString();
|
|
|
|
if ( $string == '' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $normalize ) {
|
|
|
|
$string = self::ccnorm( $string );
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
foreach ( $values as $needle ) {
|
|
|
|
$needle = $needle->toString();
|
2017-09-19 23:54:03 +00:00
|
|
|
if ( $normalize ) {
|
2017-11-13 19:13:07 +00:00
|
|
|
$needle = self::ccnorm( $needle );
|
|
|
|
}
|
|
|
|
if ( $needle === '' ) {
|
|
|
|
// T62203: Keep empty parameters from causing PHP warnings
|
|
|
|
continue;
|
2017-09-19 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
$is_found = strpos( $string, $needle ) !== false;
|
|
|
|
if ( $is_found === $is_any ) {
|
|
|
|
// If I'm here and it's ANY (OR) it means that something is found.
|
|
|
|
// Just enough! Found!
|
|
|
|
// If I'm here and it's ALL (AND) it means that something isn't found.
|
|
|
|
// Just enough! Not found!
|
|
|
|
return $is_found;
|
2009-03-26 02:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
// If I'm here and it's ANY (OR) it means that nothing was found:
|
|
|
|
// return false (because $is_any is true)
|
|
|
|
// If I'm here and it's ALL (AND) it means that everything were found:
|
|
|
|
// return true (because $is_any is false)
|
|
|
|
return ! $is_any;
|
2009-03-26 02:03:32 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $s
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-09-19 23:54:03 +00:00
|
|
|
protected static function ccnorm( $s ) {
|
2017-10-11 22:07:48 +00:00
|
|
|
// Instatiate a single version of the equivset so the data is not loaded
|
|
|
|
// more than once.
|
|
|
|
if ( !self::$equivset ) {
|
|
|
|
self::$equivset = new Equivset();
|
2009-04-23 03:37:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-10-11 22:07:48 +00:00
|
|
|
return self::$equivset->normalize( $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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $s
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $s
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $s
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'rmspecials', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'rmwhitespace', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'rmdoubles', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'norm', 1, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'substr', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'strpos', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
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
|
|
|
|
2017-11-13 19:13:07 +00:00
|
|
|
// T62203: Keep empty parameters from causing PHP warnings
|
2014-01-18 17:05:03 +00:00
|
|
|
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
|
|
|
|
2016-01-06 20:17:41 +00:00
|
|
|
if ( $result === false ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
$result = -1;
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'str_replace', 3, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ '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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'set_var', 2, count( $args ) ]
|
2010-08-19 21:12:09 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, [ __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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, [ __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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, [ __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
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $args
|
2012-03-11 20:40:04 +00:00
|
|
|
* @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 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'noparams', $this->mCur->pos, [ __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
|
|
|
}
|
|
|
|
}
|