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