Support for [[Wikipedia:Book sources]] (or localized text), content of
[mediawiki.git] / includes / User.php
blob4b81b35eb018b5e9f580ab0386a280dd6dc5f4c3
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 ;
73 $this->mId = $this->mNewtalk = 0;
74 $this->mName = getenv( "REMOTE_ADDR" );
75 $this->mEmail = "";
76 $this->mPassword = $this->mNewpassword = "";
77 $this->mRights = array();
78 $defOpt = $wgLang->getDefaultUserOptions() ;
79 foreach ( $defOpt as $oname => $val ) {
80 $this->mOptions[$oname] = $val;
82 unset( $this->mSkin );
83 $this->mDataLoaded = false;
84 $this->mBlockedby = -1; # Unset
85 $this->mTouched = '0'; # Allow any pages to be cached
86 $this->cookiePassword = "";
89 /* private */ function getBlockedStatus()
91 if ( -1 != $this->mBlockedby ) { return; }
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})";
101 $res = wfQuery( $sql, "User::getBlockedStatus" );
102 if ( 0 == wfNumRows( $res ) ) {
103 $this->mBlockedby = 0;
104 return;
106 $s = wfFetchObject( $res );
107 $this->mBlockedby = $s->ipb_by;
108 $this->mBlockreason = $s->ipb_reason;
111 function isBlocked()
113 $this->getBlockedStatus();
114 if ( 0 == $this->mBlockedby ) { return false; }
115 return true;
118 function blockedBy() {
119 $this->getBlockedStatus();
120 return $this->mBlockedby;
123 function blockedFor() {
124 $this->getBlockedStatus();
125 return $this->mBlockreason;
128 function loadFromSession()
130 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
132 if ( isset( $wsUserID ) ) {
133 if ( 0 != $wsUserID ) {
134 $sId = $wsUserID;
135 } else {
136 $this->mId = 0;
137 return;
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;
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;
156 $passwordCorrect = FALSE;
157 $this->mId = $sId;
158 $this->loadFromDatabase();
160 if ( isset( $wsUserPassword ) ) {
161 $passwordCorrect = $wsUserPassword == $this->mPassword;
162 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
163 $this->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
164 $wsUserPassword = $this->addSalt( $this->mCookiePassword );
165 $passwordCorrect = $wsUserPassword == $this->mPassword;
166 } else {
167 $this->mId = 0;
168 return;
171 if ( ( $sName == $this->mName ) && $passwordCorrect ) {
172 return;
174 $this->loadDefaults(); # Can't log in from session
177 function loadFromDatabase()
179 if ( $this->mDataLoaded ) { return; }
180 # check in separate table if there are changes to the talk page
181 $this->mNewtalk=0; # reset talk page status
182 if($this->mId) {
183 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
184 $res = wfQuery ($sql, "User::loadFromDatabase" );
186 if (wfNumRows($res)>0) {
187 $this->mNewtalk= 1;
189 wfFreeResult( $res );
190 } else {
191 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
192 $res = wfQuery ($sql, "User::loadFromDatabase" );
194 if (wfNumRows($res)>0) {
195 $this->mNewtalk= 1;
197 wfFreeResult( $res );
199 if(!$this->mId) {
200 $this->mDataLoaded = true;
201 return;
202 } # the following stuff is for non-anonymous users only
204 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
205 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
206 "{$this->mId}";
207 $res = wfQuery( $sql, "User::loadFromDatabase" );
209 if ( wfNumRows( $res ) > 0 ) {
210 $s = wfFetchObject( $res );
211 $this->mName = $s->user_name;
212 $this->mEmail = $s->user_email;
213 $this->mPassword = $s->user_password;
214 $this->mNewpassword = $s->user_newpassword;
215 $this->decodeOptions( $s->user_options );
216 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
217 $this->mTouched = $s->user_touched;
220 wfFreeResult( $res );
221 $this->mDataLoaded = true;
224 function getID() { return $this->mId; }
225 function setID( $v ) {
226 $this->mId = $v;
227 $this->mDataLoaded = false;
230 function getName() {
231 $this->loadFromDatabase();
232 return $this->mName;
235 function setName( $str )
237 $this->loadFromDatabase();
238 $this->mName = $str;
241 function getNewtalk()
243 $this->loadFromDatabase();
244 return ( 0 != $this->mNewtalk );
247 function setNewtalk( $val )
249 $this->loadFromDatabase();
250 $this->mNewtalk = $val;
251 $this->invalidateCache();
254 function invalidateCache() {
255 $this->loadFromDatabase();
256 $this->mTouched = wfTimestampNow();
257 # Don't forget to save the options after this or
258 # it won't take effect!
261 function validateCache( $timestamp ) {
262 $this->loadFromDatabase();
263 return ($timestamp >= $this->mTouched);
266 function getPassword()
268 $this->loadFromDatabase();
269 return $this->mPassword;
272 function getNewpassword()
274 $this->loadFromDatabase();
275 return $this->mNewpassword;
278 function addSalt( $p )
280 return md5( "{$this->mId}-{$p}" );
283 function encryptPassword( $p )
285 return $this->addSalt( md5( $p ) );
288 function setPassword( $str )
290 $this->loadFromDatabase();
291 $this->setCookiePassword( $str );
292 $this->mPassword = $this->encryptPassword( $str );
293 $this->mNewpassword = "";
296 function setCookiePassword( $str )
298 $this->loadFromDatabase();
299 $this->mCookiePassword = md5( $str );
302 function setNewpassword( $str )
304 $this->loadFromDatabase();
305 $this->mNewpassword = $this->encryptPassword( $str );
308 function getEmail()
310 $this->loadFromDatabase();
311 return $this->mEmail;
314 function setEmail( $str )
316 $this->loadFromDatabase();
317 $this->mEmail = $str;
320 function getOption( $oname )
322 $this->loadFromDatabase();
323 if ( array_key_exists( $oname, $this->mOptions ) ) {
324 return $this->mOptions[$oname];
325 } else {
326 return "";
330 function setOption( $oname, $val )
332 $this->loadFromDatabase();
333 $this->mOptions[$oname] = $val;
334 $this->invalidateCache();
337 function getRights()
339 $this->loadFromDatabase();
340 return $this->mRights;
343 function addRight( $rname )
345 $this->loadFromDatabase();
346 array_push( $this->mRights, $rname );
347 $this->invalidateCache();
350 function isSysop()
352 $this->loadFromDatabase();
353 if ( 0 == $this->mId ) { return false; }
355 return in_array( "sysop", $this->mRights );
358 function isDeveloper()
360 $this->loadFromDatabase();
361 if ( 0 == $this->mId ) { return false; }
363 return in_array( "developer", $this->mRights );
366 function isBot()
368 $this->loadFromDatabase();
369 if ( 0 == $this->mId ) { return false; }
371 return in_array( "bot", $this->mRights );
374 function &getSkin()
376 if ( ! isset( $this->mSkin ) ) {
377 $skinNames = Skin::getSkinNames();
378 $s = $this->getOption( "skin" );
379 if ( "" == $s ) { $s = 0; }
381 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
382 else $sn = "Skin" . $skinNames[$s];
383 $this->mSkin = new $sn;
385 return $this->mSkin;
388 function isWatched( $title )
390 # Note - $title should be a Title _object_
391 # Pages and their talk pages are considered equivalent for watching;
392 # remember that talk namespaces are numbered as page namespace+1.
393 if( $this->mId ) {
394 $sql = "SELECT 1 FROM watchlist
395 WHERE wl_user={$this->mId} AND
396 wl_namespace = " . ($title->getNamespace() & ~1) . " AND
397 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
398 $res = wfQuery( $sql );
399 return (wfNumRows( $res ) > 0);
400 } else {
401 return false;
405 function addWatch( $title )
407 if( $this->mId ) {
408 # REPLACE instead of INSERT because occasionally someone
409 # accidentally reloads a watch-add operation.
410 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
411 VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
412 ",'" . wfStrencode( $title->getDBkey() ) . "')";
413 wfQuery( $sql );
414 $this->invalidateCache();
418 function removeWatch( $title )
420 if( $this->mId ) {
421 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
422 wl_namespace=" . (($title->getNamespace() | 1) - 1) .
423 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
424 wfQuery( $sql );
425 $this->invalidateCache();
430 /* private */ function encodeOptions()
432 $a = array();
433 foreach ( $this->mOptions as $oname => $oval ) {
434 array_push( $a, "{$oname}={$oval}" );
436 $s = implode( "\n", $a );
437 return wfStrencode( $s );
440 /* private */ function decodeOptions( $str )
442 $a = explode( "\n", $str );
443 foreach ( $a as $s ) {
444 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
445 $this->mOptions[$m[1]] = $m[2];
450 function setCookies()
452 global $wsUserID, $wsUserName, $wsUserPassword;
453 global $wgCookieExpiration;
454 if ( 0 == $this->mId ) return;
455 $this->loadFromDatabase();
456 $exp = time() + $wgCookieExpiration;
458 $wsUserID = $this->mId;
459 setcookie( "wcUserID", $this->mId, $exp, "/" );
461 $wsUserName = $this->mName;
462 setcookie( "wcUserName", $this->mName, $exp, "/" );
464 $wsUserPassword = $this->mPassword;
465 if ( 1 == $this->getOption( "rememberpassword" ) ) {
466 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
467 } else {
468 setcookie( "wcUserPassword", "", time() - 3600 );
472 function logout()
474 global $wsUserID;
475 $this->mId = 0;
477 $wsUserID = 0;
479 setcookie( "wcUserID", "", time() - 3600 );
480 setcookie( "wcUserPassword", "", time() - 3600 );
483 function saveSettings()
485 global $wgUser;
487 if ( ! $this->mNewtalk ) {
488 if( $this->mId ) {
489 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
490 wfQuery ($sql,"User::saveSettings");
491 } else {
492 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
493 wfQuery ($sql,"User::saveSettings");
496 if ( 0 == $this->mId ) { return; }
498 $sql = "UPDATE user SET " .
499 "user_name= '" . wfStrencode( $this->mName ) . "', " .
500 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
501 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
502 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
503 "user_options= '" . $this->encodeOptions() . "', " .
504 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
506 "user_touched= '" . wfStrencode( $this->mTouched ) .
507 "' WHERE user_id={$this->mId}";
508 wfQuery( $sql, "User::saveSettings" );
511 # Checks if a user with the given name exists
513 function idForName()
515 $gotid = 0;
516 $s = trim( $this->mName );
517 if ( 0 == strcmp( "", $s ) ) return 0;
519 $sql = "SELECT user_id FROM user WHERE user_name='" .
520 wfStrencode( $s ) . "'";
521 $res = wfQuery( $sql, "User::idForName" );
522 if ( 0 == wfNumRows( $res ) ) { return 0; }
524 $s = wfFetchObject( $res );
525 if ( "" == $s ) return 0;
527 $gotid = $s->user_id;
528 wfFreeResult( $res );
529 return $gotid;
532 function addToDatabase()
534 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
535 "user_email, user_rights, user_options) " .
536 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
537 wfStrencode( $this->mPassword ) . "', '" .
538 wfStrencode( $this->mNewpassword ) . "', '" .
539 wfStrencode( $this->mEmail ) . "', '" .
540 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
541 $this->encodeOptions() . "')";
542 wfQuery( $sql, "User::addToDatabase" );
543 $this->mId = $this->idForName();