Removed allpagesformtext1 and allpagesformtext2, obsolete
[mediawiki.git] / includes / SpecialUserlogin.php
blobb33b95395cc9423b7416713e7b4aac16fccd4311
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;
39 $this->mName = $request->getText( 'wpName' );
40 $this->mPassword = $request->getText( 'wpPassword' );
41 $this->mRetype = $request->getText( 'wpRetype' );
42 $this->mReturnto = $request->getVal( 'returnto' );
43 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
44 $this->mPosted = $request->wasPosted();
45 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
46 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
47 && $wgEnableEmail;
48 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
49 && $wgEnableEmail;
50 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
51 $this->mAction = $request->getVal( 'action' );
52 $this->mRemember = $request->getCheck( 'wpRemember' );
54 if( $wgEnableEmail ) {
55 $this->mEmail = $request->getText( 'wpEmail' );
56 } else {
57 $this->mEmail = '';
59 if( $wgAllowRealName ) {
60 $this->mRealName = $request->getText( 'wpRealName' );
61 } else {
62 $this->mRealName = '';
65 # When switching accounts, it sucks to get automatically logged out
66 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
67 $this->mReturnto = '';
71 function execute() {
72 if ( !is_null( $this->mCookieCheck ) ) {
73 $this->onCookieRedirectCheck( $this->mCookieCheck );
74 return;
75 } else if( $this->mPosted ) {
76 if( $this->mCreateaccount ) {
77 return $this->addNewAccount();
78 } else if ( $this->mCreateaccountMail ) {
79 return $this->addNewAccountMailPassword();
80 } else if ( $this->mMailmypassword ) {
81 return $this->mailPassword();
82 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
83 return $this->processLogin();
86 $this->mainLoginForm( '' );
89 /**
90 * @access private
92 function addNewAccountMailPassword() {
93 global $wgOut;
95 if ('' == $this->mEmail) {
96 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
97 return;
100 $u = $this->addNewaccountInternal();
102 if ($u == NULL) {
103 return;
106 $u->saveSettings();
107 $result = $this->mailPasswordInternal($u);
109 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
110 $wgOut->setRobotpolicy( 'noindex,nofollow' );
111 $wgOut->setArticleRelated( false );
113 if( WikiError::isError( $result ) ) {
114 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
115 } else {
116 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
117 $wgOut->returnToMain( false );
119 $u = 0;
124 * @access private
126 function addNewAccount() {
127 global $wgUser, $wgOut;
129 $u = $this->addNewAccountInternal();
131 if ($u == NULL) {
132 return;
135 $wgUser = $u;
136 $wgUser->setCookies();
138 $wgUser->saveSettings();
139 if( $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
140 $wgUser->sendConfirmationMail();
143 if( $this->hasSessionCookie() ) {
144 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
145 } else {
146 return $this->cookieRedirectCheck( 'new' );
151 * @access private
153 function addNewAccountInternal() {
154 global $wgUser, $wgOut;
155 global $wgMaxNameChars;
156 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
157 global $wgMinimalPasswordLength;
159 if (!$wgUser->isAllowedToCreateAccount()) {
160 $this->userNotPrivilegedMessage();
161 return false;
164 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
165 $this->mainLoginForm( wfMsg( 'badretype' ) );
166 return false;
169 $name = trim( $this->mName );
170 $u = User::newFromName( $name );
171 if ( is_null( $u ) ||
172 ( '' == $name ) ||
173 $wgUser->isIP( $name ) ||
174 (strpos( $name, '/' ) !== false) ||
175 (strlen( $name ) > $wgMaxNameChars) ||
176 ucFirst($name) != $u->getName() )
178 $this->mainLoginForm( wfMsg( 'noname' ) );
179 return false;
181 if ( wfReadOnly() ) {
182 $wgOut->readOnlyPage();
183 return false;
186 if ( 0 != $u->idForName() ) {
187 $this->mainLoginForm( wfMsg( 'userexists' ) );
188 return false;
191 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
192 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
193 return false;
196 if ( $wgAccountCreationThrottle ) {
197 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
198 $value = $wgMemc->incr( $key );
199 if ( !$value ) {
200 $wgMemc->set( $key, 1, 86400 );
202 if ( $value > $wgAccountCreationThrottle ) {
203 $this->throttleHit( $wgAccountCreationThrottle );
204 return false;
208 return $this->initUser( $u );
212 * Actually add a user to the database.
213 * Give it a User object that has been initialised with a name.
215 * @param User $u
216 * @return User
217 * @access private
219 function &initUser( &$u ) {
220 $u->addToDatabase();
221 $u->setPassword( $this->mPassword );
222 $u->setEmail( $this->mEmail );
223 $u->setRealName( $this->mRealName );
224 $u->setToken();
226 global $wgAuth;
227 $wgAuth->initUser( $u );
229 if ( $this->mRemember ) { $r = 1; }
230 else { $r = 0; }
231 $u->setOption( 'rememberpassword', $r );
233 return $u;
237 * @access private
239 function processLogin() {
240 global $wgUser;
242 if ( '' == $this->mName ) {
243 $this->mainLoginForm( wfMsg( 'noname' ) );
244 return;
246 $u = User::newFromName( $this->mName );
247 if( is_null( $u ) ) {
248 $this->mainLoginForm( wfMsg( 'noname' ) );
249 return;
251 if ( 0 == $u->getID() ) {
252 global $wgAuth;
254 * If the external authentication plugin allows it,
255 * automatically create a new account for users that
256 * are externally defined but have not yet logged in.
258 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
259 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
260 $u =& $this->initUser( $u );
261 } else {
262 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
263 return;
265 } else {
266 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
267 return;
269 } else {
270 $u->loadFromDatabase();
273 if (!$u->checkPassword( $this->mPassword )) {
274 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
275 return;
278 # We've verified now, update the real record
280 if ( $this->mRemember ) {
281 $r = 1;
282 } else {
283 $r = 0;
285 $u->setOption( 'rememberpassword', $r );
287 $wgUser = $u;
288 $wgUser->setCookies();
290 $wgUser->saveSettings();
292 if( $this->hasSessionCookie() ) {
293 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
294 } else {
295 return $this->cookieRedirectCheck( 'login' );
300 * @access private
302 function mailPassword() {
303 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
304 global $wgCookiePath, $wgCookieDomain, $wgDBname;
306 if ( '' == $this->mName ) {
307 $this->mainLoginForm( wfMsg( 'noname' ) );
308 return;
310 $u = User::newFromName( $this->mName );
311 if( is_null( $u ) ) {
312 $this->mainLoginForm( wfMsg( 'noname' ) );
313 return;
315 if ( 0 == $u->getID() ) {
316 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
317 return;
320 $u->loadFromDatabase();
322 $result = $this->mailPasswordInternal( $u );
323 if( WikiError::isError( $result ) ) {
324 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
325 } else {
326 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
332 * @return mixed true on success, WikiError on failure
333 * @access private
335 function mailPasswordInternal( $u ) {
336 global $wgPasswordSender, $wgDBname, $wgIP;
337 global $wgCookiePath, $wgCookieDomain;
339 if ( '' == $u->getEmail() ) {
340 return wfMsg( 'noemail', $u->getName() );
343 $np = $u->randomPassword();
344 $u->setNewpassword( $np );
346 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
348 $u->saveSettings();
350 $ip = $wgIP;
351 if ( '' == $ip ) { $ip = '(Unknown)'; }
353 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
354 $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
356 return $result;
361 * @param string $msg Message that will be shown on success.
362 * @access private
364 function successfulLogin( $msg ) {
365 global $wgUser;
366 global $wgOut;
368 # Run any hooks; ignore results
370 wfRunHooks('UserLoginComplete', array(&$wgUser));
372 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
373 $wgOut->setRobotpolicy( 'noindex,nofollow' );
374 $wgOut->setArticleRelated( false );
375 $wgOut->addWikiText( $msg );
376 $wgOut->returnToMain();
379 /** */
380 function userNotPrivilegedMessage() {
381 global $wgOut;
383 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
384 $wgOut->setRobotpolicy( 'noindex,nofollow' );
385 $wgOut->setArticleRelated( false );
387 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
389 $wgOut->returnToMain( false );
393 * @access private
395 function mainLoginForm( $err ) {
396 global $wgUser, $wgOut, $wgLang;
397 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
399 if ( '' == $this->mName ) {
400 if ( $wgUser->isLoggedIn() ) {
401 $this->mName = $wgUser->getName();
402 } else {
403 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
407 $q = 'action=submitlogin';
408 if ( !empty( $this->mReturnto ) ) {
409 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
411 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
413 require_once( 'templates/Userlogin.php' );
414 $template =& new UserloginTemplate();
416 $template->set( 'name', $this->mName );
417 $template->set( 'password', $this->mPassword );
418 $template->set( 'retype', $this->mRetype );
419 $template->set( 'email', $this->mEmail );
420 $template->set( 'realname', $this->mRealName );
422 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
423 $template->set( 'error', $err );
424 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
425 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
426 $template->set( 'userealname', $wgAllowRealName );
427 $template->set( 'useemail', $wgEnableEmail );
428 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
430 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
431 $wgOut->setRobotpolicy( 'noindex,nofollow' );
432 $wgOut->setArticleRelated( false );
433 $wgOut->addTemplate( $template );
437 * @access private
439 function hasSessionCookie() {
440 global $wgDisableCookieCheck;
441 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
445 * @access private
447 function cookieRedirectCheck( $type ) {
448 global $wgOut, $wgLang;
450 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
451 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
453 return $wgOut->redirect( $check );
457 * @access private
459 function onCookieRedirectCheck( $type ) {
460 global $wgUser;
462 if ( !$this->hasSessionCookie() ) {
463 if ( $type == 'new' ) {
464 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
465 } else if ( $type == 'login' ) {
466 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
467 } else {
468 # shouldn't happen
469 return $this->mainLoginForm( wfMsg( 'error' ) );
471 } else {
472 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
477 * @access private
479 function throttleHit( $limit ) {
480 global $wgOut;
482 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );