(bug 6493) Preserve language selections in the mode switch link on Special:Userlogin
[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, $mLanguage;
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 $this->mLanguage = $request->getText( 'uselang' );
57
58 if( $wgEnableEmail ) {
59 $this->mEmail = $request->getText( 'wpEmail' );
60 } else {
61 $this->mEmail = '';
62 }
63 if( $wgAllowRealName ) {
64 $this->mRealName = $request->getText( 'wpRealName' );
65 } else {
66 $this->mRealName = '';
67 }
68
69 if( !$wgAuth->validDomain( $this->mDomain ) ) {
70 $this->mDomain = 'invaliddomain';
71 }
72 $wgAuth->setDomain( $this->mDomain );
73
74 # When switching accounts, it sucks to get automatically logged out
75 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
76 $this->mReturnTo = '';
77 }
78 }
79
80 function execute() {
81 if ( !is_null( $this->mCookieCheck ) ) {
82 $this->onCookieRedirectCheck( $this->mCookieCheck );
83 return;
84 } else if( $this->mPosted ) {
85 if( $this->mCreateaccount ) {
86 return $this->addNewAccount();
87 } else if ( $this->mCreateaccountMail ) {
88 return $this->addNewAccountMailPassword();
89 } else if ( $this->mMailmypassword ) {
90 return $this->mailPassword();
91 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
92 return $this->processLogin();
93 }
94 }
95 $this->mainLoginForm( '' );
96 }
97
98 /**
99 * @private
100 */
101 function addNewAccountMailPassword() {
102 global $wgOut;
103
104 if ('' == $this->mEmail) {
105 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
106 return;
107 }
108
109 $u = $this->addNewaccountInternal();
110
111 if ($u == NULL) {
112 return;
113 }
114
115 $u->saveSettings();
116 $result = $this->mailPasswordInternal($u);
117
118 wfRunHooks( 'AddNewAccount', array( $u ) );
119
120 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
121 $wgOut->setRobotpolicy( 'noindex,nofollow' );
122 $wgOut->setArticleRelated( false );
123
124 if( WikiError::isError( $result ) ) {
125 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
126 } else {
127 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
128 $wgOut->returnToMain( false );
129 }
130 $u = 0;
131 }
132
133
134 /**
135 * @private
136 */
137 function addNewAccount() {
138 global $wgUser, $wgEmailAuthentication;
139
140 # Create the account and abort if there's a problem doing so
141 $u = $this->addNewAccountInternal();
142 if( $u == NULL )
143 return;
144
145 # If we showed up language selection links, and one was in use, be
146 # smart (and sensible) and save that language as the user's preference
147 global $wgLoginLanguageSelector;
148 if( $wgLoginLanguageSelector && $this->mLanguage )
149 $u->setOption( 'language', $this->mLanguage );
150
151 # Save user settings and send out an email authentication message if needed
152 $u->saveSettings();
153 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) )
154 $u->sendConfirmationMail();
155
156 # If not logged in, assume the new account as the current one and set session cookies
157 # then show a "welcome" message or a "need cookies" message as needed
158 if( $wgUser->isAnon() ) {
159 $wgUser = $u;
160 $wgUser->setCookies();
161 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
162 if( $this->hasSessionCookie() ) {
163 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
164 } else {
165 return $this->cookieRedirectCheck( 'new' );
166 }
167 } else {
168 # Confirm that the account was created
169 global $wgOut;
170 $skin = $wgUser->getSkin();
171 $self = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
172 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
173 $wgOut->setArticleRelated( false );
174 $wgOut->setRobotPolicy( 'noindex,nofollow' );
175 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
176 $wgOut->returnToMain( $self->getPrefixedText() );
177 wfRunHooks( 'AddNewAccount', array( $u ) );
178 return true;
179 }
180 }
181
182 /**
183 * @private
184 */
185 function addNewAccountInternal() {
186 global $wgUser, $wgOut;
187 global $wgEnableSorbs, $wgProxyWhitelist;
188 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
189 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
190
191 // If the user passes an invalid domain, something is fishy
192 if( !$wgAuth->validDomain( $this->mDomain ) ) {
193 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
194 return false;
195 }
196
197 // If we are not allowing users to login locally, we should
198 // be checking to see if the user is actually able to
199 // authenticate to the authentication server before they
200 // create an account (otherwise, they can create a local account
201 // and login as any domain user). We only need to check this for
202 // domains that aren't local.
203 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
204 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
205 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
206 return false;
207 }
208 }
209
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
212 return false;
213 }
214
215 if (!$wgUser->isAllowedToCreateAccount()) {
216 $this->userNotPrivilegedMessage();
217 return false;
218 }
219
220 $ip = wfGetIP();
221 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
222 $wgUser->inSorbsBlacklist( $ip ) )
223 {
224 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
225 return;
226 }
227
228 $name = trim( $this->mName );
229 $u = User::newFromName( $name );
230 if ( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
231 $this->mainLoginForm( wfMsg( 'noname' ) );
232 return false;
233 }
234
235 if ( 0 != $u->idForName() ) {
236 $this->mainLoginForm( wfMsg( 'userexists' ) );
237 return false;
238 }
239
240 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
241 $this->mainLoginForm( wfMsg( 'badretype' ) );
242 return false;
243 }
244
245 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
246 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
247 return false;
248 }
249
250 if ( $wgAccountCreationThrottle ) {
251 $key = $wgDBname.':acctcreate:ip:'.$ip;
252 $value = $wgMemc->incr( $key );
253 if ( !$value ) {
254 $wgMemc->set( $key, 1, 86400 );
255 }
256 if ( $value > $wgAccountCreationThrottle ) {
257 $this->throttleHit( $wgAccountCreationThrottle );
258 return false;
259 }
260 }
261
262 $abortError = '';
263 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
264 // Hook point to add extra creation throttles and blocks
265 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
266 $this->mainLoginForm( $abortError );
267 return false;
268 }
269
270 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
271 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
272 return false;
273 }
274
275 # Update user count
276 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
277 $ssUpdate->doUpdate();
278
279 return $this->initUser( $u );
280 }
281
282 /**
283 * Actually add a user to the database.
284 * Give it a User object that has been initialised with a name.
285 *
286 * @param $u User object.
287 * @return User object.
288 * @private
289 */
290 function &initUser( &$u ) {
291 $u->addToDatabase();
292 $u->setPassword( $this->mPassword );
293 $u->setEmail( $this->mEmail );
294 $u->setRealName( $this->mRealName );
295 $u->setToken();
296
297 global $wgAuth;
298 $wgAuth->initUser( $u );
299
300 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
301
302 return $u;
303 }
304
305 /**
306 * @private
307 */
308 function processLogin() {
309 global $wgUser, $wgAuth, $wgReservedUsernames;
310
311 if ( '' == $this->mName ) {
312 $this->mainLoginForm( wfMsg( 'noname' ) );
313 return;
314 }
315 $u = User::newFromName( $this->mName );
316 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
317 $this->mainLoginForm( wfMsg( 'noname' ) );
318 return;
319 }
320 if ( 0 == $u->getID() ) {
321 global $wgAuth;
322 /**
323 * If the external authentication plugin allows it,
324 * automatically create a new account for users that
325 * are externally defined but have not yet logged in.
326 */
327 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
328 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
329 $u =& $this->initUser( $u );
330 } else {
331 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
332 return;
333 }
334 } else {
335 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
336 return;
337 }
338 } else {
339 $u->loadFromDatabase();
340 }
341
342 if (!$u->checkPassword( $this->mPassword )) {
343 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
344 return;
345 }
346
347 # We've verified now, update the real record
348 #
349 if ( $this->mRemember ) {
350 $r = 1;
351 } else {
352 $r = 0;
353 }
354 $u->setOption( 'rememberpassword', $r );
355
356 $wgAuth->updateUser( $u );
357
358 $wgUser = $u;
359 $wgUser->setCookies();
360
361 $wgUser->saveSettings();
362
363 if( $this->hasSessionCookie() ) {
364 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
365 } else {
366 return $this->cookieRedirectCheck( 'login' );
367 }
368 }
369
370 /**
371 * @private
372 */
373 function mailPassword() {
374 global $wgUser, $wgOut;
375
376 # Check against the rate limiter
377 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
378 $wgOut->rateLimited();
379 return;
380 }
381
382 if ( '' == $this->mName ) {
383 $this->mainLoginForm( wfMsg( 'noname' ) );
384 return;
385 }
386 $u = User::newFromName( $this->mName );
387 if( is_null( $u ) ) {
388 $this->mainLoginForm( wfMsg( 'noname' ) );
389 return;
390 }
391 if ( 0 == $u->getID() ) {
392 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
393 return;
394 }
395
396 $u->loadFromDatabase();
397
398 $result = $this->mailPasswordInternal( $u );
399 if( WikiError::isError( $result ) ) {
400 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
401 } else {
402 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
403 }
404 }
405
406
407 /**
408 * @return mixed true on success, WikiError on failure
409 * @private
410 */
411 function mailPasswordInternal( $u ) {
412 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
413 global $wgServer, $wgScript;
414
415 if ( '' == $u->getEmail() ) {
416 return wfMsg( 'noemail', $u->getName() );
417 }
418
419 $np = $u->randomPassword();
420 $u->setNewpassword( $np );
421
422 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
423
424 $u->saveSettings();
425
426 $ip = wfGetIP();
427 if ( '' == $ip ) { $ip = '(Unknown)'; }
428
429 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
430
431 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
432 return $result;
433 }
434
435
436 /**
437 * @param string $msg Message that will be shown on success
438 * @param bool $auto Toggle auto-redirect to main page; default true
439 * @private
440 */
441 function successfulLogin( $msg, $auto = true ) {
442 global $wgUser;
443 global $wgOut;
444
445 # Run any hooks; ignore results
446
447 wfRunHooks('UserLoginComplete', array(&$wgUser));
448
449 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
450 $wgOut->setRobotpolicy( 'noindex,nofollow' );
451 $wgOut->setArticleRelated( false );
452 $wgOut->addWikiText( $msg );
453 if ( !empty( $this->mReturnTo ) ) {
454 $wgOut->returnToMain( $auto, $this->mReturnTo );
455 } else {
456 $wgOut->returnToMain( $auto );
457 }
458 }
459
460 /** */
461 function userNotPrivilegedMessage() {
462 global $wgOut;
463
464 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
465 $wgOut->setRobotpolicy( 'noindex,nofollow' );
466 $wgOut->setArticleRelated( false );
467
468 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
469
470 $wgOut->returnToMain( false );
471 }
472
473 /**
474 * @private
475 */
476 function mainLoginForm( $msg, $msgtype = 'error' ) {
477 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
478 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
479
480 if ( $this->mType == 'signup' && !$wgUser->isAllowedToCreateAccount() ) {
481 $this->userNotPrivilegedMessage();
482 return;
483 }
484
485 if ( '' == $this->mName ) {
486 if ( $wgUser->isLoggedIn() ) {
487 $this->mName = $wgUser->getName();
488 } else {
489 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
490 }
491 }
492
493 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
494
495 require_once( 'SkinTemplate.php' );
496 require_once( 'templates/Userlogin.php' );
497
498 if ( $this->mType == 'signup' ) {
499 $template =& new UsercreateTemplate();
500 $q = 'action=submitlogin&type=signup';
501 $linkq = 'type=login';
502 $linkmsg = 'gotaccount';
503 } else {
504 $template =& new UserloginTemplate();
505 $q = 'action=submitlogin&type=login';
506 $linkq = 'type=signup';
507 $linkmsg = 'nologin';
508 }
509
510 if ( !empty( $this->mReturnTo ) ) {
511 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
512 $q .= $returnto;
513 $linkq .= $returnto;
514 }
515
516 # Pass any language selection on to the mode switch link
517 if( $wgLoginLanguageSelector && $this->mLanguage )
518 $linkq .= '&uselang=' . $this->mLanguage;
519
520 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
521 $link .= wfMsgHtml( $linkmsg . 'link' );
522 $link .= '</a>';
523
524 # Don't show a "create account" link if the user can't
525 if( $this->showCreateOrLoginLink( $wgUser ) )
526 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
527 else
528 $template->set( 'link', '' );
529
530 $template->set( 'header', '' );
531 $template->set( 'name', $this->mName );
532 $template->set( 'password', $this->mPassword );
533 $template->set( 'retype', $this->mRetype );
534 $template->set( 'email', $this->mEmail );
535 $template->set( 'realname', $this->mRealName );
536 $template->set( 'domain', $this->mDomain );
537
538 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
539 $template->set( 'message', $msg );
540 $template->set( 'messagetype', $msgtype );
541 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
542 $template->set( 'userealname', $wgAllowRealName );
543 $template->set( 'useemail', $wgEnableEmail );
544 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
545
546 # Prepare language selection links as needed
547 if( $wgLoginLanguageSelector ) {
548 $template->set( 'languages', $this->makeLanguageSelector() );
549 if( $this->mLanguage )
550 $template->set( 'uselang', $this->mLanguage );
551 }
552
553 // Give authentication and captcha plugins a chance to modify the form
554 $wgAuth->modifyUITemplate( $template );
555 if ( $this->mType == 'signup' ) {
556 wfRunHooks( 'UserCreateForm', array( &$template ) );
557 } else {
558 wfRunHooks( 'UserLoginForm', array( &$template ) );
559 }
560
561 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
562 $wgOut->setRobotpolicy( 'noindex,nofollow' );
563 $wgOut->setArticleRelated( false );
564 $wgOut->addTemplate( $template );
565 }
566
567 /**
568 * @private
569 */
570 function showCreateOrLoginLink( &$user ) {
571 if( $this->mType == 'signup' ) {
572 return( true );
573 } elseif( $user->isAllowedToCreateAccount() ) {
574 return( true );
575 } else {
576 return( false );
577 }
578 }
579
580 /**
581 * @private
582 */
583 function hasSessionCookie() {
584 global $wgDisableCookieCheck;
585 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
586 }
587
588 /**
589 * @private
590 */
591 function cookieRedirectCheck( $type ) {
592 global $wgOut;
593
594 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
595 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
596
597 return $wgOut->redirect( $check );
598 }
599
600 /**
601 * @private
602 */
603 function onCookieRedirectCheck( $type ) {
604 global $wgUser;
605
606 if ( !$this->hasSessionCookie() ) {
607 if ( $type == 'new' ) {
608 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
609 } else if ( $type == 'login' ) {
610 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
611 } else {
612 # shouldn't happen
613 return $this->mainLoginForm( wfMsg( 'error' ) );
614 }
615 } else {
616 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
617 }
618 }
619
620 /**
621 * @private
622 */
623 function throttleHit( $limit ) {
624 global $wgOut;
625
626 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
627 }
628
629 /**
630 * Produce a bar of links which allow the user to select another language
631 * during login/registration but retain "returnto"
632 *
633 * @return string
634 */
635 function makeLanguageSelector() {
636 $msg = wfMsgForContent( 'loginlanguagelinks' );
637 if( $msg != '' && $msg != '&lt;loginlanguagelinks&gt;' ) {
638 $langs = explode( "\n", $msg );
639 $links = array();
640 foreach( $langs as $lang ) {
641 $lang = trim( $lang, '* ' );
642 $parts = explode( '|', $lang );
643 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
644 }
645 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
646 } else {
647 return '';
648 }
649 }
650
651 /**
652 * Create a language selector link for a particular language
653 * Links back to this page preserving type and returnto
654 *
655 * @param $text Link text
656 * @param $lang Language code
657 */
658 function makeLanguageSelectorLink( $text, $lang ) {
659 global $wgUser;
660 $self = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
661 $attr[] = 'uselang=' . $lang;
662 if( $this->mType == 'signup' )
663 $attr[] = 'type=signup';
664 if( $this->mReturnTo )
665 $attr[] = 'returnto=' . $this->mReturnTo;
666 $skin =& $wgUser->getSkin();
667 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
668 }
669
670 }
671 ?>