Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
index 0cb8a2e..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();
@@ -29,8 +37,7 @@ function wfSpecialUserlogin()
        global $wgOut, $wpEmail, $wpName;
        
        if ("" == $wpEmail) {
-               $m = str_replace( "$1", $wpName, wfMsg( "noemail" ) );
-               mainLoginForm( $m );
+               mainLoginForm( wfMsg( "noemail", $wpName ) );
                return;
        }
 
@@ -41,8 +48,7 @@ function wfSpecialUserlogin()
        }
 
        $u->saveSettings();
-       if (mailPasswordInternal($u) == NULL)
-       {
+       if (mailPasswordInternal($u) == NULL) {
                return;  
        }
 
@@ -50,9 +56,7 @@ function wfSpecialUserlogin()
        $wgOut->setRobotpolicy( "noindex,nofollow" );
        $wgOut->setArticleFlag( false );
 
-       $m = str_replace( "$1", $u->getName(), wfMsg( "accmailtext" ) );
-       $m = str_replace( "$2", $u->getEmail(), $m );
-       $wgOut->addWikiText( $m );
+       $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
        $wgOut->returnToMain( false );
 
        $u = 0;
@@ -71,15 +75,23 @@ function wfSpecialUserlogin()
        }
 
        $wgUser = $u;
-       $m = str_replace( "$1", $wgUser->getName(), wfMsg( "welcomecreation" ) );
-       successfulLogin( $m );
+       $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;
+       global $wpEmail;
 
        if (!$wgUser->isAllowedToCreateAccount()) {
                userNotPrivilegedMessage();
@@ -92,7 +104,7 @@ function wfSpecialUserlogin()
        }
        $wpName = trim( $wpName );
        if ( ( "" == $wpName ) ||
-         preg_match( "/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/", $wpName ) ||
+         preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $wpName ) ||
          (strpos( $wpName, "/" ) !== false) ) 
        {
                mainLoginForm( wfMsg( "noname" ) );
@@ -124,6 +136,7 @@ function wfSpecialUserlogin()
 /* private */ function processLogin()
 {
        global $wgUser, $wpName, $wpPassword, $wpRemember;
+       global $wgDeferredUpdateList;
        global $returnto;
 
        if ( "" == $wpName ) {
@@ -133,8 +146,7 @@ function wfSpecialUserlogin()
        $u = User::newFromName( $wpName );
        $id = $u->idForName();
        if ( 0 == $id ) {
-               $m = str_replace( "$1", $u->getName(), wfMsg( "nosuchuser" ) );
-               mainLoginForm( $m );
+               mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
                return;
        }
        $u->setId( $id );
@@ -158,13 +170,22 @@ function wfSpecialUserlogin()
        $u->setOption( "rememberpassword", $r );
 
        $wgUser = $u;
-       $m = str_replace( "$1", $wgUser->getName(), wfMsg( "loginsuccess" ) );
-       successfulLogin( $m );
+       $wgUser->setCookies();
+
+       $up = new UserUpdate();
+       array_push( $wgDeferredUpdateList, $up );
+
+       if( hasSessionCookie() ) {
+               return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
+       } else {
+               return cookieRedirectCheck( "login" );
+       }
 }
 
 /* private */ function mailPassword()
 {
        global $wgUser, $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
+       global $wgCookiePath, $wgCookieDomain, $wgDBname;
 
        if ( "" == $wpName ) {
                mainLoginForm( wfMsg( "noname" ) );
@@ -173,8 +194,7 @@ function wfSpecialUserlogin()
        $u = User::newFromName( $wpName );
        $id = $u->idForName();
        if ( 0 == $id ) {
-               $m = str_replace( "$1", $u->getName(), wfMsg( "nosuchuser" ) );
-               mainLoginForm( $m );
+               mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
                return;
        }
        $u->setId( $id );
@@ -184,33 +204,29 @@ function wfSpecialUserlogin()
                return;
        }
 
-       $m = str_replace( "$1", $u->getName(), wfMsg( "passwordsent" ) );
-       mainLoginForm( $m );
+       mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
 }
 
 
 /* private */ function mailPasswordInternal( $u )
 {
        global $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
-       global $wgPasswordSender;
+       global $wgPasswordSender, $wgDBname;
 
        if ( "" == $u->getEmail() ) {
-               $m = str_replace( "$1", $u->getName(), wfMsg( "noemail" ) );
-               mainLoginForm( $m );
+               mainLoginForm( wfMsg( "noemail", $u->getName() ) );
                return;
        }
        $np = User::randomPassword();
        $u->setNewpassword( $np );
 
-       setcookie( "wcUserPassword", "", time() - 3600 );
+       setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
        $u->saveSettings();
 
        $ip = getenv( "REMOTE_ADDR" );
        if ( "" == $ip ) { $ip = "(Unknown)"; }
 
-       $m = str_replace( "$1", $ip, wfMsg( "passwordremindertext" ) );
-       $m = str_replace( "$2", $u->getName(), $m );
-       $m = str_replace( "$3", $np, $m );
+       $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
 
        mail( $u->getEmail(), wfMsg( "passwordremindertitle" ), $m,
          "MIME-Version: 1.0\r\n" .
@@ -227,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" );
@@ -241,37 +254,30 @@ 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;
        global $wpName, $wpPassword, $wpRetype, $wpRemember;
-       global $wpEmail, $HTTP_COOKIE_VARS;
+       global $wpEmail, $HTTP_COOKIE_VARS, $wgDBname;
 
        $le = wfMsg( "loginerror" );
        $yn = wfMsg( "yourname" );
        $yp = wfMsg( "yourpassword" );
        $ypa = wfMsg( "yourpasswordagain" );
        $rmp = wfMsg( "remembermypassword" );
-       $ayn = wfMsg( "areyounew" );
        $nuo = wfMsg( "newusersonly" );
        $li = wfMsg( "login" );
        $ca = wfMsg( "createaccount" );
@@ -285,7 +291,7 @@ function userNotPrivilegedMessage()
                if ( 0 != $wgUser->getID() ) {
                        $name = $wgUser->getName();
                } else {
-                       $name = $HTTP_COOKIE_VARS["wcUserName"];
+                       $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
                }
        }
        $pwd = $wpPassword;
@@ -295,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" );
@@ -319,19 +326,25 @@ color='red'>$err</font>\n" );
        }
 
        $wgOut->addHTML( "
-<form id=\"userlogin\" method=\"post\" action=\"{$action}\">
+<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()) {
 
@@ -352,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,6 +374,39 @@ $cambutton
 
 
 
+}
+
+/* private */ function hasSessionCookie()
+{
+       global $wgDisableCookieCheck;
+       return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
+}
+  
+/* private */ function cookieRedirectCheck( $type )
+{
+       global $wgOut, $wgLang;
+
+       $check = wfLocalUrl( $wgLang->specialPage( "Userlogin" ),
+                        "wpCookieCheck=$type" );
+
+       return $wgOut->redirect( $check );
+}
+
+/* private */ function onCookieRedirectCheck( $type ) {
+       global $wgUser;
+
+       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() ) );
+       }
 }
 
 ?>