Re-fix 5377 following privacy issues. Now holds back on the hooks until the scope...
[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[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, $mDomain;
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 $wgAuth;
39
40 $this->mType = $request->getText( 'type' );
41 $this->mName = $request->getText( 'wpName' );
42 $this->mPassword = $request->getText( 'wpPassword' );
43 $this->mRetype = $request->getText( 'wpRetype' );
44 $this->mDomain = $request->getText( 'wpDomain' );
45 $this->mReturnTo = $request->getVal( 'returnto' );
46 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
47 $this->mPosted = $request->wasPosted();
48 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
49 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
50 && $wgEnableEmail;
51 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
52 && $wgEnableEmail;
53 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
54 $this->mAction = $request->getVal( 'action' );
55 $this->mRemember = $request->getCheck( 'wpRemember' );
56
57 if( $wgEnableEmail ) {
58 $this->mEmail = $request->getText( 'wpEmail' );
59 } else {
60 $this->mEmail = '';
61 }
62 if( $wgAllowRealName ) {
63 $this->mRealName = $request->getText( 'wpRealName' );
64 } else {
65 $this->mRealName = '';
66 }
67
68 if( !$wgAuth->validDomain( $this->mDomain ) ) {
69 $this->mDomain = 'invaliddomain';
70 }
71 $wgAuth->setDomain( $this->mDomain );
72
73 # When switching accounts, it sucks to get automatically logged out
74 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
75 $this->mReturnTo = '';
76 }
77 }
78
79 function execute() {
80 if ( !is_null( $this->mCookieCheck ) ) {
81 $this->onCookieRedirectCheck( $this->mCookieCheck );
82 return;
83 } else if( $this->mPosted ) {
84 if( $this->mCreateaccount ) {
85 return $this->addNewAccount();
86 } else if ( $this->mCreateaccountMail ) {
87 return $this->addNewAccountMailPassword();
88 } else if ( $this->mMailmypassword ) {
89 return $this->mailPassword();
90 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
91 return $this->processLogin();
92 }
93 }
94 $this->mainLoginForm( '' );
95 }
96
97 /**
98 * @private
99 */
100 function addNewAccountMailPassword() {
101 global $wgOut;
102
103 if ('' == $this->mEmail) {
104 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
105 return;
106 }
107
108 $u = $this->addNewaccountInternal();
109
110 if ($u == NULL) {
111 return;
112 }
113
114 $u->saveSettings();
115 $result = $this->mailPasswordInternal($u);
116
117 wfRunHooks( 'AddNewAccount', array( $u ) );
118
119 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
120 $wgOut->setRobotpolicy( 'noindex,nofollow' );
121 $wgOut->setArticleRelated( false );
122
123 if( WikiError::isError( $result ) ) {
124 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
125 } else {
126 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
127 $wgOut->returnToMain( false );
128 }
129 $u = 0;
130 }
131
132
133 /**
134 * @private
135 */
136 function addNewAccount() {
137 global $wgUser, $wgEmailAuthentication;
138
139 # Create the account and abort if there's a problem doing so
140 $u = $this->addNewAccountInternal();
141 if( $u == NULL )
142 return;
143
144 # Save user settings and send out an email authentication message if needed
145 $u->saveSettings();
146 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) )
147 $u->sendConfirmationMail();
148
149 # If not logged in, assume the new account as the current one and set session cookies
150 # then show a "welcome" message or a "need cookies" message as needed
151 if( $wgUser->isAnon() ) {
152 $wgUser = $u;
153 $wgUser->setCookies();
154 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
155 if( $this->hasSessionCookie() ) {
156 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
157 } else {
158 return $this->cookieRedirectCheck( 'new' );
159 }
160 } else {
161 # Confirm that the account was created
162 global $wgOut;
163 $skin = $wgUser->getSkin();
164 $self = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
165 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
166 $wgOut->setArticleRelated( false );
167 $wgOut->setRobotPolicy( 'noindex,nofollow' );
168 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
169 $wgOut->returnToMain( $self->getPrefixedText() );
170 wfRunHooks( 'AddNewAccount', array( $u ) );
171 return true;
172 }
173 }
174
175 /**
176 * @private
177 */
178 function addNewAccountInternal() {
179 global $wgUser, $wgOut;
180 global $wgEnableSorbs, $wgProxyWhitelist;
181 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
182 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
183
184 // If the user passes an invalid domain, something is fishy
185 if( !$wgAuth->validDomain( $this->mDomain ) ) {
186 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
187 return false;
188 }
189
190 // If we are not allowing users to login locally, we should
191 // be checking to see if the user is actually able to
192 // authenticate to the authentication server before they
193 // create an account (otherwise, they can create a local account
194 // and login as any domain user). We only need to check this for
195 // domains that aren't local.
196 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
197 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
198 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
199 return false;
200 }
201 }
202
203 if ( wfReadOnly() ) {
204 $wgOut->readOnlyPage();
205 return false;
206 }
207
208 if (!$wgUser->isAllowedToCreateAccount()) {
209 $this->userNotPrivilegedMessage();
210 return false;
211 }
212
213 $ip = wfGetIP();
214 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
215 $wgUser->inSorbsBlacklist( $ip ) )
216 {
217 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
218 return;
219 }
220
221 $name = trim( $this->mName );
222 $u = User::newFromName( $name );
223 if ( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
224 $this->mainLoginForm( wfMsg( 'noname' ) );
225 return false;
226 }
227
228 if ( 0 != $u->idForName() ) {
229 $this->mainLoginForm( wfMsg( 'userexists' ) );
230 return false;
231 }
232
233 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
234 $this->mainLoginForm( wfMsg( 'badretype' ) );
235 return false;
236 }
237
238 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
239 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
240 return false;
241 }
242
243 if ( $wgAccountCreationThrottle ) {
244 $key = $wgDBname.':acctcreate:ip:'.$ip;
245 $value = $wgMemc->incr( $key );
246 if ( !$value ) {
247 $wgMemc->set( $key, 1, 86400 );
248 }
249 if ( $value > $wgAccountCreationThrottle ) {
250 $this->throttleHit( $wgAccountCreationThrottle );
251 return false;
252 }
253 }
254
255 $abortError = '';
256 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
257 // Hook point to add extra creation throttles and blocks
258 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
259 $this->mainLoginForm( $abortError );
260 return false;
261 }
262
263 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
264 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
265 return false;
266 }
267
268 # Update user count
269 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
270 $ssUpdate->doUpdate();
271
272 return $this->initUser( $u );
273 }
274
275 /**
276 * Actually add a user to the database.
277 * Give it a User object that has been initialised with a name.
278 *
279 * @param $u User object.
280 * @return User object.
281 * @private
282 */
283 function &initUser( &$u ) {
284 $u->addToDatabase();
285 $u->setPassword( $this->mPassword );
286 $u->setEmail( $this->mEmail );
287 $u->setRealName( $this->mRealName );
288 $u->setToken();
289
290 global $wgAuth;
291 $wgAuth->initUser( $u );
292
293 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
294
295 return $u;
296 }
297
298 /**
299 * @private
300 */
301 function processLogin() {
302 global $wgUser, $wgAuth, $wgReservedUsernames;
303
304 if ( '' == $this->mName ) {
305 $this->mainLoginForm( wfMsg( 'noname' ) );
306 return;
307 }
308 $u = User::newFromName( $this->mName );
309 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
310 $this->mainLoginForm( wfMsg( 'noname' ) );
311 return;
312 }
313 if ( 0 == $u->getID() ) {
314 global $wgAuth;
315 /**
316 * If the external authentication plugin allows it,
317 * automatically create a new account for users that
318 * are externally defined but have not yet logged in.
319 */
320 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
321 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
322 $u =& $this->initUser( $u );
323 } else {
324 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
325 return;
326 }
327 } else {
328 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
329 return;
330 }
331 } else {
332 $u->loadFromDatabase();
333 }
334
335 if (!$u->checkPassword( $this->mPassword )) {
336 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
337 return;
338 }
339
340 # We've verified now, update the real record
341 #
342 if ( $this->mRemember ) {
343 $r = 1;
344 } else {
345 $r = 0;
346 }
347 $u->setOption( 'rememberpassword', $r );
348
349 $wgAuth->updateUser( $u );
350
351 $wgUser = $u;
352 $wgUser->setCookies();
353
354 $wgUser->saveSettings();
355
356 if( $this->hasSessionCookie() ) {
357 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
358 } else {
359 return $this->cookieRedirectCheck( 'login' );
360 }
361 }
362
363 /**
364 * @private
365 */
366 function mailPassword() {
367 if ( '' == $this->mName ) {
368 $this->mainLoginForm( wfMsg( 'noname' ) );
369 return;
370 }
371 $u = User::newFromName( $this->mName );
372 if( is_null( $u ) ) {
373 $this->mainLoginForm( wfMsg( 'noname' ) );
374 return;
375 }
376 if ( 0 == $u->getID() ) {
377 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
378 return;
379 }
380
381 $u->loadFromDatabase();
382
383 $result = $this->mailPasswordInternal( $u );
384 if( WikiError::isError( $result ) ) {
385 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
386 } else {
387 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
388 }
389 }
390
391
392 /**
393 * @return mixed true on success, WikiError on failure
394 * @private
395 */
396 function mailPasswordInternal( $u ) {
397 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
398
399 if ( '' == $u->getEmail() ) {
400 return wfMsg( 'noemail', $u->getName() );
401 }
402
403 $np = $u->randomPassword();
404 $u->setNewpassword( $np );
405
406 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
407
408 $u->saveSettings();
409
410 $ip = wfGetIP();
411 if ( '' == $ip ) { $ip = '(Unknown)'; }
412
413 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
414
415 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
416 return $result;
417 }
418
419
420 /**
421 * @param string $msg Message that will be shown on success
422 * @param bool $auto Toggle auto-redirect to main page; default true
423 * @private
424 */
425 function successfulLogin( $msg, $auto = true ) {
426 global $wgUser;
427 global $wgOut;
428
429 # Run any hooks; ignore results
430
431 wfRunHooks('UserLoginComplete', array(&$wgUser));
432
433 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
434 $wgOut->setRobotpolicy( 'noindex,nofollow' );
435 $wgOut->setArticleRelated( false );
436 $wgOut->addWikiText( $msg );
437 if ( !empty( $this->mReturnTo ) ) {
438 $wgOut->returnToMain( $auto, $this->mReturnTo );
439 } else {
440 $wgOut->returnToMain( $auto );
441 }
442 }
443
444 /** */
445 function userNotPrivilegedMessage() {
446 global $wgOut;
447
448 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
449 $wgOut->setRobotpolicy( 'noindex,nofollow' );
450 $wgOut->setArticleRelated( false );
451
452 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
453
454 $wgOut->returnToMain( false );
455 }
456
457 /**
458 * @private
459 */
460 function mainLoginForm( $msg, $msgtype = 'error' ) {
461 global $wgUser, $wgOut;
462 global $wgAllowRealName, $wgEnableEmail;
463 global $wgCookiePrefix;
464 global $wgAuth;
465
466 if ( '' == $this->mName ) {
467 if ( $wgUser->isLoggedIn() ) {
468 $this->mName = $wgUser->getName();
469 } else {
470 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
471 }
472 }
473
474 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
475
476 require_once( 'SkinTemplate.php' );
477 require_once( 'templates/Userlogin.php' );
478
479 if ( $this->mType == 'signup' ) {
480 $template =& new UsercreateTemplate();
481 $q = 'action=submitlogin&type=signup';
482 $linkq = 'type=login';
483 $linkmsg = 'gotaccount';
484 } else {
485 $template =& new UserloginTemplate();
486 $q = 'action=submitlogin&type=login';
487 $linkq = 'type=signup';
488 $linkmsg = 'nologin';
489 }
490
491 if ( !empty( $this->mReturnTo ) ) {
492 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
493 $q .= $returnto;
494 $linkq .= $returnto;
495 }
496
497 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
498 $link .= wfMsgHtml( $linkmsg . 'link' );
499 $link .= '</a>';
500
501 # Don't show a "create account" link if the user can't
502 if( $this->showCreateOrLoginLink( $wgUser ) )
503 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
504 else
505 $template->set( 'link', '' );
506
507 $template->set( 'header', '' );
508 $template->set( 'name', $this->mName );
509 $template->set( 'password', $this->mPassword );
510 $template->set( 'retype', $this->mRetype );
511 $template->set( 'email', $this->mEmail );
512 $template->set( 'realname', $this->mRealName );
513 $template->set( 'domain', $this->mDomain );
514
515 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
516 $template->set( 'message', $msg );
517 $template->set( 'messagetype', $msgtype );
518 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
519 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
520 $template->set( 'userealname', $wgAllowRealName );
521 $template->set( 'useemail', $wgEnableEmail );
522 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
523
524 // Give authentication and captcha plugins a chance to modify the form
525 $wgAuth->modifyUITemplate( $template );
526 if ( $this->mType == 'signup' ) {
527 wfRunHooks( 'UserCreateForm', array( &$template ) );
528 } else {
529 wfRunHooks( 'UserLoginForm', array( &$template ) );
530 }
531
532 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
533 $wgOut->setRobotpolicy( 'noindex,nofollow' );
534 $wgOut->setArticleRelated( false );
535 $wgOut->addTemplate( $template );
536 }
537
538 /**
539 * @private
540 */
541 function showCreateOrLoginLink( &$user ) {
542 if( $this->mType == 'signup' ) {
543 return( true );
544 } elseif( $user->isAllowedToCreateAccount() ) {
545 return( true );
546 } else {
547 return( false );
548 }
549 }
550
551 /**
552 * @private
553 */
554 function hasSessionCookie() {
555 global $wgDisableCookieCheck;
556 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
557 }
558
559 /**
560 * @private
561 */
562 function cookieRedirectCheck( $type ) {
563 global $wgOut;
564
565 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
566 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
567
568 return $wgOut->redirect( $check );
569 }
570
571 /**
572 * @private
573 */
574 function onCookieRedirectCheck( $type ) {
575 global $wgUser;
576
577 if ( !$this->hasSessionCookie() ) {
578 if ( $type == 'new' ) {
579 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
580 } else if ( $type == 'login' ) {
581 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
582 } else {
583 # shouldn't happen
584 return $this->mainLoginForm( wfMsg( 'error' ) );
585 }
586 } else {
587 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
588 }
589 }
590
591 /**
592 * @private
593 */
594 function throttleHit( $limit ) {
595 global $wgOut;
596
597 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
598 }
599 }
600 ?>