Introduce 'FetchChangesList' hook; see docs/hooks.txt for more information
[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 global $wgUser, $wgOut;
368
369 # Check against the rate limiter
370 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
371 $wgOut->rateLimited();
372 return;
373 }
374
375 if ( '' == $this->mName ) {
376 $this->mainLoginForm( wfMsg( 'noname' ) );
377 return;
378 }
379 $u = User::newFromName( $this->mName );
380 if( is_null( $u ) ) {
381 $this->mainLoginForm( wfMsg( 'noname' ) );
382 return;
383 }
384 if ( 0 == $u->getID() ) {
385 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
386 return;
387 }
388
389 $u->loadFromDatabase();
390
391 $result = $this->mailPasswordInternal( $u );
392 if( WikiError::isError( $result ) ) {
393 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
394 } else {
395 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
396 }
397 }
398
399
400 /**
401 * @return mixed true on success, WikiError on failure
402 * @private
403 */
404 function mailPasswordInternal( $u ) {
405 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
406 global $wgServer, $wgScript;
407
408 if ( '' == $u->getEmail() ) {
409 return wfMsg( 'noemail', $u->getName() );
410 }
411
412 $np = $u->randomPassword();
413 $u->setNewpassword( $np );
414
415 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
416
417 $u->saveSettings();
418
419 $ip = wfGetIP();
420 if ( '' == $ip ) { $ip = '(Unknown)'; }
421
422 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
423
424 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
425 return $result;
426 }
427
428
429 /**
430 * @param string $msg Message that will be shown on success
431 * @param bool $auto Toggle auto-redirect to main page; default true
432 * @private
433 */
434 function successfulLogin( $msg, $auto = true ) {
435 global $wgUser;
436 global $wgOut;
437
438 # Run any hooks; ignore results
439
440 wfRunHooks('UserLoginComplete', array(&$wgUser));
441
442 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
443 $wgOut->setRobotpolicy( 'noindex,nofollow' );
444 $wgOut->setArticleRelated( false );
445 $wgOut->addWikiText( $msg );
446 if ( !empty( $this->mReturnTo ) ) {
447 $wgOut->returnToMain( $auto, $this->mReturnTo );
448 } else {
449 $wgOut->returnToMain( $auto );
450 }
451 }
452
453 /** */
454 function userNotPrivilegedMessage() {
455 global $wgOut;
456
457 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
458 $wgOut->setRobotpolicy( 'noindex,nofollow' );
459 $wgOut->setArticleRelated( false );
460
461 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
462
463 $wgOut->returnToMain( false );
464 }
465
466 /**
467 * @private
468 */
469 function mainLoginForm( $msg, $msgtype = 'error' ) {
470 global $wgUser, $wgOut;
471 global $wgAllowRealName, $wgEnableEmail;
472 global $wgCookiePrefix;
473 global $wgAuth;
474
475 if ( $this->mType == 'signup' && !$wgUser->isAllowedToCreateAccount() ) {
476 $this->userNotPrivilegedMessage();
477 return;
478 }
479
480 if ( '' == $this->mName ) {
481 if ( $wgUser->isLoggedIn() ) {
482 $this->mName = $wgUser->getName();
483 } else {
484 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
485 }
486 }
487
488 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
489
490 require_once( 'SkinTemplate.php' );
491 require_once( 'templates/Userlogin.php' );
492
493 if ( $this->mType == 'signup' ) {
494 $template =& new UsercreateTemplate();
495 $q = 'action=submitlogin&type=signup';
496 $linkq = 'type=login';
497 $linkmsg = 'gotaccount';
498 } else {
499 $template =& new UserloginTemplate();
500 $q = 'action=submitlogin&type=login';
501 $linkq = 'type=signup';
502 $linkmsg = 'nologin';
503 }
504
505 if ( !empty( $this->mReturnTo ) ) {
506 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
507 $q .= $returnto;
508 $linkq .= $returnto;
509 }
510
511 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
512 $link .= wfMsgHtml( $linkmsg . 'link' );
513 $link .= '</a>';
514
515 # Don't show a "create account" link if the user can't
516 if( $this->showCreateOrLoginLink( $wgUser ) )
517 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
518 else
519 $template->set( 'link', '' );
520
521 $template->set( 'header', '' );
522 $template->set( 'name', $this->mName );
523 $template->set( 'password', $this->mPassword );
524 $template->set( 'retype', $this->mRetype );
525 $template->set( 'email', $this->mEmail );
526 $template->set( 'realname', $this->mRealName );
527 $template->set( 'domain', $this->mDomain );
528
529 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
530 $template->set( 'message', $msg );
531 $template->set( 'messagetype', $msgtype );
532 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
533 $template->set( 'userealname', $wgAllowRealName );
534 $template->set( 'useemail', $wgEnableEmail );
535 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
536
537 // Give authentication and captcha plugins a chance to modify the form
538 $wgAuth->modifyUITemplate( $template );
539 if ( $this->mType == 'signup' ) {
540 wfRunHooks( 'UserCreateForm', array( &$template ) );
541 } else {
542 wfRunHooks( 'UserLoginForm', array( &$template ) );
543 }
544
545 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
546 $wgOut->setRobotpolicy( 'noindex,nofollow' );
547 $wgOut->setArticleRelated( false );
548 $wgOut->addTemplate( $template );
549 }
550
551 /**
552 * @private
553 */
554 function showCreateOrLoginLink( &$user ) {
555 if( $this->mType == 'signup' ) {
556 return( true );
557 } elseif( $user->isAllowedToCreateAccount() ) {
558 return( true );
559 } else {
560 return( false );
561 }
562 }
563
564 /**
565 * @private
566 */
567 function hasSessionCookie() {
568 global $wgDisableCookieCheck;
569 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
570 }
571
572 /**
573 * @private
574 */
575 function cookieRedirectCheck( $type ) {
576 global $wgOut;
577
578 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
579 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
580
581 return $wgOut->redirect( $check );
582 }
583
584 /**
585 * @private
586 */
587 function onCookieRedirectCheck( $type ) {
588 global $wgUser;
589
590 if ( !$this->hasSessionCookie() ) {
591 if ( $type == 'new' ) {
592 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
593 } else if ( $type == 'login' ) {
594 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
595 } else {
596 # shouldn't happen
597 return $this->mainLoginForm( wfMsg( 'error' ) );
598 }
599 } else {
600 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
601 }
602 }
603
604 /**
605 * @private
606 */
607 function throttleHit( $limit ) {
608 global $wgOut;
609
610 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
611 }
612 }
613 ?>