Fix #1585 : incorrect title on post-login redirection page.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * constructor
10 */
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
13 global $wgRequest;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
15 User::SetupSession();
16 }
17
18 $form = new LoginForm( $wgRequest );
19 $form->execute();
20 }
21
22 /**
23 *
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class LoginForm {
28 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail;
31
32 function LoginForm( &$request ) {
33 global $wgLang, $wgAllowRealName, $wgEnableEmail;
34 global $wgEmailAuthentication;
35
36 $this->mName = $request->getText( 'wpName' );
37 $this->mPassword = $request->getText( 'wpPassword' );
38 $this->mRetype = $request->getText( 'wpRetype' );
39 $this->mReturnto = $request->getVal( 'returnto' );
40 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
41 $this->mPosted = $request->wasPosted();
42 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
43 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
44 && $wgEnableEmail;
45 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
46 && $wgEnableEmail;
47 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
48 $this->mAction = $request->getVal( 'action' );
49 $this->mRemember = $request->getCheck( 'wpRemember' );
50
51 if( $wgEnableEmail ) {
52 $this->mEmail = $request->getText( 'wpEmail' );
53 } else {
54 $this->mEmail = '';
55 }
56 if( $wgAllowRealName ) {
57 $this->mRealName = $request->getText( 'wpRealName' );
58 } else {
59 $this->mRealName = '';
60 }
61
62 # When switching accounts, it sucks to get automatically logged out
63 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
64 $this->mReturnto = '';
65 }
66 }
67
68 function execute() {
69 if ( !is_null( $this->mCookieCheck ) ) {
70 $this->onCookieRedirectCheck( $this->mCookieCheck );
71 return;
72 } else if( $this->mPosted ) {
73 if( $this->mCreateaccount ) {
74 return $this->addNewAccount();
75 } else if ( $this->mCreateaccountMail ) {
76 return $this->addNewAccountMailPassword();
77 } else if ( $this->mMailmypassword ) {
78 return $this->mailPassword();
79 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
80 return $this->processLogin();
81 }
82 }
83 $this->mainLoginForm( '' );
84 }
85
86 /**
87 * @access private
88 */
89 function addNewAccountMailPassword() {
90 global $wgOut;
91 global $wgEmailAuthentication;
92
93 if ('' == $this->mEmail) {
94 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
95 return;
96 }
97
98 $u = $this->addNewaccountInternal();
99
100 if ($u == NULL) {
101 return;
102 }
103
104 $newadr = strtolower($this->mEmail);
105
106 # prepare for authentication and mail a temporary password to newadr
107 if ( !$u->isValidEmailAddr( $newadr ) ) {
108 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
109 }
110 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
111 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
112
113 if ($wgEmailAuthentication) {
114 $error = $this->mailPasswordInternal( $u, true, $dummy ); # mail a temporary password to the dirty address
115 }
116
117 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
118 $wgOut->setRobotpolicy( 'noindex,nofollow' );
119 $wgOut->setArticleRelated( false );
120
121 if ($wgEmailAuthentication) {
122 if ($error === '') {
123 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
124 } else {
125 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
126 }
127 # if user returns, that new email address gets authenticated in checkpassword()
128 }
129 # if ( $error === '' ) {
130 # $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
131 # $wgOut->returnToMain( false );
132 # } else {
133 # $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
134 # }
135 $u = 0;
136 }
137
138
139 /**
140 * @access private
141 */
142 function addNewAccount() {
143 global $wgUser, $wgOut;
144 global $wgEmailAuthentication;
145
146 $u = $this->addNewAccountInternal();
147
148 if ($u == NULL) {
149 return;
150 }
151
152 $newadr = strtolower($this->mEmail);
153 if ($newadr != '') { # prepare for authentication and mail a temporary password to newadr
154 if ( !$u->isValidEmailAddr( $newadr ) ) {
155 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
156 }
157 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
158 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
159
160 if ($wgEmailAuthentication) {
161 # mail a temporary password to the dirty address
162
163 $error = $this->mailPasswordInternal( $u, true, $dummy );
164 if ($error === '') {
165 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
166 } else {
167 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
168 }
169 # if user returns, that new email address gets authenticated in checkpassword()
170 }
171 }
172
173 $wgUser = $u;
174 $wgUser->setCookies();
175
176 $wgUser->saveSettings();
177
178 if( $this->hasSessionCookie() ) {
179 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
180 } else {
181 return $this->cookieRedirectCheck( 'new' );
182 }
183 }
184
185 /**
186 * @access private
187 */
188 function addNewAccountInternal() {
189 global $wgUser, $wgOut;
190 global $wgMaxNameChars, $wgUseLatin1;
191 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
192 global $wgMinimalPasswordLength;
193
194 if (!$wgUser->isAllowedToCreateAccount()) {
195 $this->userNotPrivilegedMessage();
196 return false;
197 }
198
199 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
200 $this->mainLoginForm( wfMsg( 'badretype' ) );
201 return false;
202 }
203
204 $name = trim( $this->mName );
205 $u = User::newFromName( $name );
206 if ( is_null( $u ) ||
207 ( '' == $name ) ||
208 $wgUser->isIP( $name ) ||
209 (strpos( $name, "/" ) !== false) ||
210 (strlen( $name ) > $wgMaxNameChars) ||
211 ($wgUseLatin1 && preg_match( "/[\x80-\xA0]/", $name )) ||
212 ucFirst($name) != $u->getName() )
213 {
214 $this->mainLoginForm( wfMsg( 'noname' ) );
215 return false;
216 }
217 if ( wfReadOnly() ) {
218 $wgOut->readOnlyPage();
219 return false;
220 }
221
222 if ( 0 != $u->idForName() ) {
223 $this->mainLoginForm( wfMsg( 'userexists' ) );
224 return false;
225 }
226
227 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
228 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
229 return false;
230 }
231
232 if ( $wgAccountCreationThrottle ) {
233 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
234 $value = $wgMemc->incr( $key );
235 if ( !$value ) {
236 $wgMemc->set( $key, 1, 86400 );
237 }
238 if ( $value > $wgAccountCreationThrottle ) {
239 $this->throttleHit( $wgAccountCreationThrottle );
240 return false;
241 }
242 }
243
244 return $this->initUser( $u );
245 }
246
247 /**
248 * Actually add a user to the database.
249 * Give it a User object that has been initialised with a name.
250 *
251 * @param User $u
252 * @return User
253 * @access private
254 */
255 function &initUser( &$u ) {
256 $u->addToDatabase();
257 $u->setPassword( $this->mPassword );
258 $u->setEmail( $this->mEmail );
259 $u->setRealName( $this->mRealName );
260
261 global $wgAuth;
262 $wgAuth->initUser( $u );
263
264 if ( $this->mRemember ) { $r = 1; }
265 else { $r = 0; }
266 $u->setOption( 'rememberpassword', $r );
267
268 return $u;
269 }
270
271 /**
272 * @access private
273 */
274 function processLogin() {
275 global $wgUser, $wgLang;
276 global $wgEmailAuthentication;
277
278 if ( '' == $this->mName ) {
279 $this->mainLoginForm( wfMsg( 'noname' ) );
280 return;
281 }
282 $u = User::newFromName( $this->mName );
283 if( is_null( $u ) ) {
284 $this->mainLoginForm( wfMsg( 'noname' ) );
285 return;
286 }
287 if ( 0 == $u->getID() ) {
288 global $wgAuth;
289 /**
290 * If the external authentication plugin allows it,
291 * automatically create a new account for users that
292 * are externally defined but have not yet logged in.
293 */
294 if( $wgAuth->autoCreate() &&
295 $wgAuth->userExists( $u->getName() ) &&
296 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
297 $u =& $this->initUser( $u );
298 } else {
299 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
300 return;
301 }
302 } else {
303 $u->loadFromDatabase();
304 }
305
306 # store temporarily the status before the password check is performed
307 $mailmsg = '';
308 $oldadr = strtolower($u->getEmail());
309 $newadr = strtolower($this->mEmail);
310 $alreadyauthenticated = (( $u->mEmailAuthenticationtimestamp != 0 ) || ($oldadr == '')) ;
311
312 # checkPassword sets EmailAuthenticationtimestamp, if the newPassword is used
313
314 if (!$u->checkPassword( $this->mPassword )) {
315 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
316 return;
317 }
318
319 # We've verified now, update the real record
320 #
321 if ( $this->mRemember ) {
322 $r = 1;
323 } else {
324 $r = 0;
325 }
326 $u->setOption( 'rememberpassword', $r );
327
328 /* check if user with correct password has entered a new email address */
329 if (($newadr <> '') && ($newadr <> $oldadr)) { # the user supplied a new email address on the login page
330
331 # prepare for authentication and mail a temporary password to newadr
332 if ( !$u->isValidEmailAddr( $newadr ) ) {
333 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
334 }
335 $u->mEmail = $newadr; # new behaviour: store this new emailaddr from login-page now into user database record ...
336 $u->mEmailAuthenticationtimestamp = 0; # ... but flag the address as "dirty" (unauthenticated)
337 $alreadyauthenticated = false;
338
339 if ($wgEmailAuthentication) {
340
341 # mail a temporary one-time password to the dirty address and return here to complete the user login
342 # if the user returns now or later using this temp. password, then the new email address $newadr
343 # - which is already stored in his user record - gets authenticated in checkpassword()
344
345 $error = $this->mailPasswordInternal( $u, false, $newpassword_temp);
346 $u->mNewpassword = $newpassword_temp;
347
348 # The temporary password is mailed. The user is logged-in as he entered his correct password
349 # This appears to be more intuitive than alternative 2.
350
351 if ($error === '') {
352 $mailmsg = '<br />' . wfMsg( 'passwordsentforemailauthentication', $u->getName() );
353 } else {
354 $mailmsg = '<br />' . wfMsg( 'mailerror', $error ) ;
355 }
356 }
357 }
358
359 $wgUser = $u;
360 $wgUser->setCookies();
361
362 # save all settings (incl. new email address and/or temporary password, if applicable)
363 $wgUser->saveSettings();
364
365 if ( !$wgEmailAuthentication || $alreadyauthenticated ) {
366 $authenticated = '';
367 $mailmsg = '';
368 } elseif ($u->mEmailAuthenticationtimestamp != 0) {
369 $authenticated = ' ' . wfMsg( 'emailauthenticated', $wgLang->timeanddate( $u->mEmailAuthenticationtimestamp, true ) );
370 } else {
371 $authenticated = ' ' . wfMsg( 'emailnotauthenticated' );
372 }
373
374 if( $this->hasSessionCookie() ) {
375 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) . $authenticated . $mailmsg );
376 } else {
377 return $this->cookieRedirectCheck( 'login' );
378 }
379 }
380
381 /**
382 * @access private
383 */
384 function mailPassword() {
385 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
386 global $wgCookiePath, $wgCookieDomain, $wgDBname;
387
388 if ( '' == $this->mName ) {
389 $this->mainLoginForm( wfMsg( 'noname' ) );
390 return;
391 }
392 $u = User::newFromName( $this->mName );
393 if( is_null( $u ) ) {
394 $this->mainLoginForm( wfMsg( 'noname' ) );
395 return;
396 }
397 if ( 0 == $u->getID() ) {
398 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
399 return;
400 }
401
402 $u->loadFromDatabase();
403
404 $error = $this->mailPasswordInternal( $u, true, $dummy );
405 if ($error === '') {
406 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
407 } else {
408 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
409 }
410 return;
411 }
412
413
414 /**
415 * @access private
416 */
417 function mailPasswordInternal( $u, $savesettings = true, &$newpassword_out ) {
418 global $wgPasswordSender, $wgDBname, $wgIP;
419 global $wgCookiePath, $wgCookieDomain;
420
421 if ( '' == $u->getEmail() ) {
422 return wfMsg( 'noemail', $u->getName() );
423 }
424
425 $np = $u->randomPassword();
426 $u->setNewpassword( $np );
427
428 # we want to store this new password together with other values in the calling function
429 $newpassword_out = $u->mNewpassword;
430
431 # WHY IS THIS HERE ? SHOULDN'T IT BE User::setcookie ???
432 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
433
434 if ($savesettings) {
435 $u->saveSettings();
436 }
437
438 $ip = $wgIP;
439 if ( '' == $ip ) { $ip = '(Unknown)'; }
440
441 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
442
443 require_once('UserMailer.php');
444 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindermailsubject' ), $m );
445
446 return htmlspecialchars( $error );
447 }
448
449
450 /**
451 * @access private
452 */
453 function successfulLogin( $msg ) {
454 global $wgUser;
455 global $wgOut;
456
457 # Run any hooks; ignore results
458
459 wfRunHooks('UserLoginComplete', $wgUser);
460
461 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
462 $wgOut->setRobotpolicy( 'noindex,nofollow' );
463 $wgOut->setArticleRelated( false );
464 $wgOut->addWikiText( $msg );
465 $wgOut->returnToMain();
466 }
467
468 function userNotPrivilegedMessage() {
469 global $wgOut, $wgUser, $wgLang;
470
471 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
472 $wgOut->setRobotpolicy( 'noindex,nofollow' );
473 $wgOut->setArticleRelated( false );
474
475 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
476
477 $wgOut->returnToMain( false );
478 }
479
480 /**
481 * @access private
482 */
483 function mainLoginForm( $err ) {
484 global $wgUser, $wgOut, $wgLang;
485 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
486 global $wgEmailAuthentication;
487
488 if ( '' == $this->mName ) {
489 if ( $wgUser->isLoggedIn() ) {
490 $this->mName = $wgUser->getName();
491 } else {
492 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
493 }
494 }
495
496 $q = 'action=submitlogin';
497 if ( !empty( $this->mReturnto ) ) {
498 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
499 }
500 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
501
502 require_once( 'templates/Userlogin.php' );
503 $template =& new UserloginTemplate();
504
505 $template->set( 'name', $this->mName );
506 $template->set( 'password', $this->mPassword );
507 $template->set( 'retype', $this->mRetype );
508 $template->set( 'email', $this->mEmail );
509 $template->set( 'realname', $this->mRealName );
510
511 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
512 $template->set( 'error', $err );
513 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
514 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
515 $template->set( 'userealname', $wgAllowRealName );
516 $template->set( 'useemail', $wgEnableEmail );
517 $template->set( 'useemailauthent', $wgEmailAuthentication );
518 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
519
520 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
521 $wgOut->setRobotpolicy( 'noindex,nofollow' );
522 $wgOut->setArticleRelated( false );
523 $wgOut->addTemplate( $template );
524 }
525
526 /**
527 * @access private
528 */
529 function hasSessionCookie() {
530 global $wgDisableCookieCheck;
531 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
532 }
533
534 /**
535 * @access private
536 */
537 function cookieRedirectCheck( $type ) {
538 global $wgOut, $wgLang;
539
540 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
541 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
542
543 return $wgOut->redirect( $check );
544 }
545
546 /**
547 * @access private
548 */
549 function onCookieRedirectCheck( $type ) {
550 global $wgUser;
551
552 if ( !$this->hasSessionCookie() ) {
553 if ( $type == 'new' ) {
554 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
555 } else if ( $type == 'login' ) {
556 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
557 } else {
558 # shouldn't happen
559 return $this->mainLoginForm( wfMsg( 'error' ) );
560 }
561 } else {
562 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
563 }
564 }
565
566 /**
567 * @access private
568 */
569 function throttleHit( $limit ) {
570 global $wgOut;
571
572 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
573 }
574 }
575 ?>