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