Add $wgReservedUsernames configuration directive to block account creation/use
[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 * @access 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 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
118 $wgOut->setRobotpolicy( 'noindex,nofollow' );
119 $wgOut->setArticleRelated( false );
120
121 if( WikiError::isError( $result ) ) {
122 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
123 } else {
124 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
125 $wgOut->returnToMain( false );
126 }
127 $u = 0;
128 }
129
130
131 /**
132 * @access private
133 */
134 function addNewAccount() {
135 global $wgUser, $wgEmailAuthentication;
136
137 $u = $this->addNewAccountInternal();
138
139 if ($u == NULL) {
140 return;
141 }
142
143 $wgUser = $u;
144 $wgUser->setCookies();
145
146 $wgUser->saveSettings();
147 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
148 $wgUser->sendConfirmationMail();
149 }
150
151 wfRunHooks( 'AddNewAccount' );
152
153 if( $this->hasSessionCookie() ) {
154 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
155 } else {
156 return $this->cookieRedirectCheck( 'new' );
157 }
158 }
159
160 /**
161 * @access private
162 */
163 function addNewAccountInternal() {
164 global $wgUser, $wgOut;
165 global $wgEnableSorbs, $wgProxyWhitelist;
166 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
167 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
168
169 // If the user passes an invalid domain, something is fishy
170 if( !$wgAuth->validDomain( $this->mDomain ) ) {
171 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
172 return false;
173 }
174
175 // If we are not allowing users to login locally, we should
176 // be checking to see if the user is actually able to
177 // authenticate to the authentication server before they
178 // create an account (otherwise, they can create a local account
179 // and login as any domain user). We only need to check this for
180 // domains that aren't local.
181 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
182 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
183 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
184 return false;
185 }
186 }
187
188 if ( wfReadOnly() ) {
189 $wgOut->readOnlyPage();
190 return false;
191 }
192
193 if (!$wgUser->isAllowedToCreateAccount()) {
194 $this->userNotPrivilegedMessage();
195 return false;
196 }
197
198 $ip = wfGetIP();
199 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
200 $wgUser->inSorbsBlacklist( $ip ) )
201 {
202 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
203 return;
204 }
205
206 $name = trim( $this->mName );
207 $u = User::newFromName( $name );
208 if ( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
209 $this->mainLoginForm( wfMsg( 'noname' ) );
210 return false;
211 }
212
213 if ( 0 != $u->idForName() ) {
214 $this->mainLoginForm( wfMsg( 'userexists' ) );
215 return false;
216 }
217
218 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
219 $this->mainLoginForm( wfMsg( 'badretype' ) );
220 return false;
221 }
222
223 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
224 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
225 return false;
226 }
227
228 if ( $wgAccountCreationThrottle ) {
229 $key = $wgDBname.':acctcreate:ip:'.$ip;
230 $value = $wgMemc->incr( $key );
231 if ( !$value ) {
232 $wgMemc->set( $key, 1, 86400 );
233 }
234 if ( $value > $wgAccountCreationThrottle ) {
235 $this->throttleHit( $wgAccountCreationThrottle );
236 return false;
237 }
238 }
239
240 $abortError = '';
241 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
242 // Hook point to add extra creation throttles and blocks
243 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
244 $this->mainLoginForm( $abortError );
245 return false;
246 }
247
248 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
249 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
250 return false;
251 }
252
253 # Update user count
254 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
255 $ssUpdate->doUpdate();
256
257 return $this->initUser( $u );
258 }
259
260 /**
261 * Actually add a user to the database.
262 * Give it a User object that has been initialised with a name.
263 *
264 * @param User $u
265 * @return User
266 * @access private
267 */
268 function &initUser( &$u ) {
269 $u->addToDatabase();
270 $u->setPassword( $this->mPassword );
271 $u->setEmail( $this->mEmail );
272 $u->setRealName( $this->mRealName );
273 $u->setToken();
274
275 global $wgAuth;
276 $wgAuth->initUser( $u );
277
278 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
279
280 return $u;
281 }
282
283 /**
284 * @access private
285 */
286 function processLogin() {
287 global $wgUser, $wgAuth, $wgReservedUsernames;
288
289 if ( '' == $this->mName ) {
290 $this->mainLoginForm( wfMsg( 'noname' ) );
291 return;
292 }
293 $u = User::newFromName( $this->mName );
294 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
295 $this->mainLoginForm( wfMsg( 'noname' ) );
296 return;
297 }
298 if ( 0 == $u->getID() ) {
299 global $wgAuth;
300 /**
301 * If the external authentication plugin allows it,
302 * automatically create a new account for users that
303 * are externally defined but have not yet logged in.
304 */
305 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
306 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
307 $u =& $this->initUser( $u );
308 } else {
309 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
310 return;
311 }
312 } else {
313 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
314 return;
315 }
316 } else {
317 $u->loadFromDatabase();
318 }
319
320 if (!$u->checkPassword( $this->mPassword )) {
321 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : '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 $wgAuth->updateUser( $u );
335
336 $wgUser = $u;
337 $wgUser->setCookies();
338
339 $wgUser->saveSettings();
340
341 if( $this->hasSessionCookie() ) {
342 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
343 } else {
344 return $this->cookieRedirectCheck( 'login' );
345 }
346 }
347
348 /**
349 * @access private
350 */
351 function mailPassword() {
352 if ( '' == $this->mName ) {
353 $this->mainLoginForm( wfMsg( 'noname' ) );
354 return;
355 }
356 $u = User::newFromName( $this->mName );
357 if( is_null( $u ) ) {
358 $this->mainLoginForm( wfMsg( 'noname' ) );
359 return;
360 }
361 if ( 0 == $u->getID() ) {
362 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
363 return;
364 }
365
366 $u->loadFromDatabase();
367
368 $result = $this->mailPasswordInternal( $u );
369 if( WikiError::isError( $result ) ) {
370 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
371 } else {
372 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
373 }
374 }
375
376
377 /**
378 * @return mixed true on success, WikiError on failure
379 * @access private
380 */
381 function mailPasswordInternal( $u ) {
382 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
383
384 if ( '' == $u->getEmail() ) {
385 return wfMsg( 'noemail', $u->getName() );
386 }
387
388 $np = $u->randomPassword();
389 $u->setNewpassword( $np );
390
391 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
392
393 $u->saveSettings();
394
395 $ip = wfGetIP();
396 if ( '' == $ip ) { $ip = '(Unknown)'; }
397
398 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
399
400 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
401 return $result;
402 }
403
404
405 /**
406 * @param string $msg Message that will be shown on success
407 * @param bool $auto Toggle auto-redirect to main page; default true
408 * @access private
409 */
410 function successfulLogin( $msg, $auto = true ) {
411 global $wgUser;
412 global $wgOut;
413
414 # Run any hooks; ignore results
415
416 wfRunHooks('UserLoginComplete', array(&$wgUser));
417
418 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
419 $wgOut->setRobotpolicy( 'noindex,nofollow' );
420 $wgOut->setArticleRelated( false );
421 $wgOut->addWikiText( $msg );
422 $wgOut->returnToMain( $auto );
423 }
424
425 /** */
426 function userNotPrivilegedMessage() {
427 global $wgOut;
428
429 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
430 $wgOut->setRobotpolicy( 'noindex,nofollow' );
431 $wgOut->setArticleRelated( false );
432
433 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
434
435 $wgOut->returnToMain( false );
436 }
437
438 /**
439 * @access private
440 */
441 function mainLoginForm( $msg, $msgtype = 'error' ) {
442 global $wgUser, $wgOut;
443 global $wgAllowRealName, $wgEnableEmail;
444 global $wgCookiePrefix;
445 global $wgAuth;
446
447 if ( '' == $this->mName ) {
448 if ( $wgUser->isLoggedIn() ) {
449 $this->mName = $wgUser->getName();
450 } else {
451 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
452 }
453 }
454
455 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
456
457 require_once( 'SkinTemplate.php' );
458 require_once( 'templates/Userlogin.php' );
459
460 if ( $this->mType == 'signup' ) {
461 $template =& new UsercreateTemplate();
462 $q = 'action=submitlogin&type=signup';
463 $linkq = 'type=login';
464 $linkmsg = 'gotaccount';
465 } else {
466 $template =& new UserloginTemplate();
467 $q = 'action=submitlogin&type=login';
468 $linkq = 'type=signup';
469 $linkmsg = 'nologin';
470 }
471
472 if ( !empty( $this->mReturnto ) ) {
473 $returnto = '&returnto=' . wfUrlencode( $this->mReturnto );
474 $q .= $returnto;
475 $linkq .= $returnto;
476 }
477
478 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
479 $link .= wfMsgHtml( $linkmsg . 'link' );
480 $link .= '</a>';
481
482 # Don't show a "create account" link if the user can't
483 if( $this->showCreateOrLoginLink( $wgUser ) )
484 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
485 else
486 $template->set( 'link', '' );
487
488 $template->set( 'header', '' );
489 $template->set( 'name', $this->mName );
490 $template->set( 'password', $this->mPassword );
491 $template->set( 'retype', $this->mRetype );
492 $template->set( 'email', $this->mEmail );
493 $template->set( 'realname', $this->mRealName );
494 $template->set( 'domain', $this->mDomain );
495
496 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
497 $template->set( 'message', $msg );
498 $template->set( 'messagetype', $msgtype );
499 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
500 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
501 $template->set( 'userealname', $wgAllowRealName );
502 $template->set( 'useemail', $wgEnableEmail );
503 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
504
505 // Give authentication and captcha plugins a chance to modify the form
506 $wgAuth->modifyUITemplate( $template );
507 if ( $this->mType == 'signup' ) {
508 wfRunHooks( 'UserCreateForm', array( &$template ) );
509 } else {
510 wfRunHooks( 'UserLoginForm', array( &$template ) );
511 }
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 showCreateOrLoginLink( &$user ) {
523 if( $this->mType == 'signup' ) {
524 return( true );
525 } elseif( $user->isAllowedToCreateAccount() ) {
526 return( true );
527 } else {
528 return( false );
529 }
530 }
531
532 /**
533 * @access private
534 */
535 function hasSessionCookie() {
536 global $wgDisableCookieCheck;
537 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
538 }
539
540 /**
541 * @access private
542 */
543 function cookieRedirectCheck( $type ) {
544 global $wgOut;
545
546 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
547 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
548
549 return $wgOut->redirect( $check );
550 }
551
552 /**
553 * @access private
554 */
555 function onCookieRedirectCheck( $type ) {
556 global $wgUser;
557
558 if ( !$this->hasSessionCookie() ) {
559 if ( $type == 'new' ) {
560 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
561 } else if ( $type == 'login' ) {
562 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
563 } else {
564 # shouldn't happen
565 return $this->mainLoginForm( wfMsg( 'error' ) );
566 }
567 } else {
568 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
569 }
570 }
571
572 /**
573 * @access private
574 */
575 function throttleHit( $limit ) {
576 global $wgOut;
577
578 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
579 }
580 }
581 ?>