87c004cb3be9fa62149d63fb190d70d9753c30a1
[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 if ( 0 == $u->getID() ) {
244 global $wgAuth;
245 /**
246 * If the external authentication plugin allows it,
247 * automatically create a new account for users that
248 * are externally defined but have not yet logged in.
249 */
250 if( $wgAuth->autoCreate() &&
251 $wgAuth->userExists( $u->getName() ) &&
252 $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
253 $u =& $this->initUser( $u );
254 } else {
255 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
256 return;
257 }
258 } else {
259 $u->loadFromDatabase();
260 }
261 if (!$u->checkPassword( $this->mPassword )) {
262 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
263 return;
264 }
265
266 # We've verified now, update the real record
267 #
268 if ( $this->mRemember ) {
269 $r = 1;
270 } else {
271 $r = 0;
272 }
273 $u->setOption( 'rememberpassword', $r );
274
275 $wgUser = $u;
276 $wgUser->setCookies();
277
278 $wgUser->saveSettings();
279
280 if( $this->hasSessionCookie() ) {
281 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
282 } else {
283 return $this->cookieRedirectCheck( 'login' );
284 }
285 }
286
287 /**
288 * @access private
289 */
290 function mailPassword() {
291 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
292 global $wgCookiePath, $wgCookieDomain, $wgDBname;
293
294 if ( '' == $this->mName ) {
295 $this->mainLoginForm( wfMsg( 'noname' ) );
296 return;
297 }
298 $u = User::newFromName( $this->mName );
299 if( is_null( $u ) ) {
300 $this->mainLoginForm( wfMsg( 'noname' ) );
301 return;
302 }
303 if ( 0 == $u->getID() ) {
304 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
305 return;
306 }
307
308 $u->loadFromDatabase();
309
310 $error = $this->mailPasswordInternal( $u );
311 if ($error === '') {
312 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
313 } else {
314 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
315 }
316
317 }
318
319
320 /**
321 * @access private
322 */
323 function mailPasswordInternal( $u ) {
324 global $wgDeferredUpdateList, $wgOutputEncoding;
325 global $wgPasswordSender, $wgDBname, $wgIP;
326 global $wgCookiePath, $wgCookieDomain;
327
328 if ( '' == $u->getEmail() ) {
329 return wfMsg( 'noemail', $u->getName() );
330 }
331 $np = User::randomPassword();
332 $u->setNewpassword( $np );
333
334 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
335 $u->saveSettings();
336
337 $ip = $wgIP;
338 if ( '' == $ip ) { $ip = '(Unknown)'; }
339
340 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
341
342 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
343
344 return htmlspecialchars( $error );
345 }
346
347
348 /**
349 * @access private
350 */
351 function successfulLogin( $msg ) {
352 global $wgUser;
353 global $wgDeferredUpdateList;
354 global $wgOut;
355
356 # Run any hooks; ignore results
357
358 wfRunHooks('UserLoginComplete', $wgUser);
359
360 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
361 $wgOut->setRobotpolicy( 'noindex,nofollow' );
362 $wgOut->setArticleRelated( false );
363 $wgOut->addWikiText( $msg );
364 $wgOut->returnToMain();
365 }
366
367 function userNotPrivilegedMessage() {
368 global $wgOut, $wgUser, $wgLang;
369
370 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
371 $wgOut->setRobotpolicy( 'noindex,nofollow' );
372 $wgOut->setArticleRelated( false );
373
374 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
375
376 $wgOut->returnToMain( false );
377 }
378
379 /**
380 * @access private
381 */
382 function mainLoginForm( $err ) {
383 global $wgUser, $wgOut, $wgLang;
384 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
385
386 if ( '' == $this->mName ) {
387 if ( 0 != $wgUser->getID() ) {
388 $this->mName = $wgUser->getName();
389 } else {
390 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
391 }
392 }
393
394 $q = 'action=submit';
395 if ( !empty( $this->mReturnto ) ) {
396 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
397 }
398 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
399
400
401 require_once( 'templates/Userlogin.php' );
402 $template =& new UserloginTemplate();
403
404 $template->set( 'name', $this->mName );
405 $template->set( 'password', $this->mPassword );
406 $template->set( 'retype', $this->mRetype );
407 $template->set( 'email', $this->mEmail );
408 $template->set( 'realname', $this->mRealName );
409
410 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
411 $template->set( 'error', $err );
412 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
413 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
414 $template->set( 'userealname', $wgAllowRealName );
415 $template->set( 'useemail', $wgEnableEmail );
416 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
417
418 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
419 $wgOut->setRobotpolicy( 'noindex,nofollow' );
420 $wgOut->setArticleRelated( false );
421 $wgOut->addTemplate( $template );
422 }
423
424 /**
425 * @access private
426 */
427 function hasSessionCookie() {
428 global $wgDisableCookieCheck;
429 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
430 }
431
432 /**
433 * @access private
434 */
435 function cookieRedirectCheck( $type ) {
436 global $wgOut, $wgLang;
437
438 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
439 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
440
441 return $wgOut->redirect( $check );
442 }
443
444 /**
445 * @access private
446 */
447 function onCookieRedirectCheck( $type ) {
448 global $wgUser;
449
450 if ( !$this->hasSessionCookie() ) {
451 if ( $type == 'new' ) {
452 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
453 } else if ( $type == 'login' ) {
454 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
455 } else {
456 # shouldn't happen
457 return $this->mainLoginForm( wfMsg( 'error' ) );
458 }
459 } else {
460 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
461 }
462 }
463
464 /**
465 * @access private
466 */
467 function throttleHit( $limit ) {
468 global $wgOut;
469
470 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
471 }
472 }
473 ?>