Some more tweaking of prelim memcached support; shortened keys, added newtalk 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 /* static */ function loadFromSession()
133 {
134 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
135 global $wgMemc, $wgDBname;
136
137 if ( isset( $wsUserID ) ) {
138 if ( 0 != $wsUserID ) {
139 $sId = $wsUserID;
140 } else {
141 return new User();
142 }
143 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
144 $sId = $HTTP_COOKIE_VARS["wcUserID"];
145 $wsUserID = $sId;
146 } else {
147 return new User();
148 }
149 if ( isset( $wsUserName ) ) {
150 $sName = $wsUserName;
151 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
152 $sName = $HTTP_COOKIE_VARS["wcUserName"];
153 $wsUserName = $sName;
154 } else {
155 return new User();
156 }
157
158 $passwordCorrect = FALSE;
159 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
160 if($makenew = !$user) {
161 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
162 $user = new User();
163 $user->mId = $sId;
164 $user->loadFromDatabase();
165 } else {
166 wfDebug( "User::loadFromSession() got from cache!\n" );
167 }
168
169 if ( isset( $wsUserPassword ) ) {
170 $passwordCorrect = $wsUserPassword == $user->mPassword;
171 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
172 $user->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
173 $wsUserPassword = $user->addSalt( $user->mCookiePassword );
174 $passwordCorrect = $wsUserPassword == $user->mPassword;
175 } else {
176 return new User(); # Can't log in from session
177 }
178
179 if ( ( $sName == $user->mName ) && $passwordCorrect ) {
180 if($makenew) {
181 if($wgMemc->set( $key, $user ))
182 wfDebug( "User::loadFromSession() successfully saved user\n" );
183 else
184 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
185 }
186 return $user;
187 }
188 return new User(); # Can't log in from session
189 }
190
191 function loadFromDatabase()
192 {
193 if ( $this->mDataLoaded ) { return; }
194 # check in separate table if there are changes to the talk page
195 $this->mNewtalk=0; # reset talk page status
196 if($this->mId) {
197 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
198 $res = wfQuery ($sql, "User::loadFromDatabase" );
199
200 if (wfNumRows($res)>0) {
201 $this->mNewtalk= 1;
202 }
203 wfFreeResult( $res );
204 } else {
205 global $wgDBname, $wgMemc;
206 $key = "$wgDBname:newtalk:ip:{$this->mName}";
207 $newtalk = $wgMemc->get( $key );
208 if($newtalk === false) {
209 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
210 $res = wfQuery ($sql, "User::loadFromDatabase" );
211
212 $this->mNewtalk = (wfNumRows($res)>0) ? 1 : 0;
213 wfFreeResult( $res );
214
215 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
216 } else {
217 $this->mNewtalk = $newtalk ? 1 : 0;
218 }
219 }
220 if(!$this->mId) {
221 $this->mDataLoaded = true;
222 return;
223 } # the following stuff is for non-anonymous users only
224
225 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
226 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
227 "{$this->mId}";
228 $res = wfQuery( $sql, "User::loadFromDatabase" );
229
230 if ( wfNumRows( $res ) > 0 ) {
231 $s = wfFetchObject( $res );
232 $this->mName = $s->user_name;
233 $this->mEmail = $s->user_email;
234 $this->mPassword = $s->user_password;
235 $this->mNewpassword = $s->user_newpassword;
236 $this->decodeOptions( $s->user_options );
237 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
238 $this->mTouched = $s->user_touched;
239 }
240
241 wfFreeResult( $res );
242 $this->mDataLoaded = true;
243 }
244
245 function getID() { return $this->mId; }
246 function setID( $v ) {
247 $this->mId = $v;
248 $this->mDataLoaded = false;
249 }
250
251 function getName() {
252 $this->loadFromDatabase();
253 return $this->mName;
254 }
255
256 function setName( $str )
257 {
258 $this->loadFromDatabase();
259 $this->mName = $str;
260 }
261
262 function getNewtalk()
263 {
264 $this->loadFromDatabase();
265 return ( 0 != $this->mNewtalk );
266 }
267
268 function setNewtalk( $val )
269 {
270 $this->loadFromDatabase();
271 $this->mNewtalk = $val;
272 $this->invalidateCache();
273 }
274
275 function invalidateCache() {
276 $this->loadFromDatabase();
277 $this->mTouched = wfTimestampNow();
278 # Don't forget to save the options after this or
279 # it won't take effect!
280 }
281
282 function validateCache( $timestamp ) {
283 $this->loadFromDatabase();
284 return ($timestamp >= $this->mTouched);
285 }
286
287 function getPassword()
288 {
289 $this->loadFromDatabase();
290 return $this->mPassword;
291 }
292
293 function getNewpassword()
294 {
295 $this->loadFromDatabase();
296 return $this->mNewpassword;
297 }
298
299 function addSalt( $p )
300 {
301 global $wgPasswordSalt;
302 if($wgPasswordSalt)
303 return md5( "{$this->mId}-{$p}" );
304 else
305 return $p;
306 }
307
308 function encryptPassword( $p )
309 {
310 return $this->addSalt( md5( $p ) );
311 }
312
313 function setPassword( $str )
314 {
315 $this->loadFromDatabase();
316 $this->setCookiePassword( $str );
317 $this->mPassword = $this->encryptPassword( $str );
318 $this->mNewpassword = "";
319 }
320
321 function setCookiePassword( $str )
322 {
323 $this->loadFromDatabase();
324 $this->mCookiePassword = md5( $str );
325 }
326
327 function setNewpassword( $str )
328 {
329 $this->loadFromDatabase();
330 $this->mNewpassword = $this->encryptPassword( $str );
331 }
332
333 function getEmail()
334 {
335 $this->loadFromDatabase();
336 return $this->mEmail;
337 }
338
339 function setEmail( $str )
340 {
341 $this->loadFromDatabase();
342 $this->mEmail = $str;
343 }
344
345 function getOption( $oname )
346 {
347 $this->loadFromDatabase();
348 if ( array_key_exists( $oname, $this->mOptions ) ) {
349 return $this->mOptions[$oname];
350 } else {
351 return "";
352 }
353 }
354
355 function setOption( $oname, $val )
356 {
357 $this->loadFromDatabase();
358 $this->mOptions[$oname] = $val;
359 $this->invalidateCache();
360 }
361
362 function getRights()
363 {
364 $this->loadFromDatabase();
365 return $this->mRights;
366 }
367
368 function addRight( $rname )
369 {
370 $this->loadFromDatabase();
371 array_push( $this->mRights, $rname );
372 $this->invalidateCache();
373 }
374
375 function isSysop()
376 {
377 $this->loadFromDatabase();
378 if ( 0 == $this->mId ) { return false; }
379
380 return in_array( "sysop", $this->mRights );
381 }
382
383 function isDeveloper()
384 {
385 $this->loadFromDatabase();
386 if ( 0 == $this->mId ) { return false; }
387
388 return in_array( "developer", $this->mRights );
389 }
390
391 function isBot()
392 {
393 $this->loadFromDatabase();
394 if ( 0 == $this->mId ) { return false; }
395
396 return in_array( "bot", $this->mRights );
397 }
398
399 function &getSkin()
400 {
401 if ( ! isset( $this->mSkin ) ) {
402 $skinNames = Skin::getSkinNames();
403 $s = $this->getOption( "skin" );
404 if ( "" == $s ) { $s = 0; }
405
406 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
407 else $sn = "Skin" . $skinNames[$s];
408 $this->mSkin = new $sn;
409 }
410 return $this->mSkin;
411 }
412
413 function isWatched( $title )
414 {
415 # Note - $title should be a Title _object_
416 # Pages and their talk pages are considered equivalent for watching;
417 # remember that talk namespaces are numbered as page namespace+1.
418 if( $this->mId ) {
419 $sql = "SELECT 1 FROM watchlist
420 WHERE wl_user={$this->mId} AND
421 wl_namespace = " . ($title->getNamespace() & ~1) . " AND
422 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
423 $res = wfQuery( $sql );
424 return (wfNumRows( $res ) > 0);
425 } else {
426 return false;
427 }
428 }
429
430 function addWatch( $title )
431 {
432 if( $this->mId ) {
433 # REPLACE instead of INSERT because occasionally someone
434 # accidentally reloads a watch-add operation.
435 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
436 VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
437 ",'" . wfStrencode( $title->getDBkey() ) . "')";
438 wfQuery( $sql );
439 $this->invalidateCache();
440 }
441 }
442
443 function removeWatch( $title )
444 {
445 if( $this->mId ) {
446 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
447 wl_namespace=" . (($title->getNamespace() | 1) - 1) .
448 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
449 wfQuery( $sql );
450 $this->invalidateCache();
451 }
452 }
453
454
455 /* private */ function encodeOptions()
456 {
457 $a = array();
458 foreach ( $this->mOptions as $oname => $oval ) {
459 array_push( $a, "{$oname}={$oval}" );
460 }
461 $s = implode( "\n", $a );
462 return wfStrencode( $s );
463 }
464
465 /* private */ function decodeOptions( $str )
466 {
467 $a = explode( "\n", $str );
468 foreach ( $a as $s ) {
469 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
470 $this->mOptions[$m[1]] = $m[2];
471 }
472 }
473 }
474
475 function setCookies()
476 {
477 global $wsUserID, $wsUserName, $wsUserPassword;
478 global $wgCookieExpiration;
479 if ( 0 == $this->mId ) return;
480 $this->loadFromDatabase();
481 $exp = time() + $wgCookieExpiration;
482
483 $wsUserID = $this->mId;
484 setcookie( "wcUserID", $this->mId, $exp, "/" );
485
486 $wsUserName = $this->mName;
487 setcookie( "wcUserName", $this->mName, $exp, "/" );
488
489 $wsUserPassword = $this->mPassword;
490 if ( 1 == $this->getOption( "rememberpassword" ) ) {
491 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
492 } else {
493 setcookie( "wcUserPassword", "", time() - 3600 );
494 }
495 }
496
497 function logout()
498 {
499 global $wsUserID;
500 $this->mId = 0;
501
502 $wsUserID = 0;
503
504 setcookie( "wcUserID", "", time() - 3600 );
505 setcookie( "wcUserPassword", "", time() - 3600 );
506 }
507
508 function saveSettings()
509 {
510 global $wgMemc, $wgDBname;
511
512 if ( ! $this->mNewtalk ) {
513 if( $this->mId ) {
514 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
515 wfQuery ($sql,"User::saveSettings");
516 } else {
517 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
518 wfQuery ($sql,"User::saveSettings");
519 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
520 }
521 }
522 if ( 0 == $this->mId ) { return; }
523
524 $sql = "UPDATE user SET " .
525 "user_name= '" . wfStrencode( $this->mName ) . "', " .
526 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
527 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
528 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
529 "user_options= '" . $this->encodeOptions() . "', " .
530 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
531 .
532 "user_touched= '" . wfStrencode( $this->mTouched ) .
533 "' WHERE user_id={$this->mId}";
534 wfQuery( $sql, "User::saveSettings" );
535 #$wgMemc->replace( "$wgDBname:user:id:$this->mId", $this );
536 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
537 }
538
539 # Checks if a user with the given name exists
540 #
541 function idForName()
542 {
543 $gotid = 0;
544 $s = trim( $this->mName );
545 if ( 0 == strcmp( "", $s ) ) return 0;
546
547 $sql = "SELECT user_id FROM user WHERE user_name='" .
548 wfStrencode( $s ) . "'";
549 $res = wfQuery( $sql, "User::idForName" );
550 if ( 0 == wfNumRows( $res ) ) { return 0; }
551
552 $s = wfFetchObject( $res );
553 if ( "" == $s ) return 0;
554
555 $gotid = $s->user_id;
556 wfFreeResult( $res );
557 return $gotid;
558 }
559
560 function addToDatabase()
561 {
562 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
563 "user_email, user_rights, user_options) " .
564 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
565 wfStrencode( $this->mPassword ) . "', '" .
566 wfStrencode( $this->mNewpassword ) . "', '" .
567 wfStrencode( $this->mEmail ) . "', '" .
568 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
569 $this->encodeOptions() . "')";
570 wfQuery( $sql, "User::addToDatabase" );
571 $this->mId = $this->idForName();
572 }
573 }
574
575 ?>