Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
index f84f640..584f810 100644 (file)
@@ -4,13 +4,21 @@ function wfSpecialUserlogin()
 {
        global $wpCreateaccount, $wpCreateaccountMail;
        global $wpLoginattempt, $wpMailmypassword;
-       global $action;
-
+       global $action, $_REQUEST;
+       
        $fields = array( "wpName", "wpPassword", "wpName",
          "wpPassword", "wpRetype", "wpEmail" );
        wfCleanFormFields( $fields );
 
-       if ( isset( $wpCreateaccount ) ) {
+       # When switching accounts, it sucks to get automatically logged out
+       global $returnto, $wgLang;
+       if( $returnto == $wgLang->specialPage( "Userlogout" ) ) $returnto = "";
+
+       $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
+
+       if ( isset( $wpCookieCheck ) ) {
+               onCookieRedirectCheck( $wpCookieCheck );
+       } else if ( isset( $wpCreateaccount ) ) {
                addNewAccount();
        } else if ( isset( $wpCreateaccountMail ) ) {
                addNewAccountMailPassword();
@@ -40,8 +48,7 @@ function wfSpecialUserlogin()
        }
 
        $u->saveSettings();
-       if (mailPasswordInternal($u) == NULL)
-       {
+       if (mailPasswordInternal($u) == NULL) {
                return;  
        }
 
@@ -68,18 +75,23 @@ function wfSpecialUserlogin()
        }
 
        $wgUser = $u;
-       successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
+       $wgUser->setCookies();
+
+       $up = new UserUpdate();
+       array_push( $wgDeferredUpdateList, $up );
+
+       if( hasSessionCookie() ) {
+               return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
+       } else {
+               return cookieRedirectCheck( "new" );
+       }
 }
 
 
 /* private */ function addNewAccountInternal()
 {
        global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
-       global $wpEmail, $wgDeferredUpdateList;
-
-        if (!cookieCheck()) {
-                return;
-        }
+       global $wpEmail;
 
        if (!$wgUser->isAllowedToCreateAccount()) {
                userNotPrivilegedMessage();
@@ -124,12 +136,9 @@ function wfSpecialUserlogin()
 /* private */ function processLogin()
 {
        global $wgUser, $wpName, $wpPassword, $wpRemember;
+       global $wgDeferredUpdateList;
        global $returnto;
 
-        if (!cookieCheck()) {
-                return;
-        }
-
        if ( "" == $wpName ) {
                mainLoginForm( wfMsg( "noname" ) );
                return;
@@ -161,7 +170,16 @@ function wfSpecialUserlogin()
        $u->setOption( "rememberpassword", $r );
 
        $wgUser = $u;
-       successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
+       $wgUser->setCookies();
+
+       $up = new UserUpdate();
+       array_push( $wgDeferredUpdateList, $up );
+
+       if( hasSessionCookie() ) {
+               return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
+       } else {
+               return cookieRedirectCheck( "login" );
+       }
 }
 
 /* private */ function mailPassword()
@@ -193,7 +211,7 @@ function wfSpecialUserlogin()
 /* private */ function mailPasswordInternal( $u )
 {
        global $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
-       global $wgPasswordSender;
+       global $wgPasswordSender, $wgDBname;
 
        if ( "" == $u->getEmail() ) {
                mainLoginForm( wfMsg( "noemail", $u->getName() ) );
@@ -225,12 +243,9 @@ function wfSpecialUserlogin()
 
 /* private */ function successfulLogin( $msg )
 {
-       global $wgUser, $wgOut, $returnto;
+       global $wgUser;
        global $wgDeferredUpdateList;
-
-       $wgUser->setCookies();
-       $up = new UserUpdate();
-       array_push( $wgDeferredUpdateList, $up );
+       global $wgOut;
 
        $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
        $wgOut->setRobotpolicy( "noindex,nofollow" );
@@ -239,25 +254,19 @@ function wfSpecialUserlogin()
        $wgOut->returnToMain();
 }
 
-
-
-
-
 function userNotPrivilegedMessage()
 {
        global $wgOut, $wgUser, $wgLang;
-
+       
        $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
        $wgOut->setRobotpolicy( "noindex,nofollow" );
        $wgOut->setArticleFlag( false );
 
        $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
+       
        $wgOut->returnToMain( false );
 }
 
-
-
-
 /* private */ function mainLoginForm( $err )
 {
        global $wgUser, $wgOut, $wgLang, $returnto;
@@ -269,7 +278,6 @@ function userNotPrivilegedMessage()
        $yp = wfMsg( "yourpassword" );
        $ypa = wfMsg( "yourpasswordagain" );
        $rmp = wfMsg( "remembermypassword" );
-       $ayn = wfMsg( "areyounew" );
        $nuo = wfMsg( "newusersonly" );
        $li = wfMsg( "login" );
        $ca = wfMsg( "createaccount" );
@@ -293,7 +301,8 @@ function userNotPrivilegedMessage()
        $wgOut->setArticleFlag( false );
 
        if ( "" == $err ) {
-               $wgOut->addHTML( "<h2>$li:</h2>\n" );
+                $lp = wfMsg( "loginprompt" );
+               $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
        } else {
                $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1' 
 color='red'>$err</font>\n" );
@@ -320,16 +329,22 @@ color='red'>$err</font>\n" );
 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
 <table border=0><tr>
 <td align=right>$yn:</td>
-<td colspan=2 align=left>
+<td align=left>
 <input tabindex=1 type=text name=\"wpName\" value=\"{$name}\" size=20>
-</td></tr><tr>
+</td>
+<td align=left>
+<input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
+</td>
+</tr>
+<tr>
 <td align=right>$yp:</td>
 <td align=left>
 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$pwd}\" size=20>
 </td>
 <td align=left>
-<input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
-</td></tr>");
+<input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
+</td>                     
+</tr>");
 
        if ($wgUser->isAllowedToCreateAccount()) {
 
@@ -350,10 +365,6 @@ $cambutton
        }
 
        $wgOut->addHTML("
-<tr>
-<td colspan=3 align=left>
-<input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
-</td></tr>
 <tr><td colspan=3>&nbsp;</td></tr><tr>
 <td colspan=3 align=left>
 <p>$efl<br>
@@ -365,30 +376,37 @@ $cambutton
 
 }
 
-/* private */ function cookieCheck() {
-
-       global $HTTP_COOKIE_VARS, $wgOut, $returnto;
+/* private */ function hasSessionCookie()
+{
        global $wgDisableCookieCheck;
+       return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
+}
+  
+/* private */ function cookieRedirectCheck( $type )
+{
+       global $wgOut, $wgLang;
 
-       if ( $wgDisableCookieCheck ) {
-               return true;
-       }
+       $check = wfLocalUrl( $wgLang->specialPage( "Userlogin" ),
+                        "wpCookieCheck=$type" );
 
-       # XXX: kind of crude check to see if cookies are enabled, but it works OK
+       return $wgOut->redirect( $check );
+}
 
-       if ( "" == $HTTP_COOKIE_VARS[session_name()])
-       {
-               # Don't go back to login page; they won't get time to
-               # enable cookies and send us one, so they'll get this msg again. Instead, 
-               # let them enable cookies on the error page, then go back to login page.
-               # XXX: wipes returnto, unfortunately.
-
-               $returnto = "Special:Userlogin";                
-               $wgOut->errorpage( "nocookies", "nocookiestext" );
-               return false;
-       }
+/* private */ function onCookieRedirectCheck( $type ) {
+       global $wgUser;
 
-       return true;
+       if ( !hasSessionCookie() ) {
+               if ( $type == "new" ) {
+                       return mainLoginForm( wfMsg( "nocookiesnew" ) );
+               } else if ( $type == "login" ) {
+                       return mainLoginForm( wfMsg( "nocookieslogin" ) );
+               } else {
+                       # shouldn't happen
+                       return mainLoginForm( wfMsg( "error" ) );
+               }
+       } else {
+               return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
+       }
 }
 
 ?>