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 // 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
264 Xml::element( 'input', array(
266 'name' => 'wpCaptchaId',
267 'id' => 'wpCaptchaId',
268 'value' => $index ) );
272 * Insert the captcha prompt into an edit form.
273 * @param OutputPage $out
275 function editCallback( &$out ) {
276 $out->addWikiText( $this->getMessage( $this->action ) );
277 $out->addHTML( $this->getForm() );
281 * Show a message asking the user to enter a captcha on edit
282 * The result will be treated as wiki text
284 * @param $action Action being performed
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;
297 * @fixme if multiple thingies insert a header, could break
298 * @param $form HTMLForm
299 * @return bool true to keep running callbacks
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" );
308 $form->addFooterText(
309 "<div class='captcha'>" .
310 $wgOut->parse( $this->getMessage( 'sendemail' ) ) .
319 * @fixme if multiple thingies insert a header, could break
320 * @param QuickTemplate $template
321 * @return bool true to keep running callbacks
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" );
330 $template->set( 'header',
331 "<div class='captcha'>" .
332 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
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
346 function injectUserLogin( &$template ) {
347 if ( $this->isBadLoginTriggered() ) {
349 $template->set( 'header',
350 "<div class='captcha'>" .
351 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
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.
363 * @param string $password
364 * @param int $retval authentication return value
365 * @return bool true to keep running callbacks
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 );
373 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
375 $count = $wgMemc->incr( $key );
381 * Check if a bad login has already been registered for this
382 * IP address. If so, require a captcha.
386 function isBadLoginTriggered() {
387 global $wgMemc, $wgCaptchaBadLoginAttempts;
388 return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
392 * Check if the IP is allowed to skip captchas
394 function isIPWhitelisted() {
395 global $wgCaptchaWhitelistIP;
396 if ( $wgCaptchaWhitelistIP ) {
398 foreach ( $wgCaptchaWhitelistIP as $range ) {
399 if ( IP::isInRange( $ip, $range ) ) {
408 * Internal cache key for badlogin checks.
412 function badLoginKey() {
413 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
417 * Check if the submitted form matches the captcha session data provided
418 * by the plugin when the form was generated.
422 * @param string $answer
426 function keyMatch( $answer, $info ) {
427 return $answer == $info['answer'];
430 // ----------------------------------
433 * @param EditPage $editPage
434 * @param string $action (edit/create/addurl...)
435 * @return bool true if action triggers captcha on editPage's namespace
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];
443 return ( !empty( $wgCaptchaTriggers[$action] ) ); // Default
447 * @param EditPage $editPage
448 * @param string $newtext
449 * @param string $section
450 * @return bool true if the captcha should run
452 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
454 $title = $editPage->mArticle->getTitle();
457 if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
458 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
461 if ( $this->isIPWhitelisted() )
465 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
466 if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
467 $wgUser->isEmailConfirmed() ) {
468 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
472 if ( $this->captchaTriggers( $editPage, 'edit' ) ) {
473 // Check on all edits
475 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
477 $title->getPrefixedText() );
478 $this->action = 'edit';
479 wfDebug( "ConfirmEdit: checking all edits...\n" );
483 if ( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
484 // Check if creating a page
486 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
488 $title->getPrefixedText() );
489 $this->action = 'create';
490 wfDebug( "ConfirmEdit: checking on page creation...\n" );
494 if ( $this->captchaTriggers( $editPage, 'addurl' ) ) {
495 // Only check edits that add URLs
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() );
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 );
509 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
510 $addedLinks = array_diff( $unknownLinks, $oldLinks );
511 $numLinks = count( $addedLinks );
513 if ( $numLinks > 0 ) {
515 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
518 $title->getPrefixedText(),
519 implode( ", ", $addedLinks ) );
520 $this->action = 'addurl';
525 global $wgCaptchaRegexes;
526 if ( $wgCaptchaRegexes ) {
527 // Custom regex checks
528 $oldtext = $this->loadText( $editPage, $section );
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 );
536 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
538 $numHits = count( $addedMatches );
539 if ( $numHits > 0 ) {
541 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
545 $title->getPrefixedText(),
546 implode( ", ", $addedMatches ) );
547 $this->action = 'edit';
558 * Filter callback function for URL whitelisting
559 * @param string url to check
560 * @return bool true if unknown, false if whitelisted
563 function filterLink( $url ) {
564 global $wgCaptchaWhitelist;
565 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
567 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
569 : $this->buildRegexes( explode( "\n", $source ) );
571 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
572 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
574 return !( $cwl || $wl );
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
583 function buildRegexes( $lines ) {
584 # Code duplicated from the SpamBlacklist extension (r19197)
586 # Strip comments and whitespace, then remove blanks
587 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
589 # No lines, don't make a regex which will match everything
590 if ( count( $lines ) == 0 ) {
591 wfDebug( "No lines\n" );
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';
599 $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
603 foreach ( $lines as $line ) {
604 // FIXME: not very robust size check, but should work. :)
605 if ( $build === false ) {
607 } elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
608 $regexes .= $regexStart .
609 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
613 $build .= '|' . $line;
616 if ( $build !== false ) {
617 $regexes .= $regexStart .
618 str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
626 * Load external links from the externallinks table
627 * @param $title Title
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__ );
636 foreach ( $res as $row ) {
637 $links[] = $row->el_to;
643 * Backend function for confirmEdit() and confirmEditAPI()
644 * @return bool false if the CAPTCHA is rejected, true otherwise
646 private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
647 if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
648 if ( $this->passCaptcha() ) {
654 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
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
667 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
668 if ( defined( 'MW_API' ) ) {
670 # The CAPTCHA was already checked and approved
673 if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
674 $editPage->showEditForm( array( &$this, 'editCallback' ) );
681 * A more efficient edit filter callback based on the text after section merging
682 * @param EditPage $editPage
683 * @param string $newtext
685 function confirmEditMerged( $editPage, $newtext ) {
686 return $this->confirmEdit( $editPage, $newtext, false, true );
690 function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
691 if ( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
692 $this->addCaptchaAPI( $resultArr );
699 * Hook for user creation form submissions.
701 * @param string $message
702 * @return bool true to continue, false to abort user creation
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" );
711 if ( $this->isIPWhitelisted() )
714 $this->trigger = "new account '" . $u->getName() . "'";
715 if ( !$this->passCaptcha() ) {
716 $message = wfMsg( 'captcha-createaccount-fail' );
724 * Hook for user login form submissions.
726 * @param string $message
727 * @return bool true to continue, false to abort user creation
729 function confirmUserLogin( $u, $pass, &$retval ) {
730 if ( $this->isBadLoginTriggered() ) {
731 if ( $this->isIPWhitelisted() )
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;
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
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" );
760 if ( $this->isIPWhitelisted() )
763 if ( defined( 'MW_API' ) ) {
765 # Asking for captchas in the API is really silly
766 $error = wfMsg( 'captcha-disabledinapi' );
769 $this->trigger = "{$wgUser->getName()} sending email";
770 if ( !$this->passCaptcha() ) {
771 $error = wfMsg( 'captcha-sendemail-fail' );
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
783 function passCaptcha() {
784 $info = $this->retrieveCaptcha();
787 if ( $this->keyMatch( $wgRequest->getVal( 'wpCaptchaWord' ), $info ) ) {
788 $this->log( "passed" );
789 $this->clearCaptcha( $info );
792 $this->clearCaptcha( $info );
793 $this->log( "bad form input" );
797 $this->log( "new captcha session" );
803 * Log the status and any triggering info for debugging or statistics
804 * @param string $message
806 function log( $message ) {
807 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
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.)
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.
818 * @param array $info data to store
819 * @return string captcha ID key
821 function storeCaptcha( $info ) {
822 if ( !isset( $info['index'] ) ) {
823 // Assign random index if we're not udpating
824 $info['index'] = strval( mt_rand() );
826 CaptchaStore::get()->store( $info['index'], $info );
827 return $info['index'];
831 * Fetch this session's captcha info.
832 * @return mixed array of info, or false if missing
834 function retrieveCaptcha() {
836 $index = $wgRequest->getVal( 'wpCaptchaId' );
837 return CaptchaStore::get()->retrieve( $index );
841 * Clear out existing captcha info from the session, to ensure
842 * it can't be reused.
844 function clearCaptcha( $info ) {
845 CaptchaStore::get()->clear( $info['index'] );
849 * Retrieve the current version of the page or section being edited...
850 * @param EditPage $editPage
851 * @param string $section
855 function loadText( $editPage, $section ) {
856 $rev = Revision::newFromTitle( $editPage->mTitle );
857 if ( is_null( $rev ) ) {
860 $text = $rev->getText();
861 if ( $section != '' ) {
863 return $wgParser->getSection( $text, $section );
871 * Extract a list of all recognized HTTP links in the text.
872 * @param string $text
873 * @return array of strings
875 function findLinks( &$editpage, $text ) {
876 global $wgParser, $wgUser;
878 $options = new ParserOptions();
879 $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
880 $out = $wgParser->parse( $text, $editpage->mTitle, $options );
882 return array_keys( $out->getExternalLinks() );
886 * Show a page explaining what this wacky thing is.
888 function showHelp() {
890 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
891 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
892 if ( CaptchaStore::get()->cookiesNeeded() ) {
893 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );