restore feature which loads all subsections of a section during section
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2
3 require_once('UserMailer.php');
4
5 function wfSpecialUserlogin()
6 {
7 global $wgCommandLineMode;
8 global $wgRequest;
9 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
10 User::SetupSession();
11 }
12
13 $form = new LoginForm( $wgRequest );
14 $form->execute();
15 }
16
17 class LoginForm {
18 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
19 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
20 var $mLoginattempt, $mRemember, $mEmail;
21
22 function LoginForm( &$request ) {
23 global $wgLang;
24
25 $this->mName = $request->getText( 'wpName' );
26 $this->mPassword = $request->getText( 'wpPassword' );
27 $this->mRetype = $request->getText( 'wpRetype' );
28 $this->mReturnto = $request->getVal( 'returnto' );
29 $this->mCookieCheck = $request->getVal( "wpCookieCheck" );
30 $this->mPosted = $request->wasPosted();
31 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
32 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' );
33 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' );
34 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
35 $this->mAction = $request->getVal( 'action' );
36 $this->mRemember = $request->getCheck( 'wpRemember' );
37 $this->mEmail = $request->getText( 'wpEmail' );
38 $this->mRealName = $request->getText( 'wpRealName' );
39
40 # When switching accounts, it sucks to get automatically logged out
41 if( $this->mReturnto == $wgLang->specialPage( "Userlogout" ) ) {
42 $this->mReturnto = "";
43 }
44 }
45
46 function execute() {
47 if ( !is_null( $this->mCookieCheck ) ) {
48 $this->onCookieRedirectCheck( $this->mCookieCheck );
49 } else if( $this->mPosted ) {
50 if( $this->mCreateaccount ) {
51 return $this->addNewAccount();
52 } else if ( $this->mCreateaccountMail ) {
53 return $this->addNewAccountMailPassword();
54 } else if ( $this->mMailmypassword ) {
55 return $this->mailPassword();
56 } else if ( ( "submit" == $this->mAction ) || $this->mLoginattempt ) {
57 return $this->processLogin();
58 }
59 }
60 $this->mainLoginForm( "" );
61 }
62
63 /* private */ function addNewAccountMailPassword()
64 {
65 global $wgOut;
66
67 if ("" == $this->mEmail) {
68 $this->mainLoginForm( wfMsg( "noemail", $this->mName ) );
69 return;
70 }
71
72 $u = $this->addNewaccountInternal();
73
74 if ($u == NULL) {
75 return;
76 }
77
78 $u->saveSettings();
79 $error = $this->mailPasswordInternal($u);
80
81 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
82 $wgOut->setRobotpolicy( "noindex,nofollow" );
83 $wgOut->setArticleRelated( false );
84
85 if ( $error === "" ) {
86 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
87 $wgOut->returnToMain( false );
88 } else {
89 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
90 }
91
92 $u = 0;
93 }
94
95
96 /* private */ function addNewAccount()
97 {
98 global $wgUser, $wgOut;
99 global $wgDeferredUpdateList;
100
101 $u = $this->addNewAccountInternal();
102
103 if ($u == NULL) {
104 return;
105 }
106
107 $wgUser = $u;
108 $wgUser->setCookies();
109
110 $up = new UserUpdate();
111 array_push( $wgDeferredUpdateList, $up );
112
113 if( $this->hasSessionCookie() ) {
114 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
115 } else {
116 return $this->cookieRedirectCheck( "new" );
117 }
118 }
119
120
121 /* private */ function addNewAccountInternal()
122 {
123 global $wgUser, $wgOut;
124 global $wgMaxNameChars;
125
126 if (!$wgUser->isAllowedToCreateAccount()) {
127 $this->userNotPrivilegedMessage();
128 return;
129 }
130
131 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
132 $this->mainLoginForm( wfMsg( "badretype" ) );
133 return;
134 }
135
136 $name = trim( $this->mName );
137 if ( ( "" == $name ) ||
138 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
139 (strpos( $name, "/" ) !== false) ||
140 (strlen( $name ) > $wgMaxNameChars) )
141 {
142 $this->mainLoginForm( wfMsg( "noname" ) );
143 return;
144 }
145 if ( wfReadOnly() ) {
146 $wgOut->readOnlyPage();
147 return;
148 }
149 $u = User::newFromName( $name );
150
151 if ( 0 != $u->idForName() ) {
152 $this->mainLoginForm( wfMsg( "userexists" ) );
153 return;
154 }
155 $u->addToDatabase();
156 $u->setPassword( $this->mPassword );
157 $u->setEmail( $this->mEmail );
158 $u->setRealName( $this->mRealName );
159
160 if ( $this->mRemember ) { $r = 1; }
161 else { $r = 0; }
162 $u->setOption( "rememberpassword", $r );
163
164 return $u;
165 }
166
167
168
169 /* private */ function processLogin()
170 {
171 global $wgUser;
172 global $wgDeferredUpdateList;
173
174 if ( "" == $this->mName ) {
175 $this->mainLoginForm( wfMsg( "noname" ) );
176 return;
177 }
178 $u = User::newFromName( $this->mName );
179 $id = $u->idForName();
180 if ( 0 == $id ) {
181 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
182 return;
183 }
184 $u->setId( $id );
185 $u->loadFromDatabase();
186 $ep = $u->encryptPassword( $this->mPassword );
187 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
188 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
189 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
190 return;
191 }
192 }
193
194 # We've verified now, update the real record
195 #
196 if ( $this->mRemember ) {
197 $r = 1;
198 $u->setCookiePassword( $this->mPassword );
199 } else {
200 $r = 0;
201 }
202 $u->setOption( "rememberpassword", $r );
203
204 $wgUser = $u;
205 $wgUser->setCookies();
206
207 $up = new UserUpdate();
208 array_push( $wgDeferredUpdateList, $up );
209
210 if( $this->hasSessionCookie() ) {
211 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
212 } else {
213 return $this->cookieRedirectCheck( "login" );
214 }
215 }
216
217 /* private */ function mailPassword()
218 {
219 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
220 global $wgCookiePath, $wgCookieDomain, $wgDBname;
221
222 if ( "" == $this->mName ) {
223 $this->mainLoginForm( wfMsg( "noname" ) );
224 return;
225 }
226 $u = User::newFromName( $this->mName );
227 $id = $u->idForName();
228 if ( 0 == $id ) {
229 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
230 return;
231 }
232 $u->setId( $id );
233 $u->loadFromDatabase();
234
235 $error = $this->mailPasswordInternal( $u );
236 if ($error === "") {
237 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
238 } else {
239 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
240 }
241
242 }
243
244
245 /* private */ function mailPasswordInternal( $u )
246 {
247 global $wgDeferredUpdateList, $wgOutputEncoding;
248 global $wgPasswordSender, $wgDBname, $wgIP;
249 global $wgCookiePath, $wgCookieDomain;
250
251 if ( "" == $u->getEmail() ) {
252 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
253 return;
254 }
255 $np = User::randomPassword();
256 $u->setNewpassword( $np );
257
258 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
259 $u->saveSettings();
260
261 $ip = $wgIP;
262 if ( "" == $ip ) { $ip = "(Unknown)"; }
263
264 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
265
266 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
267
268 return $error;
269 }
270
271
272
273
274
275 /* private */ function successfulLogin( $msg )
276 {
277 global $wgUser;
278 global $wgDeferredUpdateList;
279 global $wgOut;
280
281 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
282 $wgOut->setRobotpolicy( "noindex,nofollow" );
283 $wgOut->setArticleRelated( false );
284 $wgOut->addHTML( $msg );
285 $wgOut->returnToMain();
286 }
287
288 function userNotPrivilegedMessage()
289 {
290 global $wgOut, $wgUser, $wgLang;
291
292 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
293 $wgOut->setRobotpolicy( "noindex,nofollow" );
294 $wgOut->setArticleRelated( false );
295
296 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
297
298 $wgOut->returnToMain( false );
299 }
300
301 /* private */ function mainLoginForm( $err )
302 {
303 global $wgUser, $wgOut, $wgLang;
304 global $wgDBname;
305
306 $le = wfMsg( "loginerror" );
307 $yn = wfMsg( "yourname" );
308 $yp = wfMsg( "yourpassword" );
309 $ypa = wfMsg( "yourpasswordagain" );
310 $rmp = wfMsg( "remembermypassword" );
311 $nuo = wfMsg( "newusersonly" );
312 $li = wfMsg( "login" );
313 $ca = wfMsg( "createaccount" );
314 $cam = wfMsg( "createaccountmail" );
315 $ye = wfMsg( "youremail" );
316 $yrn = wfMsg( "yourrealname" );
317 $efl = wfMsg( "emailforlost" );
318 $mmp = wfMsg( "mailmypassword" );
319 $endText = wfMsg( "loginend" );
320
321 if ( $endText = "&lt;loginend&gt;" ) {
322 $endText = "";
323 }
324
325 if ( "" == $this->mName ) {
326 if ( 0 != $wgUser->getID() ) {
327 $this->mName = $wgUser->getName();
328 } else {
329 $this->mName = @$_COOKIE["{$wgDBname}UserName"];
330 }
331 }
332
333 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
334 $wgOut->setRobotpolicy( "noindex,nofollow" );
335 $wgOut->setArticleRelated( false );
336
337 if ( "" == $err ) {
338 $lp = wfMsg( "loginprompt" );
339 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
340 } else {
341 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
342 color='red'>$err</font>\n" );
343 }
344 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
345 $checked = " checked";
346 } else {
347 $checked = "";
348 }
349
350 $q = "action=submit";
351 if ( !empty( $this->mReturnto ) ) {
352 $q .= "&returnto=" . wfUrlencode( $this->mReturnto );
353 }
354
355 $titleObj = Title::makeTitle( NS_SPECIAL, "Userlogin" );
356 $action = $titleObj->escapeLocalUrl( $q );
357
358 $encName = wfEscapeHTML( $this->mName );
359 $encPassword = wfEscapeHTML( $this->mPassword );
360 $encRetype = wfEscapeHTML( $this->mRetype );
361 $encEmail = wfEscapeHTML( $this->mEmail );
362 $encRealName = wfEscapeHTML( $this->mRealName );
363
364 if ($wgUser->getID() != 0) {
365 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
366 } else {
367 $cambutton = "";
368 }
369
370 $wgOut->addHTML( "
371 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
372 <table border='0'><tr>
373 <td align='right'>$yn:</td>
374 <td align='left'>
375 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
376 </td>
377 <td align='left'>
378 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
379 </td>
380 </tr>
381 <tr>
382 <td align='right'>$yp:</td>
383 <td align='left'>
384 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
385 </td>
386 <td align='left'>
387 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
388 </td>
389 </tr>");
390
391 if ($wgUser->isAllowedToCreateAccount()) {
392 $encRetype = htmlspecialchars( $this->mRetype );
393 $encEmail = htmlspecialchars( $this->mEmail );
394 $wgOut->addHTML("<tr><td colspan='3'>&nbsp;</td></tr><tr>
395 <td align='right'>$ypa:</td>
396 <td align='left'>
397 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
398 size='20' />
399 </td><td>$nuo</td></tr>
400 <tr>
401 <td align='right'>$ye:</td>
402 <td align='left'>
403 <input tabindex='6' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
404 </td>
405 <td>&nbsp;</td>
406 </tr>
407 <tr>
408 <td align='right'>$yrn:</td>
409 <td align='left'>
410 <input tabindex='6' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
411 </td>
412 <td align='left'>
413 <input tabindex='7' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
414 $cambutton
415 </td></tr>");
416 }
417
418 $wgOut->addHTML("
419 <tr><td colspan='3'>&nbsp;</td></tr><tr>
420 <td colspan='3' align='left'>
421 <p>$efl<br />
422 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
423 </td></tr></table>
424 </form>\n" );
425 $wgOut->addHTML( $endText );
426 }
427
428 /* private */ function hasSessionCookie()
429 {
430 global $wgDisableCookieCheck;
431 return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
432 }
433
434 /* private */ function cookieRedirectCheck( $type )
435 {
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 /* private */ 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 ?>