Force string to UTF-8 if we have mb stuff available.
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * constructor
9 */
10 function wfSpecialUserlogin() {
11 global $wgRequest;
12 if( session_id() == '' ) {
13 wfSetupSession();
14 }
15
16 $form = new LoginForm( $wgRequest );
17 $form->execute();
18 }
19
20 /**
21 * implements Special:Login
22 * @addtogroup SpecialPage
23 */
24 class LoginForm {
25
26 const SUCCESS = 0;
27 const NO_NAME = 1;
28 const ILLEGAL = 2;
29 const WRONG_PLUGIN_PASS = 3;
30 const NOT_EXISTS = 4;
31 const WRONG_PASS = 5;
32 const EMPTY_PASS = 6;
33 const RESET_PASS = 7;
34 const ABORTED = 8;
35
36 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
37 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
38 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
39
40 /**
41 * Constructor
42 * @param WebRequest $request A WebRequest object passed by reference
43 */
44 function LoginForm( &$request ) {
45 global $wgLang, $wgAllowRealName, $wgEnableEmail;
46 global $wgAuth;
47
48 $this->mType = $request->getText( 'type' );
49 $this->mName = $request->getText( 'wpName' );
50 $this->mPassword = $request->getText( 'wpPassword' );
51 $this->mRetype = $request->getText( 'wpRetype' );
52 $this->mDomain = $request->getText( 'wpDomain' );
53 $this->mReturnTo = $request->getVal( 'returnto' );
54 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
55 $this->mPosted = $request->wasPosted();
56 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
57 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
58 && $wgEnableEmail;
59 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
60 && $wgEnableEmail;
61 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
62 $this->mAction = $request->getVal( 'action' );
63 $this->mRemember = $request->getCheck( 'wpRemember' );
64 $this->mLanguage = $request->getText( 'uselang' );
65
66 if( $wgEnableEmail ) {
67 $this->mEmail = $request->getText( 'wpEmail' );
68 } else {
69 $this->mEmail = '';
70 }
71 if( $wgAllowRealName ) {
72 $this->mRealName = $request->getText( 'wpRealName' );
73 } else {
74 $this->mRealName = '';
75 }
76
77 if( !$wgAuth->validDomain( $this->mDomain ) ) {
78 $this->mDomain = 'invaliddomain';
79 }
80 $wgAuth->setDomain( $this->mDomain );
81
82 # When switching accounts, it sucks to get automatically logged out
83 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
84 $this->mReturnTo = '';
85 }
86 }
87
88 function execute() {
89 if ( !is_null( $this->mCookieCheck ) ) {
90 $this->onCookieRedirectCheck( $this->mCookieCheck );
91 return;
92 } else if( $this->mPosted ) {
93 if( $this->mCreateaccount ) {
94 return $this->addNewAccount();
95 } else if ( $this->mCreateaccountMail ) {
96 return $this->addNewAccountMailPassword();
97 } else if ( $this->mMailmypassword ) {
98 return $this->mailPassword();
99 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
100 return $this->processLogin();
101 }
102 }
103 $this->mainLoginForm( '' );
104 }
105
106 /**
107 * @private
108 */
109 function addNewAccountMailPassword() {
110 global $wgOut;
111
112 if ('' == $this->mEmail) {
113 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
114 return;
115 }
116
117 $u = $this->addNewaccountInternal();
118
119 if ($u == NULL) {
120 return;
121 }
122
123 // Wipe the initial password and mail a temporary one
124 $u->setPassword( null );
125 $u->saveSettings();
126 $result = $this->mailPasswordInternal( $u, false );
127
128 wfRunHooks( 'AddNewAccount', array( $u ) );
129
130 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
131 $wgOut->setRobotpolicy( 'noindex,nofollow' );
132 $wgOut->setArticleRelated( false );
133
134 if( WikiError::isError( $result ) ) {
135 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
136 } else {
137 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
138 $wgOut->returnToMain( false );
139 }
140 $u = 0;
141 }
142
143
144 /**
145 * @private
146 */
147 function addNewAccount() {
148 global $wgUser, $wgEmailAuthentication;
149
150 # Create the account and abort if there's a problem doing so
151 $u = $this->addNewAccountInternal();
152 if( $u == NULL )
153 return;
154
155 # If we showed up language selection links, and one was in use, be
156 # smart (and sensible) and save that language as the user's preference
157 global $wgLoginLanguageSelector;
158 if( $wgLoginLanguageSelector && $this->mLanguage )
159 $u->setOption( 'language', $this->mLanguage );
160
161 # Save user settings and send out an email authentication message if needed
162 $u->saveSettings();
163 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
164 global $wgOut;
165 $error = $u->sendConfirmationMail();
166 if( WikiError::isError( $error ) ) {
167 $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $error->getMessage() ) );
168 } else {
169 $wgOut->addWikiText( wfMsg( 'confirmemail_oncreate' ) );
170 }
171 }
172
173 # If not logged in, assume the new account as the current one and set session cookies
174 # then show a "welcome" message or a "need cookies" message as needed
175 if( $wgUser->isAnon() ) {
176 $wgUser = $u;
177 $wgUser->setCookies();
178 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
179 if( $this->hasSessionCookie() ) {
180 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
181 } else {
182 return $this->cookieRedirectCheck( 'new' );
183 }
184 } else {
185 # Confirm that the account was created
186 global $wgOut;
187 $self = SpecialPage::getTitleFor( 'Userlogin' );
188 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
189 $wgOut->setArticleRelated( false );
190 $wgOut->setRobotPolicy( 'noindex,nofollow' );
191 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
192 $wgOut->returnToMain( false, $self );
193 wfRunHooks( 'AddNewAccount', array( $u ) );
194 return true;
195 }
196 }
197
198 /**
199 * @private
200 */
201 function addNewAccountInternal() {
202 global $wgUser, $wgOut;
203 global $wgEnableSorbs, $wgProxyWhitelist;
204 global $wgMemc, $wgAccountCreationThrottle;
205 global $wgAuth, $wgMinimalPasswordLength;
206 global $wgEmailConfirmToEdit;
207
208 // If the user passes an invalid domain, something is fishy
209 if( !$wgAuth->validDomain( $this->mDomain ) ) {
210 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
211 return false;
212 }
213
214 // If we are not allowing users to login locally, we should
215 // be checking to see if the user is actually able to
216 // authenticate to the authentication server before they
217 // create an account (otherwise, they can create a local account
218 // and login as any domain user). We only need to check this for
219 // domains that aren't local.
220 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
221 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
222 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
223 return false;
224 }
225 }
226
227 if ( wfReadOnly() ) {
228 $wgOut->readOnlyPage();
229 return false;
230 }
231
232 # Check permissions
233 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
234 $this->userNotPrivilegedMessage();
235 return false;
236 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
237 $this->userBlockedMessage();
238 return false;
239 }
240
241 $ip = wfGetIP();
242 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
243 $wgUser->inSorbsBlacklist( $ip ) )
244 {
245 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
246 return;
247 }
248
249 # Now create a dummy user ($u) and check if it is valid
250 $name = trim( $this->mName );
251 $u = User::newFromName( $name, 'creatable' );
252 if ( is_null( $u ) ) {
253 $this->mainLoginForm( wfMsg( 'noname' ) );
254 return false;
255 }
256
257 if ( 0 != $u->idForName() ) {
258 $this->mainLoginForm( wfMsg( 'userexists' ) );
259 return false;
260 }
261
262 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
263 $this->mainLoginForm( wfMsg( 'badretype' ) );
264 return false;
265 }
266
267 if ( !$u->isValidPassword( $this->mPassword ) ) {
268 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
269 return false;
270 }
271
272 # if you need a confirmed email address to edit, then obviously you need an email address.
273 if ( $wgEmailConfirmToEdit && empty( $this->mEmail ) ) {
274 $this->mainLoginForm( wfMsg( 'noemailtitle' ) );
275 return false;
276 }
277
278 if( !empty( $this->mEmail ) && !User::isValidEmailAddr( $this->mEmail ) ) {
279 $this->mainLoginForm( wfMsg( 'invalidemailaddress' ) );
280 return false;
281 }
282
283 # Set some additional data so the AbortNewAccount hook can be
284 # used for more than just username validation
285 $u->setEmail( $this->mEmail );
286 $u->setRealName( $this->mRealName );
287
288 $abortError = '';
289 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
290 // Hook point to add extra creation throttles and blocks
291 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
292 $this->mainLoginForm( $abortError );
293 return false;
294 }
295
296 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
297 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
298 $value = $wgMemc->incr( $key );
299 if ( !$value ) {
300 $wgMemc->set( $key, 1, 86400 );
301 }
302 if ( $value > $wgAccountCreationThrottle ) {
303 $this->throttleHit( $wgAccountCreationThrottle );
304 return false;
305 }
306 }
307
308 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
309 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
310 return false;
311 }
312
313 return $this->initUser( $u, false );
314 }
315
316 /**
317 * Actually add a user to the database.
318 * Give it a User object that has been initialised with a name.
319 *
320 * @param $u User object.
321 * @param $autocreate boolean -- true if this is an autocreation via auth plugin
322 * @return User object.
323 * @private
324 */
325 function initUser( $u, $autocreate ) {
326 global $wgAuth;
327
328 $u->addToDatabase();
329
330 if ( $wgAuth->allowPasswordChange() ) {
331 $u->setPassword( $this->mPassword );
332 }
333
334 $u->setEmail( $this->mEmail );
335 $u->setRealName( $this->mRealName );
336 $u->setToken();
337
338 $wgAuth->initUser( $u, $autocreate );
339
340 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
341 $u->saveSettings();
342
343 # Update user count
344 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
345 $ssUpdate->doUpdate();
346
347 return $u;
348 }
349
350 /**
351 * Internally authenticate the login request.
352 *
353 * This may create a local account as a side effect if the
354 * authentication plugin allows transparent local account
355 * creation.
356 *
357 * @public
358 */
359 function authenticateUserData() {
360 global $wgUser, $wgAuth;
361 if ( '' == $this->mName ) {
362 return self::NO_NAME;
363 }
364 $u = User::newFromName( $this->mName );
365 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
366 return self::ILLEGAL;
367 }
368 if ( 0 == $u->getID() ) {
369 global $wgAuth;
370 /**
371 * If the external authentication plugin allows it,
372 * automatically create a new account for users that
373 * are externally defined but have not yet logged in.
374 */
375 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
376 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
377 $u = $this->initUser( $u, true );
378 } else {
379 return self::WRONG_PLUGIN_PASS;
380 }
381 } else {
382 return self::NOT_EXISTS;
383 }
384 } else {
385 $u->load();
386 }
387
388 // Give general extensions, such as a captcha, a chance to abort logins
389 $abort = self::ABORTED;
390 if( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort ) ) ) {
391 return $abort;
392 }
393
394 if (!$u->checkPassword( $this->mPassword )) {
395 if( $u->checkTemporaryPassword( $this->mPassword ) ) {
396 // The e-mailed temporary password should not be used
397 // for actual logins; that's a very sloppy habit,
398 // and insecure if an attacker has a few seconds to
399 // click "search" on someone's open mail reader.
400 //
401 // Allow it to be used only to reset the password
402 // a single time to a new value, which won't be in
403 // the user's e-mail archives.
404 //
405 // For backwards compatibility, we'll still recognize
406 // it at the login form to minimize surprises for
407 // people who have been logging in with a temporary
408 // password for some time.
409 //
410 // As a side-effect, we can authenticate the user's
411 // e-mail address if it's not already done, since
412 // the temporary password was sent via e-mail.
413 //
414 if( !$u->isEmailConfirmed() ) {
415 $u->confirmEmail();
416 }
417
418 // At this point we just return an appropriate code
419 // indicating that the UI should show a password
420 // reset form; bot interfaces etc will probably just
421 // fail cleanly here.
422 //
423 $retval = self::RESET_PASS;
424 } else {
425 $retval = '' == $this->mPassword ? self::EMPTY_PASS : self::WRONG_PASS;
426 }
427 } else {
428 $wgAuth->updateUser( $u );
429 $wgUser = $u;
430
431 $retval = self::SUCCESS;
432 }
433 wfRunHooks( 'LoginAuthenticateAudit', array( $u, $this->mPassword, $retval ) );
434 return $retval;
435 }
436
437 function processLogin() {
438 global $wgUser, $wgAuth;
439
440 switch ($this->authenticateUserData())
441 {
442 case self::SUCCESS:
443 # We've verified now, update the real record
444 if( (bool)$this->mRemember != (bool)$wgUser->getOption( 'rememberpassword' ) ) {
445 $wgUser->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
446 $wgUser->saveSettings();
447 } else {
448 $wgUser->invalidateCache();
449 }
450 $wgUser->setCookies();
451
452 if( $this->hasSessionCookie() ) {
453 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
454 } else {
455 return $this->cookieRedirectCheck( 'login' );
456 }
457 break;
458
459 case self::NO_NAME:
460 case self::ILLEGAL:
461 $this->mainLoginForm( wfMsg( 'noname' ) );
462 break;
463 case self::WRONG_PLUGIN_PASS:
464 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
465 break;
466 case self::NOT_EXISTS:
467 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
468 break;
469 case self::WRONG_PASS:
470 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
471 break;
472 case self::EMPTY_PASS:
473 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
474 break;
475 case self::RESET_PASS:
476 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
477 break;
478 default:
479 wfDebugDieBacktrace( "Unhandled case value" );
480 }
481 }
482
483 function resetLoginForm( $error ) {
484 global $wgOut;
485 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
486 $reset = new PasswordResetForm( $this->mName, $this->mPassword );
487 $reset->execute();
488 }
489
490 /**
491 * @private
492 */
493 function mailPassword() {
494 global $wgUser, $wgOut, $wgAuth;
495
496 if( !$wgAuth->allowPasswordChange() ) {
497 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
498 return;
499 }
500
501 # Check against blocked IPs
502 # fixme -- should we not?
503 if( $wgUser->isBlocked() ) {
504 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
505 return;
506 }
507
508 # Check against the rate limiter
509 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
510 $wgOut->rateLimited();
511 return;
512 }
513
514 if ( '' == $this->mName ) {
515 $this->mainLoginForm( wfMsg( 'noname' ) );
516 return;
517 }
518 $u = User::newFromName( $this->mName );
519 if( is_null( $u ) ) {
520 $this->mainLoginForm( wfMsg( 'noname' ) );
521 return;
522 }
523 if ( 0 == $u->getID() ) {
524 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
525 return;
526 }
527
528 # Check against password throttle
529 if ( $u->isPasswordReminderThrottled() ) {
530 global $wgPasswordReminderResendTime;
531 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
532 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
533 round( $wgPasswordReminderResendTime, 3 ) ) );
534 return;
535 }
536
537 $result = $this->mailPasswordInternal( $u, true );
538 if( WikiError::isError( $result ) ) {
539 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
540 } else {
541 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
542 }
543 }
544
545
546 /**
547 * @return mixed true on success, WikiError on failure
548 * @private
549 */
550 function mailPasswordInternal( $u, $throttle = true ) {
551 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
552 global $wgServer, $wgScript;
553
554 if ( '' == $u->getEmail() ) {
555 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
556 }
557
558 $np = $u->randomPassword();
559 $u->setNewpassword( $np, $throttle );
560
561 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
562
563 $u->saveSettings();
564
565 $ip = wfGetIP();
566 if ( '' == $ip ) { $ip = '(Unknown)'; }
567
568 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
569
570 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
571 return $result;
572 }
573
574
575 /**
576 * @param string $msg Message that will be shown on success
577 * @param bool $auto Toggle auto-redirect to main page; default true
578 * @private
579 */
580 function successfulLogin( $msg, $auto = true ) {
581 global $wgUser;
582 global $wgOut;
583
584 # Run any hooks; ignore results
585
586 wfRunHooks('UserLoginComplete', array(&$wgUser));
587
588 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
589 $wgOut->setRobotpolicy( 'noindex,nofollow' );
590 $wgOut->setArticleRelated( false );
591 $wgOut->addWikiText( $msg );
592 if ( !empty( $this->mReturnTo ) ) {
593 $wgOut->returnToMain( $auto, $this->mReturnTo );
594 } else {
595 $wgOut->returnToMain( $auto );
596 }
597 }
598
599 /** */
600 function userNotPrivilegedMessage() {
601 global $wgOut;
602
603 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
604 $wgOut->setRobotpolicy( 'noindex,nofollow' );
605 $wgOut->setArticleRelated( false );
606
607 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
608
609 $wgOut->returnToMain( false );
610 }
611
612 /** */
613 function userBlockedMessage() {
614 global $wgOut, $wgUser;
615
616 # Let's be nice about this, it's likely that this feature will be used
617 # for blocking large numbers of innocent people, e.g. range blocks on
618 # schools. Don't blame it on the user. There's a small chance that it
619 # really is the user's fault, i.e. the username is blocked and they
620 # haven't bothered to log out before trying to create an account to
621 # evade it, but we'll leave that to their guilty conscience to figure
622 # out.
623
624 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
625 $wgOut->setRobotpolicy( 'noindex,nofollow' );
626 $wgOut->setArticleRelated( false );
627
628 $ip = wfGetIP();
629 $blocker = User::whoIs( $wgUser->mBlock->mBy );
630 $block_reason = $wgUser->mBlock->mReason;
631
632 $wgOut->addWikiText( wfMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker ) );
633 $wgOut->returnToMain( false );
634 }
635
636 /**
637 * @private
638 */
639 function mainLoginForm( $msg, $msgtype = 'error' ) {
640 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
641 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
642 global $wgAuth, $wgEmailConfirmToEdit;
643
644 if ( $this->mType == 'signup' ) {
645 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
646 $this->userNotPrivilegedMessage();
647 return;
648 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
649 $this->userBlockedMessage();
650 return;
651 }
652 }
653
654 if ( '' == $this->mName ) {
655 if ( $wgUser->isLoggedIn() ) {
656 $this->mName = $wgUser->getName();
657 } else {
658 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
659 }
660 }
661
662 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
663
664 if ( $this->mType == 'signup' ) {
665 $template = new UsercreateTemplate();
666 $q = 'action=submitlogin&type=signup';
667 $linkq = 'type=login';
668 $linkmsg = 'gotaccount';
669 } else {
670 $template = new UserloginTemplate();
671 $q = 'action=submitlogin&type=login';
672 $linkq = 'type=signup';
673 $linkmsg = 'nologin';
674 }
675
676 if ( !empty( $this->mReturnTo ) ) {
677 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
678 $q .= $returnto;
679 $linkq .= $returnto;
680 }
681
682 # Pass any language selection on to the mode switch link
683 if( $wgLoginLanguageSelector && $this->mLanguage )
684 $linkq .= '&uselang=' . $this->mLanguage;
685
686 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
687 $link .= wfMsgHtml( $linkmsg . 'link' );
688 $link .= '</a>';
689
690 # Don't show a "create account" link if the user can't
691 if( $this->showCreateOrLoginLink( $wgUser ) )
692 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
693 else
694 $template->set( 'link', '' );
695
696 $template->set( 'header', '' );
697 $template->set( 'name', $this->mName );
698 $template->set( 'password', $this->mPassword );
699 $template->set( 'retype', $this->mRetype );
700 $template->set( 'email', $this->mEmail );
701 $template->set( 'realname', $this->mRealName );
702 $template->set( 'domain', $this->mDomain );
703
704 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
705 $template->set( 'message', $msg );
706 $template->set( 'messagetype', $msgtype );
707 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
708 $template->set( 'userealname', $wgAllowRealName );
709 $template->set( 'useemail', $wgEnableEmail );
710 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
711 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
712 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
713
714 # Prepare language selection links as needed
715 if( $wgLoginLanguageSelector ) {
716 $template->set( 'languages', $this->makeLanguageSelector() );
717 if( $this->mLanguage )
718 $template->set( 'uselang', $this->mLanguage );
719 }
720
721 // Give authentication and captcha plugins a chance to modify the form
722 $wgAuth->modifyUITemplate( $template );
723 if ( $this->mType == 'signup' ) {
724 wfRunHooks( 'UserCreateForm', array( &$template ) );
725 } else {
726 wfRunHooks( 'UserLoginForm', array( &$template ) );
727 }
728
729 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
730 $wgOut->setRobotpolicy( 'noindex,nofollow' );
731 $wgOut->setArticleRelated( false );
732 $wgOut->disallowUserJs(); // just in case...
733 $wgOut->addTemplate( $template );
734 }
735
736 /**
737 * @private
738 */
739 function showCreateOrLoginLink( &$user ) {
740 if( $this->mType == 'signup' ) {
741 return( true );
742 } elseif( $user->isAllowed( 'createaccount' ) ) {
743 return( true );
744 } else {
745 return( false );
746 }
747 }
748
749 /**
750 * Check if a session cookie is present.
751 *
752 * This will not pick up a cookie set during _this_ request, but is
753 * meant to ensure that the client is returning the cookie which was
754 * set on a previous pass through the system.
755 *
756 * @private
757 */
758 function hasSessionCookie() {
759 global $wgDisableCookieCheck, $wgRequest;
760 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
761 }
762
763 /**
764 * @private
765 */
766 function cookieRedirectCheck( $type ) {
767 global $wgOut;
768
769 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
770 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
771
772 return $wgOut->redirect( $check );
773 }
774
775 /**
776 * @private
777 */
778 function onCookieRedirectCheck( $type ) {
779 global $wgUser;
780
781 if ( !$this->hasSessionCookie() ) {
782 if ( $type == 'new' ) {
783 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
784 } else if ( $type == 'login' ) {
785 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
786 } else {
787 # shouldn't happen
788 return $this->mainLoginForm( wfMsg( 'error' ) );
789 }
790 } else {
791 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
792 }
793 }
794
795 /**
796 * @private
797 */
798 function throttleHit( $limit ) {
799 global $wgOut;
800
801 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
802 }
803
804 /**
805 * Produce a bar of links which allow the user to select another language
806 * during login/registration but retain "returnto"
807 *
808 * @return string
809 */
810 function makeLanguageSelector() {
811 $msg = wfMsgForContent( 'loginlanguagelinks' );
812 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
813 $langs = explode( "\n", $msg );
814 $links = array();
815 foreach( $langs as $lang ) {
816 $lang = trim( $lang, '* ' );
817 $parts = explode( '|', $lang );
818 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
819 }
820 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
821 } else {
822 return '';
823 }
824 }
825
826 /**
827 * Create a language selector link for a particular language
828 * Links back to this page preserving type and returnto
829 *
830 * @param $text Link text
831 * @param $lang Language code
832 */
833 function makeLanguageSelectorLink( $text, $lang ) {
834 global $wgUser;
835 $self = SpecialPage::getTitleFor( 'Userlogin' );
836 $attr[] = 'uselang=' . $lang;
837 if( $this->mType == 'signup' )
838 $attr[] = 'type=signup';
839 if( $this->mReturnTo )
840 $attr[] = 'returnto=' . $this->mReturnTo;
841 $skin = $wgUser->getSkin();
842 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
843 }
844 }
845