Since updates are no longer truly deferred, replaced code for adding
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once('UserMailer.php');
12
13 /**
14 * consutrctor
15 */
16 function wfSpecialUserlogin() {
17 global $wgCommandLineMode;
18 global $wgRequest;
19 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
20 User::SetupSession();
21 }
22
23 $form = new LoginForm( $wgRequest );
24 $form->execute();
25 }
26
27 /**
28 *
29 * @package MediaWiki
30 * @subpackage SpecialPage
31 */
32 class LoginForm {
33 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
34 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
35 var $mLoginattempt, $mRemember, $mEmail;
36
37 function LoginForm( &$request ) {
38 global $wgLang, $wgAllowRealName, $wgEnableEmail;
39
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' );
54
55 if( $wgEnableEmail ) {
56 $this->mEmail = $request->getText( 'wpEmail' );
57 } else {
58 $this->mEmail = '';
59 }
60 if( $wgAllowRealName ) {
61 $this->mRealName = $request->getText( 'wpRealName' );
62 } else {
63 $this->mRealName = '';
64 }
65
66 # When switching accounts, it sucks to get automatically logged out
67 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
68 $this->mReturnto = '';
69 }
70 }
71
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();
85 }
86 }
87 $this->mainLoginForm( '' );
88 }
89
90 /**
91 * @access private
92 */
93 function addNewAccountMailPassword() {
94 global $wgOut;
95
96 if ('' == $this->mEmail) {
97 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
98 return;
99 }
100
101 $u = $this->addNewaccountInternal();
102
103 if ($u == NULL) {
104 return;
105 }
106
107 $u->saveSettings();
108 $error = $this->mailPasswordInternal($u);
109
110 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
111 $wgOut->setRobotpolicy( 'noindex,nofollow' );
112 $wgOut->setArticleRelated( false );
113
114 if ( $error === '' ) {
115 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
116 $wgOut->returnToMain( false );
117 } else {
118 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
119 }
120
121 $u = 0;
122 }
123
124
125 /**
126 * @access private
127 */
128 function addNewAccount() {
129 global $wgUser, $wgOut;
130
131 $u = $this->addNewAccountInternal();
132
133 if ($u == NULL) {
134 return;
135 }
136
137 $wgUser = $u;
138 $wgUser->setCookies();
139
140 $wgUser->saveSettings();
141
142 if( $this->hasSessionCookie() ) {
143 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
144 } else {
145 return $this->cookieRedirectCheck( 'new' );
146 }
147 }
148
149 /**
150 * @access private
151 */
152 function addNewAccountInternal() {
153 global $wgUser, $wgOut;
154 global $wgMaxNameChars;
155 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
156
157 if (!$wgUser->isAllowedToCreateAccount()) {
158 $this->userNotPrivilegedMessage();
159 return;
160 }
161
162 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
163 $this->mainLoginForm( wfMsg( 'badretype' ) );
164 return;
165 }
166
167 $name = trim( $this->mName );
168 $u = User::newFromName( $name );
169 if ( is_null( $u ) ||
170 ( '' == $name ) ||
171 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
172 (strpos( $name, "/" ) !== false) ||
173 (strlen( $name ) > $wgMaxNameChars) ||
174 ucFirst($name) != $u->getName() )
175 {
176 $this->mainLoginForm( wfMsg( 'noname' ) );
177 return;
178 }
179 if ( wfReadOnly() ) {
180 $wgOut->readOnlyPage();
181 return;
182 }
183
184 if ( 0 != $u->idForName() ) {
185 $this->mainLoginForm( wfMsg( 'userexists' ) );
186 return;
187 }
188
189 if ( $wgAccountCreationThrottle ) {
190 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
191 $value = $wgMemc->incr( $key );
192 if ( !$value ) {
193 $wgMemc->set( $key, 1, 86400 );
194 }
195 if ( $value > $wgAccountCreationThrottle ) {
196 $this->throttleHit( $wgAccountCreationThrottle );
197 return;
198 }
199 }
200
201 return $this->initUser( $u );
202 }
203
204 /**
205 * Actually add a user to the database.
206 * Give it a User object that has been initialised with a name.
207 *
208 * @param User $u
209 * @return User
210 * @access private
211 */
212 function &initUser( &$u ) {
213 $u->addToDatabase();
214 $u->setPassword( $this->mPassword );
215 $u->setEmail( $this->mEmail );
216 $u->setRealName( $this->mRealName );
217
218 global $wgAuth;
219 $wgAuth->initUser( $u );
220
221 if ( $this->mRemember ) { $r = 1; }
222 else { $r = 0; }
223 $u->setOption( 'rememberpassword', $r );
224
225 return $u;
226 }
227
228 /**
229 * @access private
230 */
231 function processLogin() {
232 global $wgUser;
233
234 if ( '' == $this->mName ) {
235 $this->mainLoginForm( wfMsg( 'noname' ) );
236 return;
237 }
238 $u = User::newFromName( $this->mName );
239 if( is_null( $u ) ) {
240 $this->mainLoginForm( wfMsg( 'noname' ) );
241 return;
242 }
243 $id = $u->idForName();
244 if ( 0 == $id ) {
245 global $wgAuth;
246 /**
247 * If the external authentication plugin allows it,
248 * automatically create a new account for users that
249 * are externally defined but have not yet logged in.
250 */
251 if( $wgAuth->autoCreate() &&
252 $wgAuth->userExists( $u->getName() ) &&
253 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
254 $u =& $this->initUser( $u );
255 } else {
256 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
257 return;
258 }
259 } else {
260 $u->setId( $id );
261 $u->loadFromDatabase();
262 }
263 if (!$u->checkPassword( $this->mPassword )) {
264 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
265 return;
266 }
267
268 # We've verified now, update the real record
269 #
270 if ( $this->mRemember ) {
271 $r = 1;
272 } else {
273 $r = 0;
274 }
275 $u->setOption( 'rememberpassword', $r );
276
277 $wgUser = $u;
278 $wgUser->setCookies();
279
280 $wgUser->saveSettings();
281
282 if( $this->hasSessionCookie() ) {
283 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
284 } else {
285 return $this->cookieRedirectCheck( 'login' );
286 }
287 }
288
289 /**
290 * @access private
291 */
292 function mailPassword() {
293 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
294 global $wgCookiePath, $wgCookieDomain, $wgDBname;
295
296 if ( '' == $this->mName ) {
297 $this->mainLoginForm( wfMsg( 'noname' ) );
298 return;
299 }
300 $u = User::newFromName( $this->mName );
301 if( is_null( $u ) ) {
302 $this->mainLoginForm( wfMsg( 'noname' ) );
303 return;
304 }
305 $id = $u->idForName();
306 if ( 0 == $id ) {
307 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
308 return;
309 }
310 $u->setId( $id );
311 $u->loadFromDatabase();
312
313 $error = $this->mailPasswordInternal( $u );
314 if ($error === '') {
315 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
316 } else {
317 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
318 }
319
320 }
321
322
323 /**
324 * @access private
325 */
326 function mailPasswordInternal( $u ) {
327 global $wgDeferredUpdateList, $wgOutputEncoding;
328 global $wgPasswordSender, $wgDBname, $wgIP;
329 global $wgCookiePath, $wgCookieDomain;
330
331 if ( '' == $u->getEmail() ) {
332 return wfMsg( 'noemail', $u->getName() );
333 }
334 $np = User::randomPassword();
335 $u->setNewpassword( $np );
336
337 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
338 $u->saveSettings();
339
340 $ip = $wgIP;
341 if ( '' == $ip ) { $ip = '(Unknown)'; }
342
343 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
344
345 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
346
347 return htmlspecialchars( $error );
348 }
349
350
351 /**
352 * @access private
353 */
354 function successfulLogin( $msg ) {
355 global $wgUser;
356 global $wgDeferredUpdateList;
357 global $wgOut;
358
359 # Run any hooks; ignore results
360
361 wfRunHooks('UserLoginComplete', $wgUser);
362
363 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
364 $wgOut->setRobotpolicy( 'noindex,nofollow' );
365 $wgOut->setArticleRelated( false );
366 $wgOut->addWikiText( $msg );
367 $wgOut->returnToMain();
368 }
369
370 function userNotPrivilegedMessage() {
371 global $wgOut, $wgUser, $wgLang;
372
373 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
374 $wgOut->setRobotpolicy( 'noindex,nofollow' );
375 $wgOut->setArticleRelated( false );
376
377 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
378
379 $wgOut->returnToMain( false );
380 }
381
382 /**
383 * @access private
384 */
385 function mainLoginForm( $err ) {
386 global $wgUser, $wgOut, $wgLang;
387 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
388
389 if ( '' == $this->mName ) {
390 if ( 0 != $wgUser->getID() ) {
391 $this->mName = $wgUser->getName();
392 } else {
393 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
394 }
395 }
396
397 $q = 'action=submit';
398 if ( !empty( $this->mReturnto ) ) {
399 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
400 }
401 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
402
403
404 require_once( 'templates/Userlogin.php' );
405 $template =& new UserloginTemplate();
406
407 $template->set( 'name', $this->mName );
408 $template->set( 'password', $this->mPassword );
409 $template->set( 'retype', $this->mRetype );
410 $template->set( 'email', $this->mEmail );
411 $template->set( 'realname', $this->mRealName );
412
413 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
414 $template->set( 'error', $err );
415 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
416 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
417 $template->set( 'userealname', $wgAllowRealName );
418 $template->set( 'useemail', $wgEnableEmail );
419 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
420
421 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
422 $wgOut->setRobotpolicy( 'noindex,nofollow' );
423 $wgOut->setArticleRelated( false );
424 $wgOut->addTemplate( $template );
425 }
426
427 /**
428 * @access private
429 */
430 function hasSessionCookie() {
431 global $wgDisableCookieCheck;
432 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
433 }
434
435 /**
436 * @access private
437 */
438 function cookieRedirectCheck( $type ) {
439 global $wgOut, $wgLang;
440
441 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
442 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
443
444 return $wgOut->redirect( $check );
445 }
446
447 /**
448 * @access private
449 */
450 function onCookieRedirectCheck( $type ) {
451 global $wgUser;
452
453 if ( !$this->hasSessionCookie() ) {
454 if ( $type == 'new' ) {
455 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
456 } else if ( $type == 'login' ) {
457 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
458 } else {
459 # shouldn't happen
460 return $this->mainLoginForm( wfMsg( 'error' ) );
461 }
462 } else {
463 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
464 }
465 }
466
467 /**
468 * @access private
469 */
470 function throttleHit( $limit ) {
471 global $wgOut;
472
473 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
474 }
475 }
476 ?>