FU r102105, escaped the quotes.
[toast/cookiecaptcha.git] / Captcha.php
1 <?php
2
3 /**
4  * Object encapsulating a captcha process.  The captcha has two elements: it must be able
5  * to generate a frontend HTML representation of itself which can be presented to the user,
6  * which provides inputs for users to provide their interpretation of the captcha; and it
7  * must be able to retrieve that data from a subsequently-submitted request and validate
8  * whether the user got the data correct.
9  */
10 abstract class Captcha {
11
12         /**
13          * @var String
14          */
15         protected $id;
16
17         /**
18          * Information about the captcha, in array form
19          * @var $info Array
20          */
21         protected $info;
22
23         /**
24          * Whether this captcha exists in the storage
25          * @var Bool
26          */
27         protected $exists;
28
29         /**
30          * Generate a new empty Captcha.  This is guaranteed to return a Captcha object if it
31          * does not throw an exception
32          *
33          * @return Captcha subclass
34          */
35         public final static function factory() {
36                 global $wgCaptchaClass;
37                 $obj = new $wgCaptchaClass;
38                 if ( $obj instanceof Captcha ) {
39                         return $obj;
40                 } else {
41                         throw new MWException( "Invalid Captcha class $wgCaptchaClass, must extend Captcha" );
42                 }
43         }
44
45         /**
46          * Instantiate a new Captcha object for a given Id
47          * 
48          * @param  $id Int
49          * @return Captcha
50          */
51         public final static function newFromId( $id ){
52                 $obj = self::factory();
53                 $obj->setId( $id );
54                 return $obj->exists()
55                         ? $obj
56                         : null;
57         }
58
59         /**
60          * Instantiate a brand new captcha, never seen before.
61          *
62          * @return Captcha
63          */
64         public final static function newRandom(){
65                 $obj = self::factory();
66                 $obj->generateNew();
67                 return $obj;
68         }
69
70         /**
71          * Protected constructor - use only the factory methods above to instantiate captchas,
72          * or you may end up with the wrong type of object
73          */
74         protected function __construct(){}
75
76         /**
77          * Get the captcha Id
78          *
79          * @return String
80          */
81         public function getId(){
82                 return $this->id;
83         }
84
85         /**
86          * Set the Id internally.  Don't include wierd things like entities or characters that
87          * need to be HTML-escaped, you'll just be creating more work and pain for yourself...
88          *
89          * @param  $id String
90          */
91         protected function setId( $id ){
92                 $this->id = $id;
93         }
94
95         /**
96          * Initialise $this->info etc with information needed to make this object a new,
97          * (ideally) never-seen-before captcha.  Implementations should not save the data in
98          * the store in this function, as the captcha may not ever be used.
99          *
100          * @return Array of captcha info
101          */
102         # FIXME: detail
103         protected abstract function generateNew();
104
105         /**
106          * Save a generated captcha in storage somewhere where it won't be lost between
107          * requests. A random ID is used so legit users can make edits in multiple tabs
108          * or windows without being unnecessarily hobbled by a serial order requirement.
109          */
110         protected function store() {
111                 // Assign random index if we're not udpating
112                 if ( !isset( $this->info['index'] ) ) {
113                         if( !$this->getId() ){
114                                 $this->setId( strval( mt_rand() ) );
115                         }
116                         $this->info['index'] = $this->getId();
117                 }
118                 CaptchaStore::get()->store( $this->info['index'], $this->info );
119         }
120
121         /**
122          * Fetch the data for this captcha from the CaptchaStore.  This requires $this->id
123          * to be set.
124          *
125          * @return Array|Bool: Array of info, or false if missing
126          */
127         protected function retrieve() {
128                 if( $this->getId() === null ){
129                         return null;
130                 }
131                 if( $this->info === null ){
132                         $this->info = CaptchaStore::get()->retrieve( $this->getId() );
133                         $this->exists = $this->info !== false;
134                 }
135                 return $this->info;
136         }
137
138         /**
139          * Clear the information about this captcha from the CaptchaStore, so it cannot
140          * be reused at a later date.
141          */
142         protected function delete() {
143                 if( $this->getId() !== null ){
144                         CaptchaStore::get()->clear( $this->getId() );
145                 }
146         }
147
148         /**
149          * Whether this captcha exists.  $this->setId() must have been called from some context
150          *
151          * @return Bool
152          */
153         public function exists(){
154                 if( $this->exists === null ){
155                         $this->retrieve();
156                 }
157                 return $this->exists;
158         }
159
160         /**
161          * Load some data from a WebRequest.  Implementations must load all data they need
162          * from the request in this function, they must not use the global $wgRequest, as
163          * in the post-1.18 environment they may not necessarily be the same.
164          *
165          * @param $request WebRequest
166          * @param $field HTMLCaptchaField will be passed if the captcha is part of an HTMLForm
167          */
168         public abstract function loadFromRequest( WebRequest $request, HTMLCaptchaField $field = null );
169
170         /**
171          * Return the data that would be needed to pass the captcha challenge through the API.
172          * Implementations must return an array with at least the following parameters:
173          *     'type' - a unique description of the type of challenge.  This could be
174          *         the class name
175          *     'mime' - the MIME type of the challenge
176          *     'id' - the captcha Id produced by getId()
177          * Implementations should document how the user should use the provided data to answer
178          * the captcha.
179          *
180          * Implementations may return False to indicate that it is not possible to represent
181          * the challenge via the API.  API actions protected by such a captcha will be disabled.
182          *
183          * @return Array|Bool
184          */
185         public abstract function getApiParams();
186
187         /**
188          * Return the HTML which will be placed in the 'input' table cell of an HTMLForm.
189          * Implementations must include input fields which will perpetuate the captcha Id and
190          * any special data, as well as providing a means for the user to answer the captcha.
191          * Implementations should not include any help or label text, as these will be set in
192          * the label-message and help-message attributes of the HTMLCaptchafield.
193          * Implementations should honour the options set in the HTMLFormField such as
194          * $field->mName and $field->mReadonly.
195          *
196          * @param $field HTMLCaptchaField
197          * @return String raw HTML
198          */
199         public abstract function getFormHTML( HTMLCaptchaField $field );
200
201         /**
202          * Return the HTML which will be used in legacy forms which do not implement HTMLForm
203          * Implementations must include input fields which will perpetuate the captcha Id and
204          * any other necessary data, as well as providing a means for the user to answer the
205          * captcha, and any relevant descriptions and instructions.
206          *
207          * @return String raw HTML
208          */
209         public abstract function getFreeflowHTML();
210
211         /**
212          * Using the parameters loaded from the web request, check the captcha, maybe delete
213          * it if that's desirable, do any other necessary cleanup, and return Bool
214          * @return Bool whether the captcha was successfully answered
215          */
216         public abstract function checkCaptcha();
217 }
218
219 class SimpleCaptcha {
220
221         function getCaptcha() {
222                 $a = mt_rand( 0, 100 );
223                 $b = mt_rand( 0, 10 );
224
225                 /* Minus sign is used in the question. UTF-8,
226                    since the api uses text/plain, not text/html */
227                 $op = mt_rand( 0, 1 ) ? '+' : '−';
228
229                 $test = "$a $op $b";
230                 $answer = ( $op == '+' ) ? ( $a + $b ) : ( $a - $b );
231                 return array( 'question' => $test, 'answer' => $answer );
232         }
233
234         function addCaptchaAPI( &$resultArr ) {
235                 $captcha = $this->getCaptcha();
236                 $index = $this->storeCaptcha( $captcha );
237                 $resultArr['captcha']['type'] = 'simple';
238                 $resultArr['captcha']['mime'] = 'text/plain';
239                 $resultArr['captcha']['id'] = $index;
240                 $resultArr['captcha']['question'] = $captcha['question'];
241         }
242
243         /**
244          * Insert a captcha prompt into the edit form.
245          * This sample implementation generates a simple arithmetic operation;
246          * it would be easy to defeat by machine.
247          *
248          * Override this!
249          *
250          * @return string HTML
251          */
252         function getForm() {
253                 $captcha = $this->getCaptcha();
254                 $index = $this->storeCaptcha( $captcha );
255
256                 // dir="ltr" is needed because otherwise it may say
257                 // "5 - 20" instead of "20 - 5" and that would be wrong.
258                 return "<p><label dir=\"ltr\" for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
259                         Xml::element( 'input', array(
260                                 'name' => 'wpCaptchaWord',
261                                 'id'   => 'wpCaptchaWord',
262                                 'tabindex' => 1 ) ) . // tab in before the edit textarea
263                         "</p>\n" .
264                         Xml::element( 'input', array(
265                                 'type'  => 'hidden',
266                                 'name'  => 'wpCaptchaId',
267                                 'id'    => 'wpCaptchaId',
268                                 'value' => $index ) );
269         }
270
271         /**
272          * Insert the captcha prompt into an edit form.
273          * @param OutputPage $out
274          */
275         function editCallback( &$out ) {
276                 $out->addWikiText( $this->getMessage( $this->action ) );
277                 $out->addHTML( $this->getForm() );
278         }
279
280         /**
281          * Show a message asking the user to enter a captcha on edit
282          * The result will be treated as wiki text
283          *
284          * @param $action Action being performed
285          * @return string
286          */
287         function getMessage( $action ) {
288                 $name = 'captcha-' . $action;
289                 $text = wfMsg( $name );
290                 # Obtain a more tailored message, if possible, otherwise, fall back to
291                 # the default for edits
292                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
293         }
294
295         /**
296          * Inject whazawhoo
297          * @fixme if multiple thingies insert a header, could break
298          * @param $form HTMLForm
299          * @return bool true to keep running callbacks
300          */
301         function injectEmailUser( &$form ) {
302                 global $wgCaptchaTriggers, $wgOut, $wgUser;
303                 if ( $wgCaptchaTriggers['sendemail'] ) {
304                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
305                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
306                                 return true;
307                         }
308                         $form->addFooterText( 
309                                 "<div class='captcha'>" .
310                                 $wgOut->parse( $this->getMessage( 'sendemail' ) ) .
311                                 $this->getForm() .
312                                 "</div>\n" );
313                 }
314                 return true;
315         }
316
317         /**
318          * Inject whazawhoo
319          * @fixme if multiple thingies insert a header, could break
320          * @param QuickTemplate $template
321          * @return bool true to keep running callbacks
322          */
323         function injectUserCreate( &$template ) {
324                 global $wgCaptchaTriggers, $wgOut, $wgUser;
325                 if ( $wgCaptchaTriggers['createaccount'] ) {
326                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
327                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
328                                 return true;
329                         }
330                         $template->set( 'header',
331                                 "<div class='captcha'>" .
332                                 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
333                                 $this->getForm() .
334                                 "</div>\n" );
335                 }
336                 return true;
337         }
338
339         /**
340          * Inject a captcha into the user login form after a failed
341          * password attempt as a speedbump for mass attacks.
342          * @fixme if multiple thingies insert a header, could break
343          * @param $template QuickTemplate
344          * @return bool true to keep running callbacks
345          */
346         function injectUserLogin( &$template ) {
347                 if ( $this->isBadLoginTriggered() ) {
348                         global $wgOut;
349                         $template->set( 'header',
350                                 "<div class='captcha'>" .
351                                 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
352                                 $this->getForm() .
353                                 "</div>\n" );
354                 }
355                 return true;
356         }
357
358         /**
359          * When a bad login attempt is made, increment an expiring counter
360          * in the memcache cloud. Later checks for this may trigger a
361          * captcha display to prevent too many hits from the same place.
362          * @param User $user
363          * @param string $password
364          * @param int $retval authentication return value
365          * @return bool true to keep running callbacks
366          */
367         function triggerUserLogin( $user, $password, $retval ) {
368                 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
369                 if ( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
370                         $key = $this->badLoginKey();
371                         $count = $wgMemc->get( $key );
372                         if ( !$count ) {
373                                 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
374                         }
375                         $count = $wgMemc->incr( $key );
376                 }
377                 return true;
378         }
379
380         /**
381          * Check if a bad login has already been registered for this
382          * IP address. If so, require a captcha.
383          * @return bool
384          * @access private
385          */
386         function isBadLoginTriggered() {
387                 global $wgMemc, $wgCaptchaBadLoginAttempts;
388                 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
389         }
390
391         /**
392          * Check if the IP is allowed to skip captchas
393          */
394         function isIPWhitelisted() {
395                 global $wgCaptchaWhitelistIP;
396                 if ( $wgCaptchaWhitelistIP ) {
397                         $ip = wfGetIp();
398                         foreach ( $wgCaptchaWhitelistIP as $range ) {
399                                 if ( IP::isInRange( $ip, $range ) ) {
400                                         return true;
401                                 }
402                         }
403                 }
404                 return false;
405         }
406
407         /**
408          * Internal cache key for badlogin checks.
409          * @return string
410          * @access private
411          */
412         function badLoginKey() {
413                 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
414         }
415
416         /**
417          * Check if the submitted form matches the captcha session data provided
418          * by the plugin when the form was generated.
419          *
420          * Override this!
421          *
422          * @param string $answer
423          * @param array $info
424          * @return bool
425          */
426         function keyMatch( $answer, $info ) {
427                 return $answer == $info['answer'];
428         }
429
430         // ----------------------------------
431
432         /**
433          * @param EditPage $editPage
434          * @param string $action (edit/create/addurl...)
435          * @return bool true if action triggers captcha on editPage's namespace
436          */
437         function captchaTriggers( &$editPage, $action ) {
438                 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
439                 // Special config for this NS?
440                 if ( isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
441                         return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
442
443                 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
444         }
445
446         /**
447          * @param EditPage $editPage
448          * @param string $newtext
449          * @param string $section
450          * @return bool true if the captcha should run
451          */
452         function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
453                 $this->trigger = '';
454                 $title = $editPage->mArticle->getTitle();
455
456                 global $wgUser;
457                 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
458                         wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
459                         return false;
460                 }
461                 if ( $this->isIPWhitelisted() )
462                         return false;
463
464
465                 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
466                 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
467                         $wgUser->isEmailConfirmed() ) {
468                         wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
469                         return false;
470                 }
471
472                 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
473                         // Check on all edits
474                         global $wgUser;
475                         $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
476                                 $wgUser->getName(),
477                                 $title->getPrefixedText() );
478                         $this->action = 'edit';
479                         wfDebug( "ConfirmEdit: checking all edits...\n" );
480                         return true;
481                 }
482
483                 if ( $this->captchaTriggers( $editPage, 'create' )  && !$editPage->mTitle->exists() ) {
484                         // Check if creating a page
485                         global $wgUser;
486                         $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
487                                 $wgUser->getName(),
488                                 $title->getPrefixedText() );
489                         $this->action = 'create';
490                         wfDebug( "ConfirmEdit: checking on page creation...\n" );
491                         return true;
492                 }
493
494                 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
495                         // Only check edits that add URLs
496                         if ( $merged ) {
497                                 // Get links from the database
498                                 $oldLinks = $this->getLinksFromTracker( $title );
499                                 // Share a parse operation with Article::doEdit()
500                                 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
501                                 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
502                         } else {
503                                 // Get link changes in the slowest way known to man
504                                 $oldtext = $this->loadText( $editPage, $section );
505                                 $oldLinks = $this->findLinks( $editPage, $oldtext );
506                                 $newLinks = $this->findLinks( $editPage, $newtext );
507                         }
508
509                         $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
510                         $addedLinks = array_diff( $unknownLinks, $oldLinks );
511                         $numLinks = count( $addedLinks );
512
513                         if ( $numLinks > 0 ) {
514                                 global $wgUser;
515                                 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
516                                         $numLinks,
517                                         $wgUser->getName(),
518                                         $title->getPrefixedText(),
519                                         implode( ", ", $addedLinks ) );
520                                 $this->action = 'addurl';
521                                 return true;
522                         }
523                 }
524
525                 global $wgCaptchaRegexes;
526                 if ( $wgCaptchaRegexes ) {
527                         // Custom regex checks
528                         $oldtext = $this->loadText( $editPage, $section );
529
530                         foreach ( $wgCaptchaRegexes as $regex ) {
531                                 $newMatches = array();
532                                 if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
533                                         $oldMatches = array();
534                                         preg_match_all( $regex, $oldtext, $oldMatches );
535
536                                         $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
537
538                                         $numHits = count( $addedMatches );
539                                         if ( $numHits > 0 ) {
540                                                 global $wgUser;
541                                                 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
542                                                         $numHits,
543                                                         $regex,
544                                                         $wgUser->getName(),
545                                                         $title->getPrefixedText(),
546                                                         implode( ", ", $addedMatches ) );
547                                                 $this->action = 'edit';
548                                                 return true;
549                                         }
550                                 }
551                         }
552                 }
553
554                 return false;
555         }
556
557         /**
558          * Filter callback function for URL whitelisting
559          * @param string url to check
560          * @return bool true if unknown, false if whitelisted
561          * @access private
562          */
563         function filterLink( $url ) {
564                 global $wgCaptchaWhitelist;
565                 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
566
567                 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
568                         ? false
569                         : $this->buildRegexes( explode( "\n", $source ) );
570
571                 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
572                 $wl  = $whitelist          !== false ? preg_match( $whitelist, $url )          : false;
573
574                 return !( $cwl || $wl );
575         }
576
577         /**
578          * Build regex from whitelist
579          * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
580          * @return string Regex or bool false if whitelist is empty
581          * @access private
582          */
583         function buildRegexes( $lines ) {
584                 # Code duplicated from the SpamBlacklist extension (r19197)
585
586                 # Strip comments and whitespace, then remove blanks
587                 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
588
589                 # No lines, don't make a regex which will match everything
590                 if ( count( $lines ) == 0 ) {
591                         wfDebug( "No lines\n" );
592                         return false;
593                 } else {
594                         # Make regex
595                         # It's faster using the S modifier even though it will usually only be run once
596                         // $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
597                         // return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
598                         $regexes = '';
599                         $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
600                         $regexEnd = ')/Si';
601                         $regexMax = 4096;
602                         $build = false;
603                         foreach ( $lines as $line ) {
604                                 // FIXME: not very robust size check, but should work. :)
605                                 if ( $build === false ) {
606                                         $build = $line;
607                                 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
608                                         $regexes .= $regexStart .
609                                                 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
610                                                 $regexEnd;
611                                         $build = $line;
612                                 } else {
613                                         $build .= '|' . $line;
614                                 }
615                         }
616                         if ( $build !== false ) {
617                                 $regexes .= $regexStart .
618                                         str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
619                                         $regexEnd;
620                         }
621                         return $regexes;
622                 }
623         }
624
625         /**
626          * Load external links from the externallinks table
627          * @param $title Title
628          * @return Array
629          */
630         function getLinksFromTracker( $title ) {
631                 $dbr = wfGetDB( DB_SLAVE );
632                 $id = $title->getArticleId(); // should be zero queries
633                 $res = $dbr->select( 'externallinks', array( 'el_to' ),
634                         array( 'el_from' => $id ), __METHOD__ );
635                 $links = array();
636                 foreach ( $res as $row ) {
637                         $links[] = $row->el_to;
638                 }
639                 return $links;
640         }
641
642         /**
643          * Backend function for confirmEdit() and confirmEditAPI()
644          * @return bool false if the CAPTCHA is rejected, true otherwise
645          */
646         private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
647                 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
648                         if ( $this->passCaptcha() ) {
649                                 return true;
650                         } else {
651                                 return false;
652                         }
653                 } else {
654                         wfDebug( "ConfirmEdit: no need to show captcha.\n" );
655                         return true;
656                 }
657         }
658
659         /**
660          * The main callback run on edit attempts.
661          * @param EditPage $editPage
662          * @param string $newtext
663          * @param string $section
664          * @param bool $merged
665          * @return bool true to continue saving, false to abort and show a captcha form
666          */
667         function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
668                 if ( defined( 'MW_API' ) ) {
669                         # API mode
670                         # The CAPTCHA was already checked and approved
671                         return true;
672                 }
673                 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
674                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
675                         return false;
676                 }
677                 return true;
678         }
679
680         /**
681          * A more efficient edit filter callback based on the text after section merging
682          * @param EditPage $editPage
683          * @param string $newtext
684          */
685         function confirmEditMerged( $editPage, $newtext ) {
686                 return $this->confirmEdit( $editPage, $newtext, false, true );
687         }
688
689
690         function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
691                 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
692                         $this->addCaptchaAPI( $resultArr );
693                         return false;
694                 }
695                 return true;
696         }
697
698         /**
699          * Hook for user creation form submissions.
700          * @param User $u
701          * @param string $message
702          * @return bool true to continue, false to abort user creation
703          */
704         function confirmUserCreate( $u, &$message ) {
705                 global $wgCaptchaTriggers, $wgUser;
706                 if ( $wgCaptchaTriggers['createaccount'] ) {
707                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
708                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
709                                 return true;
710                         }
711                         if ( $this->isIPWhitelisted() )
712                                 return true;
713
714                         $this->trigger = "new account '" . $u->getName() . "'";
715                         if ( !$this->passCaptcha() ) {
716                                 $message = wfMsg( 'captcha-createaccount-fail' );
717                                 return false;
718                         }
719                 }
720                 return true;
721         }
722
723         /**
724          * Hook for user login form submissions.
725          * @param User $u
726          * @param string $message
727          * @return bool true to continue, false to abort user creation
728          */
729         function confirmUserLogin( $u, $pass, &$retval ) {
730                 if ( $this->isBadLoginTriggered() ) {
731                         if ( $this->isIPWhitelisted() )
732                                 return true;
733
734                         $this->trigger = "post-badlogin login '" . $u->getName() . "'";
735                         if ( !$this->passCaptcha() ) {
736                                 // Emulate a bad-password return to confuse the shit out of attackers
737                                 $retval = LoginForm::WRONG_PASS;
738                                 return false;
739                         }
740                 }
741                 return true;
742         }
743
744         /**
745          * Check the captcha on Special:EmailUser 
746          * @param $from MailAddress
747          * @param $to MailAddress
748          * @param $subject String
749          * @param $text String
750          * @param $error String reference
751          * @return Bool true to continue saving, false to abort and show a captcha form
752          */
753         function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
754                 global $wgCaptchaTriggers, $wgUser;
755                 if ( $wgCaptchaTriggers['sendemail'] ) {
756                         if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
757                                 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
758                                 return true;
759                         }
760                         if ( $this->isIPWhitelisted() )
761                                 return true;
762                 
763                         if ( defined( 'MW_API' ) ) {
764                                 # API mode
765                                 # Asking for captchas in the API is really silly
766                                 $error = wfMsg( 'captcha-disabledinapi' );
767                                 return false;
768                         }
769                         $this->trigger = "{$wgUser->getName()} sending email";
770                         if ( !$this->passCaptcha() ) {
771                                 $error = wfMsg( 'captcha-sendemail-fail' );
772                                 return false;
773                         }
774                 }
775                 return true;
776         }
777
778         /**
779          * Given a required captcha run, test form input for correct
780          * input on the open session.
781          * @return bool if passed, false if failed or new session
782          */
783         function passCaptcha() {
784                 $info = $this->retrieveCaptcha();
785                 if ( $info ) {
786                         global $wgRequest;
787                         if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
788                                 $this->log( "passed" );
789                                 $this->clearCaptcha( $info );
790                                 return true;
791                         } else {
792                                 $this->clearCaptcha( $info );
793                                 $this->log( "bad form input" );
794                                 return false;
795                         }
796                 } else {
797                         $this->log( "new captcha session" );
798                         return false;
799                 }
800         }
801
802         /**
803          * Log the status and any triggering info for debugging or statistics
804          * @param string $message
805          */
806         function log( $message ) {
807                 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' .  $this->trigger );
808         }
809
810         /**
811          * Generate a captcha session ID and save the info in PHP's session storage.
812          * (Requires the user to have cookies enabled to get through the captcha.)
813          *
814          * A random ID is used so legit users can make edits in multiple tabs or
815          * windows without being unnecessarily hobbled by a serial order requirement.
816          * Pass the returned id value into the edit form as wpCaptchaId.
817          *
818          * @param array $info data to store
819          * @return string captcha ID key
820          */
821         function storeCaptcha( $info ) {
822                 if ( !isset( $info['index'] ) ) {
823                         // Assign random index if we're not udpating
824                         $info['index'] = strval( mt_rand() );
825                 }
826                 CaptchaStore::get()->store( $info['index'], $info );
827                 return $info['index'];
828         }
829
830         /**
831          * Fetch this session's captcha info.
832          * @return mixed array of info, or false if missing
833          */
834         function retrieveCaptcha() {
835                 global $wgRequest;
836                 $index = $wgRequest->getVal( 'wpCaptchaId' );
837                 return CaptchaStore::get()->retrieve( $index );
838         }
839
840         /**
841          * Clear out existing captcha info from the session, to ensure
842          * it can't be reused.
843          */
844         function clearCaptcha( $info ) {
845                 CaptchaStore::get()->clear( $info['index'] );
846         }
847
848         /**
849          * Retrieve the current version of the page or section being edited...
850          * @param EditPage $editPage
851          * @param string $section
852          * @return string
853          * @access private
854          */
855         function loadText( $editPage, $section ) {
856                 $rev = Revision::newFromTitle( $editPage->mTitle );
857                 if ( is_null( $rev ) ) {
858                         return "";
859                 } else {
860                         $text = $rev->getText();
861                         if ( $section != '' ) {
862                                 global $wgParser;
863                                 return $wgParser->getSection( $text, $section );
864                         } else {
865                                 return $text;
866                         }
867                 }
868         }
869
870         /**
871          * Extract a list of all recognized HTTP links in the text.
872          * @param string $text
873          * @return array of strings
874          */
875         function findLinks( &$editpage, $text ) {
876                 global $wgParser, $wgUser;
877
878                 $options = new ParserOptions();
879                 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
880                 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
881
882                 return array_keys( $out->getExternalLinks() );
883         }
884
885         /**
886          * Show a page explaining what this wacky thing is.
887          */
888         function showHelp() {
889                 global $wgOut;
890                 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
891                 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
892                 if ( CaptchaStore::get()->cookiesNeeded() ) {
893                         $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
894                 }
895         }
896 }