Add site-wide options to disable all e-mail functions or only user-to-user email.
[mediawiki.git] / includes / SpecialUserlogin.php
blobb8881862307d131a3a5ac81bf37c8690d5cfcbef
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
11 require_once('UserMailer.php');
13 /**
14 * consutrctor
16 function wfSpecialUserlogin() {
17 global $wgCommandLineMode;
18 global $wgRequest;
19 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
20 User::SetupSession();
23 $form = new LoginForm( $wgRequest );
24 $form->execute();
27 /**
29 * @package MediaWiki
30 * @subpackage SpecialPage
32 class LoginForm {
33 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
34 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
35 var $mLoginattempt, $mRemember, $mEmail;
37 function LoginForm( &$request ) {
38 global $wgLang, $wgAllowRealName, $wgEnableEmail;
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 ( ( 'submit' == $this->mAction ) || $this->mLoginattempt ) {
84 return $this->processLogin();
87 $this->mainLoginForm( '' );
90 /**
91 * @access private
93 function addNewAccountMailPassword() {
94 global $wgOut;
96 if ('' == $this->mEmail) {
97 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
98 return;
101 $u = $this->addNewaccountInternal();
103 if ($u == NULL) {
104 return;
107 $u->saveSettings();
108 $error = $this->mailPasswordInternal($u);
110 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
111 $wgOut->setRobotpolicy( 'noindex,nofollow' );
112 $wgOut->setArticleRelated( false );
114 if ( $error === '' ) {
115 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
116 $wgOut->returnToMain( false );
117 } else {
118 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
121 $u = 0;
126 * @access private
128 function addNewAccount() {
129 global $wgUser, $wgOut;
130 global $wgDeferredUpdateList;
132 $u = $this->addNewAccountInternal();
134 if ($u == NULL) {
135 return;
138 $wgUser = $u;
139 $wgUser->setCookies();
141 $up = new UserUpdate();
142 array_push( $wgDeferredUpdateList, $up );
144 if( $this->hasSessionCookie() ) {
145 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
146 } else {
147 return $this->cookieRedirectCheck( 'new' );
153 * @access private
155 function addNewAccountInternal() {
156 global $wgUser, $wgOut;
157 global $wgMaxNameChars;
158 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
160 if (!$wgUser->isAllowedToCreateAccount()) {
161 $this->userNotPrivilegedMessage();
162 return;
165 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
166 $this->mainLoginForm( wfMsg( 'badretype' ) );
167 return;
170 $name = trim( $this->mName );
171 $u = User::newFromName( $name );
172 if ( is_null( $u ) ||
173 ( '' == $name ) ||
174 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
175 (strpos( $name, "/" ) !== false) ||
176 (strlen( $name ) > $wgMaxNameChars) ||
177 ucFirst($name) != $u->getName() )
179 $this->mainLoginForm( wfMsg( 'noname' ) );
180 return;
182 if ( wfReadOnly() ) {
183 $wgOut->readOnlyPage();
184 return;
187 if ( 0 != $u->idForName() ) {
188 $this->mainLoginForm( wfMsg( 'userexists' ) );
189 return;
192 if ( $wgAccountCreationThrottle ) {
193 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
194 $value = $wgMemc->incr( $key );
195 if ( !$value ) {
196 $wgMemc->set( $key, 1, 86400 );
198 if ( $value > $wgAccountCreationThrottle ) {
199 $this->throttleHit( $wgAccountCreationThrottle );
200 return;
204 return $this->initUser( $u );
208 * Actually add a user to the database.
209 * Give it a User object that has been initialised with a name.
211 * @param User $u
212 * @return User
213 * @access private
215 function &initUser( &$u ) {
216 $u->addToDatabase();
217 $u->setPassword( $this->mPassword );
218 $u->setEmail( $this->mEmail );
219 $u->setRealName( $this->mRealName );
221 global $wgAuth;
222 $wgAuth->initUser( $u );
224 if ( $this->mRemember ) { $r = 1; }
225 else { $r = 0; }
226 $u->setOption( 'rememberpassword', $r );
228 return $u;
232 * @access private
234 function processLogin() {
235 global $wgUser;
236 global $wgDeferredUpdateList;
238 if ( '' == $this->mName ) {
239 $this->mainLoginForm( wfMsg( 'noname' ) );
240 return;
242 $u = User::newFromName( $this->mName );
243 if( is_null( $u ) ) {
244 $this->mainLoginForm( wfMsg( 'noname' ) );
245 return;
247 $id = $u->idForName();
248 if ( 0 == $id ) {
249 global $wgAuth;
251 * If the external authentication plugin allows it,
252 * automatically create a new account for users that
253 * are externally defined but have not yet logged in.
255 if( $wgAuth->autoCreate() &&
256 $wgAuth->userExists( $u->getName() ) &&
257 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
258 $u =& $this->initUser( $u );
259 } else {
260 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
261 return;
263 } else {
264 $u->setId( $id );
265 $u->loadFromDatabase();
267 if (!$u->checkPassword( $this->mPassword )) {
268 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
269 return;
272 # We've verified now, update the real record
274 if ( $this->mRemember ) {
275 $r = 1;
276 } else {
277 $r = 0;
279 $u->setOption( 'rememberpassword', $r );
281 $wgUser = $u;
282 $wgUser->setCookies();
284 $up = new UserUpdate();
285 array_push( $wgDeferredUpdateList, $up );
287 if( $this->hasSessionCookie() ) {
288 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
289 } else {
290 return $this->cookieRedirectCheck( 'login' );
295 * @access private
297 function mailPassword() {
298 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
299 global $wgCookiePath, $wgCookieDomain, $wgDBname;
301 if ( '' == $this->mName ) {
302 $this->mainLoginForm( wfMsg( 'noname' ) );
303 return;
305 $u = User::newFromName( $this->mName );
306 if( is_null( $u ) ) {
307 $this->mainLoginForm( wfMsg( 'noname' ) );
308 return;
310 $id = $u->idForName();
311 if ( 0 == $id ) {
312 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
313 return;
315 $u->setId( $id );
316 $u->loadFromDatabase();
318 $error = $this->mailPasswordInternal( $u );
319 if ($error === '') {
320 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
321 } else {
322 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
329 * @access private
331 function mailPasswordInternal( $u ) {
332 global $wgDeferredUpdateList, $wgOutputEncoding;
333 global $wgPasswordSender, $wgDBname, $wgIP;
334 global $wgCookiePath, $wgCookieDomain;
336 if ( '' == $u->getEmail() ) {
337 return wfMsg( 'noemail', $u->getName() );
339 $np = User::randomPassword();
340 $u->setNewpassword( $np );
342 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
343 $u->saveSettings();
345 $ip = $wgIP;
346 if ( '' == $ip ) { $ip = '(Unknown)'; }
348 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
350 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
352 return htmlspecialchars( $error );
357 * @access private
359 function successfulLogin( $msg ) {
360 global $wgUser;
361 global $wgDeferredUpdateList;
362 global $wgOut;
364 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
365 $wgOut->setRobotpolicy( 'noindex,nofollow' );
366 $wgOut->setArticleRelated( false );
367 $wgOut->addWikiText( $msg );
368 $wgOut->returnToMain();
371 function userNotPrivilegedMessage() {
372 global $wgOut, $wgUser, $wgLang;
374 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
375 $wgOut->setRobotpolicy( 'noindex,nofollow' );
376 $wgOut->setArticleRelated( false );
378 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
380 $wgOut->returnToMain( false );
384 * @access private
386 function mainLoginForm( $err ) {
387 global $wgUser, $wgOut, $wgLang;
388 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
390 if ( '' == $this->mName ) {
391 if ( 0 != $wgUser->getID() ) {
392 $this->mName = $wgUser->getName();
393 } else {
394 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
398 $q = 'action=submit';
399 if ( !empty( $this->mReturnto ) ) {
400 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
402 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
405 require_once( 'templates/Userlogin.php' );
406 $template =& new UserloginTemplate();
408 $template->set( 'name', $this->mName );
409 $template->set( 'password', $this->mPassword );
410 $template->set( 'retype', $this->mRetype );
411 $template->set( 'email', $this->mEmail );
412 $template->set( 'realname', $this->mRealName );
414 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
415 $template->set( 'error', $err );
416 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
417 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
418 $template->set( 'userealname', $wgAllowRealName );
419 $template->set( 'useemail', $wgEnableEmail );
420 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
422 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
423 $wgOut->setRobotpolicy( 'noindex,nofollow' );
424 $wgOut->setArticleRelated( false );
425 $wgOut->addTemplate( $template );
429 * @access private
431 function hasSessionCookie() {
432 global $wgDisableCookieCheck;
433 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
437 * @access private
439 function cookieRedirectCheck( $type ) {
440 global $wgOut, $wgLang;
442 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
443 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
445 return $wgOut->redirect( $check );
449 * @access private
451 function onCookieRedirectCheck( $type ) {
452 global $wgUser;
454 if ( !$this->hasSessionCookie() ) {
455 if ( $type == 'new' ) {
456 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
457 } else if ( $type == 'login' ) {
458 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
459 } else {
460 # shouldn't happen
461 return $this->mainLoginForm( wfMsg( 'error' ) );
463 } else {
464 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
469 * @access private
471 function throttleHit( $limit ) {
472 global $wgOut;
474 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );