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
27 * @subpackage Extensions
30 if ( defined( 'MEDIAWIKI' ) ) {
32 global $wgExtensionFunctions, $wgGroupPermissions;
34 $wgExtensionFunctions[] = 'ceSetup';
37 * The 'skipcaptcha' permission key can be given out to
38 * let known-good users perform triggering actions without
39 * having to go through the captcha.
41 * By default, sysops and registered bot accounts will be
42 * able to skip, while others have to go through it.
44 $wgGroupPermissions['*' ]['skipcaptcha'] = false;
45 $wgGroupPermissions['user' ]['skipcaptcha'] = false;
46 $wgGroupPermissions['autoconfirmed']['skipcaptcha'] = false;
47 $wgGroupPermissions['bot' ]['skipcaptcha'] = true; // registered bots
48 $wgGroupPermissions['sysop' ]['skipcaptcha'] = true;
50 global $wgCaptcha, $wgCaptchaClass, $wgCaptchaTriggers;
52 $wgCaptchaClass = 'SimpleCaptcha';
55 * Currently the captcha works only for page edits.
57 * If the 'edit' trigger is on, *every* edit will trigger the captcha.
58 * This may be useful for protecting against vandalbot attacks.
60 * If using the default 'addurl' trigger, the captcha will trigger on
61 * edits that include URLs that aren't in the current version of the page.
62 * This should catch automated linkspammers without annoying people when
63 * they make more typical edits.
65 $wgCaptchaTriggers = array();
66 $wgCaptchaTriggers['edit'] = false; // Would check on every edit
67 $wgCaptchaTriggers['addurl'] = true; // Check on edits that add URLs
68 $wgCaptchaTriggers['createaccount'] = true; // Special:Userlogin&type=signup
72 * Allow users who have confirmed their e-mail addresses to post
73 * URL links without being harassed by the captcha.
75 global $ceAllowConfirmedEmail;
76 $ceAllowConfirmedEmail = false;
79 * Regex to whitelist URLs to known-good sites...
81 * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
82 * @fixme Use the 'spam-whitelist' thingy instead?
84 $wgCaptchaWhitelist = false;
87 * Additional regexes to check for. Use full regexes; can match things
88 * other than URLs such as junk edits.
90 * If the new version matches one and the old version doesn't,
91 * toss up the captcha screen.
93 * @fixme Add a message for local admins to add items as well.
95 $wgCaptchaRegexes = array();
98 * Set up message strings for captcha utilities.
101 global $wgMessageCache, $wgHooks, $wgCaptcha, $wgCaptchaClass, $wgSpecialPages;
102 $wgMessageCache->addMessages( array(
104 "Your edit includes new URL links; as a protection against automated " .
105 "spam, you'll need to type in the words that appear in this image:\n" .
106 "<br />([[Special:Captcha/help|What is this?]])",
107 'captchahelp-title' =>
109 'captchahelp-text' =>
110 "Web sites that accept postings from the public, like this wiki, " .
111 "are often abused by spammers who use automated tools to post their " .
112 "links to many sites. While these spam links can be removed, they " .
113 "are a significant nuisance." .
115 "Sometimes, especially when adding new web links to a page, " .
116 "the wiki may show you an image of colored or distorted text and " .
117 "ask you to type the words shown. Since this is a task that's hard " .
118 "to automate, it will allow most real humans to make their posts " .
119 "while stopping most spammers and other robotic attackers." .
121 "Unfortunately this may inconvenience users with limited vision or " .
122 "using text-based or speech-based browsers. At the moment we do not " .
123 "have an audio alternative available. Please contact the site " .
124 "administrators for assistance if this is unexpectedly preventing " .
125 "you from making legitimate posts." .
127 "Hit the 'back' button in your browser to return to the page editor.",
128 'captcha-createaccount' =>
129 "As a protection against automated spam, you'll need to type in the " .
130 "words that appear in this image to register an account:\n" .
131 "<br />([[Special:Captcha/help|What is this?]])",
132 'captcha-createaccount-fail' =>
133 "Incorrect or missing confirmation code." ) );
135 $wgSpecialPages['Captcha'] = array( /*class*/ 'SpecialPage', /*name*/'Captcha', false,
136 /*listed*/ false, /*function*/ false, /*file*/ false );
138 $wgCaptcha = new $wgCaptchaClass();
139 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
141 $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
142 $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
146 * Entry point for Special:Captcha
148 function wfSpecialCaptcha( $par = null ) {
152 return $wgCaptcha->showImage();
155 return $wgCaptcha->showHelp();
159 class SimpleCaptcha {
161 * Insert a captcha prompt into the edit form.
162 * This sample implementation generates a simple arithmetic operation;
163 * it would be easy to defeat by machine.
167 * @return string HTML
170 $a = mt_rand(0, 100);
172 $op = mt_rand(0, 1) ? '+' : '-';
175 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
177 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
179 return "<p><label for=\"wpCaptchaWord\">$test</label> = " .
180 wfElement( 'input', array(
181 'name' => 'wpCaptchaWord',
182 'id' => 'wpCaptchaWord',
183 'tabindex' => 1 ) ) . // tab in before the edit textarea
185 wfElement( 'input', array(
187 'name' => 'wpCaptchaId',
188 'id' => 'wpCaptchaId',
189 'value' => $index ) );
193 * Insert the captcha prompt into an edit form.
194 * @param OutputPage $out
196 function editCallback( &$out ) {
197 $out->addWikiText( wfMsg( "captcha-short" ) );
198 $out->addHTML( $this->getForm() );
203 * @fixme if multiple thingies insert a header, could break
204 * @param SimpleTemplate $template
205 * @return bool true to keep running callbacks
207 function injectUserCreate( &$template ) {
208 global $wgCaptchaTriggers, $wgOut;
209 if( $wgCaptchaTriggers['createaccount'] ) {
210 $template->set( 'header',
211 "<div class='captcha'>" .
212 $wgOut->parse( wfMsg( 'captcha-createaccount' ) ) .
220 * Check if the submitted form matches the captcha session data provided
221 * by the plugin when the form was generated.
225 * @param WebRequest $request
229 function keyMatch( $request, $info ) {
230 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
233 // ----------------------------------
236 * @param EditPage $editPage
237 * @param string $newtext
238 * @param string $section
239 * @return bool true if the captcha should run
241 function shouldCheck( &$editPage, $newtext, $section ) {
245 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
246 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
250 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
251 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
252 $wgUser->isEmailConfirmed() ) {
253 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
257 global $wgCaptchaTriggers;
258 if( !empty( $wgCaptchaTriggers['edit'] ) ) {
259 // Check on all edits
260 global $wgUser, $wgTitle;
261 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
263 $wgTitle->getPrefixedText() );
264 wfDebug( "ConfirmEdit: checking all edits...\n" );
268 if( !empty( $wgCaptchaTriggers['addurl'] ) ) {
269 // Only check edits that add URLs
270 $oldtext = $this->loadText( $editPage, $section );
272 $oldLinks = $this->findLinks( $oldtext );
273 $newLinks = $this->findLinks( $newtext );
274 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
276 $addedLinks = array_diff( $unknownLinks, $oldLinks );
277 $numLinks = count( $addedLinks );
279 if( $numLinks > 0 ) {
280 global $wgUser, $wgTitle;
281 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
284 $wgTitle->getPrefixedText(),
285 implode( ", ", $addedLinks ) );
290 global $wgCaptchaRegexes;
291 if( !empty( $wgCaptchaRegexes ) ) {
292 // Custom regex checks
293 $oldtext = $this->loadText( $editPage, $section );
295 foreach( $wgCaptchaRegexes as $regex ) {
296 $newMatches = array();
297 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
298 $oldMatches = array();
299 preg_match_all( $regex, $oldtext, $oldMatches );
301 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
303 $numHits = count( $addedMatches );
305 global $wgUser, $wgTitle;
306 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
310 $wgTitle->getPrefixedText(),
311 implode( ", ", $addedMatches ) );
322 * Filter callback function for URL whitelisting
323 * @return bool true if unknown, false if whitelisted
326 function filterLink( $url ) {
327 global $wgCaptchaWhitelist;
328 return !( $wgCaptchaWhitelist && preg_match( $wgCaptchaWhitelist, $url ) );
332 * The main callback run on edit attempts.
333 * @param EditPage $editPage
334 * @param string $newtext
335 * @param string $section
336 * @param bool true to continue saving, false to abort and show a captcha form
338 function confirmEdit( &$editPage, $newtext, $section ) {
339 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
340 if( $this->passCaptcha() ) {
343 $editPage->showEditForm( array( &$this, 'editCallback' ) );
347 wfDebug( "ConfirmEdit: no new links.\n" );
353 * Hook for user creation form submissions.
355 * @param string $message
356 * @return bool true to continue, false to abort user creation
358 function confirmUserCreate( $u, &$message ) {
359 global $wgCaptchaTriggers;
360 if( $wgCaptchaTriggers['createaccount'] ) {
361 $this->trigger = "new account '" . $u->getName() . "'";
362 if( !$this->passCaptcha() ) {
363 $message = wfMsg( 'captcha-createaccount-fail' );
371 * Given a required captcha run, test form input for correct
372 * input on the open session.
373 * @return bool if passed, false if failed or new session
375 function passCaptcha() {
376 $info = $this->retrieveCaptcha();
379 if( $this->keyMatch( $wgRequest, $info ) ) {
380 $this->log( "passed" );
381 $this->clearCaptcha( $info );
384 $this->clearCaptcha( $info );
385 $this->log( "bad form input" );
389 $this->log( "new captcha session" );
395 * Log the status and any triggering info for debugging or statistics
396 * @param string $message
398 function log( $message ) {
399 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
403 * Generate a captcha session ID and save the info in PHP's session storage.
404 * (Requires the user to have cookies enabled to get through the captcha.)
406 * A random ID is used so legit users can make edits in multiple tabs or
407 * windows without being unnecessarily hobbled by a serial order requirement.
408 * Pass the returned id value into the edit form as wpCaptchaId.
410 * @param array $info data to store
411 * @return string captcha ID key
413 function storeCaptcha( $info ) {
414 if( !isset( $info['index'] ) ) {
415 // Assign random index if we're not udpating
416 $info['index'] = strval( mt_rand() );
418 $_SESSION['captcha' . $info['index']] = $info;
419 return $info['index'];
423 * Fetch this session's captcha info.
424 * @return mixed array of info, or false if missing
426 function retrieveCaptcha() {
428 $index = $wgRequest->getVal( 'wpCaptchaId' );
429 if( isset( $_SESSION['captcha' . $index] ) ) {
430 return $_SESSION['captcha' . $index];
437 * Clear out existing captcha info from the session, to ensure
438 * it can't be reused.
440 function clearCaptcha( $info ) {
441 unset( $_SESSION['captcha' . $info['index']] );
445 * Retrieve the current version of the page or section being edited...
446 * @param EditPage $editPage
447 * @param string $section
451 function loadText( $editPage, $section ) {
452 $rev = Revision::newFromTitle( $editPage->mTitle );
453 if( is_null( $rev ) ) {
456 $text = $rev->getText();
457 if( $section != '' ) {
458 return Article::getSection( $text, $section );
466 * Extract a list of all recognized HTTP links in the text.
467 * @param string $text
468 * @return array of strings
470 function findLinks( $text ) {
471 $regex = '/((?:' . HTTP_PROTOCOLS . ')' . EXT_LINK_URL_CLASS . '+)/';
473 if( preg_match_all( $regex, $text, $matches, PREG_PATTERN_ORDER ) ) {
481 * Show a page explaining what this wacky thing is.
483 function showHelp() {
484 global $wgOut, $ceAllowConfirmedEmail;
485 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
486 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
491 } # End invocation guard