Merge "Set initial focus on some special pages"
[lhc/web/wiklou.git] / includes / specials / SpecialUserlogin.php
index e0ddc96..71efefa 100644 (file)
@@ -51,9 +51,6 @@ class LoginForm extends SpecialPage {
        var $mAbortLoginErrorMsg = 'login-abort-generic';
        private $mLoaded = false;
        private $mSecureLoginUrl;
-       // TODO Remove old forms and mShowVForm gating after all WMF wikis have
-       // adapted messages and help links to new versions.
-       private $mShowVForm;
 
        /**
         * @ var WebRequest
@@ -115,18 +112,18 @@ class LoginForm extends SpecialPage {
                $this->mReturnTo = $request->getVal( 'returnto', '' );
                $this->mReturnToQuery = $request->getVal( 'returntoquery', '' );
 
-               if( $wgEnableEmail ) {
+               if ( $wgEnableEmail ) {
                        $this->mEmail = $request->getText( 'wpEmail' );
                } else {
                        $this->mEmail = '';
                }
-               if( !in_array( 'realname', $wgHiddenPrefs ) ) {
+               if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
                        $this->mRealName = $request->getText( 'wpRealName' );
                } else {
                        $this->mRealName = '';
                }
 
-               if( !$wgAuth->validDomain( $this->mDomain ) ) {
+               if ( !$wgAuth->validDomain( $this->mDomain ) ) {
                        $this->mDomain = $wgAuth->getDomain();
                }
                $wgAuth->setDomain( $this->mDomain );
@@ -135,7 +132,7 @@ class LoginForm extends SpecialPage {
                # 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 ) && (
+               if ( is_object( $returnToTitle ) && (
                        $returnToTitle->isSpecial( 'Userlogout' )
                        || $returnToTitle->isSpecial( 'PasswordReset' ) ) ) {
                        $this->mReturnTo = '';
@@ -144,17 +141,10 @@ class LoginForm extends SpecialPage {
        }
 
        function getDescription() {
-               if ( !$this->getUser()->isAllowed( 'createaccount' ) ) {
-                       return $this->msg( 'userloginnocreate' )->text();
-               }
-               if ( $this->mShowVForm ) {
-                       if ( $this->mType === 'signup' ) {
-                               return $this->msg( 'createaccount' )->text();
-                       } else {
-                               return $this->msg( 'login' )->text();
-                       }
+               if ( $this->mType === 'signup' ) {
+                       return $this->msg( 'createaccount' )->text();
                } else {
-                       return $this->msg( 'userlogin' )->text();
+                       return $this->msg( 'login' )->text();
                }
        }
 
@@ -173,8 +163,6 @@ class LoginForm extends SpecialPage {
                if ( $subPage == 'signup' ) {
                        $this->mType = 'signup';
                }
-               $this->mShowVForm = $this->shouldShowVForm();
-
                $this->setHeaders();
 
                // If logging in and not on HTTPS, either redirect to it or offer a link.
@@ -206,8 +194,8 @@ class LoginForm extends SpecialPage {
                if ( !is_null( $this->mCookieCheck ) ) {
                        $this->onCookieRedirectCheck( $this->mCookieCheck );
                        return;
-               } elseif( $this->mPosted ) {
-                       if( $this->mCreateaccount ) {
+               } elseif ( $this->mPosted ) {
+                       if ( $this->mCreateaccount ) {
                                $this->addNewAccount();
                                return;
                        } elseif ( $this->mCreateaccountMail ) {
@@ -231,7 +219,7 @@ class LoginForm extends SpecialPage {
                }
 
                $status = $this->addNewaccountInternal();
-               if( !$status->isGood() ) {
+               if ( !$status->isGood() ) {
                        $error = $this->getOutput()->parse( $status->getWikiText() );
                        $this->mainLoginForm( $error );
                        return;
@@ -250,7 +238,7 @@ class LoginForm extends SpecialPage {
                $out = $this->getOutput();
                $out->setPageTitle( $this->msg( 'accmailtitle' ) );
 
-               if( !$result->isGood() ) {
+               if ( !$result->isGood() ) {
                        $this->mainLoginForm( $this->msg( 'mailerror', $result->getWikiText() )->text() );
                } else {
                        $out->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
@@ -267,7 +255,7 @@ class LoginForm extends SpecialPage {
 
                # Create the account and abort if there's a problem doing so
                $status = $this->addNewAccountInternal();
-               if( !$status->isGood() ) {
+               if ( !$status->isGood() ) {
                        $error = $this->getOutput()->parse( $status->getWikiText() );
                        $this->mainLoginForm( $error );
                        return false;
@@ -279,7 +267,7 @@ class LoginForm extends SpecialPage {
                if ( $this->getUser()->isAnon() ) {
                        # If we showed up language selection links, and one was in use, be
                        # smart (and sensible) and save that language as the user's preference
-                       if( $wgLoginLanguageSelector && $this->mLanguage ) {
+                       if ( $wgLoginLanguageSelector && $this->mLanguage ) {
                                $u->setOption( 'language', $this->mLanguage );
                        } else {
 
@@ -296,9 +284,9 @@ class LoginForm extends SpecialPage {
                $out = $this->getOutput();
 
                # Send out an email authentication message if needed
-               if( $wgEmailAuthentication && Sanitizer::validateEmail( $u->getEmail() ) ) {
+               if ( $wgEmailAuthentication && Sanitizer::validateEmail( $u->getEmail() ) ) {
                        $status = $u->sendConfirmationMail();
-                       if( $status->isGood() ) {
+                       if ( $status->isGood() ) {
                                $out->addWikiMsg( 'confirmemail_oncreate' );
                        } else {
                                $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
@@ -311,7 +299,7 @@ class LoginForm extends SpecialPage {
                # If not logged in, assume the new account as the current one and set
                # session cookies then show a "welcome" message or a "need cookies"
                # message as needed
-               if( $this->getUser()->isAnon() ) {
+               if ( $this->getUser()->isAnon() ) {
                        $u->setCookies();
                        $wgUser = $u;
                        // This should set it for OutputPage and the Skin
@@ -320,7 +308,7 @@ class LoginForm extends SpecialPage {
                        $this->getContext()->setUser( $u );
                        wfRunHooks( 'AddNewAccount', array( $u, false ) );
                        $u->addNewUserLogEntry( 'create' );
-                       if( $this->hasSessionCookie() ) {
+                       if ( $this->hasSessionCookie() ) {
                                $this->successfulCreation();
                        } else {
                                $this->cookieRedirectCheck( 'new' );
@@ -347,7 +335,7 @@ class LoginForm extends SpecialPage {
                        $wgMinimalPasswordLength, $wgEmailConfirmToEdit;
 
                // If the user passes an invalid domain, something is fishy
-               if( !$wgAuth->validDomain( $this->mDomain ) ) {
+               if ( !$wgAuth->validDomain( $this->mDomain ) ) {
                        return Status::newFatal( 'wrongpassword' );
                }
 
@@ -356,8 +344,8 @@ class LoginForm extends SpecialPage {
                // cation server before they create an account (otherwise, they can
                // create a local account and login as any domain user). We only need
                // to check this for domains that aren't local.
-               if( 'local' != $this->mDomain && $this->mDomain != '' ) {
-                       if(
+               if ( 'local' != $this->mDomain && $this->mDomain != '' ) {
+                       if (
                                !$wgAuth->canCreateAccounts() &&
                                (
                                        !$wgAuth->userExists( $this->mUsername ) ||
@@ -455,7 +443,7 @@ class LoginForm extends SpecialPage {
                $u->setRealName( $this->mRealName );
 
                $abortError = '';
-               if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
+               if ( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
                        // Hook point to add extra creation throttles and blocks
                        wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
                        return Status::newFatal( new RawMessage( $abortError ) );
@@ -478,7 +466,7 @@ class LoginForm extends SpecialPage {
                        }
                }
 
-               if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
+               if ( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
                        return Status::newFatal( 'externaldberror' );
                }
 
@@ -576,7 +564,7 @@ class LoginForm extends SpecialPage {
                }
 
                $u = User::newFromName( $this->mUsername );
-               if( !( $u instanceof User ) || !User::isUsableName( $u->getName() ) ) {
+               if ( !( $u instanceof User ) || !User::isUsableName( $u->getName() ) ) {
                        return self::ILLEGAL;
                }
 
@@ -594,13 +582,13 @@ class LoginForm extends SpecialPage {
 
                // Give general extensions, such as a captcha, a chance to abort logins
                $abort = self::ABORTED;
-               if( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort, &$this->mAbortLoginErrorMsg ) ) ) {
+               if ( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort, &$this->mAbortLoginErrorMsg ) ) ) {
                        return $abort;
                }
 
                global $wgBlockDisablesLogin;
                if ( !$u->checkPassword( $this->mPassword ) ) {
-                       if( $u->checkTemporaryPassword( $this->mPassword ) ) {
+                       if ( $u->checkTemporaryPassword( $this->mPassword ) ) {
                                // The e-mailed temporary password should not be used for actu-
                                // al logins; that's a very sloppy habit, and insecure if an
                                // attacker has a few seconds to click "search" on someone's o-
@@ -617,7 +605,7 @@ class LoginForm extends SpecialPage {
                                // As a side-effect, we can authenticate the user's e-mail ad-
                                // dress if it's not already done, since the temporary password
                                // was sent via e-mail.
-                               if( !$u->isEmailConfirmed() ) {
+                               if ( !$u->isEmailConfirmed() ) {
                                        $u->confirmEmail();
                                        $u->saveSettings();
                                }
@@ -726,7 +714,7 @@ class LoginForm extends SpecialPage {
                }
 
                $abortError = '';
-               if( !wfRunHooks( 'AbortAutoAccount', array( $user, &$abortError ) ) ) {
+               if ( !wfRunHooks( 'AbortAutoAccount', array( $user, &$abortError ) ) ) {
                        // Hook point to add extra creation throttles and blocks
                        wfDebug( "LoginForm::attemptAutoCreate: a hook blocked creation: $abortError\n" );
                        $this->mAbortLoginErrorMsg = $abortError;
@@ -752,14 +740,14 @@ 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' ) ) {
+                               if ( (bool)$this->mRemember != $user->getBoolOption( 'rememberpassword' ) ) {
                                        $user->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
                                        $user->saveSettings();
                                } else {
                                        $user->invalidateCache();
                                }
 
-                               if( $wgSecureLogin && !$this->mStickHTTPS ) {
+                               if ( $wgSecureLogin && !$this->mStickHTTPS ) {
                                        $user->setCookies( null, false );
                                } else {
                                        $user->setCookies();
@@ -771,7 +759,7 @@ class LoginForm extends SpecialPage {
                                $key = wfMemcKey( 'password-throttle', $request->getIP(), md5( $this->mUsername ) );
                                $wgMemc->delete( $key );
 
-                               if( $this->hasSessionCookie() || $this->mSkipCookieCheck ) {
+                               if ( $this->hasSessionCookie() || $this->mSkipCookieCheck ) {
                                        /* Replace the language object to provide user interface in
                                         * correct language immediately on this first page load.
                                         */
@@ -801,7 +789,7 @@ class LoginForm extends SpecialPage {
                                $this->mainLoginForm( $this->msg( 'wrongpassword' )->text() );
                                break;
                        case self::NOT_EXISTS:
-                               if( $this->getUser()->isAllowed( 'createaccount' ) ) {
+                               if ( $this->getUser()->isAllowed( 'createaccount' ) ) {
                                        $this->mainLoginForm( $this->msg( 'nosuchuser',
                                                wfEscapeWikiText( $this->mUsername ) )->parse() );
                                } else {
@@ -860,7 +848,7 @@ class LoginForm extends SpecialPage {
                        return Status::newFatal( 'noemail', $u->getName() );
                }
                $ip = $this->getRequest()->getIP();
-               if( !$ip ) {
+               if ( !$ip ) {
                        return Status::newFatal( 'badipaddress' );
                }
 
@@ -894,7 +882,7 @@ class LoginForm extends SpecialPage {
                $injected_html = '';
                wfRunHooks( 'UserLoginComplete', array( &$currentUser, &$injected_html ) );
 
-               if( $injected_html !== '' ) {
+               if ( $injected_html !== '' ) {
                        $this->displaySuccessfulAction( $this->msg( 'loginsuccesstitle' ),
                                'loginsuccess', $injected_html );
                } else {
@@ -973,6 +961,29 @@ class LoginForm extends SpecialPage {
                );
        }
 
+       /**
+        * Add a "return to" link or redirect to it.
+        * 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:
+        *    - error: display a return to link ignoring $wgRedirectOnLogin
+        *    - success: display a return to link using $wgRedirectOnLogin if needed
+        *    - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
+        * @param string $returnTo
+        * @param array|string $returnToQuery
+        * @param bool $stickHTTPs Keep redirect link on HTTPs
+        * @since 1.22
+        */
+       public function showReturnToPage(
+               $type, $returnTo = '', $returnToQuery = '', $stickHTTPs = false
+       ) {
+               $this->mReturnTo = $returnTo;
+               $this->mReturnToQuery = $returnToQuery;
+               $this->mStickHTTPS = $stickHTTPs;
+               $this->executeReturnTo( $type );
+       }
+
        /**
         * Add a "return to" link or redirect to it.
         *
@@ -1000,7 +1011,7 @@ class LoginForm extends SpecialPage {
                if ( $wgSecureLogin && !$this->mStickHTTPS ) {
                        $options = array( 'http' );
                        $proto = PROTO_HTTP;
-               } elseif( $wgSecureLogin ) {
+               } elseif ( $wgSecureLogin ) {
                        $options = array( 'https' );
                        $proto = PROTO_HTTPS;
                } else {
@@ -1016,22 +1027,6 @@ class LoginForm extends SpecialPage {
                }
        }
 
-       /**
-        * Whether to show new vertically laid out login form.
-        * ?useNew=1 forces new style, ?useNew=0 forces old style,
-        * otherwise consult $wgUseVFormUserLogin.
-        * @return Boolean
-        */
-       private function shouldShowVForm() {
-               global $wgUseVFormUserLogin;
-
-               if ( $this->mType == 'signup' ) {
-                       return false;
-               } else {
-                       return $this->mRequest->getBool( 'useNew', $wgUseVFormUserLogin );
-               }
-       }
-
        /**
         * @private
         */
@@ -1071,23 +1066,33 @@ class LoginForm extends SpecialPage {
 
                if ( $this->mType == 'signup' ) {
                        $template = new UsercreateTemplate();
+
+                       $out->addModuleStyles( array(
+                               'mediawiki.ui',
+                               'mediawiki.special.createaccount'
+                       ) );
+                       // XXX hack pending RL or JS parse() support for complex content messages
+                       // https://bugzilla.wikimedia.org/show_bug.cgi?id=25349
+                       $out->addJsConfigVars( 'wgCreateacctImgcaptchaHelp',
+                               $this->msg( 'createacct-imgcaptcha-help' )->parse() );
+                       $out->addModules( array(
+                               'mediawiki.special.createaccount.js'
+                       ) );
+                       // Must match number of benefits defined in messages
+                       $template->set( 'benefitCount', 3 );
+
                        $q = 'action=submitlogin&type=signup';
                        $linkq = 'type=login';
-                       $linkmsg = 'gotaccount';
-                       $out->addModules( 'mediawiki.special.userlogin.signup' );
                } else {
-                       if ( $this->mShowVForm ) {
-                               $template = new UserloginTemplateVForm();
-                               $out->addModuleStyles( array(
-                                       'mediawiki.ui',
-                                       'mediawiki.special.userlogin.vform'
-                               ) );
-                       } else {
-                               $template = new UserloginTemplate();
-                       }
+                       $template = new UserloginTemplate();
+
+                       $out->addModuleStyles( array(
+                               'mediawiki.ui',
+                               'mediawiki.special.userlogin'
+                       ) );
+
                        $q = 'action=submitlogin&type=login';
                        $linkq = 'type=signup';
-                       $linkmsg = 'nologin';
                }
 
                if ( $this->mReturnTo !== '' ) {
@@ -1101,22 +1106,13 @@ class LoginForm extends SpecialPage {
                }
 
                # Don't show a "create account" link if the user can't.
-               if( $this->showCreateOrLoginLink( $user ) ) {
+               if ( $this->showCreateOrLoginLink( $user ) ) {
                        # Pass any language selection on to the mode switch link
-                       if( $wgLoginLanguageSelector && $this->mLanguage ) {
+                       if ( $wgLoginLanguageSelector && $this->mLanguage ) {
                                $linkq .= '&uselang=' . $this->mLanguage;
                        }
-                       if ( !$this->mShowVForm ) {
-                               $link = Html::element( 'a', array( 'href' => $titleObj->getLocalURL( $linkq ) ),
-                                       $this->msg( $linkmsg . 'link' )->text() ); # Calling either 'gotaccountlink' or 'nologinlink'
-
-                                       $template->set( 'link', $this->msg( $linkmsg )->rawParams( $link )->parse() );
-
-                       } else {
-                               // Supply URL, login template creates the button.
-                               // (The template 'link' key, passed above, is obsolete in the VForm design.)
-                               $template->set( 'createOrLoginHref', $titleObj->getLocalURL( $linkq ) );
-                       }
+                       // Supply URL, login template creates the button.
+                       $template->set( 'createOrLoginHref', $titleObj->getLocalURL( $linkq ) );
                } else {
                        $template->set( 'link', '' );
                }
@@ -1131,6 +1127,7 @@ class LoginForm extends SpecialPage {
                        : is_array( $wgPasswordResetRoutes ) && in_array( true, array_values( $wgPasswordResetRoutes ) );
 
                $template->set( 'header', '' );
+               $template->set( 'skin', $this->getSkin() );
                $template->set( 'name', $this->mUsername );
                $template->set( 'password', $this->mPassword );
                $template->set( 'retype', $this->mRetype );
@@ -1169,9 +1166,9 @@ class LoginForm extends SpecialPage {
                }
 
                # Prepare language selection links as needed
-               if( $wgLoginLanguageSelector ) {
+               if ( $wgLoginLanguageSelector ) {
                        $template->set( 'languages', $this->makeLanguageSelector() );
-                       if( $this->mLanguage ) {
+                       if ( $this->mLanguage ) {
                                $template->set( 'uselang', $this->mLanguage );
                        }
                }
@@ -1213,9 +1210,9 @@ class LoginForm extends SpecialPage {
         * @return Boolean
         */
        function showCreateOrLoginLink( &$user ) {
-               if( $this->mType == 'signup' ) {
+               if ( $this->mType == 'signup' ) {
                        return true;
-               } elseif( $user->isAllowed( 'createaccount' ) ) {
+               } elseif ( $user->isAllowed( 'createaccount' ) ) {
                        return true;
                } else {
                        return false;
@@ -1294,7 +1291,7 @@ class LoginForm extends SpecialPage {
         */
        private function renewSessionId() {
                global $wgSecureLogin, $wgCookieSecure;
-               if( $wgSecureLogin && !$this->mStickHTTPS ) {
+               if ( $wgSecureLogin && !$this->mStickHTTPS ) {
                        $wgCookieSecure = false;
                }
 
@@ -1353,10 +1350,10 @@ class LoginForm extends SpecialPage {
         */
        function makeLanguageSelector() {
                $msg = $this->msg( 'loginlanguagelinks' )->inContentLanguage();
-               if( !$msg->isBlank() ) {
+               if ( !$msg->isBlank() ) {
                        $langs = explode( "\n", $msg->text() );
                        $links = array();
-                       foreach( $langs as $lang ) {
+                       foreach ( $langs as $lang ) {
                                $lang = trim( $lang, '* ' );
                                $parts = explode( '|', $lang );
                                if ( count( $parts ) >= 2 ) {
@@ -1379,15 +1376,15 @@ class LoginForm extends SpecialPage {
         * @return string
         */
        function makeLanguageSelectorLink( $text, $lang ) {
-               if( $this->getLanguage()->getCode() == $lang ) {
+               if ( $this->getLanguage()->getCode() == $lang ) {
                        // no link for currently used language
                        return htmlspecialchars( $text );
                }
                $query = array( 'uselang' => $lang );
-               if( $this->mType == 'signup' ) {
+               if ( $this->mType == 'signup' ) {
                        $query['type'] = 'signup';
                }
-               if( $this->mReturnTo !== '' ) {
+               if ( $this->mReturnTo !== '' ) {
                        $query['returnto'] = $this->mReturnTo;
                        $query['returntoquery'] = $this->mReturnToQuery;
                }