* (bug 4298) Include rc_id on enhanced RC singleton diff links for patrolling
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
index b33b953..719be81 100644 (file)
@@ -11,7 +11,7 @@
 function wfSpecialUserlogin() {
        global $wgCommandLineMode;
        global $wgRequest;
-       if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] )  ) {
+       if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] )  ) {
                User::SetupSession();
        }
        
@@ -27,7 +27,7 @@ function wfSpecialUserlogin() {
 class LoginForm {
        var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
        var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
-       var $mLoginattempt, $mRemember, $mEmail;
+       var $mLoginattempt, $mRemember, $mEmail, $mDomain;
        
        /**
         * Constructor
@@ -35,10 +35,13 @@ class LoginForm {
         */
        function LoginForm( &$request ) {
                global $wgLang, $wgAllowRealName, $wgEnableEmail;
+               global $wgAuth;
 
+               $this->mType = $request->getText( 'type' );
                $this->mName = $request->getText( 'wpName' );
                $this->mPassword = $request->getText( 'wpPassword' );
                $this->mRetype = $request->getText( 'wpRetype' );
+               $this->mDomain = $request->getText( 'wpDomain' );
                $this->mReturnto = $request->getVal( 'returnto' );
                $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
                $this->mPosted = $request->wasPosted();
@@ -61,7 +64,12 @@ class LoginForm {
                } else {
                    $this->mRealName = '';
                }
-           
+               
+               if( !$wgAuth->validDomain( $this->mDomain ) ) {
+                       $this->mDomain = 'invaliddomain';
+               }
+               $wgAuth->setDomain( $this->mDomain );
                # When switching accounts, it sucks to get automatically logged out
                if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
                        $this->mReturnto = '';
@@ -124,7 +132,7 @@ class LoginForm {
         * @access private
         */
        function addNewAccount() {
-               global $wgUser, $wgOut;
+               global $wgUser, $wgOut, $wgEmailAuthentication;
 
                $u = $this->addNewAccountInternal();
 
@@ -136,10 +144,12 @@ class LoginForm {
                $wgUser->setCookies();
 
                $wgUser->saveSettings();
-               if( $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
+               if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
                        $wgUser->sendConfirmationMail();
                }
 
+               wfRunHooks( 'AddNewAccount' );
+
                if( $this->hasSessionCookie() ) {
                        return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
                } else {
@@ -152,15 +162,43 @@ class LoginForm {
         */
        function addNewAccountInternal() {
                global $wgUser, $wgOut;
-               global $wgMaxNameChars;
-               global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
-               global $wgMinimalPasswordLength;
+               global $wgEnableSorbs, $wgProxyWhitelist;
+               global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
+               global $wgAuth, $wgMinimalPasswordLength;
+
+               // If the user passes an invalid domain, something is fishy
+               if( !$wgAuth->validDomain( $this->mDomain ) ) {
+                       $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
+                       return false;
+               }
+
+               // If we are not allowing users to login locally, we should
+               // be checking to see if the user is actually able to
+               // authenticate to the authentication 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( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
+                               $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
+                               return false;
+                       }
+               }
 
                if (!$wgUser->isAllowedToCreateAccount()) {
                        $this->userNotPrivilegedMessage();
                        return false;
                }
 
+               $ip = wfGetIP();
+               if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) && 
+                 $wgUser->inSorbsBlacklist( $ip ) ) 
+               {
+                       $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
+                       return;
+               }
+
+
                if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
                        $this->mainLoginForm( wfMsg( 'badretype' ) );
                        return false;
@@ -168,16 +206,11 @@ class LoginForm {
                
                $name = trim( $this->mName );
                $u = User::newFromName( $name );
-               if ( is_null( $u ) ||
-                 ( '' == $name ) ||
-                 $wgUser->isIP( $name ) ||
-                 (strpos( $name, '/' ) !== false) ||
-                 (strlen( $name ) > $wgMaxNameChars) ||
-                 ucFirst($name) != $u->getName() ) 
-               {
+               if ( is_null( $u ) ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
                        return false;
                }
+               
                if ( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
                        return false;
@@ -188,13 +221,13 @@ class LoginForm {
                        return false;
                }
 
-               if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
+               if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
                        $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
                        return false;
                }
 
                if ( $wgAccountCreationThrottle ) {
-                       $key = $wgDBname.':acctcreate:ip:'.$wgIP;
+                       $key = $wgDBname.':acctcreate:ip:'.$ip;
                        $value = $wgMemc->incr( $key );
                        if ( !$value ) {
                                $wgMemc->set( $key, 1, 86400 );
@@ -205,6 +238,15 @@ class LoginForm {
                        }
                }
 
+               if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
+                       $this->mainLoginForm( wfMsg( 'externaldberror' ) );
+                       return false;
+               }
+
+               # Update user count
+               $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
+               $ssUpdate->doUpdate();
+
                return $this->initUser( $u );
        }
        
@@ -226,9 +268,7 @@ class LoginForm {
                global $wgAuth;
                $wgAuth->initUser( $u );
 
-               if ( $this->mRemember ) { $r = 1; }
-               else { $r = 0; }
-               $u->setOption( 'rememberpassword', $r );
+               $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
                
                return $u;
        }
@@ -238,6 +278,7 @@ class LoginForm {
         */
        function processLogin() {
                global $wgUser;
+               global $wgAuth;
 
                if ( '' == $this->mName ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
@@ -271,7 +312,7 @@ class LoginForm {
                }
 
                if (!$u->checkPassword( $this->mPassword )) {
-                       $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
+                       $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
                        return;
                }
 
@@ -284,6 +325,8 @@ class LoginForm {
                }
                $u->setOption( 'rememberpassword', $r );
 
+               $wgAuth->updateUser( $u );
+
                $wgUser = $u;
                $wgUser->setCookies();
 
@@ -300,8 +343,7 @@ class LoginForm {
         * @access private
         */
        function mailPassword() {
-               global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
-               global $wgCookiePath, $wgCookieDomain, $wgDBname;
+               global $wgUser, $wgDBname;
 
                if ( '' == $this->mName ) {
                        $this->mainLoginForm( wfMsg( 'noname' ) );
@@ -323,7 +365,7 @@ class LoginForm {
                if( WikiError::isError( $result ) ) {
                        $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
                } else {
-                       $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
+                       $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
                }
        }
 
@@ -333,8 +375,7 @@ class LoginForm {
         * @access private
         */
        function mailPasswordInternal( $u ) {
-               global $wgPasswordSender, $wgDBname, $wgIP;
-               global $wgCookiePath, $wgCookieDomain;
+               global $wgDBname, $wgCookiePath, $wgCookieDomain;
 
                if ( '' == $u->getEmail() ) {
                        return wfMsg( 'noemail', $u->getName() );
@@ -347,12 +388,12 @@ class LoginForm {
 
                $u->saveSettings();
 
-               $ip = $wgIP;
+               $ip = wfGetIP();
                if ( '' == $ip ) { $ip = '(Unknown)'; }
 
-               $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
-               $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
-               
+               $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
+
+               $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
                return $result;
        }
 
@@ -392,9 +433,10 @@ class LoginForm {
        /**
         * @access private
         */
-       function mainLoginForm( $err ) {
+       function mainLoginForm( $msg, $msgtype = 'error' ) {
                global $wgUser, $wgOut, $wgLang;
                global $wgDBname, $wgAllowRealName, $wgEnableEmail;
+               global $wgAuth;
 
                if ( '' == $this->mName ) {
                        if ( $wgUser->isLoggedIn() ) {
@@ -404,28 +446,50 @@ class LoginForm {
                        }
                }
 
-               $q = 'action=submitlogin';
-               if ( !empty( $this->mReturnto ) ) {
-                       $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
-               }
                $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
 
                require_once( 'templates/Userlogin.php' );
-               $template =& new UserloginTemplate();
+
+               if ( $this->mType == 'signup' ) {
+                       $template =& new UsercreateTemplate();
+                       $q = 'action=submitlogin&type=signup';
+                       $linkq = 'type=login';
+                       $linkmsg = 'gotaccount';
+               } else {
+                       $template =& new UserloginTemplate();
+                       $q = 'action=submitlogin&type=login';
+                       $linkq = 'type=signup';
+                       $linkmsg = 'nologin';
+               }
+
+               if ( !empty( $this->mReturnto ) ) {
+                       $returnto = '&returnto=' . wfUrlencode( $this->mReturnto );
+                       $q .= $returnto;
+                       $linkq .= $returnto;
+               }
+
+               $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
+               $link .= wfMsgHtml( $linkmsg . 'link' );
+               $link .= '</a>';
+
+               $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
                
                $template->set( 'name', $this->mName );
                $template->set( 'password', $this->mPassword );
                $template->set( 'retype', $this->mRetype );
                $template->set( 'email', $this->mEmail );
                $template->set( 'realname', $this->mRealName );
+               $template->set( 'domain', $this->mDomain );
 
                $template->set( 'action', $titleObj->getLocalUrl( $q ) );
-               $template->set( 'error', $err );
+               $template->set( 'message', $msg );
+               $template->set( 'messagetype', $msgtype );
                $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
                $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
                $template->set( 'userealname', $wgAllowRealName );
                $template->set( 'useemail', $wgEnableEmail );
                $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember  );
+               $wgAuth->modifyUITemplate( $template );
                
                $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
@@ -438,7 +502,7 @@ class LoginForm {
         */
        function hasSessionCookie() {
                global $wgDisableCookieCheck;
-               return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
+               return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
        }
          
        /**