00a70d4cf06d0cfa796c043906b9cfc84c16ff7b
[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 ( ( 'submit' == $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;
191 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
192
193 if (!$wgUser->isAllowedToCreateAccount()) {
194 $this->userNotPrivilegedMessage();
195 return;
196 }
197
198 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
199 $this->mainLoginForm( wfMsg( 'badretype' ) );
200 return;
201 }
202
203 $name = trim( $this->mName );
204 $u = User::newFromName( $name );
205 if ( is_null( $u ) ||
206 ( '' == $name ) ||
207 $wgUser->isIP( $name ) ||
208 (strpos( $name, "/" ) !== false) ||
209 (strlen( $name ) > $wgMaxNameChars) ||
210 ucFirst($name) != $u->getName() )
211 {
212 $this->mainLoginForm( wfMsg( 'noname' ) );
213 return;
214 }
215 if ( wfReadOnly() ) {
216 $wgOut->readOnlyPage();
217 return;
218 }
219
220 if ( 0 != $u->idForName() ) {
221 $this->mainLoginForm( wfMsg( 'userexists' ) );
222 return;
223 }
224
225 if ( $wgAccountCreationThrottle ) {
226 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
227 $value = $wgMemc->incr( $key );
228 if ( !$value ) {
229 $wgMemc->set( $key, 1, 86400 );
230 }
231 if ( $value > $wgAccountCreationThrottle ) {
232 $this->throttleHit( $wgAccountCreationThrottle );
233 return;
234 }
235 }
236
237 return $this->initUser( $u );
238 }
239
240 /**
241 * Actually add a user to the database.
242 * Give it a User object that has been initialised with a name.
243 *
244 * @param User $u
245 * @return User
246 * @access private
247 */
248 function &initUser( &$u ) {
249 $u->addToDatabase();
250 $u->setPassword( $this->mPassword );
251 $u->setEmail( $this->mEmail );
252 $u->setRealName( $this->mRealName );
253
254 global $wgAuth;
255 $wgAuth->initUser( $u );
256
257 if ( $this->mRemember ) { $r = 1; }
258 else { $r = 0; }
259 $u->setOption( 'rememberpassword', $r );
260
261 return $u;
262 }
263
264 /**
265 * @access private
266 */
267 function processLogin() {
268 global $wgUser, $wgLang;
269 global $wgEmailAuthentication;
270
271 if ( '' == $this->mName ) {
272 $this->mainLoginForm( wfMsg( 'noname' ) );
273 return;
274 }
275 $u = User::newFromName( $this->mName );
276 if( is_null( $u ) ) {
277 $this->mainLoginForm( wfMsg( 'noname' ) );
278 return;
279 }
280 if ( 0 == $u->getID() ) {
281 global $wgAuth;
282 /**
283 * If the external authentication plugin allows it,
284 * automatically create a new account for users that
285 * are externally defined but have not yet logged in.
286 */
287 if( $wgAuth->autoCreate() &&
288 $wgAuth->userExists( $u->getName() ) &&
289 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
290 $u =& $this->initUser( $u );
291 } else {
292 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
293 return;
294 }
295 } else {
296 $u->loadFromDatabase();
297 }
298
299 # store temporarily the status before the password check is performed
300 $mailmsg = '';
301 $oldadr = strtolower($u->getEmail());
302 $newadr = strtolower($this->mEmail);
303 $alreadyauthenticated = (( $u->mEmailAuthenticationtimestamp != 0 ) || ($oldadr == '')) ;
304
305 # checkPassword sets EmailAuthenticationtimestamp, if the newPassword is used
306
307 if (!$u->checkPassword( $this->mPassword )) {
308 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
309 return;
310 }
311
312 # We've verified now, update the real record
313 #
314 if ( $this->mRemember ) {
315 $r = 1;
316 } else {
317 $r = 0;
318 }
319 $u->setOption( 'rememberpassword', $r );
320
321 /* check if user with correct password has entered a new email address */
322 if (($newadr <> '') && ($newadr <> $oldadr)) { # the user supplied a new email address on the login page
323
324 # prepare for authentication and mail a temporary password to newadr
325 if ( !$u->isValidEmailAddr( $newadr ) ) {
326 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
327 }
328 $u->mEmail = $newadr; # new behaviour: store this new emailaddr from login-page now into user database record ...
329 $u->mEmailAuthenticationtimestamp = 0; # ... but flag the address as "dirty" (unauthenticated)
330 $alreadyauthenticated = false;
331
332 if ($wgEmailAuthentication) {
333
334 # mail a temporary one-time password to the dirty address and return here to complete the user login
335 # if the user returns now or later using this temp. password, then the new email address $newadr
336 # - which is already stored in his user record - gets authenticated in checkpassword()
337
338 $error = $this->mailPasswordInternal( $u, false, $newpassword_temp);
339 $u->mNewpassword = $newpassword_temp;
340
341 # The temporary password is mailed. The user is logged-in as he entered his correct password
342 # This appears to be more intuitive than alternative 2.
343
344 if ($error === '') {
345 $mailmsg = '<br />' . wfMsg( 'passwordsentforemailauthentication', $u->getName() );
346 } else {
347 $mailmsg = '<br />' . wfMsg( 'mailerror', $error ) ;
348 }
349 }
350 }
351
352 $wgUser = $u;
353 $wgUser->setCookies();
354
355 # save all settings (incl. new email address and/or temporary password, if applicable)
356 $wgUser->saveSettings();
357
358 if ( !$wgEmailAuthentication || $alreadyauthenticated ) {
359 $authenticated = '';
360 $mailmsg = '';
361 } elseif ($u->mEmailAuthenticationtimestamp != 0) {
362 $authenticated = ' ' . wfMsg( 'emailauthenticated', $wgLang->timeanddate( $u->mEmailAuthenticationtimestamp, true ) );
363 } else {
364 $authenticated = ' ' . wfMsg( 'emailnotauthenticated' );
365 }
366
367 if( $this->hasSessionCookie() ) {
368 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) . $authenticated . $mailmsg );
369 } else {
370 return $this->cookieRedirectCheck( 'login' );
371 }
372 }
373
374 /**
375 * @access private
376 */
377 function mailPassword() {
378 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
379 global $wgCookiePath, $wgCookieDomain, $wgDBname;
380
381 if ( '' == $this->mName ) {
382 $this->mainLoginForm( wfMsg( 'noname' ) );
383 return;
384 }
385 $u = User::newFromName( $this->mName );
386 if( is_null( $u ) ) {
387 $this->mainLoginForm( wfMsg( 'noname' ) );
388 return;
389 }
390 if ( 0 == $u->getID() ) {
391 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
392 return;
393 }
394
395 $u->loadFromDatabase();
396
397 $error = $this->mailPasswordInternal( $u, true, $dummy );
398 if ($error === '') {
399 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
400 } else {
401 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
402 }
403 return;
404 }
405
406
407 /**
408 * @access private
409 */
410 function mailPasswordInternal( $u, $savesettings = true, &$newpassword_out ) {
411 global $wgPasswordSender, $wgDBname, $wgIP;
412 global $wgCookiePath, $wgCookieDomain;
413
414 if ( '' == $u->getEmail() ) {
415 return wfMsg( 'noemail', $u->getName() );
416 }
417
418 $np = $u->randomPassword();
419 $u->setNewpassword( $np );
420
421 # we want to store this new password together with other values in the calling function
422 $newpassword_out = $u->mNewpassword;
423
424 # WHY IS THIS HERE ? SHOULDN'T IT BE User::setcookie ???
425 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
426
427 if ($savesettings) {
428 $u->saveSettings();
429 }
430
431 $ip = $wgIP;
432 if ( '' == $ip ) { $ip = '(Unknown)'; }
433
434 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
435
436 require_once('UserMailer.php');
437 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindermailsubject' ), $m );
438
439 return htmlspecialchars( $error );
440 }
441
442
443 /**
444 * @access private
445 */
446 function successfulLogin( $msg ) {
447 global $wgUser;
448 global $wgOut;
449
450 # Run any hooks; ignore results
451
452 wfRunHooks('UserLoginComplete', $wgUser);
453
454 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
455 $wgOut->setRobotpolicy( 'noindex,nofollow' );
456 $wgOut->setArticleRelated( false );
457 $wgOut->addWikiText( $msg );
458 $wgOut->returnToMain();
459 }
460
461 function userNotPrivilegedMessage() {
462 global $wgOut, $wgUser, $wgLang;
463
464 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
465 $wgOut->setRobotpolicy( 'noindex,nofollow' );
466 $wgOut->setArticleRelated( false );
467
468 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
469
470 $wgOut->returnToMain( false );
471 }
472
473 /**
474 * @access private
475 */
476 function mainLoginForm( $err ) {
477 global $wgUser, $wgOut, $wgLang;
478 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
479 global $wgEmailAuthentication;
480
481 if ( '' == $this->mName ) {
482 if ( 0 != $wgUser->getID() ) {
483 $this->mName = $wgUser->getName();
484 } else {
485 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
486 }
487 }
488
489 $q = 'action=submit';
490 if ( !empty( $this->mReturnto ) ) {
491 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
492 }
493 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
494
495 require_once( 'templates/Userlogin.php' );
496 $template =& new UserloginTemplate();
497
498 $template->set( 'name', $this->mName );
499 $template->set( 'password', $this->mPassword );
500 $template->set( 'retype', $this->mRetype );
501 $template->set( 'email', $this->mEmail );
502 $template->set( 'realname', $this->mRealName );
503
504 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
505 $template->set( 'error', $err );
506 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
507 $template->set( 'createemail', $wgEnableEmail && ($wgUser->getID() != 0) );
508 $template->set( 'userealname', $wgAllowRealName );
509 $template->set( 'useemail', $wgEnableEmail );
510 $template->set( 'useemailauthent', $wgEmailAuthentication );
511 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
512
513 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
514 $wgOut->setRobotpolicy( 'noindex,nofollow' );
515 $wgOut->setArticleRelated( false );
516 $wgOut->addTemplate( $template );
517 }
518
519 /**
520 * @access private
521 */
522 function hasSessionCookie() {
523 global $wgDisableCookieCheck;
524 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
525 }
526
527 /**
528 * @access private
529 */
530 function cookieRedirectCheck( $type ) {
531 global $wgOut, $wgLang;
532
533 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
534 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
535
536 return $wgOut->redirect( $check );
537 }
538
539 /**
540 * @access private
541 */
542 function onCookieRedirectCheck( $type ) {
543 global $wgUser;
544
545 if ( !$this->hasSessionCookie() ) {
546 if ( $type == 'new' ) {
547 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
548 } else if ( $type == 'login' ) {
549 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
550 } else {
551 # shouldn't happen
552 return $this->mainLoginForm( wfMsg( 'error' ) );
553 }
554 } else {
555 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
556 }
557 }
558
559 /**
560 * @access private
561 */
562 function throttleHit( $limit ) {
563 global $wgOut;
564
565 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
566 }
567 }
568 ?>