3 class ConfirmEditHooks {
4 static function getInstance() {
5 global $wgCaptcha, $wgCaptchaClass, $wgExtensionMessagesFiles;
9 wfLoadExtensionMessages( 'ConfirmEdit' );
10 if ( isset( $wgExtensionMessagesFiles[$wgCaptchaClass] ) ) {
11 wfLoadExtensionMessages( $wgCaptchaClass );
13 $wgCaptcha = new $wgCaptchaClass;
18 static function confirmEdit( &$editPage, $newtext, $section ) {
19 return self::getInstance()->confirmEdit( $editPage, $newtext, $section );
22 static function confirmEditMerged( &$editPage, $newtext ) {
23 return self::getInstance()->confirmEditMerged( $editPage, $newtext );
26 static function confirmEditAPI( &$editPage, $newtext, &$resultArr ) {
27 return self::getInstance()->confirmEditAPI( $editPage, $newtext, $resultArr );
30 static function injectUserCreate( &$template ) {
31 return self::getInstance()->injectUserCreate( $template );
34 static function confirmUserCreate( $u, &$message ) {
35 return self::getInstance()->confirmUserCreate( $u, $message );
38 static function triggerUserLogin( $user, $password, $retval ) {
39 return self::getInstance()->triggerUserLogin( $user, $password, $retval );
42 static function injectUserLogin( &$template ) {
43 return self::getInstance()->injectUserLogin( $template );
46 static function confirmUserLogin( $u, $pass, &$retval ) {
47 return self::getInstance()->confirmUserLogin( $u, $pass, $retval );
51 class CaptchaSpecialPage extends UnlistedSpecialPage {
52 function execute( $par ) {
54 $instance = ConfirmEditHooks::getInstance();
57 return $instance->showImage();
60 return $instance->showHelp();
67 function SimpleCaptcha() {
68 global $wgCaptchaStorageClass;
69 $this->storage = new $wgCaptchaStorageClass;
72 function getCaptcha() {
75 $op = mt_rand(0, 1) ? '+' : '-';
78 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
79 return array('question' => $test, 'answer' => $answer);
82 function addCaptchaAPI(&$resultArr) {
83 $captcha = $this->getCaptcha();
84 $index = $this->storeCaptcha( $captcha );
85 $resultArr['captcha']['type'] = 'simple';
86 $resultArr['captcha']['mime'] = 'text/plain';
87 $resultArr['captcha']['id'] = $index;
88 $resultArr['captcha']['question'] = $captcha['question'];
92 * Insert a captcha prompt into the edit form.
93 * This sample implementation generates a simple arithmetic operation;
94 * it would be easy to defeat by machine.
101 $captcha = $this->getCaptcha();
102 $index = $this->storeCaptcha( $captcha );
104 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
105 wfElement( 'input', array(
106 'name' => 'wpCaptchaWord',
107 'id' => 'wpCaptchaWord',
108 'tabindex' => 1 ) ) . // tab in before the edit textarea
110 wfElement( 'input', array(
112 'name' => 'wpCaptchaId',
113 'id' => 'wpCaptchaId',
114 'value' => $index ) );
118 * Insert the captcha prompt into an edit form.
119 * @param OutputPage $out
121 function editCallback( &$out ) {
122 $out->addWikiText( $this->getMessage( $this->action ) );
123 $out->addHTML( $this->getForm() );
127 * Show a message asking the user to enter a captcha on edit
128 * The result will be treated as wiki text
130 * @param $action Action being performed
133 function getMessage( $action ) {
134 $name = 'captcha-' . $action;
135 $text = wfMsg( $name );
136 # Obtain a more tailored message, if possible, otherwise, fall back to
137 # the default for edits
138 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
143 * @fixme if multiple thingies insert a header, could break
144 * @param SimpleTemplate $template
145 * @return bool true to keep running callbacks
147 function injectUserCreate( &$template ) {
148 global $wgCaptchaTriggers, $wgOut;
149 if( $wgCaptchaTriggers['createaccount'] ) {
150 $template->set( 'header',
151 "<div class='captcha'>" .
152 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
160 * Inject a captcha into the user login form after a failed
161 * password attempt as a speedbump for mass attacks.
162 * @fixme if multiple thingies insert a header, could break
163 * @param SimpleTemplate $template
164 * @return bool true to keep running callbacks
166 function injectUserLogin( &$template ) {
167 if( $this->isBadLoginTriggered() ) {
169 $template->set( 'header',
170 "<div class='captcha'>" .
171 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
179 * When a bad login attempt is made, increment an expiring counter
180 * in the memcache cloud. Later checks for this may trigger a
181 * captcha display to prevent too many hits from the same place.
183 * @param string $password
184 * @param int $retval authentication return value
185 * @return bool true to keep running callbacks
187 function triggerUserLogin( $user, $password, $retval ) {
188 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
189 if( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
190 $key = $this->badLoginKey();
191 $count = $wgMemc->get( $key );
193 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
195 $count = $wgMemc->incr( $key );
201 * Check if a bad login has already been registered for this
202 * IP address. If so, require a captcha.
206 function isBadLoginTriggered() {
207 global $wgMemc, $wgCaptchaBadLoginAttempts;
208 return intval( $wgMemc->get( $this->badLoginKey() ) ) > $wgCaptchaBadLoginAttempts;
212 * Internal cache key for badlogin checks.
216 function badLoginKey() {
217 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
221 * Check if the submitted form matches the captcha session data provided
222 * by the plugin when the form was generated.
226 * @param string $answer
230 function keyMatch( $answer, $info ) {
231 return $answer == $info['answer'];
234 // ----------------------------------
237 * @param EditPage $editPage
238 * @param string $action (edit/create/addurl...)
239 * @return bool true if action triggers captcha on editPage's namespace
241 function captchaTriggers( &$editPage, $action) {
242 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
243 //Special config for this NS?
244 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
245 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
247 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
252 * @param EditPage $editPage
253 * @param string $newtext
254 * @param string $section
255 * @return bool true if the captcha should run
257 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
259 $title = $editPage->mArticle->getTitle();
262 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
263 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
266 global $wgCaptchaWhitelistIP;
267 if( !empty( $wgCaptchaWhitelistIP ) ) {
269 foreach ( $wgCaptchaWhitelistIP as $range ) {
270 if ( IP::isInRange( $ip, $range ) ) {
277 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
278 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
279 $wgUser->isEmailConfirmed() ) {
280 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
284 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
285 // Check on all edits
287 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
289 $title->getPrefixedText() );
290 $this->action = 'edit';
291 wfDebug( "ConfirmEdit: checking all edits...\n" );
295 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
296 //Check if creating a page
298 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
300 $title->getPrefixedText() );
301 $this->action = 'create';
302 wfDebug( "ConfirmEdit: checking on page creation...\n" );
306 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
307 // Only check edits that add URLs
309 // Get links from the database
310 $oldLinks = $this->getLinksFromTracker( $title );
311 // Share a parse operation with Article::doEdit()
312 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
313 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
315 // Get link changes in the slowest way known to man
316 $oldtext = $this->loadText( $editPage, $section );
317 $oldLinks = $this->findLinks( $oldtext );
318 $newLinks = $this->findLinks( $newtext );
321 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
322 $addedLinks = array_diff( $unknownLinks, $oldLinks );
323 $numLinks = count( $addedLinks );
325 if( $numLinks > 0 ) {
327 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
330 $title->getPrefixedText(),
331 implode( ", ", $addedLinks ) );
332 $this->action = 'addurl';
337 global $wgCaptchaRegexes;
338 if( !empty( $wgCaptchaRegexes ) ) {
339 // Custom regex checks
340 $oldtext = $this->loadText( $editPage, $section );
342 foreach( $wgCaptchaRegexes as $regex ) {
343 $newMatches = array();
344 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
345 $oldMatches = array();
346 preg_match_all( $regex, $oldtext, $oldMatches );
348 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
350 $numHits = count( $addedMatches );
353 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
357 $title->getPrefixedText(),
358 implode( ", ", $addedMatches ) );
359 $this->action = 'edit';
370 * Filter callback function for URL whitelisting
371 * @param string url to check
372 * @return bool true if unknown, false if whitelisted
375 function filterLink( $url ) {
376 global $wgCaptchaWhitelist;
377 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
379 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
381 : $this->buildRegexes( explode( "\n", $source ) );
383 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
384 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
386 return !( $cwl || $wl );
390 * Build regex from whitelist
391 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
392 * @return string Regex or bool false if whitelist is empty
395 function buildRegexes( $lines ) {
396 # Code duplicated from the SpamBlacklist extension (r19197)
398 # Strip comments and whitespace, then remove blanks
399 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
401 # No lines, don't make a regex which will match everything
402 if ( count( $lines ) == 0 ) {
403 wfDebug( "No lines\n" );
407 # It's faster using the S modifier even though it will usually only be run once
408 //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
409 //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
411 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
415 foreach( $lines as $line ) {
416 // FIXME: not very robust size check, but should work. :)
417 if( $build === false ) {
419 } elseif( strlen( $build ) + strlen( $line ) > $regexMax ) {
420 $regexes .= $regexStart .
421 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
425 $build .= '|' . $line;
428 if( $build !== false ) {
429 $regexes .= $regexStart .
430 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
438 * Load external links from the externallinks table
440 function getLinksFromTracker( $title ) {
441 $dbr =& wfGetDB( DB_SLAVE );
442 $id = $title->getArticleId(); // should be zero queries
443 $res = $dbr->select( 'externallinks', array( 'el_to' ),
444 array( 'el_from' => $id ), __METHOD__ );
446 while ( $row = $dbr->fetchObject( $res ) ) {
447 $links[] = $row->el_to;
453 * Backend function for confirmEdit() and confirmEditAPI()
454 * @return bool false if the CAPTCHA is rejected, true otherwise
456 private function doConfirmEdit( &$editPage, $newtext, $section, $merged = false ) {
457 if( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
458 if( $this->passCaptcha() ) {
464 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
470 * The main callback run on edit attempts.
471 * @param EditPage $editPage
472 * @param string $newtext
473 * @param string $section
474 * @param bool $merged
475 * @return bool true to continue saving, false to abort and show a captcha form
477 function confirmEdit( &$editPage, $newtext, $section, $merged = false ) {
479 if( is_null( $wgTitle ) ) {
481 # The CAPTCHA was already checked and approved
484 if( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
485 $editPage->showEditForm( array( &$this, 'editCallback' ) );
492 * A more efficient edit filter callback based on the text after section merging
493 * @param EditPage $editPage
494 * @param string $newtext
496 function confirmEditMerged( &$editPage, $newtext ) {
497 return $this->confirmEdit( $editPage, $newtext, false, true );
501 function confirmEditAPI( &$editPage, $newtext, &$resultArr) {
502 if( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
503 $this->addCaptchaAPI($resultArr);
510 * Hook for user creation form submissions.
512 * @param string $message
513 * @return bool true to continue, false to abort user creation
515 function confirmUserCreate( $u, &$message ) {
516 global $wgCaptchaTriggers;
517 if( $wgCaptchaTriggers['createaccount'] ) {
518 $this->trigger = "new account '" . $u->getName() . "'";
519 if( !$this->passCaptcha() ) {
520 $message = wfMsg( 'captcha-createaccount-fail' );
528 * Hook for user login form submissions.
530 * @param string $message
531 * @return bool true to continue, false to abort user creation
533 function confirmUserLogin( $u, $pass, &$retval ) {
534 if( $this->isBadLoginTriggered() ) {
535 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
536 if( !$this->passCaptcha() ) {
537 $message = wfMsg( 'captcha-badlogin-fail' );
538 // Emulate a bad-password return to confuse the shit out of attackers
539 $retval = LoginForm::WRONG_PASS;
547 * Given a required captcha run, test form input for correct
548 * input on the open session.
549 * @return bool if passed, false if failed or new session
551 function passCaptcha() {
552 $info = $this->retrieveCaptcha();
555 if( $this->keyMatch( $wgRequest->getVal('wpCaptchaWord'), $info ) ) {
556 $this->log( "passed" );
557 $this->clearCaptcha( $info );
560 $this->clearCaptcha( $info );
561 $this->log( "bad form input" );
565 $this->log( "new captcha session" );
571 * Log the status and any triggering info for debugging or statistics
572 * @param string $message
574 function log( $message ) {
575 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
579 * Generate a captcha session ID and save the info in PHP's session storage.
580 * (Requires the user to have cookies enabled to get through the captcha.)
582 * A random ID is used so legit users can make edits in multiple tabs or
583 * windows without being unnecessarily hobbled by a serial order requirement.
584 * Pass the returned id value into the edit form as wpCaptchaId.
586 * @param array $info data to store
587 * @return string captcha ID key
589 function storeCaptcha( $info ) {
590 if( !isset( $info['index'] ) ) {
591 // Assign random index if we're not udpating
592 $info['index'] = strval( mt_rand() );
594 $this->storage->store( $info['index'], $info );
595 return $info['index'];
599 * Fetch this session's captcha info.
600 * @return mixed array of info, or false if missing
602 function retrieveCaptcha() {
604 $index = $wgRequest->getVal( 'wpCaptchaId' );
605 return $this->storage->retrieve( $index );
609 * Clear out existing captcha info from the session, to ensure
610 * it can't be reused.
612 function clearCaptcha( $info ) {
613 $this->storage->clear( $info['index'] );
617 * Retrieve the current version of the page or section being edited...
618 * @param EditPage $editPage
619 * @param string $section
623 function loadText( $editPage, $section ) {
624 $rev = Revision::newFromTitle( $editPage->mTitle );
625 if( is_null( $rev ) ) {
628 $text = $rev->getText();
629 if( $section != '' ) {
630 return Article::getSection( $text, $section );
638 * Extract a list of all recognized HTTP links in the text.
639 * @param string $text
640 * @return array of strings
642 function findLinks( $text ) {
643 global $wgParser, $wgTitle, $wgUser;
645 $options = new ParserOptions();
646 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
647 $out = $wgParser->parse( $text, $wgTitle, $options );
649 return array_keys( $out->getExternalLinks() );
653 * Show a page explaining what this wacky thing is.
655 function showHelp() {
656 global $wgOut, $ceAllowConfirmedEmail;
657 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
658 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
659 if ( $this->storage->cookiesNeeded() ) {
660 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
666 class CaptchaSessionStore {
667 function store( $index, $info ) {
668 $_SESSION['captcha' . $info['index']] = $info;
671 function retrieve( $index ) {
672 if( isset( $_SESSION['captcha' . $index] ) ) {
673 return $_SESSION['captcha' . $index];
679 function clear( $index ) {
680 unset( $_SESSION['captcha' . $index] );
683 function cookiesNeeded() {
688 class CaptchaCacheStore {
689 function store( $index, $info ) {
690 global $wgMemc, $wgCaptchaSessionExpiration;
691 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
692 $wgCaptchaSessionExpiration );
695 function retrieve( $index ) {
697 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
705 function clear( $index ) {
707 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
710 function cookiesNeeded() {