Merge "(bug 40541) Fixed $wgSecureLogin functionality."
[lhc/web/wiklou.git] / includes / User.php
index f43844e..8216914 100644 (file)
@@ -286,7 +286,10 @@ class User {
                                $this->loadFromId();
                                break;
                        case 'session':
-                               $this->loadFromSession();
+                               if( !$this->loadFromSession() ) {
+                                       // Loading from session failed. Load defaults.
+                                       $this->loadDefaults();
+                               }
                                wfRunHooks( 'UserLoadAfterLoadFromSession', array( $this ) );
                                break;
                        default:
@@ -624,7 +627,7 @@ class User {
                // Certain names may be reserved for batch processes.
                foreach ( $reservedUsernames as $reserved ) {
                        if ( substr( $reserved, 0, 4 ) == 'msg:' ) {
-                               $reserved = wfMsgForContent( substr( $reserved, 4 ) );
+                               $reserved = wfMessage( substr( $reserved, 4 ) )->inContentLanguage()->text();
                        }
                        if ( $reserved == $name ) {
                                return false;
@@ -933,8 +936,7 @@ class User {
        }
 
        /**
-        * Load user data from the session or login cookie. If there are no valid
-        * credentials, initialises the user as an anonymous user.
+        * Load user data from the session or login cookie.
         * @return Bool True if the user is logged in, false otherwise.
         */
        private function loadFromSession() {
@@ -962,7 +964,6 @@ class User {
                if ( $cookieId !== null ) {
                        $sId = intval( $cookieId );
                        if( $sessId !== null && $cookieId != $sessId ) {
-                               $this->loadDefaults(); // Possible collision!
                                wfDebugLog( 'loginSessions', "Session user ID ($sessId) and
                                        cookie user ID ($sId) don't match!" );
                                return false;
@@ -971,7 +972,6 @@ class User {
                } elseif ( $sessId !== null && $sessId != 0 ) {
                        $sId = $sessId;
                } else {
-                       $this->loadDefaults();
                        return false;
                }
 
@@ -981,21 +981,18 @@ class User {
                        $sName = $request->getCookie( 'UserName' );
                        $request->setSessionData( 'wsUserName', $sName );
                } else {
-                       $this->loadDefaults();
                        return false;
                }
 
                $proposedUser = User::newFromId( $sId );
                if ( !$proposedUser->isLoggedIn() ) {
                        # Not a valid ID
-                       $this->loadDefaults();
                        return false;
                }
 
                global $wgBlockDisablesLogin;
                if( $wgBlockDisablesLogin && $proposedUser->isBlocked() ) {
                        # User blocked and we've disabled blocked user logins
-                       $this->loadDefaults();
                        return false;
                }
 
@@ -1007,7 +1004,6 @@ class User {
                        $from = 'cookie';
                } else {
                        # No session or persistent login cookie
-                       $this->loadDefaults();
                        return false;
                }
 
@@ -1019,7 +1015,6 @@ class User {
                } else {
                        # Invalid credentials
                        wfDebug( "User: can't log in from $from, invalid credentials\n" );
-                       $this->loadDefaults();
                        return false;
                }
        }
@@ -1305,13 +1300,13 @@ class User {
                        # Local list
                        if ( self::isLocallyBlockedProxy( $ip ) ) {
                                $block = new Block;
-                               $block->setBlocker( wfMsg( 'proxyblocker' ) );
-                               $block->mReason = wfMsg( 'proxyblockreason' );
+                               $block->setBlocker( wfMessage( 'proxyblocker' )->text() );
+                               $block->mReason = wfMessage( 'proxyblockreason' )->text();
                                $block->setTarget( $ip );
                        } elseif ( $this->isAnon() && $this->isDnsBlacklisted( $ip ) ) {
                                $block = new Block;
-                               $block->setBlocker( wfMsg( 'sorbs' ) );
-                               $block->mReason = wfMsg( 'sorbsreason' );
+                               $block->setBlocker( wfMessage( 'sorbs' )->text() );
+                               $block->mReason = wfMessage( 'sorbsreason' )->text();
                                $block->setTarget( $ip );
                        }
                }
@@ -2016,7 +2011,7 @@ class User {
 
                if( $str !== null ) {
                        if( !$wgAuth->allowPasswordChange() ) {
-                               throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
+                               throw new PasswordError( wfMessage( 'password-change-forbidden' )->text() );
                        }
 
                        if( !$this->isValidPassword( $str ) ) {
@@ -2029,12 +2024,12 @@ class User {
                                        $message = $valid;
                                        $params = array( $wgMinimalPasswordLength );
                                }
-                               throw new PasswordError( wfMsgExt( $message, array( 'parsemag' ), $params ) );
+                               throw new PasswordError( wfMessage( $message, $params )->text() );
                        }
                }
 
                if( !$wgAuth->setPassword( $this, $str ) ) {
-                       throw new PasswordError( wfMsg( 'externaldberror' ) );
+                       throw new PasswordError( wfMessage( 'externaldberror' )->text() );
                }
 
                $this->setInternalPassword( $str );
@@ -2798,9 +2793,13 @@ class User {
         * @param $value String Value to set
         * @param $exp Int Expiration time, as a UNIX time value;
         *                   if 0 or not specified, use the default $wgCookieExpiration
+        * @param $secure Bool
+        *  true: Force setting the secure attribute when setting the cookie
+        *  false: Force NOT setting the secure attribute when setting the cookie
+        *  null (default): Use the default ($wgCookieSecure) to set the secure attribute
         */
-       protected function setCookie( $name, $value, $exp = 0 ) {
-               $this->getRequest()->response()->setcookie( $name, $value, $exp );
+       protected function setCookie( $name, $value, $exp = 0, $secure = null ) {
+               $this->getRequest()->response()->setcookie( $name, $value, $exp, null, null, $secure );
        }
 
        /**
@@ -2816,8 +2815,9 @@ class User {
         *
         * @param $request WebRequest object to use; $wgRequest will be used if null
         *        is passed.
+        * @param $secure Whether to force secure/insecure cookies or use default
         */
-       public function setCookies( $request = null ) {
+       public function setCookies( $request = null, $secure = null ) {
                if ( $request === null ) {
                        $request = $this->getRequest();
                }
@@ -2856,9 +2856,18 @@ class User {
                        if ( $value === false ) {
                                $this->clearCookie( $name );
                        } else {
-                               $this->setCookie( $name, $value );
+                               $this->setCookie( $name, $value, 0, $secure );
                        }
                }
+
+               /**
+                * If wpStickHTTPS was selected, also set an insecure cookie that
+                * will cause the site to redirect the user to HTTPS, if they access
+                * it over HTTP. Bug 29898.
+                */
+               if ( $request->getCheck( 'wpStickHTTPS' ) ) {
+                       $this->setCookie( 'forceHTTPS', 'true', time() + 2592000, false ); //30 days
+               }
        }
 
        /**
@@ -2881,6 +2890,7 @@ class User {
 
                $this->clearCookie( 'UserID' );
                $this->clearCookie( 'Token' );
+               $this->clearCookie( 'forceHTTPS' );
 
                # Remember when user logged out, to prevent seeing cached pages
                $this->setCookie( 'LoggedOut', wfTimestampNow(), time() + 86400 );
@@ -2891,11 +2901,16 @@ class User {
         * @todo Only rarely do all these fields need to be set!
         */
        public function saveSettings() {
+               global $wgAuth;
+
                $this->load();
                if ( wfReadOnly() ) { return; }
                if ( 0 == $this->mId ) { return; }
 
                $this->mTouched = self::newTouchedTimestamp();
+               if ( !$wgAuth->allowSetLocalPassword() ) {
+                       $this->mPassword = '';
+               }
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->update( 'user',
@@ -3353,15 +3368,15 @@ class User {
                        $message = 'confirmemail_body_' . $type;
                }
 
-               return $this->sendMail( wfMsg( 'confirmemail_subject' ),
-                       wfMsg( $message,
+               return $this->sendMail( wfMessage( 'confirmemail_subject' )->text(),
+                       wfMessage( $message,
                                $this->getRequest()->getIP(),
                                $this->getName(),
                                $url,
                                $wgLang->timeanddate( $expiration, false ),
                                $invalidateURL,
                                $wgLang->date( $expiration, false ),
-                               $wgLang->time( $expiration, false ) ) );
+                               $wgLang->time( $expiration, false ) )->text() );
        }
 
        /**
@@ -4013,10 +4028,10 @@ class User {
                        $action = 'create2';
                        if ( $byEmail ) {
                                if ( $reason === '' ) {
-                                       $reason = wfMsgForContent( 'newuserlog-byemail' );
+                                       $reason = wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text();
                                } else {
                                        $reason = $wgContLang->commaList( array(
-                                               $reason, wfMsgForContent( 'newuserlog-byemail' ) ) );
+                                               $reason, wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text() ) );
                                }
                        }
                }
@@ -4185,8 +4200,8 @@ class User {
                /*
                if ( $wgMinimalPasswordLength > 1 ) {
                        $ret['pattern'] = '.{' . intval( $wgMinimalPasswordLength ) . ',}';
-                       $ret['title'] = wfMsgExt( 'passwordtooshort', 'parsemag',
-                               $wgMinimalPasswordLength );
+                       $ret['title'] = wfMessage( 'passwordtooshort' )
+                               ->numParams( $wgMinimalPasswordLength )->text();
                }
                */