Code obfuscation (useless $r)
[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;
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->mName = $request->getText( 'wpName' );
41 $this->mPassword = $request->getText( 'wpPassword' );
42 $this->mRetype = $request->getText( 'wpRetype' );
43 $this->mDomain = $request->getText( 'wpDomain' );
44 $this->mReturnto = $request->getVal( 'returnto' );
45 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
46 $this->mPosted = $request->wasPosted();
47 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
48 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
49 && $wgEnableEmail;
50 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
51 && $wgEnableEmail;
52 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
53 $this->mAction = $request->getVal( 'action' );
54 $this->mRemember = $request->getCheck( 'wpRemember' );
55
56 if( $wgEnableEmail ) {
57 $this->mEmail = $request->getText( 'wpEmail' );
58 } else {
59 $this->mEmail = '';
60 }
61 if( $wgAllowRealName ) {
62 $this->mRealName = $request->getText( 'wpRealName' );
63 } else {
64 $this->mRealName = '';
65 }
66
67 if( !$wgAuth->validDomain( $this->mDomain ) ) {
68 $this->mDomain = 'invaliddomain';
69 }
70 $wgAuth->setDomain( $this->mDomain );
71
72 # When switching accounts, it sucks to get automatically logged out
73 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
74 $this->mReturnto = '';
75 }
76 }
77
78 function execute() {
79 if ( !is_null( $this->mCookieCheck ) ) {
80 $this->onCookieRedirectCheck( $this->mCookieCheck );
81 return;
82 } else if( $this->mPosted ) {
83 if( $this->mCreateaccount ) {
84 return $this->addNewAccount();
85 } else if ( $this->mCreateaccountMail ) {
86 return $this->addNewAccountMailPassword();
87 } else if ( $this->mMailmypassword ) {
88 return $this->mailPassword();
89 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
90 return $this->processLogin();
91 }
92 }
93 $this->mainLoginForm( '' );
94 }
95
96 /**
97 * @access private
98 */
99 function addNewAccountMailPassword() {
100 global $wgOut;
101
102 if ('' == $this->mEmail) {
103 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
104 return;
105 }
106
107 $u = $this->addNewaccountInternal();
108
109 if ($u == NULL) {
110 return;
111 }
112
113 $u->saveSettings();
114 $result = $this->mailPasswordInternal($u);
115
116 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
117 $wgOut->setRobotpolicy( 'noindex,nofollow' );
118 $wgOut->setArticleRelated( false );
119
120 if( WikiError::isError( $result ) ) {
121 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
122 } else {
123 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
124 $wgOut->returnToMain( false );
125 }
126 $u = 0;
127 }
128
129
130 /**
131 * @access private
132 */
133 function addNewAccount() {
134 global $wgUser, $wgOut, $wgEmailAuthentication;
135
136 $u = $this->addNewAccountInternal();
137
138 if ($u == NULL) {
139 return;
140 }
141
142 $wgUser = $u;
143 $wgUser->setCookies();
144
145 $wgUser->saveSettings();
146 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
147 $wgUser->sendConfirmationMail();
148 }
149
150 wfRunHooks( 'AddNewAccount' );
151
152 if( $this->hasSessionCookie() ) {
153 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
154 } else {
155 return $this->cookieRedirectCheck( 'new' );
156 }
157 }
158
159 /**
160 * @access private
161 */
162 function addNewAccountInternal() {
163 global $wgUser, $wgOut;
164 global $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
165 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
166 global $wgAuth, $wgMinimalPasswordLength;
167
168 // If the user passes an invalid domain, something is fishy
169 if( !$wgAuth->validDomain( $this->mDomain ) ) {
170 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
171 return false;
172 }
173
174 // If we are not allowing users to login locally, we should
175 // be checking to see if the user is actually able to
176 // authenticate to the authentication server before they
177 // create an account (otherwise, they can create a local account
178 // and login as any domain user). We only need to check this for
179 // domains that aren't local.
180 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
181 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
182 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
183 return false;
184 }
185 }
186
187 if (!$wgUser->isAllowedToCreateAccount()) {
188 $this->userNotPrivilegedMessage();
189 return false;
190 }
191
192 $ip = wfGetIP();
193 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
194 $wgUser->inSorbsBlacklist( $ip ) )
195 {
196 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
197 return;
198 }
199
200
201 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
202 $this->mainLoginForm( wfMsg( 'badretype' ) );
203 return false;
204 }
205
206 $name = trim( $this->mName );
207 $u = User::newFromName( $name );
208 if ( is_null( $u ) ) {
209 $this->mainLoginForm( wfMsg( 'noname' ) );
210 return false;
211 }
212
213 if ( wfReadOnly() ) {
214 $wgOut->readOnlyPage();
215 return false;
216 }
217
218 if ( 0 != $u->idForName() ) {
219 $this->mainLoginForm( wfMsg( 'userexists' ) );
220 return false;
221 }
222
223 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
224 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
225 return false;
226 }
227
228 if ( $wgAccountCreationThrottle ) {
229 $key = $wgDBname.':acctcreate:ip:'.$ip;
230 $value = $wgMemc->incr( $key );
231 if ( !$value ) {
232 $wgMemc->set( $key, 1, 86400 );
233 }
234 if ( $value > $wgAccountCreationThrottle ) {
235 $this->throttleHit( $wgAccountCreationThrottle );
236 return false;
237 }
238 }
239
240 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
241 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
242 return false;
243 }
244
245 # Update user count
246 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
247 $ssUpdate->doUpdate();
248
249 return $this->initUser( $u );
250 }
251
252 /**
253 * Actually add a user to the database.
254 * Give it a User object that has been initialised with a name.
255 *
256 * @param User $u
257 * @return User
258 * @access private
259 */
260 function &initUser( &$u ) {
261 $u->addToDatabase();
262 $u->setPassword( $this->mPassword );
263 $u->setEmail( $this->mEmail );
264 $u->setRealName( $this->mRealName );
265 $u->setToken();
266
267 global $wgAuth;
268 $wgAuth->initUser( $u );
269
270 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
271
272 return $u;
273 }
274
275 /**
276 * @access private
277 */
278 function processLogin() {
279 global $wgUser;
280 global $wgAuth;
281
282 if ( '' == $this->mName ) {
283 $this->mainLoginForm( wfMsg( 'noname' ) );
284 return;
285 }
286 $u = User::newFromName( $this->mName );
287 if( is_null( $u ) ) {
288 $this->mainLoginForm( wfMsg( 'noname' ) );
289 return;
290 }
291 if ( 0 == $u->getID() ) {
292 global $wgAuth;
293 /**
294 * If the external authentication plugin allows it,
295 * automatically create a new account for users that
296 * are externally defined but have not yet logged in.
297 */
298 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
299 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
300 $u =& $this->initUser( $u );
301 } else {
302 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
303 return;
304 }
305 } else {
306 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
307 return;
308 }
309 } else {
310 $u->loadFromDatabase();
311 }
312
313 if (!$u->checkPassword( $this->mPassword )) {
314 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
315 return;
316 }
317
318 # We've verified now, update the real record
319 #
320 if ( $this->mRemember ) {
321 $r = 1;
322 } else {
323 $r = 0;
324 }
325 $u->setOption( 'rememberpassword', $r );
326
327 $wgAuth->updateUser( $u );
328
329 $wgUser = $u;
330 $wgUser->setCookies();
331
332 $wgUser->saveSettings();
333
334 if( $this->hasSessionCookie() ) {
335 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
336 } else {
337 return $this->cookieRedirectCheck( 'login' );
338 }
339 }
340
341 /**
342 * @access private
343 */
344 function mailPassword() {
345 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
346 global $wgCookiePath, $wgCookieDomain, $wgDBname;
347
348 if ( '' == $this->mName ) {
349 $this->mainLoginForm( wfMsg( 'noname' ) );
350 return;
351 }
352 $u = User::newFromName( $this->mName );
353 if( is_null( $u ) ) {
354 $this->mainLoginForm( wfMsg( 'noname' ) );
355 return;
356 }
357 if ( 0 == $u->getID() ) {
358 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
359 return;
360 }
361
362 $u->loadFromDatabase();
363
364 $result = $this->mailPasswordInternal( $u );
365 if( WikiError::isError( $result ) ) {
366 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
367 } else {
368 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
369 }
370 }
371
372
373 /**
374 * @return mixed true on success, WikiError on failure
375 * @access private
376 */
377 function mailPasswordInternal( $u ) {
378 global $wgPasswordSender, $wgDBname;
379 global $wgCookiePath, $wgCookieDomain;
380
381 if ( '' == $u->getEmail() ) {
382 return wfMsg( 'noemail', $u->getName() );
383 }
384
385 $np = $u->randomPassword();
386 $u->setNewpassword( $np );
387
388 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
389
390 $u->saveSettings();
391
392 $ip = wfGetIP();
393 if ( '' == $ip ) { $ip = '(Unknown)'; }
394
395 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
396
397 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
398 return $result;
399 }
400
401
402 /**
403 * @param string $msg Message that will be shown on success.
404 * @access private
405 */
406 function successfulLogin( $msg ) {
407 global $wgUser;
408 global $wgOut;
409
410 # Run any hooks; ignore results
411
412 wfRunHooks('UserLoginComplete', array(&$wgUser));
413
414 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
415 $wgOut->setRobotpolicy( 'noindex,nofollow' );
416 $wgOut->setArticleRelated( false );
417 $wgOut->addWikiText( $msg );
418 $wgOut->returnToMain();
419 }
420
421 /** */
422 function userNotPrivilegedMessage() {
423 global $wgOut;
424
425 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
426 $wgOut->setRobotpolicy( 'noindex,nofollow' );
427 $wgOut->setArticleRelated( false );
428
429 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
430
431 $wgOut->returnToMain( false );
432 }
433
434 /**
435 * @access private
436 */
437 function mainLoginForm( $err ) {
438 global $wgUser, $wgOut, $wgLang;
439 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
440 global $wgAuth;
441
442 if ( '' == $this->mName ) {
443 if ( $wgUser->isLoggedIn() ) {
444 $this->mName = $wgUser->getName();
445 } else {
446 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
447 }
448 }
449
450 $q = 'action=submitlogin';
451 if ( !empty( $this->mReturnto ) ) {
452 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
453 }
454 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
455
456 require_once( 'templates/Userlogin.php' );
457 $template =& new UserloginTemplate();
458
459 $template->set( 'name', $this->mName );
460 $template->set( 'password', $this->mPassword );
461 $template->set( 'retype', $this->mRetype );
462 $template->set( 'email', $this->mEmail );
463 $template->set( 'realname', $this->mRealName );
464 $template->set( 'domain', $this->mDomain );
465
466 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
467 $template->set( 'error', $err );
468 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
469 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
470 $template->set( 'userealname', $wgAllowRealName );
471 $template->set( 'useemail', $wgEnableEmail );
472 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
473 $wgAuth->modifyUITemplate( $template );
474
475 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
476 $wgOut->setRobotpolicy( 'noindex,nofollow' );
477 $wgOut->setArticleRelated( false );
478 $wgOut->addTemplate( $template );
479 }
480
481 /**
482 * @access private
483 */
484 function hasSessionCookie() {
485 global $wgDisableCookieCheck;
486 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
487 }
488
489 /**
490 * @access private
491 */
492 function cookieRedirectCheck( $type ) {
493 global $wgOut, $wgLang;
494
495 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
496 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
497
498 return $wgOut->redirect( $check );
499 }
500
501 /**
502 * @access private
503 */
504 function onCookieRedirectCheck( $type ) {
505 global $wgUser;
506
507 if ( !$this->hasSessionCookie() ) {
508 if ( $type == 'new' ) {
509 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
510 } else if ( $type == 'login' ) {
511 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
512 } else {
513 # shouldn't happen
514 return $this->mainLoginForm( wfMsg( 'error' ) );
515 }
516 } else {
517 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
518 }
519 }
520
521 /**
522 * @access private
523 */
524 function throttleHit( $limit ) {
525 global $wgOut;
526
527 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
528 }
529 }
530 ?>