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;
14 $this->loadDefaults();
17 # Static factory method
19 function newFromName( $name )
23 # Clean up name according to title rules
25 $t = Title
::newFromText( $name );
26 $u->setName( $t->getText() );
30 /* static */ function whoIs( $id )
32 return wfGetSQL( "user", "user_name", "user_id=$id" );
35 /* static */ function idFromName( $name )
37 $nt = Title
::newFromText( $name );
38 $sql = "SELECT user_id FROM user WHERE user_name='" .
39 wfStrencode( $nt->getText() ) . "'";
40 $res = wfQuery( $sql, "User::idFromName" );
42 if ( 0 == wfNumRows( $res ) ) { return 0; }
44 $s = wfFetchObject( $res );
49 # does the string match an anonymous user IP address?
50 /* static */ function isIP( $name ) {
51 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
57 /* static */ function randomPassword()
59 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
60 $l = strlen( $pwchars ) - 1;
63 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
64 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
65 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
66 $pwchars{mt_rand( 0, $l )};
70 function loadDefaults()
74 $this->mId
= $this->mNewtalk
= 0;
75 $this->mName
= getenv( "REMOTE_ADDR" );
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 unset( $this->mSkin
);
84 $this->mDataLoaded
= false;
85 $this->mBlockedby
= -1; # Unset
86 $this->mTouched
= '0'; # Allow any pages to be cached
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'";
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;
106 $s = wfFetchObject( $res );
107 $this->mBlockedby
= $s->ipb_by
;
108 $this->mBlockreason
= $s->ipb_reason
;
113 $this->getBlockedStatus();
114 if ( 0 == $this->mBlockedby
) { return false; }
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 ) {
139 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
140 $sId = $HTTP_COOKIE_VARS["wcUserID"];
146 if ( isset( $wsUserName ) ) {
147 $sName = $wsUserName;
148 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
149 $sName = $HTTP_COOKIE_VARS["wcUserName"];
150 $wsUserName = $sName;
155 if ( isset( $wsUserPassword ) ) {
156 $sPass = $wsUserPassword;
157 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
158 $sPass = $HTTP_COOKIE_VARS["wcUserPassword"];
159 $wsUserPassword = $sPass;
165 $this->loadFromDatabase();
167 if ( ( $sName == $this->mName
) &&
168 ( ( $sPass == $this->mPassword
) ||
169 ( $sPass == $this->mNewpassword
) ) ) {
172 $this->loadDefaults(); # Can't log in from session
175 function loadFromDatabase()
177 if ( $this->mDataLoaded
) { return; }
178 # check in separate table if there are changes to the talk page
179 $this->mNewtalk
=0; # reset talk page status
181 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
182 $res = wfQuery ($sql, "User::loadFromDatabase" );
184 if (wfNumRows($res)>0) {
187 wfFreeResult( $res );
189 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
190 $res = wfQuery ($sql, "User::loadFromDatabase" );
192 if (wfNumRows($res)>0) {
195 wfFreeResult( $res );
198 $this->mDataLoaded
= true;
200 } # the following stuff is for non-anonymous users only
202 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
203 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
205 $res = wfQuery( $sql, "User::loadFromDatabase" );
207 if ( wfNumRows( $res ) > 0 ) {
208 $s = wfFetchObject( $res );
209 $this->mName
= $s->user_name
;
210 $this->mEmail
= $s->user_email
;
211 $this->mPassword
= $s->user_password
;
212 $this->mNewpassword
= $s->user_newpassword
;
213 $this->decodeOptions( $s->user_options
);
214 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
215 $this->mTouched
= $s->user_touched
;
218 wfFreeResult( $res );
219 $this->mDataLoaded
= true;
222 function getID() { return $this->mId
; }
223 function setID( $v ) {
225 $this->mDataLoaded
= false;
229 $this->loadFromDatabase();
233 function setName( $str )
235 $this->loadFromDatabase();
239 function getNewtalk()
241 $this->loadFromDatabase();
242 return ( 0 != $this->mNewtalk
);
245 function setNewtalk( $val )
247 $this->loadFromDatabase();
248 $this->mNewtalk
= $val;
249 $this->invalidateCache();
252 function invalidateCache() {
253 $this->loadFromDatabase();
254 $this->mTouched
= wfTimestampNow();
255 # Don't forget to save the options after this or
256 # it won't take effect!
259 function validateCache( $timestamp ) {
260 $this->loadFromDatabase();
261 return ($timestamp >= $this->mTouched
);
264 function getPassword()
266 $this->loadFromDatabase();
267 return $this->mPassword
;
270 function getNewpassword()
272 $this->loadFromDatabase();
273 return $this->mNewpassword
;
276 /* static */ function encryptPassword( $p )
282 function setPassword( $str )
284 $this->loadFromDatabase();
285 $this->mPassword
= User
::encryptPassword( $str );
286 $this->mNewpassword
= "";
289 function setNewpassword( $str )
291 $this->loadFromDatabase();
292 $this->mNewpassword
= User
::encryptPassword( $str );
297 $this->loadFromDatabase();
298 return $this->mEmail
;
301 function setEmail( $str )
303 $this->loadFromDatabase();
304 $this->mEmail
= $str;
307 function getOption( $oname )
309 $this->loadFromDatabase();
310 if ( array_key_exists( $oname, $this->mOptions
) ) {
311 return $this->mOptions
[$oname];
317 function setOption( $oname, $val )
319 $this->loadFromDatabase();
320 $this->mOptions
[$oname] = $val;
321 $this->invalidateCache();
326 $this->loadFromDatabase();
327 return $this->mRights
;
332 $this->loadFromDatabase();
333 if ( 0 == $this->mId
) { return false; }
335 return in_array( "sysop", $this->mRights
);
338 function isDeveloper()
340 $this->loadFromDatabase();
341 if ( 0 == $this->mId
) { return false; }
343 return in_array( "developer", $this->mRights
);
348 $this->loadFromDatabase();
349 if ( 0 == $this->mId
) { return false; }
351 return in_array( "bot", $this->mRights
);
356 if ( ! isset( $this->mSkin
) ) {
357 $skinNames = Skin
::getSkinNames();
358 $s = $this->getOption( "skin" );
359 if ( "" == $s ) { $s = 0; }
361 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
362 else $sn = "Skin" . $skinNames[$s];
363 $this->mSkin
= new $sn;
368 function isWatched( $title )
370 # Note - $title should be a Title _object_
371 # Pages and their talk pages are considered equivalent for watching;
372 # remember that talk namespaces are numbered as page namespace+1.
374 $sql = "SELECT 1 FROM watchlist
375 WHERE wl_user={$this->mId} AND
376 wl_namespace = " . ($title->getNamespace() & ~
1) . " AND
377 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
378 $res = wfQuery( $sql );
379 return (wfNumRows( $res ) > 0);
385 function addWatch( $title )
388 # REPLACE instead of INSERT because occasionally someone
389 # accidentally reloads a watch-add operation.
390 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
391 VALUES ({$this->mId}," . (($title->getNamespace() |
1) - 1) .
392 ",'" . wfStrencode( $title->getDBkey() ) . "')";
394 $this->invalidateCache();
398 function removeWatch( $title )
401 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
402 wl_namespace=" . (($title->getNamespace() |
1) - 1) .
403 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
405 $this->invalidateCache();
410 /* private */ function encodeOptions()
413 foreach ( $this->mOptions
as $oname => $oval ) {
414 array_push( $a, "{$oname}={$oval}" );
416 $s = implode( "\n", $a );
417 return wfStrencode( $s );
420 /* private */ function decodeOptions( $str )
422 $a = explode( "\n", $str );
423 foreach ( $a as $s ) {
424 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
425 $this->mOptions
[$m[1]] = $m[2];
430 function setCookies()
432 global $wsUserID, $wsUserName, $wsUserPassword;
433 global $wgCookieExpiration;
434 if ( 0 == $this->mId
) return;
435 $this->loadFromDatabase();
436 $exp = time() +
$wgCookieExpiration;
438 $wsUserID = $this->mId
;
439 setcookie( "wcUserID", $this->mId
, $exp, "/" );
441 $wsUserName = $this->mName
;
442 setcookie( "wcUserName", $this->mName
, $exp, "/" );
444 $wsUserPassword = $this->mPassword
;
445 if ( 1 == $this->getOption( "rememberpassword" ) ) {
446 setcookie( "wcUserPassword", $this->mPassword
, $exp, "/" );
448 setcookie( "wcUserPassword", "", time() - 3600 );
459 setcookie( "wcUserID", "", time() - 3600 );
460 setcookie( "wcUserPassword", "", time() - 3600 );
463 function saveSettings()
467 if(!$this->mNewtalk
) {
470 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
471 wfQuery ($sql,"User::saveSettings");
475 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
476 wfQuery ($sql,"User::saveSettings");
481 if ( 0 == $this->mId
) { return; }
483 $sql = "UPDATE user SET " .
484 "user_name= '" . wfStrencode( $this->mName
) . "', " .
485 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
486 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
487 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
488 "user_options= '" . $this->encodeOptions() . "', " .
489 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
490 "user_touched= '" . wfStrencode( $this->mTouched
) .
491 "' WHERE user_id={$this->mId}";
492 wfQuery( $sql, "User::saveSettings" );
495 # Checks if a user with the given name exists
500 $s = trim( $this->mName
);
501 if ( 0 == strcmp( "", $s ) ) return 0;
503 $sql = "SELECT user_id FROM user WHERE user_name='" .
504 wfStrencode( $s ) . "'";
505 $res = wfQuery( $sql, "User::idForName" );
506 if ( 0 == wfNumRows( $res ) ) { return 0; }
508 $s = wfFetchObject( $res );
509 if ( "" == $s ) return 0;
511 $gotid = $s->user_id
;
512 wfFreeResult( $res );
516 function addToDatabase()
518 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
519 "user_email, user_rights, user_options) " .
520 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
521 wfStrencode( $this->mPassword
) . "', '" .
522 wfStrencode( $this->mNewpassword
) . "', '" .
523 wfStrencode( $this->mEmail
) . "', '" .
524 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
525 $this->encodeOptions() . "')";
526 wfQuery( $sql, "User::addToDatabase" );
527 $this->mId
= $this->idForName();