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