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