]> ToastFreeware Gitweb - toast/cookiecaptcha.git/blob - ConfirmEdit.php
Action-specific messages. Don't claim that the user is adding a URL when they aren...
[toast/cookiecaptcha.git] / ConfirmEdit.php
1 <?php
2 /**
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.
7  *
8  * Copyright (C) 2005, 2006 Brion Vibber <brion@pobox.com>
9  * http://www.mediawiki.org/
10  *
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.
15  *
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.
20  *
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
25  *
26  * @addtogroup Extensions
27  */
28
29 if ( defined( 'MEDIAWIKI' ) ) {
30
31 global $wgExtensionFunctions, $wgGroupPermissions;
32
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',
39 );
40
41 # Internationalisation file
42 require_once( 'ConfirmEdit.i18n.php' );
43
44 /**
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.
48  *
49  * By default, sysops and registered bot accounts will be
50  * able to skip, while others have to go through it.
51  */
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;
57
58 global $wgCaptcha, $wgCaptchaClass, $wgCaptchaTriggers;
59 $wgCaptcha = null;
60 $wgCaptchaClass = 'SimpleCaptcha';
61
62 /**
63  * Currently the captcha works only for page edits.
64  *
65  * If the 'edit' trigger is on, *every* edit will trigger the captcha.
66  * This may be useful for protecting against vandalbot attacks.
67  *
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.
72  *
73  * The captcha code should not use $wgCaptchaTriggers, but CaptchaTriggers()
74  * which also takes into account per namespace triggering.
75  */
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
81
82 /**
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).
87  *
88  * Shall not be used with 'createaccount' (it is not checked).
89  */
90 $wgCaptchaTriggersOnNamespace = array();
91
92 #Example:
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.
95
96 /**
97  * Indicate how to store per-session data required to match up the
98  * internal captcha data with the editor.
99  *
100  * 'CaptchaSessionStore' uses PHP's session storage, which is cookie-based
101  * and may fail for anons with cookies disabled.
102  *
103  * 'CaptchaCacheStore' uses $wgMemc, which avoids the cookie dependency
104  * but may be fragile depending on cache configuration.
105  */
106 global $wgCaptchaStorageClass;
107 $wgCaptchaStorageClass = 'CaptchaSessionStore';
108
109 /**
110  * Number of sections a captcha session should last in the data cache
111  * before expiring when managing through CaptchaCacheStore class.
112  *
113  * Default is a half hour.
114  */
115 global $wgCaptchaSessionExpiration;
116 $wgCaptchaSessionExpiration = 30 * 60;
117
118 /**
119  * Allow users who have confirmed their e-mail addresses to post
120  * URL links without being harassed by the captcha.
121  */
122 global $ceAllowConfirmedEmail;
123 $ceAllowConfirmedEmail = false;
124
125 /**
126  * Regex to whitelist URLs to known-good sites...
127  * For instance:
128  * $wgCaptchaWhitelist = '#^https?://([a-z0-9-]+\\.)?(wikimedia|wikipedia)\.org/#i';
129  * @fixme Use the 'spam-whitelist' thingy instead?
130  */
131 $wgCaptchaWhitelist = false;
132
133 /**
134  * Additional regexes to check for. Use full regexes; can match things
135  * other than URLs such as junk edits.
136  *
137  * If the new version matches one and the old version doesn't,
138  * toss up the captcha screen.
139  *
140  * @fixme Add a message for local admins to add items as well.
141  */
142 $wgCaptchaRegexes = array();
143
144 /** Register special page */
145 global $wgSpecialPages;
146 $wgSpecialPages['Captcha'] = array( /*class*/ 'SpecialPage', /*name*/'Captcha', /*restriction*/ '',
147         /*listed*/ false, /*function*/ false, /*file*/ false );
148
149 /**
150  * Set up message strings for captcha utilities.
151  */
152 function ceSetup() {
153         # Add messages
154         global $wgMessageCache, $wgConfirmEditMessages;
155         foreach( $wgConfirmEditMessages as $lang => $messages )
156                 $wgMessageCache->addMessages( $messages, $lang );
157
158         global $wgHooks, $wgCaptcha, $wgCaptchaClass, $wgSpecialPages;
159         $wgCaptcha = new $wgCaptchaClass();
160         $wgHooks['EditFilter'][] = array( &$wgCaptcha, 'confirmEdit' );
161
162         $wgHooks['UserCreateForm'][] = array( &$wgCaptcha, 'injectUserCreate' );
163         $wgHooks['AbortNewAccount'][] = array( &$wgCaptcha, 'confirmUserCreate' );
164 }
165
166 /**
167  * Entry point for Special:Captcha
168  */
169 function wfSpecialCaptcha( $par = null ) {
170         global $wgCaptcha;
171         switch( $par ) {
172         case "image":
173                 return $wgCaptcha->showImage();
174         case "help":
175         default:
176                 return $wgCaptcha->showHelp();
177         }
178 }
179
180 class SimpleCaptcha {
181         function SimpleCaptcha() {
182                 global $wgCaptchaStorageClass;
183                 $this->storage = new $wgCaptchaStorageClass;
184         }
185         
186         /**
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.
190          *
191          * Override this!
192          *
193          * @return string HTML
194          */
195         function getForm() {
196                 $a = mt_rand(0, 100);
197                 $b = mt_rand(0, 10);
198                 $op = mt_rand(0, 1) ? '+' : '-';
199
200                 $test = "$a $op $b";
201                 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
202
203                 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
204
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
210                         "</p>\n" .
211                         wfElement( 'input', array(
212                                 'type'  => 'hidden',
213                                 'name'  => 'wpCaptchaId',
214                                 'id'    => 'wpCaptchaId',
215                                 'value' => $index ) );
216         }
217
218         /**
219          * Insert the captcha prompt into an edit form.
220          * @param OutputPage $out
221          */
222         function editCallback( &$out ) {
223                 $out->addWikiText( $this->getMessage( $this->action ) );
224                 $out->addHTML( $this->getForm() );
225         }
226
227         /**
228          * Show a message asking the user to enter a captcha on edit
229          * The result will be treated as wiki text
230          *
231          * @param $action Action being performed
232          * @return string
233          */
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;
240         }
241
242         /**
243          * Inject whazawhoo
244          * @fixme if multiple thingies insert a header, could break
245          * @param SimpleTemplate $template
246          * @return bool true to keep running callbacks
247          */
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' ) ) .
254                                 $this->getForm() .
255                                 "</div>\n" );
256                 }
257                 return true;
258         }
259
260         /**
261          * Check if the submitted form matches the captcha session data provided
262          * by the plugin when the form was generated.
263          *
264          * Override this!
265          *
266          * @param WebRequest $request
267          * @param array $info
268          * @return bool
269          */
270         function keyMatch( $request, $info ) {
271                 return $request->getVal( 'wpCaptchaWord' ) == $info['answer'];
272         }
273
274         // ----------------------------------
275
276         /**
277          * @param EditPage $editPage
278          * @param string $action (edit/create/addurl...)
279          * @return bool true if action triggers captcha on editPage's namespace
280          */
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];
286
287                 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
288         }
289
290
291         /**
292          * @param EditPage $editPage
293          * @param string $newtext
294          * @param string $section
295          * @return bool true if the captcha should run
296          */
297         function shouldCheck( &$editPage, $newtext, $section ) {
298                 $this->trigger = '';
299
300                 global $wgUser;
301                 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
302                         wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
303                         return false;
304                 }
305
306                 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
307                 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
308                         $wgUser->isEmailConfirmed() ) {
309                         wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
310                         return false;
311                 }
312
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]]",
317                                 $wgUser->getName(),
318                                 $wgTitle->getPrefixedText() );
319                         $this->action = 'edit';
320                         wfDebug( "ConfirmEdit: checking all edits...\n" );
321                         return true;
322                 }
323
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]]",
328                                 $wgUser->getName(),
329                                 $wgTitle->getPrefixedText() );
330                         $this->action = 'create';
331                         wfDebug( "ConfirmEdit: checking on page creation...\n" );
332                         return true;
333                 }
334
335                 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
336                         // Only check edits that add URLs
337                         $oldtext = $this->loadText( $editPage, $section );
338
339                         $oldLinks = $this->findLinks( $oldtext );
340                         $newLinks = $this->findLinks( $newtext );
341                         $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
342
343                         $addedLinks = array_diff( $unknownLinks, $oldLinks );
344                         $numLinks = count( $addedLinks );
345
346                         if( $numLinks > 0 ) {
347                                 global $wgUser, $wgTitle;
348                                 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
349                                         $numLinks,
350                                         $wgUser->getName(),
351                                         $wgTitle->getPrefixedText(),
352                                         implode( ", ", $addedLinks ) );
353                                 $this->action = 'addurl';
354                                 return true;
355                         }
356                 }
357
358                 global $wgCaptchaRegexes;
359                 if( !empty( $wgCaptchaRegexes ) ) {
360                         // Custom regex checks
361                         $oldtext = $this->loadText( $editPage, $section );
362
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 );
368
369                                         $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
370
371                                         $numHits = count( $addedMatches );
372                                         if( $numHits > 0 ) {
373                                                 global $wgUser, $wgTitle;
374                                                 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
375                                                         $numHits,
376                                                         $regex,
377                                                         $wgUser->getName(),
378                                                         $wgTitle->getPrefixedText(),
379                                                         implode( ", ", $addedMatches ) );
380                                                 $this->action = 'edit';
381                                                 return true;
382                                         }
383                                 }
384                         }
385                 }
386
387                 return false;
388         }
389
390         /**
391          * Filter callback function for URL whitelisting
392          * @return bool true if unknown, false if whitelisted
393          * @access private
394          */
395         function filterLink( $url ) {
396                 global $wgCaptchaWhitelist;
397                 return !( $wgCaptchaWhitelist && preg_match( $wgCaptchaWhitelist, $url ) );
398         }
399
400         /**
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
406          */
407         function confirmEdit( &$editPage, $newtext, $section ) {
408                 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
409                         if( $this->passCaptcha() ) {
410                                 return true;
411                         } else {
412                                 $editPage->showEditForm( array( &$this, 'editCallback' ) );
413                                 return false;
414                         }
415                 } else {
416                         wfDebug( "ConfirmEdit: no need to show captcha.\n" );
417                         return true;
418                 }
419         }
420
421         /**
422          * Hook for user creation form submissions.
423          * @param User $u
424          * @param string $message
425          * @return bool true to continue, false to abort user creation
426          */
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' );
433                                 return false;
434                         }
435                 }
436                 return true;
437         }
438
439         /**
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
443          */
444         function passCaptcha() {
445                 $info = $this->retrieveCaptcha();
446                 if( $info ) {
447                         global $wgRequest;
448                         if( $this->keyMatch( $wgRequest, $info ) ) {
449                                 $this->log( "passed" );
450                                 $this->clearCaptcha( $info );
451                                 return true;
452                         } else {
453                                 $this->clearCaptcha( $info );
454                                 $this->log( "bad form input" );
455                                 return false;
456                         }
457                 } else {
458                         $this->log( "new captcha session" );
459                         return false;
460                 }
461         }
462
463         /**
464          * Log the status and any triggering info for debugging or statistics
465          * @param string $message
466          */
467         function log( $message ) {
468                 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' .  $this->trigger );
469         }
470
471         /**
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.)
474          *
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.
478          *
479          * @param array $info data to store
480          * @return string captcha ID key
481          */
482         function storeCaptcha( $info ) {
483                 if( !isset( $info['index'] ) ) {
484                         // Assign random index if we're not udpating
485                         $info['index'] = strval( mt_rand() );
486                 }
487                 $this->storage->store( $info['index'], $info );
488                 return $info['index'];
489         }
490
491         /**
492          * Fetch this session's captcha info.
493          * @return mixed array of info, or false if missing
494          */
495         function retrieveCaptcha() {
496                 global $wgRequest;
497                 $index = $wgRequest->getVal( 'wpCaptchaId' );
498                 return $this->storage->retrieve( $index );
499         }
500
501         /**
502          * Clear out existing captcha info from the session, to ensure
503          * it can't be reused.
504          */
505         function clearCaptcha( $info ) {
506                 $this->storage->clear( $info['index'] );
507         }
508
509         /**
510          * Retrieve the current version of the page or section being edited...
511          * @param EditPage $editPage
512          * @param string $section
513          * @return string
514          * @access private
515          */
516         function loadText( $editPage, $section ) {
517                 $rev = Revision::newFromTitle( $editPage->mTitle );
518                 if( is_null( $rev ) ) {
519                         return "";
520                 } else {
521                         $text = $rev->getText();
522                         if( $section != '' ) {
523                                 return Article::getSection( $text, $section );
524                         } else {
525                                 return $text;
526                         }
527                 }
528         }
529
530         /**
531          * Extract a list of all recognized HTTP links in the text.
532          * @param string $text
533          * @return array of strings
534          */
535         function findLinks( $text ) {
536                 global $wgParser, $wgTitle, $wgUser;
537
538                 $options = new ParserOptions();
539                 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
540                 $out = $wgParser->parse( $text, $wgTitle, $options );
541
542                 return array_keys( $out->getExternalLinks() );
543         }
544
545         /**
546          * Show a page explaining what this wacky thing is.
547          */
548         function showHelp() {
549                 global $wgOut, $ceAllowConfirmedEmail;
550                 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
551                 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
552         }
553
554 }
555
556 class CaptchaSessionStore {
557         function store( $index, $info ) {
558                 $_SESSION['captcha' . $info['index']] = $info;
559         }
560         
561         function retrieve( $index ) {
562                 if( isset( $_SESSION['captcha' . $index] ) ) {
563                         return $_SESSION['captcha' . $index];
564                 } else {
565                         return false;
566                 }
567         }
568         
569         function clear( $index ) {
570                 unset( $_SESSION['captcha' . $index] );
571         }
572 }
573
574 class CaptchaCacheStore {
575         function store( $index, $info ) {
576                 global $wgMemc, $wgCaptchaSessionExpiration;
577                 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
578                         $wgCaptchaSessionExpiration );
579         }
580
581         function retrieve( $index ) {
582                 global $wgMemc;
583                 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
584                 if( $info ) {
585                         return $info;
586                 } else {
587                         return false;
588                 }
589         }
590         
591         function clear( $index ) {
592                 global $wgMemc;
593                 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
594         }
595 }
596
597 } # End invocation guard
598
599 ?>