Added support for whitelists for reading and writing articles and for
[lhc/web/wiklou.git] / includes / User.php
1 <?
2 # See user.doc
3
4 class User {
5 /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
6 /* private */ var $mRights, $mOptions;
7 /* private */ var $mDataLoaded, $mNewpassword;
8 /* private */ var $mSkin;
9 /* private */ var $mBlockedby, $mBlockreason;
10 /* private */ var $mTouched;
11 /* private */ var $mCookiePassword;
12
13 function User()
14 {
15 $this->loadDefaults();
16 }
17
18 # Static factory method
19 #
20 function newFromName( $name )
21 {
22 $u = new User();
23
24 # Clean up name according to title rules
25
26 $t = Title::newFromText( $name );
27 $u->setName( $t->getText() );
28 return $u;
29 }
30
31 /* static */ function whoIs( $id )
32 {
33 return wfGetSQL( "user", "user_name", "user_id=$id" );
34 }
35
36 /* static */ function idFromName( $name )
37 {
38 $nt = Title::newFromText( $name );
39 $sql = "SELECT user_id FROM user WHERE user_name='" .
40 wfStrencode( $nt->getText() ) . "'";
41 $res = wfQuery( $sql, "User::idFromName" );
42
43 if ( 0 == wfNumRows( $res ) ) { return 0; }
44 else {
45 $s = wfFetchObject( $res );
46 return $s->user_id;
47 }
48 }
49
50 # does the string match an anonymous user IP address?
51 /* static */ function isIP( $name ) {
52 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
53
54 }
55
56 /* static */ function randomPassword()
57 {
58 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
59 $l = strlen( $pwchars ) - 1;
60
61 wfSeedRandom();
62 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
63 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
64 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
65 $pwchars{mt_rand( 0, $l )};
66 return $np;
67 }
68
69 function loadDefaults()
70 {
71 global $wgLang ;
72 global $wgNamespacesToBeSearchedDefault;
73
74 $this->mId = $this->mNewtalk = 0;
75 $this->mName = getenv( "REMOTE_ADDR" );
76 $this->mEmail = "";
77 $this->mPassword = $this->mNewpassword = "";
78 $this->mRights = array();
79 $defOpt = $wgLang->getDefaultUserOptions() ;
80 foreach ( $defOpt as $oname => $val ) {
81 $this->mOptions[$oname] = $val;
82 }
83 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
84 $this->mOptions["searchNs".$nsnum] = $val;
85 }
86 unset( $this->mSkin );
87 $this->mDataLoaded = false;
88 $this->mBlockedby = -1; # Unset
89 $this->mTouched = '0'; # Allow any pages to be cached
90 $this->cookiePassword = "";
91 }
92
93 /* private */ function getBlockedStatus()
94 {
95 if ( -1 != $this->mBlockedby ) { return; }
96
97 $remaddr = getenv( "REMOTE_ADDR" );
98 if ( 0 == $this->mId ) {
99 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
100 "ipb_address='$remaddr'";
101 } else {
102 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
103 "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
104 }
105 $res = wfQuery( $sql, "User::getBlockedStatus" );
106 if ( 0 == wfNumRows( $res ) ) {
107 $this->mBlockedby = 0;
108 return;
109 }
110 $s = wfFetchObject( $res );
111 $this->mBlockedby = $s->ipb_by;
112 $this->mBlockreason = $s->ipb_reason;
113 }
114
115 function isBlocked()
116 {
117 $this->getBlockedStatus();
118 if ( 0 == $this->mBlockedby ) { return false; }
119 return true;
120 }
121
122 function blockedBy() {
123 $this->getBlockedStatus();
124 return $this->mBlockedby;
125 }
126
127 function blockedFor() {
128 $this->getBlockedStatus();
129 return $this->mBlockreason;
130 }
131
132 function loadFromSession()
133 {
134 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
135
136 if ( isset( $wsUserID ) ) {
137 if ( 0 != $wsUserID ) {
138 $sId = $wsUserID;
139 } else {
140 $this->mId = 0;
141 return;
142 }
143 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
144 $sId = $HTTP_COOKIE_VARS["wcUserID"];
145 $wsUserID = $sId;
146 } else {
147 $this->mId = 0;
148 return;
149 }
150 if ( isset( $wsUserName ) ) {
151 $sName = $wsUserName;
152 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
153 $sName = $HTTP_COOKIE_VARS["wcUserName"];
154 $wsUserName = $sName;
155 } else {
156 $this->mId = 0;
157 return;
158 }
159
160 $passwordCorrect = FALSE;
161 $this->mId = $sId;
162 $this->loadFromDatabase();
163
164 if ( isset( $wsUserPassword ) ) {
165 $passwordCorrect = $wsUserPassword == $this->mPassword;
166 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
167 $this->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
168 $wsUserPassword = $this->addSalt( $this->mCookiePassword );
169 $passwordCorrect = $wsUserPassword == $this->mPassword;
170 } else {
171 $this->mId = 0;
172 $this->loadDefaults(); # Can't log in from session
173 return;
174 }
175
176 if ( ( $sName == $this->mName ) && $passwordCorrect ) {
177 return;
178 }
179 $this->loadDefaults(); # Can't log in from session
180 }
181
182 function loadFromDatabase()
183 {
184 if ( $this->mDataLoaded ) { return; }
185 # check in separate table if there are changes to the talk page
186 $this->mNewtalk=0; # reset talk page status
187 if($this->mId) {
188 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
189 $res = wfQuery ($sql, "User::loadFromDatabase" );
190
191 if (wfNumRows($res)>0) {
192 $this->mNewtalk= 1;
193 }
194 wfFreeResult( $res );
195 } else {
196 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
197 $res = wfQuery ($sql, "User::loadFromDatabase" );
198
199 if (wfNumRows($res)>0) {
200 $this->mNewtalk= 1;
201 }
202 wfFreeResult( $res );
203 }
204 if(!$this->mId) {
205 $this->mDataLoaded = true;
206 return;
207 } # the following stuff is for non-anonymous users only
208
209 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
210 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
211 "{$this->mId}";
212 $res = wfQuery( $sql, "User::loadFromDatabase" );
213
214 if ( wfNumRows( $res ) > 0 ) {
215 $s = wfFetchObject( $res );
216 $this->mName = $s->user_name;
217 $this->mEmail = $s->user_email;
218 $this->mPassword = $s->user_password;
219 $this->mNewpassword = $s->user_newpassword;
220 $this->decodeOptions( $s->user_options );
221 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
222 $this->mTouched = $s->user_touched;
223 }
224
225 wfFreeResult( $res );
226 $this->mDataLoaded = true;
227 }
228
229 function getID() { return $this->mId; }
230 function setID( $v ) {
231 $this->mId = $v;
232 $this->mDataLoaded = false;
233 }
234
235 function getName() {
236 $this->loadFromDatabase();
237 return $this->mName;
238 }
239
240 function setName( $str )
241 {
242 $this->loadFromDatabase();
243 $this->mName = $str;
244 }
245
246 function getNewtalk()
247 {
248 $this->loadFromDatabase();
249 return ( 0 != $this->mNewtalk );
250 }
251
252 function setNewtalk( $val )
253 {
254 $this->loadFromDatabase();
255 $this->mNewtalk = $val;
256 $this->invalidateCache();
257 }
258
259 function invalidateCache() {
260 $this->loadFromDatabase();
261 $this->mTouched = wfTimestampNow();
262 # Don't forget to save the options after this or
263 # it won't take effect!
264 }
265
266 function validateCache( $timestamp ) {
267 $this->loadFromDatabase();
268 return ($timestamp >= $this->mTouched);
269 }
270
271 function getPassword()
272 {
273 $this->loadFromDatabase();
274 return $this->mPassword;
275 }
276
277 function getNewpassword()
278 {
279 $this->loadFromDatabase();
280 return $this->mNewpassword;
281 }
282
283 function addSalt( $p )
284 {
285 return md5( "{$this->mId}-{$p}" );
286 }
287
288 function encryptPassword( $p )
289 {
290 return $this->addSalt( md5( $p ) );
291 }
292
293 function setPassword( $str )
294 {
295 $this->loadFromDatabase();
296 $this->setCookiePassword( $str );
297 $this->mPassword = $this->encryptPassword( $str );
298 $this->mNewpassword = "";
299 }
300
301 function setCookiePassword( $str )
302 {
303 $this->loadFromDatabase();
304 $this->mCookiePassword = md5( $str );
305 }
306
307 function setNewpassword( $str )
308 {
309 $this->loadFromDatabase();
310 $this->mNewpassword = $this->encryptPassword( $str );
311 }
312
313 function getEmail()
314 {
315 $this->loadFromDatabase();
316 return $this->mEmail;
317 }
318
319 function setEmail( $str )
320 {
321 $this->loadFromDatabase();
322 $this->mEmail = $str;
323 }
324
325 function getOption( $oname )
326 {
327 $this->loadFromDatabase();
328 if ( array_key_exists( $oname, $this->mOptions ) ) {
329 return $this->mOptions[$oname];
330 } else {
331 return "";
332 }
333 }
334
335 function setOption( $oname, $val )
336 {
337 $this->loadFromDatabase();
338 $this->mOptions[$oname] = $val;
339 $this->invalidateCache();
340 }
341
342 function getRights()
343 {
344 $this->loadFromDatabase();
345 return $this->mRights;
346 }
347
348 function addRight( $rname )
349 {
350 $this->loadFromDatabase();
351 array_push( $this->mRights, $rname );
352 $this->invalidateCache();
353 }
354
355 function isSysop()
356 {
357 $this->loadFromDatabase();
358 if ( 0 == $this->mId ) { return false; }
359
360 return in_array( "sysop", $this->mRights );
361 }
362
363 function isDeveloper()
364 {
365 $this->loadFromDatabase();
366 if ( 0 == $this->mId ) { return false; }
367
368 return in_array( "developer", $this->mRights );
369 }
370
371 function isBot()
372 {
373 $this->loadFromDatabase();
374 if ( 0 == $this->mId ) { return false; }
375
376 return in_array( "bot", $this->mRights );
377 }
378
379 function &getSkin()
380 {
381 if ( ! isset( $this->mSkin ) ) {
382 $skinNames = Skin::getSkinNames();
383 $s = $this->getOption( "skin" );
384 if ( "" == $s ) { $s = 0; }
385
386 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
387 else $sn = "Skin" . $skinNames[$s];
388 $this->mSkin = new $sn;
389 }
390 return $this->mSkin;
391 }
392
393 function isWatched( $title )
394 {
395 # Note - $title should be a Title _object_
396 # Pages and their talk pages are considered equivalent for watching;
397 # remember that talk namespaces are numbered as page namespace+1.
398 if( $this->mId ) {
399 $sql = "SELECT 1 FROM watchlist
400 WHERE wl_user={$this->mId} AND
401 wl_namespace = " . ($title->getNamespace() & ~1) . " AND
402 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
403 $res = wfQuery( $sql );
404 return (wfNumRows( $res ) > 0);
405 } else {
406 return false;
407 }
408 }
409
410 function addWatch( $title )
411 {
412 if( $this->mId ) {
413 # REPLACE instead of INSERT because occasionally someone
414 # accidentally reloads a watch-add operation.
415 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
416 VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
417 ",'" . wfStrencode( $title->getDBkey() ) . "')";
418 wfQuery( $sql );
419 $this->invalidateCache();
420 }
421 }
422
423 function removeWatch( $title )
424 {
425 if( $this->mId ) {
426 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
427 wl_namespace=" . (($title->getNamespace() | 1) - 1) .
428 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
429 wfQuery( $sql );
430 $this->invalidateCache();
431 }
432 }
433
434
435 /* private */ function encodeOptions()
436 {
437 $a = array();
438 foreach ( $this->mOptions as $oname => $oval ) {
439 array_push( $a, "{$oname}={$oval}" );
440 }
441 $s = implode( "\n", $a );
442 return wfStrencode( $s );
443 }
444
445 /* private */ function decodeOptions( $str )
446 {
447 $a = explode( "\n", $str );
448 foreach ( $a as $s ) {
449 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
450 $this->mOptions[$m[1]] = $m[2];
451 }
452 }
453 }
454
455 function setCookies()
456 {
457 global $wsUserID, $wsUserName, $wsUserPassword;
458 global $wgCookieExpiration;
459 if ( 0 == $this->mId ) return;
460 $this->loadFromDatabase();
461 $exp = time() + $wgCookieExpiration;
462
463 $wsUserID = $this->mId;
464 setcookie( "wcUserID", $this->mId, $exp, "/" );
465
466 $wsUserName = $this->mName;
467 setcookie( "wcUserName", $this->mName, $exp, "/" );
468
469 $wsUserPassword = $this->mPassword;
470 if ( 1 == $this->getOption( "rememberpassword" ) ) {
471 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
472 } else {
473 setcookie( "wcUserPassword", "", time() - 3600 );
474 }
475 }
476
477 function logout()
478 {
479 global $wsUserID;
480 $this->mId = 0;
481
482 $wsUserID = 0;
483
484 setcookie( "wcUserID", "", time() - 3600 );
485 setcookie( "wcUserPassword", "", time() - 3600 );
486 }
487
488 function saveSettings()
489 {
490 global $wgUser;
491
492 if ( ! $this->mNewtalk ) {
493 if( $this->mId ) {
494 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
495 wfQuery ($sql,"User::saveSettings");
496 } else {
497 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
498 wfQuery ($sql,"User::saveSettings");
499 }
500 }
501 if ( 0 == $this->mId ) { return; }
502
503 $sql = "UPDATE user SET " .
504 "user_name= '" . wfStrencode( $this->mName ) . "', " .
505 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
506 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
507 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
508 "user_options= '" . $this->encodeOptions() . "', " .
509 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
510 .
511 "user_touched= '" . wfStrencode( $this->mTouched ) .
512 "' WHERE user_id={$this->mId}";
513 wfQuery( $sql, "User::saveSettings" );
514 }
515
516 # Checks if a user with the given name exists
517 #
518 function idForName()
519 {
520 $gotid = 0;
521 $s = trim( $this->mName );
522 if ( 0 == strcmp( "", $s ) ) return 0;
523
524 $sql = "SELECT user_id FROM user WHERE user_name='" .
525 wfStrencode( $s ) . "'";
526 $res = wfQuery( $sql, "User::idForName" );
527 if ( 0 == wfNumRows( $res ) ) { return 0; }
528
529 $s = wfFetchObject( $res );
530 if ( "" == $s ) return 0;
531
532 $gotid = $s->user_id;
533 wfFreeResult( $res );
534 return $gotid;
535 }
536
537 function addToDatabase()
538 {
539 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
540 "user_email, user_rights, user_options) " .
541 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
542 wfStrencode( $this->mPassword ) . "', '" .
543 wfStrencode( $this->mNewpassword ) . "', '" .
544 wfStrencode( $this->mEmail ) . "', '" .
545 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
546 $this->encodeOptions() . "')";
547 wfQuery( $sql, "User::addToDatabase" );
548 $this->mId = $this->idForName();
549 }
550 }
551
552 ?>