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