* (bug 12370) Add throttle on password attempts. Defaults to max 5 attempts in 5...
[lhc/web/wiklou.git] / includes / specials / SpecialUserlogin.php
index 179ef3f..f597d62 100644 (file)
@@ -33,6 +33,7 @@ class LoginForm {
        const RESET_PASS = 7;
        const ABORTED = 8;
        const CREATE_BLOCKED = 9;
+       const THROTTLED = 10;
 
        var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
        var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
@@ -130,7 +131,7 @@ class LoginForm {
                wfRunHooks( 'AddNewAccount', array( $u, true ) );
 
                $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
 
                if( WikiError::isError( $result ) ) {
@@ -181,7 +182,7 @@ class LoginForm {
                        $wgUser->setCookies();
                        wfRunHooks( 'AddNewAccount', array( $wgUser ) );
                        if( $this->hasSessionCookie() ) {
-                               return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
+                               return $this->successfulLogin( 'welcomecreation', $wgUser->getName(), false );
                        } else {
                                return $this->cookieRedirectCheck( 'new' );
                        }
@@ -372,6 +373,23 @@ class LoginForm {
                if ( '' == $this->mName ) {
                        return self::NO_NAME;
                }
+               
+               global $wgPasswordAttemptThrottle;
+               if (is_array($wgPasswordAttemptThrottle) && count($wgPasswordAttemptThrottle) >=2) {
+                       list($count,$period) = $wgPasswordAttemptThrottle;
+                       $key = wfMemcKey( 'password-throttle', wfGetIP(), $this->mName );
+                       
+                       global $wgMemc;
+                       $cur = $wgMemc->get($key);
+                       if ($cur>0 && $cur<$count) {
+                               $wgMemc->incr($key);
+                               // Okay
+                       } elseif ($cur>0) {
+                               return self::THROTTLED;
+                       } elseif (!$cur) {
+                               $wgMemc->add( $key, 1, $period );
+                       }
+               }
 
                // Load $wgUser now, and check to see if we're logging in as the same name. 
                // This is necessary because loading $wgUser (say by calling getName()) calls
@@ -509,7 +527,7 @@ class LoginForm {
                                        global $wgLang, $wgRequest;
                                        $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
                                        $wgLang = Language::factory( $code );
-                                       return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
+                                       return $this->successfulLogin( 'loginsuccess', $wgUser->getName() );
                                } else {
                                        return $this->cookieRedirectCheck( 'login' );
                                }
@@ -541,6 +559,9 @@ class LoginForm {
                        case self::CREATE_BLOCKED:
                                $this->userBlockedMessage();
                                break;
+                       case self::THROTTLED:
+                               $this->mainLoginForm( wfMsg( 'login-throttled' ) );
+                               break;
                        default:
                                throw new MWException( "Unhandled case value" );
                }
@@ -640,11 +661,12 @@ class LoginForm {
 
 
        /**
-        * @param string $msg Message that will be shown on success
+        * @param string $msg Message key that will be shown on success
+        * @param $params String: parameters for the above message
         * @param bool $auto Toggle auto-redirect to main page; default true
         * @private
         */
-       function successfulLogin( $msg, $auto = true ) {
+       function successfulLogin( $msg, $params, $auto = true ) {
                global $wgUser;
                global $wgOut;
 
@@ -654,9 +676,9 @@ class LoginForm {
                wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
 
                $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
-               $wgOut->addWikiText( $msg );
+               $wgOut->addWikiMsgArray( $msg, $params );
                $wgOut->addHtml( $injected_html );
                if ( !empty( $this->mReturnTo ) ) {
                        $wgOut->returnToMain( $auto, $this->mReturnTo );
@@ -670,7 +692,7 @@ class LoginForm {
                global $wgOut;
 
                $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
 
                $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
@@ -693,7 +715,7 @@ class LoginForm {
                # out.
 
                $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
 
                $ip = wfGetIP();
@@ -809,7 +831,7 @@ class LoginForm {
                }
 
                $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
                $wgOut->disallowUserJs();  // just in case...
                $wgOut->addTemplate( $template );
@@ -870,7 +892,7 @@ class LoginForm {
                                return $this->mainLoginForm( wfMsg( 'error' ) );
                        }
                } else {
-                       return $this->successfulLogin( wfMsgExt( 'loginsuccess', array( 'parseinline' ), $wgUser->getName() ) );
+                       return $this->successfulLogin( 'loginsuccess', $wgUser->getName() );
                }
        }