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