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