updates from the French Wikipedia's copy
[mediawiki.git] / includes / User.php
blob691860b30af42399f18702e51e7e6ef211caa016
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 $this->loadDefaults(); # Can't log in from session
169 return;
172 if ( ( $sName == $this->mName ) && $passwordCorrect ) {
173 return;
175 $this->loadDefaults(); # Can't log in from session
178 function loadFromDatabase()
180 if ( $this->mDataLoaded ) { return; }
181 # check in separate table if there are changes to the talk page
182 $this->mNewtalk=0; # reset talk page status
183 if($this->mId) {
184 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
185 $res = wfQuery ($sql, "User::loadFromDatabase" );
187 if (wfNumRows($res)>0) {
188 $this->mNewtalk= 1;
190 wfFreeResult( $res );
191 } else {
192 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
193 $res = wfQuery ($sql, "User::loadFromDatabase" );
195 if (wfNumRows($res)>0) {
196 $this->mNewtalk= 1;
198 wfFreeResult( $res );
200 if(!$this->mId) {
201 $this->mDataLoaded = true;
202 return;
203 } # the following stuff is for non-anonymous users only
205 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
206 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
207 "{$this->mId}";
208 $res = wfQuery( $sql, "User::loadFromDatabase" );
210 if ( wfNumRows( $res ) > 0 ) {
211 $s = wfFetchObject( $res );
212 $this->mName = $s->user_name;
213 $this->mEmail = $s->user_email;
214 $this->mPassword = $s->user_password;
215 $this->mNewpassword = $s->user_newpassword;
216 $this->decodeOptions( $s->user_options );
217 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
218 $this->mTouched = $s->user_touched;
221 wfFreeResult( $res );
222 $this->mDataLoaded = true;
225 function getID() { return $this->mId; }
226 function setID( $v ) {
227 $this->mId = $v;
228 $this->mDataLoaded = false;
231 function getName() {
232 $this->loadFromDatabase();
233 return $this->mName;
236 function setName( $str )
238 $this->loadFromDatabase();
239 $this->mName = $str;
242 function getNewtalk()
244 $this->loadFromDatabase();
245 return ( 0 != $this->mNewtalk );
248 function setNewtalk( $val )
250 $this->loadFromDatabase();
251 $this->mNewtalk = $val;
252 $this->invalidateCache();
255 function invalidateCache() {
256 $this->loadFromDatabase();
257 $this->mTouched = wfTimestampNow();
258 # Don't forget to save the options after this or
259 # it won't take effect!
262 function validateCache( $timestamp ) {
263 $this->loadFromDatabase();
264 return ($timestamp >= $this->mTouched);
267 function getPassword()
269 $this->loadFromDatabase();
270 return $this->mPassword;
273 function getNewpassword()
275 $this->loadFromDatabase();
276 return $this->mNewpassword;
279 function addSalt( $p )
281 return md5( "{$this->mId}-{$p}" );
284 function encryptPassword( $p )
286 return $this->addSalt( md5( $p ) );
289 function setPassword( $str )
291 $this->loadFromDatabase();
292 $this->setCookiePassword( $str );
293 $this->mPassword = $this->encryptPassword( $str );
294 $this->mNewpassword = "";
297 function setCookiePassword( $str )
299 $this->loadFromDatabase();
300 $this->mCookiePassword = md5( $str );
303 function setNewpassword( $str )
305 $this->loadFromDatabase();
306 $this->mNewpassword = $this->encryptPassword( $str );
309 function getEmail()
311 $this->loadFromDatabase();
312 return $this->mEmail;
315 function setEmail( $str )
317 $this->loadFromDatabase();
318 $this->mEmail = $str;
321 function getOption( $oname )
323 $this->loadFromDatabase();
324 if ( array_key_exists( $oname, $this->mOptions ) ) {
325 return $this->mOptions[$oname];
326 } else {
327 return "";
331 function setOption( $oname, $val )
333 $this->loadFromDatabase();
334 $this->mOptions[$oname] = $val;
335 $this->invalidateCache();
338 function getRights()
340 $this->loadFromDatabase();
341 return $this->mRights;
344 function addRight( $rname )
346 $this->loadFromDatabase();
347 array_push( $this->mRights, $rname );
348 $this->invalidateCache();
351 function isSysop()
353 $this->loadFromDatabase();
354 if ( 0 == $this->mId ) { return false; }
356 return in_array( "sysop", $this->mRights );
359 function isDeveloper()
361 $this->loadFromDatabase();
362 if ( 0 == $this->mId ) { return false; }
364 return in_array( "developer", $this->mRights );
367 function isBot()
369 $this->loadFromDatabase();
370 if ( 0 == $this->mId ) { return false; }
372 return in_array( "bot", $this->mRights );
375 function &getSkin()
377 if ( ! isset( $this->mSkin ) ) {
378 $skinNames = Skin::getSkinNames();
379 $s = $this->getOption( "skin" );
380 if ( "" == $s ) { $s = 0; }
382 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
383 else $sn = "Skin" . $skinNames[$s];
384 $this->mSkin = new $sn;
386 return $this->mSkin;
389 function isWatched( $title )
391 # Note - $title should be a Title _object_
392 # Pages and their talk pages are considered equivalent for watching;
393 # remember that talk namespaces are numbered as page namespace+1.
394 if( $this->mId ) {
395 $sql = "SELECT 1 FROM watchlist
396 WHERE wl_user={$this->mId} AND
397 wl_namespace = " . ($title->getNamespace() & ~1) . " AND
398 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
399 $res = wfQuery( $sql );
400 return (wfNumRows( $res ) > 0);
401 } else {
402 return false;
406 function addWatch( $title )
408 if( $this->mId ) {
409 # REPLACE instead of INSERT because occasionally someone
410 # accidentally reloads a watch-add operation.
411 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
412 VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
413 ",'" . wfStrencode( $title->getDBkey() ) . "')";
414 wfQuery( $sql );
415 $this->invalidateCache();
419 function removeWatch( $title )
421 if( $this->mId ) {
422 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
423 wl_namespace=" . (($title->getNamespace() | 1) - 1) .
424 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
425 wfQuery( $sql );
426 $this->invalidateCache();
431 /* private */ function encodeOptions()
433 $a = array();
434 foreach ( $this->mOptions as $oname => $oval ) {
435 array_push( $a, "{$oname}={$oval}" );
437 $s = implode( "\n", $a );
438 return wfStrencode( $s );
441 /* private */ function decodeOptions( $str )
443 $a = explode( "\n", $str );
444 foreach ( $a as $s ) {
445 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
446 $this->mOptions[$m[1]] = $m[2];
451 function setCookies()
453 global $wsUserID, $wsUserName, $wsUserPassword;
454 global $wgCookieExpiration;
455 if ( 0 == $this->mId ) return;
456 $this->loadFromDatabase();
457 $exp = time() + $wgCookieExpiration;
459 $wsUserID = $this->mId;
460 setcookie( "wcUserID", $this->mId, $exp, "/" );
462 $wsUserName = $this->mName;
463 setcookie( "wcUserName", $this->mName, $exp, "/" );
465 $wsUserPassword = $this->mPassword;
466 if ( 1 == $this->getOption( "rememberpassword" ) ) {
467 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
468 } else {
469 setcookie( "wcUserPassword", "", time() - 3600 );
473 function logout()
475 global $wsUserID;
476 $this->mId = 0;
478 $wsUserID = 0;
480 setcookie( "wcUserID", "", time() - 3600 );
481 setcookie( "wcUserPassword", "", time() - 3600 );
484 function saveSettings()
486 global $wgUser;
488 if ( ! $this->mNewtalk ) {
489 if( $this->mId ) {
490 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
491 wfQuery ($sql,"User::saveSettings");
492 } else {
493 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
494 wfQuery ($sql,"User::saveSettings");
497 if ( 0 == $this->mId ) { return; }
499 $sql = "UPDATE user SET " .
500 "user_name= '" . wfStrencode( $this->mName ) . "', " .
501 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
502 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
503 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
504 "user_options= '" . $this->encodeOptions() . "', " .
505 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
507 "user_touched= '" . wfStrencode( $this->mTouched ) .
508 "' WHERE user_id={$this->mId}";
509 wfQuery( $sql, "User::saveSettings" );
512 # Checks if a user with the given name exists
514 function idForName()
516 $gotid = 0;
517 $s = trim( $this->mName );
518 if ( 0 == strcmp( "", $s ) ) return 0;
520 $sql = "SELECT user_id FROM user WHERE user_name='" .
521 wfStrencode( $s ) . "'";
522 $res = wfQuery( $sql, "User::idForName" );
523 if ( 0 == wfNumRows( $res ) ) { return 0; }
525 $s = wfFetchObject( $res );
526 if ( "" == $s ) return 0;
528 $gotid = $s->user_id;
529 wfFreeResult( $res );
530 return $gotid;
533 function addToDatabase()
535 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
536 "user_email, user_rights, user_options) " .
537 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
538 wfStrencode( $this->mPassword ) . "', '" .
539 wfStrencode( $this->mNewpassword ) . "', '" .
540 wfStrencode( $this->mEmail ) . "', '" .
541 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
542 $this->encodeOptions() . "')";
543 wfQuery( $sql, "User::addToDatabase" );
544 $this->mId = $this->idForName();