Revert broken rewrite of login system; totally broken.
[toast/cookiecaptcha.git] / ConfirmEdit_body.php
1 <?php
2
3 class ConfirmEditHooks {
4         static function getInstance() {
5                 global $wgCaptcha, $wgCaptchaClass, $wgExtensionMessagesFiles;
6                 static $done = false;
7                 if ( !$done ) {
8                         $done = true;
9                         wfLoadExtensionMessages( 'ConfirmEdit' );
10                         if ( isset( $wgExtensionMessagesFiles[$wgCaptchaClass] ) ) {
11                                 wfLoadExtensionMessages( $wgCaptchaClass );
12                         }
13                         $wgCaptcha = new $wgCaptchaClass;
14                 }
15                 return $wgCaptcha;
16         }
17
18         static function confirmEdit( $editPage, $newtext, $section ) {
19                 return self::getInstance()->confirmEdit( $editPage, $newtext, $section );
20         }
21
22         static function confirmEditMerged( $editPage, $newtext ) {
23                 return self::getInstance()->confirmEditMerged( $editPage, $newtext );
24         }
25
26         static function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
27                 return self::getInstance()->confirmEditAPI( $editPage, $newtext, $resultArr );
28         }
29
30         static function injectUserCreate( &$template ) {
31                 return self::getInstance()->injectUserCreate( $template );
32         }
33
34         static function confirmUserCreate( $u, &$message ) {
35                 return self::getInstance()->confirmUserCreate( $u, $message );
36         }
37
38         static function triggerUserLogin( $user, $password, $retval ) {
39                 return self::getInstance()->triggerUserLogin( $user, $password, $retval );
40         }
41
42         static function injectUserLogin( &$template ) {
43                 return self::getInstance()->injectUserLogin( $template );
44         }
45
46         static function confirmUserLogin( $u, $pass, &$retval ) {
47                 return self::getInstance()->confirmUserLogin( $u, $pass, $retval );
48         }
49 }
50
51 class CaptchaSpecialPage extends UnlistedSpecialPage {
52         function execute( $par ) {
53                 $this->setHeaders();
54                 $instance = ConfirmEditHooks::getInstance();
55                 switch( $par ) {
56                 case "image":
57                         if ( method_exists( $instance, 'showImage' ) )
58                                 return $instance->showImage();
59                 case "help":
60                 default:
61                         return $instance->showHelp();
62                 }
63         }
64 }
65
66 class SimpleCaptcha {
67         function SimpleCaptcha() {
68                 global $wgCaptchaStorageClass;
69                 $this->storage = new $wgCaptchaStorageClass;
70         }
71
72         function getCaptcha() {
73                 $a = mt_rand( 0, 100 );
74                 $b = mt_rand( 0, 10 );
75                 $op = mt_rand( 0, 1 ) ? '+' : '-';
76
77                 $test = "$a $op $b";
78                 $answer = ( $op == '+' ) ? ( $a + $b ) : ( $a - $b );
79                 return array( 'question' => $test, 'answer' => $answer );
80         }
81
82         function addCaptchaAPI( &$resultArr ) {
83                 $captcha = $this->getCaptcha();
84                 $index = $this->storeCaptcha( $captcha );
85                 $resultArr['captcha']['type'] = 'simple';
86                 $resultArr['captcha']['mime'] = 'text/plain';
87                 $resultArr['captcha']['id'] = $index;
88                 $resultArr['captcha']['question'] = $captcha['question'];
89         }
90
91         /**
92          * Insert a captcha prompt into the edit form.
93          * This sample implementation generates a simple arithmetic operation;
94          * it would be easy to defeat by machine.
95          *
96          * Override this!
97          *
98          * @return string HTML
99          */
100         function getForm() {
101                 $captcha = $this->getCaptcha();
102                 $index = $this->storeCaptcha( $captcha );
103
104                 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
105                         Xml::element( 'input', array(
106                                 'name' => 'wpCaptchaWord',
107                                 'id'   => 'wpCaptchaWord',
108                                 'tabindex' => 1 ) ) . // tab in before the edit textarea
109                         "</p>\n" .
110                         Xml::element( 'input', array(
111                                 'type'  => 'hidden',
112                                 'name'  => 'wpCaptchaId',
113                                 'id'    => 'wpCaptchaId',
114                                 'value' => $index ) );
115         }
116
117         /**
118          * Insert the captcha prompt into an edit form.
119          * @param OutputPage $out
120          */
121         function editCallback( &$out ) {
122                 $out->addWikiText( $this->getMessage( $this->action ) );
123                 $out->addHTML( $this->getForm() );
124         }
125
126         /**
127          * Show a message asking the user to enter a captcha on edit
128          * The result will be treated as wiki text
129          *
130          * @param $action Action being performed
131          * @return string
132          */
133         function getMessage( $action ) {
134                 $name = 'captcha-' . $action;
135                 $text = wfMsg( $name );
136                 # Obtain a more tailored message, if possible, otherwise, fall back to
137                 # the default for edits
138                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
139         }
140
141         /**
142          * Inject whazawhoo
143          * @fixme if multiple thingies insert a header, could break
144          * @param SimpleTemplate $template
145          * @return bool true to keep running callbacks
146          */
147         function injectUserCreate( &$template ) {
148                 global $wgCaptchaTriggers, $wgOut, $wgUser;
149                 if ( $wgCaptchaTriggers['createaccount'] ) {
150                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
151                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
152                                 return true;
153                         }
154                         $template->set( 'header',
155                                 "<div class='captcha'>" .
156                                 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
157                                 $this->getForm() .
158                                 "</div>\n" );
159                 }
160                 return true;
161         }
162
163         /**
164          * Inject a captcha into the user login form after a failed
165          * password attempt as a speedbump for mass attacks.
166          * @fixme if multiple thingies insert a header, could break
167          * @param SimpleTemplate $template
168          * @return bool true to keep running callbacks
169          */
170         function injectUserLogin( &$template ) {
171                 if ( $this->isBadLoginTriggered() ) {
172                         global $wgOut;
173                         $template->set( 'header',
174                                 "<div class='captcha'>" .
175                                 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
176                                 $this->getForm() .
177                                 "</div>\n" );
178                 }
179                 return true;
180         }
181
182         /**
183          * When a bad login attempt is made, increment an expiring counter
184          * in the memcache cloud. Later checks for this may trigger a
185          * captcha display to prevent too many hits from the same place.
186          * @param User $user
187          * @param string $password
188          * @param int $retval authentication return value
189          * @return bool true to keep running callbacks
190          */
191         function triggerUserLogin( $user, $password, $retval ) {
192                 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
193                 if ( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
194                         $key = $this->badLoginKey();
195                         $count = $wgMemc->get( $key );
196                         if ( !$count ) {
197                                 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
198                         }
199                         $count = $wgMemc->incr( $key );
200                 }
201                 return true;
202         }
203
204         /**
205          * Check if a bad login has already been registered for this
206          * IP address. If so, require a captcha.
207          * @return bool
208          * @access private
209          */
210         function isBadLoginTriggered() {
211                 global $wgMemc, $wgCaptchaBadLoginAttempts;
212                 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
213         }
214
215         /**
216          * Check if the IP is allowed to skip captchas
217          */
218         function isIPWhitelisted() {
219                 global $wgCaptchaWhitelistIP;
220                 if ( $wgCaptchaWhitelistIP ) {
221                         $ip = wfGetIp();
222                         foreach ( $wgCaptchaWhitelistIP as $range ) {
223                                 if ( IP::isInRange( $ip, $range ) ) {
224                                         return true;
225                                 }
226                         }
227                 }
228                 return false;
229         }
230
231         /**
232          * Internal cache key for badlogin checks.
233          * @return string
234          * @access private
235          */
236         function badLoginKey() {
237                 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
238         }
239
240         /**
241          * Check if the submitted form matches the captcha session data provided
242          * by the plugin when the form was generated.
243          *
244          * Override this!
245          *
246          * @param string $answer
247          * @param array $info
248          * @return bool
249          */
250         function keyMatch( $answer, $info ) {
251                 return $answer == $info['answer'];
252         }
253
254         // ----------------------------------
255
256         /**
257          * @param EditPage $editPage
258          * @param string $action (edit/create/addurl...)
259          * @return bool true if action triggers captcha on editPage's namespace
260          */
261         function captchaTriggers( &$editPage, $action ) {
262                 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
263                 // Special config for this NS?
264                 if ( isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
265                         return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
266
267                 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
268         }
269
270         /**
271          * @param EditPage $editPage
272          * @param string $newtext
273          * @param string $section
274          * @return bool true if the captcha should run
275          */
276         function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
277                 $this->trigger = '';
278                 $title = $editPage->mArticle->getTitle();
279
280                 global $wgUser;
281                 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
282                         wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
283                         return false;
284                 }
285                 if ( $this->isIPWhitelisted() )
286                         return false;
287
288
289                 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
290                 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
291                         $wgUser->isEmailConfirmed() ) {
292                         wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
293                         return false;
294                 }
295
296                 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
297                         // Check on all edits
298                         global $wgUser;
299                         $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
300                                 $wgUser->getName(),
301                                 $title->getPrefixedText() );
302                         $this->action = 'edit';
303                         wfDebug( "ConfirmEdit: checking all edits...\n" );
304                         return true;
305                 }
306
307                 if ( $this->captchaTriggers( $editPage, 'create' )  && !$editPage->mTitle->exists() ) {
308                         // Check if creating a page
309                         global $wgUser;
310                         $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
311                                 $wgUser->getName(),
312                                 $title->getPrefixedText() );
313                         $this->action = 'create';
314                         wfDebug( "ConfirmEdit: checking on page creation...\n" );
315                         return true;
316                 }
317
318                 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
319                         // Only check edits that add URLs
320                         if ( $merged ) {
321                                 // Get links from the database
322                                 $oldLinks = $this->getLinksFromTracker( $title );
323                                 // Share a parse operation with Article::doEdit()
324                                 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
325                                 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
326                         } else {
327                                 // Get link changes in the slowest way known to man
328                                 $oldtext = $this->loadText( $editPage, $section );
329                                 $oldLinks = $this->findLinks( $editPage, $oldtext );
330                                 $newLinks = $this->findLinks( $editPage, $newtext );
331                         }
332
333                         $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
334                         $addedLinks = array_diff( $unknownLinks, $oldLinks );
335                         $numLinks = count( $addedLinks );
336
337                         if ( $numLinks > 0 ) {
338                                 global $wgUser;
339                                 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
340                                         $numLinks,
341                                         $wgUser->getName(),
342                                         $title->getPrefixedText(),
343                                         implode( ", ", $addedLinks ) );
344                                 $this->action = 'addurl';
345                                 return true;
346                         }
347                 }
348
349                 global $wgCaptchaRegexes;
350                 if ( $wgCaptchaRegexes ) {
351                         // Custom regex checks
352                         $oldtext = $this->loadText( $editPage, $section );
353
354                         foreach ( $wgCaptchaRegexes as $regex ) {
355                                 $newMatches = array();
356                                 if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
357                                         $oldMatches = array();
358                                         preg_match_all( $regex, $oldtext, $oldMatches );
359
360                                         $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
361
362                                         $numHits = count( $addedMatches );
363                                         if ( $numHits > 0 ) {
364                                                 global $wgUser;
365                                                 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
366                                                         $numHits,
367                                                         $regex,
368                                                         $wgUser->getName(),
369                                                         $title->getPrefixedText(),
370                                                         implode( ", ", $addedMatches ) );
371                                                 $this->action = 'edit';
372                                                 return true;
373                                         }
374                                 }
375                         }
376                 }
377
378                 return false;
379         }
380
381         /**
382          * Filter callback function for URL whitelisting
383          * @param string url to check
384          * @return bool true if unknown, false if whitelisted
385          * @access private
386          */
387         function filterLink( $url ) {
388                 global $wgCaptchaWhitelist;
389                 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
390
391                 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
392                         ? false
393                         : $this->buildRegexes( explode( "\n", $source ) );
394
395                 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
396                 $wl  = $whitelist          !== false ? preg_match( $whitelist, $url )          : false;
397
398                 return !( $cwl || $wl );
399         }
400
401         /**
402          * Build regex from whitelist
403          * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
404          * @return string Regex or bool false if whitelist is empty
405          * @access private
406          */
407         function buildRegexes( $lines ) {
408                 # Code duplicated from the SpamBlacklist extension (r19197)
409
410                 # Strip comments and whitespace, then remove blanks
411                 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
412
413                 # No lines, don't make a regex which will match everything
414                 if ( count( $lines ) == 0 ) {
415                         wfDebug( "No lines\n" );
416                         return false;
417                 } else {
418                         # Make regex
419                         # It's faster using the S modifier even though it will usually only be run once
420                         // $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
421                         // return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
422                         $regexes = '';
423                         $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
424                         $regexEnd = ')/Si';
425                         $regexMax = 4096;
426                         $build = false;
427                         foreach ( $lines as $line ) {
428                                 // FIXME: not very robust size check, but should work. :)
429                                 if ( $build === false ) {
430                                         $build = $line;
431                                 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
432                                         $regexes .= $regexStart .
433                                                 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
434                                                 $regexEnd;
435                                         $build = $line;
436                                 } else {
437                                         $build .= '|' . $line;
438                                 }
439                         }
440                         if ( $build !== false ) {
441                                 $regexes .= $regexStart .
442                                         str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
443                                         $regexEnd;
444                         }
445                         return $regexes;
446                 }
447         }
448
449         /**
450          * Load external links from the externallinks table
451          */
452         function getLinksFromTracker( $title ) {
453                 $dbr =& wfGetDB( DB_SLAVE );
454                 $id = $title->getArticleId(); // should be zero queries
455                 $res = $dbr->select( 'externallinks', array( 'el_to' ),
456                         array( 'el_from' => $id ), __METHOD__ );
457                 $links = array();
458                 while ( $row = $dbr->fetchObject( $res ) ) {
459                         $links[] = $row->el_to;
460                 }
461                 return $links;
462         }
463
464         /**
465          * Backend function for confirmEdit() and confirmEditAPI()
466          * @return bool false if the CAPTCHA is rejected, true otherwise
467          */
468         private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
469                 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
470                         if ( $this->passCaptcha() ) {
471                                 return true;
472                         } else {
473                                 return false;
474                         }
475                 } else {
476                         wfDebug( "ConfirmEdit: no need to show captcha.\n" );
477                         return true;
478                 }
479         }
480
481         /**
482          * The main callback run on edit attempts.
483          * @param EditPage $editPage
484          * @param string $newtext
485          * @param string $section
486          * @param bool $merged
487          * @return bool true to continue saving, false to abort and show a captcha form
488          */
489         function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
490                 if ( defined( 'MW_API' ) ) {
491                         # API mode
492                         # The CAPTCHA was already checked and approved
493                         return true;
494                 }
495                 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
496                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
497                         return false;
498                 }
499                 return true;
500         }
501
502         /**
503          * A more efficient edit filter callback based on the text after section merging
504          * @param EditPage $editPage
505          * @param string $newtext
506          */
507         function confirmEditMerged( $editPage, $newtext ) {
508                 return $this->confirmEdit( $editPage, $newtext, false, true );
509         }
510
511
512         function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
513                 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
514                         $this->addCaptchaAPI( $resultArr );
515                         return false;
516                 }
517                 return true;
518         }
519
520         /**
521          * Hook for user creation form submissions.
522          * @param User $u
523          * @param string $message
524          * @return bool true to continue, false to abort user creation
525          */
526         function confirmUserCreate( $u, &$message ) {
527                 global $wgCaptchaTriggers, $wgUser;
528                 if ( $wgCaptchaTriggers['createaccount'] ) {
529                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
530                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
531                                 return true;
532                         }
533                         if ( $this->isIPWhitelisted() )
534                                 return true;
535
536                         $this->trigger = "new account '" . $u->getName() . "'";
537                         if ( !$this->passCaptcha() ) {
538                                 $message = wfMsg( 'captcha-createaccount-fail' );
539                                 return false;
540                         }
541                 }
542                 return true;
543         }
544
545         /**
546          * Hook for user login form submissions.
547          * @param User $u
548          * @param string $message
549          * @return bool true to continue, false to abort user creation
550          */
551         function confirmUserLogin( $u, $pass, &$retval ) {
552                 if ( $this->isBadLoginTriggered() ) {
553                         if ( $this->isIPWhitelisted() )
554                                 return true;
555
556                         $this->trigger = "post-badlogin login '" . $u->getName() . "'";
557                         if ( !$this->passCaptcha() ) {
558                                 $message = wfMsg( 'captcha-badlogin-fail' );
559                                 // Emulate a bad-password return to confuse the shit out of attackers
560                                 $retval = LoginForm::WRONG_PASS;
561                                 return false;
562                         }
563                 }
564                 return true;
565         }
566
567         /**
568          * Given a required captcha run, test form input for correct
569          * input on the open session.
570          * @return bool if passed, false if failed or new session
571          */
572         function passCaptcha() {
573                 $info = $this->retrieveCaptcha();
574                 if ( $info ) {
575                         global $wgRequest;
576                         if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
577                                 $this->log( "passed" );
578                                 $this->clearCaptcha( $info );
579                                 return true;
580                         } else {
581                                 $this->clearCaptcha( $info );
582                                 $this->log( "bad form input" );
583                                 return false;
584                         }
585                 } else {
586                         $this->log( "new captcha session" );
587                         return false;
588                 }
589         }
590
591         /**
592          * Log the status and any triggering info for debugging or statistics
593          * @param string $message
594          */
595         function log( $message ) {
596                 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' .  $this->trigger );
597         }
598
599         /**
600          * Generate a captcha session ID and save the info in PHP's session storage.
601          * (Requires the user to have cookies enabled to get through the captcha.)
602          *
603          * A random ID is used so legit users can make edits in multiple tabs or
604          * windows without being unnecessarily hobbled by a serial order requirement.
605          * Pass the returned id value into the edit form as wpCaptchaId.
606          *
607          * @param array $info data to store
608          * @return string captcha ID key
609          */
610         function storeCaptcha( $info ) {
611                 if ( !isset( $info['index'] ) ) {
612                         // Assign random index if we're not udpating
613                         $info['index'] = strval( mt_rand() );
614                 }
615                 $this->storage->store( $info['index'], $info );
616                 return $info['index'];
617         }
618
619         /**
620          * Fetch this session's captcha info.
621          * @return mixed array of info, or false if missing
622          */
623         function retrieveCaptcha() {
624                 global $wgRequest;
625                 $index = $wgRequest->getVal( 'wpCaptchaId' );
626                 return $this->storage->retrieve( $index );
627         }
628
629         /**
630          * Clear out existing captcha info from the session, to ensure
631          * it can't be reused.
632          */
633         function clearCaptcha( $info ) {
634                 $this->storage->clear( $info['index'] );
635         }
636
637         /**
638          * Retrieve the current version of the page or section being edited...
639          * @param EditPage $editPage
640          * @param string $section
641          * @return string
642          * @access private
643          */
644         function loadText( $editPage, $section ) {
645                 $rev = Revision::newFromTitle( $editPage->mTitle );
646                 if ( is_null( $rev ) ) {
647                         return "";
648                 } else {
649                         $text = $rev->getText();
650                         if ( $section != '' ) {
651                                 return Article::getSection( $text, $section );
652                         } else {
653                                 return $text;
654                         }
655                 }
656         }
657
658         /**
659          * Extract a list of all recognized HTTP links in the text.
660          * @param string $text
661          * @return array of strings
662          */
663         function findLinks( &$editpage, $text ) {
664                 global $wgParser, $wgUser;
665
666                 $options = new ParserOptions();
667                 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
668                 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
669
670                 return array_keys( $out->getExternalLinks() );
671         }
672
673         /**
674          * Show a page explaining what this wacky thing is.
675          */
676         function showHelp() {
677                 global $wgOut, $ceAllowConfirmedEmail;
678                 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
679                 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
680                 if ( $this->storage->cookiesNeeded() ) {
681                         $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
682                 }
683         }
684 }
685
686 class CaptchaSessionStore {
687         function store( $index, $info ) {
688                 $_SESSION['captcha' . $info['index']] = $info;
689         }
690
691         function retrieve( $index ) {
692                 if ( isset( $_SESSION['captcha' . $index] ) ) {
693                         return $_SESSION['captcha' . $index];
694                 } else {
695                         return false;
696                 }
697         }
698
699         function clear( $index ) {
700                 unset( $_SESSION['captcha' . $index] );
701         }
702
703         function cookiesNeeded() {
704                 return true;
705         }
706 }
707
708 class CaptchaCacheStore {
709         function store( $index, $info ) {
710                 global $wgMemc, $wgCaptchaSessionExpiration;
711                 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
712                         $wgCaptchaSessionExpiration );
713         }
714
715         function retrieve( $index ) {
716                 global $wgMemc;
717                 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
718                 if ( $info ) {
719                         return $info;
720                 } else {
721                         return false;
722                 }
723         }
724
725         function clear( $index ) {
726                 global $wgMemc;
727                 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
728         }
729
730         function cookiesNeeded() {
731                 return false;
732         }
733 }