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