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
70 * Allow users who have confirmed their e-mail addresses to post
71 * URL links without being harassed by the captcha.
73 global $ceAllowConfirmedEmail;
74 $ceAllowConfirmedEmail = false;
77 * Regex to whitelist URLs to known-good sites...
79 * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
81 $wgCaptchaWhitelist = false;
84 * Set up message strings for captcha utilities.
87 global $wgMessageCache, $wgHooks, $wgCaptcha, $wgCaptchaClass;
88 $wgMessageCache->addMessages( array(
90 "Your edit includes new URL links; as a protection against automated " .
91 "spam, you'll need to type in the words that appear in this image:\n" .
92 "<br />([[Special:Captcha/help|What is this?]])",
93 'captchahelp-title' =>
96 "Web sites that accept postings from the public, like this wiki, " .
97 "are often abused by spammers who use automated tools to post their " .
98 "links to many sites. While these spam links can be removed, they " .
99 "are a significant nuisance." .
101 "Sometimes, especially when adding new web links to a page, " .
102 "the wiki may show you an image of colored or distorted text and " .
103 "ask you to type the words shown. Since this is a task that's hard " .
104 "to automate, it will allow most real humans to make their posts " .
105 "while stopping most spammers and other robotic attackers." .
107 "Unfortunately this may inconvenience users with limited vision or " .
108 "using text-based or speech-based browsers. At the moment we do not " .
109 "have an audio alternative available. Please contact the site " .
110 "administrators for assistance if this is unexpectedly preventing " .
111 "you from making legitimate posts." .
113 "Hit the 'back' button in your browser to return to the page editor." ) );
115 SpecialPage::addPage( new SpecialPage( 'Captcha', false,
116 /*listed*/ false, /*function*/ false, /*file*/ false ) );
118 $wgCaptcha = new $wgCaptchaClass();
119 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
123 * Entry point for Special:Captcha
125 function wfSpecialCaptcha( $par = null ) {
129 return $wgCaptcha->showImage();
132 return $wgCaptcha->showHelp();
136 class SimpleCaptcha {
138 * Insert a captcha prompt into the edit form.
139 * This sample implementation generates a simple arithmetic operation;
140 * it would be easy to defeat by machine.
144 * @param OutputPage $out
146 function formCallback( &$out ) {
147 $a = mt_rand(0, 100);
149 $op = mt_rand(0, 1) ? '+' : '-';
152 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
154 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
156 $out->addWikiText( wfMsg( "captcha-short" ) );
157 $out->addHTML( "<p><label for=\"wpCaptchaWord\">$test</label> = " .
158 wfElement( 'input', array(
159 'name' => 'wpCaptchaWord',
160 'id' => 'wpCaptchaWord',
161 'tabindex' => 1 ) ) . // tab in before the edit textarea
163 wfElement( 'input', array(
165 'name' => 'wpCaptchaId',
166 'id' => 'wpCaptchaId',
167 'value' => $index ) ) );
171 * Check if the submitted form matches the captcha session data provided
172 * by the plugin when the form was generated.
176 * @param WebRequest $request
180 function keyMatch( $request, $info ) {
181 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
184 // ----------------------------------
187 * @param EditPage $editPage
188 * @param string $newtext
189 * @param string $section
190 * @return bool true if the captcha should run
192 function shouldCheck( &$editPage, $newtext, $section ) {
194 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
195 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
199 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
200 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
201 $wgUser->isEmailConfirmed() ) {
202 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
206 global $wgCaptchaTriggers;
207 if( !empty( $wgCaptchaTriggers['edit'] ) ) {
208 // Check on all edits
209 wfDebug( "ConfirmEdit: checking all edits...\n" );
213 if( !empty( $wgCaptchaTriggers['addurl'] ) ) {
214 // Only check edits that add URLs
215 $oldtext = $this->loadText( $editPage, $section );
217 $oldLinks = $this->findLinks( $oldtext );
218 $newLinks = $this->findLinks( $newtext );
219 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
221 $addedLinks = array_diff( $unknownLinks, $oldLinks );
222 $numLinks = count( $addedLinks );
224 if( $numLinks > 0 ) {
225 global $wgUser, $wgTitle;
226 wfDebugLog( "captcha", sprintf( "ConfirmEdit: %dx url trigger by %s at [[%s]]: %s",
229 $wgTitle->getPrefixedText(),
230 implode( ", ", $addedLinks ) ) );
239 * Filter callback function for URL whitelisting
240 * @return bool true if unknown, false if whitelisted
243 function filterLink( $url ) {
244 global $wgCaptchaWhitelist;
245 return !( $wgCaptchaWhitelist && preg_match( $wgCaptchaWhitelist, $url ) );
249 * The main callback run on edit attempts.
250 * @param EditPage $editPage
251 * @param string $newtext
252 * @param string $section
253 * @param bool true to continue saving, false to abort and show a captcha form
255 function confirmEdit( &$editPage, $newtext, $section ) {
256 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
257 $info = $this->retrieveCaptcha();
260 if( $this->keyMatch( $wgRequest, $info ) ) {
261 wfDebug( "ConfirmEdit given proper key from form, passing.\n" );
264 wfDebug( "ConfirmEdit missing form key, prompting.\n" );
267 wfDebug( "ConfirmEdit: no session captcha key set, this is new visitor.\n" );
269 $editPage->showEditForm( array( &$this, 'formCallback' ) );
272 wfDebug( "ConfirmEdit: no new links.\n" );
278 * Generate a captcha session ID and save the info in PHP's session storage.
279 * (Requires the user to have cookies enabled to get through the captcha.)
281 * A random ID is used so legit users can make edits in multiple tabs or
282 * windows without being unnecessarily hobbled by a serial order requirement.
283 * Pass the returned id value into the edit form as wpCaptchaId.
285 * @param array $info data to store
286 * @param string $index optional, to overwrite used session
287 * @return string captcha ID key
289 function storeCaptcha( $info, $index=null ) {
290 if( is_null( $index ) ) {
291 $index = strval( mt_rand() );
292 $info['index'] = $index;
294 $_SESSION['captcha' . $index] = $info;
299 * Fetch this session's captcha info.
300 * @return mixed array of info, or false if missing
302 function retrieveCaptcha() {
304 $index = $wgRequest->getVal( 'wpCaptchaId' );
305 if( isset( $_SESSION['captcha' . $index] ) ) {
306 return $_SESSION['captcha' . $index];
313 * Retrieve the current version of the page or section being edited...
314 * @param EditPage $editPage
315 * @param string $section
319 function loadText( $editPage, $section ) {
320 $rev = Revision::newFromTitle( $editPage->mTitle );
321 if( is_null( $rev ) ) {
324 $text = $rev->getText();
325 if( $section != '' ) {
326 return Article::getSection( $text, $section );
334 * Extract a list of all recognized HTTP links in the text.
335 * @param string $text
336 * @return array of strings
338 function findLinks( $text ) {
339 $regex = '/((?:' . HTTP_PROTOCOLS . ')' . EXT_LINK_URL_CLASS . '+)/';
341 if( preg_match_all( $regex, $text, $matches, PREG_PATTERN_ORDER ) ) {
349 * Show a page explaining what this wacky thing is.
351 function showHelp() {
352 global $wgOut, $ceAllowConfirmedEmail;
353 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
354 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
359 } # End invocation guard