Merge "Remove adittional type hinting stuff because PHP will die if its there >_>"
[lhc/web/wiklou.git] / includes / specials / SpecialUserlogin.php
index f73bfb7..7d91096 100644 (file)
@@ -72,8 +72,6 @@ class LoginForm extends SpecialPage {
 
        /**
         * Loader
-        *
-        * @param $request WebRequest object
         */
        function load() {
                global $wgAuth, $wgHiddenPrefs, $wgEnableEmail, $wgRedirectOnLogin;
@@ -127,24 +125,34 @@ class LoginForm extends SpecialPage {
                }
 
                if( !$wgAuth->validDomain( $this->mDomain ) ) {
-                       $this->mDomain = 'invaliddomain';
+                       $this->mDomain = $wgAuth->getDomain();
                }
                $wgAuth->setDomain( $this->mDomain );
 
-               # When switching accounts, it sucks to get automatically logged out
+               # 1. When switching accounts, it sucks to get automatically logged out
+               # 2. Do not return to PasswordReset after a successful password change
+               #    but goto Wiki start page (Main_Page) instead ( bug 33997 )
                $returnToTitle = Title::newFromText( $this->mReturnTo );
-               if( is_object( $returnToTitle ) && $returnToTitle->isSpecial( 'Userlogout' ) ) {
+               if( is_object( $returnToTitle ) && (
+                       $returnToTitle->isSpecial( 'Userlogout' )
+                       || $returnToTitle->isSpecial( 'PasswordReset' ) ) ) {
                        $this->mReturnTo = '';
                        $this->mReturnToQuery = '';
                }
        }
 
+       function getDescription() {
+               return $this->msg( $this->getUser()->isAllowed( 'createaccount' ) ?
+                       'userlogin' : 'userloginnocreate' )->text();
+       }
+
        public function execute( $par ) {
                if ( session_id() == '' ) {
                        wfSetupSession();
                }
 
                $this->load();
+               $this->setHeaders();
 
                if ( $par == 'signup' ) { # Check for [[Special:Userlogin/signup]]
                        $this->mType = 'signup';
@@ -155,11 +163,14 @@ class LoginForm extends SpecialPage {
                        return;
                } elseif( $this->mPosted ) {
                        if( $this->mCreateaccount ) {
-                               return $this->addNewAccount();
+                               $this->addNewAccount();
+                               return;
                        } elseif ( $this->mCreateaccountMail ) {
-                               return $this->addNewAccountMailPassword();
+                               $this->addNewAccountMailPassword();
+                               return;
                        } elseif ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
-                               return $this->processLogin();
+                               $this->processLogin();
+                               return;
                        }
                }
                $this->mainLoginForm( '' );
@@ -189,7 +200,7 @@ class LoginForm extends SpecialPage {
                $u->addNewUserLogEntry( true, $this->mReason );
 
                $out = $this->getOutput();
-               $out->setPageTitleMsg( 'accmailtitle' );
+               $out->setPageTitle( $this->msg( 'accmailtitle' ) );
 
                if( !$result->isGood() ) {
                        $this->mainLoginForm( $this->msg( 'mailerror', $result->getWikiText() )->text() );
@@ -201,6 +212,7 @@ class LoginForm extends SpecialPage {
 
        /**
         * @private
+        * @return bool
         */
        function addNewAccount() {
                global $wgUser, $wgEmailAuthentication, $wgLoginLanguageSelector;
@@ -208,7 +220,7 @@ class LoginForm extends SpecialPage {
                # Create the account and abort if there's a problem doing so
                $u = $this->addNewAccountInternal();
                if( $u == null ) {
-                       return;
+                       return false;
                }
 
                # If we showed up language selection links, and one was in use, be
@@ -245,23 +257,24 @@ class LoginForm extends SpecialPage {
                        wfRunHooks( 'AddNewAccount', array( $u, false ) );
                        $u->addNewUserLogEntry();
                        if( $this->hasSessionCookie() ) {
-                               return $this->successfulCreation();
+                               $this->successfulCreation();
                        } else {
-                               return $this->cookieRedirectCheck( 'new' );
+                               $this->cookieRedirectCheck( 'new' );
                        }
                } else {
                        # Confirm that the account was created
-                       $out->setPageTitleMsg( 'accountcreated' );
+                       $out->setPageTitle( $this->msg( 'accountcreated' ) );
                        $out->addWikiMsg( 'accountcreatedtext', $u->getName() );
                        $out->returnToMain( false, $this->getTitle() );
                        wfRunHooks( 'AddNewAccount', array( $u, false ) );
                        $u->addNewUserLogEntry( false, $this->mReason );
-                       return true;
                }
+               return true;
        }
 
        /**
         * @private
+        * @return bool|\User
         */
        function addNewAccountInternal() {
                global $wgAuth, $wgMemc, $wgAccountCreationThrottle,
@@ -320,7 +333,7 @@ class LoginForm extends SpecialPage {
 
                $ip = $this->getRequest()->getIP();
                if ( $currentUser->isDnsBlacklisted( $ip, true /* check $wgProxyWhitelist */ ) ) {
-                       $this->mainLoginForm( $this->msg( 'sorbs_create_account_reason' )->text() . ' (' . htmlspecialchars( $ip ) . ')' );
+                       $this->mainLoginForm( $this->msg( 'sorbs_create_account_reason' )->text() . ' ' . $this->msg( 'parentheses', $ip )->escaped() );
                        return false;
                }
 
@@ -462,6 +475,7 @@ class LoginForm extends SpecialPage {
         * This may create a local account as a side effect if the
         * authentication plugin allows transparent local account
         * creation.
+        * @return int
         */
        public function authenticateUserData() {
                global $wgUser, $wgAuth;
@@ -602,7 +616,7 @@ class LoginForm extends SpecialPage {
                return $retval;
        }
 
-       /*
+       /**
         * Increment the login attempt throttle hit count for the (username,current IP)
         * tuple unless the throttle was already reached.
         * @param $username string The user name
@@ -631,7 +645,7 @@ class LoginForm extends SpecialPage {
                return $throttleCount;
        }
 
-       /*
+       /**
         * Clear the login attempt throttle hit count for the (username,current IP) tuple.
         * @param $username string The user name
         * @return void
@@ -730,10 +744,10 @@ class LoginForm extends SpecialPage {
                                        $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
                                        $userLang = Language::factory( $code );
                                        $wgLang = $userLang;
-                                       $this->getContext()->setLang( $userLang );
-                                       return $this->successfulLogin();
+                                       $this->getContext()->setLanguage( $userLang );
+                                       $this->successfulLogin();
                                } else {
-                                       return $this->cookieRedirectCheck( 'login' );
+                                       $this->cookieRedirectCheck( 'login' );
                                }
                                break;
 
@@ -753,7 +767,7 @@ class LoginForm extends SpecialPage {
                        case self::NOT_EXISTS:
                                if( $this->getUser()->isAllowed( 'createaccount' ) ) {
                                        $this->mainLoginForm( $this->msg( 'nosuchuser',
-                                          wfEscapeWikiText( $this->mUsername ) )->parse() );
+                                               wfEscapeWikiText( $this->mUsername ) )->parse() );
                                } else {
                                        $this->mainLoginForm( $this->msg( 'nosuchusershort',
                                                wfEscapeWikiText( $this->mUsername ) )->text() );
@@ -789,7 +803,7 @@ class LoginForm extends SpecialPage {
        function resetLoginForm( $error ) {
                $this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $error ) );
                $reset = new SpecialChangePassword();
-               $reset->setContext( $this );
+               $reset->setContext( $this->getContext() );
                $reset->execute( null );
        }
 
@@ -872,7 +886,11 @@ class LoginForm extends SpecialPage {
 
                wfRunHooks( 'UserLoginComplete', array( &$currentUser, &$injected_html ) );
 
-               //let any extensions change what message is shown
+               /**
+                * Let any extensions change what message is shown.
+                * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
+                * @since 1.18
+                */
                wfRunHooks( 'BeforeWelcomeCreation', array( &$welcome_creation_msg, &$injected_html ) );
 
                $this->displaySuccessfulLogin( $welcome_creation_msg, $injected_html );
@@ -880,10 +898,12 @@ class LoginForm extends SpecialPage {
 
        /**
         * Display a "login successful" page.
+        * @param $msgname string
+        * @param $injected_html string
         */
        private function displaySuccessfulLogin( $msgname, $injected_html ) {
                $out = $this->getOutput();
-               $out->setPageTitleMsg( 'loginsuccesstitle' );
+               $out->setPageTitle( $this->msg( 'loginsuccesstitle' ) );
                if( $msgname ){
                        $out->addWikiMsg( $msgname, wfEscapeWikiText( $this->getUser()->getName() ) );
                }
@@ -914,7 +934,7 @@ class LoginForm extends SpecialPage {
                # out.
 
                $out = $this->getOutput();
-               $out->setPageTitleMsg( 'cantcreateaccounttitle' );
+               $out->setPageTitle( $this->msg( 'cantcreateaccounttitle' ) );
 
                $block_reason = $block->mReason;
                if ( strval( $block_reason ) === '' ) {
@@ -925,7 +945,7 @@ class LoginForm extends SpecialPage {
                        'cantcreateaccount-text',
                        $block->getTarget(),
                        $block_reason,
-                       $block->getBlocker()->getName()
+                       $block->getByName()
                );
 
                $out->returnToMain( false );
@@ -947,14 +967,14 @@ class LoginForm extends SpecialPage {
                        // Block signup here if in readonly. Keeps user from
                        // going through the process (filling out data, etc)
                        // and being informed later.
-                       if ( wfReadOnly() ) {
-                               throw new ReadOnlyError;
+                       $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $user, true );
+                       if ( count( $permErrors ) ) {
+                               throw new PermissionsError( 'createaccount', $permErrors );
                        } elseif ( $user->isBlockedFromCreateAccount() ) {
                                $this->userBlockedMessage( $user->isBlockedFromCreateAccount() );
                                return;
-                       } elseif ( count( $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $user, true ) )>0 ) {
-                               $this->getOutput()->showPermissionsErrorPage( $permErrors, 'createaccount' );
-                               return;
+                       } elseif ( wfReadOnly() ) {
+                               throw new ReadOnlyError;
                        }
                }
 
@@ -1046,10 +1066,11 @@ class LoginForm extends SpecialPage {
                # Prepare language selection links as needed
                if( $wgLoginLanguageSelector ) {
                        $template->set( 'languages', $this->makeLanguageSelector() );
-                       if( $this->mLanguage )
+                       if( $this->mLanguage ) {
                                $template->set( 'uselang', $this->mLanguage );
+                       }
                }
-               
+
                // Use loginend-https for HTTPS requests if it's not blank, loginend otherwise
                // Ditto for signupend
                $usingHTTPS = WebRequest::detectProtocol() == 'https';
@@ -1074,14 +1095,7 @@ class LoginForm extends SpecialPage {
                        wfRunHooks( 'UserLoginForm', array( &$template ) );
                }
 
-               // Changes the title depending on permissions for creating account
                $out = $this->getOutput();
-               if ( $user->isAllowed( 'createaccount' ) ) {
-                       $out->setPageTitleMsg( 'userlogin' );
-               } else {
-                       $out->setPageTitleMsg( 'userloginnocreate' );
-               }
-
                $out->disallowUserJs(); // just in case...
                $out->addTemplate( $template );
        }
@@ -1111,6 +1125,7 @@ class LoginForm extends SpecialPage {
         * previous pass through the system.
         *
         * @private
+        * @return bool
         */
        function hasSessionCookie() {
                global $wgDisableCookieCheck;
@@ -1119,6 +1134,7 @@ class LoginForm extends SpecialPage {
 
        /**
         * Get the login token from the current session
+        * @return Mixed
         */
        public static function getLoginToken() {
                global $wgRequest;
@@ -1130,9 +1146,9 @@ class LoginForm extends SpecialPage {
         */
        public static function setLoginToken() {
                global $wgRequest;
-               // Use User::generateToken() instead of $user->editToken()
+               // Generate a token directly instead of using $user->editToken()
                // because the latter reuses $_SESSION['wsEditToken']
-               $wgRequest->setSessionData( 'wsLoginToken', User::generateToken() );
+               $wgRequest->setSessionData( 'wsLoginToken', MWCryptRand::generateHex( 32 ) );
        }
 
        /**
@@ -1145,6 +1161,7 @@ class LoginForm extends SpecialPage {
 
        /**
         * Get the createaccount token from the current session
+        * @return Mixed
         */
        public static function getCreateaccountToken() {
                global $wgRequest;
@@ -1156,7 +1173,7 @@ class LoginForm extends SpecialPage {
         */
        public static function setCreateaccountToken() {
                global $wgRequest;
-               $wgRequest->setSessionData( 'wsCreateaccountToken', User::generateToken() );
+               $wgRequest->setSessionData( 'wsCreateaccountToken', MWCryptRand::generateHex( 32 ) );
        }
 
        /**
@@ -1178,7 +1195,7 @@ class LoginForm extends SpecialPage {
                }
                $check = $titleObj->getFullURL( $query );
 
-               return $this->getOutput()->redirect( $check );
+               $this->getOutput()->redirect( $check );
        }
 
        /**
@@ -1187,15 +1204,15 @@ class LoginForm extends SpecialPage {
        function onCookieRedirectCheck( $type ) {
                if ( !$this->hasSessionCookie() ) {
                        if ( $type == 'new' ) {
-                               return $this->mainLoginForm( $this->msg( 'nocookiesnew' )->parse() );
+                               $this->mainLoginForm( $this->msg( 'nocookiesnew' )->parse() );
                        } elseif ( $type == 'login' ) {
-                               return $this->mainLoginForm( $this->msg( 'nocookieslogin' )->parse() );
+                               $this->mainLoginForm( $this->msg( 'nocookieslogin' )->parse() );
                        } else {
                                # shouldn't happen
-                               return $this->mainLoginForm( $this->msg( 'error' )->text() );
+                               $this->mainLoginForm( $this->msg( 'error' )->text() );
                        }
                } else {
-                       return $this->successfulLogin();
+                       $this->successfulLogin();
                }
        }
 
@@ -1225,7 +1242,7 @@ class LoginForm extends SpecialPage {
                                }
                        }
                        return count( $links ) > 0 ? $this->msg( 'loginlanguagelabel' )->rawParams(
-                               $this->getLang()->pipeList( $links ) )->escaped() : '';
+                               $this->getLanguage()->pipeList( $links ) )->escaped() : '';
                } else {
                        return '';
                }
@@ -1237,8 +1254,13 @@ class LoginForm extends SpecialPage {
         *
         * @param $text Link text
         * @param $lang Language code
+        * @return string
         */
        function makeLanguageSelectorLink( $text, $lang ) {
+               if( $this->getLanguage()->getCode() == $lang ) {
+                       // no link for currently used language
+                       return htmlspecialchars( $text );
+               }
                $attr = array( 'uselang' => $lang );
                if( $this->mType == 'signup' ) {
                        $attr['type'] = 'signup';