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;
15 $this->loadDefaults();
18 # Static factory method
20 function newFromName( $name )
24 # Clean up name according to title rules
26 $t = Title
::newFromText( $name );
27 $u->setName( $t->getText() );
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; }
45 $s = wfFetchObject( $res );
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);
58 /* static */ function randomPassword()
60 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
61 $l = strlen( $pwchars ) - 1;
64 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
65 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
66 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
67 $pwchars{mt_rand( 0, $l )};
71 function loadDefaults()
75 $this->mId
= $this->mNewtalk
= 0;
76 $this->mName
= getenv( "REMOTE_ADDR" );
78 $this->mPassword
= $this->mNewpassword
= "";
79 $this->mRights
= array();
80 $defOpt = $wgLang->getDefaultUserOptions() ;
81 foreach ( $defOpt as $oname => $val ) {
82 $this->mOptions
[$oname] = $val;
84 unset( $this->mSkin
);
85 $this->mDataLoaded
= false;
86 $this->mBlockedby
= -1; # Unset
87 $this->mTouched
= '0'; # Allow any pages to be cached
88 $this->cookiePassword
= "";
91 /* private */ function getBlockedStatus()
93 if ( -1 != $this->mBlockedby
) { return; }
95 $remaddr = getenv( "REMOTE_ADDR" );
96 if ( 0 == $this->mId
) {
97 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
98 "ipb_address='$remaddr'";
100 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
101 "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
103 $res = wfQuery( $sql, "User::getBlockedStatus" );
104 if ( 0 == wfNumRows( $res ) ) {
105 $this->mBlockedby
= 0;
108 $s = wfFetchObject( $res );
109 $this->mBlockedby
= $s->ipb_by
;
110 $this->mBlockreason
= $s->ipb_reason
;
115 $this->getBlockedStatus();
116 if ( 0 == $this->mBlockedby
) { return false; }
120 function blockedBy() {
121 $this->getBlockedStatus();
122 return $this->mBlockedby
;
125 function blockedFor() {
126 $this->getBlockedStatus();
127 return $this->mBlockreason
;
130 function loadFromSession()
132 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
134 if ( isset( $wsUserID ) ) {
135 if ( 0 != $wsUserID ) {
141 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
142 $sId = $HTTP_COOKIE_VARS["wcUserID"];
148 if ( isset( $wsUserName ) ) {
149 $sName = $wsUserName;
150 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
151 $sName = $HTTP_COOKIE_VARS["wcUserName"];
152 $wsUserName = $sName;
158 $passwordCorrect = FALSE;
160 $this->loadFromDatabase();
162 if ( isset( $wsUserPassword ) ) {
163 $passwordCorrect = $wsUserPassword == $this->mPassword
;
164 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
165 $this->mCookiePassword
= $HTTP_COOKIE_VARS["wcUserPassword"];
166 $wsUserPassword = $this->addSalt($this->mCookiePassword
);
167 $passwordCorrect = $wsUserPassword == $this->mPassword
;
173 if ( ( $sName == $this->mName
) && $passwordCorrect ) {
176 $this->loadDefaults(); # Can't log in from session
179 function loadFromDatabase()
181 if ( $this->mDataLoaded
) { return; }
182 # check in separate table if there are changes to the talk page
183 $this->mNewtalk
=0; # reset talk page status
185 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
186 $res = wfQuery ($sql, "User::loadFromDatabase" );
188 if (wfNumRows($res)>0) {
191 wfFreeResult( $res );
193 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
194 $res = wfQuery ($sql, "User::loadFromDatabase" );
196 if (wfNumRows($res)>0) {
199 wfFreeResult( $res );
202 $this->mDataLoaded
= true;
204 } # the following stuff is for non-anonymous users only
206 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
207 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
209 $res = wfQuery( $sql, "User::loadFromDatabase" );
211 if ( wfNumRows( $res ) > 0 ) {
212 $s = wfFetchObject( $res );
213 $this->mName
= $s->user_name
;
214 $this->mEmail
= $s->user_email
;
215 $this->mPassword
= $s->user_password
;
216 $this->mNewpassword
= $s->user_newpassword
;
217 $this->decodeOptions( $s->user_options
);
218 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
219 $this->mTouched
= $s->user_touched
;
222 wfFreeResult( $res );
223 $this->mDataLoaded
= true;
226 function getID() { return $this->mId
; }
227 function setID( $v ) {
229 $this->mDataLoaded
= false;
233 $this->loadFromDatabase();
237 function setName( $str )
239 $this->loadFromDatabase();
243 function getNewtalk()
245 $this->loadFromDatabase();
246 return ( 0 != $this->mNewtalk
);
249 function setNewtalk( $val )
251 $this->loadFromDatabase();
252 $this->mNewtalk
= $val;
253 $this->invalidateCache();
256 function invalidateCache() {
257 $this->loadFromDatabase();
258 $this->mTouched
= wfTimestampNow();
259 # Don't forget to save the options after this or
260 # it won't take effect!
263 function validateCache( $timestamp ) {
264 $this->loadFromDatabase();
265 return ($timestamp >= $this->mTouched
);
268 function getPassword()
270 $this->loadFromDatabase();
271 return $this->mPassword
;
274 function getNewpassword()
276 $this->loadFromDatabase();
277 return $this->mNewpassword
;
280 function addSalt( $p )
282 return md5( "wikipedia{$this->mId}-{$p}" );
285 function encryptPassword( $p )
287 return $this->addSalt( md5( $p ) );
290 function setPassword( $str )
292 $this->loadFromDatabase();
293 $this->setCookiePassword( $str );
294 $this->mPassword
= $this->encryptPassword( $str );
295 $this->mNewpassword
= "";
298 function setCookiePassword( $str )
300 $this->loadFromDatabase();
301 $this->mCookiePassword
= md5( $str );
304 function setNewpassword( $str )
306 $this->loadFromDatabase();
307 $this->mNewpassword
= $this->encryptPassword( $str );
312 $this->loadFromDatabase();
313 return $this->mEmail
;
316 function setEmail( $str )
318 $this->loadFromDatabase();
319 $this->mEmail
= $str;
322 function getOption( $oname )
324 $this->loadFromDatabase();
325 if ( array_key_exists( $oname, $this->mOptions
) ) {
326 return $this->mOptions
[$oname];
332 function setOption( $oname, $val )
334 $this->loadFromDatabase();
335 $this->mOptions
[$oname] = $val;
336 $this->invalidateCache();
341 $this->loadFromDatabase();
342 return $this->mRights
;
347 $this->loadFromDatabase();
348 if ( 0 == $this->mId
) { return false; }
350 return in_array( "sysop", $this->mRights
);
353 function isDeveloper()
355 $this->loadFromDatabase();
356 if ( 0 == $this->mId
) { return false; }
358 return in_array( "developer", $this->mRights
);
363 $this->loadFromDatabase();
364 if ( 0 == $this->mId
) { return false; }
366 return in_array( "bot", $this->mRights
);
371 if ( ! isset( $this->mSkin
) ) {
372 $skinNames = Skin
::getSkinNames();
373 $s = $this->getOption( "skin" );
374 if ( "" == $s ) { $s = 0; }
376 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
377 else $sn = "Skin" . $skinNames[$s];
378 $this->mSkin
= new $sn;
383 function isWatched( $title )
385 # Note - $title should be a Title _object_
386 # Pages and their talk pages are considered equivalent for watching;
387 # remember that talk namespaces are numbered as page namespace+1.
389 $sql = "SELECT 1 FROM watchlist
390 WHERE wl_user={$this->mId} AND
391 wl_namespace = " . ($title->getNamespace() & ~
1) . " AND
392 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
393 $res = wfQuery( $sql );
394 return (wfNumRows( $res ) > 0);
400 function addWatch( $title )
403 # REPLACE instead of INSERT because occasionally someone
404 # accidentally reloads a watch-add operation.
405 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
406 VALUES ({$this->mId}," . (($title->getNamespace() |
1) - 1) .
407 ",'" . wfStrencode( $title->getDBkey() ) . "')";
409 $this->invalidateCache();
413 function removeWatch( $title )
416 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
417 wl_namespace=" . (($title->getNamespace() |
1) - 1) .
418 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
420 $this->invalidateCache();
425 /* private */ function encodeOptions()
428 foreach ( $this->mOptions
as $oname => $oval ) {
429 array_push( $a, "{$oname}={$oval}" );
431 $s = implode( "\n", $a );
432 return wfStrencode( $s );
435 /* private */ function decodeOptions( $str )
437 $a = explode( "\n", $str );
438 foreach ( $a as $s ) {
439 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
440 $this->mOptions
[$m[1]] = $m[2];
445 function setCookies()
447 global $wsUserID, $wsUserName, $wsUserPassword;
448 global $wgCookieExpiration;
449 if ( 0 == $this->mId
) return;
450 $this->loadFromDatabase();
451 $exp = time() +
$wgCookieExpiration;
453 $wsUserID = $this->mId
;
454 setcookie( "wcUserID", $this->mId
, $exp, "/" );
456 $wsUserName = $this->mName
;
457 setcookie( "wcUserName", $this->mName
, $exp, "/" );
459 $wsUserPassword = $this->mPassword
;
460 if ( 1 == $this->getOption( "rememberpassword" ) ) {
461 setcookie( "wcUserPassword", $this->mCookiePassword
, $exp, "/" );
463 setcookie( "wcUserPassword", "", time() - 3600 );
474 setcookie( "wcUserID", "", time() - 3600 );
475 setcookie( "wcUserPassword", "", time() - 3600 );
478 function saveSettings()
482 if(!$this->mNewtalk
) {
485 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
486 wfQuery ($sql,"User::saveSettings");
490 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
491 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
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 );
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();