4 * Experimental captcha plugin framework.
5 * Not intended as a real production captcha system; derived classes
6 * can extend the base to produce their fancy images in place of the
7 * text-based test output here.
9 * Copyright (C) 2005-2007 Brion Vibber <brion@wikimedia.org>
10 * http://www.mediawiki.org/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @addtogroup Extensions
30 if ( defined( 'MEDIAWIKI' ) ) {
32 global $wgExtensionFunctions, $wgGroupPermissions;
34 $wgExtensionFunctions[] = 'ceSetup';
35 $wgExtensionCredits['other'][] = array(
36 'name' => 'ConfirmEdit',
37 'author' => 'Brion Vibber',
38 'url' => 'http://www.mediawiki.org/wiki/Extension:ConfirmEdit',
39 'description' => 'Simple captcha implementation',
42 # Internationalisation file
43 require_once( 'ConfirmEdit.i18n.php' );
46 * The 'skipcaptcha' permission key can be given out to
47 * let known-good users perform triggering actions without
48 * having to go through the captcha.
50 * By default, sysops and registered bot accounts will be
51 * able to skip, while others have to go through it.
53 $wgGroupPermissions['*' ]['skipcaptcha'] = false;
54 $wgGroupPermissions['user' ]['skipcaptcha'] = false;
55 $wgGroupPermissions['autoconfirmed']['skipcaptcha'] = false;
56 $wgGroupPermissions['bot' ]['skipcaptcha'] = true; // registered bots
57 $wgGroupPermissions['sysop' ]['skipcaptcha'] = true;
59 global $wgCaptcha, $wgCaptchaClass, $wgCaptchaTriggers;
61 $wgCaptchaClass = 'SimpleCaptcha';
64 * Actions which can trigger a captcha
66 * If the 'edit' trigger is on, *every* edit will trigger the captcha.
67 * This may be useful for protecting against vandalbot attacks.
69 * If using the default 'addurl' trigger, the captcha will trigger on
70 * edits that include URLs that aren't in the current version of the page.
71 * This should catch automated linkspammers without annoying people when
72 * they make more typical edits.
74 * The captcha code should not use $wgCaptchaTriggers, but CaptchaTriggers()
75 * which also takes into account per namespace triggering.
77 $wgCaptchaTriggers = array();
78 $wgCaptchaTriggers['edit'] = false; // Would check on every edit
79 $wgCaptchaTriggers['create'] = false; // Check on page creation.
80 $wgCaptchaTriggers['addurl'] = true; // Check on edits that add URLs
81 $wgCaptchaTriggers['createaccount'] = true; // Special:Userlogin&type=signup
82 $wgCaptchaTriggers['badlogin'] = true; // Special:Userlogin after failure
85 * You may wish to apply special rules for captcha triggering on some namespaces.
86 * $wgCaptchaTriggersOnNamespace[<namespace id>][<trigger>] forces an always on /
87 * always off configuration with that trigger for the given namespace.
88 * Leave unset to use the global options ($wgCaptchaTriggers).
90 * Shall not be used with 'createaccount' (it is not checked).
92 $wgCaptchaTriggersOnNamespace = array();
95 #$wgCaptchaTriggersOnNamespace[NS_TALK]['create'] = false; //Allow creation of talk pages without captchas.
96 #$wgCaptchaTriggersOnNamespace[NS_PROJECT]['edit'] = true; //Show captcha whenever editing Project pages.
99 * Indicate how to store per-session data required to match up the
100 * internal captcha data with the editor.
102 * 'CaptchaSessionStore' uses PHP's session storage, which is cookie-based
103 * and may fail for anons with cookies disabled.
105 * 'CaptchaCacheStore' uses $wgMemc, which avoids the cookie dependency
106 * but may be fragile depending on cache configuration.
108 global $wgCaptchaStorageClass;
109 $wgCaptchaStorageClass = 'CaptchaSessionStore';
112 * Number of seconds a captcha session should last in the data cache
113 * before expiring when managing through CaptchaCacheStore class.
115 * Default is a half hour.
117 global $wgCaptchaSessionExpiration;
118 $wgCaptchaSessionExpiration = 30 * 60;
121 * Number of seconds after a bad login that a captcha will be shown to
122 * that client on the login form to slow down password-guessing bots.
124 * Has no effect if 'badlogin' is disabled in $wgCaptchaTriggers or
125 * if there is not a caching engine enabled.
127 * Default is five minutes.
129 global $wgCaptchaBadLoginExpiration;
130 $wgCaptchaBadLoginExpiration = 5 * 60;
133 * Allow users who have confirmed their e-mail addresses to post
134 * URL links without being harassed by the captcha.
136 global $ceAllowConfirmedEmail;
137 $ceAllowConfirmedEmail = false;
140 * Regex to whitelist URLs to known-good sites...
142 * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
143 * Local admins can define a whitelist under [[MediaWiki:captcha-addurl-whitelist]]
145 $wgCaptchaWhitelist = false;
148 * Additional regexes to check for. Use full regexes; can match things
149 * other than URLs such as junk edits.
151 * If the new version matches one and the old version doesn't,
152 * toss up the captcha screen.
154 * @fixme Add a message for local admins to add items as well.
156 $wgCaptchaRegexes = array();
158 /** Register special page */
159 global $wgSpecialPages;
160 $wgSpecialPages['Captcha'] = array( /*class*/ 'SpecialPage', /*name*/'Captcha', /*restriction*/ '',
161 /*listed*/ false, /*function*/ false, /*file*/ false );
164 * Set up message strings for captcha utilities.
168 global $wgMessageCache, $wgConfirmEditMessages;
169 foreach( $wgConfirmEditMessages as $lang => $messages )
170 $wgMessageCache->addMessages( $messages, $lang );
172 global $wgHooks, $wgCaptcha, $wgCaptchaClass, $wgSpecialPages;
173 $wgCaptcha = new $wgCaptchaClass();
174 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
176 $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
177 $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
179 $wgHooks['LoginAuthenticateAudit'][] = array( &$wgCaptcha, 'triggerUserLogin' );
180 $wgHooks['UserLoginForm'][] = array( &$wgCaptcha, 'injectUserLogin' );
181 $wgHooks['AbortLogin'][] = array( &$wgCaptcha, 'confirmUserLogin' );
183 global $wgGroupPermissions, $wgCaptchaTriggers;
184 if( !$wgGroupPermissions['*']['read'] && $wgCaptchaTriggers['badlogin'] ) {
185 // We need to ensure that the captcha interface is accessible
186 // so that unauthenticated users can actually get in after a
187 // mistaken password typing.
188 global $wgWhitelistRead;
189 $image = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
190 $help = Title::makeTitle( NS_SPECIAL, 'Captcha/help' );
191 $wgWhitelistRead[] = $image->getPrefixedText();
192 $wgWhitelistRead[] = $help->getPrefixedText();
197 * Entry point for Special:Captcha
199 function wfSpecialCaptcha( $par = null ) {
203 return $wgCaptcha->showImage();
206 return $wgCaptcha->showHelp();
210 class SimpleCaptcha {
211 function SimpleCaptcha() {
212 global $wgCaptchaStorageClass;
213 $this->storage = new $wgCaptchaStorageClass;
217 * Insert a captcha prompt into the edit form.
218 * This sample implementation generates a simple arithmetic operation;
219 * it would be easy to defeat by machine.
223 * @return string HTML
226 $a = mt_rand(0, 100);
228 $op = mt_rand(0, 1) ? '+' : '-';
231 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
233 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
235 return "<p><label for=\"wpCaptchaWord\">$test</label> = " .
236 wfElement( 'input', array(
237 'name' => 'wpCaptchaWord',
238 'id' => 'wpCaptchaWord',
239 'tabindex' => 1 ) ) . // tab in before the edit textarea
241 wfElement( 'input', array(
243 'name' => 'wpCaptchaId',
244 'id' => 'wpCaptchaId',
245 'value' => $index ) );
249 * Insert the captcha prompt into an edit form.
250 * @param OutputPage $out
252 function editCallback( &$out ) {
253 $out->addWikiText( $this->getMessage( $this->action ) );
254 $out->addHTML( $this->getForm() );
258 * Show a message asking the user to enter a captcha on edit
259 * The result will be treated as wiki text
261 * @param $action Action being performed
264 function getMessage( $action ) {
265 $name = 'captcha-' . $action;
266 $text = wfMsg( $name );
267 # Obtain a more tailored message, if possible, otherwise, fall back to
268 # the default for edits
269 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
274 * @fixme if multiple thingies insert a header, could break
275 * @param SimpleTemplate $template
276 * @return bool true to keep running callbacks
278 function injectUserCreate( &$template ) {
279 global $wgCaptchaTriggers, $wgOut;
280 if( $wgCaptchaTriggers['createaccount'] ) {
281 $template->set( 'header',
282 "<div class='captcha'>" .
283 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
291 * Inject a captcha into the user login form after a failed
292 * password attempt as a speedbump for mass attacks.
293 * @fixme if multiple thingies insert a header, could break
294 * @param SimpleTemplate $template
295 * @return bool true to keep running callbacks
297 function injectUserLogin( &$template ) {
298 if( $this->isBadLoginTriggered() ) {
300 $template->set( 'header',
301 "<div class='captcha'>" .
302 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
310 * When a bad login attempt is made, increment an expiring counter
311 * in the memcache cloud. Later checks for this may trigger a
312 * captcha display to prevent too many hits from the same place.
314 * @param string $password
315 * @param int $retval authentication return value
316 * @return bool true to keep running callbacks
318 function triggerUserLogin( $user, $password, $retval ) {
319 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
320 if( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
321 $key = $this->badLoginKey();
322 $count = $wgMemc->get( $key );
324 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
326 $count = $wgMemc->incr( $key );
332 * Check if a bad login has already been registered for this
333 * IP address. If so, require a captcha.
337 function isBadLoginTriggered() {
339 return intval( $wgMemc->get( $this->badLoginKey() ) ) > 0;
343 * Internal cache key for badlogin checks.
347 function badLoginKey() {
348 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
352 * Check if the submitted form matches the captcha session data provided
353 * by the plugin when the form was generated.
357 * @param WebRequest $request
361 function keyMatch( $request, $info ) {
362 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
365 // ----------------------------------
368 * @param EditPage $editPage
369 * @param string $action (edit/create/addurl...)
370 * @return bool true if action triggers captcha on editPage's namespace
372 function captchaTriggers( &$editPage, $action) {
373 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
374 //Special config for this NS?
375 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
376 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
378 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
383 * @param EditPage $editPage
384 * @param string $newtext
385 * @param string $section
386 * @return bool true if the captcha should run
388 function shouldCheck( &$editPage, $newtext, $section ) {
392 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
393 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
397 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
398 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
399 $wgUser->isEmailConfirmed() ) {
400 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
404 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
405 // Check on all edits
406 global $wgUser, $wgTitle;
407 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
409 $wgTitle->getPrefixedText() );
410 $this->action = 'edit';
411 wfDebug( "ConfirmEdit: checking all edits...\n" );
415 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
416 //Check if creating a page
417 global $wgUser, $wgTitle;
418 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
420 $wgTitle->getPrefixedText() );
421 $this->action = 'create';
422 wfDebug( "ConfirmEdit: checking on page creation...\n" );
426 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
427 // Only check edits that add URLs
428 $oldtext = $this->loadText( $editPage, $section );
430 $oldLinks = $this->findLinks( $oldtext );
431 $newLinks = $this->findLinks( $newtext );
432 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
434 $addedLinks = array_diff( $unknownLinks, $oldLinks );
435 $numLinks = count( $addedLinks );
437 if( $numLinks > 0 ) {
438 global $wgUser, $wgTitle;
439 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
442 $wgTitle->getPrefixedText(),
443 implode( ", ", $addedLinks ) );
444 $this->action = 'addurl';
449 global $wgCaptchaRegexes;
450 if( !empty( $wgCaptchaRegexes ) ) {
451 // Custom regex checks
452 $oldtext = $this->loadText( $editPage, $section );
454 foreach( $wgCaptchaRegexes as $regex ) {
455 $newMatches = array();
456 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
457 $oldMatches = array();
458 preg_match_all( $regex, $oldtext, $oldMatches );
460 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
462 $numHits = count( $addedMatches );
464 global $wgUser, $wgTitle;
465 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
469 $wgTitle->getPrefixedText(),
470 implode( ", ", $addedMatches ) );
471 $this->action = 'edit';
482 * Filter callback function for URL whitelisting
483 * @param string url to check
484 * @return bool true if unknown, false if whitelisted
487 function filterLink( $url ) {
488 global $wgCaptchaWhitelist;
489 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
491 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
493 : $this->buildRegexes( explode( "\n", $source ) );
495 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
496 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
498 return !( $cwl || $wl );
502 * Build regex from whitelist
503 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
504 * @return string Regex or bool false if whitelist is empty
507 function buildRegexes( $lines ) {
508 # Code duplicated from the SpamBlacklist extension (r19197)
510 # Strip comments and whitespace, then remove blanks
511 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
513 # No lines, don't make a regex which will match everything
514 if ( count( $lines ) == 0 ) {
515 wfDebug( "No lines\n" );
519 # It's faster using the S modifier even though it will usually only be run once
520 //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
521 //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
523 $regexStart = '/http:\/\/+[a-z0-9_\-.]*(';
527 foreach( $lines as $line ) {
528 // FIXME: not very robust size check, but should work. :)
529 if( $build === false ) {
531 } elseif( strlen( $build ) + strlen( $line ) > $regexMax ) {
532 $regexes .= $regexStart .
533 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
537 $build .= '|' . $line;
540 if( $build !== false ) {
541 $regexes .= $regexStart .
542 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
550 * The main callback run on edit attempts.
551 * @param EditPage $editPage
552 * @param string $newtext
553 * @param string $section
554 * @param bool true to continue saving, false to abort and show a captcha form
556 function confirmEdit( &$editPage, $newtext, $section ) {
557 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
558 if( $this->passCaptcha() ) {
561 $editPage->showEditForm( array( &$this, 'editCallback' ) );
565 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
571 * Hook for user creation form submissions.
573 * @param string $message
574 * @return bool true to continue, false to abort user creation
576 function confirmUserCreate( $u, &$message ) {
577 global $wgCaptchaTriggers;
578 if( $wgCaptchaTriggers['createaccount'] ) {
579 $this->trigger = "new account '" . $u->getName() . "'";
580 if( !$this->passCaptcha() ) {
581 $message = wfMsg( 'captcha-createaccount-fail' );
589 * Hook for user login form submissions.
591 * @param string $message
592 * @return bool true to continue, false to abort user creation
594 function confirmUserLogin( $u, $pass, &$retval ) {
595 if( $this->isBadLoginTriggered() ) {
596 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
597 if( !$this->passCaptcha() ) {
598 $message = wfMsg( 'captcha-badlogin-fail' );
599 // Emulate a bad-password return to confuse the shit out of attackers
600 $retval = LoginForm::WRONG_PASS;
608 * Given a required captcha run, test form input for correct
609 * input on the open session.
610 * @return bool if passed, false if failed or new session
612 function passCaptcha() {
613 $info = $this->retrieveCaptcha();
616 if( $this->keyMatch( $wgRequest, $info ) ) {
617 $this->log( "passed" );
618 $this->clearCaptcha( $info );
621 $this->clearCaptcha( $info );
622 $this->log( "bad form input" );
626 $this->log( "new captcha session" );
632 * Log the status and any triggering info for debugging or statistics
633 * @param string $message
635 function log( $message ) {
636 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
640 * Generate a captcha session ID and save the info in PHP's session storage.
641 * (Requires the user to have cookies enabled to get through the captcha.)
643 * A random ID is used so legit users can make edits in multiple tabs or
644 * windows without being unnecessarily hobbled by a serial order requirement.
645 * Pass the returned id value into the edit form as wpCaptchaId.
647 * @param array $info data to store
648 * @return string captcha ID key
650 function storeCaptcha( $info ) {
651 if( !isset( $info['index'] ) ) {
652 // Assign random index if we're not udpating
653 $info['index'] = strval( mt_rand() );
655 $this->storage->store( $info['index'], $info );
656 return $info['index'];
660 * Fetch this session's captcha info.
661 * @return mixed array of info, or false if missing
663 function retrieveCaptcha() {
665 $index = $wgRequest->getVal( 'wpCaptchaId' );
666 return $this->storage->retrieve( $index );
670 * Clear out existing captcha info from the session, to ensure
671 * it can't be reused.
673 function clearCaptcha( $info ) {
674 $this->storage->clear( $info['index'] );
678 * Retrieve the current version of the page or section being edited...
679 * @param EditPage $editPage
680 * @param string $section
684 function loadText( $editPage, $section ) {
685 $rev = Revision::newFromTitle( $editPage->mTitle );
686 if( is_null( $rev ) ) {
689 $text = $rev->getText();
690 if( $section != '' ) {
691 return Article::getSection( $text, $section );
699 * Extract a list of all recognized HTTP links in the text.
700 * @param string $text
701 * @return array of strings
703 function findLinks( $text ) {
704 global $wgParser, $wgTitle, $wgUser;
706 $options = new ParserOptions();
707 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
708 $out = $wgParser->parse( $text, $wgTitle, $options );
710 return array_keys( $out->getExternalLinks() );
714 * Show a page explaining what this wacky thing is.
716 function showHelp() {
717 global $wgOut, $ceAllowConfirmedEmail;
718 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
719 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
720 if ( $this->storage->cookiesNeeded() ) {
721 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
727 class CaptchaSessionStore {
728 function store( $index, $info ) {
729 $_SESSION['captcha' . $info['index']] = $info;
732 function retrieve( $index ) {
733 if( isset( $_SESSION['captcha' . $index] ) ) {
734 return $_SESSION['captcha' . $index];
740 function clear( $index ) {
741 unset( $_SESSION['captcha' . $index] );
744 function cookiesNeeded() {
749 class CaptchaCacheStore {
750 function store( $index, $info ) {
751 global $wgMemc, $wgCaptchaSessionExpiration;
752 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
753 $wgCaptchaSessionExpiration );
756 function retrieve( $index ) {
758 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
766 function clear( $index ) {
768 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
771 function cookiesNeeded() {
776 } # End invocation guard