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.
10 abstract class Captcha {
18 * Information about the captcha, in array form
24 * Whether this captcha exists in the storage
30 * Generate a new empty Captcha. This is guaranteed to return a Captcha object if it
31 * does not throw an exception
33 * @return Captcha subclass
35 public final static function factory() {
36 global $wgCaptchaClass;
37 $obj = new $wgCaptchaClass;
38 if ( $obj instanceof Captcha ) {
41 throw new MWException( "Invalid Captcha class $wgCaptchaClass, must extend Captcha" );
46 * Instantiate a new Captcha object for a given Id
51 public final static function newFromId( $id ){
52 $obj = self::factory();
60 * Instantiate a brand new captcha, never seen before.
64 public final static function newRandom(){
65 $obj = self::factory();
71 * Protected constructor - use only the factory methods above to instantiate captchas,
72 * or you may end up with the wrong type of object
74 protected function __construct(){}
81 public function getId(){
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...
91 protected function setId( $id ){
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.
100 * @return Array of captcha info
103 protected abstract function generateNew();
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.
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() ) );
116 $this->info['index'] = $this->getId();
118 CaptchaStore::get()->store( $this->info['index'], $this->info );
122 * Fetch the data for this captcha from the CaptchaStore. This requires $this->id
125 * @return Array|Bool: Array of info, or false if missing
127 protected function retrieve() {
128 if( $this->getId() === null ){
131 if( $this->info === null ){
132 $this->info = CaptchaStore::get()->retrieve( $this->getId() );
133 $this->exists = $this->info !== false;
139 * Clear the information about this captcha from the CaptchaStore, so it cannot
140 * be reused at a later date.
142 protected function delete() {
143 if( $this->getId() !== null ){
144 CaptchaStore::get()->clear( $this->getId() );
149 * Whether this captcha exists. $this->setId() must have been called from some context
153 public function exists(){
154 if( $this->exists === null ){
157 return $this->exists;
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.
165 * @param $request WebRequest
166 * @param $field HTMLCaptchaField will be passed if the captcha is part of an HTMLForm
168 public abstract function loadFromRequest( WebRequest $request, HTMLCaptchaField $field = null );
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
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
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.
185 public abstract function getApiParams();
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.
196 * @param $field HTMLCaptchaField
197 * @return String raw HTML
199 public abstract function getFormHTML( HTMLCaptchaField $field );
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.
207 * @return String raw HTML
209 public abstract function getFreeflowHTML();
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
216 public abstract function checkCaptcha();
219 class SimpleCaptcha {
221 function getCaptcha() {
222 $a = mt_rand( 0, 100 );
223 $b = mt_rand( 0, 10 );
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 ) ? '+' : '−';
230 $answer = ( $op == '+' ) ? ( $a + $b ) : ( $a - $b );
231 return array( 'question' => $test, 'answer' => $answer );
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'];
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.
250 * @return string HTML
253 $captcha = $this->getCaptcha();
254 $index = $this->storeCaptcha( $captcha );
256 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
257 Xml::element( 'input', array(
258 'name' => 'wpCaptchaWord',
259 'id' => 'wpCaptchaWord',
260 'tabindex' => 1 ) ) . // tab in before the edit textarea
262 Xml::element( 'input', array(
264 'name' => 'wpCaptchaId',
265 'id' => 'wpCaptchaId',
266 'value' => $index ) );
270 * Insert the captcha prompt into an edit form.
271 * @param OutputPage $out
273 function editCallback( &$out ) {
274 $out->addWikiText( $this->getMessage( $this->action ) );
275 $out->addHTML( $this->getForm() );
279 * Show a message asking the user to enter a captcha on edit
280 * The result will be treated as wiki text
282 * @param $action Action being performed
285 function getMessage( $action ) {
286 $name = 'captcha-' . $action;
287 $text = wfMsg( $name );
288 # Obtain a more tailored message, if possible, otherwise, fall back to
289 # the default for edits
290 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
295 * @fixme if multiple thingies insert a header, could break
296 * @param $form HTMLForm
297 * @return bool true to keep running callbacks
299 function injectEmailUser( &$form ) {
300 global $wgCaptchaTriggers, $wgOut, $wgUser;
301 if ( $wgCaptchaTriggers['sendemail'] ) {
302 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
303 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
306 $form->addFooterText(
307 "<div class='captcha'>" .
308 $wgOut->parse( $this->getMessage( 'sendemail' ) ) .
317 * @fixme if multiple thingies insert a header, could break
318 * @param QuickTemplate $template
319 * @return bool true to keep running callbacks
321 function injectUserCreate( &$template ) {
322 global $wgCaptchaTriggers, $wgOut, $wgUser;
323 if ( $wgCaptchaTriggers['createaccount'] ) {
324 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
325 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
328 $template->set( 'header',
329 "<div class='captcha'>" .
330 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
338 * Inject a captcha into the user login form after a failed
339 * password attempt as a speedbump for mass attacks.
340 * @fixme if multiple thingies insert a header, could break
341 * @param $template QuickTemplate
342 * @return bool true to keep running callbacks
344 function injectUserLogin( &$template ) {
345 if ( $this->isBadLoginTriggered() ) {
347 $template->set( 'header',
348 "<div class='captcha'>" .
349 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
357 * When a bad login attempt is made, increment an expiring counter
358 * in the memcache cloud. Later checks for this may trigger a
359 * captcha display to prevent too many hits from the same place.
361 * @param string $password
362 * @param int $retval authentication return value
363 * @return bool true to keep running callbacks
365 function triggerUserLogin( $user, $password, $retval ) {
366 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
367 if ( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
368 $key = $this->badLoginKey();
369 $count = $wgMemc->get( $key );
371 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
373 $count = $wgMemc->incr( $key );
379 * Check if a bad login has already been registered for this
380 * IP address. If so, require a captcha.
384 function isBadLoginTriggered() {
385 global $wgMemc, $wgCaptchaBadLoginAttempts;
386 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
390 * Check if the IP is allowed to skip captchas
392 function isIPWhitelisted() {
393 global $wgCaptchaWhitelistIP;
394 if ( $wgCaptchaWhitelistIP ) {
396 foreach ( $wgCaptchaWhitelistIP as $range ) {
397 if ( IP::isInRange( $ip, $range ) ) {
406 * Internal cache key for badlogin checks.
410 function badLoginKey() {
411 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
415 * Check if the submitted form matches the captcha session data provided
416 * by the plugin when the form was generated.
420 * @param string $answer
424 function keyMatch( $answer, $info ) {
425 return $answer == $info['answer'];
428 // ----------------------------------
431 * @param EditPage $editPage
432 * @param string $action (edit/create/addurl...)
433 * @return bool true if action triggers captcha on editPage's namespace
435 function captchaTriggers( &$editPage, $action ) {
436 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
437 // Special config for this NS?
438 if ( isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
439 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
441 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
445 * @param EditPage $editPage
446 * @param string $newtext
447 * @param string $section
448 * @return bool true if the captcha should run
450 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
452 $title = $editPage->mArticle->getTitle();
455 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
456 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
459 if ( $this->isIPWhitelisted() )
463 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
464 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
465 $wgUser->isEmailConfirmed() ) {
466 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
470 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
471 // Check on all edits
473 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
475 $title->getPrefixedText() );
476 $this->action = 'edit';
477 wfDebug( "ConfirmEdit: checking all edits...\n" );
481 if ( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
482 // Check if creating a page
484 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
486 $title->getPrefixedText() );
487 $this->action = 'create';
488 wfDebug( "ConfirmEdit: checking on page creation...\n" );
492 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
493 // Only check edits that add URLs
495 // Get links from the database
496 $oldLinks = $this->getLinksFromTracker( $title );
497 // Share a parse operation with Article::doEdit()
498 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
499 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
501 // Get link changes in the slowest way known to man
502 $oldtext = $this->loadText( $editPage, $section );
503 $oldLinks = $this->findLinks( $editPage, $oldtext );
504 $newLinks = $this->findLinks( $editPage, $newtext );
507 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
508 $addedLinks = array_diff( $unknownLinks, $oldLinks );
509 $numLinks = count( $addedLinks );
511 if ( $numLinks > 0 ) {
513 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
516 $title->getPrefixedText(),
517 implode( ", ", $addedLinks ) );
518 $this->action = 'addurl';
523 global $wgCaptchaRegexes;
524 if ( $wgCaptchaRegexes ) {
525 // Custom regex checks
526 $oldtext = $this->loadText( $editPage, $section );
528 foreach ( $wgCaptchaRegexes as $regex ) {
529 $newMatches = array();
530 if ( preg_match_all( $regex, $newtext, $newMatches ) ) {
531 $oldMatches = array();
532 preg_match_all( $regex, $oldtext, $oldMatches );
534 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
536 $numHits = count( $addedMatches );
537 if ( $numHits > 0 ) {
539 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
543 $title->getPrefixedText(),
544 implode( ", ", $addedMatches ) );
545 $this->action = 'edit';
556 * Filter callback function for URL whitelisting
557 * @param string url to check
558 * @return bool true if unknown, false if whitelisted
561 function filterLink( $url ) {
562 global $wgCaptchaWhitelist;
563 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
565 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
567 : $this->buildRegexes( explode( "\n", $source ) );
569 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
570 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
572 return !( $cwl || $wl );
576 * Build regex from whitelist
577 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
578 * @return string Regex or bool false if whitelist is empty
581 function buildRegexes( $lines ) {
582 # Code duplicated from the SpamBlacklist extension (r19197)
584 # Strip comments and whitespace, then remove blanks
585 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
587 # No lines, don't make a regex which will match everything
588 if ( count( $lines ) == 0 ) {
589 wfDebug( "No lines\n" );
593 # It's faster using the S modifier even though it will usually only be run once
594 // $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
595 // return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
597 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
601 foreach ( $lines as $line ) {
602 // FIXME: not very robust size check, but should work. :)
603 if ( $build === false ) {
605 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
606 $regexes .= $regexStart .
607 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
611 $build .= '|' . $line;
614 if ( $build !== false ) {
615 $regexes .= $regexStart .
616 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
624 * Load external links from the externallinks table
625 * @param $title Title
628 function getLinksFromTracker( $title ) {
629 $dbr = wfGetDB( DB_SLAVE );
630 $id = $title->getArticleId(); // should be zero queries
631 $res = $dbr->select( 'externallinks', array( 'el_to' ),
632 array( 'el_from' => $id ), __METHOD__ );
634 foreach ( $res as $row ) {
635 $links[] = $row->el_to;
641 * Backend function for confirmEdit() and confirmEditAPI()
642 * @return bool false if the CAPTCHA is rejected, true otherwise
644 private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
645 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
646 if ( $this->passCaptcha() ) {
652 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
658 * The main callback run on edit attempts.
659 * @param EditPage $editPage
660 * @param string $newtext
661 * @param string $section
662 * @param bool $merged
663 * @return bool true to continue saving, false to abort and show a captcha form
665 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
666 if ( defined( 'MW_API' ) ) {
668 # The CAPTCHA was already checked and approved
671 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
672 $editPage->showEditForm( array( &$this, 'editCallback' ) );
679 * A more efficient edit filter callback based on the text after section merging
680 * @param EditPage $editPage
681 * @param string $newtext
683 function confirmEditMerged( $editPage, $newtext ) {
684 return $this->confirmEdit( $editPage, $newtext, false, true );
688 function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
689 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
690 $this->addCaptchaAPI( $resultArr );
697 * Hook for user creation form submissions.
699 * @param string $message
700 * @return bool true to continue, false to abort user creation
702 function confirmUserCreate( $u, &$message ) {
703 global $wgCaptchaTriggers, $wgUser;
704 if ( $wgCaptchaTriggers['createaccount'] ) {
705 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
706 wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
709 if ( $this->isIPWhitelisted() )
712 $this->trigger = "new account '" . $u->getName() . "'";
713 if ( !$this->passCaptcha() ) {
714 $message = wfMsg( 'captcha-createaccount-fail' );
722 * Hook for user login form submissions.
724 * @param string $message
725 * @return bool true to continue, false to abort user creation
727 function confirmUserLogin( $u, $pass, &$retval ) {
728 if ( $this->isBadLoginTriggered() ) {
729 if ( $this->isIPWhitelisted() )
732 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
733 if ( !$this->passCaptcha() ) {
734 // Emulate a bad-password return to confuse the shit out of attackers
735 $retval = LoginForm::WRONG_PASS;
743 * Check the captcha on Special:EmailUser
744 * @param $from MailAddress
745 * @param $to MailAddress
746 * @param $subject String
747 * @param $text String
748 * @param $error String reference
749 * @return Bool true to continue saving, false to abort and show a captcha form
751 function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
752 global $wgCaptchaTriggers, $wgUser;
753 if ( $wgCaptchaTriggers['sendemail'] ) {
754 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
755 wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
758 if ( $this->isIPWhitelisted() )
761 if ( defined( 'MW_API' ) ) {
763 # Asking for captchas in the API is really silly
764 $error = wfMsg( 'captcha-disabledinapi' );
767 $this->trigger = "{$wgUser->getName()} sending email";
768 if ( !$this->passCaptcha() ) {
769 $error = wfMsg( 'captcha-sendemail-fail' );
777 * Given a required captcha run, test form input for correct
778 * input on the open session.
779 * @return bool if passed, false if failed or new session
781 function passCaptcha() {
782 $info = $this->retrieveCaptcha();
785 if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
786 $this->log( "passed" );
787 $this->clearCaptcha( $info );
790 $this->clearCaptcha( $info );
791 $this->log( "bad form input" );
795 $this->log( "new captcha session" );
801 * Log the status and any triggering info for debugging or statistics
802 * @param string $message
804 function log( $message ) {
805 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
809 * Generate a captcha session ID and save the info in PHP's session storage.
810 * (Requires the user to have cookies enabled to get through the captcha.)
812 * A random ID is used so legit users can make edits in multiple tabs or
813 * windows without being unnecessarily hobbled by a serial order requirement.
814 * Pass the returned id value into the edit form as wpCaptchaId.
816 * @param array $info data to store
817 * @return string captcha ID key
819 function storeCaptcha( $info ) {
820 if ( !isset( $info['index'] ) ) {
821 // Assign random index if we're not udpating
822 $info['index'] = strval( mt_rand() );
824 CaptchaStore::get()->store( $info['index'], $info );
825 return $info['index'];
829 * Fetch this session's captcha info.
830 * @return mixed array of info, or false if missing
832 function retrieveCaptcha() {
834 $index = $wgRequest->getVal( 'wpCaptchaId' );
835 return CaptchaStore::get()->retrieve( $index );
839 * Clear out existing captcha info from the session, to ensure
840 * it can't be reused.
842 function clearCaptcha( $info ) {
843 CaptchaStore::get()->clear( $info['index'] );
847 * Retrieve the current version of the page or section being edited...
848 * @param EditPage $editPage
849 * @param string $section
853 function loadText( $editPage, $section ) {
854 $rev = Revision::newFromTitle( $editPage->mTitle );
855 if ( is_null( $rev ) ) {
858 $text = $rev->getText();
859 if ( $section != '' ) {
861 return $wgParser->getSection( $text, $section );
869 * Extract a list of all recognized HTTP links in the text.
870 * @param string $text
871 * @return array of strings
873 function findLinks( &$editpage, $text ) {
874 global $wgParser, $wgUser;
876 $options = new ParserOptions();
877 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
878 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
880 return array_keys( $out->getExternalLinks() );
884 * Show a page explaining what this wacky thing is.
886 function showHelp() {
888 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
889 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
890 if ( CaptchaStore::get()->cookiesNeeded() ) {
891 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );