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;
60 * List of IP ranges to allow to skip the captcha, similar to the group setting:
61 * "$wgGroupPermission[...]['skipcaptcha'] = true"
63 * Specific IP addresses or CIDR-style ranges may be used,
65 * $wgCaptchaWhitelistIP = array('192.168.1.0/24', '10.1.0.0/16');
67 $wgCaptchaWhitelistIP = false;
69 global $wgCaptcha, $wgCaptchaClass, $wgCaptchaTriggers;
71 $wgCaptchaClass = 'SimpleCaptcha';
74 * Actions which can trigger a captcha
76 * If the 'edit' trigger is on, *every* edit will trigger the captcha.
77 * This may be useful for protecting against vandalbot attacks.
79 * If using the default 'addurl' trigger, the captcha will trigger on
80 * edits that include URLs that aren't in the current version of the page.
81 * This should catch automated linkspammers without annoying people when
82 * they make more typical edits.
84 * The captcha code should not use $wgCaptchaTriggers, but CaptchaTriggers()
85 * which also takes into account per namespace triggering.
87 $wgCaptchaTriggers = array();
88 $wgCaptchaTriggers['edit'] = false; // Would check on every edit
89 $wgCaptchaTriggers['create'] = false; // Check on page creation.
90 $wgCaptchaTriggers['addurl'] = true; // Check on edits that add URLs
91 $wgCaptchaTriggers['createaccount'] = true; // Special:Userlogin&type=signup
92 $wgCaptchaTriggers['badlogin'] = true; // Special:Userlogin after failure
95 * You may wish to apply special rules for captcha triggering on some namespaces.
96 * $wgCaptchaTriggersOnNamespace[<namespace id>][<trigger>] forces an always on /
97 * always off configuration with that trigger for the given namespace.
98 * Leave unset to use the global options ($wgCaptchaTriggers).
100 * Shall not be used with 'createaccount' (it is not checked).
102 $wgCaptchaTriggersOnNamespace = array();
105 #$wgCaptchaTriggersOnNamespace[NS_TALK]['create'] = false; //Allow creation of talk pages without captchas.
106 #$wgCaptchaTriggersOnNamespace[NS_PROJECT]['edit'] = true; //Show captcha whenever editing Project pages.
109 * Indicate how to store per-session data required to match up the
110 * internal captcha data with the editor.
112 * 'CaptchaSessionStore' uses PHP's session storage, which is cookie-based
113 * and may fail for anons with cookies disabled.
115 * 'CaptchaCacheStore' uses $wgMemc, which avoids the cookie dependency
116 * but may be fragile depending on cache configuration.
118 global $wgCaptchaStorageClass;
119 $wgCaptchaStorageClass = 'CaptchaSessionStore';
122 * Number of seconds a captcha session should last in the data cache
123 * before expiring when managing through CaptchaCacheStore class.
125 * Default is a half hour.
127 global $wgCaptchaSessionExpiration;
128 $wgCaptchaSessionExpiration = 30 * 60;
131 * Number of seconds after a bad login that a captcha will be shown to
132 * that client on the login form to slow down password-guessing bots.
134 * Has no effect if 'badlogin' is disabled in $wgCaptchaTriggers or
135 * if there is not a caching engine enabled.
137 * Default is five minutes.
139 global $wgCaptchaBadLoginExpiration;
140 $wgCaptchaBadLoginExpiration = 5 * 60;
143 * Allow users who have confirmed their e-mail addresses to post
144 * URL links without being harassed by the captcha.
146 global $ceAllowConfirmedEmail;
147 $ceAllowConfirmedEmail = false;
150 * Regex to whitelist URLs to known-good sites...
152 * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
153 * Local admins can define a whitelist under [[MediaWiki:captcha-addurl-whitelist]]
155 $wgCaptchaWhitelist = false;
158 * Additional regexes to check for. Use full regexes; can match things
159 * other than URLs such as junk edits.
161 * If the new version matches one and the old version doesn't,
162 * toss up the captcha screen.
164 * @fixme Add a message for local admins to add items as well.
166 $wgCaptchaRegexes = array();
168 /** Register special page */
169 global $wgSpecialPages;
170 $wgSpecialPages['Captcha'] = array( /*class*/ 'SpecialPage', /*name*/'Captcha', /*restriction*/ '',
171 /*listed*/ false, /*function*/ false, /*file*/ false );
174 * Set up message strings for captcha utilities.
178 global $wgMessageCache, $wgConfirmEditMessages;
179 foreach( $wgConfirmEditMessages as $lang => $messages )
180 $wgMessageCache->addMessages( $messages, $lang );
182 global $wgHooks, $wgCaptcha, $wgCaptchaClass, $wgSpecialPages;
183 $wgCaptcha = new $wgCaptchaClass();
184 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
186 $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
187 $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
189 $wgHooks['LoginAuthenticateAudit'][] = array( &$wgCaptcha, 'triggerUserLogin' );
190 $wgHooks['UserLoginForm'][] = array( &$wgCaptcha, 'injectUserLogin' );
191 $wgHooks['AbortLogin'][] = array( &$wgCaptcha, 'confirmUserLogin' );
193 global $wgGroupPermissions, $wgCaptchaTriggers;
194 if( !$wgGroupPermissions['*']['read'] && $wgCaptchaTriggers['badlogin'] ) {
195 // We need to ensure that the captcha interface is accessible
196 // so that unauthenticated users can actually get in after a
197 // mistaken password typing.
198 global $wgWhitelistRead;
199 $image = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
200 $help = Title::makeTitle( NS_SPECIAL, 'Captcha/help' );
201 $wgWhitelistRead[] = $image->getPrefixedText();
202 $wgWhitelistRead[] = $help->getPrefixedText();
207 * Entry point for Special:Captcha
209 function wfSpecialCaptcha( $par = null ) {
213 return $wgCaptcha->showImage();
216 return $wgCaptcha->showHelp();
220 class SimpleCaptcha {
221 function SimpleCaptcha() {
222 global $wgCaptchaStorageClass;
223 $this->storage = new $wgCaptchaStorageClass;
227 * Insert a captcha prompt into the edit form.
228 * This sample implementation generates a simple arithmetic operation;
229 * it would be easy to defeat by machine.
233 * @return string HTML
236 $a = mt_rand(0, 100);
238 $op = mt_rand(0, 1) ? '+' : '-';
241 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
243 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
245 return "<p><label for=\"wpCaptchaWord\">$test</label> = " .
246 wfElement( 'input', array(
247 'name' => 'wpCaptchaWord',
248 'id' => 'wpCaptchaWord',
249 'tabindex' => 1 ) ) . // tab in before the edit textarea
251 wfElement( 'input', array(
253 'name' => 'wpCaptchaId',
254 'id' => 'wpCaptchaId',
255 'value' => $index ) );
259 * Insert the captcha prompt into an edit form.
260 * @param OutputPage $out
262 function editCallback( &$out ) {
263 $out->addWikiText( $this->getMessage( $this->action ) );
264 $out->addHTML( $this->getForm() );
268 * Show a message asking the user to enter a captcha on edit
269 * The result will be treated as wiki text
271 * @param $action Action being performed
274 function getMessage( $action ) {
275 $name = 'captcha-' . $action;
276 $text = wfMsg( $name );
277 # Obtain a more tailored message, if possible, otherwise, fall back to
278 # the default for edits
279 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
284 * @fixme if multiple thingies insert a header, could break
285 * @param SimpleTemplate $template
286 * @return bool true to keep running callbacks
288 function injectUserCreate( &$template ) {
289 global $wgCaptchaTriggers, $wgOut;
290 if( $wgCaptchaTriggers['createaccount'] ) {
291 $template->set( 'header',
292 "<div class='captcha'>" .
293 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
301 * Inject a captcha into the user login form after a failed
302 * password attempt as a speedbump for mass attacks.
303 * @fixme if multiple thingies insert a header, could break
304 * @param SimpleTemplate $template
305 * @return bool true to keep running callbacks
307 function injectUserLogin( &$template ) {
308 if( $this->isBadLoginTriggered() ) {
310 $template->set( 'header',
311 "<div class='captcha'>" .
312 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
320 * When a bad login attempt is made, increment an expiring counter
321 * in the memcache cloud. Later checks for this may trigger a
322 * captcha display to prevent too many hits from the same place.
324 * @param string $password
325 * @param int $retval authentication return value
326 * @return bool true to keep running callbacks
328 function triggerUserLogin( $user, $password, $retval ) {
329 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
330 if( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
331 $key = $this->badLoginKey();
332 $count = $wgMemc->get( $key );
334 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
336 $count = $wgMemc->incr( $key );
342 * Check if a bad login has already been registered for this
343 * IP address. If so, require a captcha.
347 function isBadLoginTriggered() {
349 return intval( $wgMemc->get( $this->badLoginKey() ) ) > 0;
353 * Internal cache key for badlogin checks.
357 function badLoginKey() {
358 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
362 * Check if the submitted form matches the captcha session data provided
363 * by the plugin when the form was generated.
367 * @param WebRequest $request
371 function keyMatch( $request, $info ) {
372 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
375 // ----------------------------------
378 * @param EditPage $editPage
379 * @param string $action (edit/create/addurl...)
380 * @return bool true if action triggers captcha on editPage's namespace
382 function captchaTriggers( &$editPage, $action) {
383 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
384 //Special config for this NS?
385 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
386 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
388 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
393 * @param EditPage $editPage
394 * @param string $newtext
395 * @param string $section
396 * @return bool true if the captcha should run
398 function shouldCheck( &$editPage, $newtext, $section ) {
402 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
403 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
406 global $wgCaptchaWhitelistIP;
407 if( !empty( $wgCaptchaWhitelistIP ) ) {
409 foreach ( $wgCaptchaWhitelistIP as $range ) {
410 if ( IP::isInRange( $ip, $range ) ) {
417 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
418 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
419 $wgUser->isEmailConfirmed() ) {
420 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
424 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
425 // Check on all edits
426 global $wgUser, $wgTitle;
427 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
429 $wgTitle->getPrefixedText() );
430 $this->action = 'edit';
431 wfDebug( "ConfirmEdit: checking all edits...\n" );
435 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
436 //Check if creating a page
437 global $wgUser, $wgTitle;
438 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
440 $wgTitle->getPrefixedText() );
441 $this->action = 'create';
442 wfDebug( "ConfirmEdit: checking on page creation...\n" );
446 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
447 // Only check edits that add URLs
448 $oldtext = $this->loadText( $editPage, $section );
450 $oldLinks = $this->findLinks( $oldtext );
451 $newLinks = $this->findLinks( $newtext );
452 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
454 $addedLinks = array_diff( $unknownLinks, $oldLinks );
455 $numLinks = count( $addedLinks );
457 if( $numLinks > 0 ) {
458 global $wgUser, $wgTitle;
459 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
462 $wgTitle->getPrefixedText(),
463 implode( ", ", $addedLinks ) );
464 $this->action = 'addurl';
469 global $wgCaptchaRegexes;
470 if( !empty( $wgCaptchaRegexes ) ) {
471 // Custom regex checks
472 $oldtext = $this->loadText( $editPage, $section );
474 foreach( $wgCaptchaRegexes as $regex ) {
475 $newMatches = array();
476 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
477 $oldMatches = array();
478 preg_match_all( $regex, $oldtext, $oldMatches );
480 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
482 $numHits = count( $addedMatches );
484 global $wgUser, $wgTitle;
485 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
489 $wgTitle->getPrefixedText(),
490 implode( ", ", $addedMatches ) );
491 $this->action = 'edit';
502 * Filter callback function for URL whitelisting
503 * @param string url to check
504 * @return bool true if unknown, false if whitelisted
507 function filterLink( $url ) {
508 global $wgCaptchaWhitelist;
509 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
511 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
513 : $this->buildRegexes( explode( "\n", $source ) );
515 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
516 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
518 return !( $cwl || $wl );
522 * Build regex from whitelist
523 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
524 * @return string Regex or bool false if whitelist is empty
527 function buildRegexes( $lines ) {
528 # Code duplicated from the SpamBlacklist extension (r19197)
530 # Strip comments and whitespace, then remove blanks
531 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
533 # No lines, don't make a regex which will match everything
534 if ( count( $lines ) == 0 ) {
535 wfDebug( "No lines\n" );
539 # It's faster using the S modifier even though it will usually only be run once
540 //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
541 //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
543 $regexStart = '/http:\/\/+[a-z0-9_\-.]*(';
547 foreach( $lines as $line ) {
548 // FIXME: not very robust size check, but should work. :)
549 if( $build === false ) {
551 } elseif( strlen( $build ) + strlen( $line ) > $regexMax ) {
552 $regexes .= $regexStart .
553 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
557 $build .= '|' . $line;
560 if( $build !== false ) {
561 $regexes .= $regexStart .
562 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
570 * The main callback run on edit attempts.
571 * @param EditPage $editPage
572 * @param string $newtext
573 * @param string $section
574 * @param bool true to continue saving, false to abort and show a captcha form
576 function confirmEdit( &$editPage, $newtext, $section ) {
577 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
578 if( $this->passCaptcha() ) {
581 $editPage->showEditForm( array( &$this, 'editCallback' ) );
585 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
591 * Hook for user creation form submissions.
593 * @param string $message
594 * @return bool true to continue, false to abort user creation
596 function confirmUserCreate( $u, &$message ) {
597 global $wgCaptchaTriggers;
598 if( $wgCaptchaTriggers['createaccount'] ) {
599 $this->trigger = "new account '" . $u->getName() . "'";
600 if( !$this->passCaptcha() ) {
601 $message = wfMsg( 'captcha-createaccount-fail' );
609 * Hook for user login form submissions.
611 * @param string $message
612 * @return bool true to continue, false to abort user creation
614 function confirmUserLogin( $u, $pass, &$retval ) {
615 if( $this->isBadLoginTriggered() ) {
616 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
617 if( !$this->passCaptcha() ) {
618 $message = wfMsg( 'captcha-badlogin-fail' );
619 // Emulate a bad-password return to confuse the shit out of attackers
620 $retval = LoginForm::WRONG_PASS;
628 * Given a required captcha run, test form input for correct
629 * input on the open session.
630 * @return bool if passed, false if failed or new session
632 function passCaptcha() {
633 $info = $this->retrieveCaptcha();
636 if( $this->keyMatch( $wgRequest, $info ) ) {
637 $this->log( "passed" );
638 $this->clearCaptcha( $info );
641 $this->clearCaptcha( $info );
642 $this->log( "bad form input" );
646 $this->log( "new captcha session" );
652 * Log the status and any triggering info for debugging or statistics
653 * @param string $message
655 function log( $message ) {
656 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
660 * Generate a captcha session ID and save the info in PHP's session storage.
661 * (Requires the user to have cookies enabled to get through the captcha.)
663 * A random ID is used so legit users can make edits in multiple tabs or
664 * windows without being unnecessarily hobbled by a serial order requirement.
665 * Pass the returned id value into the edit form as wpCaptchaId.
667 * @param array $info data to store
668 * @return string captcha ID key
670 function storeCaptcha( $info ) {
671 if( !isset( $info['index'] ) ) {
672 // Assign random index if we're not udpating
673 $info['index'] = strval( mt_rand() );
675 $this->storage->store( $info['index'], $info );
676 return $info['index'];
680 * Fetch this session's captcha info.
681 * @return mixed array of info, or false if missing
683 function retrieveCaptcha() {
685 $index = $wgRequest->getVal( 'wpCaptchaId' );
686 return $this->storage->retrieve( $index );
690 * Clear out existing captcha info from the session, to ensure
691 * it can't be reused.
693 function clearCaptcha( $info ) {
694 $this->storage->clear( $info['index'] );
698 * Retrieve the current version of the page or section being edited...
699 * @param EditPage $editPage
700 * @param string $section
704 function loadText( $editPage, $section ) {
705 $rev = Revision::newFromTitle( $editPage->mTitle );
706 if( is_null( $rev ) ) {
709 $text = $rev->getText();
710 if( $section != '' ) {
711 return Article::getSection( $text, $section );
719 * Extract a list of all recognized HTTP links in the text.
720 * @param string $text
721 * @return array of strings
723 function findLinks( $text ) {
724 global $wgParser, $wgTitle, $wgUser;
726 $options = new ParserOptions();
727 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
728 $out = $wgParser->parse( $text, $wgTitle, $options );
730 return array_keys( $out->getExternalLinks() );
734 * Show a page explaining what this wacky thing is.
736 function showHelp() {
737 global $wgOut, $ceAllowConfirmedEmail;
738 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
739 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
740 if ( $this->storage->cookiesNeeded() ) {
741 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
747 class CaptchaSessionStore {
748 function store( $index, $info ) {
749 $_SESSION['captcha' . $info['index']] = $info;
752 function retrieve( $index ) {
753 if( isset( $_SESSION['captcha' . $index] ) ) {
754 return $_SESSION['captcha' . $index];
760 function clear( $index ) {
761 unset( $_SESSION['captcha' . $index] );
764 function cookiesNeeded() {
769 class CaptchaCacheStore {
770 function store( $index, $info ) {
771 global $wgMemc, $wgCaptchaSessionExpiration;
772 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
773 $wgCaptchaSessionExpiration );
776 function retrieve( $index ) {
778 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
786 function clear( $index ) {
788 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
791 function cookiesNeeded() {
796 } # End invocation guard