Merge "Added a function to LoginForm to show the "return to" page."
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 26 Apr 2013 14:41:32 +0000 (14:41 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 26 Apr 2013 14:41:32 +0000 (14:41 +0000)
1  2 
includes/specials/SpecialUserlogin.php

@@@ -115,18 -115,18 +115,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 );
                # 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 = '';
                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 ) {
                }
  
                $status = $this->addNewaccountInternal();
 -              if( !$status->isGood() ) {
 +              if ( !$status->isGood() ) {
                        $error = $this->getOutput()->parse( $status->getWikiText() );
                        $this->mainLoginForm( $error );
                        return;
                $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() );
  
                # 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;
                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 {
  
                $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' ) );
                # 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
                        $this->getContext()->setUser( $u );
                        wfRunHooks( 'AddNewAccount', array( $u, false ) );
                        $u->addNewUserLogEntry( 'create' );
 -                      if( $this->hasSessionCookie() ) {
 +                      if ( $this->hasSessionCookie() ) {
                                $this->successfulCreation();
                        } else {
                                $this->cookieRedirectCheck( 'new' );
                        $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' );
                }
  
                // 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 ) ||
                $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 ) );
                        }
                }
  
 -              if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
 +              if ( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
                        return Status::newFatal( 'externaldberror' );
                }
  
                }
  
                $u = User::newFromName( $this->mUsername );
 -              if( !( $u instanceof User ) || !User::isUsableName( $u->getName() ) ) {
 +              if ( !( $u instanceof User ) || !User::isUsableName( $u->getName() ) ) {
                        return self::ILLEGAL;
                }
  
  
                // 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-
                                // 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();
                                }
                }
  
                $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;
                        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();
                                $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.
                                         */
                                $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 {
                        return Status::newFatal( 'noemail', $u->getName() );
                }
                $ip = $this->getRequest()->getIP();
 -              if( !$ip ) {
 +              if ( !$ip ) {
                        return Status::newFatal( 'badipaddress' );
                }
  
                $injected_html = '';
                wfRunHooks( 'UserLoginComplete', array( &$currentUser, &$injected_html ) );
  
 -              if( $injected_html !== '' ) {
 +              if ( $injected_html !== '' ) {
                        $this->displaySuccessfulAction( $this->msg( 'loginsuccesstitle' ),
                                'loginsuccess', $injected_html );
                } else {
                );
        }
  
+       /**
+        * 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.
         *
                if ( $wgSecureLogin && !$this->mStickHTTPS ) {
                        $options = array( 'http' );
                        $proto = PROTO_HTTP;
 -              } elseif( $wgSecureLogin ) {
 +              } elseif ( $wgSecureLogin ) {
                        $options = array( 'https' );
                        $proto = PROTO_HTTPS;
                } else {
         * @return Boolean
         */
        private function shouldShowVForm() {
 -              global $wgUseVFormUserLogin;
 +              global $wgUseVFormCreateAccount, $wgUseVFormUserLogin;
  
                if ( $this->mType == 'signup' ) {
 -                      return false;
 +                      return $this->mRequest->getBool( 'useNew', $wgUseVFormCreateAccount );
                } else {
                        return $this->mRequest->getBool( 'useNew', $wgUseVFormUserLogin );
                }
                }
  
                if ( $this->mType == 'signup' ) {
 -                      $template = new UsercreateTemplate();
 +                      $out->addModules( 'mediawiki.special.userlogin.signup' );
 +                      if ( $this->mShowVForm ) {
 +                              $template = new UsercreateTemplateVForm();
 +                              $out->addModuleStyles( array(
 +                                      'mediawiki.ui',
 +                                      'mediawiki.special.createaccount.vform'
 +                              ) );
 +                              // 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( 'mediawiki.special.createaccount.vform.js' );
 +                      } else {
 +                              $template = new UsercreateTemplate();
 +                      }
                        $q = 'action=submitlogin&type=signup';
                        $linkq = 'type=login';
                        $linkmsg = 'gotaccount';
 -                      $out->addModules( 'mediawiki.special.userlogin.signup' );
                } else {
                        if ( $this->mShowVForm ) {
                                $template = new UserloginTemplateVForm();
                }
  
                # 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 ) {
                        : 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 );
                }
  
                # 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 );
                        }
                }
         * @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;
         */
        private function renewSessionId() {
                global $wgSecureLogin, $wgCookieSecure;
 -              if( $wgSecureLogin && !$this->mStickHTTPS ) {
 +              if ( $wgSecureLogin && !$this->mStickHTTPS ) {
                        $wgCookieSecure = false;
                }
  
         */
        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 ) {
         * @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;
                }