*cough* Er, make it work? :) [dropped the action=submit] part
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * constructor
10 */
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
13 global $wgRequest;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) {
15 User::SetupSession();
16 }
17
18 $form = new LoginForm( $wgRequest );
19 $form->execute();
20 }
21
22 /**
23 *
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class LoginForm {
28 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
31
32 /**
33 * Constructor
34 * @param webrequest $request A webrequest object passed by reference
35 */
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
38 global $wgAuth;
39
40 $this->mType = $request->getText( 'type' );
41 $this->mName = $request->getText( 'wpName' );
42 $this->mPassword = $request->getText( 'wpPassword' );
43 $this->mRetype = $request->getText( 'wpRetype' );
44 $this->mDomain = $request->getText( 'wpDomain' );
45 $this->mReturnTo = $request->getVal( 'returnto' );
46 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
47 $this->mPosted = $request->wasPosted();
48 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
49 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
50 && $wgEnableEmail;
51 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
52 && $wgEnableEmail;
53 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
54 $this->mAction = $request->getVal( 'action' );
55 $this->mRemember = $request->getCheck( 'wpRemember' );
56 $this->mLanguage = $request->getText( 'uselang' );
57
58 if( $wgEnableEmail ) {
59 $this->mEmail = $request->getText( 'wpEmail' );
60 } else {
61 $this->mEmail = '';
62 }
63 if( $wgAllowRealName ) {
64 $this->mRealName = $request->getText( 'wpRealName' );
65 } else {
66 $this->mRealName = '';
67 }
68
69 if( !$wgAuth->validDomain( $this->mDomain ) ) {
70 $this->mDomain = 'invaliddomain';
71 }
72 $wgAuth->setDomain( $this->mDomain );
73
74 # When switching accounts, it sucks to get automatically logged out
75 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
76 $this->mReturnTo = '';
77 }
78 }
79
80 function execute() {
81 if ( !is_null( $this->mCookieCheck ) ) {
82 $this->onCookieRedirectCheck( $this->mCookieCheck );
83 return;
84 } else if( $this->mPosted ) {
85 if( $this->mCreateaccount ) {
86 return $this->addNewAccount();
87 } else if ( $this->mCreateaccountMail ) {
88 return $this->addNewAccountMailPassword();
89 } else if ( $this->mMailmypassword ) {
90 return $this->mailPassword();
91 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
92 return $this->processLogin();
93 }
94 }
95 $this->mainLoginForm( '' );
96 }
97
98 /**
99 * @private
100 */
101 function addNewAccountMailPassword() {
102 global $wgOut;
103
104 if ('' == $this->mEmail) {
105 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
106 return;
107 }
108
109 $u = $this->addNewaccountInternal();
110
111 if ($u == NULL) {
112 return;
113 }
114
115 $u->saveSettings();
116 $result = $this->mailPasswordInternal($u);
117
118 wfRunHooks( 'AddNewAccount', array( $u ) );
119
120 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
121 $wgOut->setRobotpolicy( 'noindex,nofollow' );
122 $wgOut->setArticleRelated( false );
123
124 if( WikiError::isError( $result ) ) {
125 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
126 } else {
127 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
128 $wgOut->returnToMain( false );
129 }
130 $u = 0;
131 }
132
133
134 /**
135 * @private
136 */
137 function addNewAccount() {
138 global $wgUser, $wgEmailAuthentication;
139
140 # Create the account and abort if there's a problem doing so
141 $u = $this->addNewAccountInternal();
142 if( $u == NULL )
143 return;
144
145 # If we showed up language selection links, and one was in use, be
146 # smart (and sensible) and save that language as the user's preference
147 global $wgLoginLanguageSelector;
148 if( $wgLoginLanguageSelector && $this->mLanguage )
149 $u->setOption( 'language', $this->mLanguage );
150
151 # Save user settings and send out an email authentication message if needed
152 $u->saveSettings();
153 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) )
154 $u->sendConfirmationMail();
155
156 # If not logged in, assume the new account as the current one and set session cookies
157 # then show a "welcome" message or a "need cookies" message as needed
158 if( $wgUser->isAnon() ) {
159 $wgUser = $u;
160 $wgUser->setCookies();
161 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
162 if( $this->hasSessionCookie() ) {
163 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
164 } else {
165 return $this->cookieRedirectCheck( 'new' );
166 }
167 } else {
168 # Confirm that the account was created
169 global $wgOut;
170 $skin = $wgUser->getSkin();
171 $self = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
172 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
173 $wgOut->setArticleRelated( false );
174 $wgOut->setRobotPolicy( 'noindex,nofollow' );
175 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
176 $wgOut->returnToMain( $self->getPrefixedText() );
177 wfRunHooks( 'AddNewAccount', array( $u ) );
178 return true;
179 }
180 }
181
182 /**
183 * @private
184 */
185 function addNewAccountInternal() {
186 global $wgUser, $wgOut;
187 global $wgEnableSorbs, $wgProxyWhitelist;
188 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
189 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
190
191 // If the user passes an invalid domain, something is fishy
192 if( !$wgAuth->validDomain( $this->mDomain ) ) {
193 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
194 return false;
195 }
196
197 // If we are not allowing users to login locally, we should
198 // be checking to see if the user is actually able to
199 // authenticate to the authentication server before they
200 // create an account (otherwise, they can create a local account
201 // and login as any domain user). We only need to check this for
202 // domains that aren't local.
203 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
204 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
205 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
206 return false;
207 }
208 }
209
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
212 return false;
213 }
214
215 if (!$wgUser->isAllowedToCreateAccount()) {
216 $this->userNotPrivilegedMessage();
217 return false;
218 }
219
220 $ip = wfGetIP();
221 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
222 $wgUser->inSorbsBlacklist( $ip ) )
223 {
224 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
225 return;
226 }
227
228 $name = trim( $this->mName );
229 $u = User::newFromName( $name );
230 if ( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
231 $this->mainLoginForm( wfMsg( 'noname' ) );
232 return false;
233 }
234
235 if ( 0 != $u->idForName() ) {
236 $this->mainLoginForm( wfMsg( 'userexists' ) );
237 return false;
238 }
239
240 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
241 $this->mainLoginForm( wfMsg( 'badretype' ) );
242 return false;
243 }
244
245 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
246 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
247 return false;
248 }
249
250 if ( $wgAccountCreationThrottle ) {
251 $key = $wgDBname.':acctcreate:ip:'.$ip;
252 $value = $wgMemc->incr( $key );
253 if ( !$value ) {
254 $wgMemc->set( $key, 1, 86400 );
255 }
256 if ( $value > $wgAccountCreationThrottle ) {
257 $this->throttleHit( $wgAccountCreationThrottle );
258 return false;
259 }
260 }
261
262 $abortError = '';
263 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
264 // Hook point to add extra creation throttles and blocks
265 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
266 $this->mainLoginForm( $abortError );
267 return false;
268 }
269
270 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
271 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
272 return false;
273 }
274
275 # Update user count
276 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
277 $ssUpdate->doUpdate();
278
279 return $this->initUser( $u );
280 }
281
282 /**
283 * Actually add a user to the database.
284 * Give it a User object that has been initialised with a name.
285 *
286 * @param $u User object.
287 * @return User object.
288 * @private
289 */
290 function &initUser( &$u ) {
291 $u->addToDatabase();
292 $u->setPassword( $this->mPassword );
293 $u->setEmail( $this->mEmail );
294 $u->setRealName( $this->mRealName );
295 $u->setToken();
296
297 global $wgAuth;
298 $wgAuth->initUser( $u );
299
300 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
301
302 return $u;
303 }
304
305 /**
306 * @private
307 */
308 function processLogin() {
309 global $wgUser, $wgAuth, $wgReservedUsernames;
310
311 if ( '' == $this->mName ) {
312 $this->mainLoginForm( wfMsg( 'noname' ) );
313 return;
314 }
315 $u = User::newFromName( $this->mName );
316 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
317 $this->mainLoginForm( wfMsg( 'noname' ) );
318 return;
319 }
320 if ( 0 == $u->getID() ) {
321 global $wgAuth;
322 /**
323 * If the external authentication plugin allows it,
324 * automatically create a new account for users that
325 * are externally defined but have not yet logged in.
326 */
327 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
328 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
329 $u =& $this->initUser( $u );
330 } else {
331 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
332 return;
333 }
334 } else {
335 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
336 return;
337 }
338 } else {
339 $u->loadFromDatabase();
340 }
341
342 if (!$u->checkPassword( $this->mPassword )) {
343 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
344 return;
345 }
346
347 # We've verified now, update the real record
348 #
349 if ( $this->mRemember ) {
350 $r = 1;
351 } else {
352 $r = 0;
353 }
354 $u->setOption( 'rememberpassword', $r );
355
356 $wgAuth->updateUser( $u );
357
358 $wgUser = $u;
359 $wgUser->setCookies();
360
361 $wgUser->saveSettings();
362
363 if( $this->hasSessionCookie() ) {
364 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
365 } else {
366 return $this->cookieRedirectCheck( 'login' );
367 }
368 }
369
370 /**
371 * @private
372 */
373 function mailPassword() {
374 global $wgUser, $wgOut;
375
376 # Check against the rate limiter
377 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
378 $wgOut->rateLimited();
379 return;
380 }
381
382 if ( '' == $this->mName ) {
383 $this->mainLoginForm( wfMsg( 'noname' ) );
384 return;
385 }
386 $u = User::newFromName( $this->mName );
387 if( is_null( $u ) ) {
388 $this->mainLoginForm( wfMsg( 'noname' ) );
389 return;
390 }
391 if ( 0 == $u->getID() ) {
392 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
393 return;
394 }
395
396 $u->loadFromDatabase();
397
398 $result = $this->mailPasswordInternal( $u );
399 if( WikiError::isError( $result ) ) {
400 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
401 } else {
402 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
403 }
404 }
405
406
407 /**
408 * @return mixed true on success, WikiError on failure
409 * @private
410 */
411 function mailPasswordInternal( $u ) {
412 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
413 global $wgServer, $wgScript;
414
415 if ( '' == $u->getEmail() ) {
416 return wfMsg( 'noemail', $u->getName() );
417 }
418
419 $np = $u->randomPassword();
420 $u->setNewpassword( $np );
421
422 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
423
424 $u->saveSettings();
425
426 $ip = wfGetIP();
427 if ( '' == $ip ) { $ip = '(Unknown)'; }
428
429 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
430
431 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
432 return $result;
433 }
434
435
436 /**
437 * @param string $msg Message that will be shown on success
438 * @param bool $auto Toggle auto-redirect to main page; default true
439 * @private
440 */
441 function successfulLogin( $msg, $auto = true ) {
442 global $wgUser;
443 global $wgOut;
444
445 # Run any hooks; ignore results
446
447 wfRunHooks('UserLoginComplete', array(&$wgUser));
448
449 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
450 $wgOut->setRobotpolicy( 'noindex,nofollow' );
451 $wgOut->setArticleRelated( false );
452 $wgOut->addWikiText( $msg );
453 if ( !empty( $this->mReturnTo ) ) {
454 $wgOut->returnToMain( $auto, $this->mReturnTo );
455 } else {
456 $wgOut->returnToMain( $auto );
457 }
458 }
459
460 /** */
461 function userNotPrivilegedMessage() {
462 global $wgOut;
463
464 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
465 $wgOut->setRobotpolicy( 'noindex,nofollow' );
466 $wgOut->setArticleRelated( false );
467
468 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
469
470 $wgOut->returnToMain( false );
471 }
472
473 /**
474 * @private
475 */
476 function mainLoginForm( $msg, $msgtype = 'error' ) {
477 global $wgUser, $wgOut;
478 global $wgAllowRealName, $wgEnableEmail;
479 global $wgCookiePrefix;
480 global $wgAuth;
481
482 if ( $this->mType == 'signup' && !$wgUser->isAllowedToCreateAccount() ) {
483 $this->userNotPrivilegedMessage();
484 return;
485 }
486
487 if ( '' == $this->mName ) {
488 if ( $wgUser->isLoggedIn() ) {
489 $this->mName = $wgUser->getName();
490 } else {
491 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
492 }
493 }
494
495 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
496
497 require_once( 'SkinTemplate.php' );
498 require_once( 'templates/Userlogin.php' );
499
500 if ( $this->mType == 'signup' ) {
501 $template =& new UsercreateTemplate();
502 $q = 'action=submitlogin&type=signup';
503 $linkq = 'type=login';
504 $linkmsg = 'gotaccount';
505 } else {
506 $template =& new UserloginTemplate();
507 $q = 'action=submitlogin&type=login';
508 $linkq = 'type=signup';
509 $linkmsg = 'nologin';
510 }
511
512 if ( !empty( $this->mReturnTo ) ) {
513 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
514 $q .= $returnto;
515 $linkq .= $returnto;
516 }
517
518 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
519 $link .= wfMsgHtml( $linkmsg . 'link' );
520 $link .= '</a>';
521
522 # Don't show a "create account" link if the user can't
523 if( $this->showCreateOrLoginLink( $wgUser ) )
524 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
525 else
526 $template->set( 'link', '' );
527
528 $template->set( 'header', '' );
529 $template->set( 'name', $this->mName );
530 $template->set( 'password', $this->mPassword );
531 $template->set( 'retype', $this->mRetype );
532 $template->set( 'email', $this->mEmail );
533 $template->set( 'realname', $this->mRealName );
534 $template->set( 'domain', $this->mDomain );
535
536 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
537 $template->set( 'message', $msg );
538 $template->set( 'messagetype', $msgtype );
539 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
540 $template->set( 'userealname', $wgAllowRealName );
541 $template->set( 'useemail', $wgEnableEmail );
542 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
543
544 global $wgLoginLanguageSelector;
545 if( $wgLoginLanguageSelector ) {
546 $template->set( 'languages', $this->makeLanguageSelector() );
547 if( $this->mLanguage )
548 $template->set( 'uselang', $this->mLanguage );
549 }
550
551 // Give authentication and captcha plugins a chance to modify the form
552 $wgAuth->modifyUITemplate( $template );
553 if ( $this->mType == 'signup' ) {
554 wfRunHooks( 'UserCreateForm', array( &$template ) );
555 } else {
556 wfRunHooks( 'UserLoginForm', array( &$template ) );
557 }
558
559 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
560 $wgOut->setRobotpolicy( 'noindex,nofollow' );
561 $wgOut->setArticleRelated( false );
562 $wgOut->addTemplate( $template );
563 }
564
565 /**
566 * @private
567 */
568 function showCreateOrLoginLink( &$user ) {
569 if( $this->mType == 'signup' ) {
570 return( true );
571 } elseif( $user->isAllowedToCreateAccount() ) {
572 return( true );
573 } else {
574 return( false );
575 }
576 }
577
578 /**
579 * @private
580 */
581 function hasSessionCookie() {
582 global $wgDisableCookieCheck;
583 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
584 }
585
586 /**
587 * @private
588 */
589 function cookieRedirectCheck( $type ) {
590 global $wgOut;
591
592 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
593 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
594
595 return $wgOut->redirect( $check );
596 }
597
598 /**
599 * @private
600 */
601 function onCookieRedirectCheck( $type ) {
602 global $wgUser;
603
604 if ( !$this->hasSessionCookie() ) {
605 if ( $type == 'new' ) {
606 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
607 } else if ( $type == 'login' ) {
608 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
609 } else {
610 # shouldn't happen
611 return $this->mainLoginForm( wfMsg( 'error' ) );
612 }
613 } else {
614 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
615 }
616 }
617
618 /**
619 * @private
620 */
621 function throttleHit( $limit ) {
622 global $wgOut;
623
624 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
625 }
626
627 /**
628 * Produce a bar of links which allow the user to select another language
629 * during login/registration but retain "returnto"
630 *
631 * @return string
632 */
633 function makeLanguageSelector() {
634 $msg = wfMsgForContent( 'loginlanguagelinks' );
635 if( $msg != '' && $msg != '&lt;loginlanguagelinks&gt;' ) {
636 $langs = explode( "\n", $msg );
637 $links = array();
638 foreach( $langs as $lang ) {
639 $lang = trim( $lang, '* ' );
640 $parts = explode( '|', $lang );
641 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
642 }
643 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
644 } else {
645 return '';
646 }
647 }
648
649 /**
650 * Create a language selector link for a particular language
651 * Links back to this page preserving type and returnto
652 *
653 * @param $text Link text
654 * @param $lang Language code
655 */
656 function makeLanguageSelectorLink( $text, $lang ) {
657 global $wgUser;
658 $self = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
659 $attr[] = 'uselang=' . $lang;
660 if( $this->mType == 'signup' )
661 $attr[] = 'type=signup';
662 if( $this->mReturnTo )
663 $attr[] = 'returnto=' . $this->mReturnTo;
664 $skin =& $wgUser->getSkin();
665 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
666 }
667
668 }
669 ?>