Fix early finish problem introduced by dupe message fix
[mediawiki.git] / includes / SpecialUserlogin.php
blob0ef1d3ba1e09b21355b5d0160be4215fde2e3e6b
1 <?php
3 function wfSpecialUserlogin()
5 global $wpCreateaccount, $wpCreateaccountMail;
6 global $wpLoginattempt, $wpMailmypassword;
7 global $action, $_REQUEST, $wgCommandLineMode;
8 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
9 User::SetupSession();
12 $fields = array( "wpName", "wpPassword", "wpName",
13 "wpPassword", "wpRetype", "wpEmail" );
14 wfCleanFormFields( $fields );
16 # When switching accounts, it sucks to get automatically logged out
17 global $returnto, $wgLang;
18 if( $returnto == $wgLang->specialPage( "Userlogout" ) ) $returnto = "";
20 $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
22 if ( isset( $wpCookieCheck ) ) {
23 onCookieRedirectCheck( $wpCookieCheck );
24 } else if ( isset( $wpCreateaccount ) ) {
25 addNewAccount();
26 } else if ( isset( $wpCreateaccountMail ) ) {
27 addNewAccountMailPassword();
28 } else if ( isset( $wpMailmypassword ) ) {
29 mailPassword();
30 } else if ( "submit" == $action || isset( $wpLoginattempt ) ) {
31 processLogin();
32 } else {
33 mainLoginForm( "" );
38 /* private */ function addNewAccountMailPassword()
40 global $wgOut, $wpEmail, $wpName;
42 if ("" == $wpEmail) {
43 mainLoginForm( wfMsg( "noemail", $wpName ) );
44 return;
47 $u = addNewaccountInternal();
49 if ($u == NULL) {
50 return;
53 $u->saveSettings();
54 if (mailPasswordInternal($u) == NULL) {
55 return;
58 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
59 $wgOut->setRobotpolicy( "noindex,nofollow" );
60 $wgOut->setArticleRelated( false );
62 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
63 $wgOut->returnToMain( false );
65 $u = 0;
69 /* private */ function addNewAccount()
71 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
72 global $wpEmail, $wgDeferredUpdateList;
74 $u = addNewAccountInternal();
76 if ($u == NULL) {
77 return;
80 $wgUser = $u;
81 $wgUser->setCookies();
83 $up = new UserUpdate();
84 array_push( $wgDeferredUpdateList, $up );
86 if( hasSessionCookie() ) {
87 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
88 } else {
89 return cookieRedirectCheck( "new" );
94 /* private */ function addNewAccountInternal()
96 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
97 global $wpEmail, $wgMaxNameChars;
99 if (!$wgUser->isAllowedToCreateAccount()) {
100 userNotPrivilegedMessage();
101 return;
104 if ( 0 != strcmp( $wpPassword, $wpRetype ) ) {
105 mainLoginForm( wfMsg( "badretype" ) );
106 return;
108 $wpName = trim( $wpName );
109 if ( ( "" == $wpName ) ||
110 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $wpName ) ||
111 (strpos( $wpName, "/" ) !== false) ||
112 (strlen( $wpName ) > $wgMaxNameChars) )
114 mainLoginForm( wfMsg( "noname" ) );
115 return;
117 if ( wfReadOnly() ) {
118 $wgOut->readOnlyPage();
119 return;
121 $u = User::newFromName( $wpName );
123 if ( 0 != $u->idForName() ) {
124 mainLoginForm( wfMsg( "userexists" ) );
125 return;
127 $u->addToDatabase();
128 $u->setPassword( $wpPassword );
129 $u->setEmail( $wpEmail );
130 if ( 1 == $wpRemember ) { $r = 1; }
131 else { $r = 0; }
132 $u->setOption( "rememberpassword", $r );
134 return $u;
140 /* private */ function processLogin()
142 global $wgUser, $wpName, $wpPassword, $wpRemember;
143 global $wgDeferredUpdateList;
144 global $returnto;
146 if ( "" == $wpName ) {
147 mainLoginForm( wfMsg( "noname" ) );
148 return;
150 $u = User::newFromName( $wpName );
151 $id = $u->idForName();
152 if ( 0 == $id ) {
153 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
154 return;
156 $u->setId( $id );
157 $u->loadFromDatabase();
158 $ep = $u->encryptPassword( $wpPassword );
159 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
160 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
161 mainLoginForm( wfMsg( "wrongpassword" ) );
162 return;
166 # We've verified now, update the real record
168 if ( 1 == $wpRemember ) {
169 $r = 1;
170 $u->setCookiePassword( $wpPassword );
171 } else {
172 $r = 0;
174 $u->setOption( "rememberpassword", $r );
176 $wgUser = $u;
177 $wgUser->setCookies();
179 $up = new UserUpdate();
180 array_push( $wgDeferredUpdateList, $up );
182 if( hasSessionCookie() ) {
183 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
184 } else {
185 return cookieRedirectCheck( "login" );
189 /* private */ function mailPassword()
191 global $wgUser, $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
192 global $wgCookiePath, $wgCookieDomain, $wgDBname;
194 if ( "" == $wpName ) {
195 mainLoginForm( wfMsg( "noname" ) );
196 return;
198 $u = User::newFromName( $wpName );
199 $id = $u->idForName();
200 if ( 0 == $id ) {
201 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
202 return;
204 $u->setId( $id );
205 $u->loadFromDatabase();
207 if (mailPasswordInternal($u) == NULL) {
208 return;
211 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
215 /* private */ function mailPasswordInternal( $u )
217 global $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
218 global $wgPasswordSender, $wgDBname, $wgIP;
220 if ( "" == $u->getEmail() ) {
221 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
222 return;
224 $np = User::randomPassword();
225 $u->setNewpassword( $np );
227 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
228 $u->saveSettings();
230 $ip = $wgIP;
231 if ( "" == $ip ) { $ip = "(Unknown)"; }
233 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
235 mail( $u->getEmail(), wfMsg( "passwordremindertitle" ), $m,
236 "MIME-Version: 1.0\r\n" .
237 "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
238 "Content-transfer-encoding: 8bit\r\n" .
239 "From: $wgPasswordSender" );
241 return $u;
248 /* private */ function successfulLogin( $msg )
250 global $wgUser;
251 global $wgDeferredUpdateList;
252 global $wgOut;
254 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
255 $wgOut->setRobotpolicy( "noindex,nofollow" );
256 $wgOut->setArticleRelated( false );
257 $wgOut->addHTML( $msg . "\n<p>" );
258 $wgOut->returnToMain();
261 function userNotPrivilegedMessage()
263 global $wgOut, $wgUser, $wgLang;
265 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
266 $wgOut->setRobotpolicy( "noindex,nofollow" );
267 $wgOut->setArticleRelated( false );
269 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
271 $wgOut->returnToMain( false );
274 /* private */ function mainLoginForm( $err )
276 global $wgUser, $wgOut, $wgLang, $returnto;
277 global $wpName, $wpPassword, $wpRetype, $wpRemember;
278 global $wpEmail, $HTTP_COOKIE_VARS, $wgDBname;
280 $le = wfMsg( "loginerror" );
281 $yn = wfMsg( "yourname" );
282 $yp = wfMsg( "yourpassword" );
283 $ypa = wfMsg( "yourpasswordagain" );
284 $rmp = wfMsg( "remembermypassword" );
285 $nuo = wfMsg( "newusersonly" );
286 $li = wfMsg( "login" );
287 $ca = wfMsg( "createaccount" );
288 $cam = wfMsg( "createaccountmail" );
289 $ye = wfMsg( "youremail" );
290 $efl = wfMsg( "emailforlost" );
291 $mmp = wfMsg( "mailmypassword" );
292 $endText = wfMsg( "loginend" );
295 $name = $wpName;
296 if ( "" == $name ) {
297 if ( 0 != $wgUser->getID() ) {
298 $name = $wgUser->getName();
299 } else {
300 $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
303 $pwd = $wpPassword;
305 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
306 $wgOut->setRobotpolicy( "noindex,nofollow" );
307 $wgOut->setArticleRelated( false );
309 if ( "" == $err ) {
310 $lp = wfMsg( "loginprompt" );
311 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
312 } else {
313 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
314 color='red'>$err</font>\n" );
316 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
317 $checked = " checked";
318 } else {
319 $checked = "";
321 $q = "action=submit";
322 if ( "" != $returnto ) { $q .= "&returnto=" . wfUrlencode($returnto); }
323 $action = wfLocalUrlE( $wgLang->specialPage( "Userlogin" ), $q );
325 $wpName = wfEscapeHTML( $wpName );
326 $wpPassword = wfEscapeHTML( $wpPassword );
327 $wpRetype = wfEscapeHTML( $wpRetype );
328 $wpEmail = wfEscapeHTML( $wpEmail );
330 if ($wgUser->getID() != 0) {
331 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
334 $wgOut->addHTML( "
335 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
336 <table border=0><tr>
337 <td align=right>$yn:</td>
338 <td align=left>
339 <input tabindex=1 type=text name=\"wpName\" value=\"{$name}\" size=20>
340 </td>
341 <td align=left>
342 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
343 </td>
344 </tr>
345 <tr>
346 <td align=right>$yp:</td>
347 <td align=left>
348 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$pwd}\" size=20>
349 </td>
350 <td align=left>
351 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
352 </td>
353 </tr>");
355 if ($wgUser->isAllowedToCreateAccount()) {
357 $wgOut->addHTML("<tr><td colspan=3>&nbsp;</td></tr><tr>
358 <td align=right>$ypa:</td>
359 <td align=left>
360 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$wpRetype}\"
361 size=20>
362 </td><td>$nuo</td></tr>
363 <tr>
364 <td align=right>$ye:</td>
365 <td align=left>
366 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$wpEmail}\" size=20>
367 </td><td align=left>
368 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
369 $cambutton
370 </td></tr>");
373 $wgOut->addHTML("
374 <tr><td colspan=3>&nbsp;</td></tr><tr>
375 <td colspan=3 align=left>
376 <p>$efl<br>
377 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
378 </td></tr></table>
379 </form>\n" );
380 $wgOut->addHTML( $endText );
383 /* private */ function hasSessionCookie()
385 global $wgDisableCookieCheck;
386 return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
389 /* private */ function cookieRedirectCheck( $type )
391 global $wgOut, $wgLang;
393 $check = wfLocalUrl( wfUrlEncode( $wgLang->specialPage( "Userlogin" ) ),
394 "wpCookieCheck=$type" );
396 return $wgOut->redirect( $check );
399 /* private */ function onCookieRedirectCheck( $type ) {
400 global $wgUser;
402 if ( !hasSessionCookie() ) {
403 if ( $type == "new" ) {
404 return mainLoginForm( wfMsg( "nocookiesnew" ) );
405 } else if ( $type == "login" ) {
406 return mainLoginForm( wfMsg( "nocookieslogin" ) );
407 } else {
408 # shouldn't happen
409 return mainLoginForm( wfMsg( "error" ) );
411 } else {
412 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );