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