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