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 if( method_exists($instance,'showImage') )
58 return $instance->showImage();
61 return $instance->showHelp();
68 function SimpleCaptcha() {
69 global $wgCaptchaStorageClass;
70 $this->storage = new $wgCaptchaStorageClass;
73 function getCaptcha() {
76 $op = mt_rand(0, 1) ? '+' : '-';
79 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
80 return array('question' => $test, 'answer' => $answer);
83 function addCaptchaAPI(&$resultArr) {
84 $captcha = $this->getCaptcha();
85 $index = $this->storeCaptcha( $captcha );
86 $resultArr['captcha']['type'] = 'simple';
87 $resultArr['captcha']['mime'] = 'text/plain';
88 $resultArr['captcha']['id'] = $index;
89 $resultArr['captcha']['question'] = $captcha['question'];
93 * Insert a captcha prompt into the edit form.
94 * This sample implementation generates a simple arithmetic operation;
95 * it would be easy to defeat by machine.
102 $captcha = $this->getCaptcha();
103 $index = $this->storeCaptcha( $captcha );
105 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
106 Xml::element( 'input', array(
107 'name' => 'wpCaptchaWord',
108 'id' => 'wpCaptchaWord',
109 'tabindex' => 1 ) ) . // tab in before the edit textarea
111 Xml::element( 'input', array(
113 'name' => 'wpCaptchaId',
114 'id' => 'wpCaptchaId',
115 'value' => $index ) );
119 * Insert the captcha prompt into an edit form.
120 * @param OutputPage $out
122 function editCallback( &$out ) {
123 $out->addWikiText( $this->getMessage( $this->action ) );
124 $out->addHTML( $this->getForm() );
128 * Show a message asking the user to enter a captcha on edit
129 * The result will be treated as wiki text
131 * @param $action Action being performed
134 function getMessage( $action ) {
135 $name = 'captcha-' . $action;
136 $text = wfMsg( $name );
137 # Obtain a more tailored message, if possible, otherwise, fall back to
138 # the default for edits
139 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
144 * @fixme if multiple thingies insert a header, could break
145 * @param SimpleTemplate $template
146 * @return bool true to keep running callbacks
148 function injectUserCreate( &$template ) {
149 global $wgCaptchaTriggers, $wgOut, $wgUser;
150 if( $wgCaptchaTriggers['createaccount'] ) {
151 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
152 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
155 $template->set( 'header',
156 "<div class='captcha'>" .
157 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
165 * Inject a captcha into the user login form after a failed
166 * password attempt as a speedbump for mass attacks.
167 * @fixme if multiple thingies insert a header, could break
168 * @param SimpleTemplate $template
169 * @return bool true to keep running callbacks
171 function injectUserLogin( &$template ) {
172 if( $this->isBadLoginTriggered() ) {
174 $template->set( 'header',
175 "<div class='captcha'>" .
176 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
184 * When a bad login attempt is made, increment an expiring counter
185 * in the memcache cloud. Later checks for this may trigger a
186 * captcha display to prevent too many hits from the same place.
188 * @param string $password
189 * @param int $retval authentication return value
190 * @return bool true to keep running callbacks
192 function triggerUserLogin( $user, $password, $retval ) {
193 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
194 if( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
195 $key = $this->badLoginKey();
196 $count = $wgMemc->get( $key );
198 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
200 $count = $wgMemc->incr( $key );
206 * Check if a bad login has already been registered for this
207 * IP address. If so, require a captcha.
211 function isBadLoginTriggered() {
212 global $wgMemc, $wgCaptchaBadLoginAttempts;
213 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
217 * Internal cache key for badlogin checks.
221 function badLoginKey() {
222 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
226 * Check if the submitted form matches the captcha session data provided
227 * by the plugin when the form was generated.
231 * @param string $answer
235 function keyMatch( $answer, $info ) {
236 return $answer == $info['answer'];
239 // ----------------------------------
242 * @param EditPage $editPage
243 * @param string $action (edit/create/addurl...)
244 * @return bool true if action triggers captcha on editPage's namespace
246 function captchaTriggers( &$editPage, $action) {
247 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
248 //Special config for this NS?
249 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
250 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
252 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
257 * @param EditPage $editPage
258 * @param string $newtext
259 * @param string $section
260 * @return bool true if the captcha should run
262 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
264 $title = $editPage->mArticle->getTitle();
267 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
268 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
271 global $wgCaptchaWhitelistIP;
272 if( !empty( $wgCaptchaWhitelistIP ) ) {
274 foreach ( $wgCaptchaWhitelistIP as $range ) {
275 if ( IP::isInRange( $ip, $range ) ) {
282 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
283 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
284 $wgUser->isEmailConfirmed() ) {
285 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
289 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
290 // Check on all edits
292 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
294 $title->getPrefixedText() );
295 $this->action = 'edit';
296 wfDebug( "ConfirmEdit: checking all edits...\n" );
300 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
301 //Check if creating a page
303 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
305 $title->getPrefixedText() );
306 $this->action = 'create';
307 wfDebug( "ConfirmEdit: checking on page creation...\n" );
311 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
312 // Only check edits that add URLs
314 // Get links from the database
315 $oldLinks = $this->getLinksFromTracker( $title );
316 // Share a parse operation with Article::doEdit()
317 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
318 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
320 // Get link changes in the slowest way known to man
321 $oldtext = $this->loadText( $editPage, $section );
322 $oldLinks = $this->findLinks( $editPage, $oldtext );
323 $newLinks = $this->findLinks( $editPage, $newtext );
326 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
327 $addedLinks = array_diff( $unknownLinks, $oldLinks );
328 $numLinks = count( $addedLinks );
330 if( $numLinks > 0 ) {
332 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
335 $title->getPrefixedText(),
336 implode( ", ", $addedLinks ) );
337 $this->action = 'addurl';
342 global $wgCaptchaRegexes;
343 if( !empty( $wgCaptchaRegexes ) ) {
344 // Custom regex checks
345 $oldtext = $this->loadText( $editPage, $section );
347 foreach( $wgCaptchaRegexes as $regex ) {
348 $newMatches = array();
349 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
350 $oldMatches = array();
351 preg_match_all( $regex, $oldtext, $oldMatches );
353 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
355 $numHits = count( $addedMatches );
358 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
362 $title->getPrefixedText(),
363 implode( ", ", $addedMatches ) );
364 $this->action = 'edit';
375 * Filter callback function for URL whitelisting
376 * @param string url to check
377 * @return bool true if unknown, false if whitelisted
380 function filterLink( $url ) {
381 global $wgCaptchaWhitelist;
382 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
384 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
386 : $this->buildRegexes( explode( "\n", $source ) );
388 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
389 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
391 return !( $cwl || $wl );
395 * Build regex from whitelist
396 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
397 * @return string Regex or bool false if whitelist is empty
400 function buildRegexes( $lines ) {
401 # Code duplicated from the SpamBlacklist extension (r19197)
403 # Strip comments and whitespace, then remove blanks
404 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
406 # No lines, don't make a regex which will match everything
407 if ( count( $lines ) == 0 ) {
408 wfDebug( "No lines\n" );
412 # It's faster using the S modifier even though it will usually only be run once
413 //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
414 //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
416 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
420 foreach( $lines as $line ) {
421 // FIXME: not very robust size check, but should work. :)
422 if( $build === false ) {
424 } elseif( strlen( $build ) + strlen( $line ) > $regexMax ) {
425 $regexes .= $regexStart .
426 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
430 $build .= '|' . $line;
433 if( $build !== false ) {
434 $regexes .= $regexStart .
435 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
443 * Load external links from the externallinks table
445 function getLinksFromTracker( $title ) {
446 $dbr =& wfGetDB( DB_SLAVE );
447 $id = $title->getArticleId(); // should be zero queries
448 $res = $dbr->select( 'externallinks', array( 'el_to' ),
449 array( 'el_from' => $id ), __METHOD__ );
451 while ( $row = $dbr->fetchObject( $res ) ) {
452 $links[] = $row->el_to;
458 * Backend function for confirmEdit() and confirmEditAPI()
459 * @return bool false if the CAPTCHA is rejected, true otherwise
461 private function doConfirmEdit( &$editPage, $newtext, $section, $merged = false ) {
462 if( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
463 if( $this->passCaptcha() ) {
469 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
475 * The main callback run on edit attempts.
476 * @param EditPage $editPage
477 * @param string $newtext
478 * @param string $section
479 * @param bool $merged
480 * @return bool true to continue saving, false to abort and show a captcha form
482 function confirmEdit( &$editPage, $newtext, $section, $merged = false ) {
484 if( is_null( $wgTitle ) ) {
486 # The CAPTCHA was already checked and approved
489 if( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
490 $editPage->showEditForm( array( &$this, 'editCallback' ) );
497 * A more efficient edit filter callback based on the text after section merging
498 * @param EditPage $editPage
499 * @param string $newtext
501 function confirmEditMerged( &$editPage, $newtext ) {
502 return $this->confirmEdit( $editPage, $newtext, false, true );
506 function confirmEditAPI( &$editPage, $newtext, &$resultArr) {
507 if( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
508 $this->addCaptchaAPI($resultArr);
515 * Hook for user creation form submissions.
517 * @param string $message
518 * @return bool true to continue, false to abort user creation
520 function confirmUserCreate( $u, &$message ) {
521 global $wgCaptchaTriggers, $wgUser;
522 if( $wgCaptchaTriggers['createaccount'] ) {
523 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
524 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
527 $this->trigger = "new account '" . $u->getName() . "'";
528 if( !$this->passCaptcha() ) {
529 $message = wfMsg( 'captcha-createaccount-fail' );
537 * Hook for user login form submissions.
539 * @param string $message
540 * @return bool true to continue, false to abort user creation
542 function confirmUserLogin( $u, $pass, &$retval ) {
543 if( $this->isBadLoginTriggered() ) {
544 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
545 if( !$this->passCaptcha() ) {
546 $message = wfMsg( 'captcha-badlogin-fail' );
547 // Emulate a bad-password return to confuse the shit out of attackers
548 $retval = LoginForm::WRONG_PASS;
556 * Given a required captcha run, test form input for correct
557 * input on the open session.
558 * @return bool if passed, false if failed or new session
560 function passCaptcha() {
561 $info = $this->retrieveCaptcha();
564 if( $this->keyMatch( $wgRequest->getVal('wpCaptchaWord'), $info ) ) {
565 $this->log( "passed" );
566 $this->clearCaptcha( $info );
569 $this->clearCaptcha( $info );
570 $this->log( "bad form input" );
574 $this->log( "new captcha session" );
580 * Log the status and any triggering info for debugging or statistics
581 * @param string $message
583 function log( $message ) {
584 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
588 * Generate a captcha session ID and save the info in PHP's session storage.
589 * (Requires the user to have cookies enabled to get through the captcha.)
591 * A random ID is used so legit users can make edits in multiple tabs or
592 * windows without being unnecessarily hobbled by a serial order requirement.
593 * Pass the returned id value into the edit form as wpCaptchaId.
595 * @param array $info data to store
596 * @return string captcha ID key
598 function storeCaptcha( $info ) {
599 if( !isset( $info['index'] ) ) {
600 // Assign random index if we're not udpating
601 $info['index'] = strval( mt_rand() );
603 $this->storage->store( $info['index'], $info );
604 return $info['index'];
608 * Fetch this session's captcha info.
609 * @return mixed array of info, or false if missing
611 function retrieveCaptcha() {
613 $index = $wgRequest->getVal( 'wpCaptchaId' );
614 return $this->storage->retrieve( $index );
618 * Clear out existing captcha info from the session, to ensure
619 * it can't be reused.
621 function clearCaptcha( $info ) {
622 $this->storage->clear( $info['index'] );
626 * Retrieve the current version of the page or section being edited...
627 * @param EditPage $editPage
628 * @param string $section
632 function loadText( $editPage, $section ) {
633 $rev = Revision::newFromTitle( $editPage->mTitle );
634 if( is_null( $rev ) ) {
637 $text = $rev->getText();
638 if( $section != '' ) {
639 return Article::getSection( $text, $section );
647 * Extract a list of all recognized HTTP links in the text.
648 * @param string $text
649 * @return array of strings
651 function findLinks( &$editpage, $text ) {
652 global $wgParser, $wgUser;
654 $options = new ParserOptions();
655 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
656 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
658 return array_keys( $out->getExternalLinks() );
662 * Show a page explaining what this wacky thing is.
664 function showHelp() {
665 global $wgOut, $ceAllowConfirmedEmail;
666 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
667 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
668 if ( $this->storage->cookiesNeeded() ) {
669 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
675 class CaptchaSessionStore {
676 function store( $index, $info ) {
677 $_SESSION['captcha' . $info['index']] = $info;
680 function retrieve( $index ) {
681 if( isset( $_SESSION['captcha' . $index] ) ) {
682 return $_SESSION['captcha' . $index];
688 function clear( $index ) {
689 unset( $_SESSION['captcha' . $index] );
692 function cookiesNeeded() {
697 class CaptchaCacheStore {
698 function store( $index, $info ) {
699 global $wgMemc, $wgCaptchaSessionExpiration;
700 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
701 $wgCaptchaSessionExpiration );
704 function retrieve( $index ) {
706 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
714 function clear( $index ) {
716 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
719 function cookiesNeeded() {