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