3 class ConfirmEditHooks {
4 static function getInstance() {
5 global $wgCaptcha, $wgCaptchaClass;
9 $wgCaptcha = new $wgCaptchaClass;
14 static function confirmEdit( $editPage, $newtext, $section ) {
15 return self::getInstance()->confirmEdit( $editPage, $newtext, $section );
18 static function confirmEditMerged( $editPage, $newtext ) {
19 return self::getInstance()->confirmEditMerged( $editPage, $newtext );
22 static function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
23 return self::getInstance()->confirmEditAPI( $editPage, $newtext, $resultArr );
26 static function injectUserCreate( &$template ) {
27 return self::getInstance()->injectUserCreate( $template );
30 static function confirmUserCreate( $u, &$message ) {
31 return self::getInstance()->confirmUserCreate( $u, $message );
34 static function triggerUserLogin( $user, $password, $retval ) {
35 return self::getInstance()->triggerUserLogin( $user, $password, $retval );
38 static function injectUserLogin( &$template ) {
39 return self::getInstance()->injectUserLogin( $template );
42 static function confirmUserLogin( $u, $pass, &$retval ) {
43 return self::getInstance()->confirmUserLogin( $u, $pass, $retval );
46 static function injectEmailUser( &$form ) {
47 return self::getInstance()->injectEmailUser( $form );
50 static function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
51 return self::getInstance()->confirmEmailUser( $from, $to, $subject, $text, $error );
55 class CaptchaSpecialPage extends UnlistedSpecialPage {
56 public function __construct(){
57 parent::__construct( 'Captcha' );
59 function execute( $par ) {
61 $instance = ConfirmEditHooks::getInstance();
64 if ( method_exists( $instance, 'showImage' ) )
65 return $instance->showImage();
68 return $instance->showHelp();
74 function __construct() {
75 global $wgCaptchaStorageClass;
76 $this->storage = new $wgCaptchaStorageClass;
79 function getCaptcha() {
80 $a = mt_rand( 0, 100 );
81 $b = mt_rand( 0, 10 );
83 /* Minus sign is used in the question. UTF-8,
84 since the api uses text/plain, not text/html */
85 $op = mt_rand( 0, 1 ) ? '+' : '−';
88 $answer = ( $op == '+' ) ? ( $a + $b ) : ( $a - $b );
89 return array( 'question' => $test, 'answer' => $answer );
92 function addCaptchaAPI( &$resultArr ) {
93 $captcha = $this->getCaptcha();
94 $index = $this->storeCaptcha( $captcha );
95 $resultArr['captcha']['type'] = 'simple';
96 $resultArr['captcha']['mime'] = 'text/plain';
97 $resultArr['captcha']['id'] = $index;
98 $resultArr['captcha']['question'] = $captcha['question'];
102 * Insert a captcha prompt into the edit form.
103 * This sample implementation generates a simple arithmetic operation;
104 * it would be easy to defeat by machine.
108 * @return string HTML
111 $captcha = $this->getCaptcha();
112 $index = $this->storeCaptcha( $captcha );
114 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
115 Xml::element( 'input', array(
116 'name' => 'wpCaptchaWord',
117 'id' => 'wpCaptchaWord',
118 'tabindex' => 1 ) ) . // tab in before the edit textarea
120 Xml::element( 'input', array(
122 'name' => 'wpCaptchaId',
123 'id' => 'wpCaptchaId',
124 'value' => $index ) );
128 * Insert the captcha prompt into an edit form.
129 * @param OutputPage $out
131 function editCallback( &$out ) {
132 $out->addWikiText( $this->getMessage( $this->action ) );
133 $out->addHTML( $this->getForm() );
137 * Show a message asking the user to enter a captcha on edit
138 * The result will be treated as wiki text
140 * @param $action Action being performed
143 function getMessage( $action ) {
144 $name = 'captcha-' . $action;
145 $text = wfMsg( $name );
146 # Obtain a more tailored message, if possible, otherwise, fall back to
147 # the default for edits
148 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
153 * @fixme if multiple thingies insert a header, could break
155 * @return bool true to keep running callbacks
157 function injectEmailUser( &$form ) {
158 global $wgCaptchaTriggers, $wgOut, $wgUser;
159 if ( $wgCaptchaTriggers['sendemail'] ) {
160 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
161 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
164 $form->addFooterText(
165 "<div class='captcha'>" .
166 $wgOut->parse( $this->getMessage( 'sendemail' ) ) .
175 * @fixme if multiple thingies insert a header, could break
176 * @param SimpleTemplate $template
177 * @return bool true to keep running callbacks
179 function injectUserCreate( &$template ) {
180 global $wgCaptchaTriggers, $wgOut, $wgUser;
181 if ( $wgCaptchaTriggers['createaccount'] ) {
182 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
183 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
186 $template->set( 'header',
187 "<div class='captcha'>" .
188 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
196 * Inject a captcha into the user login form after a failed
197 * password attempt as a speedbump for mass attacks.
198 * @fixme if multiple thingies insert a header, could break
199 * @param SimpleTemplate $template
200 * @return bool true to keep running callbacks
202 function injectUserLogin( &$template ) {
203 if ( $this->isBadLoginTriggered() ) {
205 $template->set( 'header',
206 "<div class='captcha'>" .
207 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
215 * When a bad login attempt is made, increment an expiring counter
216 * in the memcache cloud. Later checks for this may trigger a
217 * captcha display to prevent too many hits from the same place.
219 * @param string $password
220 * @param int $retval authentication return value
221 * @return bool true to keep running callbacks
223 function triggerUserLogin( $user, $password, $retval ) {
224 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
225 if ( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
226 $key = $this->badLoginKey();
227 $count = $wgMemc->get( $key );
229 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
231 $count = $wgMemc->incr( $key );
237 * Check if a bad login has already been registered for this
238 * IP address. If so, require a captcha.
242 function isBadLoginTriggered() {
243 global $wgMemc, $wgCaptchaBadLoginAttempts;
244 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
248 * Check if the IP is allowed to skip captchas
250 function isIPWhitelisted() {
251 global $wgCaptchaWhitelistIP;
252 if ( $wgCaptchaWhitelistIP ) {
254 foreach ( $wgCaptchaWhitelistIP as $range ) {
255 if ( IP::isInRange( $ip, $range ) ) {
264 * Internal cache key for badlogin checks.
268 function badLoginKey() {
269 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
273 * Check if the submitted form matches the captcha session data provided
274 * by the plugin when the form was generated.
278 * @param string $answer
282 function keyMatch( $answer, $info ) {
283 return $answer == $info['answer'];
286 // ----------------------------------
289 * @param EditPage $editPage
290 * @param string $action (edit/create/addurl...)
291 * @return bool true if action triggers captcha on editPage's namespace
293 function captchaTriggers( &$editPage, $action ) {
294 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
295 // Special config for this NS?
296 if ( isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
297 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
299 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
303 * @param EditPage $editPage
304 * @param string $newtext
305 * @param string $section
306 * @return bool true if the captcha should run
308 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
310 $title = $editPage->mArticle->getTitle();
313 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
314 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
317 if ( $this->isIPWhitelisted() )
321 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
322 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
323 $wgUser->isEmailConfirmed() ) {
324 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
328 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
329 // Check on all edits
331 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
333 $title->getPrefixedText() );
334 $this->action = 'edit';
335 wfDebug( "ConfirmEdit: checking all edits...\n" );
339 if ( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
340 // Check if creating a page
342 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
344 $title->getPrefixedText() );
345 $this->action = 'create';
346 wfDebug( "ConfirmEdit: checking on page creation...\n" );
350 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
351 // Only check edits that add URLs
353 // Get links from the database
354 $oldLinks = $this->getLinksFromTracker( $title );
355 // Share a parse operation with Article::doEdit()
356 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
357 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
359 // Get link changes in the slowest way known to man
360 $oldtext = $this->loadText( $editPage, $section );
361 $oldLinks = $this->findLinks( $editPage, $oldtext );
362 $newLinks = $this->findLinks( $editPage, $newtext );
365 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
366 $addedLinks = array_diff( $unknownLinks, $oldLinks );
367 $numLinks = count( $addedLinks );
369 if ( $numLinks > 0 ) {
371 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
374 $title->getPrefixedText(),
375 implode( ", ", $addedLinks ) );
376 $this->action = 'addurl';
381 global $wgCaptchaRegexes;
382 if ( $wgCaptchaRegexes ) {
383 // Custom regex checks
384 $oldtext = $this->loadText( $editPage, $section );
386 foreach ( $wgCaptchaRegexes as $regex ) {
387 $newMatches = array();
388 if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
389 $oldMatches = array();
390 preg_match_all( $regex, $oldtext, $oldMatches );
392 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
394 $numHits = count( $addedMatches );
395 if ( $numHits > 0 ) {
397 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
401 $title->getPrefixedText(),
402 implode( ", ", $addedMatches ) );
403 $this->action = 'edit';
414 * Filter callback function for URL whitelisting
415 * @param string url to check
416 * @return bool true if unknown, false if whitelisted
419 function filterLink( $url ) {
420 global $wgCaptchaWhitelist;
421 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
423 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
425 : $this->buildRegexes( explode( "\n", $source ) );
427 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
428 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
430 return !( $cwl || $wl );
434 * Build regex from whitelist
435 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
436 * @return string Regex or bool false if whitelist is empty
439 function buildRegexes( $lines ) {
440 # Code duplicated from the SpamBlacklist extension (r19197)
442 # Strip comments and whitespace, then remove blanks
443 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
445 # No lines, don't make a regex which will match everything
446 if ( count( $lines ) == 0 ) {
447 wfDebug( "No lines\n" );
451 # It's faster using the S modifier even though it will usually only be run once
452 // $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
453 // return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
455 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
459 foreach ( $lines as $line ) {
460 // FIXME: not very robust size check, but should work. :)
461 if ( $build === false ) {
463 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
464 $regexes .= $regexStart .
465 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
469 $build .= '|' . $line;
472 if ( $build !== false ) {
473 $regexes .= $regexStart .
474 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
482 * Load external links from the externallinks table
484 function getLinksFromTracker( $title ) {
485 $dbr = wfGetDB( DB_SLAVE );
486 $id = $title->getArticleId(); // should be zero queries
487 $res = $dbr->select( 'externallinks', array( 'el_to' ),
488 array( 'el_from' => $id ), __METHOD__ );
490 foreach ( $res as $row ) {
491 $links[] = $row->el_to;
497 * Backend function for confirmEdit() and confirmEditAPI()
498 * @return bool false if the CAPTCHA is rejected, true otherwise
500 private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
501 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
502 if ( $this->passCaptcha() ) {
508 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
514 * The main callback run on edit attempts.
515 * @param EditPage $editPage
516 * @param string $newtext
517 * @param string $section
518 * @param bool $merged
519 * @return bool true to continue saving, false to abort and show a captcha form
521 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
522 if ( defined( 'MW_API' ) ) {
524 # The CAPTCHA was already checked and approved
527 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
528 $editPage->showEditForm( array( &$this, 'editCallback' ) );
535 * A more efficient edit filter callback based on the text after section merging
536 * @param EditPage $editPage
537 * @param string $newtext
539 function confirmEditMerged( $editPage, $newtext ) {
540 return $this->confirmEdit( $editPage, $newtext, false, true );
544 function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
545 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
546 $this->addCaptchaAPI( $resultArr );
553 * Hook for user creation form submissions.
555 * @param string $message
556 * @return bool true to continue, false to abort user creation
558 function confirmUserCreate( $u, &$message ) {
559 global $wgCaptchaTriggers, $wgUser;
560 if ( $wgCaptchaTriggers['createaccount'] ) {
561 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
562 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
565 if ( $this->isIPWhitelisted() )
568 $this->trigger = "new account '" . $u->getName() . "'";
569 if ( !$this->passCaptcha() ) {
570 $message = wfMsg( 'captcha-createaccount-fail' );
578 * Hook for user login form submissions.
580 * @param string $message
581 * @return bool true to continue, false to abort user creation
583 function confirmUserLogin( $u, $pass, &$retval ) {
584 if ( $this->isBadLoginTriggered() ) {
585 if ( $this->isIPWhitelisted() )
588 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
589 if ( !$this->passCaptcha() ) {
590 // Emulate a bad-password return to confuse the shit out of attackers
591 $retval = LoginForm::WRONG_PASS;
599 * Check the captcha on Special:EmailUser
600 * @param $from MailAddress
601 * @param $to MailAddress
602 * @param $subject String
603 * @param $text String
604 * @param $error String reference
605 * @return Bool true to continue saving, false to abort and show a captcha form
607 function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
608 global $wgCaptchaTriggers, $wgUser;
609 if ( $wgCaptchaTriggers['sendemail'] ) {
610 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
611 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
614 if ( $this->isIPWhitelisted() )
617 if ( defined( 'MW_API' ) ) {
619 # Asking for captchas in the API is really silly
620 $error = wfMsg( 'captcha-disabledinapi' );
623 $this->trigger = "{$wgUser->getName()} sending email";
624 if ( !$this->passCaptcha() ) {
625 $error = wfMsg( 'captcha-sendemail-fail' );
633 * Given a required captcha run, test form input for correct
634 * input on the open session.
635 * @return bool if passed, false if failed or new session
637 function passCaptcha() {
638 $info = $this->retrieveCaptcha();
641 if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
642 $this->log( "passed" );
643 $this->clearCaptcha( $info );
646 $this->clearCaptcha( $info );
647 $this->log( "bad form input" );
651 $this->log( "new captcha session" );
657 * Log the status and any triggering info for debugging or statistics
658 * @param string $message
660 function log( $message ) {
661 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
665 * Generate a captcha session ID and save the info in PHP's session storage.
666 * (Requires the user to have cookies enabled to get through the captcha.)
668 * A random ID is used so legit users can make edits in multiple tabs or
669 * windows without being unnecessarily hobbled by a serial order requirement.
670 * Pass the returned id value into the edit form as wpCaptchaId.
672 * @param array $info data to store
673 * @return string captcha ID key
675 function storeCaptcha( $info ) {
676 if ( !isset( $info['index'] ) ) {
677 // Assign random index if we're not udpating
678 $info['index'] = strval( mt_rand() );
680 $this->storage->store( $info['index'], $info );
681 return $info['index'];
685 * Fetch this session's captcha info.
686 * @return mixed array of info, or false if missing
688 function retrieveCaptcha() {
690 $index = $wgRequest->getVal( 'wpCaptchaId' );
691 return $this->storage->retrieve( $index );
695 * Clear out existing captcha info from the session, to ensure
696 * it can't be reused.
698 function clearCaptcha( $info ) {
699 $this->storage->clear( $info['index'] );
703 * Retrieve the current version of the page or section being edited...
704 * @param EditPage $editPage
705 * @param string $section
709 function loadText( $editPage, $section ) {
710 $rev = Revision::newFromTitle( $editPage->mTitle );
711 if ( is_null( $rev ) ) {
714 $text = $rev->getText();
715 if ( $section != '' ) {
717 return $wgParser->getSection( $text, $section );
725 * Extract a list of all recognized HTTP links in the text.
726 * @param string $text
727 * @return array of strings
729 function findLinks( &$editpage, $text ) {
730 global $wgParser, $wgUser;
732 $options = new ParserOptions();
733 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
734 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
736 return array_keys( $out->getExternalLinks() );
740 * Show a page explaining what this wacky thing is.
742 function showHelp() {
744 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
745 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
746 if ( $this->storage->cookiesNeeded() ) {
747 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
752 class CaptchaSessionStore {
753 function store( $index, $info ) {
754 $_SESSION['captcha' . $info['index']] = $info;
757 function retrieve( $index ) {
758 if ( isset( $_SESSION['captcha' . $index] ) ) {
759 return $_SESSION['captcha' . $index];
765 function clear( $index ) {
766 unset( $_SESSION['captcha' . $index] );
769 function cookiesNeeded() {
774 class CaptchaCacheStore {
775 function store( $index, $info ) {
776 global $wgMemc, $wgCaptchaSessionExpiration;
777 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
778 $wgCaptchaSessionExpiration );
781 function retrieve( $index ) {
783 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
791 function clear( $index ) {
793 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
796 function cookiesNeeded() {