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 * Set up message strings for captcha utilities.
80 global $wgMessageCache, $wgHooks, $wgCaptcha, $wgCaptchaClass;
81 $wgMessageCache->addMessages( array(
83 "Your edit includes new URL links; as a protection against automated " .
84 "spam, you'll need to type in the words that appear in this image:\n" .
85 "<br />([[Special:Captcha/help|What is this?]])",
86 'captchahelp-title' =>
89 "Web sites that accept postings from the public, like this wiki, " .
90 "are often abused by spammers who use automated tools to post their " .
91 "links to many sites. While these spam links can be removed, they " .
92 "are a significant nuisance." .
94 "Sometimes, especially when adding new web links to a page, " .
95 "the wiki may show you an image of colored or distorted text and " .
96 "ask you to type the words shown. Since this is a task that's hard " .
97 "to automate, it will allow most real humans to make their posts " .
98 "while stopping most spammers and other robotic attackers." .
100 "Unfortunately this may inconvenience users with limited vision or " .
101 "using text-based or speech-based browsers. At the moment we do not " .
102 "have an audio alternative available. Please contact the site " .
103 "administrators for assistance if this is unexpectedly preventing " .
104 "you from making legitimate posts." .
106 "Hit the 'back' button in your browser to return to the page editor." ) );
108 SpecialPage::addPage( new SpecialPage( 'Captcha', false,
109 /*listed*/ false, /*function*/ false, /*file*/ false ) );
111 $wgCaptcha = new $wgCaptchaClass();
112 $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
116 * Entry point for Special:Captcha
118 function wfSpecialCaptcha( $par = null ) {
122 return $wgCaptcha->showImage();
125 return $wgCaptcha->showHelp();
129 class SimpleCaptcha {
131 * Insert a captcha prompt into the edit form.
132 * This sample implementation generates a simple arithmetic operation;
133 * it would be easy to defeat by machine.
137 * @param OutputPage $out
139 function formCallback( &$out ) {
140 $a = mt_rand(0, 100);
142 $op = mt_rand(0, 1) ? '+' : '-';
145 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
147 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
149 $out->addWikiText( wfMsg( "captcha-short" ) );
150 $out->addHTML( "<p><label for=\"wpCaptchaWord\">$test</label> = " .
151 wfElement( 'input', array(
152 'name' => 'wpCaptchaWord',
153 'id' => 'wpCaptchaWord',
154 'tabindex' => 1 ) ) . // tab in before the edit textarea
156 wfElement( 'input', array(
158 'name' => 'wpCaptchaId',
159 'id' => 'wpCaptchaId',
160 'value' => $index ) ) );
164 * Check if the submitted form matches the captcha session data provided
165 * by the plugin when the form was generated.
169 * @param WebRequest $request
173 function keyMatch( $request, $info ) {
174 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
177 // ----------------------------------
180 * @param EditPage $editPage
181 * @param string $newtext
182 * @param string $section
183 * @return bool true if the captcha should run
185 function shouldCheck( &$editPage, $newtext, $section ) {
187 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
188 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
192 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
193 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
194 $wgUser->isEmailConfirmed() ) {
195 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
199 global $wgCaptchaTriggers;
200 if( !empty( $wgCaptchaTriggers['edit'] ) ) {
201 // Check on all edits
202 wfDebug( "ConfirmEdit: checking all edits...\n" );
206 if( !empty( $wgCaptchaTriggers['addurl'] ) ) {
207 // Only check edits that add URLs
208 $oldtext = $this->loadText( $editPage, $section );
210 $oldLinks = $this->findLinks( $oldtext );
211 $newLinks = $this->findLinks( $newtext );
213 $addedLinks = array_diff( $newLinks, $oldLinks );
214 $numLinks = count( $addedLinks );
216 if( $numLinks > 0 ) {
217 global $wgUser, $wgTitle;
218 wfDebugLog( "captcha", sprintf( "ConfirmEdit: %dx url trigger by %s at [[%s]]: %s",
221 $wgTitle->getPrefixedText(),
222 implode( ", ", $addedLinks ) ) );
231 * The main callback run on edit attempts.
232 * @param EditPage $editPage
233 * @param string $newtext
234 * @param string $section
235 * @param bool true to continue saving, false to abort and show a captcha form
237 function confirmEdit( &$editPage, $newtext, $section ) {
238 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
239 $info = $this->retrieveCaptcha();
242 if( $this->keyMatch( $wgRequest, $info ) ) {
243 wfDebug( "ConfirmEdit given proper key from form, passing.\n" );
246 wfDebug( "ConfirmEdit missing form key, prompting.\n" );
249 wfDebug( "ConfirmEdit: no session captcha key set, this is new visitor.\n" );
251 $editPage->showEditForm( array( &$this, 'formCallback' ) );
254 wfDebug( "ConfirmEdit: no new links.\n" );
260 * Generate a captcha session ID and save the info in PHP's session storage.
261 * (Requires the user to have cookies enabled to get through the captcha.)
263 * A random ID is used so legit users can make edits in multiple tabs or
264 * windows without being unnecessarily hobbled by a serial order requirement.
265 * Pass the returned id value into the edit form as wpCaptchaId.
267 * @param array $info data to store
268 * @param string $index optional, to overwrite used session
269 * @return string captcha ID key
271 function storeCaptcha( $info, $index=null ) {
272 if( is_null( $index ) ) {
273 $index = strval( mt_rand() );
274 $info['index'] = $index;
276 $_SESSION['captcha' . $index] = $info;
281 * Fetch this session's captcha info.
282 * @return mixed array of info, or false if missing
284 function retrieveCaptcha() {
286 $index = $wgRequest->getVal( 'wpCaptchaId' );
287 if( isset( $_SESSION['captcha' . $index] ) ) {
288 return $_SESSION['captcha' . $index];
295 * Retrieve the current version of the page or section being edited...
296 * @param EditPage $editPage
297 * @param string $section
301 function loadText( $editPage, $section ) {
302 $rev = Revision::newFromTitle( $editPage->mTitle );
303 if( is_null( $rev ) ) {
306 $text = $rev->getText();
307 if( $section != '' ) {
308 return Article::getSection( $text, $section );
316 * Extract a list of all recognized HTTP links in the text.
317 * @param string $text
318 * @return array of strings
320 function findLinks( $text ) {
321 $regex = '/((?:' . HTTP_PROTOCOLS . ')' . EXT_LINK_URL_CLASS . '+)/';
323 if( preg_match_all( $regex, $text, $matches, PREG_PATTERN_ORDER ) ) {
331 * Show a page explaining what this wacky thing is.
333 function showHelp() {
334 global $wgOut, $ceAllowConfirmedEmail;
335 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
336 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
341 } # End invocation guard