Split image description page handling into ImagePage child class of Article.
[mediawiki.git] / includes / User.php
blobe1dc80eb37b43c2d02c779387f6fa18afebdc4ae
1 <?
2 # See user.doc
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;
13 function User()
15 $this->loadDefaults();
18 # Static factory method
20 function newFromName( $name )
22 $u = new User();
24 # Clean up name according to title rules
26 $t = Title::newFromText( $name );
27 $u->setName( $t->getText() );
28 return $u;
31 /* static */ function whoIs( $id )
33 return wfGetSQL( "user", "user_name", "user_id=$id" );
36 /* static */ function idFromName( $name )
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" );
43 if ( 0 == wfNumRows( $res ) ) { return 0; }
44 else {
45 $s = wfFetchObject( $res );
46 return $s->user_id;
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);
56 /* static */ function randomPassword()
58 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
59 $l = strlen( $pwchars ) - 1;
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;
69 function loadDefaults()
71 global $wgLang ;
72 global $wgNamespacesToBeSearchedDefault;
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;
83 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
84 $this->mOptions["searchNs".$nsnum] = $val;
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 = "";
93 /* private */ function getBlockedStatus()
95 if ( -1 != $this->mBlockedby ) { return; }
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})";
105 $res = wfQuery( $sql, "User::getBlockedStatus" );
106 if ( 0 == wfNumRows( $res ) ) {
107 $this->mBlockedby = 0;
108 return;
110 $s = wfFetchObject( $res );
111 $this->mBlockedby = $s->ipb_by;
112 $this->mBlockreason = $s->ipb_reason;
115 function isBlocked()
117 $this->getBlockedStatus();
118 if ( 0 == $this->mBlockedby ) { return false; }
119 return true;
122 function blockedBy() {
123 $this->getBlockedStatus();
124 return $this->mBlockedby;
127 function blockedFor() {
128 $this->getBlockedStatus();
129 return $this->mBlockreason;
132 /* static */ function loadFromSession()
134 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
135 global $wgMemc, $wgDBname;
137 if ( isset( $wsUserID ) ) {
138 if ( 0 != $wsUserID ) {
139 $sId = $wsUserID;
140 } else {
141 return new User();
143 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
144 $sId = $HTTP_COOKIE_VARS["wcUserID"];
145 $wsUserID = $sId;
146 } else {
147 return new User();
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();
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" );
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
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" );
186 $user->spreadBlock();
187 return $user;
189 return new User(); # Can't log in from session
192 function loadFromDatabase()
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" );
201 if (wfNumRows($res)>0) {
202 $this->mNewtalk= 1;
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" );
213 $this->mNewtalk = (wfNumRows($res)>0) ? 1 : 0;
214 wfFreeResult( $res );
216 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
217 } else {
218 $this->mNewtalk = $newtalk ? 1 : 0;
221 if(!$this->mId) {
222 $this->mDataLoaded = true;
223 return;
224 } # the following stuff is for non-anonymous users only
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" );
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;
242 wfFreeResult( $res );
243 $this->mDataLoaded = true;
246 function getID() { return $this->mId; }
247 function setID( $v ) {
248 $this->mId = $v;
249 $this->mDataLoaded = false;
252 function getName() {
253 $this->loadFromDatabase();
254 return $this->mName;
257 function setName( $str )
259 $this->loadFromDatabase();
260 $this->mName = $str;
263 function getNewtalk()
265 $this->loadFromDatabase();
266 return ( 0 != $this->mNewtalk );
269 function setNewtalk( $val )
271 $this->loadFromDatabase();
272 $this->mNewtalk = $val;
273 $this->invalidateCache();
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!
283 function validateCache( $timestamp ) {
284 $this->loadFromDatabase();
285 return ($timestamp >= $this->mTouched);
288 function getPassword()
290 $this->loadFromDatabase();
291 return $this->mPassword;
294 function getNewpassword()
296 $this->loadFromDatabase();
297 return $this->mNewpassword;
300 function addSalt( $p )
302 global $wgPasswordSalt;
303 if($wgPasswordSalt)
304 return md5( "{$this->mId}-{$p}" );
305 else
306 return $p;
309 function encryptPassword( $p )
311 return $this->addSalt( md5( $p ) );
314 function setPassword( $str )
316 $this->loadFromDatabase();
317 $this->setCookiePassword( $str );
318 $this->mPassword = $this->encryptPassword( $str );
319 $this->mNewpassword = "";
322 function setCookiePassword( $str )
324 $this->loadFromDatabase();
325 $this->mCookiePassword = md5( $str );
328 function setNewpassword( $str )
330 $this->loadFromDatabase();
331 $this->mNewpassword = $this->encryptPassword( $str );
334 function getEmail()
336 $this->loadFromDatabase();
337 return $this->mEmail;
340 function setEmail( $str )
342 $this->loadFromDatabase();
343 $this->mEmail = $str;
346 function getOption( $oname )
348 $this->loadFromDatabase();
349 if ( array_key_exists( $oname, $this->mOptions ) ) {
350 return $this->mOptions[$oname];
351 } else {
352 return "";
356 function setOption( $oname, $val )
358 $this->loadFromDatabase();
359 $this->mOptions[$oname] = $val;
360 $this->invalidateCache();
363 function getRights()
365 $this->loadFromDatabase();
366 return $this->mRights;
369 function addRight( $rname )
371 $this->loadFromDatabase();
372 array_push( $this->mRights, $rname );
373 $this->invalidateCache();
376 function isSysop()
378 $this->loadFromDatabase();
379 if ( 0 == $this->mId ) { return false; }
381 return in_array( "sysop", $this->mRights );
384 function isDeveloper()
386 $this->loadFromDatabase();
387 if ( 0 == $this->mId ) { return false; }
389 return in_array( "developer", $this->mRights );
392 function isBot()
394 $this->loadFromDatabase();
395 if ( 0 == $this->mId ) { return false; }
397 return in_array( "bot", $this->mRights );
400 function &getSkin()
402 if ( ! isset( $this->mSkin ) ) {
403 $skinNames = Skin::getSkinNames();
404 $s = $this->getOption( "skin" );
405 if ( "" == $s ) { $s = 0; }
407 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
408 else $sn = "Skin" . $skinNames[$s];
409 $this->mSkin = new $sn;
411 return $this->mSkin;
414 function isWatched( $title )
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;
431 function addWatch( $title )
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();
444 function removeWatch( $title )
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();
456 /* private */ function encodeOptions()
458 $a = array();
459 foreach ( $this->mOptions as $oname => $oval ) {
460 array_push( $a, "{$oname}={$oval}" );
462 $s = implode( "\n", $a );
463 return wfStrencode( $s );
466 /* private */ function decodeOptions( $str )
468 $a = explode( "\n", $str );
469 foreach ( $a as $s ) {
470 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
471 $this->mOptions[$m[1]] = $m[2];
476 function setCookies()
478 global $wsUserID, $wsUserName, $wsUserPassword;
479 global $wgCookieExpiration;
480 if ( 0 == $this->mId ) return;
481 $this->loadFromDatabase();
482 $exp = time() + $wgCookieExpiration;
484 $wsUserID = $this->mId;
485 setcookie( "wcUserID", $this->mId, $exp, "/" );
487 $wsUserName = $this->mName;
488 setcookie( "wcUserName", $this->mName, $exp, "/" );
490 $wsUserPassword = $this->mPassword;
491 if ( 1 == $this->getOption( "rememberpassword" ) ) {
492 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
493 } else {
494 setcookie( "wcUserPassword", "", time() - 3600 );
498 function logout()
500 global $wsUserID;
501 $this->mId = 0;
503 $wsUserID = 0;
505 setcookie( "wcUserID", "", time() - 3600 );
506 setcookie( "wcUserPassword", "", time() - 3600 );
509 function saveSettings()
511 global $wgMemc, $wgDBname;
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}" );
523 if ( 0 == $this->mId ) { return; }
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 ) ) . "', "
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" );
540 # Checks if a user with the given name exists
542 function idForName()
544 $gotid = 0;
545 $s = trim( $this->mName );
546 if ( 0 == strcmp( "", $s ) ) return 0;
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; }
553 $s = wfFetchObject( $res );
554 if ( "" == $s ) return 0;
556 $gotid = $s->user_id;
557 wfFreeResult( $res );
558 return $gotid;
561 function addToDatabase()
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();
575 function spreadBlock()
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";
581 if ( $this->mId == 0 || !$this->isBlocked()) {
582 return;
585 $sql = "SELECT * FROM ipblocks WHERE ipb_user={$this->mId}";
586 $res = wfQuery( $sql, $fname );
587 if ( wfNumRows( $res ) == 0 ) {
588 return;
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;
599 $row = wfFetchObject( $res );
600 $reason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
601 $reason = str_replace( "$2", $row->ipb_reason, $reason );
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 );
609 wfFreeResult( $res );
610 wfFreeResult( $res2 );