* (bug 3973) Use a separate message for the email content when an account is created...
[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, 'createaccount-title', 'createaccount-text' );
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, 'passwordremindertitle', 'passwordremindertext' );
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 * @param object user
548 * @param bool throttle
549 * @param string message name of email title
550 * @param string message name of email text
551 * @return mixed true on success, WikiError on failure
552 * @private
553 */
554 function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
555 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
556 global $wgServer, $wgScript;
557
558 if ( '' == $u->getEmail() ) {
559 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
560 }
561
562 $np = $u->randomPassword();
563 $u->setNewpassword( $np, $throttle );
564
565 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
566
567 $u->saveSettings();
568
569 $ip = wfGetIP();
570 if ( '' == $ip ) { $ip = '(Unknown)'; }
571
572 $m = wfMsg( $emailText, $ip, $u->getName(), $np, $wgServer . $wgScript );
573 $result = $u->sendMail( wfMsg( $emailTitle ), $m );
574
575 return $result;
576 }
577
578
579 /**
580 * @param string $msg Message that will be shown on success
581 * @param bool $auto Toggle auto-redirect to main page; default true
582 * @private
583 */
584 function successfulLogin( $msg, $auto = true ) {
585 global $wgUser;
586 global $wgOut;
587
588 # Run any hooks; ignore results
589
590 wfRunHooks('UserLoginComplete', array(&$wgUser));
591
592 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
593 $wgOut->setRobotpolicy( 'noindex,nofollow' );
594 $wgOut->setArticleRelated( false );
595 $wgOut->addWikiText( $msg );
596 if ( !empty( $this->mReturnTo ) ) {
597 $wgOut->returnToMain( $auto, $this->mReturnTo );
598 } else {
599 $wgOut->returnToMain( $auto );
600 }
601 }
602
603 /** */
604 function userNotPrivilegedMessage() {
605 global $wgOut;
606
607 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
608 $wgOut->setRobotpolicy( 'noindex,nofollow' );
609 $wgOut->setArticleRelated( false );
610
611 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
612
613 $wgOut->returnToMain( false );
614 }
615
616 /** */
617 function userBlockedMessage() {
618 global $wgOut, $wgUser;
619
620 # Let's be nice about this, it's likely that this feature will be used
621 # for blocking large numbers of innocent people, e.g. range blocks on
622 # schools. Don't blame it on the user. There's a small chance that it
623 # really is the user's fault, i.e. the username is blocked and they
624 # haven't bothered to log out before trying to create an account to
625 # evade it, but we'll leave that to their guilty conscience to figure
626 # out.
627
628 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
629 $wgOut->setRobotpolicy( 'noindex,nofollow' );
630 $wgOut->setArticleRelated( false );
631
632 $ip = wfGetIP();
633 $blocker = User::whoIs( $wgUser->mBlock->mBy );
634 $block_reason = $wgUser->mBlock->mReason;
635
636 $wgOut->addWikiText( wfMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker ) );
637 $wgOut->returnToMain( false );
638 }
639
640 /**
641 * @private
642 */
643 function mainLoginForm( $msg, $msgtype = 'error' ) {
644 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
645 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
646 global $wgAuth, $wgEmailConfirmToEdit;
647
648 if ( $this->mType == 'signup' ) {
649 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
650 $this->userNotPrivilegedMessage();
651 return;
652 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
653 $this->userBlockedMessage();
654 return;
655 }
656 }
657
658 if ( '' == $this->mName ) {
659 if ( $wgUser->isLoggedIn() ) {
660 $this->mName = $wgUser->getName();
661 } else {
662 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
663 }
664 }
665
666 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
667
668 if ( $this->mType == 'signup' ) {
669 $template = new UsercreateTemplate();
670 $q = 'action=submitlogin&type=signup';
671 $linkq = 'type=login';
672 $linkmsg = 'gotaccount';
673 } else {
674 $template = new UserloginTemplate();
675 $q = 'action=submitlogin&type=login';
676 $linkq = 'type=signup';
677 $linkmsg = 'nologin';
678 }
679
680 if ( !empty( $this->mReturnTo ) ) {
681 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
682 $q .= $returnto;
683 $linkq .= $returnto;
684 }
685
686 # Pass any language selection on to the mode switch link
687 if( $wgLoginLanguageSelector && $this->mLanguage )
688 $linkq .= '&uselang=' . $this->mLanguage;
689
690 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
691 $link .= wfMsgHtml( $linkmsg . 'link' );
692 $link .= '</a>';
693
694 # Don't show a "create account" link if the user can't
695 if( $this->showCreateOrLoginLink( $wgUser ) )
696 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
697 else
698 $template->set( 'link', '' );
699
700 $template->set( 'header', '' );
701 $template->set( 'name', $this->mName );
702 $template->set( 'password', $this->mPassword );
703 $template->set( 'retype', $this->mRetype );
704 $template->set( 'email', $this->mEmail );
705 $template->set( 'realname', $this->mRealName );
706 $template->set( 'domain', $this->mDomain );
707
708 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
709 $template->set( 'message', $msg );
710 $template->set( 'messagetype', $msgtype );
711 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
712 $template->set( 'userealname', $wgAllowRealName );
713 $template->set( 'useemail', $wgEnableEmail );
714 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
715 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
716 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
717
718 # Prepare language selection links as needed
719 if( $wgLoginLanguageSelector ) {
720 $template->set( 'languages', $this->makeLanguageSelector() );
721 if( $this->mLanguage )
722 $template->set( 'uselang', $this->mLanguage );
723 }
724
725 // Give authentication and captcha plugins a chance to modify the form
726 $wgAuth->modifyUITemplate( $template );
727 if ( $this->mType == 'signup' ) {
728 wfRunHooks( 'UserCreateForm', array( &$template ) );
729 } else {
730 wfRunHooks( 'UserLoginForm', array( &$template ) );
731 }
732
733 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
734 $wgOut->setRobotpolicy( 'noindex,nofollow' );
735 $wgOut->setArticleRelated( false );
736 $wgOut->disallowUserJs(); // just in case...
737 $wgOut->addTemplate( $template );
738 }
739
740 /**
741 * @private
742 */
743 function showCreateOrLoginLink( &$user ) {
744 if( $this->mType == 'signup' ) {
745 return( true );
746 } elseif( $user->isAllowed( 'createaccount' ) ) {
747 return( true );
748 } else {
749 return( false );
750 }
751 }
752
753 /**
754 * Check if a session cookie is present.
755 *
756 * This will not pick up a cookie set during _this_ request, but is
757 * meant to ensure that the client is returning the cookie which was
758 * set on a previous pass through the system.
759 *
760 * @private
761 */
762 function hasSessionCookie() {
763 global $wgDisableCookieCheck, $wgRequest;
764 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
765 }
766
767 /**
768 * @private
769 */
770 function cookieRedirectCheck( $type ) {
771 global $wgOut;
772
773 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
774 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
775
776 return $wgOut->redirect( $check );
777 }
778
779 /**
780 * @private
781 */
782 function onCookieRedirectCheck( $type ) {
783 global $wgUser;
784
785 if ( !$this->hasSessionCookie() ) {
786 if ( $type == 'new' ) {
787 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
788 } else if ( $type == 'login' ) {
789 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
790 } else {
791 # shouldn't happen
792 return $this->mainLoginForm( wfMsg( 'error' ) );
793 }
794 } else {
795 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
796 }
797 }
798
799 /**
800 * @private
801 */
802 function throttleHit( $limit ) {
803 global $wgOut;
804
805 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
806 }
807
808 /**
809 * Produce a bar of links which allow the user to select another language
810 * during login/registration but retain "returnto"
811 *
812 * @return string
813 */
814 function makeLanguageSelector() {
815 $msg = wfMsgForContent( 'loginlanguagelinks' );
816 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
817 $langs = explode( "\n", $msg );
818 $links = array();
819 foreach( $langs as $lang ) {
820 $lang = trim( $lang, '* ' );
821 $parts = explode( '|', $lang );
822 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
823 }
824 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
825 } else {
826 return '';
827 }
828 }
829
830 /**
831 * Create a language selector link for a particular language
832 * Links back to this page preserving type and returnto
833 *
834 * @param $text Link text
835 * @param $lang Language code
836 */
837 function makeLanguageSelectorLink( $text, $lang ) {
838 global $wgUser;
839 $self = SpecialPage::getTitleFor( 'Userlogin' );
840 $attr[] = 'uselang=' . $lang;
841 if( $this->mType == 'signup' )
842 $attr[] = 'type=signup';
843 if( $this->mReturnTo )
844 $attr[] = 'returnto=' . $this->mReturnTo;
845 $skin = $wgUser->getSkin();
846 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
847 }
848 }
849