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