Create PostLoginRedirect Hook for changing the redirect behavior
[lhc/web/wiklou.git] / includes / specials / SpecialUserlogin.php
index b049975..abb32ce 100644 (file)
@@ -166,7 +166,7 @@ class LoginForm extends SpecialPage {
        }
 
        /*
-        * @param $subPage string|null
+        * @param string|null $subPage
         */
        public function execute( $subPage ) {
                if ( session_id() == '' ) {
@@ -193,7 +193,10 @@ class LoginForm extends SpecialPage {
                                'title' => null,
                        ) + $this->mRequest->getQueryValues();
                        $url = $title->getFullURL( $query, false, PROTO_HTTPS );
-                       if ( $wgSecureLogin && wfCanIPUseHTTPS( $this->getRequest()->getIP() ) ) {
+                       if ( $wgSecureLogin
+                               && wfCanIPUseHTTPS( $this->getRequest()->getIP() )
+                               && !$this->mFromHTTP ) // Avoid infinite redirect
+                       {
                                $url = wfAppendQuery( $url, 'fromhttp=1' );
                                $this->getOutput()->redirect( $url );
                                // Since we only do this redir to change proto, always vary
@@ -435,7 +438,11 @@ class LoginForm extends SpecialPage {
 
                // Normalize the name so that silly things don't cause "invalid username"
                // errors. User::newFromName does some rather strict checking, rejecting
-               // e.g. leading/trailing/multiple spaces.
+               // e.g. leading/trailing/multiple spaces. But first we need to reject
+               // usernames that would be treated as titles with a fragment part.
+               if ( strpos( $this->mUsername, '#' ) !== false ) {
+                       return Status::newFatal( 'noname' );
+               }
                $title = Title::makeTitleSafe( NS_USER, $this->mUsername );
                if ( !is_object( $title ) ) {
                        return Status::newFatal( 'noname' );
@@ -536,9 +543,9 @@ class LoginForm extends SpecialPage {
         * Actually add a user to the database.
         * Give it a User object that has been initialised with a name.
         *
-        * @param $u User object.
-        * @param $autocreate boolean -- true if this is an autocreation via auth plugin
-        * @return Status object, with the User object in the value member on success
+        * @param User $u
+        * @param bool $autocreate True if this is an autocreation via auth plugin
+        * @return Status Status object, with the User object in the value member on success
         * @private
         */
        function initUser( $u, $autocreate ) {
@@ -559,7 +566,6 @@ class LoginForm extends SpecialPage {
 
                $wgAuth->initUser( $u, $autocreate );
 
-               $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
                $u->saveSettings();
 
                // Update user count
@@ -721,7 +727,7 @@ class LoginForm extends SpecialPage {
         * Increment the login attempt throttle hit count for the (username,current IP)
         * tuple unless the throttle was already reached.
         * @param string $username The user name
-        * @return Bool|Integer The integer hit count or True if it is already at the limit
+        * @return bool|int The integer hit count or True if it is already at the limit
         */
        public static function incLoginThrottle( $username ) {
                global $wgPasswordAttemptThrottle, $wgMemc, $wgRequest;
@@ -763,9 +769,9 @@ class LoginForm extends SpecialPage {
         * Attempt to automatically create a user on login. Only succeeds if there
         * is an external authentication method which allows it.
         *
-        * @param $user User
+        * @param User $user
         *
-        * @return integer Status code
+        * @return int Status code
         */
        function attemptAutoCreate( $user ) {
                global $wgAuth;
@@ -822,21 +828,16 @@ class LoginForm extends SpecialPage {
                        case self::SUCCESS:
                                # We've verified now, update the real record
                                $user = $this->getUser();
-                               if ( (bool)$this->mRemember != $user->getBoolOption( 'rememberpassword' ) ) {
-                                       $user->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
-                                       $user->saveSettings();
-                               } else {
-                                       $user->invalidateCache();
-                               }
+                               $user->invalidateCache();
 
                                if ( $user->requiresHTTPS() ) {
                                        $this->mStickHTTPS = true;
                                }
 
                                if ( $wgSecureLogin && !$this->mStickHTTPS ) {
-                                       $user->setCookies( null, false );
+                                       $user->setCookies( $this->mRequest, false, $this->mRemember );
                                } else {
-                                       $user->setCookies();
+                                       $user->setCookies( $this->mRequest, null, $this->mRemember );
                                }
                                self::clearLoginToken();
 
@@ -954,11 +955,11 @@ class LoginForm extends SpecialPage {
        }
 
        /**
-        * @param $u User object
-        * @param $throttle Boolean
-        * @param string $emailTitle message name of email title
-        * @param string $emailText message name of email text
-        * @return Status object
+        * @param User $u
+        * @param bool $throttle
+        * @param string $emailTitle Message name of email title
+        * @param string $emailText Message name of email text
+        * @return Status
         */
        function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle',
                $emailText = 'passwordremindertext'
@@ -1008,7 +1009,7 @@ class LoginForm extends SpecialPage {
                wfRunHooks( 'UserLoginComplete', array( &$currentUser, &$injected_html ) );
 
                if ( $injected_html !== '' ) {
-                       $this->displaySuccessfulAction( $this->msg( 'loginsuccesstitle' ),
+                       $this->displaySuccessfulAction( 'success', $this->msg( 'loginsuccesstitle' ),
                                'loginsuccess', $injected_html );
                } else {
                        $this->executeReturnTo( 'successredirect' );
@@ -1036,18 +1037,22 @@ class LoginForm extends SpecialPage {
                 */
                wfRunHooks( 'BeforeWelcomeCreation', array( &$welcome_creation_msg, &$injected_html ) );
 
-               $this->displaySuccessfulAction( $this->msg( 'welcomeuser', $this->getUser()->getName() ),
-                       $welcome_creation_msg, $injected_html );
+               $this->displaySuccessfulAction(
+                       'signup',
+                       $this->msg( 'welcomeuser', $this->getUser()->getName() ),
+                       $welcome_creation_msg, $injected_html
+               );
        }
 
        /**
-        * Display an "successful action" page.
+        * Display a "successful action" page.
         *
-        * @param string|Message $title page's title
-        * @param $msgname string
-        * @param $injected_html string
+        * @param string $type condition of return to; see `executeReturnTo`
+        * @param string|Message $title Page's title
+        * @param string $msgname
+        * @param string $injected_html
         */
-       private function displaySuccessfulAction( $title, $msgname, $injected_html ) {
+       private function displaySuccessfulAction( $type, $title, $msgname, $injected_html ) {
                $out = $this->getOutput();
                $out->setPageTitle( $title );
                if ( $msgname ) {
@@ -1056,7 +1061,7 @@ class LoginForm extends SpecialPage {
 
                $out->addHTML( $injected_html );
 
-               $this->executeReturnTo( 'success' );
+               $this->executeReturnTo( $type );
        }
 
        /**
@@ -1064,7 +1069,7 @@ class LoginForm extends SpecialPage {
         * there is a block on them or their IP which prevents account creation.  Note that
         * User::isBlockedFromCreateAccount(), which gets this block, ignores the 'hardblock'
         * setting on blocks (bug 13611).
-        * @param $block Block the block causing this error
+        * @param Block $block The block causing this error
         * @throws ErrorPageError
         */
        function userBlockedMessage( Block $block ) {
@@ -1100,8 +1105,9 @@ class LoginForm extends SpecialPage {
         * Extensions can use this to reuse the "return to" logic after
         * inject steps (such as redirection) into the login process.
         *
-        * @param $type string, one of the following:
+        * @param string $type One of the following:
         *    - error: display a return to link ignoring $wgRedirectOnLogin
+        *    - signup: display a return to link using $wgRedirectOnLogin if needed
         *    - success: display a return to link using $wgRedirectOnLogin if needed
         *    - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
         * @param string $returnTo
@@ -1121,8 +1127,9 @@ class LoginForm extends SpecialPage {
        /**
         * Add a "return to" link or redirect to it.
         *
-        * @param $type string, one of the following:
+        * @param string $type One of the following:
         *    - error: display a return to link ignoring $wgRedirectOnLogin
+        *    - signup: display a return to link using $wgRedirectOnLogin if needed
         *    - success: display a return to link using $wgRedirectOnLogin if needed
         *    - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
         */
@@ -1137,6 +1144,9 @@ class LoginForm extends SpecialPage {
                        $returnToQuery = wfCgiToArray( $this->mReturnToQuery );
                }
 
+               // Allow modification of redirect behavior
+               wfRunHooks( 'PostLoginRedirect', array( &$returnTo, &$returnToQuery, &$type ) );
+
                $returnToTitle = Title::newFromText( $returnTo );
                if ( !$returnToTitle ) {
                        $returnToTitle = Title::newMainPage();
@@ -1291,7 +1301,7 @@ class LoginForm extends SpecialPage {
                $template->set( 'resetlink', $resetLink );
                $template->set( 'canremember', ( $wgCookieExpiration > 0 ) );
                $template->set( 'usereason', $user->isLoggedIn() );
-               $template->set( 'remember', $user->getOption( 'rememberpassword' ) || $this->mRemember );
+               $template->set( 'remember', $this->mRemember );
                $template->set( 'cansecurelogin', ( $wgSecureLogin === true ) );
                $template->set( 'stickhttps', (int)$this->mStickHTTPS );
                $template->set( 'loggedin', $user->isLoggedIn() );
@@ -1350,7 +1360,7 @@ class LoginForm extends SpecialPage {
         * Whether the login/create account form should display a link to the
         * other form (in addition to whatever the skin provides).
         *
-        * @param $user User
+        * @param User $user
         * @return bool
         */
        private function showCreateOrLoginLink( &$user ) {
@@ -1381,7 +1391,7 @@ class LoginForm extends SpecialPage {
 
        /**
         * Get the login token from the current session
-        * @return Mixed
+        * @return mixed
         */
        public static function getLoginToken() {
                global $wgRequest;
@@ -1409,7 +1419,7 @@ class LoginForm extends SpecialPage {
 
        /**
         * Get the createaccount token from the current session
-        * @return Mixed
+        * @return mixed
         */
        public static function getCreateaccountToken() {
                global $wgRequest;