2007-11-12 07:42:25 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2015-04-21 12:14:52 +00:00
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
class SimpleCaptcha {
|
2015-04-02 16:27:37 +00:00
|
|
|
|
/** @var boolean|null Was the CAPTCHA already passed and if yes, with which result? */
|
|
|
|
|
private $captchaSolved = null;
|
|
|
|
|
|
2008-02-28 17:42:23 +00:00
|
|
|
|
function getCaptcha() {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
$a = mt_rand( 0, 100 );
|
|
|
|
|
$b = mt_rand( 0, 10 );
|
2010-10-29 19:31:07 +00:00
|
|
|
|
|
|
|
|
|
/* Minus sign is used in the question. UTF-8,
|
|
|
|
|
since the api uses text/plain, not text/html */
|
|
|
|
|
$op = mt_rand( 0, 1 ) ? '+' : '−';
|
2008-02-28 17:42:23 +00:00
|
|
|
|
|
2011-12-08 14:22:41 +00:00
|
|
|
|
// No space before and after $op, to ensure correct
|
|
|
|
|
// directionality.
|
|
|
|
|
$test = "$a$op$b";
|
2009-07-19 15:13:01 +00:00
|
|
|
|
$answer = ( $op == '+' ) ? ( $a + $b ) : ( $a - $b );
|
|
|
|
|
return array( 'question' => $test, 'answer' => $answer );
|
2008-02-28 17:42:23 +00:00
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
|
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
|
$captcha = $this->getCaptcha();
|
|
|
|
|
$index = $this->storeCaptcha( $captcha );
|
|
|
|
|
$resultArr['captcha']['type'] = 'simple';
|
2008-02-29 21:44:21 +00:00
|
|
|
|
$resultArr['captcha']['mime'] = 'text/plain';
|
2008-02-28 17:42:23 +00:00
|
|
|
|
$resultArr['captcha']['id'] = $index;
|
|
|
|
|
$resultArr['captcha']['question'] = $captcha['question'];
|
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Insert a captcha prompt into the edit form.
|
|
|
|
|
* This sample implementation generates a simple arithmetic operation;
|
|
|
|
|
* it would be easy to defeat by machine.
|
|
|
|
|
*
|
|
|
|
|
* Override this!
|
|
|
|
|
*
|
|
|
|
|
* @return string HTML
|
|
|
|
|
*/
|
2015-09-23 06:05:28 +00:00
|
|
|
|
function getForm( OutputPage $out, $tabIndex = 1 ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
|
$captcha = $this->getCaptcha();
|
|
|
|
|
$index = $this->storeCaptcha( $captcha );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2014-04-07 17:25:45 +00:00
|
|
|
|
return "<p><label for=\"wpCaptchaWord\">{$captcha['question']} = </label>" .
|
2008-12-20 09:32:47 +00:00
|
|
|
|
Xml::element( 'input', array(
|
2007-11-12 07:42:25 +00:00
|
|
|
|
'name' => 'wpCaptchaWord',
|
2014-07-30 01:39:22 +00:00
|
|
|
|
'class' => 'mw-ui-input',
|
2007-11-12 07:42:25 +00:00
|
|
|
|
'id' => 'wpCaptchaWord',
|
2013-06-28 18:36:31 +00:00
|
|
|
|
'size' => 5,
|
2013-05-03 01:09:51 +00:00
|
|
|
|
'autocomplete' => 'off',
|
2015-09-23 06:05:28 +00:00
|
|
|
|
'tabindex' => $tabIndex ) ) . // tab in before the edit textarea
|
2007-11-12 07:42:25 +00:00
|
|
|
|
"</p>\n" .
|
2008-12-20 09:32:47 +00:00
|
|
|
|
Xml::element( 'input', array(
|
2007-11-12 07:42:25 +00:00
|
|
|
|
'type' => 'hidden',
|
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
|
'id' => 'wpCaptchaId',
|
|
|
|
|
'value' => $index ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-30 14:33:05 +00:00
|
|
|
|
* Show error message for missing or incorrect captcha on EditPage.
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param EditPage $editPage
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @param OutputPage $out
|
|
|
|
|
*/
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
function showEditFormFields( &$editPage, &$out ) {
|
|
|
|
|
$page = $editPage->getArticle()->getPage();
|
|
|
|
|
if ( !isset( $page->ConfirmEdit_ActivateCaptcha ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-12 17:03:40 +00:00
|
|
|
|
|
|
|
|
|
if ( $this->action !== 'edit' ) {
|
|
|
|
|
unset( $page->ConfirmEdit_ActivateCaptcha );
|
|
|
|
|
$out->addWikiText( $this->getMessage( $this->action ) );
|
2015-07-01 19:16:59 +00:00
|
|
|
|
$out->addHTML( $this->getForm( $out ) );
|
2015-01-12 17:03:40 +00:00
|
|
|
|
}
|
2014-05-30 14:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert the captcha prompt into an edit form.
|
|
|
|
|
* @param EditPage $editPage
|
|
|
|
|
*/
|
2015-02-27 18:29:04 +00:00
|
|
|
|
function editShowCaptcha( $editPage ) {
|
2014-05-30 14:33:05 +00:00
|
|
|
|
$context = $editPage->getArticle()->getContext();
|
|
|
|
|
$page = $editPage->getArticle()->getPage();
|
|
|
|
|
$out = $context->getOutput();
|
|
|
|
|
if ( isset( $page->ConfirmEdit_ActivateCaptcha ) ||
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$this->shouldCheck( $page, '', '', $context )
|
2014-05-30 14:33:05 +00:00
|
|
|
|
) {
|
|
|
|
|
$out->addWikiText( $this->getMessage( $this->action ) );
|
2014-12-14 20:47:55 +00:00
|
|
|
|
$out->addHTML( $this->getForm( $out ) );
|
2014-05-30 14:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
unset( $page->ConfirmEdit_ActivateCaptcha );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show a message asking the user to enter a captcha on edit
|
|
|
|
|
* The result will be treated as wiki text
|
|
|
|
|
*
|
2012-09-02 12:26:25 +00:00
|
|
|
|
* @param $action string Action being performed
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function getMessage( $action ) {
|
|
|
|
|
$name = 'captcha-' . $action;
|
2012-08-16 21:21:50 +00:00
|
|
|
|
$text = wfMessage( $name )->text();
|
2007-11-12 07:42:25 +00:00
|
|
|
|
# Obtain a more tailored message, if possible, otherwise, fall back to
|
|
|
|
|
# the default for edits
|
2012-08-16 21:21:50 +00:00
|
|
|
|
return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'captcha-edit' )->text() : $text;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-10 21:26:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Inject whazawhoo
|
|
|
|
|
* @fixme if multiple thingies insert a header, could break
|
2011-04-24 11:41:49 +00:00
|
|
|
|
* @param $form HTMLForm
|
2010-04-10 21:26:03 +00:00
|
|
|
|
* @return bool true to keep running callbacks
|
|
|
|
|
*/
|
|
|
|
|
function injectEmailUser( &$form ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
global $wgCaptchaTriggers, $wgOut, $wgUser;
|
2010-04-10 21:26:03 +00:00
|
|
|
|
if ( $wgCaptchaTriggers['sendemail'] ) {
|
2015-01-18 13:39:02 +00:00
|
|
|
|
$this->action = 'sendemail';
|
2015-04-02 22:56:48 +00:00
|
|
|
|
if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
|
2010-04-10 21:26:03 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-11-23 19:09:57 +00:00
|
|
|
|
$form->addFooterText(
|
2010-04-10 21:26:03 +00:00
|
|
|
|
"<div class='captcha'>" .
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgOut->parse( $this->getMessage( 'sendemail' ) ) .
|
2014-12-14 20:47:55 +00:00
|
|
|
|
$this->getForm( $wgOut ) .
|
2010-04-10 21:26:03 +00:00
|
|
|
|
"</div>\n" );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Inject whazawhoo
|
|
|
|
|
* @fixme if multiple thingies insert a header, could break
|
2011-04-24 11:41:49 +00:00
|
|
|
|
* @param QuickTemplate $template
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true to keep running callbacks
|
|
|
|
|
*/
|
2009-09-26 00:49:32 +00:00
|
|
|
|
function injectUserCreate( &$template ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
global $wgCaptchaTriggers, $wgOut, $wgUser;
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $wgCaptchaTriggers['createaccount'] ) {
|
2015-01-18 13:39:02 +00:00
|
|
|
|
$this->action = 'usercreate';
|
2015-04-02 22:56:48 +00:00
|
|
|
|
if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
|
2008-08-07 17:05:10 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-04-21 12:14:52 +00:00
|
|
|
|
LoggerFactory::getInstance( 'authmanager' )->info( 'Captcha shown on account creation', array(
|
|
|
|
|
'event' => 'captcha.display',
|
|
|
|
|
'type' => 'accountcreation',
|
|
|
|
|
) );
|
2015-01-14 16:34:23 +00:00
|
|
|
|
$captcha = "<div class='captcha'>" .
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgOut->parse( $this->getMessage( 'createaccount' ) ) .
|
2015-09-23 06:05:28 +00:00
|
|
|
|
// FIXME: Hardcoded tab index
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// Usually, the CAPTCHA is added after the E-Mail address field,
|
|
|
|
|
// which actually has 6 as the tabIndex, but
|
|
|
|
|
// there may are wikis which allows to mention the "real name",
|
|
|
|
|
// which would have 7 as tabIndex, so increase
|
2015-09-23 06:05:28 +00:00
|
|
|
|
// 6 by 2 and use it for the CAPTCHA -> 8 (the submit button has a tabIndex of 10)
|
|
|
|
|
$this->getForm( $wgOut, 8 ) .
|
2015-01-14 16:34:23 +00:00
|
|
|
|
"</div>\n";
|
|
|
|
|
// for older MediaWiki versions
|
|
|
|
|
if ( is_callable( array( $template, 'extend' ) ) ) {
|
|
|
|
|
$template->extend( 'extrafields', $captcha );
|
|
|
|
|
} else {
|
|
|
|
|
$template->set( 'header', $captcha );
|
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inject a captcha into the user login form after a failed
|
|
|
|
|
* password attempt as a speedbump for mass attacks.
|
2009-09-26 00:49:32 +00:00
|
|
|
|
* @fixme if multiple thingies insert a header, could break
|
2011-04-24 11:41:49 +00:00
|
|
|
|
* @param $template QuickTemplate
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true to keep running callbacks
|
|
|
|
|
*/
|
2009-09-26 00:49:32 +00:00
|
|
|
|
function injectUserLogin( &$template ) {
|
2016-02-29 01:26:17 +00:00
|
|
|
|
$perUserTriggered = false;
|
|
|
|
|
$username = $template->get( 'name', '' );
|
|
|
|
|
if ( $username !== '' ) {
|
|
|
|
|
// Note: The first time the user attempts to login, they may
|
|
|
|
|
// get a incorrect password error due to the captcha not being
|
|
|
|
|
// shown since we don't know that they will attempt to login in
|
|
|
|
|
// to a captcha-limitted user account, until they actually try.
|
|
|
|
|
$perUserTriggered = $this->isBadLoginPerUserTriggered( $username );
|
|
|
|
|
}
|
|
|
|
|
$perIPTriggered = $this->isBadLoginTriggered();
|
|
|
|
|
if ( $perIPTriggered || $perUserTriggered ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
global $wgOut;
|
2015-01-18 13:39:02 +00:00
|
|
|
|
|
2015-04-21 12:14:52 +00:00
|
|
|
|
LoggerFactory::getInstance( 'authmanager' )->info( 'Captcha shown on login', array(
|
|
|
|
|
'event' => 'captcha.display',
|
|
|
|
|
'type' => 'login',
|
2016-02-29 01:26:17 +00:00
|
|
|
|
'perIp' => $perIPTriggered,
|
|
|
|
|
'perUser' => $perUserTriggered
|
2015-04-21 12:14:52 +00:00
|
|
|
|
) );
|
2015-01-18 13:39:02 +00:00
|
|
|
|
$this->action = 'badlogin';
|
2015-01-14 16:34:23 +00:00
|
|
|
|
$captcha = "<div class='captcha'>" .
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgOut->parse( $this->getMessage( 'badlogin' ) ) .
|
2014-12-14 20:47:55 +00:00
|
|
|
|
$this->getForm( $wgOut ) .
|
2015-01-14 16:34:23 +00:00
|
|
|
|
"</div>\n";
|
|
|
|
|
// for older MediaWiki versions
|
|
|
|
|
if ( is_callable( array( $template, 'extend' ) ) ) {
|
|
|
|
|
$template->extend( 'extrafields', $captcha );
|
|
|
|
|
} else {
|
|
|
|
|
$template->set( 'header', $captcha );
|
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* When a bad login attempt is made, increment an expiring counter
|
|
|
|
|
* in the memcache cloud. Later checks for this may trigger a
|
|
|
|
|
* captcha display to prevent too many hits from the same place.
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param string $password
|
|
|
|
|
* @param int $retval authentication return value
|
|
|
|
|
* @return bool true to keep running callbacks
|
|
|
|
|
*/
|
|
|
|
|
function triggerUserLogin( $user, $password, $retval ) {
|
2016-02-29 01:26:17 +00:00
|
|
|
|
global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration,
|
|
|
|
|
$wgMemc, $wgCaptchaBadLoginPerUserExpiration;
|
|
|
|
|
if ( $retval === LoginForm::WRONG_PASS ) {
|
|
|
|
|
if ( $wgCaptchaTriggers['badlogin'] ) {
|
|
|
|
|
$key = $this->badLoginKey();
|
|
|
|
|
$count = $wgMemc->get( $key );
|
|
|
|
|
if ( !$count ) {
|
|
|
|
|
$wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgMemc->incr( $key );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
2016-02-29 01:26:17 +00:00
|
|
|
|
if ( $wgCaptchaTriggers['badloginperuser'] ) {
|
|
|
|
|
$key = $this->badLoginPerUserKey( $user->getName() );
|
|
|
|
|
$count = $wgMemc->get( $key );
|
|
|
|
|
if ( !$count ) {
|
|
|
|
|
$wgMemc->add( $key, 0, $wgCaptchaBadLoginPerUserExpiration );
|
|
|
|
|
}
|
2012-08-16 21:21:50 +00:00
|
|
|
|
|
2016-02-29 01:26:17 +00:00
|
|
|
|
$wgMemc->incr( $key );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $retval === LoginForm::SUCCESS && $wgCaptchaTriggers['badloginperuser'] ) {
|
|
|
|
|
// Reset the per-user limit after a succesful login.
|
|
|
|
|
$wgMemc->delete( $this->badLoginPerUserKey( $user->getName() ) );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Check if a bad login has already been registered for this
|
|
|
|
|
* IP address. If so, require a captcha.
|
|
|
|
|
* @return bool
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function isBadLoginTriggered() {
|
2011-12-21 23:39:53 +00:00
|
|
|
|
global $wgMemc, $wgCaptchaTriggers, $wgCaptchaBadLoginAttempts;
|
2015-10-28 15:52:04 +00:00
|
|
|
|
return $wgCaptchaTriggers['badlogin'] && intval(
|
|
|
|
|
$wgMemc->get( $this->badLoginKey() )
|
|
|
|
|
) >= $wgCaptchaBadLoginAttempts;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2016-02-29 01:26:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* Is the per-user captcha triggered?
|
|
|
|
|
*
|
|
|
|
|
* @param $u User|String User object, or name
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private function isBadLoginPerUserTriggered( $u ) {
|
|
|
|
|
global $wgMemc, $wgCaptchaTriggers, $wgCaptchaBadLoginPerUserAttempts;
|
|
|
|
|
|
|
|
|
|
if ( is_object( $u ) ) {
|
|
|
|
|
$u = $u->getName();
|
|
|
|
|
}
|
|
|
|
|
return $wgCaptchaTriggers['badloginperuser'] && intval(
|
|
|
|
|
$wgMemc->get( $this->badLoginPerUserKey( $u ) )
|
|
|
|
|
) >= $wgCaptchaBadLoginPerUserAttempts;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-09 14:00:13 +00:00
|
|
|
|
/**
|
2016-03-07 16:29:53 +00:00
|
|
|
|
* Check if the current IP is allowed to skip captchas. This checks
|
|
|
|
|
* the whitelist from two sources.
|
|
|
|
|
* 1) From the server-side config array $wgCaptchaWhitelistIP
|
|
|
|
|
* 2) From the local [[MediaWiki:Captcha-ip-whitelist]] message
|
|
|
|
|
*
|
|
|
|
|
* @return bool true if whitelisted, false if not
|
2009-05-09 14:00:13 +00:00
|
|
|
|
*/
|
|
|
|
|
function isIPWhitelisted() {
|
2016-03-07 16:29:53 +00:00
|
|
|
|
global $wgCaptchaWhitelistIP, $wgRequest;
|
|
|
|
|
$ip = $wgRequest->getIP();
|
2012-01-12 08:58:40 +00:00
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $wgCaptchaWhitelistIP ) {
|
2016-03-07 16:29:53 +00:00
|
|
|
|
if ( IP::isInRanges( $ip, $wgCaptchaWhitelistIP ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-12 08:58:40 +00:00
|
|
|
|
|
2016-03-07 16:29:53 +00:00
|
|
|
|
$whitelistMsg = wfMessage( 'captcha-ip-whitelist' )->inContentLanguage();
|
|
|
|
|
if ( !$whitelistMsg->isDisabled() ) {
|
|
|
|
|
$whitelistedIPs = $this->getWikiIPWhitelist( $whitelistMsg );
|
|
|
|
|
if ( IP::isInRanges( $ip, $whitelistedIPs ) ) {
|
|
|
|
|
return true;
|
2009-05-09 14:00:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-07 16:29:53 +00:00
|
|
|
|
|
2009-05-09 14:00:13 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2016-03-07 16:29:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Get the on-wiki IP whitelist stored in [[MediaWiki:Captcha-ip-whitelist]]
|
|
|
|
|
* page from cache if possible.
|
|
|
|
|
*
|
|
|
|
|
* @param Message $msg whitelist Message on wiki
|
|
|
|
|
* @return array whitelisted IP addresses or IP ranges, empty array if no whitelist
|
|
|
|
|
*/
|
|
|
|
|
private function getWikiIPWhitelist( Message $msg ) {
|
|
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
|
$cacheKey = $cache->makeKey( 'confirmedit', 'ipwhitelist' );
|
|
|
|
|
|
|
|
|
|
$cachedWhitelist = $cache->get( $cacheKey );
|
|
|
|
|
if ( $cachedWhitelist === false ) {
|
|
|
|
|
// Could not retrieve from cache so build the whitelist directly
|
|
|
|
|
// from the wikipage
|
|
|
|
|
$whitelist = $this->buildValidIPs(
|
|
|
|
|
explode( "\n", $msg->plain() )
|
|
|
|
|
);
|
|
|
|
|
// And then store it in cache for one day. This cache is cleared on
|
|
|
|
|
// modifications to the whitelist page.
|
|
|
|
|
// @see ConfirmEditHooks::onPageContentSaveComplete()
|
|
|
|
|
$cache->set( $cacheKey, $whitelist, 86400 );
|
|
|
|
|
} else {
|
|
|
|
|
// Whitelist from the cache
|
|
|
|
|
$whitelist = $cachedWhitelist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $whitelist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* From a list of unvalidated input, get all the valid
|
|
|
|
|
* IP addresses and IP ranges from it.
|
|
|
|
|
*
|
|
|
|
|
* Note that only lines with just the IP address or IP range is considered
|
|
|
|
|
* as valid. Whitespace is allowed but if there is any other character on
|
|
|
|
|
* the line, it's not considered as a valid entry.
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $input
|
|
|
|
|
* @return string[] of valid IP addresses and IP ranges
|
|
|
|
|
*/
|
|
|
|
|
private function buildValidIPs( array $input ) {
|
|
|
|
|
// Remove whitespace and blank lines first
|
|
|
|
|
$ips = array_map( 'trim', $input );
|
|
|
|
|
$ips = array_filter( $ips );
|
|
|
|
|
|
|
|
|
|
$validIPs = array();
|
|
|
|
|
foreach ( $ips as $ip ) {
|
|
|
|
|
if ( IP::isIPAddress( $ip ) ) {
|
|
|
|
|
$validIPs[] = $ip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $validIPs;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Internal cache key for badlogin checks.
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function badLoginKey() {
|
2011-12-13 21:24:03 +00:00
|
|
|
|
global $wgRequest;
|
2012-09-02 12:26:25 +00:00
|
|
|
|
$ip = $wgRequest->getIP();
|
2016-02-15 02:18:36 +00:00
|
|
|
|
return wfGlobalCacheKey( 'captcha', 'badlogin', 'ip', $ip );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2016-02-29 01:26:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cache key for badloginPerUser checks.
|
|
|
|
|
* @param $username string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function badLoginPerUserKey( $username ) {
|
|
|
|
|
$username = User::getCanonicalName( $username, 'usable' ) ?: $username;
|
|
|
|
|
return wfGlobalCacheKey( 'captcha', 'badlogin', 'user', md5( $username ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Check if the submitted form matches the captcha session data provided
|
|
|
|
|
* by the plugin when the form was generated.
|
|
|
|
|
*
|
|
|
|
|
* Override this!
|
|
|
|
|
*
|
2008-01-24 21:42:21 +00:00
|
|
|
|
* @param string $answer
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @param array $info
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-01-24 21:42:21 +00:00
|
|
|
|
function keyMatch( $answer, $info ) {
|
|
|
|
|
return $answer == $info['answer'];
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param Title $title
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @param string $action (edit/create/addurl...)
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @return bool true if action triggers captcha on $title's namespace
|
2007-11-12 07:42:25 +00:00
|
|
|
|
*/
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
function captchaTriggers( $title, $action ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
|
|
|
|
|
// Special config for this NS?
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( isset( $wgCaptchaTriggersOnNamespace[$title->getNamespace()][$action] ) )
|
|
|
|
|
return $wgCaptchaTriggersOnNamespace[$title->getNamespace()][$action];
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
|
return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param WikiPage $page
|
|
|
|
|
* @param $content Content|string
|
2012-08-16 21:21:50 +00:00
|
|
|
|
* @param $section string
|
2015-08-12 01:25:44 +00:00
|
|
|
|
* @param IContextSource $context
|
2015-07-01 19:16:59 +00:00
|
|
|
|
* @param $oldtext string The content of the revision prior to $content. When
|
2015-03-25 22:17:08 +00:00
|
|
|
|
* null this will be loaded from the database.
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true if the captcha should run
|
|
|
|
|
*/
|
2015-08-12 01:25:44 +00:00
|
|
|
|
function shouldCheck( WikiPage $page, $content, $section, $context, $oldtext = null ) {
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// @codingStandardsIgnoreStart
|
2015-08-12 01:25:44 +00:00
|
|
|
|
global $ceAllowConfirmedEmail;
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// @codingStandardsIgnoreEnd
|
2015-06-22 19:24:21 +00:00
|
|
|
|
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( !$context instanceof IContextSource ) {
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$request = $context->getRequest();
|
|
|
|
|
$user = $context->getUser();
|
|
|
|
|
|
|
|
|
|
// captcha check exceptions, which will return always false
|
|
|
|
|
if ( $user->isAllowed( 'skipcaptcha' ) ) {
|
2015-06-22 19:24:21 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
|
|
|
|
|
return false;
|
|
|
|
|
} elseif ( $this->isIPWhitelisted() ) {
|
2015-08-12 01:25:44 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user IP is whitelisted" );
|
2015-06-22 19:24:21 +00:00
|
|
|
|
return false;
|
2015-08-12 01:25:44 +00:00
|
|
|
|
} elseif ( $ceAllowConfirmedEmail && $user->isEmailConfirmed() ) {
|
2015-06-22 19:24:21 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
$title = $page->getTitle();
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->trigger = '';
|
2014-12-14 21:16:57 +00:00
|
|
|
|
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( $content instanceof Content ) {
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( $content->getModel() == CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
|
$newtext = $content->getNativeData();
|
|
|
|
|
} else {
|
|
|
|
|
$newtext = null;
|
|
|
|
|
}
|
2015-02-27 18:29:04 +00:00
|
|
|
|
$isEmpty = $content->isEmpty();
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
} else {
|
|
|
|
|
$newtext = $content;
|
2015-02-27 18:29:04 +00:00
|
|
|
|
$isEmpty = $content === '';
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( $this->captchaTriggers( $title, 'edit' ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// Check on all edits
|
|
|
|
|
$this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$user->getName(),
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$title->getPrefixedText() );
|
|
|
|
|
$this->action = 'edit';
|
|
|
|
|
wfDebug( "ConfirmEdit: checking all edits...\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
|
if ( $this->captchaTriggers( $title, 'create' ) && !$title->exists() ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
// Check if creating a page
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$user->getName(),
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$title->getPrefixedText() );
|
|
|
|
|
$this->action = 'create';
|
|
|
|
|
wfDebug( "ConfirmEdit: checking on page creation...\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// The following checks are expensive and should be done only,
|
|
|
|
|
// if we can assume, that the edit will be saved
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( !$request->wasPosted() ) {
|
2015-10-28 15:52:04 +00:00
|
|
|
|
wfDebug(
|
|
|
|
|
"ConfirmEdit: request not posted, assuming that no content will be saved -> no CAPTCHA check"
|
|
|
|
|
);
|
2015-08-12 01:25:44 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:29:04 +00:00
|
|
|
|
if ( !$isEmpty && $this->captchaTriggers( $title, 'addurl' ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// Only check edits that add URLs
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( $content instanceof Content ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// Get links from the database
|
|
|
|
|
$oldLinks = $this->getLinksFromTracker( $title );
|
|
|
|
|
// Share a parse operation with Article::doEdit()
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
$editInfo = $page->prepareContentForEdit( $content );
|
|
|
|
|
if ( $editInfo->output ) {
|
|
|
|
|
$newLinks = array_keys( $editInfo->output->getExternalLinks() );
|
|
|
|
|
} else {
|
|
|
|
|
$newLinks = array();
|
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Get link changes in the slowest way known to man
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$oldtext = isset( $oldtext ) ? $oldtext : $this->loadText( $title, $section );
|
2015-04-03 18:14:36 +00:00
|
|
|
|
$oldLinks = $this->findLinks( $title, $oldtext );
|
|
|
|
|
$newLinks = $this->findLinks( $title, $newtext );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
|
|
|
|
|
$addedLinks = array_diff( $unknownLinks, $oldLinks );
|
|
|
|
|
$numLinks = count( $addedLinks );
|
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $numLinks > 0 ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
|
|
|
|
|
$numLinks,
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$user->getName(),
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$title->getPrefixedText(),
|
|
|
|
|
implode( ", ", $addedLinks ) );
|
|
|
|
|
$this->action = 'addurl';
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 22:56:48 +00:00
|
|
|
|
global $wgCaptchaRegexes;
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( $newtext !== null && $wgCaptchaRegexes ) {
|
2015-08-12 01:39:12 +00:00
|
|
|
|
if ( !is_array( $wgCaptchaRegexes ) ) {
|
2015-10-28 15:52:04 +00:00
|
|
|
|
throw new UnexpectedValueException(
|
|
|
|
|
'$wgCaptchaRegexes is required to be an array, ' . gettype( $wgCaptchaRegexes ) . ' given.'
|
|
|
|
|
);
|
2015-08-12 01:39:12 +00:00
|
|
|
|
}
|
2012-09-26 21:15:39 +00:00
|
|
|
|
// Custom regex checks. Reuse $oldtext if set above.
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$oldtext = isset( $oldtext ) ? $oldtext : $this->loadText( $title, $section );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
|
foreach ( $wgCaptchaRegexes as $regex ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$newMatches = array();
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$oldMatches = array();
|
|
|
|
|
preg_match_all( $regex, $oldtext, $oldMatches );
|
|
|
|
|
|
|
|
|
|
$addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
|
|
|
|
|
|
|
|
|
|
$numHits = count( $addedMatches );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $numHits > 0 ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->trigger = sprintf( "%dx %s at [[%s]]: %s",
|
|
|
|
|
$numHits,
|
|
|
|
|
$regex,
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$user->getName(),
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$title->getPrefixedText(),
|
|
|
|
|
implode( ", ", $addedMatches ) );
|
|
|
|
|
$this->action = 'edit';
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter callback function for URL whitelisting
|
2012-09-02 12:26:25 +00:00
|
|
|
|
* @param $url string to check
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true if unknown, false if whitelisted
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function filterLink( $url ) {
|
|
|
|
|
global $wgCaptchaWhitelist;
|
2013-11-23 17:59:53 +00:00
|
|
|
|
static $regexes = null;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2013-11-23 17:59:53 +00:00
|
|
|
|
if ( $regexes === null ) {
|
|
|
|
|
$source = wfMessage( 'captcha-addurl-whitelist' )->inContentLanguage();
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2013-11-23 17:59:53 +00:00
|
|
|
|
$regexes = $source->isDisabled()
|
|
|
|
|
? array()
|
|
|
|
|
: $this->buildRegexes( explode( "\n", $source->plain() ) );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
2013-11-23 17:59:53 +00:00
|
|
|
|
if ( $wgCaptchaWhitelist !== false ) {
|
|
|
|
|
array_unshift( $regexes, $wgCaptchaWhitelist );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $regexes as $regex ) {
|
|
|
|
|
if ( preg_match( $regex, $url ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build regex from whitelist
|
2012-09-02 12:26:25 +00:00
|
|
|
|
* @param $lines string from [[MediaWiki:Captcha-addurl-whitelist]]
|
2013-11-23 17:59:53 +00:00
|
|
|
|
* @return array Regexes
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function buildRegexes( $lines ) {
|
|
|
|
|
# Code duplicated from the SpamBlacklist extension (r19197)
|
2013-11-23 17:59:53 +00:00
|
|
|
|
# and later modified.
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
|
|
# Strip comments and whitespace, then remove blanks
|
|
|
|
|
$lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
|
|
|
|
|
|
|
|
|
|
# No lines, don't make a regex which will match everything
|
|
|
|
|
if ( count( $lines ) == 0 ) {
|
|
|
|
|
wfDebug( "No lines\n" );
|
2013-11-23 17:59:53 +00:00
|
|
|
|
return array();
|
2007-11-12 07:42:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
# Make regex
|
|
|
|
|
# It's faster using the S modifier even though it will usually only be run once
|
2009-07-19 15:13:01 +00:00
|
|
|
|
// $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
|
|
|
|
|
// return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
|
2013-11-23 17:59:53 +00:00
|
|
|
|
$regexes = array();
|
|
|
|
|
$regexStart = array(
|
2014-10-22 14:10:37 +00:00
|
|
|
|
'normal' => '/^(?:https?:)?\/\/+[a-z0-9_\-.]*(?:',
|
2013-11-23 17:59:53 +00:00
|
|
|
|
'noprotocol' => '/^(?:',
|
|
|
|
|
);
|
|
|
|
|
$regexEnd = array(
|
|
|
|
|
'normal' => ')/Si',
|
|
|
|
|
'noprotocol' => ')/Si',
|
|
|
|
|
);
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$regexMax = 4096;
|
2013-11-23 17:59:53 +00:00
|
|
|
|
$build = array();
|
2009-07-19 15:13:01 +00:00
|
|
|
|
foreach ( $lines as $line ) {
|
2013-11-23 17:59:53 +00:00
|
|
|
|
# Extract flags from the line
|
|
|
|
|
$options = array();
|
|
|
|
|
if ( preg_match( '/^(.*?)\s*<([^<>]*)>$/', $line, $matches ) ) {
|
|
|
|
|
if ( $matches[1] === '' ) {
|
|
|
|
|
wfDebug( "Line with empty regex\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$line = $matches[1];
|
|
|
|
|
$opts = preg_split( '/\s*\|\s*/', trim( $matches[2] ) );
|
|
|
|
|
foreach ( $opts as $opt ) {
|
|
|
|
|
$opt = strtolower( $opt );
|
|
|
|
|
if ( $opt == 'noprotocol' ) {
|
|
|
|
|
$options['noprotocol'] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$key = isset( $options['noprotocol'] ) ? 'noprotocol' : 'normal';
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// FIXME: not very robust size check, but should work. :)
|
2013-11-23 17:59:53 +00:00
|
|
|
|
if ( !isset( $build[$key] ) ) {
|
|
|
|
|
$build[$key] = $line;
|
|
|
|
|
} elseif ( strlen( $build[$key] ) + strlen( $line ) > $regexMax ) {
|
|
|
|
|
$regexes[] = $regexStart[$key] .
|
|
|
|
|
str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build[$key] ) ) .
|
|
|
|
|
$regexEnd[$key];
|
|
|
|
|
$build[$key] = $line;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
} else {
|
2013-11-23 17:59:53 +00:00
|
|
|
|
$build[$key] .= '|' . $line;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 17:59:53 +00:00
|
|
|
|
foreach ( $build as $key => $value ) {
|
|
|
|
|
$regexes[] = $regexStart[$key] .
|
|
|
|
|
str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build[$key] ) ) .
|
|
|
|
|
$regexEnd[$key];
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
return $regexes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load external links from the externallinks table
|
2011-04-24 11:41:49 +00:00
|
|
|
|
* @param $title Title
|
|
|
|
|
* @return Array
|
2007-11-12 07:42:25 +00:00
|
|
|
|
*/
|
|
|
|
|
function getLinksFromTracker( $title ) {
|
2010-02-13 23:03:40 +00:00
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2012-03-11 19:04:37 +00:00
|
|
|
|
$id = $title->getArticleID(); // should be zero queries
|
2009-07-19 15:13:01 +00:00
|
|
|
|
$res = $dbr->select( 'externallinks', array( 'el_to' ),
|
2007-11-12 07:42:25 +00:00
|
|
|
|
array( 'el_from' => $id ), __METHOD__ );
|
|
|
|
|
$links = array();
|
2010-10-29 21:30:20 +00:00
|
|
|
|
foreach ( $res as $row ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$links[] = $row->el_to;
|
|
|
|
|
}
|
|
|
|
|
return $links;
|
2008-02-28 17:42:23 +00:00
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
2008-02-28 17:42:23 +00:00
|
|
|
|
* Backend function for confirmEdit() and confirmEditAPI()
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param WikiPage $page
|
2012-08-16 21:21:50 +00:00
|
|
|
|
* @param $newtext string
|
|
|
|
|
* @param $section
|
2015-08-12 01:25:44 +00:00
|
|
|
|
* @param IContextSource $context
|
2008-02-28 17:42:23 +00:00
|
|
|
|
* @return bool false if the CAPTCHA is rejected, true otherwise
|
2007-11-12 07:42:25 +00:00
|
|
|
|
*/
|
2015-08-12 01:25:44 +00:00
|
|
|
|
private function doConfirmEdit( WikiPage $page, $newtext, $section, IContextSource $context ) {
|
2015-11-07 00:25:25 +00:00
|
|
|
|
global $wgRequest;
|
2015-08-12 01:25:44 +00:00
|
|
|
|
$request = $context->getRequest();
|
2015-11-07 00:25:25 +00:00
|
|
|
|
// FIXME: Stop using wgRequest in other parts of ConfirmEdit so we can
|
|
|
|
|
// stop having to duplicate code for it.
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( $request->getVal( 'captchaid' ) ) {
|
|
|
|
|
$request->setVal( 'wpCaptchaId', $request->getVal( 'captchaid' ) );
|
2015-11-07 00:25:25 +00:00
|
|
|
|
$wgRequest->setVal( 'wpCaptchaId', $request->getVal( 'captchaid' ) );
|
2011-11-23 20:37:13 +00:00
|
|
|
|
}
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( $request->getVal( 'captchaword' ) ) {
|
|
|
|
|
$request->setVal( 'wpCaptchaWord', $request->getVal( 'captchaword' ) );
|
2015-11-07 00:25:25 +00:00
|
|
|
|
$wgRequest->setVal( 'wpCaptchaWord', $request->getVal( 'captchaword' ) );
|
2011-11-23 20:37:13 +00:00
|
|
|
|
}
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( $this->shouldCheck( $page, $newtext, $section, $context ) ) {
|
2015-01-02 22:56:02 +00:00
|
|
|
|
return $this->passCaptchaLimited();
|
2007-11-12 07:42:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "ConfirmEdit: no need to show captcha.\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-28 17:42:23 +00:00
|
|
|
|
/**
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* An efficient edit filter callback based on the text after section merging
|
|
|
|
|
* @param RequestContext $context
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @param Status $status
|
|
|
|
|
* @param $summary
|
|
|
|
|
* @param $user
|
|
|
|
|
* @param $minorEdit
|
|
|
|
|
* @return bool
|
2008-02-28 17:42:23 +00:00
|
|
|
|
*/
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
function confirmEditMerged( $context, $content, $status, $summary, $user, $minorEdit ) {
|
|
|
|
|
$legacyMode = !defined( 'MW_EDITFILTERMERGED_SUPPORTS_API' );
|
|
|
|
|
if ( defined( 'MW_API' ) && $legacyMode ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
|
# API mode
|
2009-07-19 15:13:01 +00:00
|
|
|
|
# The CAPTCHA was already checked and approved
|
2008-02-28 17:42:23 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-05-07 21:24:02 +00:00
|
|
|
|
if ( !$context->canUseWikiPage() ) {
|
|
|
|
|
// we check WikiPage only
|
|
|
|
|
// try to get an appropriate title for this page
|
|
|
|
|
$title = $context->getTitle();
|
|
|
|
|
if ( $title instanceof Title ) {
|
|
|
|
|
$title = $title->getFullText();
|
|
|
|
|
} else {
|
|
|
|
|
// otherwise it's an unknown page where this function is called from
|
|
|
|
|
$title = 'unknown';
|
|
|
|
|
}
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// log this error, it could be a problem in another extension,
|
|
|
|
|
// edits should always have a WikiPage if
|
2015-05-07 21:24:02 +00:00
|
|
|
|
// they go through EditFilterMergedContent.
|
|
|
|
|
wfDebug( __METHOD__ . ': Skipped ConfirmEdit check: No WikiPage for title ' . $title );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
$page = $context->getWikiPage();
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( !$this->doConfirmEdit( $page, $content, false, $context ) ) {
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( $legacyMode ) {
|
|
|
|
|
$status->fatal( 'hookaborted' );
|
|
|
|
|
}
|
|
|
|
|
$status->value = EditPage::AS_HOOK_ERROR_EXPECTED;
|
|
|
|
|
$status->apiHookResult = array();
|
2015-01-12 17:42:39 +00:00
|
|
|
|
// give an error message for the user to know, what goes wrong here.
|
|
|
|
|
// this can't be done for addurl trigger, because this requires one "free" save
|
|
|
|
|
// for the user, which we don't know, when he did it.
|
|
|
|
|
if ( $this->action === 'edit' ) {
|
|
|
|
|
$status->fatal(
|
|
|
|
|
new RawMessage(
|
|
|
|
|
Html::element(
|
|
|
|
|
'div',
|
|
|
|
|
array( 'class' => 'errorbox' ),
|
|
|
|
|
$context->msg( 'captcha-edit-fail' )->text()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
$this->addCaptchaAPI( $status->apiHookResult );
|
|
|
|
|
$page->ConfirmEdit_ActivateCaptcha = true;
|
|
|
|
|
return $legacyMode;
|
2008-02-28 17:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
function confirmEditAPI( $editPage, $newText, &$resultArr ) {
|
|
|
|
|
$page = $editPage->getArticle()->getPage();
|
2015-08-12 01:25:44 +00:00
|
|
|
|
if ( !$this->doConfirmEdit( $page, $newText, false, $editPage->getArticle()->getContext() ) ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
$this->addCaptchaAPI( $resultArr );
|
2008-02-28 17:42:23 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-11-23 19:09:57 +00:00
|
|
|
|
|
2008-02-28 17:42:23 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hook for user creation form submissions.
|
|
|
|
|
* @param User $u
|
|
|
|
|
* @param string $message
|
2014-01-17 20:56:56 +00:00
|
|
|
|
* @param Status $status
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true to continue, false to abort user creation
|
|
|
|
|
*/
|
2014-01-17 20:56:56 +00:00
|
|
|
|
function confirmUserCreate( $u, &$message, &$status = null ) {
|
2014-01-10 23:16:34 +00:00
|
|
|
|
if ( $this->needCreateAccountCaptcha() ) {
|
|
|
|
|
$this->trigger = "new account '" . $u->getName() . "'";
|
2015-04-21 12:14:52 +00:00
|
|
|
|
$success = $this->passCaptchaLimited();
|
2015-10-28 15:52:04 +00:00
|
|
|
|
LoggerFactory::getInstance(
|
|
|
|
|
'authmanager'
|
|
|
|
|
)->info( 'Captcha submitted on account creation', array(
|
2015-04-21 12:14:52 +00:00
|
|
|
|
'event' => 'captcha.submit',
|
|
|
|
|
'type' => 'accountcreation',
|
|
|
|
|
'successful' => $success,
|
|
|
|
|
) );
|
|
|
|
|
if ( !$success ) {
|
2014-01-17 20:56:56 +00:00
|
|
|
|
// For older MediaWiki
|
2014-01-10 23:16:34 +00:00
|
|
|
|
$message = wfMessage( 'captcha-createaccount-fail' )->text();
|
2014-01-17 20:56:56 +00:00
|
|
|
|
// For MediaWiki 1.23+
|
Cleaner response for captcha-related createaccount API failure
Instead of a generic API error, you get back a non-Success-resulted
creataccount response, with result='NeedCaptcha'. There's also a warning
included with the message key, and of course the captcha key:
array (
'createaccount' =>
array (
'result' => 'NeedCaptcha',
'warnings' =>
array (
0 =>
array (
'type' => 'warning',
'message' => 'captcha-createaccount-fail',
'params' =>
array (
),
),
),
'captcha' =>
array (
'type' => 'simple',
'mime' => 'text/plain',
'id' => '91510936',
'question' => '76−3',
),
),
)
Change-Id: Id1c9e387c592e6d51a5bd58d99ce3d644dfa300b
2014-02-28 22:42:41 +00:00
|
|
|
|
$status = Status::newGood();
|
2014-05-30 14:33:05 +00:00
|
|
|
|
|
Cleaner response for captcha-related createaccount API failure
Instead of a generic API error, you get back a non-Success-resulted
creataccount response, with result='NeedCaptcha'. There's also a warning
included with the message key, and of course the captcha key:
array (
'createaccount' =>
array (
'result' => 'NeedCaptcha',
'warnings' =>
array (
0 =>
array (
'type' => 'warning',
'message' => 'captcha-createaccount-fail',
'params' =>
array (
),
),
),
'captcha' =>
array (
'type' => 'simple',
'mime' => 'text/plain',
'id' => '91510936',
'question' => '76−3',
),
),
)
Change-Id: Id1c9e387c592e6d51a5bd58d99ce3d644dfa300b
2014-02-28 22:42:41 +00:00
|
|
|
|
// Apply a *non*-fatal warning. This will still abort the
|
|
|
|
|
// account creation but returns a "Warning" response to the
|
|
|
|
|
// API or UI.
|
|
|
|
|
$status->warning( 'captcha-createaccount-fail' );
|
2014-01-10 23:16:34 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-05-30 14:33:05 +00:00
|
|
|
|
|
2014-01-10 23:16:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* Logic to check if we need to pass a captcha for the current user
|
|
|
|
|
* to create a new account, or not
|
|
|
|
|
*
|
|
|
|
|
* @return bool true to show captcha, false to skip captcha
|
|
|
|
|
*/
|
2015-04-02 22:56:48 +00:00
|
|
|
|
function needCreateAccountCaptcha() {
|
|
|
|
|
global $wgCaptchaTriggers, $wgUser;
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $wgCaptchaTriggers['createaccount'] ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
|
2008-08-07 17:05:10 +00:00
|
|
|
|
wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
|
2014-01-10 23:16:34 +00:00
|
|
|
|
return false;
|
2008-08-07 17:05:10 +00:00
|
|
|
|
}
|
2014-01-10 23:16:34 +00:00
|
|
|
|
if ( $this->isIPWhitelisted() ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-01-10 23:16:34 +00:00
|
|
|
|
return true;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
2014-01-10 23:16:34 +00:00
|
|
|
|
return false;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Hook for user login form submissions.
|
2012-08-16 21:21:50 +00:00
|
|
|
|
* @param $u User
|
|
|
|
|
* @param $pass
|
|
|
|
|
* @param $retval
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return bool true to continue, false to abort user creation
|
|
|
|
|
*/
|
|
|
|
|
function confirmUserLogin( $u, $pass, &$retval ) {
|
2016-02-29 01:26:17 +00:00
|
|
|
|
if ( $this->isBadLoginTriggered() || $this->isBadLoginPerUserTriggered( $u ) ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $this->isIPWhitelisted() )
|
2009-05-09 14:00:13 +00:00
|
|
|
|
return true;
|
2009-07-19 15:13:01 +00:00
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->trigger = "post-badlogin login '" . $u->getName() . "'";
|
2015-04-21 12:14:52 +00:00
|
|
|
|
$success = $this->passCaptchaLimited();
|
|
|
|
|
LoggerFactory::getInstance( 'authmanager' )->info( 'Captcha submitted on login', array(
|
|
|
|
|
'event' => 'captcha.submit',
|
|
|
|
|
'type' => 'login',
|
|
|
|
|
'successful' => $success,
|
|
|
|
|
) );
|
|
|
|
|
if ( !$success ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// Emulate a bad-password return to confuse the shit out of attackers
|
2009-09-26 00:49:32 +00:00
|
|
|
|
$retval = LoginForm::WRONG_PASS;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-10 21:26:03 +00:00
|
|
|
|
/**
|
2011-11-23 19:09:57 +00:00
|
|
|
|
* Check the captcha on Special:EmailUser
|
2010-04-10 21:26:03 +00:00
|
|
|
|
* @param $from MailAddress
|
|
|
|
|
* @param $to MailAddress
|
|
|
|
|
* @param $subject String
|
|
|
|
|
* @param $text String
|
|
|
|
|
* @param $error String reference
|
|
|
|
|
* @return Bool true to continue saving, false to abort and show a captcha form
|
|
|
|
|
*/
|
|
|
|
|
function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
|
|
|
|
|
global $wgCaptchaTriggers, $wgUser;
|
|
|
|
|
if ( $wgCaptchaTriggers['sendemail'] ) {
|
|
|
|
|
if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
|
|
|
|
|
wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->isIPWhitelisted() )
|
|
|
|
|
return true;
|
2011-11-23 19:09:57 +00:00
|
|
|
|
|
2010-04-10 21:26:03 +00:00
|
|
|
|
if ( defined( 'MW_API' ) ) {
|
|
|
|
|
# API mode
|
|
|
|
|
# Asking for captchas in the API is really silly
|
2012-08-16 21:21:50 +00:00
|
|
|
|
$error = wfMessage( 'captcha-disabledinapi' )->text();
|
2010-04-10 21:26:03 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->trigger = "{$wgUser->getName()} sending email";
|
2015-01-02 22:56:02 +00:00
|
|
|
|
if ( !$this->passCaptchaLimited() ) {
|
2012-08-16 21:21:50 +00:00
|
|
|
|
$error = wfMessage( 'captcha-sendemail-fail' )->text();
|
2010-04-10 21:26:03 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-16 15:42:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param $module ApiBase
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function isAPICaptchaModule( $module ) {
|
2014-01-10 23:16:34 +00:00
|
|
|
|
return $module instanceof ApiEditPage || $module instanceof ApiCreateAccount;
|
2013-03-16 15:42:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 19:09:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param $module ApiBase
|
|
|
|
|
* @param $params array
|
2013-03-16 15:42:51 +00:00
|
|
|
|
* @param $flags int
|
2011-11-23 19:09:57 +00:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-03-16 15:42:51 +00:00
|
|
|
|
public function APIGetAllowedParams( &$module, &$params, $flags ) {
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
if ( $this->isAPICaptchaModule( $module ) ) {
|
2015-12-18 16:44:37 +00:00
|
|
|
|
if ( defined( 'ApiBase::PARAM_HELP_MSG' ) ) {
|
|
|
|
|
$params['captchaword'] = array(
|
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'captcha-apihelp-param-captchaword',
|
|
|
|
|
);
|
|
|
|
|
$params['captchaid'] = array(
|
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'captcha-apihelp-param-captchaid',
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// @todo: Remove this branch when support for MediaWiki < 1.25 is dropped
|
|
|
|
|
$params['captchaword'] = null;
|
|
|
|
|
$params['captchaid'] = null;
|
|
|
|
|
}
|
2011-11-23 19:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 16:44:37 +00:00
|
|
|
|
* @deprecated Since MediaWiki 1.25
|
2012-09-02 12:26:25 +00:00
|
|
|
|
* @param $module ApiBase
|
2011-11-23 19:09:57 +00:00
|
|
|
|
* @param $desc array
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function APIGetParamDescription( &$module, &$desc ) {
|
2013-03-16 15:42:51 +00:00
|
|
|
|
if ( $this->isAPICaptchaModule( $module ) ) {
|
|
|
|
|
$desc['captchaid'] = 'CAPTCHA ID from previous request';
|
|
|
|
|
$desc['captchaword'] = 'Answer to the CAPTCHA';
|
2011-11-23 19:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 22:56:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Checks, if the user reached the amount of false CAPTCHAs and give him some vacation
|
|
|
|
|
* or run self::passCaptcha() and clear counter if correct.
|
|
|
|
|
*
|
|
|
|
|
* @see self::passCaptcha()
|
|
|
|
|
*/
|
2015-04-27 06:28:40 +00:00
|
|
|
|
public function passCaptchaLimited() {
|
2015-01-02 22:56:02 +00:00
|
|
|
|
global $wgUser;
|
|
|
|
|
|
|
|
|
|
// don't increase pingLimiter here, just check, if CAPTCHA limit exceeded
|
|
|
|
|
if ( $wgUser->pingLimiter( 'badcaptcha', 0 ) ) {
|
|
|
|
|
// for debugging add an proper error message, the user just see an false captcha error message
|
2015-07-27 20:16:18 +00:00
|
|
|
|
$this->log( 'User reached RateLimit, preventing action.' );
|
2015-01-02 22:56:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->passCaptcha() ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// captcha was not solved: increase limit and return false
|
|
|
|
|
$wgUser->pingLimiter( 'badcaptcha' );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Given a required captcha run, test form input for correct
|
|
|
|
|
* input on the open session.
|
|
|
|
|
* @return bool if passed, false if failed or new session
|
|
|
|
|
*/
|
|
|
|
|
function passCaptcha() {
|
2014-12-14 21:16:57 +00:00
|
|
|
|
global $wgRequest;
|
|
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
|
// Don't check the same CAPTCHA twice in one session,
|
|
|
|
|
// if the CAPTCHA was already checked - Bug T94276
|
2015-04-02 16:27:37 +00:00
|
|
|
|
if ( isset( $this->captchaSolved ) ) {
|
|
|
|
|
return $this->captchaSolved;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-14 21:16:57 +00:00
|
|
|
|
$info = $this->retrieveCaptcha( $wgRequest );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $info ) {
|
|
|
|
|
if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
$this->log( "passed" );
|
|
|
|
|
$this->clearCaptcha( $info );
|
2015-04-02 16:27:37 +00:00
|
|
|
|
$this->captchaSolved = true;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
$this->clearCaptcha( $info );
|
|
|
|
|
$this->log( "bad form input" );
|
2015-04-02 16:27:37 +00:00
|
|
|
|
$this->captchaSolved = false;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->log( "new captcha session" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log the status and any triggering info for debugging or statistics
|
|
|
|
|
* @param string $message
|
|
|
|
|
*/
|
|
|
|
|
function log( $message ) {
|
|
|
|
|
wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a captcha session ID and save the info in PHP's session storage.
|
|
|
|
|
* (Requires the user to have cookies enabled to get through the captcha.)
|
|
|
|
|
*
|
|
|
|
|
* A random ID is used so legit users can make edits in multiple tabs or
|
|
|
|
|
* windows without being unnecessarily hobbled by a serial order requirement.
|
|
|
|
|
* Pass the returned id value into the edit form as wpCaptchaId.
|
|
|
|
|
*
|
|
|
|
|
* @param array $info data to store
|
|
|
|
|
* @return string captcha ID key
|
|
|
|
|
*/
|
|
|
|
|
function storeCaptcha( $info ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( !isset( $info['index'] ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
// Assign random index if we're not udpating
|
|
|
|
|
$info['index'] = strval( mt_rand() );
|
|
|
|
|
}
|
2011-04-24 17:33:41 +00:00
|
|
|
|
CaptchaStore::get()->store( $info['index'], $info );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return $info['index'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch this session's captcha info.
|
|
|
|
|
* @return mixed array of info, or false if missing
|
|
|
|
|
*/
|
2015-04-02 22:56:48 +00:00
|
|
|
|
function retrieveCaptcha() {
|
|
|
|
|
global $wgRequest;
|
|
|
|
|
$index = $wgRequest->getVal( 'wpCaptchaId' );
|
2011-04-24 17:33:41 +00:00
|
|
|
|
return CaptchaStore::get()->retrieve( $index );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear out existing captcha info from the session, to ensure
|
|
|
|
|
* it can't be reused.
|
|
|
|
|
*/
|
|
|
|
|
function clearCaptcha( $info ) {
|
2011-04-24 17:33:41 +00:00
|
|
|
|
CaptchaStore::get()->clear( $info['index'] );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the current version of the page or section being edited...
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param Title $title
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @param string $section
|
2015-03-25 20:10:02 +00:00
|
|
|
|
* @param integer $flags Flags for Revision loading methods
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2015-03-25 20:10:02 +00:00
|
|
|
|
function loadText( $title, $section, $flags = Revision::READ_LATEST ) {
|
|
|
|
|
$rev = Revision::newFromTitle( $title, false, $flags );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( is_null( $rev ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
|
return "";
|
|
|
|
|
} else {
|
|
|
|
|
$text = $rev->getText();
|
2009-07-19 15:13:01 +00:00
|
|
|
|
if ( $section != '' ) {
|
2010-07-27 09:00:27 +00:00
|
|
|
|
global $wgParser;
|
|
|
|
|
return $wgParser->getSection( $text, $section );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract a list of all recognized HTTP links in the text.
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
* @param $title Title
|
2012-08-16 21:21:50 +00:00
|
|
|
|
* @param $text string
|
2007-11-12 07:42:25 +00:00
|
|
|
|
* @return array of strings
|
|
|
|
|
*/
|
2015-04-02 22:56:48 +00:00
|
|
|
|
function findLinks( $title, $text ) {
|
|
|
|
|
global $wgParser, $wgUser;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
|
|
$options = new ParserOptions();
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options );
|
Use the shared parse on API edit
ConfirmEdit was tripling the API save time, because it was parsing the
entire content twice to evaluate whether the addurl trigger is hit.
While I was here, I stopped using the deprecated non-Content hooks. The
new hook, EditEditFilterMergedContent, does not pass an EditPage object,
which means that Title or WikiPage objects need to be passed around
instead. Also, since EditPage::showEditForm() cannot be called with no
EditPage object, use a EditPage::showEditForm:fields hook instead.
If non-wikitext content is edited, assume that the regex trigger is not
hit.
For further architectural details, see the associated core change:
I4b4270dd868a . MW_EDITFILTERMERGED_SUPPORTS_API is a constant
introduced to detect the presence of the associated core change.
Also, in APIGetAllowedParams, set the allowed parameters even if we are
not on the help screen. This allows API users to submit their CAPTCHA
answer without it failing with an "unrecognized parameter" error.
Compatibility with MediaWiki 1.21 is retained, compatibility before that
is dropped.
Change-Id: I9529b7e8d3fc9301c754b28fda185aa3ab36f13e
2014-12-05 04:48:13 +00:00
|
|
|
|
$out = $wgParser->parse( $text, $title, $options );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
|
|
return array_keys( $out->getExternalLinks() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show a page explaining what this wacky thing is.
|
|
|
|
|
*/
|
2015-04-02 22:56:48 +00:00
|
|
|
|
function showHelp() {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
|
|
|
|
|
$wgOut->addWikiMsg( 'captchahelp-text' );
|
2011-04-24 17:33:41 +00:00
|
|
|
|
if ( CaptchaStore::get()->cookiesNeeded() ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-10 23:16:34 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pass API captcha parameters on to the login form when using
|
|
|
|
|
* API account creation.
|
|
|
|
|
*
|
|
|
|
|
* @param ApiCreateAccount $apiModule
|
|
|
|
|
* @param LoginForm $loginForm
|
|
|
|
|
* @return hook return value
|
|
|
|
|
*/
|
|
|
|
|
function addNewAccountApiForm( $apiModule, $loginForm ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
global $wgRequest;
|
2014-01-10 23:16:34 +00:00
|
|
|
|
$main = $apiModule->getMain();
|
|
|
|
|
|
|
|
|
|
$id = $main->getVal( 'captchaid' );
|
|
|
|
|
if ( $id ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgRequest->setVal( 'wpCaptchaId', $id );
|
2014-01-10 23:16:34 +00:00
|
|
|
|
|
|
|
|
|
// Suppress "unrecognized parameter" warning:
|
|
|
|
|
$main->getVal( 'wpCaptchaId' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$word = $main->getVal( 'captchaword' );
|
|
|
|
|
if ( $word ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
$wgRequest->setVal( 'wpCaptchaWord', $word );
|
2014-01-10 23:16:34 +00:00
|
|
|
|
|
|
|
|
|
// Suppress "unrecognized parameter" warning:
|
|
|
|
|
$main->getVal( 'wpCaptchaWord' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-03-12 22:37:17 +00:00
|
|
|
|
|
2014-01-10 23:16:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* Pass extra data back in API results for account creation.
|
|
|
|
|
*
|
|
|
|
|
* @param ApiCreateAccount $apiModule
|
2014-03-12 22:37:17 +00:00
|
|
|
|
* @param LoginForm &loginPage
|
|
|
|
|
* @param array &$result
|
|
|
|
|
* @return bool: Hook return value
|
2014-01-10 23:16:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function addNewAccountApiResult( $apiModule, $loginPage, &$result ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
|
if ( $result['result'] !== 'Success' && $this->needCreateAccountCaptcha() ) {
|
Cleaner response for captcha-related createaccount API failure
Instead of a generic API error, you get back a non-Success-resulted
creataccount response, with result='NeedCaptcha'. There's also a warning
included with the message key, and of course the captcha key:
array (
'createaccount' =>
array (
'result' => 'NeedCaptcha',
'warnings' =>
array (
0 =>
array (
'type' => 'warning',
'message' => 'captcha-createaccount-fail',
'params' =>
array (
),
),
),
'captcha' =>
array (
'type' => 'simple',
'mime' => 'text/plain',
'id' => '91510936',
'question' => '76−3',
),
),
)
Change-Id: Id1c9e387c592e6d51a5bd58d99ce3d644dfa300b
2014-02-28 22:42:41 +00:00
|
|
|
|
|
|
|
|
|
// If we failed a captcha, override the generic 'Warning' result string
|
|
|
|
|
if ( $result['result'] === 'Warning' && isset( $result['warnings'] ) ) {
|
|
|
|
|
foreach ( $result['warnings'] as $warning ) {
|
|
|
|
|
if ( $warning['message'] === 'captcha-createaccount-fail' ) {
|
2014-03-12 22:37:17 +00:00
|
|
|
|
$this->addCaptchaAPI( $result );
|
Cleaner response for captcha-related createaccount API failure
Instead of a generic API error, you get back a non-Success-resulted
creataccount response, with result='NeedCaptcha'. There's also a warning
included with the message key, and of course the captcha key:
array (
'createaccount' =>
array (
'result' => 'NeedCaptcha',
'warnings' =>
array (
0 =>
array (
'type' => 'warning',
'message' => 'captcha-createaccount-fail',
'params' =>
array (
),
),
),
'captcha' =>
array (
'type' => 'simple',
'mime' => 'text/plain',
'id' => '91510936',
'question' => '76−3',
),
),
)
Change-Id: Id1c9e387c592e6d51a5bd58d99ce3d644dfa300b
2014-02-28 22:42:41 +00:00
|
|
|
|
$result['result'] = 'NeedCaptcha';
|
2015-04-21 12:14:52 +00:00
|
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
|
LoggerFactory::getInstance(
|
|
|
|
|
'authmanager'
|
|
|
|
|
)->info( 'Captcha data added in account creation API', array(
|
2015-04-21 12:14:52 +00:00
|
|
|
|
'event' => 'captcha.display',
|
|
|
|
|
'type' => 'accountcreation',
|
|
|
|
|
) );
|
Cleaner response for captcha-related createaccount API failure
Instead of a generic API error, you get back a non-Success-resulted
creataccount response, with result='NeedCaptcha'. There's also a warning
included with the message key, and of course the captcha key:
array (
'createaccount' =>
array (
'result' => 'NeedCaptcha',
'warnings' =>
array (
0 =>
array (
'type' => 'warning',
'message' => 'captcha-createaccount-fail',
'params' =>
array (
),
),
),
'captcha' =>
array (
'type' => 'simple',
'mime' => 'text/plain',
'id' => '91510936',
'question' => '76−3',
),
),
)
Change-Id: Id1c9e387c592e6d51a5bd58d99ce3d644dfa300b
2014-02-28 22:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-10 23:16:34 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
}
|