3 * Experimental captcha plugin framework.
4 * Not intended as a real production captcha system; derived classes
5 * can extend the base to produce their fancy images in place of the
6 * text-based test output here.
8 * Copyright (C) 2005, 2006 Brion Vibber <brion@pobox.com>
9 * http://www.mediawiki.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
26 * @addtogroup Extensions
29 if ( defined( 'MEDIAWIKI' ) ) {
31 global $wgExtensionFunctions, $wgGroupPermissions;
33 $wgExtensionFunctions[] = 'ceSetup';
34 $wgExtensionCredits['other'][] = array(
35 'name' => 'ConfirmEdit',
36 'author' => 'Brion Vibber',
37 'url' => 'http://meta.wikimedia.org/wiki/ConfirmEdit_extension',
38 'description' => 'Simple captcha implementation',
41 # Internationalisation file
42 require_once( 'ConfirmEdit.i18n.php' );
45 * The 'skipcaptcha' permission key can be given out to
46 * let known-good users perform triggering actions without
47 * having to go through the captcha.
49 * By default, sysops and registered bot accounts will be
50 * able to skip, while others have to go through it.
52 $wgGroupPermissions['*' ]['skipcaptcha'] = false;
53 $wgGroupPermissions['user' ]['skipcaptcha'] = false;
54 $wgGroupPermissions['autoconfirmed']['skipcaptcha'] = false;
55 $wgGroupPermissions['bot' ]['skipcaptcha'] = true; // registered bots
56 $wgGroupPermissions['sysop' ]['skipcaptcha'] = true;
58 global $wgCaptcha, $wgCaptchaClass, $wgCaptchaTriggers;
60 $wgCaptchaClass = 'SimpleCaptcha';
63 * Currently the captcha works only for page edits.
65 * If the 'edit' trigger is on, *every* edit will trigger the captcha.
66 * This may be useful for protecting against vandalbot attacks.
68 * If using the default 'addurl' trigger, the captcha will trigger on
69 * edits that include URLs that aren't in the current version of the page.
70 * This should catch automated linkspammers without annoying people when
71 * they make more typical edits.
73 * The captcha code should not use $wgCaptchaTriggers, but CaptchaTriggers()
74 * which also takes into account per namespace triggering.
76 $wgCaptchaTriggers = array();
77 $wgCaptchaTriggers['edit'] = false; // Would check on every edit
78 $wgCaptchaTriggers['create'] = false; // Check on page creation.
79 $wgCaptchaTriggers['addurl'] = true; // Check on edits that add URLs
80 $wgCaptchaTriggers['createaccount'] = true; // Special:Userlogin&type=signup
83 * You may wish to apply special rules for captcha triggering on some namespaces.
84 * $wgCaptchaTriggersOnNamespace[<namespace id>][<trigger>] forces an always on /
85 * always off configuration with that trigger for the given namespace.
86 * Leave unset to use the global options ($wgCaptchaTriggers).
88 * Shall not be used with 'createaccount' (it is not checked).
90 $wgCaptchaTriggersOnNamespace = array();
93 #$wgCaptchaTriggersOnNamespace[NS_TALK]['create'] = false; //Allow creation of talk pages without captchas.
94 #$wgCaptchaTriggersOnNamespace[NS_PROJECT]['edit'] = true; //Show captcha whenever editing Project pages.
97 * Indicate how to store per-session data required to match up the
98 * internal captcha data with the editor.
100 * 'CaptchaSessionStore' uses PHP's session storage, which is cookie-based
101 * and may fail for anons with cookies disabled.
103 * 'CaptchaCacheStore' uses $wgMemc, which avoids the cookie dependency
104 * but may be fragile depending on cache configuration.
106 global $wgCaptchaStorageClass;
107 $wgCaptchaStorageClass = 'CaptchaSessionStore';
110 * Number of sections a captcha session should last in the data cache
111 * before expiring when managing through CaptchaCacheStore class.
113 * Default is a half hour.
115 global $wgCaptchaSessionExpiration;
116 $wgCaptchaSessionExpiration = 30 * 60;
119 * Allow users who have confirmed their e-mail addresses to post
120 * URL links without being harassed by the captcha.
122 global $ceAllowConfirmedEmail;
123 $ceAllowConfirmedEmail = false;
126 * Regex to whitelist URLs to known-good sites...
128 * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
129 * @fixme Use the 'spam-whitelist' thingy instead?
131 $wgCaptchaWhitelist = false;
134 * Additional regexes to check for. Use full regexes; can match things
135 * other than URLs such as junk edits.
137 * If the new version matches one and the old version doesn't,
138 * toss up the captcha screen.
140 * @fixme Add a message for local admins to add items as well.
142 $wgCaptchaRegexes = array();
144 /** Register special page */
145 global $wgSpecialPages;
146 $wgSpecialPages['Captcha'] = array( /*class*/ 'SpecialPage', /*name*/'Captcha', /*restriction*/ '',
147 /*listed*/ false, /*function*/ false, /*file*/ false );
150 * Set up message strings for captcha utilities.
154 global $wgMessageCache, $wgConfirmEditMessages;
155 foreach( $wgConfirmEditMessages as $lang => $messages )
156 $wgMessageCache->addMessages( $messages, $lang );
158 global $wgHooks, $wgCaptcha, $wgCaptchaClass, $wgSpecialPages;
159 $wgCaptcha = new $wgCaptchaClass();
160 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
162 $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
163 $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
167 * Entry point for Special:Captcha
169 function wfSpecialCaptcha( $par = null ) {
173 return $wgCaptcha->showImage();
176 return $wgCaptcha->showHelp();
180 class SimpleCaptcha {
181 function SimpleCaptcha() {
182 global $wgCaptchaStorageClass;
183 $this->storage = new $wgCaptchaStorageClass;
187 * Insert a captcha prompt into the edit form.
188 * This sample implementation generates a simple arithmetic operation;
189 * it would be easy to defeat by machine.
193 * @return string HTML
196 $a = mt_rand(0, 100);
198 $op = mt_rand(0, 1) ? '+' : '-';
201 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
203 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
205 return "<p><label for=\"wpCaptchaWord\">$test</label> = " .
206 wfElement( 'input', array(
207 'name' => 'wpCaptchaWord',
208 'id' => 'wpCaptchaWord',
209 'tabindex' => 1 ) ) . // tab in before the edit textarea
211 wfElement( 'input', array(
213 'name' => 'wpCaptchaId',
214 'id' => 'wpCaptchaId',
215 'value' => $index ) );
219 * Insert the captcha prompt into an edit form.
220 * @param OutputPage $out
222 function editCallback( &$out ) {
223 $out->addWikiText( $this->getMessage( $this->action ) );
224 $out->addHTML( $this->getForm() );
228 * Show a message asking the user to enter a captcha on edit
229 * The result will be treated as wiki text
231 * @param $action Action being performed
234 function getMessage( $action ) {
235 $name = 'captcha-' . $action;
236 $text = wfMsg( $name );
237 # Obtain a more tailored message, if possible, otherwise, fall back to
238 # the default for edits
239 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
244 * @fixme if multiple thingies insert a header, could break
245 * @param SimpleTemplate $template
246 * @return bool true to keep running callbacks
248 function injectUserCreate( &$template ) {
249 global $wgCaptchaTriggers, $wgOut;
250 if( $wgCaptchaTriggers['createaccount'] ) {
251 $template->set( 'header',
252 "<div class='captcha'>" .
253 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
261 * Check if the submitted form matches the captcha session data provided
262 * by the plugin when the form was generated.
266 * @param WebRequest $request
270 function keyMatch( $request, $info ) {
271 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
274 // ----------------------------------
277 * @param EditPage $editPage
278 * @param string $action (edit/create/addurl...)
279 * @return bool true if action triggers captcha on editPage's namespace
281 function captchaTriggers( &$editPage, $action) {
282 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
283 //Special config for this NS?
284 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
285 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
287 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
292 * @param EditPage $editPage
293 * @param string $newtext
294 * @param string $section
295 * @return bool true if the captcha should run
297 function shouldCheck( &$editPage, $newtext, $section ) {
301 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
302 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
306 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
307 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
308 $wgUser->isEmailConfirmed() ) {
309 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
313 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
314 // Check on all edits
315 global $wgUser, $wgTitle;
316 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
318 $wgTitle->getPrefixedText() );
319 $this->action = 'edit';
320 wfDebug( "ConfirmEdit: checking all edits...\n" );
324 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
325 //Check if creating a page
326 global $wgUser, $wgTitle;
327 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
329 $wgTitle->getPrefixedText() );
330 $this->action = 'create';
331 wfDebug( "ConfirmEdit: checking on page creation...\n" );
335 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
336 // Only check edits that add URLs
337 $oldtext = $this->loadText( $editPage, $section );
339 $oldLinks = $this->findLinks( $oldtext );
340 $newLinks = $this->findLinks( $newtext );
341 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
343 $addedLinks = array_diff( $unknownLinks, $oldLinks );
344 $numLinks = count( $addedLinks );
346 if( $numLinks > 0 ) {
347 global $wgUser, $wgTitle;
348 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
351 $wgTitle->getPrefixedText(),
352 implode( ", ", $addedLinks ) );
353 $this->action = 'addurl';
358 global $wgCaptchaRegexes;
359 if( !empty( $wgCaptchaRegexes ) ) {
360 // Custom regex checks
361 $oldtext = $this->loadText( $editPage, $section );
363 foreach( $wgCaptchaRegexes as $regex ) {
364 $newMatches = array();
365 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
366 $oldMatches = array();
367 preg_match_all( $regex, $oldtext, $oldMatches );
369 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
371 $numHits = count( $addedMatches );
373 global $wgUser, $wgTitle;
374 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
378 $wgTitle->getPrefixedText(),
379 implode( ", ", $addedMatches ) );
380 $this->action = 'edit';
391 * Filter callback function for URL whitelisting
392 * @return bool true if unknown, false if whitelisted
395 function filterLink( $url ) {
396 global $wgCaptchaWhitelist;
397 return !( $wgCaptchaWhitelist && preg_match( $wgCaptchaWhitelist, $url ) );
401 * The main callback run on edit attempts.
402 * @param EditPage $editPage
403 * @param string $newtext
404 * @param string $section
405 * @param bool true to continue saving, false to abort and show a captcha form
407 function confirmEdit( &$editPage, $newtext, $section ) {
408 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
409 if( $this->passCaptcha() ) {
412 $editPage->showEditForm( array( &$this, 'editCallback' ) );
416 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
422 * Hook for user creation form submissions.
424 * @param string $message
425 * @return bool true to continue, false to abort user creation
427 function confirmUserCreate( $u, &$message ) {
428 global $wgCaptchaTriggers;
429 if( $wgCaptchaTriggers['createaccount'] ) {
430 $this->trigger = "new account '" . $u->getName() . "'";
431 if( !$this->passCaptcha() ) {
432 $message = wfMsg( 'captcha-createaccount-fail' );
440 * Given a required captcha run, test form input for correct
441 * input on the open session.
442 * @return bool if passed, false if failed or new session
444 function passCaptcha() {
445 $info = $this->retrieveCaptcha();
448 if( $this->keyMatch( $wgRequest, $info ) ) {
449 $this->log( "passed" );
450 $this->clearCaptcha( $info );
453 $this->clearCaptcha( $info );
454 $this->log( "bad form input" );
458 $this->log( "new captcha session" );
464 * Log the status and any triggering info for debugging or statistics
465 * @param string $message
467 function log( $message ) {
468 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
472 * Generate a captcha session ID and save the info in PHP's session storage.
473 * (Requires the user to have cookies enabled to get through the captcha.)
475 * A random ID is used so legit users can make edits in multiple tabs or
476 * windows without being unnecessarily hobbled by a serial order requirement.
477 * Pass the returned id value into the edit form as wpCaptchaId.
479 * @param array $info data to store
480 * @return string captcha ID key
482 function storeCaptcha( $info ) {
483 if( !isset( $info['index'] ) ) {
484 // Assign random index if we're not udpating
485 $info['index'] = strval( mt_rand() );
487 $this->storage->store( $info['index'], $info );
488 return $info['index'];
492 * Fetch this session's captcha info.
493 * @return mixed array of info, or false if missing
495 function retrieveCaptcha() {
497 $index = $wgRequest->getVal( 'wpCaptchaId' );
498 return $this->storage->retrieve( $index );
502 * Clear out existing captcha info from the session, to ensure
503 * it can't be reused.
505 function clearCaptcha( $info ) {
506 $this->storage->clear( $info['index'] );
510 * Retrieve the current version of the page or section being edited...
511 * @param EditPage $editPage
512 * @param string $section
516 function loadText( $editPage, $section ) {
517 $rev = Revision::newFromTitle( $editPage->mTitle );
518 if( is_null( $rev ) ) {
521 $text = $rev->getText();
522 if( $section != '' ) {
523 return Article::getSection( $text, $section );
531 * Extract a list of all recognized HTTP links in the text.
532 * @param string $text
533 * @return array of strings
535 function findLinks( $text ) {
536 global $wgParser, $wgTitle, $wgUser;
538 $options = new ParserOptions();
539 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
540 $out = $wgParser->parse( $text, $wgTitle, $options );
542 return array_keys( $out->getExternalLinks() );
546 * Show a page explaining what this wacky thing is.
548 function showHelp() {
549 global $wgOut, $ceAllowConfirmedEmail;
550 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
551 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
556 class CaptchaSessionStore {
557 function store( $index, $info ) {
558 $_SESSION['captcha' . $info['index']] = $info;
561 function retrieve( $index ) {
562 if( isset( $_SESSION['captcha' . $index] ) ) {
563 return $_SESSION['captcha' . $index];
569 function clear( $index ) {
570 unset( $_SESSION['captcha' . $index] );
574 class CaptchaCacheStore {
575 function store( $index, $info ) {
576 global $wgMemc, $wgCaptchaSessionExpiration;
577 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
578 $wgCaptchaSessionExpiration );
581 function retrieve( $index ) {
583 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
591 function clear( $index ) {
593 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
597 } # End invocation guard