Typo/logic error in move page watchlist update.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2
3 require_once('UserMailer.php');
4
5 function wfSpecialUserlogin()
6 {
7 global $wgCommandLineMode;
8 global $wgRequest;
9 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
10 User::SetupSession();
11 }
12
13 $form = new LoginForm( $wgRequest );
14 $form->execute();
15 }
16
17 class LoginForm {
18 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
19 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
20 var $mLoginattempt, $mRemember, $mEmail;
21
22 function LoginForm( &$request ) {
23 global $wgLang, $wgAllowRealName;
24
25 $this->mName = $request->getText( 'wpName' );
26 $this->mPassword = $request->getText( 'wpPassword' );
27 $this->mRetype = $request->getText( 'wpRetype' );
28 $this->mReturnto = $request->getVal( 'returnto' );
29 $this->mCookieCheck = $request->getVal( "wpCookieCheck" );
30 $this->mPosted = $request->wasPosted();
31 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
32 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' );
33 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' );
34 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
35 $this->mAction = $request->getVal( 'action' );
36 $this->mRemember = $request->getCheck( 'wpRemember' );
37 $this->mEmail = $request->getText( 'wpEmail' );
38 if ($wgAllowRealName) {
39 $this->mRealName = $request->getText( 'wpRealName' );
40 } else {
41 $this->mRealName = '';
42 }
43
44 # When switching accounts, it sucks to get automatically logged out
45 if( $this->mReturnto == $wgLang->specialPage( "Userlogout" ) ) {
46 $this->mReturnto = "";
47 }
48 }
49
50 function execute() {
51 if ( !is_null( $this->mCookieCheck ) ) {
52 $this->onCookieRedirectCheck( $this->mCookieCheck );
53 } else if( $this->mPosted ) {
54 if( $this->mCreateaccount ) {
55 return $this->addNewAccount();
56 } else if ( $this->mCreateaccountMail ) {
57 return $this->addNewAccountMailPassword();
58 } else if ( $this->mMailmypassword ) {
59 return $this->mailPassword();
60 } else if ( ( "submit" == $this->mAction ) || $this->mLoginattempt ) {
61 return $this->processLogin();
62 }
63 }
64 $this->mainLoginForm( "" );
65 }
66
67 /* private */ function addNewAccountMailPassword()
68 {
69 global $wgOut;
70
71 if ("" == $this->mEmail) {
72 $this->mainLoginForm( wfMsg( "noemail", $this->mName ) );
73 return;
74 }
75
76 $u = $this->addNewaccountInternal();
77
78 if ($u == NULL) {
79 return;
80 }
81
82 $u->saveSettings();
83 $error = $this->mailPasswordInternal($u);
84
85 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
86 $wgOut->setRobotpolicy( "noindex,nofollow" );
87 $wgOut->setArticleRelated( false );
88
89 if ( $error === "" ) {
90 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
91 $wgOut->returnToMain( false );
92 } else {
93 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
94 }
95
96 $u = 0;
97 }
98
99
100 /* private */ function addNewAccount()
101 {
102 global $wgUser, $wgOut;
103 global $wgDeferredUpdateList;
104
105 $u = $this->addNewAccountInternal();
106
107 if ($u == NULL) {
108 return;
109 }
110
111 $wgUser = $u;
112 $wgUser->setCookies();
113
114 $up = new UserUpdate();
115 array_push( $wgDeferredUpdateList, $up );
116
117 if( $this->hasSessionCookie() ) {
118 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
119 } else {
120 return $this->cookieRedirectCheck( "new" );
121 }
122 }
123
124
125 /* private */ function addNewAccountInternal()
126 {
127 global $wgUser, $wgOut;
128 global $wgMaxNameChars;
129 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
130
131 if (!$wgUser->isAllowedToCreateAccount()) {
132 $this->userNotPrivilegedMessage();
133 return;
134 }
135
136 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
137 $this->mainLoginForm( wfMsg( "badretype" ) );
138 return;
139 }
140
141 $name = trim( $this->mName );
142 $u = User::newFromName( $name );
143 if ( ( "" == $name ) ||
144 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
145 (strpos( $name, "/" ) !== false) ||
146 (strlen( $name ) > $wgMaxNameChars) ||
147 ucFirst($name) != $u->getName() )
148 {
149 $this->mainLoginForm( wfMsg( "noname" ) );
150 return;
151 }
152 if ( wfReadOnly() ) {
153 $wgOut->readOnlyPage();
154 return;
155 }
156
157 if ( 0 != $u->idForName() ) {
158 $this->mainLoginForm( wfMsg( "userexists" ) );
159 return;
160 }
161
162 if ( $wgAccountCreationThrottle ) {
163 $key = "$wgDBname:acctcreate:ip:$wgIP";
164 $value = $wgMemc->incr( $key );
165 if ( !$value ) {
166 $wgMemc->set( $key, 1, 86400 );
167 }
168 if ( $value > $wgAccountCreationThrottle ) {
169 $this->throttleHit( $wgAccountCreationThrottle );
170 return;
171 }
172 }
173
174 $u->addToDatabase();
175 $u->setPassword( $this->mPassword );
176 $u->setEmail( $this->mEmail );
177 $u->setRealName( $this->mRealName );
178
179 if ( $this->mRemember ) { $r = 1; }
180 else { $r = 0; }
181 $u->setOption( "rememberpassword", $r );
182
183 return $u;
184 }
185
186
187
188 /* private */ function processLogin()
189 {
190 global $wgUser;
191 global $wgDeferredUpdateList;
192
193 if ( "" == $this->mName ) {
194 $this->mainLoginForm( wfMsg( "noname" ) );
195 return;
196 }
197 $u = User::newFromName( $this->mName );
198 $id = $u->idForName();
199 if ( 0 == $id ) {
200 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
201 return;
202 }
203 $u->setId( $id );
204 $u->loadFromDatabase();
205 if (!$u->checkPassword( $this->mPassword )) {
206 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
207 return;
208 }
209
210 # We've verified now, update the real record
211 #
212 if ( $this->mRemember ) {
213 $r = 1;
214 $u->setCookiePassword( $this->mPassword );
215 } else {
216 $r = 0;
217 }
218 $u->setOption( "rememberpassword", $r );
219
220 $wgUser = $u;
221 $wgUser->setCookies();
222
223 $up = new UserUpdate();
224 array_push( $wgDeferredUpdateList, $up );
225
226 if( $this->hasSessionCookie() ) {
227 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
228 } else {
229 return $this->cookieRedirectCheck( "login" );
230 }
231 }
232
233 /* private */ function mailPassword()
234 {
235 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
236 global $wgCookiePath, $wgCookieDomain, $wgDBname;
237
238 if ( "" == $this->mName ) {
239 $this->mainLoginForm( wfMsg( "noname" ) );
240 return;
241 }
242 $u = User::newFromName( $this->mName );
243 $id = $u->idForName();
244 if ( 0 == $id ) {
245 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
246 return;
247 }
248 $u->setId( $id );
249 $u->loadFromDatabase();
250
251 $error = $this->mailPasswordInternal( $u );
252 if ($error === "") {
253 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
254 } else {
255 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
256 }
257
258 }
259
260
261 /* private */ function mailPasswordInternal( $u )
262 {
263 global $wgDeferredUpdateList, $wgOutputEncoding;
264 global $wgPasswordSender, $wgDBname, $wgIP;
265 global $wgCookiePath, $wgCookieDomain;
266
267 if ( "" == $u->getEmail() ) {
268 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
269 return;
270 }
271 $np = User::randomPassword();
272 $u->setNewpassword( $np );
273
274 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
275 $u->saveSettings();
276
277 $ip = $wgIP;
278 if ( "" == $ip ) { $ip = "(Unknown)"; }
279
280 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
281
282 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
283
284 return $error;
285 }
286
287
288
289
290
291 /* private */ function successfulLogin( $msg )
292 {
293 global $wgUser;
294 global $wgDeferredUpdateList;
295 global $wgOut;
296
297 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
298 $wgOut->setRobotpolicy( "noindex,nofollow" );
299 $wgOut->setArticleRelated( false );
300 $wgOut->addHTML( $msg );
301 $wgOut->returnToMain();
302 }
303
304 function userNotPrivilegedMessage()
305 {
306 global $wgOut, $wgUser, $wgLang;
307
308 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
309 $wgOut->setRobotpolicy( "noindex,nofollow" );
310 $wgOut->setArticleRelated( false );
311
312 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
313
314 $wgOut->returnToMain( false );
315 }
316
317 /* private */ function mainLoginForm( $err )
318 {
319 global $wgUser, $wgOut, $wgLang;
320 global $wgDBname, $wgAllowRealName;
321
322 $le = wfMsg( "loginerror" );
323 $yn = wfMsg( "yourname" );
324 $yp = wfMsg( "yourpassword" );
325 $ypa = wfMsg( "yourpasswordagain" );
326 $rmp = wfMsg( "remembermypassword" );
327 $nuo = wfMsg( "newusersonly" );
328 $li = wfMsg( "login" );
329 $ca = wfMsg( "createaccount" );
330 $cam = wfMsg( "createaccountmail" );
331 $ye = wfMsg( "youremail" );
332 if ($wgAllowRealName) {
333 $yrn = wfMsg( "yourrealname" );
334 } else {
335 $yrn = '';
336 }
337 $efl = wfMsg( "emailforlost" );
338 $mmp = wfMsg( "mailmypassword" );
339 $endText = wfMsg( "loginend" );
340
341 if ( $endText = "&lt;loginend&gt;" ) {
342 $endText = "";
343 }
344
345 if ( "" == $this->mName ) {
346 if ( 0 != $wgUser->getID() ) {
347 $this->mName = $wgUser->getName();
348 } else {
349 $this->mName = @$_COOKIE["{$wgDBname}UserName"];
350 }
351 }
352
353 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
354 $wgOut->setRobotpolicy( "noindex,nofollow" );
355 $wgOut->setArticleRelated( false );
356
357 if ( "" == $err ) {
358 $lp = wfMsg( "loginprompt" );
359 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
360 } else {
361 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
362 color='red'>$err</font>\n" );
363 }
364 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
365 $checked = " checked";
366 } else {
367 $checked = "";
368 }
369
370 $q = "action=submit";
371 if ( !empty( $this->mReturnto ) ) {
372 $q .= "&returnto=" . wfUrlencode( $this->mReturnto );
373 }
374
375 $titleObj = Title::makeTitle( NS_SPECIAL, "Userlogin" );
376 $action = $titleObj->escapeLocalUrl( $q );
377
378 $encName = htmlspecialchars( $this->mName );
379 $encPassword = htmlspecialchars( $this->mPassword );
380 $encRetype = htmlspecialchars( $this->mRetype );
381 $encEmail = htmlspecialchars( $this->mEmail );
382 $encRealName = htmlspecialchars( $this->mRealName );
383
384 if ($wgUser->getID() != 0) {
385 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
386 } else {
387 $cambutton = "";
388 }
389
390 $wgOut->addHTML( "
391 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
392 <table border='0'><tr>
393 <td align='right'>$yn:</td>
394 <td align='left'>
395 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
396 </td>
397 <td align='left'>
398 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
399 </td>
400 </tr>
401 <tr>
402 <td align='right'>$yp:</td>
403 <td align='left'>
404 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
405 </td>
406 <td align='left'>
407 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
408 </td>
409 </tr>");
410
411 if ($wgUser->isAllowedToCreateAccount()) {
412 $encRetype = htmlspecialchars( $this->mRetype );
413 $encEmail = htmlspecialchars( $this->mEmail );
414 $wgOut->addHTML("<tr><td colspan='3'>&nbsp;</td></tr><tr>
415 <td align='right'>$ypa:</td>
416 <td align='left'>
417 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
418 size='20' />
419 </td><td>$nuo</td></tr>
420 <tr>
421 <td align='right'>$ye:</td>
422 <td align='left'>
423 <input tabindex='6' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
424 </td>");
425
426 if ($wgAllowRealName) {
427 $wgOut->addHTML("<td>&nbsp;</td>
428 </tr><tr>
429 <td align='right'>$yrn:</td>
430 <td align='left'>
431 <input tabindex='6' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
432 </td>");
433 }
434
435 $wgOut->addHTML("<td align='left'>
436 <input tabindex='7' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
437 $cambutton
438 </td></tr>");
439 }
440
441 $wgOut->addHTML("
442 <tr><td colspan='3'>&nbsp;</td></tr><tr>
443 <td colspan='3' align='left'>
444 <p>$efl<br />
445 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
446 </td></tr></table>
447 </form>\n" );
448 $wgOut->addHTML( $endText );
449 }
450
451 /* private */ function hasSessionCookie()
452 {
453 global $wgDisableCookieCheck;
454 return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
455 }
456
457 /* private */ function cookieRedirectCheck( $type )
458 {
459 global $wgOut, $wgLang;
460
461 $titleObj = Title::makeTitle( NS_SPECIAL, "Userlogin" );
462 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
463
464 return $wgOut->redirect( $check );
465 }
466
467 /* private */ function onCookieRedirectCheck( $type ) {
468 global $wgUser;
469
470 if ( !$this->hasSessionCookie() ) {
471 if ( $type == "new" ) {
472 return $this->mainLoginForm( wfMsg( "nocookiesnew" ) );
473 } else if ( $type == "login" ) {
474 return $this->mainLoginForm( wfMsg( "nocookieslogin" ) );
475 } else {
476 # shouldn't happen
477 return $this->mainLoginForm( wfMsg( "error" ) );
478 }
479 } else {
480 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
481 }
482 }
483
484 /* private */ function throttleHit( $limit ) {
485 global $wgOut;
486
487 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
488 }
489 }
490 ?>