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( &$sp ) {
43 return self::getInstance()->injectUserLogin( $sp );
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 if ( method_exists( $instance, 'showImage' ) )
58 return $instance->showImage();
61 return $instance->showHelp();
67 function SimpleCaptcha() {
68 global $wgCaptchaStorageClass;
69 $this->storage = new $wgCaptchaStorageClass;
72 function getCaptcha() {
73 $a = mt_rand( 0, 100 );
74 $b = mt_rand( 0, 10 );
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 Xml::element( 'input', array(
106 'name' => 'wpCaptchaWord',
107 'id' => 'wpCaptchaWord',
108 'tabindex' => 1 ) ) . // tab in before the edit textarea
110 Xml::element( '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( &$sp ) {
148 global $wgCaptchaTriggers, $wgOut, $wgUser;
149 if ( $wgCaptchaTriggers['createaccount'] ) {
150 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
151 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
155 "<div class='captcha'>" .
156 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
164 * Inject a captcha into the user login form after a failed
165 * password attempt as a speedbump for mass attacks.
166 * @param SimpleTemplate $template
167 * @return bool true to keep running callbacks
169 function injectUserLogin( &$sp ) {
170 if ( $this->isBadLoginTriggered() ) {
173 "<div class='captcha'>" .
174 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
182 * When a bad login attempt is made, increment an expiring counter
183 * in the memcache cloud. Later checks for this may trigger a
184 * captcha display to prevent too many hits from the same place.
186 * @param string $password
187 * @param int $retval authentication return value
188 * @return bool true to keep running callbacks
190 function triggerUserLogin( $user, $password, $retval ) {
191 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
192 if ( $retval == Login::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
193 $key = $this->badLoginKey();
194 $count = $wgMemc->get( $key );
196 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
198 $count = $wgMemc->incr( $key );
204 * Check if a bad login has already been registered for this
205 * IP address. If so, require a captcha.
209 function isBadLoginTriggered() {
210 global $wgMemc, $wgCaptchaBadLoginAttempts;
211 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
215 * Check if the IP is allowed to skip captchas
217 function isIPWhitelisted() {
218 global $wgCaptchaWhitelistIP;
219 if ( $wgCaptchaWhitelistIP ) {
221 foreach ( $wgCaptchaWhitelistIP as $range ) {
222 if ( IP::isInRange( $ip, $range ) ) {
231 * Internal cache key for badlogin checks.
235 function badLoginKey() {
236 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
240 * Check if the submitted form matches the captcha session data provided
241 * by the plugin when the form was generated.
245 * @param string $answer
249 function keyMatch( $answer, $info ) {
250 return $answer == $info['answer'];
253 // ----------------------------------
256 * @param EditPage $editPage
257 * @param string $action (edit/create/addurl...)
258 * @return bool true if action triggers captcha on editPage's namespace
260 function captchaTriggers( &$editPage, $action ) {
261 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
262 // Special config for this NS?
263 if ( isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
264 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
266 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
270 * @param EditPage $editPage
271 * @param string $newtext
272 * @param string $section
273 * @return bool true if the captcha should run
275 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
277 $title = $editPage->mArticle->getTitle();
280 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
281 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
284 if ( $this->isIPWhitelisted() )
288 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
289 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
290 $wgUser->isEmailConfirmed() ) {
291 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
295 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
296 // Check on all edits
298 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
300 $title->getPrefixedText() );
301 $this->action = 'edit';
302 wfDebug( "ConfirmEdit: checking all edits...\n" );
306 if ( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
307 // Check if creating a page
309 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
311 $title->getPrefixedText() );
312 $this->action = 'create';
313 wfDebug( "ConfirmEdit: checking on page creation...\n" );
317 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
318 // Only check edits that add URLs
320 // Get links from the database
321 $oldLinks = $this->getLinksFromTracker( $title );
322 // Share a parse operation with Article::doEdit()
323 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
324 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
326 // Get link changes in the slowest way known to man
327 $oldtext = $this->loadText( $editPage, $section );
328 $oldLinks = $this->findLinks( $editPage, $oldtext );
329 $newLinks = $this->findLinks( $editPage, $newtext );
332 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
333 $addedLinks = array_diff( $unknownLinks, $oldLinks );
334 $numLinks = count( $addedLinks );
336 if ( $numLinks > 0 ) {
338 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
341 $title->getPrefixedText(),
342 implode( ", ", $addedLinks ) );
343 $this->action = 'addurl';
348 global $wgCaptchaRegexes;
349 if ( $wgCaptchaRegexes ) {
350 // Custom regex checks
351 $oldtext = $this->loadText( $editPage, $section );
353 foreach ( $wgCaptchaRegexes as $regex ) {
354 $newMatches = array();
355 if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
356 $oldMatches = array();
357 preg_match_all( $regex, $oldtext, $oldMatches );
359 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
361 $numHits = count( $addedMatches );
362 if ( $numHits > 0 ) {
364 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
368 $title->getPrefixedText(),
369 implode( ", ", $addedMatches ) );
370 $this->action = 'edit';
381 * Filter callback function for URL whitelisting
382 * @param string url to check
383 * @return bool true if unknown, false if whitelisted
386 function filterLink( $url ) {
387 global $wgCaptchaWhitelist;
388 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
390 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
392 : $this->buildRegexes( explode( "\n", $source ) );
394 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
395 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
397 return !( $cwl || $wl );
401 * Build regex from whitelist
402 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
403 * @return string Regex or bool false if whitelist is empty
406 function buildRegexes( $lines ) {
407 # Code duplicated from the SpamBlacklist extension (r19197)
409 # Strip comments and whitespace, then remove blanks
410 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
412 # No lines, don't make a regex which will match everything
413 if ( count( $lines ) == 0 ) {
414 wfDebug( "No lines\n" );
418 # It's faster using the S modifier even though it will usually only be run once
419 // $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
420 // return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
422 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
426 foreach ( $lines as $line ) {
427 // FIXME: not very robust size check, but should work. :)
428 if ( $build === false ) {
430 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
431 $regexes .= $regexStart .
432 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
436 $build .= '|' . $line;
439 if ( $build !== false ) {
440 $regexes .= $regexStart .
441 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
449 * Load external links from the externallinks table
451 function getLinksFromTracker( $title ) {
452 $dbr =& wfGetDB( DB_SLAVE );
453 $id = $title->getArticleId(); // should be zero queries
454 $res = $dbr->select( 'externallinks', array( 'el_to' ),
455 array( 'el_from' => $id ), __METHOD__ );
457 while ( $row = $dbr->fetchObject( $res ) ) {
458 $links[] = $row->el_to;
464 * Backend function for confirmEdit() and confirmEditAPI()
465 * @return bool false if the CAPTCHA is rejected, true otherwise
467 private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
468 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
469 if ( $this->passCaptcha() ) {
475 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
481 * The main callback run on edit attempts.
482 * @param EditPage $editPage
483 * @param string $newtext
484 * @param string $section
485 * @param bool $merged
486 * @return bool true to continue saving, false to abort and show a captcha form
488 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
489 if ( defined( 'MW_API' ) ) {
491 # The CAPTCHA was already checked and approved
494 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
495 $editPage->showEditForm( array( &$this, 'editCallback' ) );
502 * A more efficient edit filter callback based on the text after section merging
503 * @param EditPage $editPage
504 * @param string $newtext
506 function confirmEditMerged( $editPage, $newtext ) {
507 return $this->confirmEdit( $editPage, $newtext, false, true );
511 function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
512 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
513 $this->addCaptchaAPI( $resultArr );
520 * Hook for user creation form submissions.
522 * @param string $message
523 * @return bool true to continue, false to abort user creation
525 function confirmUserCreate( $u, &$message ) {
526 global $wgCaptchaTriggers, $wgUser;
527 if ( $wgCaptchaTriggers['createaccount'] ) {
528 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
529 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
532 if ( $this->isIPWhitelisted() )
535 $this->trigger = "new account '" . $u->getName() . "'";
536 if ( !$this->passCaptcha() ) {
537 $message = wfMsg( 'captcha-createaccount-fail' );
545 * Hook for user login form submissions.
547 * @param string $message
548 * @return bool true to continue, false to abort user creation
550 function confirmUserLogin( $u, $pass, &$retval ) {
551 if ( $this->isBadLoginTriggered() ) {
552 if ( $this->isIPWhitelisted() )
555 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
556 if ( !$this->passCaptcha() ) {
557 $message = wfMsg( 'captcha-badlogin-fail' );
558 // Emulate a bad-password return to confuse the shit out of attackers
559 $retval = Login::WRONG_PASS;
567 * Given a required captcha run, test form input for correct
568 * input on the open session.
569 * @return bool if passed, false if failed or new session
571 function passCaptcha() {
572 $info = $this->retrieveCaptcha();
575 if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
576 $this->log( "passed" );
577 $this->clearCaptcha( $info );
580 $this->clearCaptcha( $info );
581 $this->log( "bad form input" );
585 $this->log( "new captcha session" );
591 * Log the status and any triggering info for debugging or statistics
592 * @param string $message
594 function log( $message ) {
595 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
599 * Generate a captcha session ID and save the info in PHP's session storage.
600 * (Requires the user to have cookies enabled to get through the captcha.)
602 * A random ID is used so legit users can make edits in multiple tabs or
603 * windows without being unnecessarily hobbled by a serial order requirement.
604 * Pass the returned id value into the edit form as wpCaptchaId.
606 * @param array $info data to store
607 * @return string captcha ID key
609 function storeCaptcha( $info ) {
610 if ( !isset( $info['index'] ) ) {
611 // Assign random index if we're not udpating
612 $info['index'] = strval( mt_rand() );
614 $this->storage->store( $info['index'], $info );
615 return $info['index'];
619 * Fetch this session's captcha info.
620 * @return mixed array of info, or false if missing
622 function retrieveCaptcha() {
624 $index = $wgRequest->getVal( 'wpCaptchaId' );
625 return $this->storage->retrieve( $index );
629 * Clear out existing captcha info from the session, to ensure
630 * it can't be reused.
632 function clearCaptcha( $info ) {
633 $this->storage->clear( $info['index'] );
637 * Retrieve the current version of the page or section being edited...
638 * @param EditPage $editPage
639 * @param string $section
643 function loadText( $editPage, $section ) {
644 $rev = Revision::newFromTitle( $editPage->mTitle );
645 if ( is_null( $rev ) ) {
648 $text = $rev->getText();
649 if ( $section != '' ) {
650 return Article::getSection( $text, $section );
658 * Extract a list of all recognized HTTP links in the text.
659 * @param string $text
660 * @return array of strings
662 function findLinks( &$editpage, $text ) {
663 global $wgParser, $wgUser;
665 $options = new ParserOptions();
666 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
667 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
669 return array_keys( $out->getExternalLinks() );
673 * Show a page explaining what this wacky thing is.
675 function showHelp() {
676 global $wgOut, $ceAllowConfirmedEmail;
677 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
678 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
679 if ( $this->storage->cookiesNeeded() ) {
680 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
685 class CaptchaSessionStore {
686 function store( $index, $info ) {
687 $_SESSION['captcha' . $info['index']] = $info;
690 function retrieve( $index ) {
691 if ( isset( $_SESSION['captcha' . $index] ) ) {
692 return $_SESSION['captcha' . $index];
698 function clear( $index ) {
699 unset( $_SESSION['captcha' . $index] );
702 function cookiesNeeded() {
707 class CaptchaCacheStore {
708 function store( $index, $info ) {
709 global $wgMemc, $wgCaptchaSessionExpiration;
710 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
711 $wgCaptchaSessionExpiration );
714 function retrieve( $index ) {
716 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
724 function clear( $index ) {
726 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
729 function cookiesNeeded() {