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);
56 /* static */ function randomPassword()
58 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
59 $l = strlen( $pwchars ) - 1;
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 )};
69 function loadDefaults()
72 global $wgNamespacesToBeSearchedDefault;
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 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'";
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;
110 $s = wfFetchObject( $res );
111 $this->mBlockedby
= $s->ipb_by
;
112 $this->mBlockreason
= $s->ipb_reason
;
117 $this->getBlockedStatus();
118 if ( 0 == $this->mBlockedby
) { return false; }
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 ) {
143 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
144 $sId = $HTTP_COOKIE_VARS["wcUserID"];
149 if ( isset( $wsUserName ) ) {
150 $sName = $wsUserName;
151 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
152 $sName = $HTTP_COOKIE_VARS["wcUserName"];
153 $wsUserName = $sName;
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" );
164 $user->loadFromDatabase();
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
;
176 return new User(); # Can't log in from session
179 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
181 if($wgMemc->set( $key, $user ))
182 wfDebug( "User::loadFromSession() successfully saved user\n" );
184 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
188 return new User(); # Can't log in from session
191 function loadFromDatabase()
193 if ( $this->mDataLoaded
) { return; }
194 # check in separate table if there are changes to the talk page
195 $this->mNewtalk
=0; # reset talk page status
197 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
198 $res = wfQuery ($sql, "User::loadFromDatabase" );
200 if (wfNumRows($res)>0) {
203 wfFreeResult( $res );
205 global $wgDBname, $wgMemc;
206 $key = "$wgDBname:newtalk:ip:{$this->mName}";
207 $newtalk = $wgMemc->get( $key );
208 if($newtalk === false) {
209 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
210 $res = wfQuery ($sql, "User::loadFromDatabase" );
212 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
213 wfFreeResult( $res );
215 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
217 $this->mNewtalk
= $newtalk ?
1 : 0;
221 $this->mDataLoaded
= true;
223 } # the following stuff is for non-anonymous users only
225 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
226 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
228 $res = wfQuery( $sql, "User::loadFromDatabase" );
230 if ( wfNumRows( $res ) > 0 ) {
231 $s = wfFetchObject( $res );
232 $this->mName
= $s->user_name
;
233 $this->mEmail
= $s->user_email
;
234 $this->mPassword
= $s->user_password
;
235 $this->mNewpassword
= $s->user_newpassword
;
236 $this->decodeOptions( $s->user_options
);
237 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
238 $this->mTouched
= $s->user_touched
;
241 wfFreeResult( $res );
242 $this->mDataLoaded
= true;
245 function getID() { return $this->mId
; }
246 function setID( $v ) {
248 $this->mDataLoaded
= false;
252 $this->loadFromDatabase();
256 function setName( $str )
258 $this->loadFromDatabase();
262 function getNewtalk()
264 $this->loadFromDatabase();
265 return ( 0 != $this->mNewtalk
);
268 function setNewtalk( $val )
270 $this->loadFromDatabase();
271 $this->mNewtalk
= $val;
272 $this->invalidateCache();
275 function invalidateCache() {
276 $this->loadFromDatabase();
277 $this->mTouched
= wfTimestampNow();
278 # Don't forget to save the options after this or
279 # it won't take effect!
282 function validateCache( $timestamp ) {
283 $this->loadFromDatabase();
284 return ($timestamp >= $this->mTouched
);
287 function getPassword()
289 $this->loadFromDatabase();
290 return $this->mPassword
;
293 function getNewpassword()
295 $this->loadFromDatabase();
296 return $this->mNewpassword
;
299 function addSalt( $p )
301 global $wgPasswordSalt;
303 return md5( "{$this->mId}-{$p}" );
308 function encryptPassword( $p )
310 return $this->addSalt( md5( $p ) );
313 function setPassword( $str )
315 $this->loadFromDatabase();
316 $this->setCookiePassword( $str );
317 $this->mPassword
= $this->encryptPassword( $str );
318 $this->mNewpassword
= "";
321 function setCookiePassword( $str )
323 $this->loadFromDatabase();
324 $this->mCookiePassword
= md5( $str );
327 function setNewpassword( $str )
329 $this->loadFromDatabase();
330 $this->mNewpassword
= $this->encryptPassword( $str );
335 $this->loadFromDatabase();
336 return $this->mEmail
;
339 function setEmail( $str )
341 $this->loadFromDatabase();
342 $this->mEmail
= $str;
345 function getOption( $oname )
347 $this->loadFromDatabase();
348 if ( array_key_exists( $oname, $this->mOptions
) ) {
349 return $this->mOptions
[$oname];
355 function setOption( $oname, $val )
357 $this->loadFromDatabase();
358 $this->mOptions
[$oname] = $val;
359 $this->invalidateCache();
364 $this->loadFromDatabase();
365 return $this->mRights
;
368 function addRight( $rname )
370 $this->loadFromDatabase();
371 array_push( $this->mRights
, $rname );
372 $this->invalidateCache();
377 $this->loadFromDatabase();
378 if ( 0 == $this->mId
) { return false; }
380 return in_array( "sysop", $this->mRights
);
383 function isDeveloper()
385 $this->loadFromDatabase();
386 if ( 0 == $this->mId
) { return false; }
388 return in_array( "developer", $this->mRights
);
393 $this->loadFromDatabase();
394 if ( 0 == $this->mId
) { return false; }
396 return in_array( "bot", $this->mRights
);
401 if ( ! isset( $this->mSkin
) ) {
402 $skinNames = Skin
::getSkinNames();
403 $s = $this->getOption( "skin" );
404 if ( "" == $s ) { $s = 0; }
406 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
407 else $sn = "Skin" . $skinNames[$s];
408 $this->mSkin
= new $sn;
413 function isWatched( $title )
415 # Note - $title should be a Title _object_
416 # Pages and their talk pages are considered equivalent for watching;
417 # remember that talk namespaces are numbered as page namespace+1.
419 $sql = "SELECT 1 FROM watchlist
420 WHERE wl_user={$this->mId} AND
421 wl_namespace = " . ($title->getNamespace() & ~
1) . " AND
422 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
423 $res = wfQuery( $sql );
424 return (wfNumRows( $res ) > 0);
430 function addWatch( $title )
433 # REPLACE instead of INSERT because occasionally someone
434 # accidentally reloads a watch-add operation.
435 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
436 VALUES ({$this->mId}," . (($title->getNamespace() |
1) - 1) .
437 ",'" . wfStrencode( $title->getDBkey() ) . "')";
439 $this->invalidateCache();
443 function removeWatch( $title )
446 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
447 wl_namespace=" . (($title->getNamespace() |
1) - 1) .
448 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
450 $this->invalidateCache();
455 /* private */ function encodeOptions()
458 foreach ( $this->mOptions
as $oname => $oval ) {
459 array_push( $a, "{$oname}={$oval}" );
461 $s = implode( "\n", $a );
462 return wfStrencode( $s );
465 /* private */ function decodeOptions( $str )
467 $a = explode( "\n", $str );
468 foreach ( $a as $s ) {
469 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
470 $this->mOptions
[$m[1]] = $m[2];
475 function setCookies()
477 global $wsUserID, $wsUserName, $wsUserPassword;
478 global $wgCookieExpiration;
479 if ( 0 == $this->mId
) return;
480 $this->loadFromDatabase();
481 $exp = time() +
$wgCookieExpiration;
483 $wsUserID = $this->mId
;
484 setcookie( "wcUserID", $this->mId
, $exp, "/" );
486 $wsUserName = $this->mName
;
487 setcookie( "wcUserName", $this->mName
, $exp, "/" );
489 $wsUserPassword = $this->mPassword
;
490 if ( 1 == $this->getOption( "rememberpassword" ) ) {
491 setcookie( "wcUserPassword", $this->mCookiePassword
, $exp, "/" );
493 setcookie( "wcUserPassword", "", time() - 3600 );
504 setcookie( "wcUserID", "", time() - 3600 );
505 setcookie( "wcUserPassword", "", time() - 3600 );
508 function saveSettings()
510 global $wgMemc, $wgDBname;
512 if ( ! $this->mNewtalk
) {
514 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
515 wfQuery ($sql,"User::saveSettings");
517 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
518 wfQuery ($sql,"User::saveSettings");
519 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
522 if ( 0 == $this->mId
) { return; }
524 $sql = "UPDATE user SET " .
525 "user_name= '" . wfStrencode( $this->mName
) . "', " .
526 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
527 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
528 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
529 "user_options= '" . $this->encodeOptions() . "', " .
530 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', "
532 "user_touched= '" . wfStrencode( $this->mTouched
) .
533 "' WHERE user_id={$this->mId}";
534 wfQuery( $sql, "User::saveSettings" );
535 #$wgMemc->replace( "$wgDBname:user:id:$this->mId", $this );
536 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
539 # Checks if a user with the given name exists
544 $s = trim( $this->mName
);
545 if ( 0 == strcmp( "", $s ) ) return 0;
547 $sql = "SELECT user_id FROM user WHERE user_name='" .
548 wfStrencode( $s ) . "'";
549 $res = wfQuery( $sql, "User::idForName" );
550 if ( 0 == wfNumRows( $res ) ) { return 0; }
552 $s = wfFetchObject( $res );
553 if ( "" == $s ) return 0;
555 $gotid = $s->user_id
;
556 wfFreeResult( $res );
560 function addToDatabase()
562 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
563 "user_email, user_rights, user_options) " .
564 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
565 wfStrencode( $this->mPassword
) . "', '" .
566 wfStrencode( $this->mNewpassword
) . "', '" .
567 wfStrencode( $this->mEmail
) . "', '" .
568 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
569 $this->encodeOptions() . "')";
570 wfQuery( $sql, "User::addToDatabase" );
571 $this->mId
= $this->idForName();