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