(bug 26809) Uploading files with multiple extensions where one of the extensions...
[lhc/web/wiklou.git] / includes / specials / SpecialUserlogin.php
index 6d074f3..69f55aa 100644 (file)
  * @ingroup SpecialPage
  */
 
-/**
- * Constructor
- */
-function wfSpecialUserlogin( $par = '' ) {
-       global $wgRequest;
-       if( session_id() == '' ) {
-               wfSetupSession();
-       }
-
-       $form = new LoginForm( $wgRequest, $par );
-       $form->execute();
-}
-
 /**
  * Implements Special:UserLogin
  *
  * @ingroup SpecialPage
  */
-class LoginForm {
+class LoginForm extends SpecialPage {
 
        const SUCCESS = 0;
        const NO_NAME = 1;
@@ -56,23 +43,34 @@ class LoginForm {
        const NEED_TOKEN = 12;
        const WRONG_TOKEN = 13;
 
-       var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
+       var $mUsername, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
        var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
        var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
-       var $mSkipCookieCheck, $mReturnToQuery, $mToken;
+       var $mSkipCookieCheck, $mReturnToQuery, $mToken, $mStickHTTPS;
 
        private $mExtUser = null;
 
+       public function __construct( $request = null ) {
+               parent::__construct( 'Userlogin' );
+
+               if ( $request === null ) {
+                       global $wgRequest;
+                       $this->load( $wgRequest );
+               } else {
+                       $this->load( $request );
+               }
+       }
+
        /**
-        * Constructor
-        * @param $request WebRequest: a WebRequest object passed by reference
-        * @param $par String: subpage parameter
+        * Loader
+        *
+        * @param $request WebRequest object
         */
-       function LoginForm( &$request, $par = '' ) {
+       function load( $request ) {
                global $wgAuth, $wgHiddenPrefs, $wgEnableEmail, $wgRedirectOnLogin;
 
-               $this->mType = ( $par == 'signup' ) ? $par : $request->getText( 'type' ); # Check for [[Special:Userlogin/signup]]
-               $this->mName = $request->getText( 'wpName' );
+               $this->mType = $request->getText( 'type' );
+               $this->mUsername = $request->getText( 'wpName' );
                $this->mPassword = $request->getText( 'wpPassword' );
                $this->mRetype = $request->getText( 'wpRetype' );
                $this->mDomain = $request->getText( 'wpDomain' );
@@ -89,6 +87,7 @@ class LoginForm {
                $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
                $this->mAction = $request->getVal( 'action' );
                $this->mRemember = $request->getCheck( 'wpRemember' );
+               $this->mStickHTTPS = $request->getCheck( 'wpStickHTTPS' );
                $this->mLanguage = $request->getText( 'uselang' );
                $this->mSkipCookieCheck = $request->getCheck( 'wpSkipCookieCheck' );
                $this->mToken = ( $this->mType == 'signup' ) ? $request->getVal( 'wpCreateaccountToken' ) : $request->getVal( 'wpLoginToken' );
@@ -122,7 +121,15 @@ class LoginForm {
                }
        }
 
-       function execute() {
+       public function execute( $par ) {
+               if ( session_id() == '' ) {
+                       wfSetupSession();
+               }
+
+               if ( $par == 'signup' ) { # Check for [[Special:Userlogin/signup]]
+                       $this->mType = 'signup';
+               }
+
                if ( !is_null( $this->mCookieCheck ) ) {
                        $this->onCookieRedirectCheck( $this->mCookieCheck );
                        return;
@@ -147,7 +154,7 @@ class LoginForm {
                global $wgOut;
 
                if ( $this->mEmail == '' ) {
-                       $this->mainLoginForm( wfMsgExt( 'noemail', array( 'parsemag', 'escape' ), $this->mName ) );
+                       $this->mainLoginForm( wfMsgExt( 'noemail', array( 'parsemag', 'escape' ), $this->mUsername ) );
                        return;
                }
 
@@ -166,16 +173,13 @@ class LoginForm {
                $u->addNewUserLogEntry( true, $this->mReason );
 
                $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
 
-               if( WikiError::isError( $result ) ) {
-                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
+               if( !$result->isGood() ) {
+                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getWikiText() ) );
                } else {
                        $wgOut->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
                        $wgOut->returnToMain( false );
                }
-               $u = 0;
        }
 
        /**
@@ -199,11 +203,11 @@ class LoginForm {
 
                # Send out an email authentication message if needed
                if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
-                       $error = $u->sendConfirmationMail();
-                       if( WikiError::isError( $error ) ) {
-                               $wgOut->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
-                       } else {
+                       $status = $u->sendConfirmationMail();
+                       if( $status->isGood() ) {
                                $wgOut->addWikiMsg( 'confirmemail_oncreate' );
+                       } else {
+                               $wgOut->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
                        }
                }
 
@@ -227,8 +231,6 @@ class LoginForm {
                        # Confirm that the account was created
                        $self = SpecialPage::getTitleFor( 'Userlogin' );
                        $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
-                       $wgOut->setArticleRelated( false );
-                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
                        $wgOut->addHTML( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
                        $wgOut->returnToMain( false, $self );
                        wfRunHooks( 'AddNewAccount', array( $u, false ) );
@@ -258,7 +260,7 @@ class LoginForm {
                // 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( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
+                       if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mUsername ) || !$wgAuth->authenticate( $this->mUsername, $this->mPassword ) ) ) {
                                $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
                                return false;
                        }
@@ -272,7 +274,7 @@ class LoginForm {
                # Request forgery checks.
                if ( !self::getCreateaccountToken() ) {
                        self::setCreateaccountToken();
-                       $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
+                       $this->mainLoginForm( wfMsgExt( 'nocookiesfornew', array( 'parseinline' ) ) );
                        return false;
                }
 
@@ -290,7 +292,7 @@ class LoginForm {
 
                # Check permissions
                if ( !$wgUser->isAllowed( 'createaccount' ) ) {
-                       $this->userNotPrivilegedMessage();
+                       $wgOut->permissionRequired( 'createaccount' );
                        return false;
                } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
                        $this->userBlockedMessage();
@@ -304,7 +306,7 @@ class LoginForm {
                }
 
                # Now create a dummy user ($u) and check if it is valid
-               $name = trim( $this->mName );
+               $name = trim( $this->mUsername );
                $u = User::newFromName( $name, 'creatable' );
                if ( !is_object( $u ) ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
@@ -433,7 +435,7 @@ class LoginForm {
        public function authenticateUserData() {
                global $wgUser, $wgAuth, $wgMemc;
 
-               if ( $this->mName == '' ) {
+               if ( $this->mUsername == '' ) {
                        return self::NO_NAME;
                }
 
@@ -456,7 +458,7 @@ class LoginForm {
 
                $throttleCount = 0;
                if ( is_array( $wgPasswordAttemptThrottle ) ) {
-                       $throttleKey = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
+                       $throttleKey = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mUsername ) );
                        $count = $wgPasswordAttemptThrottle['count'];
                        $period = $wgPasswordAttemptThrottle['seconds'];
 
@@ -481,16 +483,16 @@ class LoginForm {
                // creates the user in the database. Until we load $wgUser, checking
                // for user existence using User::newFromName($name)->getId() below
                // will effectively be using stale data.
-               if ( $wgUser->getName() === $this->mName ) {
-                       wfDebug( __METHOD__ . ": already logged in as {$this->mName}\n" );
+               if ( $wgUser->getName() === $this->mUsername ) {
+                       wfDebug( __METHOD__ . ": already logged in as {$this->mUsername}\n" );
                        return self::SUCCESS;
                }
 
-               $this->mExtUser = ExternalUser::newFromName( $this->mName );
+               $this->mExtUser = ExternalUser::newFromName( $this->mUsername );
 
                # TODO: Allow some magic here for invalid external names, e.g., let the
                # user choose a different wiki name.
-               $u = User::newFromName( $this->mName );
+               $u = User::newFromName( $this->mUsername );
                if( !( $u instanceof User ) || !User::isUsableName( $u->getName() ) ) {
                        return self::ILLEGAL;
                }
@@ -619,7 +621,7 @@ class LoginForm {
                }
 
                wfDebug( __METHOD__ . ": creating account\n" );
-               $user = $this->initUser( $user, true );
+               $this->initUser( $user, true );
                return self::SUCCESS;
        }
 
@@ -639,7 +641,7 @@ class LoginForm {
                                self::clearLoginToken();
 
                                // Reset the throttle
-                               $key = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
+                               $key = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mUsername ) );
                                global $wgMemc;
                                $wgMemc->delete( $key );
 
@@ -657,6 +659,8 @@ class LoginForm {
                                break;
 
                        case self::NEED_TOKEN:
+                               $this->mainLoginForm( wfMsgExt( 'nocookiesforlogin', array( 'parseinline' ) ) );
+                               break;
                        case self::WRONG_TOKEN:
                                $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
                                break;
@@ -669,9 +673,9 @@ class LoginForm {
                                break;
                        case self::NOT_EXISTS:
                                if( $wgUser->isAllowed( 'createaccount' ) ) {
-                                       $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
+                                       $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $this->mUsername ) ) );
                                } else {
-                                       $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
+                                       $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mUsername ) ) );
                                }
                                break;
                        case self::WRONG_PASS:
@@ -691,7 +695,7 @@ class LoginForm {
                                break;
                        case self::USER_BLOCKED:
                                $this->mainLoginForm( wfMsgExt( 'login-userblocked',
-                                       array( 'parsemag', 'escape' ), $this->mName ) );
+                                       array( 'parsemag', 'escape' ), $this->mUsername ) );
                                break;
                        default:
                                throw new MWException( 'Unhandled case value' );
@@ -730,7 +734,7 @@ class LoginForm {
 
                # Check for hooks
                $error = null;
-               if ( !wfRunHooks( 'UserLoginMailPassword', array( $this->mName, &$error ) ) ) {
+               if ( !wfRunHooks( 'UserLoginMailPassword', array( $this->mUsername, &$error ) ) ) {
                        $this->mainLoginForm( $error );
                        return;
                }
@@ -754,11 +758,11 @@ class LoginForm {
                        return;
                }
 
-               if ( $this->mName == '' ) {
+               if ( $this->mUsername == '' ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
                        return;
                }
-               $u = User::newFromName( $this->mName );
+               $u = User::newFromName( $this->mUsername );
                if( !$u instanceof User ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
                        return;
@@ -785,11 +789,11 @@ class LoginForm {
                }
 
                $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
-               if( WikiError::isError( $result ) ) {
-                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
-               } else {
+               if( $result->isGood() ) {
                        $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
                        self::clearLoginToken();
+               } else {
+                       $this->mainLoginForm( $result->getWikiText( 'mailerror' ) );
                }
        }
 
@@ -799,18 +803,18 @@ class LoginForm {
         * @param $throttle Boolean
         * @param $emailTitle String: message name of email title
         * @param $emailText String: message name of email text
-        * @return Mixed: true on success, WikiError on failure
+        * @return Status object
         * @private
         */
        function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
                global $wgServer, $wgScript, $wgUser, $wgNewPasswordExpiry;
 
                if ( $u->getEmail() == '' ) {
-                       return new WikiError( wfMsg( 'noemail', $u->getName() ) );
+                       return Status::newFatal( 'noemail', $u->getName() );
                }
                $ip = wfGetIP();
                if( !$ip ) {
-                       return new WikiError( wfMsg( 'badipaddress' ) );
+                       return Status::newFatal( 'badipaddress' );
                }
 
                wfRunHooks( 'User::mailPasswordInternal', array( &$wgUser, &$ip, &$u ) );
@@ -851,7 +855,12 @@ class LoginForm {
                        if ( !$titleObj instanceof Title ) {
                                $titleObj = Title::newMainPage();
                        }
-                       $wgOut->redirect( $titleObj->getFullURL( $this->mReturnToQuery ) );
+                       $redirectUrl = $titleObj->getFullURL( $this->mReturnToQuery );
+                       global $wgSecureLogin;
+                       if( $wgSecureLogin && !$this->mStickHTTPS ) {
+                               $redirectUrl = preg_replace( '/^https:/', 'http:', $redirectUrl );
+                       }
+                       $wgOut->redirect( $redirectUrl );
                }
        }
 
@@ -877,8 +886,6 @@ class LoginForm {
                global $wgOut, $wgUser;
 
                $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
                $wgOut->addWikiMsg( $msgname, $wgUser->getName() );
                $wgOut->addHTML( $injected_html );
 
@@ -889,22 +896,6 @@ class LoginForm {
                }
        }
 
-       /** */
-       function userNotPrivilegedMessage($errors) {
-               global $wgOut;
-
-               $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
-
-               $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
-               // Stuff that might want to be added at the end. For example, instruc-
-               // tions if blocked.
-               $wgOut->addWikiMsg( 'cantcreateaccount-nonblock-text' );
-
-               $wgOut->returnToMain( false );
-       }
-
        /** */
        function userBlockedMessage() {
                global $wgOut, $wgUser;
@@ -918,8 +909,6 @@ class LoginForm {
                # out.
 
                $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
 
                $ip = wfGetIP();
                $blocker = User::whoIs( $wgUser->mBlock->mBy );
@@ -936,9 +925,11 @@ class LoginForm {
         * @private
         */
        function mainLoginForm( $msg, $msgtype = 'error' ) {
-               global $wgUser, $wgOut, $wgHiddenPrefs, $wgEnableEmail;
+               global $wgUser, $wgOut, $wgHiddenPrefs;
+               global $wgEnableEmail, $wgEnableUserEmail;
                global $wgRequest, $wgLoginLanguageSelector;
                global $wgAuth, $wgEmailConfirmToEdit, $wgCookieExpiration;
+               global $wgSecureLogin;
 
                $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
 
@@ -958,16 +949,14 @@ class LoginForm {
                        }
                }
 
-               if ( $this->mName == '' ) {
+               if ( $this->mUsername == '' ) {
                        if ( $wgUser->isLoggedIn() ) {
-                               $this->mName = $wgUser->getName();
+                               $this->mUsername = $wgUser->getName();
                        } else {
-                               $this->mName = $wgRequest->getCookie( 'UserName' );
+                               $this->mUsername = $wgRequest->getCookie( 'UserName' );
                        }
                }
 
-               $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
-
                if ( $this->mType == 'signup' ) {
                        global $wgLivePasswordStrengthChecks;
                        if ( $wgLivePasswordStrengthChecks ) {
@@ -1011,7 +1000,7 @@ class LoginForm {
                }
 
                $template->set( 'header', '' );
-               $template->set( 'name', $this->mName );
+               $template->set( 'name', $this->mUsername );
                $template->set( 'password', $this->mPassword );
                $template->set( 'retype', $this->mRetype );
                $template->set( 'email', $this->mEmail );
@@ -1026,10 +1015,13 @@ class LoginForm {
                $template->set( 'userealname', !in_array( 'realname', $wgHiddenPrefs ) );
                $template->set( 'useemail', $wgEnableEmail );
                $template->set( 'emailrequired', $wgEmailConfirmToEdit );
+               $template->set( 'emailothers', $wgEnableUserEmail );
                $template->set( 'canreset', $wgAuth->allowPasswordChange() );
                $template->set( 'canremember', ( $wgCookieExpiration > 0 ) );
                $template->set( 'usereason', $wgUser->isLoggedIn() );
                $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) || $this->mRemember );
+               $template->set( 'cansecurelogin', ( $wgSecureLogin === true ) );
+               $template->set( 'stickHTTPS', $this->mStickHTTPS );
 
                if ( $this->mType == 'signup' ) {
                        if ( !self::getCreateaccountToken() ) {
@@ -1065,8 +1057,6 @@ class LoginForm {
                        $wgOut->setPageTitle( wfMsg( 'userloginnocreate' ) );
                }
 
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
                $wgOut->disallowUserJs(); // just in case...
                $wgOut->addTemplate( $template );
        }
@@ -1198,9 +1188,9 @@ class LoginForm {
        function makeLanguageSelector() {
                global $wgLang;
 
-               $msg = wfMsgForContent( 'loginlanguagelinks' );
-               if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
-                       $langs = explode( "\n", $msg );
+               $msg = wfMessage( 'loginlanguagelinks' )->inContentLanguage();
+               if( !$msg->isBlank() ) {
+                       $langs = explode( "\n", $msg->text() );
                        $links = array();
                        foreach( $langs as $lang ) {
                                $lang = trim( $lang, '* ' );