* (bug 814) Integrate AuthPlugin changes to support Ryan Lane's external
[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[ini_get('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;
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( $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
147 $wgUser->sendConfirmationMail();
148 }
149
150 if( $this->hasSessionCookie() ) {
151 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
152 } else {
153 return $this->cookieRedirectCheck( 'new' );
154 }
155 }
156
157 /**
158 * @access private
159 */
160 function addNewAccountInternal() {
161 global $wgUser, $wgOut;
162 global $wgMaxNameChars;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgMinimalPasswordLength;
165 global $wgAuth;
166
167 // If the user passes an invalid domain, something is fishy
168 if( !$wgAuth->validDomain( $this->mDomain ) ) {
169 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
170 return false;
171 }
172
173 // If we are not allowing users to login locally, we should
174 // be checking to see if the user is actually able to
175 // authenticate to the authentication server before they
176 // create an account (otherwise, they can create a local account
177 // and login as any domain user). We only need to check this for
178 // domains that aren't local.
179 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
180 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
181 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
182 return false;
183 }
184 }
185
186
187
188 if (!$wgUser->isAllowedToCreateAccount()) {
189 $this->userNotPrivilegedMessage();
190 return false;
191 }
192
193 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
194 $this->mainLoginForm( wfMsg( 'badretype' ) );
195 return false;
196 }
197
198 $name = trim( $this->mName );
199 $u = User::newFromName( $name );
200 if ( is_null( $u ) ||
201 ( '' == $name ) ||
202 $wgUser->isIP( $name ) ||
203 (strpos( $name, '/' ) !== false) ||
204 (strlen( $name ) > $wgMaxNameChars) ||
205 ucFirst($name) != $u->getName() )
206 {
207 $this->mainLoginForm( wfMsg( 'noname' ) );
208 return false;
209 }
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
212 return false;
213 }
214
215 if ( 0 != $u->idForName() ) {
216 $this->mainLoginForm( wfMsg( 'userexists' ) );
217 return false;
218 }
219
220 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
221 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
222 return false;
223 }
224
225 if ( $wgAccountCreationThrottle ) {
226 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
227 $value = $wgMemc->incr( $key );
228 if ( !$value ) {
229 $wgMemc->set( $key, 1, 86400 );
230 }
231 if ( $value > $wgAccountCreationThrottle ) {
232 $this->throttleHit( $wgAccountCreationThrottle );
233 return false;
234 }
235 }
236
237 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
238 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
239 return false;
240 }
241
242 return $this->initUser( $u );
243 }
244
245 /**
246 * Actually add a user to the database.
247 * Give it a User object that has been initialised with a name.
248 *
249 * @param User $u
250 * @return User
251 * @access private
252 */
253 function &initUser( &$u ) {
254 $u->addToDatabase();
255 $u->setPassword( $this->mPassword );
256 $u->setEmail( $this->mEmail );
257 $u->setRealName( $this->mRealName );
258 $u->setToken();
259
260 global $wgAuth;
261 $wgAuth->initUser( $u );
262
263 if ( $this->mRemember ) { $r = 1; }
264 else { $r = 0; }
265 $u->setOption( 'rememberpassword', $r );
266
267 return $u;
268 }
269
270 /**
271 * @access private
272 */
273 function processLogin() {
274 global $wgUser;
275 global $wgAuth;
276
277 if ( '' == $this->mName ) {
278 $this->mainLoginForm( wfMsg( 'noname' ) );
279 return;
280 }
281 $u = User::newFromName( $this->mName );
282 if( is_null( $u ) ) {
283 $this->mainLoginForm( wfMsg( 'noname' ) );
284 return;
285 }
286 if ( 0 == $u->getID() ) {
287 global $wgAuth;
288 /**
289 * If the external authentication plugin allows it,
290 * automatically create a new account for users that
291 * are externally defined but have not yet logged in.
292 */
293 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
294 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
295 $u =& $this->initUser( $u );
296 } else {
297 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
298 return;
299 }
300 } else {
301 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
302 return;
303 }
304 } else {
305 $u->loadFromDatabase();
306 }
307
308 if (!$u->checkPassword( $this->mPassword )) {
309 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
310 return;
311 }
312
313 # We've verified now, update the real record
314 #
315 if ( $this->mRemember ) {
316 $r = 1;
317 } else {
318 $r = 0;
319 }
320 $u->setOption( 'rememberpassword', $r );
321
322 $wgAuth->updateUser( $u );
323
324 $wgUser = $u;
325 $wgUser->setCookies();
326
327 $wgUser->saveSettings();
328
329 if( $this->hasSessionCookie() ) {
330 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
331 } else {
332 return $this->cookieRedirectCheck( 'login' );
333 }
334 }
335
336 /**
337 * @access private
338 */
339 function mailPassword() {
340 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
341 global $wgCookiePath, $wgCookieDomain, $wgDBname;
342
343 if ( '' == $this->mName ) {
344 $this->mainLoginForm( wfMsg( 'noname' ) );
345 return;
346 }
347 $u = User::newFromName( $this->mName );
348 if( is_null( $u ) ) {
349 $this->mainLoginForm( wfMsg( 'noname' ) );
350 return;
351 }
352 if ( 0 == $u->getID() ) {
353 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
354 return;
355 }
356
357 $u->loadFromDatabase();
358
359 $result = $this->mailPasswordInternal( $u );
360 if( WikiError::isError( $result ) ) {
361 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
362 } else {
363 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
364 }
365 }
366
367
368 /**
369 * @return mixed true on success, WikiError on failure
370 * @access private
371 */
372 function mailPasswordInternal( $u ) {
373 global $wgPasswordSender, $wgDBname, $wgIP;
374 global $wgCookiePath, $wgCookieDomain;
375
376 if ( '' == $u->getEmail() ) {
377 return wfMsg( 'noemail', $u->getName() );
378 }
379
380 $np = $u->randomPassword();
381 $u->setNewpassword( $np );
382
383 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
384
385 $u->saveSettings();
386
387 $ip = $wgIP;
388 if ( '' == $ip ) { $ip = '(Unknown)'; }
389
390 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
391 $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
392
393 return $result;
394 }
395
396
397 /**
398 * @param string $msg Message that will be shown on success.
399 * @access private
400 */
401 function successfulLogin( $msg ) {
402 global $wgUser;
403 global $wgOut;
404
405 # Run any hooks; ignore results
406
407 wfRunHooks('UserLoginComplete', array(&$wgUser));
408
409 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
410 $wgOut->setRobotpolicy( 'noindex,nofollow' );
411 $wgOut->setArticleRelated( false );
412 $wgOut->addWikiText( $msg );
413 $wgOut->returnToMain();
414 }
415
416 /** */
417 function userNotPrivilegedMessage() {
418 global $wgOut;
419
420 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
421 $wgOut->setRobotpolicy( 'noindex,nofollow' );
422 $wgOut->setArticleRelated( false );
423
424 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
425
426 $wgOut->returnToMain( false );
427 }
428
429 /**
430 * @access private
431 */
432 function mainLoginForm( $err ) {
433 global $wgUser, $wgOut, $wgLang;
434 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
435 global $wgAuth;
436
437 if ( '' == $this->mName ) {
438 if ( $wgUser->isLoggedIn() ) {
439 $this->mName = $wgUser->getName();
440 } else {
441 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
442 }
443 }
444
445 $q = 'action=submitlogin';
446 if ( !empty( $this->mReturnto ) ) {
447 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
448 }
449 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
450
451 require_once( 'templates/Userlogin.php' );
452 $template =& new UserloginTemplate();
453
454 $template->set( 'name', $this->mName );
455 $template->set( 'password', $this->mPassword );
456 $template->set( 'retype', $this->mRetype );
457 $template->set( 'email', $this->mEmail );
458 $template->set( 'realname', $this->mRealName );
459 $template->set( 'domain', $this->mDomain );
460
461 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
462 $template->set( 'error', $err );
463 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
464 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
465 $template->set( 'userealname', $wgAllowRealName );
466 $template->set( 'useemail', $wgEnableEmail );
467 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
468 $wgAuth->modifyUITemplate( $template );
469
470 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
471 $wgOut->setRobotpolicy( 'noindex,nofollow' );
472 $wgOut->setArticleRelated( false );
473 $wgOut->addTemplate( $template );
474 }
475
476 /**
477 * @access private
478 */
479 function hasSessionCookie() {
480 global $wgDisableCookieCheck;
481 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
482 }
483
484 /**
485 * @access private
486 */
487 function cookieRedirectCheck( $type ) {
488 global $wgOut, $wgLang;
489
490 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
491 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
492
493 return $wgOut->redirect( $check );
494 }
495
496 /**
497 * @access private
498 */
499 function onCookieRedirectCheck( $type ) {
500 global $wgUser;
501
502 if ( !$this->hasSessionCookie() ) {
503 if ( $type == 'new' ) {
504 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
505 } else if ( $type == 'login' ) {
506 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
507 } else {
508 # shouldn't happen
509 return $this->mainLoginForm( wfMsg( 'error' ) );
510 }
511 } else {
512 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
513 }
514 }
515
516 /**
517 * @access private
518 */
519 function throttleHit( $limit ) {
520 global $wgOut;
521
522 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
523 }
524 }
525 ?>