* Refactored Article::getAutosummary(), so there's not a very simple static function...
[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($errors) {
669 global $wgOut;
670
671 $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
672 $wgOut->setRobotpolicy( 'noindex,nofollow' );
673 $wgOut->setArticleRelated( false );
674
675 $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
676 // Stuff that might want to be added at the end. For example, instructions if blocked.
677 $wgOut->addWikiMsg( 'cantcreateaccount-nonblock-text' );
678
679 $wgOut->returnToMain( false );
680 }
681
682 /** */
683 function userBlockedMessage() {
684 global $wgOut, $wgUser;
685
686 # Let's be nice about this, it's likely that this feature will be used
687 # for blocking large numbers of innocent people, e.g. range blocks on
688 # schools. Don't blame it on the user. There's a small chance that it
689 # really is the user's fault, i.e. the username is blocked and they
690 # haven't bothered to log out before trying to create an account to
691 # evade it, but we'll leave that to their guilty conscience to figure
692 # out.
693
694 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
695 $wgOut->setRobotpolicy( 'noindex,nofollow' );
696 $wgOut->setArticleRelated( false );
697
698 $ip = wfGetIP();
699 $blocker = User::whoIs( $wgUser->mBlock->mBy );
700 $block_reason = $wgUser->mBlock->mReason;
701
702 if ( strval( $block_reason ) === '' ) {
703 $block_reason = wfMsg( 'blockednoreason' );
704 }
705 $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
706 $wgOut->returnToMain( false );
707 }
708
709 /**
710 * @private
711 */
712 function mainLoginForm( $msg, $msgtype = 'error' ) {
713 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
714 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
715 global $wgAuth, $wgEmailConfirmToEdit;
716
717 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
718
719 if ( $this->mType == 'signup' ) {
720 if ( $wgUser->isBlockedFromCreateAccount() ) {
721 $this->userBlockedMessage();
722 return;
723 } elseif ( count( $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $wgUser, true ) )>0 ) {
724 $wgOut->showPermissionsErrorPage( $permErrors, 'createaccount' );
725 return;
726 }
727 }
728
729 if ( '' == $this->mName ) {
730 if ( $wgUser->isLoggedIn() ) {
731 $this->mName = $wgUser->getName();
732 } else {
733 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
734 }
735 }
736
737 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
738
739 if ( $this->mType == 'signup' ) {
740 $template = new UsercreateTemplate();
741 $q = 'action=submitlogin&type=signup';
742 $linkq = 'type=login';
743 $linkmsg = 'gotaccount';
744 } else {
745 $template = new UserloginTemplate();
746 $q = 'action=submitlogin&type=login';
747 $linkq = 'type=signup';
748 $linkmsg = 'nologin';
749 }
750
751 if ( !empty( $this->mReturnTo ) ) {
752 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
753 $q .= $returnto;
754 $linkq .= $returnto;
755 }
756
757 # Pass any language selection on to the mode switch link
758 if( $wgLoginLanguageSelector && $this->mLanguage )
759 $linkq .= '&uselang=' . $this->mLanguage;
760
761 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
762 $link .= wfMsgHtml( $linkmsg . 'link' ); # Calling either 'gotaccountlink' or 'nologinlink'
763 $link .= '</a>';
764
765 # Don't show a "create account" link if the user can't
766 if( $this->showCreateOrLoginLink( $wgUser ) )
767 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
768 else
769 $template->set( 'link', '' );
770
771 $template->set( 'header', '' );
772 $template->set( 'name', $this->mName );
773 $template->set( 'password', $this->mPassword );
774 $template->set( 'retype', $this->mRetype );
775 $template->set( 'email', $this->mEmail );
776 $template->set( 'realname', $this->mRealName );
777 $template->set( 'domain', $this->mDomain );
778
779 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
780 $template->set( 'message', $msg );
781 $template->set( 'messagetype', $msgtype );
782 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
783 $template->set( 'userealname', $wgAllowRealName );
784 $template->set( 'useemail', $wgEnableEmail );
785 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
786 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
787 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
788
789 # Prepare language selection links as needed
790 if( $wgLoginLanguageSelector ) {
791 $template->set( 'languages', $this->makeLanguageSelector() );
792 if( $this->mLanguage )
793 $template->set( 'uselang', $this->mLanguage );
794 }
795
796 // Give authentication and captcha plugins a chance to modify the form
797 $wgAuth->modifyUITemplate( $template );
798 if ( $this->mType == 'signup' ) {
799 wfRunHooks( 'UserCreateForm', array( &$template ) );
800 } else {
801 wfRunHooks( 'UserLoginForm', array( &$template ) );
802 }
803
804 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
805 $wgOut->setRobotpolicy( 'noindex,nofollow' );
806 $wgOut->setArticleRelated( false );
807 $wgOut->disallowUserJs(); // just in case...
808 $wgOut->addTemplate( $template );
809 }
810
811 /**
812 * @private
813 */
814 function showCreateOrLoginLink( &$user ) {
815 if( $this->mType == 'signup' ) {
816 return( true );
817 } elseif( $user->isAllowed( 'createaccount' ) ) {
818 return( true );
819 } else {
820 return( false );
821 }
822 }
823
824 /**
825 * Check if a session cookie is present.
826 *
827 * This will not pick up a cookie set during _this_ request, but is
828 * meant to ensure that the client is returning the cookie which was
829 * set on a previous pass through the system.
830 *
831 * @private
832 */
833 function hasSessionCookie() {
834 global $wgDisableCookieCheck, $wgRequest;
835 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
836 }
837
838 /**
839 * @private
840 */
841 function cookieRedirectCheck( $type ) {
842 global $wgOut;
843
844 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
845 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
846
847 return $wgOut->redirect( $check );
848 }
849
850 /**
851 * @private
852 */
853 function onCookieRedirectCheck( $type ) {
854 global $wgUser;
855
856 if ( !$this->hasSessionCookie() ) {
857 if ( $type == 'new' ) {
858 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
859 } else if ( $type == 'login' ) {
860 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
861 } else {
862 # shouldn't happen
863 return $this->mainLoginForm( wfMsg( 'error' ) );
864 }
865 } else {
866 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
867 }
868 }
869
870 /**
871 * @private
872 */
873 function throttleHit( $limit ) {
874 global $wgOut;
875
876 $wgOut->addWikiMsg( 'acct_creation_throttle_hit', $limit );
877 }
878
879 /**
880 * Produce a bar of links which allow the user to select another language
881 * during login/registration but retain "returnto"
882 *
883 * @return string
884 */
885 function makeLanguageSelector() {
886 $msg = wfMsgForContent( 'loginlanguagelinks' );
887 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
888 $langs = explode( "\n", $msg );
889 $links = array();
890 foreach( $langs as $lang ) {
891 $lang = trim( $lang, '* ' );
892 $parts = explode( '|', $lang );
893 if (count($parts) >= 2) {
894 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
895 }
896 }
897 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
898 } else {
899 return '';
900 }
901 }
902
903 /**
904 * Create a language selector link for a particular language
905 * Links back to this page preserving type and returnto
906 *
907 * @param $text Link text
908 * @param $lang Language code
909 */
910 function makeLanguageSelectorLink( $text, $lang ) {
911 global $wgUser;
912 $self = SpecialPage::getTitleFor( 'Userlogin' );
913 $attr[] = 'uselang=' . $lang;
914 if( $this->mType == 'signup' )
915 $attr[] = 'type=signup';
916 if( $this->mReturnTo )
917 $attr[] = 'returnto=' . $this->mReturnTo;
918 $skin = $wgUser->getSkin();
919 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
920 }
921 }