* (bug 2629) Automatically capitalize usernames again instead of
[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, $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->mName = $request->getText( 'wpName' );
41 $this->mPassword = $request->getText( 'wpPassword' );
42 $this->mRetype = $request->getText( 'wpRetype' );
43 $this->mDomain = $request->getText( 'wpDomain' );
44 $this->mReturnto = $request->getVal( 'returnto' );
45 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
46 $this->mPosted = $request->wasPosted();
47 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
48 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
49 && $wgEnableEmail;
50 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
51 && $wgEnableEmail;
52 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
53 $this->mAction = $request->getVal( 'action' );
54 $this->mRemember = $request->getCheck( 'wpRemember' );
55
56 if( $wgEnableEmail ) {
57 $this->mEmail = $request->getText( 'wpEmail' );
58 } else {
59 $this->mEmail = '';
60 }
61 if( $wgAllowRealName ) {
62 $this->mRealName = $request->getText( 'wpRealName' );
63 } else {
64 $this->mRealName = '';
65 }
66
67 if( !$wgAuth->validDomain( $this->mDomain ) ) {
68 $this->mDomain = 'invaliddomain';
69 }
70 $wgAuth->setDomain( $this->mDomain );
71
72 # When switching accounts, it sucks to get automatically logged out
73 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
74 $this->mReturnto = '';
75 }
76 }
77
78 function execute() {
79 if ( !is_null( $this->mCookieCheck ) ) {
80 $this->onCookieRedirectCheck( $this->mCookieCheck );
81 return;
82 } else if( $this->mPosted ) {
83 if( $this->mCreateaccount ) {
84 return $this->addNewAccount();
85 } else if ( $this->mCreateaccountMail ) {
86 return $this->addNewAccountMailPassword();
87 } else if ( $this->mMailmypassword ) {
88 return $this->mailPassword();
89 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
90 return $this->processLogin();
91 }
92 }
93 $this->mainLoginForm( '' );
94 }
95
96 /**
97 * @access private
98 */
99 function addNewAccountMailPassword() {
100 global $wgOut;
101
102 if ('' == $this->mEmail) {
103 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
104 return;
105 }
106
107 $u = $this->addNewaccountInternal();
108
109 if ($u == NULL) {
110 return;
111 }
112
113 $u->saveSettings();
114 $result = $this->mailPasswordInternal($u);
115
116 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
117 $wgOut->setRobotpolicy( 'noindex,nofollow' );
118 $wgOut->setArticleRelated( false );
119
120 if( WikiError::isError( $result ) ) {
121 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
122 } else {
123 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
124 $wgOut->returnToMain( false );
125 }
126 $u = 0;
127 }
128
129
130 /**
131 * @access private
132 */
133 function addNewAccount() {
134 global $wgUser, $wgOut, $wgEmailAuthentication;
135
136 $u = $this->addNewAccountInternal();
137
138 if ($u == NULL) {
139 return;
140 }
141
142 $wgUser = $u;
143 $wgUser->setCookies();
144
145 $wgUser->saveSettings();
146 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
147 $wgUser->sendConfirmationMail();
148 }
149
150 if( $this->hasSessionCookie() ) {
151 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
152 } else {
153 return $this->cookieRedirectCheck( 'new' );
154 }
155 }
156
157 /**
158 * @access private
159 */
160 function addNewAccountInternal() {
161 global $wgUser, $wgOut;
162 global $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgAuth;
165
166 // If the user passes an invalid domain, something is fishy
167 if( !$wgAuth->validDomain( $this->mDomain ) ) {
168 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
169 return false;
170 }
171
172 // If we are not allowing users to login locally, we should
173 // be checking to see if the user is actually able to
174 // authenticate to the authentication server before they
175 // create an account (otherwise, they can create a local account
176 // and login as any domain user). We only need to check this for
177 // domains that aren't local.
178 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
179 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
180 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
181 return false;
182 }
183 }
184
185 if (!$wgUser->isAllowedToCreateAccount()) {
186 $this->userNotPrivilegedMessage();
187 return false;
188 }
189
190 if ( $wgEnableSorbs && !in_array( $wgIP, $wgProxyWhitelist ) &&
191 $wgUser->inSorbsBlacklist( $wgIP ) )
192 {
193 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) );
194 return;
195 }
196
197
198 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
199 $this->mainLoginForm( wfMsg( 'badretype' ) );
200 return false;
201 }
202
203 $name = trim( $this->mName );
204 $u = User::newFromName( $name );
205 if ( is_null( $u ) ) {
206 $this->mainLoginForm( wfMsg( 'noname' ) );
207 return false;
208 }
209
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
212 return false;
213 }
214
215 if ( 0 != $u->idForName() ) {
216 $this->mainLoginForm( wfMsg( 'userexists' ) );
217 return false;
218 }
219
220 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
221 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
222 return false;
223 }
224
225 if ( $wgAccountCreationThrottle ) {
226 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
227 $value = $wgMemc->incr( $key );
228 if ( !$value ) {
229 $wgMemc->set( $key, 1, 86400 );
230 }
231 if ( $value > $wgAccountCreationThrottle ) {
232 $this->throttleHit( $wgAccountCreationThrottle );
233 return false;
234 }
235 }
236
237 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
238 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
239 return false;
240 }
241
242 # Update user count
243 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
244 $ssUpdate->doUpdate();
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;
279 global $wgAuth;
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 if (!$u->checkPassword( $this->mPassword )) {
313 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
314 return;
315 }
316
317 # We've verified now, update the real record
318 #
319 if ( $this->mRemember ) {
320 $r = 1;
321 } else {
322 $r = 0;
323 }
324 $u->setOption( 'rememberpassword', $r );
325
326 $wgAuth->updateUser( $u );
327
328 $wgUser = $u;
329 $wgUser->setCookies();
330
331 $wgUser->saveSettings();
332
333 if( $this->hasSessionCookie() ) {
334 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
335 } else {
336 return $this->cookieRedirectCheck( 'login' );
337 }
338 }
339
340 /**
341 * @access private
342 */
343 function mailPassword() {
344 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
345 global $wgCookiePath, $wgCookieDomain, $wgDBname;
346
347 if ( '' == $this->mName ) {
348 $this->mainLoginForm( wfMsg( 'noname' ) );
349 return;
350 }
351 $u = User::newFromName( $this->mName );
352 if( is_null( $u ) ) {
353 $this->mainLoginForm( wfMsg( 'noname' ) );
354 return;
355 }
356 if ( 0 == $u->getID() ) {
357 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
358 return;
359 }
360
361 $u->loadFromDatabase();
362
363 $result = $this->mailPasswordInternal( $u );
364 if( WikiError::isError( $result ) ) {
365 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
366 } else {
367 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
368 }
369 }
370
371
372 /**
373 * @return mixed true on success, WikiError on failure
374 * @access private
375 */
376 function mailPasswordInternal( $u ) {
377 global $wgPasswordSender, $wgDBname, $wgIP;
378 global $wgCookiePath, $wgCookieDomain;
379
380 if ( '' == $u->getEmail() ) {
381 return wfMsg( 'noemail', $u->getName() );
382 }
383
384 $np = $u->randomPassword();
385 $u->setNewpassword( $np );
386
387 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
388
389 $u->saveSettings();
390
391 $ip = $wgIP;
392 if ( '' == $ip ) { $ip = '(Unknown)'; }
393
394 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
395
396 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
397 return $result;
398 }
399
400
401 /**
402 * @param string $msg Message that will be shown on success.
403 * @access private
404 */
405 function successfulLogin( $msg ) {
406 global $wgUser;
407 global $wgOut;
408
409 # Run any hooks; ignore results
410
411 wfRunHooks('UserLoginComplete', array(&$wgUser));
412
413 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
414 $wgOut->setRobotpolicy( 'noindex,nofollow' );
415 $wgOut->setArticleRelated( false );
416 $wgOut->addWikiText( $msg );
417 $wgOut->returnToMain();
418 }
419
420 /** */
421 function userNotPrivilegedMessage() {
422 global $wgOut;
423
424 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
425 $wgOut->setRobotpolicy( 'noindex,nofollow' );
426 $wgOut->setArticleRelated( false );
427
428 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
429
430 $wgOut->returnToMain( false );
431 }
432
433 /**
434 * @access private
435 */
436 function mainLoginForm( $err ) {
437 global $wgUser, $wgOut, $wgLang;
438 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
439 global $wgAuth;
440
441 if ( '' == $this->mName ) {
442 if ( $wgUser->isLoggedIn() ) {
443 $this->mName = $wgUser->getName();
444 } else {
445 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
446 }
447 }
448
449 $q = 'action=submitlogin';
450 if ( !empty( $this->mReturnto ) ) {
451 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
452 }
453 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
454
455 require_once( 'templates/Userlogin.php' );
456 $template =& new UserloginTemplate();
457
458 $template->set( 'name', $this->mName );
459 $template->set( 'password', $this->mPassword );
460 $template->set( 'retype', $this->mRetype );
461 $template->set( 'email', $this->mEmail );
462 $template->set( 'realname', $this->mRealName );
463 $template->set( 'domain', $this->mDomain );
464
465 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
466 $template->set( 'error', $err );
467 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
468 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
469 $template->set( 'userealname', $wgAllowRealName );
470 $template->set( 'useemail', $wgEnableEmail );
471 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
472 $wgAuth->modifyUITemplate( $template );
473
474 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
475 $wgOut->setRobotpolicy( 'noindex,nofollow' );
476 $wgOut->setArticleRelated( false );
477 $wgOut->addTemplate( $template );
478 }
479
480 /**
481 * @access private
482 */
483 function hasSessionCookie() {
484 global $wgDisableCookieCheck;
485 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
486 }
487
488 /**
489 * @access private
490 */
491 function cookieRedirectCheck( $type ) {
492 global $wgOut, $wgLang;
493
494 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
495 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
496
497 return $wgOut->redirect( $check );
498 }
499
500 /**
501 * @access private
502 */
503 function onCookieRedirectCheck( $type ) {
504 global $wgUser;
505
506 if ( !$this->hasSessionCookie() ) {
507 if ( $type == 'new' ) {
508 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
509 } else if ( $type == 'login' ) {
510 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
511 } else {
512 # shouldn't happen
513 return $this->mainLoginForm( wfMsg( 'error' ) );
514 }
515 } else {
516 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
517 }
518 }
519
520 /**
521 * @access private
522 */
523 function throttleHit( $limit ) {
524 global $wgOut;
525
526 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
527 }
528 }
529 ?>