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