3 class ConfirmEditHooks {
4 static function getInstance() {
5 global $wgCaptcha, $wgCaptchaClass, $wgExtensionMessagesFiles;
9 wfLoadExtensionMessages( 'ConfirmEdit' );
10 if ( isset( $wgExtensionMessagesFiles[$wgCaptchaClass] ) ) {
11 wfLoadExtensionMessages( $wgCaptchaClass );
13 $wgCaptcha = new $wgCaptchaClass;
18 static function confirmEdit( &$editPage, $newtext, $section ) {
19 return self::getInstance()->confirmEdit( $editPage, $newtext, $section );
22 static function confirmEditMerged( &$editPage, $newtext ) {
23 return self::getInstance()->confirmEditMerged( $editPage, $newtext );
26 static function confirmEditAPI( &$editPage, $newtext, &$resultArr ) {
27 return self::getInstance()->confirmEditAPI( $editPage, $newtext, $resultArr );
30 static function injectUserCreate( &$template ) {
31 return self::getInstance()->injectUserCreate( $template );
34 static function confirmUserCreate( $u, &$message ) {
35 return self::getInstance()->confirmUserCreate( $u, $message );
38 static function triggerUserLogin( $user, $password, $retval ) {
39 return self::getInstance()->triggerUserLogin( $user, $password, $retval );
42 static function injectUserLogin( &$template ) {
43 return self::getInstance()->injectUserLogin( $template );
46 static function confirmUserLogin( $u, $pass, &$retval ) {
47 return self::getInstance()->confirmUserLogin( $u, $pass, $retval );
51 class CaptchaSpecialPage extends UnlistedSpecialPage {
52 function execute( $par ) {
54 $instance = ConfirmEditHooks::getInstance();
57 return $instance->showImage();
60 return $instance->showHelp();
67 function SimpleCaptcha() {
68 global $wgCaptchaStorageClass;
69 $this->storage = new $wgCaptchaStorageClass;
72 function getCaptcha() {
75 $op = mt_rand(0, 1) ? '+' : '-';
78 $answer = ($op == '+') ? ($a + $b) : ($a - $b);
79 return array('question' => $test, 'answer' => $answer);
82 function addCaptchaAPI(&$resultArr) {
83 $captcha = $this->getCaptcha();
84 $index = $this->storeCaptcha( $captcha );
85 $resultArr['captcha']['type'] = 'simple';
86 $resultArr['captcha']['id'] = $index;
87 $resultArr['captcha']['question'] = $captcha['question'];
91 * Insert a captcha prompt into the edit form.
92 * This sample implementation generates a simple arithmetic operation;
93 * it would be easy to defeat by machine.
100 $captcha = $this->getCaptcha();
101 $index = $this->storeCaptcha( $captcha );
103 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
104 wfElement( 'input', array(
105 'name' => 'wpCaptchaWord',
106 'id' => 'wpCaptchaWord',
107 'tabindex' => 1 ) ) . // tab in before the edit textarea
109 wfElement( 'input', array(
111 'name' => 'wpCaptchaId',
112 'id' => 'wpCaptchaId',
113 'value' => $index ) );
117 * Insert the captcha prompt into an edit form.
118 * @param OutputPage $out
120 function editCallback( &$out ) {
121 $out->addWikiText( $this->getMessage( $this->action ) );
122 $out->addHTML( $this->getForm() );
126 * Show a message asking the user to enter a captcha on edit
127 * The result will be treated as wiki text
129 * @param $action Action being performed
132 function getMessage( $action ) {
133 $name = 'captcha-' . $action;
134 $text = wfMsg( $name );
135 # Obtain a more tailored message, if possible, otherwise, fall back to
136 # the default for edits
137 return wfEmptyMsg( $name, $text ) ? wfMsg( 'captcha-edit' ) : $text;
142 * @fixme if multiple thingies insert a header, could break
143 * @param SimpleTemplate $template
144 * @return bool true to keep running callbacks
146 function injectUserCreate( &$template ) {
147 global $wgCaptchaTriggers, $wgOut;
148 if( $wgCaptchaTriggers['createaccount'] ) {
149 $template->set( 'header',
150 "<div class='captcha'>" .
151 $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
159 * Inject a captcha into the user login form after a failed
160 * password attempt as a speedbump for mass attacks.
161 * @fixme if multiple thingies insert a header, could break
162 * @param SimpleTemplate $template
163 * @return bool true to keep running callbacks
165 function injectUserLogin( &$template ) {
166 if( $this->isBadLoginTriggered() ) {
168 $template->set( 'header',
169 "<div class='captcha'>" .
170 $wgOut->parse( $this->getMessage( 'badlogin' ) ) .
178 * When a bad login attempt is made, increment an expiring counter
179 * in the memcache cloud. Later checks for this may trigger a
180 * captcha display to prevent too many hits from the same place.
182 * @param string $password
183 * @param int $retval authentication return value
184 * @return bool true to keep running callbacks
186 function triggerUserLogin( $user, $password, $retval ) {
187 global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration, $wgMemc;
188 if( $retval == LoginForm::WRONG_PASS && $wgCaptchaTriggers['badlogin'] ) {
189 $key = $this->badLoginKey();
190 $count = $wgMemc->get( $key );
192 $wgMemc->add( $key, 0, $wgCaptchaBadLoginExpiration );
194 $count = $wgMemc->incr( $key );
200 * Check if a bad login has already been registered for this
201 * IP address. If so, require a captcha.
205 function isBadLoginTriggered() {
207 return intval( $wgMemc->get( $this->badLoginKey() ) ) > 0;
211 * Internal cache key for badlogin checks.
215 function badLoginKey() {
216 return wfMemcKey( 'captcha', 'badlogin', 'ip', wfGetIP() );
220 * Check if the submitted form matches the captcha session data provided
221 * by the plugin when the form was generated.
225 * @param string $answer
229 function keyMatch( $answer, $info ) {
230 return $answer == $info['answer'];
233 // ----------------------------------
236 * @param EditPage $editPage
237 * @param string $action (edit/create/addurl...)
238 * @return bool true if action triggers captcha on editPage's namespace
240 function captchaTriggers( &$editPage, $action) {
241 global $wgCaptchaTriggers, $wgCaptchaTriggersOnNamespace;
242 //Special config for this NS?
243 if (isset( $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action] ) )
244 return $wgCaptchaTriggersOnNamespace[$editPage->mTitle->getNamespace()][$action];
246 return ( !empty( $wgCaptchaTriggers[$action] ) ); //Default
251 * @param EditPage $editPage
252 * @param string $newtext
253 * @param string $section
254 * @return bool true if the captcha should run
256 function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
258 $title = $editPage->mArticle->getTitle();
261 if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
262 wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
265 global $wgCaptchaWhitelistIP;
266 if( !empty( $wgCaptchaWhitelistIP ) ) {
268 foreach ( $wgCaptchaWhitelistIP as $range ) {
269 if ( IP::isInRange( $ip, $range ) ) {
276 global $wgEmailAuthentication, $ceAllowConfirmedEmail;
277 if( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
278 $wgUser->isEmailConfirmed() ) {
279 wfDebug( "ConfirmEdit: user has confirmed mail, skipping captcha\n" );
283 if( $this->captchaTriggers( $editPage, 'edit' ) ) {
284 // Check on all edits
286 $this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
288 $title->getPrefixedText() );
289 $this->action = 'edit';
290 wfDebug( "ConfirmEdit: checking all edits...\n" );
294 if( $this->captchaTriggers( $editPage, 'create' ) && !$editPage->mTitle->exists() ) {
295 //Check if creating a page
297 $this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
299 $title->getPrefixedText() );
300 $this->action = 'create';
301 wfDebug( "ConfirmEdit: checking on page creation...\n" );
305 if( $this->captchaTriggers( $editPage, 'addurl' ) ) {
306 // Only check edits that add URLs
308 // Get links from the database
309 $oldLinks = $this->getLinksFromTracker( $title );
310 // Share a parse operation with Article::doEdit()
311 $editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
312 $newLinks = array_keys( $editInfo->output->getExternalLinks() );
314 // Get link changes in the slowest way known to man
315 $oldtext = $this->loadText( $editPage, $section );
316 $oldLinks = $this->findLinks( $oldtext );
317 $newLinks = $this->findLinks( $newtext );
320 $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
321 $addedLinks = array_diff( $unknownLinks, $oldLinks );
322 $numLinks = count( $addedLinks );
324 if( $numLinks > 0 ) {
326 $this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
329 $title->getPrefixedText(),
330 implode( ", ", $addedLinks ) );
331 $this->action = 'addurl';
336 global $wgCaptchaRegexes;
337 if( !empty( $wgCaptchaRegexes ) ) {
338 // Custom regex checks
339 $oldtext = $this->loadText( $editPage, $section );
341 foreach( $wgCaptchaRegexes as $regex ) {
342 $newMatches = array();
343 if( preg_match_all( $regex, $newtext, $newMatches ) ) {
344 $oldMatches = array();
345 preg_match_all( $regex, $oldtext, $oldMatches );
347 $addedMatches = array_diff( $newMatches[0], $oldMatches[0] );
349 $numHits = count( $addedMatches );
352 $this->trigger = sprintf( "%dx %s at [[%s]]: %s",
356 $title->getPrefixedText(),
357 implode( ", ", $addedMatches ) );
358 $this->action = 'edit';
369 * Filter callback function for URL whitelisting
370 * @param string url to check
371 * @return bool true if unknown, false if whitelisted
374 function filterLink( $url ) {
375 global $wgCaptchaWhitelist;
376 $source = wfMsgForContent( 'captcha-addurl-whitelist' );
378 $whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
380 : $this->buildRegexes( explode( "\n", $source ) );
382 $cwl = $wgCaptchaWhitelist !== false ? preg_match( $wgCaptchaWhitelist, $url ) : false;
383 $wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
385 return !( $cwl || $wl );
389 * Build regex from whitelist
390 * @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
391 * @return string Regex or bool false if whitelist is empty
394 function buildRegexes( $lines ) {
395 # Code duplicated from the SpamBlacklist extension (r19197)
397 # Strip comments and whitespace, then remove blanks
398 $lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
400 # No lines, don't make a regex which will match everything
401 if ( count( $lines ) == 0 ) {
402 wfDebug( "No lines\n" );
406 # It's faster using the S modifier even though it will usually only be run once
407 //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
408 //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
410 $regexStart = '/http:\/\/+[a-z0-9_\-.]*(';
414 foreach( $lines as $line ) {
415 // FIXME: not very robust size check, but should work. :)
416 if( $build === false ) {
418 } elseif( strlen( $build ) + strlen( $line ) > $regexMax ) {
419 $regexes .= $regexStart .
420 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
424 $build .= '|' . $line;
427 if( $build !== false ) {
428 $regexes .= $regexStart .
429 str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $build) ) .
437 * Load external links from the externallinks table
439 function getLinksFromTracker( $title ) {
440 $dbr =& wfGetDB( DB_SLAVE );
441 $id = $title->getArticleId(); // should be zero queries
442 $res = $dbr->select( 'externallinks', array( 'el_to' ),
443 array( 'el_from' => $id ), __METHOD__ );
445 while ( $row = $dbr->fetchObject( $res ) ) {
446 $links[] = $row->el_to;
452 * Backend function for confirmEdit() and confirmEditAPI()
453 * @return bool false if the CAPTCHA is rejected, true otherwise
455 private function doConfirmEdit( &$editPage, $newtext, $section, $merged = false ) {
456 if( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
457 if( $this->passCaptcha() ) {
463 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
469 * The main callback run on edit attempts.
470 * @param EditPage $editPage
471 * @param string $newtext
472 * @param string $section
473 * @param bool $merged
474 * @return bool true to continue saving, false to abort and show a captcha form
476 function confirmEdit( &$editPage, $newtext, $section, $merged = false ) {
478 if( is_null( $wgTitle ) ) {
480 # The CAPTCHA was already checked and approved
483 if( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) {
484 $editPage->showEditForm( array( &$this, 'editCallback' ) );
491 * A more efficient edit filter callback based on the text after section merging
492 * @param EditPage $editPage
493 * @param string $newtext
495 function confirmEditMerged( &$editPage, $newtext ) {
496 return $this->confirmEdit( $editPage, $newtext, false, true );
500 function confirmEditAPI( &$editPage, $newtext, &$resultArr) {
501 if( !$this->doConfirmEdit( $editPage, $newtext, false, false ) ) {
502 $this->addCaptchaAPI($resultArr);
509 * Hook for user creation form submissions.
511 * @param string $message
512 * @return bool true to continue, false to abort user creation
514 function confirmUserCreate( $u, &$message ) {
515 global $wgCaptchaTriggers;
516 if( $wgCaptchaTriggers['createaccount'] ) {
517 $this->trigger = "new account '" . $u->getName() . "'";
518 if( !$this->passCaptcha() ) {
519 $message = wfMsg( 'captcha-createaccount-fail' );
527 * Hook for user login form submissions.
529 * @param string $message
530 * @return bool true to continue, false to abort user creation
532 function confirmUserLogin( $u, $pass, &$retval ) {
533 if( $this->isBadLoginTriggered() ) {
534 $this->trigger = "post-badlogin login '" . $u->getName() . "'";
535 if( !$this->passCaptcha() ) {
536 $message = wfMsg( 'captcha-badlogin-fail' );
537 // Emulate a bad-password return to confuse the shit out of attackers
538 $retval = LoginForm::WRONG_PASS;
546 * Given a required captcha run, test form input for correct
547 * input on the open session.
548 * @return bool if passed, false if failed or new session
550 function passCaptcha() {
551 $info = $this->retrieveCaptcha();
554 if( $this->keyMatch( $wgRequest->getVal('wpCaptchaWord'), $info ) ) {
555 $this->log( "passed" );
556 $this->clearCaptcha( $info );
559 $this->clearCaptcha( $info );
560 $this->log( "bad form input" );
564 $this->log( "new captcha session" );
570 * Log the status and any triggering info for debugging or statistics
571 * @param string $message
573 function log( $message ) {
574 wfDebugLog( 'captcha', 'ConfirmEdit: ' . $message . '; ' . $this->trigger );
578 * Generate a captcha session ID and save the info in PHP's session storage.
579 * (Requires the user to have cookies enabled to get through the captcha.)
581 * A random ID is used so legit users can make edits in multiple tabs or
582 * windows without being unnecessarily hobbled by a serial order requirement.
583 * Pass the returned id value into the edit form as wpCaptchaId.
585 * @param array $info data to store
586 * @return string captcha ID key
588 function storeCaptcha( $info ) {
589 if( !isset( $info['index'] ) ) {
590 // Assign random index if we're not udpating
591 $info['index'] = strval( mt_rand() );
593 $this->storage->store( $info['index'], $info );
594 return $info['index'];
598 * Fetch this session's captcha info.
599 * @return mixed array of info, or false if missing
601 function retrieveCaptcha() {
603 $index = $wgRequest->getVal( 'wpCaptchaId' );
604 return $this->storage->retrieve( $index );
608 * Clear out existing captcha info from the session, to ensure
609 * it can't be reused.
611 function clearCaptcha( $info ) {
612 $this->storage->clear( $info['index'] );
616 * Retrieve the current version of the page or section being edited...
617 * @param EditPage $editPage
618 * @param string $section
622 function loadText( $editPage, $section ) {
623 $rev = Revision::newFromTitle( $editPage->mTitle );
624 if( is_null( $rev ) ) {
627 $text = $rev->getText();
628 if( $section != '' ) {
629 return Article::getSection( $text, $section );
637 * Extract a list of all recognized HTTP links in the text.
638 * @param string $text
639 * @return array of strings
641 function findLinks( $text ) {
642 global $wgParser, $wgTitle, $wgUser;
644 $options = new ParserOptions();
645 $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
646 $out = $wgParser->parse( $text, $wgTitle, $options );
648 return array_keys( $out->getExternalLinks() );
652 * Show a page explaining what this wacky thing is.
654 function showHelp() {
655 global $wgOut, $ceAllowConfirmedEmail;
656 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
657 $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
658 if ( $this->storage->cookiesNeeded() ) {
659 $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
665 class CaptchaSessionStore {
666 function store( $index, $info ) {
667 $_SESSION['captcha' . $info['index']] = $info;
670 function retrieve( $index ) {
671 if( isset( $_SESSION['captcha' . $index] ) ) {
672 return $_SESSION['captcha' . $index];
678 function clear( $index ) {
679 unset( $_SESSION['captcha' . $index] );
682 function cookiesNeeded() {
687 class CaptchaCacheStore {
688 function store( $index, $info ) {
689 global $wgMemc, $wgCaptchaSessionExpiration;
690 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
691 $wgCaptchaSessionExpiration );
694 function retrieve( $index ) {
696 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
704 function clear( $index ) {
706 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
709 function cookiesNeeded() {