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';
83 $wgCaptchaWhitelist = false;
86 * Set up message strings for captcha utilities.
89 global $wgMessageCache, $wgHooks, $wgCaptcha, $wgCaptchaClass;
90 $wgMessageCache->addMessages( array(
92 "Your edit includes new URL links; as a protection against automated " .
93 "spam, you'll need to type in the words that appear in this image:\n" .
94 "<br />([[Special:Captcha/help|What is this?]])",
95 'captchahelp-title' =>
98 "Web sites that accept postings from the public, like this wiki, " .
99 "are often abused by spammers who use automated tools to post their " .
100 "links to many sites. While these spam links can be removed, they " .
101 "are a significant nuisance." .
103 "Sometimes, especially when adding new web links to a page, " .
104 "the wiki may show you an image of colored or distorted text and " .
105 "ask you to type the words shown. Since this is a task that's hard " .
106 "to automate, it will allow most real humans to make their posts " .
107 "while stopping most spammers and other robotic attackers." .
109 "Unfortunately this may inconvenience users with limited vision or " .
110 "using text-based or speech-based browsers. At the moment we do not " .
111 "have an audio alternative available. Please contact the site " .
112 "administrators for assistance if this is unexpectedly preventing " .
113 "you from making legitimate posts." .
115 "Hit the 'back' button in your browser to return to the page editor.",
116 'captcha-createaccount' =>
117 "As a protection against automated spam, you'll need to type in the " .
118 "words that appear in this image to register an account:\n" .
119 "<br />([[Special:Captcha/help|What is this?]])",
120 'captcha-createaccount-fail' =>
121 "Incorrect or missing confirmation code." ) );
123 SpecialPage::addPage( new SpecialPage( 'Captcha', false,
124 /*listed*/ false, /*function*/ false, /*file*/ false ) );
126 $wgCaptcha = new $wgCaptchaClass();
127 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
129 $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
130 $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
134 * Entry point for Special:Captcha
136 function wfSpecialCaptcha( $par = null ) {
140 return $wgCaptcha->showImage();
143 return $wgCaptcha->showHelp();
147 class SimpleCaptcha {
149 * Insert a captcha prompt into the edit form.
150 * This sample implementation generates a simple arithmetic operation;
151 * it would be easy to defeat by machine.
155 * @return string HTML
158 $a = mt_rand(0, 100);
160 $op = mt_rand(0, 1) ? '+' : '-';
163 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
165 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
167 return "<p><label for=\"wpCaptchaWord\">$test</label> = " .
168 wfElement( 'input', array(
169 'name' => 'wpCaptchaWord',
170 'id' => 'wpCaptchaWord',
171 'tabindex' => 1 ) ) . // tab in before the edit textarea
173 wfElement( 'input', array(
175 'name' => 'wpCaptchaId',
176 'id' => 'wpCaptchaId',
177 'value' => $index ) );
181 * Insert the captcha prompt into an edit form.
182 * @param OutputPage $out
184 function editCallback( &$out ) {
185 $out->addWikiText( wfMsg( "captcha-short" ) );
186 $out->addHTML( $this->getForm() );
191 * @fixme if multiple thingies insert a header, could break
192 * @param SimpleTemplate $template
193 * @return bool true to keep running callbacks
195 function injectUserCreate( &$template ) {
196 global $wgCaptchaTriggers, $wgOut;
197 if( $wgCaptchaTriggers['createaccount'] ) {
198 $template->set( 'header',
199 "<div class='captcha'>" .
200 $wgOut->parse( wfMsg( 'captcha-createaccount' ) ) .
208 * Check if the submitted form matches the captcha session data provided
209 * by the plugin when the form was generated.
213 * @param WebRequest $request
217 function keyMatch( $request, $info ) {
218 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
221 // ----------------------------------
224 * @param EditPage $editPage
225 * @param string $newtext
226 * @param string $section
227 * @return bool true if the captcha should run
229 function shouldCheck( &$editPage, $newtext, $section ) {
233 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
234 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
238 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
239 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
240 $wgUser->isEmailConfirmed() ) {
241 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
245 global $wgCaptchaTriggers;
246 if( !empty( $wgCaptchaTriggers['edit'] ) ) {
247 // Check on all edits
248 wfDebug( "ConfirmEdit: checking all edits...\n" );
252 if( !empty( $wgCaptchaTriggers['addurl'] ) ) {
253 // Only check edits that add URLs
254 $oldtext = $this->loadText( $editPage, $section );
256 $oldLinks = $this->findLinks( $oldtext );
257 $newLinks = $this->findLinks( $newtext );
258 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
260 $addedLinks = array_diff( $unknownLinks, $oldLinks );
261 $numLinks = count( $addedLinks );
263 if( $numLinks > 0 ) {
264 global $wgUser, $wgTitle;
265 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
268 $wgTitle->getPrefixedText(),
269 implode( ", ", $addedLinks ) );
278 * Filter callback function for URL whitelisting
279 * @return bool true if unknown, false if whitelisted
282 function filterLink( $url ) {
283 global $wgCaptchaWhitelist;
284 return !( $wgCaptchaWhitelist && preg_match( $wgCaptchaWhitelist, $url ) );
288 * The main callback run on edit attempts.
289 * @param EditPage $editPage
290 * @param string $newtext
291 * @param string $section
292 * @param bool true to continue saving, false to abort and show a captcha form
294 function confirmEdit( &$editPage, $newtext, $section ) {
295 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
296 if( $this->passCaptcha() ) {
299 $editPage->showEditForm( array( &$this, 'editCallback' ) );
303 wfDebug( "ConfirmEdit: no new links.\n" );
309 * Hook for user creation form submissions.
311 * @param string $message
312 * @return bool true to continue, false to abort user creation
314 function confirmUserCreate( $u, &$message ) {
315 global $wgCaptchaTriggers;
316 if( $wgCaptchaTriggers['createaccount'] ) {
317 $this->trigger = "new account '" . $u->getName() . "'";
318 if( !$this->passCaptcha() ) {
319 $message = wfMsg( 'captcha-createaccount-fail' );
327 * Given a required captcha run, test form input for correct
328 * input on the open session.
329 * @return bool if passed, false if failed or new session
331 function passCaptcha() {
332 $info = $this->retrieveCaptcha();
335 if( $this->keyMatch( $wgRequest, $info ) ) {
336 $this->log( "passed" );
337 $this->clearCaptcha( $info );
340 $this->clearCaptcha( $info );
341 $this->log( "bad form input" );
345 $this->log( "new captcha session" );
351 * Log the status and any triggering info for debugging or statistics
352 * @param string $message
354 function log( $message ) {
355 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
359 * Generate a captcha session ID and save the info in PHP's session storage.
360 * (Requires the user to have cookies enabled to get through the captcha.)
362 * A random ID is used so legit users can make edits in multiple tabs or
363 * windows without being unnecessarily hobbled by a serial order requirement.
364 * Pass the returned id value into the edit form as wpCaptchaId.
366 * @param array $info data to store
367 * @return string captcha ID key
369 function storeCaptcha( $info ) {
370 if( !isset( $info['index'] ) ) {
371 // Assign random index if we're not udpating
372 $info['index'] = strval( mt_rand() );
374 $_SESSION['captcha' . $info['index']] = $info;
375 return $info['index'];
379 * Fetch this session's captcha info.
380 * @return mixed array of info, or false if missing
382 function retrieveCaptcha() {
384 $index = $wgRequest->getVal( 'wpCaptchaId' );
385 if( isset( $_SESSION['captcha' . $index] ) ) {
386 return $_SESSION['captcha' . $index];
393 * Clear out existing captcha info from the session, to ensure
394 * it can't be reused.
396 function clearCaptcha( $info ) {
397 unset( $_SESSION['captcha' . $info['index']] );
401 * Retrieve the current version of the page or section being edited...
402 * @param EditPage $editPage
403 * @param string $section
407 function loadText( $editPage, $section ) {
408 $rev = Revision::newFromTitle( $editPage->mTitle );
409 if( is_null( $rev ) ) {
412 $text = $rev->getText();
413 if( $section != '' ) {
414 return Article::getSection( $text, $section );
422 * Extract a list of all recognized HTTP links in the text.
423 * @param string $text
424 * @return array of strings
426 function findLinks( $text ) {
427 $regex = '/((?:' . HTTP_PROTOCOLS . ')' . EXT_LINK_URL_CLASS . '+)/';
429 if( preg_match_all( $regex, $text, $matches, PREG_PATTERN_ORDER ) ) {
437 * Show a page explaining what this wacky thing is.
439 function showHelp() {
440 global $wgOut, $ceAllowConfirmedEmail;
441 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
442 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
447 } # End invocation guard