f7b66802c9c4d282722545da6d78dea9e7abfcf5
[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 $wgMaxNameChars, $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgMinimalPasswordLength;
165 global $wgAuth;
166
167 // If the user passes an invalid domain, something is fishy
168 if( !$wgAuth->validDomain( $this->mDomain ) ) {
169 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
170 return false;
171 }
172
173 // If we are not allowing users to login locally, we should
174 // be checking to see if the user is actually able to
175 // authenticate to the authentication server before they
176 // create an account (otherwise, they can create a local account
177 // and login as any domain user). We only need to check this for
178 // domains that aren't local.
179 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
180 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
181 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
182 return false;
183 }
184 }
185
186
187
188 if (!$wgUser->isAllowedToCreateAccount()) {
189 $this->userNotPrivilegedMessage();
190 return false;
191 }
192
193 if ( $wgEnableSorbs && !in_array( $wgIP, $wgProxyWhitelist ) &&
194 $wgUser->inSorbsBlacklist( $wgIP ) )
195 {
196 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) );
197 return;
198 }
199
200
201 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
202 $this->mainLoginForm( wfMsg( 'badretype' ) );
203 return false;
204 }
205
206 $name = trim( $this->mName );
207 $u = User::newFromName( $name );
208 if ( is_null( $u ) ||
209 ( '' == $name ) ||
210 $wgUser->isIP( $name ) ||
211 (strpos( $name, '/' ) !== false) ||
212 (strlen( $name ) > $wgMaxNameChars) ||
213 ucFirst($name) != $u->getName() )
214 {
215 $this->mainLoginForm( wfMsg( 'noname' ) );
216 return false;
217 }
218 if ( wfReadOnly() ) {
219 $wgOut->readOnlyPage();
220 return false;
221 }
222
223 if ( 0 != $u->idForName() ) {
224 $this->mainLoginForm( wfMsg( 'userexists' ) );
225 return false;
226 }
227
228 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
229 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
230 return false;
231 }
232
233 if ( $wgAccountCreationThrottle ) {
234 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
235 $value = $wgMemc->incr( $key );
236 if ( !$value ) {
237 $wgMemc->set( $key, 1, 86400 );
238 }
239 if ( $value > $wgAccountCreationThrottle ) {
240 $this->throttleHit( $wgAccountCreationThrottle );
241 return false;
242 }
243 }
244
245 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
246 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
247 return false;
248 }
249
250 # Update user count
251 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
252 $ssUpdate->doUpdate();
253
254 return $this->initUser( $u );
255 }
256
257 /**
258 * Actually add a user to the database.
259 * Give it a User object that has been initialised with a name.
260 *
261 * @param User $u
262 * @return User
263 * @access private
264 */
265 function &initUser( &$u ) {
266 $u->addToDatabase();
267 $u->setPassword( $this->mPassword );
268 $u->setEmail( $this->mEmail );
269 $u->setRealName( $this->mRealName );
270 $u->setToken();
271
272 global $wgAuth;
273 $wgAuth->initUser( $u );
274
275 if ( $this->mRemember ) { $r = 1; }
276 else { $r = 0; }
277 $u->setOption( 'rememberpassword', $r );
278
279 return $u;
280 }
281
282 /**
283 * @access private
284 */
285 function processLogin() {
286 global $wgUser;
287 global $wgAuth;
288
289 if ( '' == $this->mName ) {
290 $this->mainLoginForm( wfMsg( 'noname' ) );
291 return;
292 }
293 $u = User::newFromName( $this->mName );
294 if( is_null( $u ) ) {
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( '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 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
353 global $wgCookiePath, $wgCookieDomain, $wgDBname;
354
355 if ( '' == $this->mName ) {
356 $this->mainLoginForm( wfMsg( 'noname' ) );
357 return;
358 }
359 $u = User::newFromName( $this->mName );
360 if( is_null( $u ) ) {
361 $this->mainLoginForm( wfMsg( 'noname' ) );
362 return;
363 }
364 if ( 0 == $u->getID() ) {
365 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
366 return;
367 }
368
369 $u->loadFromDatabase();
370
371 $result = $this->mailPasswordInternal( $u );
372 if( WikiError::isError( $result ) ) {
373 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
374 } else {
375 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
376 }
377 }
378
379
380 /**
381 * @return mixed true on success, WikiError on failure
382 * @access private
383 */
384 function mailPasswordInternal( $u ) {
385 global $wgPasswordSender, $wgDBname, $wgIP;
386 global $wgCookiePath, $wgCookieDomain;
387
388 if ( '' == $u->getEmail() ) {
389 return wfMsg( 'noemail', $u->getName() );
390 }
391
392 $np = $u->randomPassword();
393 $u->setNewpassword( $np );
394
395 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
396
397 $u->saveSettings();
398
399 $ip = $wgIP;
400 if ( '' == $ip ) { $ip = '(Unknown)'; }
401
402 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
403
404 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
405 return $result;
406 }
407
408
409 /**
410 * @param string $msg Message that will be shown on success.
411 * @access private
412 */
413 function successfulLogin( $msg ) {
414 global $wgUser;
415 global $wgOut;
416
417 # Run any hooks; ignore results
418
419 wfRunHooks('UserLoginComplete', array(&$wgUser));
420
421 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
422 $wgOut->setRobotpolicy( 'noindex,nofollow' );
423 $wgOut->setArticleRelated( false );
424 $wgOut->addWikiText( $msg );
425 $wgOut->returnToMain();
426 }
427
428 /** */
429 function userNotPrivilegedMessage() {
430 global $wgOut;
431
432 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
433 $wgOut->setRobotpolicy( 'noindex,nofollow' );
434 $wgOut->setArticleRelated( false );
435
436 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
437
438 $wgOut->returnToMain( false );
439 }
440
441 /**
442 * @access private
443 */
444 function mainLoginForm( $err ) {
445 global $wgUser, $wgOut, $wgLang;
446 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
447 global $wgAuth;
448
449 if ( '' == $this->mName ) {
450 if ( $wgUser->isLoggedIn() ) {
451 $this->mName = $wgUser->getName();
452 } else {
453 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
454 }
455 }
456
457 $q = 'action=submitlogin';
458 if ( !empty( $this->mReturnto ) ) {
459 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
460 }
461 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
462
463 require_once( 'templates/Userlogin.php' );
464 $template =& new UserloginTemplate();
465
466 $template->set( 'name', $this->mName );
467 $template->set( 'password', $this->mPassword );
468 $template->set( 'retype', $this->mRetype );
469 $template->set( 'email', $this->mEmail );
470 $template->set( 'realname', $this->mRealName );
471 $template->set( 'domain', $this->mDomain );
472
473 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
474 $template->set( 'error', $err );
475 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
476 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
477 $template->set( 'userealname', $wgAllowRealName );
478 $template->set( 'useemail', $wgEnableEmail );
479 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
480 $wgAuth->modifyUITemplate( $template );
481
482 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
483 $wgOut->setRobotpolicy( 'noindex,nofollow' );
484 $wgOut->setArticleRelated( false );
485 $wgOut->addTemplate( $template );
486 }
487
488 /**
489 * @access private
490 */
491 function hasSessionCookie() {
492 global $wgDisableCookieCheck;
493 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
494 }
495
496 /**
497 * @access private
498 */
499 function cookieRedirectCheck( $type ) {
500 global $wgOut, $wgLang;
501
502 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
503 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
504
505 return $wgOut->redirect( $check );
506 }
507
508 /**
509 * @access private
510 */
511 function onCookieRedirectCheck( $type ) {
512 global $wgUser;
513
514 if ( !$this->hasSessionCookie() ) {
515 if ( $type == 'new' ) {
516 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
517 } else if ( $type == 'login' ) {
518 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
519 } else {
520 # shouldn't happen
521 return $this->mainLoginForm( wfMsg( 'error' ) );
522 }
523 } else {
524 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
525 }
526 }
527
528 /**
529 * @access private
530 */
531 function throttleHit( $limit ) {
532 global $wgOut;
533
534 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
535 }
536 }
537 ?>