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