Lessons learnt from installing MediaWiki with safe_mode, especially support for uploa...
[mediawiki.git] / includes / SpecialUserlogin.php
blob7b5fea8e3557526a1614aa974072685fd60fd63d
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
9 * constructor
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
13 global $wgRequest;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
15 User::SetupSession();
18 $form = new LoginForm( $wgRequest );
19 $form->execute();
22 /**
24 * @package MediaWiki
25 * @subpackage SpecialPage
27 class LoginForm {
28 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail;
32 /**
33 * Constructor
34 * @param webrequest $request A webrequest object passed by reference
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
38 global $wgEmailAuthentication;
40 $this->mName = $request->getText( 'wpName' );
41 $this->mPassword = $request->getText( 'wpPassword' );
42 $this->mRetype = $request->getText( 'wpRetype' );
43 $this->mReturnto = $request->getVal( 'returnto' );
44 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
45 $this->mPosted = $request->wasPosted();
46 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
47 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
48 && $wgEnableEmail;
49 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
50 && $wgEnableEmail;
51 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
52 $this->mAction = $request->getVal( 'action' );
53 $this->mRemember = $request->getCheck( 'wpRemember' );
55 if( $wgEnableEmail ) {
56 $this->mEmail = $request->getText( 'wpEmail' );
57 } else {
58 $this->mEmail = '';
60 if( $wgAllowRealName ) {
61 $this->mRealName = $request->getText( 'wpRealName' );
62 } else {
63 $this->mRealName = '';
66 # When switching accounts, it sucks to get automatically logged out
67 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
68 $this->mReturnto = '';
72 function execute() {
73 if ( !is_null( $this->mCookieCheck ) ) {
74 $this->onCookieRedirectCheck( $this->mCookieCheck );
75 return;
76 } else if( $this->mPosted ) {
77 if( $this->mCreateaccount ) {
78 return $this->addNewAccount();
79 } else if ( $this->mCreateaccountMail ) {
80 return $this->addNewAccountMailPassword();
81 } else if ( $this->mMailmypassword ) {
82 return $this->mailPassword();
83 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
84 return $this->processLogin();
87 $this->mainLoginForm( '' );
90 /**
91 * @access private
93 function addNewAccountMailPassword() {
94 global $wgOut;
95 global $wgEmailAuthentication;
97 if ('' == $this->mEmail) {
98 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
99 return;
102 $u = $this->addNewaccountInternal();
104 if ($u == NULL) {
105 return;
108 $newadr = strtolower($this->mEmail);
110 # prepare for authentication and mail a temporary password to newadr
111 if ( !$u->isValidEmailAddr( $newadr ) ) {
112 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
114 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
115 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
117 if ($wgEmailAuthentication) {
118 $error = $this->mailPasswordInternal( $u, true, $dummy ); # mail a temporary password to the dirty address
121 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
122 $wgOut->setRobotpolicy( 'noindex,nofollow' );
123 $wgOut->setArticleRelated( false );
125 if ($wgEmailAuthentication) {
126 if ($error === '') {
127 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
128 } else {
129 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
131 # if user returns, that new email address gets authenticated in checkpassword()
133 # if ( $error === '' ) {
134 # $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
135 # $wgOut->returnToMain( false );
136 # } else {
137 # $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
139 $u = 0;
144 * @access private
146 function addNewAccount() {
147 global $wgUser, $wgOut;
148 global $wgEmailAuthentication;
150 $u = $this->addNewAccountInternal();
152 if ($u == NULL) {
153 return;
156 $newadr = strtolower($this->mEmail);
157 if ($newadr != '') { # prepare for authentication and mail a temporary password to newadr
158 if ( !$u->isValidEmailAddr( $newadr ) ) {
159 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
161 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
162 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
164 if ($wgEmailAuthentication) {
165 # mail a temporary password to the dirty address
167 $error = $this->mailPasswordInternal( $u, true, $dummy );
168 if ($error === '') {
169 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
170 } else {
171 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
173 # if user returns, that new email address gets authenticated in checkpassword()
177 $wgUser = $u;
178 $wgUser->setCookies();
180 $wgUser->saveSettings();
182 if( $this->hasSessionCookie() ) {
183 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
184 } else {
185 return $this->cookieRedirectCheck( 'new' );
190 * @access private
192 function addNewAccountInternal() {
193 global $wgUser, $wgOut;
194 global $wgMaxNameChars;
195 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
196 global $wgMinimalPasswordLength;
198 if (!$wgUser->isAllowedToCreateAccount()) {
199 $this->userNotPrivilegedMessage();
200 return false;
203 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
204 $this->mainLoginForm( wfMsg( 'badretype' ) );
205 return false;
208 $name = trim( $this->mName );
209 $u = User::newFromName( $name );
210 if ( is_null( $u ) ||
211 ( '' == $name ) ||
212 $wgUser->isIP( $name ) ||
213 (strpos( $name, '/' ) !== false) ||
214 (strlen( $name ) > $wgMaxNameChars) ||
215 ucFirst($name) != $u->getName() )
217 $this->mainLoginForm( wfMsg( 'noname' ) );
218 return false;
220 if ( wfReadOnly() ) {
221 $wgOut->readOnlyPage();
222 return false;
225 if ( 0 != $u->idForName() ) {
226 $this->mainLoginForm( wfMsg( 'userexists' ) );
227 return false;
230 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
231 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
232 return false;
235 if ( $wgAccountCreationThrottle ) {
236 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
237 $value = $wgMemc->incr( $key );
238 if ( !$value ) {
239 $wgMemc->set( $key, 1, 86400 );
241 if ( $value > $wgAccountCreationThrottle ) {
242 $this->throttleHit( $wgAccountCreationThrottle );
243 return false;
247 return $this->initUser( $u );
251 * Actually add a user to the database.
252 * Give it a User object that has been initialised with a name.
254 * @param User $u
255 * @return User
256 * @access private
258 function &initUser( &$u ) {
259 $u->addToDatabase();
260 $u->setPassword( $this->mPassword );
261 $u->setEmail( $this->mEmail );
262 $u->setRealName( $this->mRealName );
263 $u->setToken();
265 global $wgAuth;
266 $wgAuth->initUser( $u );
268 if ( $this->mRemember ) { $r = 1; }
269 else { $r = 0; }
270 $u->setOption( 'rememberpassword', $r );
272 return $u;
276 * @access private
278 function processLogin() {
279 global $wgUser, $wgLang;
280 global $wgEmailAuthentication;
282 if ( '' == $this->mName ) {
283 $this->mainLoginForm( wfMsg( 'noname' ) );
284 return;
286 $u = User::newFromName( $this->mName );
287 if( is_null( $u ) ) {
288 $this->mainLoginForm( wfMsg( 'noname' ) );
289 return;
291 if ( 0 == $u->getID() ) {
292 global $wgAuth;
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.
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;
305 } else {
306 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
307 return;
309 } else {
310 $u->loadFromDatabase();
313 # store temporarily the status before the password check is performed
314 $mailmsg = '';
315 $oldadr = strtolower($u->getEmail());
316 $newadr = strtolower($this->mEmail);
317 $alreadyauthenticated = (( $u->mEmailAuthenticationtimestamp != 0 ) || ($oldadr == '')) ;
319 # checkPassword sets EmailAuthenticationtimestamp, if the newPassword is used
321 if (!$u->checkPassword( $this->mPassword )) {
322 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
323 return;
326 # We've verified now, update the real record
328 if ( $this->mRemember ) {
329 $r = 1;
330 } else {
331 $r = 0;
333 $u->setOption( 'rememberpassword', $r );
335 /* check if user with correct password has entered a new email address */
336 if (($newadr <> '') && ($newadr <> $oldadr)) { # the user supplied a new email address on the login page
338 # prepare for authentication and mail a temporary password to newadr
339 if ( !$u->isValidEmailAddr( $newadr ) ) {
340 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
342 $u->mEmail = $newadr; # new behaviour: store this new emailaddr from login-page now into user database record ...
343 $u->mEmailAuthenticationtimestamp = 0; # ... but flag the address as "dirty" (unauthenticated)
344 $alreadyauthenticated = false;
346 if ($wgEmailAuthentication) {
348 # mail a temporary one-time password to the dirty address and return here to complete the user login
349 # if the user returns now or later using this temp. password, then the new email address $newadr
350 # - which is already stored in his user record - gets authenticated in checkpassword()
352 $error = $this->mailPasswordInternal( $u, false, $newpassword_temp);
353 $u->mNewpassword = $newpassword_temp;
355 # The temporary password is mailed. The user is logged-in as he entered his correct password
356 # This appears to be more intuitive than alternative 2.
358 if ($error === '') {
359 $mailmsg = '<br />' . wfMsg( 'passwordsentforemailauthentication', $u->getName() );
360 } else {
361 $mailmsg = '<br />' . wfMsg( 'mailerror', $error ) ;
366 $wgUser = $u;
367 $wgUser->setCookies();
369 # save all settings (incl. new email address and/or temporary password, if applicable)
370 $wgUser->saveSettings();
372 if ( !$wgEmailAuthentication || $alreadyauthenticated ) {
373 $authenticated = '';
374 $mailmsg = '';
375 } elseif ($u->mEmailAuthenticationtimestamp != 0) {
376 $authenticated = ' ' . wfMsg( 'emailauthenticated', $wgLang->timeanddate( $u->mEmailAuthenticationtimestamp, true ) );
377 } else {
378 $authenticated = ' ' . wfMsg( 'emailnotauthenticated' );
381 if( $this->hasSessionCookie() ) {
382 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) . $authenticated . $mailmsg );
383 } else {
384 return $this->cookieRedirectCheck( 'login' );
389 * @access private
391 function mailPassword() {
392 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
393 global $wgCookiePath, $wgCookieDomain, $wgDBname;
395 if ( '' == $this->mName ) {
396 $this->mainLoginForm( wfMsg( 'noname' ) );
397 return;
399 $u = User::newFromName( $this->mName );
400 if( is_null( $u ) ) {
401 $this->mainLoginForm( wfMsg( 'noname' ) );
402 return;
404 if ( 0 == $u->getID() ) {
405 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
406 return;
409 $u->loadFromDatabase();
411 $error = $this->mailPasswordInternal( $u, true, $dummy );
412 if ($error === '') {
413 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
414 } else {
415 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
417 return;
422 * @access private
424 function mailPasswordInternal( $u, $savesettings = true, &$newpassword_out ) {
425 global $wgPasswordSender, $wgDBname, $wgIP;
426 global $wgCookiePath, $wgCookieDomain;
428 if ( '' == $u->getEmail() ) {
429 return wfMsg( 'noemail', $u->getName() );
432 $np = $u->randomPassword();
433 $u->setNewpassword( $np );
435 # we want to store this new password together with other values in the calling function
436 $newpassword_out = $u->mNewpassword;
438 # WHY IS THIS HERE ? SHOULDN'T IT BE User::setcookie ???
439 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
441 if ($savesettings) {
442 $u->saveSettings();
445 $ip = $wgIP;
446 if ( '' == $ip ) { $ip = '(Unknown)'; }
448 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
450 require_once('UserMailer.php');
451 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindermailsubject' ), $m );
453 return htmlspecialchars( $error );
458 * @param string $msg Message that will be shown on success.
459 * @access private
461 function successfulLogin( $msg ) {
462 global $wgUser;
463 global $wgOut;
465 # Run any hooks; ignore results
467 wfRunHooks('UserLoginComplete', array(&$wgUser));
469 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
470 $wgOut->setRobotpolicy( 'noindex,nofollow' );
471 $wgOut->setArticleRelated( false );
472 $wgOut->addWikiText( $msg );
473 $wgOut->returnToMain();
476 /** */
477 function userNotPrivilegedMessage() {
478 global $wgOut;
480 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
481 $wgOut->setRobotpolicy( 'noindex,nofollow' );
482 $wgOut->setArticleRelated( false );
484 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
486 $wgOut->returnToMain( false );
490 * @access private
492 function mainLoginForm( $err ) {
493 global $wgUser, $wgOut, $wgLang;
494 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
495 global $wgEmailAuthentication;
497 if ( '' == $this->mName ) {
498 if ( $wgUser->isLoggedIn() ) {
499 $this->mName = $wgUser->getName();
500 } else {
501 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
505 $q = 'action=submitlogin';
506 if ( !empty( $this->mReturnto ) ) {
507 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
509 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
511 require_once( 'templates/Userlogin.php' );
512 $template =& new UserloginTemplate();
514 $template->set( 'name', $this->mName );
515 $template->set( 'password', $this->mPassword );
516 $template->set( 'retype', $this->mRetype );
517 $template->set( 'email', $this->mEmail );
518 $template->set( 'realname', $this->mRealName );
520 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
521 $template->set( 'error', $err );
522 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
523 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
524 $template->set( 'userealname', $wgAllowRealName );
525 $template->set( 'useemail', $wgEnableEmail );
526 $template->set( 'useemailauthent', $wgEmailAuthentication );
527 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
529 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
530 $wgOut->setRobotpolicy( 'noindex,nofollow' );
531 $wgOut->setArticleRelated( false );
532 $wgOut->addTemplate( $template );
536 * @access private
538 function hasSessionCookie() {
539 global $wgDisableCookieCheck;
540 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
544 * @access private
546 function cookieRedirectCheck( $type ) {
547 global $wgOut, $wgLang;
549 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
550 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
552 return $wgOut->redirect( $check );
556 * @access private
558 function onCookieRedirectCheck( $type ) {
559 global $wgUser;
561 if ( !$this->hasSessionCookie() ) {
562 if ( $type == 'new' ) {
563 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
564 } else if ( $type == 'login' ) {
565 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
566 } else {
567 # shouldn't happen
568 return $this->mainLoginForm( wfMsg( 'error' ) );
570 } else {
571 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
576 * @access private
578 function throttleHit( $limit ) {
579 global $wgOut;
581 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );