11 require_once( 'WatchedItem.php' );
12 require_once( 'Group.php' );
14 # Number of characters in user_token field
15 define( 'USER_TOKEN_LENGTH', 32 );
25 var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
26 var $mEmailAuthenticationtimestamp;
27 var $mRights, $mOptions;
28 var $mDataLoaded, $mNewpassword;
30 var $mBlockedby, $mBlockreason;
35 /** Array of group id the user belong to */
39 /** Construct using User:loadDefaults() */
41 $this->loadDefaults();
45 * Static factory method
47 * @param string $name Username, validated by Title:newFromText()
49 function newFromName( $name ) {
52 # Clean up name according to title rules
54 $t = Title
::newFromText( $name );
58 $u->setName( $t->getText() );
59 $u->setId( $u->idFromName( $t->getText() ) );
65 * Get username given an id.
66 * @param integer $id Database user id
67 * @return string Nickname of a user
70 function whoIs( $id ) {
71 $dbr =& wfGetDB( DB_SLAVE
);
72 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ) );
76 * Get real username given an id.
77 * @param integer $id Database user id
78 * @return string Realname of a user
81 function whoIsReal( $id ) {
82 $dbr =& wfGetDB( DB_SLAVE
);
83 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ) );
87 * Get database id given a user name
88 * @param string $name Nickname of a user
89 * @return integer|null Database user id (null: if non existent
92 function idFromName( $name ) {
93 $fname = "User::idFromName";
95 $nt = Title
::newFromText( $name );
96 if( is_null( $nt ) ) {
100 $dbr =& wfGetDB( DB_SLAVE
);
101 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), $fname );
103 if ( $s === false ) {
111 * does the string match an anonymous user IP address?
112 * @param string $name Nickname of a user
115 function isIP( $name ) {
116 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
120 * does the string match roughly an email address ?
121 * @param string $addr email address
124 function isValidEmailAddr ( $addr ) {
125 return preg_match( '/^([a-z0-9_.-]+([a-z0-9_.-]+)*\@[a-z0-9_-]+([a-z0-9_.-]+)*([a-z.]{2,})+)$/', strtolower($addr));
129 * probably return a random password
130 * @return string probably a random password
132 * @todo Check what is doing really [AV]
134 function randomPassword() {
135 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
136 $l = strlen( $pwchars ) - 1;
138 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
139 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
140 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
141 $pwchars{mt_rand( 0, $l )};
146 * Set properties to default
147 * Used at construction. It will load per language default settings only
148 * if we have an available language object.
150 function loadDefaults() {
153 $fname = 'User::loadDefaults' . $n;
154 wfProfileIn( $fname );
156 global $wgContLang, $wgIP;
157 global $wgNamespacesToBeSearchedDefault;
160 $this->mNewtalk
= -1;
161 $this->mName
= $wgIP;
162 $this->mRealName
= $this->mEmail
= '';
163 $this->mEmailAuthenticationtimestamp
= 0;
164 $this->mPassword
= $this->mNewpassword
= '';
165 $this->mRights
= array();
166 $this->mGroups
= array();
167 // Getting user defaults only if we have an available language
168 if( isset( $wgContLang ) ) {
169 $this->loadDefaultFromLanguage();
172 foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
173 $this->mOptions
['searchNs'.$nsnum] = $val;
175 unset( $this->mSkin
);
176 $this->mDataLoaded
= false;
177 $this->mBlockedby
= -1; # Unset
178 $this->mTouched
= '0'; # Allow any pages to be cached
179 $this->setToken(); # Random
180 $this->mHash
= false;
181 wfProfileOut( $fname );
185 * Used to load user options from a language.
186 * This is not in loadDefault() cause we sometime create user before having
189 function loadDefaultFromLanguage(){
190 $this->mOptions
= User
::getDefaultOptions();
194 * Combine the language default options with any site-specific options
195 * and add the default language variants.
201 function getDefaultOptions() {
203 * Site defaults will override the global/language defaults
205 global $wgContLang, $wgDefaultUserOptions;
206 $defOpt = $wgDefaultUserOptions +
$wgContLang->getDefaultUserOptions();
209 * default language setting
211 $variant = $wgContLang->getPreferredVariant();
212 $defOpt['variant'] = $variant;
213 $defOpt['language'] = $variant;
219 * Get a given default option value.
226 function getDefaultOption( $opt ) {
227 $defOpts = User
::getDefaultOptions();
228 if( isset( $defOpts[$opt] ) ) {
229 return $defOpts[$opt];
236 * Get blocking information
239 function getBlockedStatus() {
240 global $wgIP, $wgBlockCache, $wgProxyList;
242 if ( -1 != $this->mBlockedby
) { return; }
244 $this->mBlockedby
= 0;
248 $block = new Block();
249 if ( $block->load( $wgIP , $this->mId
) ) {
250 $this->mBlockedby
= $block->mBy
;
251 $this->mBlockreason
= $block->mReason
;
256 if ( !$this->mBlockedby
) {
257 $block = $wgBlockCache->get( $wgIP );
258 if ( $block !== false ) {
259 $this->mBlockedby
= $block->mBy
;
260 $this->mBlockreason
= $block->mReason
;
265 if ( !$this->mBlockedby
) {
266 if ( array_key_exists( $wgIP, $wgProxyList ) ) {
267 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
268 $this->mBlockedby
= "Proxy blocker";
274 * Check if user is blocked
275 * @return bool True if blocked, false otherwise
277 function isBlocked() {
278 $this->getBlockedStatus();
279 if ( 0 === $this->mBlockedby
) { return false; }
284 * Get name of blocker
285 * @return string name of blocker
287 function blockedBy() {
288 $this->getBlockedStatus();
289 return $this->mBlockedby
;
293 * Get blocking reason
294 * @return string Blocking reason
296 function blockedFor() {
297 $this->getBlockedStatus();
298 return $this->mBlockreason
;
302 * Initialise php session
304 function SetupSession() {
305 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
306 if( $wgSessionsInMemcached ) {
307 require_once( 'MemcachedSessions.php' );
308 } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
309 # If it's left on 'user' or another setting from another
310 # application, it will end up failing. Try to recover.
311 ini_set ( 'session.save_handler', 'files' );
313 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
314 session_cache_limiter( 'private, must-revalidate' );
319 * Read datas from session
322 function loadFromSession() {
323 global $wgMemc, $wgDBname;
325 if ( isset( $_SESSION['wsUserID'] ) ) {
326 if ( 0 != $_SESSION['wsUserID'] ) {
327 $sId = $_SESSION['wsUserID'];
331 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
332 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
333 $_SESSION['wsUserID'] = $sId;
337 if ( isset( $_SESSION['wsUserName'] ) ) {
338 $sName = $_SESSION['wsUserName'];
339 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
340 $sName = $_COOKIE["{$wgDBname}UserName"];
341 $_SESSION['wsUserName'] = $sName;
346 $passwordCorrect = FALSE;
347 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
348 if($makenew = !$user) {
349 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
352 $user->loadFromDatabase();
354 wfDebug( "User::loadFromSession() got from cache!\n" );
357 if ( isset( $_SESSION['wsToken'] ) ) {
358 $passwordCorrect = $_SESSION['wsToken'] == $user->mToken
;
359 } else if ( isset( $_COOKIE["{$wgDBname}Token"] ) ) {
360 $passwordCorrect = $user->mToken
== $_COOKIE["{$wgDBname}Token"];
362 return new User(); # Can't log in from session
365 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
367 if($wgMemc->set( $key, $user ))
368 wfDebug( "User::loadFromSession() successfully saved user\n" );
370 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
372 $user->spreadBlock();
375 return new User(); # Can't log in from session
379 * Load a user from the database
381 function loadFromDatabase() {
382 global $wgCommandLineMode, $wgAnonGroupId, $wgLoggedInGroupId;
383 $fname = "User::loadFromDatabase";
384 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
389 $this->mId
= IntVal( $this->mId
);
391 /** Anonymous user */
394 $anong = Group
::newFromId($wgAnonGroupId);
396 wfDebugDieBacktrace("Please update your database schema "
397 ."and populate initial group data from "
398 ."maintenance/archives patches");
399 $anong->loadFromDatabase();
400 $this->mRights
= explode(',', $anong->getRights());
401 $this->mDataLoaded
= true;
403 } # the following stuff is for non-anonymous users only
405 $dbr =& wfGetDB( DB_SLAVE
);
406 $s = $dbr->selectRow( 'user', array( 'user_name','user_password','user_newpassword','user_email',
407 'user_emailauthenticationtimestamp',
408 'user_real_name','user_options','user_touched', 'user_token' ),
409 array( 'user_id' => $this->mId
), $fname );
411 if ( $s !== false ) {
412 $this->mName
= $s->user_name
;
413 $this->mEmail
= $s->user_email
;
414 $this->mEmailAuthenticationtimestamp
= $s->user_emailauthenticationtimestamp
;
415 $this->mRealName
= $s->user_real_name
;
416 $this->mPassword
= $s->user_password
;
417 $this->mNewpassword
= $s->user_newpassword
;
418 $this->decodeOptions( $s->user_options
);
419 $this->mTouched
= wfTimestamp(TS_MW
,$s->user_touched
);
420 $this->mToken
= $s->user_token
;
423 $res = $dbr->select( 'user_groups', array( 'ug_group' ), array( 'ug_user' => $this->mId
) );
425 while($group = $dbr->fetchRow($res)) {
426 $this->mGroups
[] = $group[0];
429 // add the default group for logged in user
430 $this->mGroups
[] = $wgLoggedInGroupId;
432 $this->mRights
= array();
433 // now we merge groups rights to get this user rights
434 foreach($this->mGroups
as $aGroupId) {
435 $g = Group
::newFromId($aGroupId);
436 $g->loadFromDatabase();
437 $this->mRights
= array_merge($this->mRights
, explode(',', $g->getRights()));
440 // array merge duplicate rights which are part of several groups
441 $this->mRights
= array_unique($this->mRights
);
443 $dbr->freeResult($res);
446 $this->mDataLoaded
= true;
449 function getID() { return $this->mId
; }
450 function setID( $v ) {
452 $this->mDataLoaded
= false;
456 $this->loadFromDatabase();
460 function setName( $str ) {
461 $this->loadFromDatabase();
465 function getNewtalk() {
466 $fname = 'User::getNewtalk';
467 $this->loadFromDatabase();
469 # Load the newtalk status if it is unloaded (mNewtalk=-1)
470 if ( $this->mNewtalk
== -1 ) {
471 $this->mNewtalk
=0; # reset talk page status
472 $dbr =& wfGetDB( DB_SLAVE
);
473 extract( $dbr->tableNames( 'watchlist' ) );
475 $sql = "SELECT wl_user FROM $watchlist
476 WHERE wl_title='" . $dbr->strencode( str_replace( ' ', '_', $this->mName
) )."' AND wl_namespace = " . NS_USER_TALK
.
477 " AND wl_user=" . $this->mId
. " AND wl_notificationtimestamp != 0";
478 $res = $dbr->query( $sql,'User::get:Newtalk');
480 if ( $dbr->numRows($res)>0 ) {
483 $dbr->freeResult( $res );
485 global $wgDBname, $wgMemc;
486 $key = "$wgDBname:newtalk:ip:{$this->mName}";
487 $newtalk = $wgMemc->get( $key );
488 if( ! is_integer( $newtalk ) ){
489 extract( $dbr->tableNames( 'watchlist' ) );
490 $sql = "SELECT wl_user FROM $watchlist
491 WHERE wl_title='" . $dbr->strencode( str_replace( ' ', '_', $this->mName
) )."' AND wl_namespace = " . NS_USER_TALK
.
492 " AND wl_user =" . 0 . " AND wl_notificationtimestamp != 0";
493 $res = $dbr->query( $sql,'User::getNewtalk');
495 $this->mNewtalk
= $dbr->numRows( $res ) > 0 ?
1 : 0;
496 $dbr->freeResult( $res );
498 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
500 $this->mNewtalk
= $newtalk ?
1 : 0;
505 return ( 0 != $this->mNewtalk
);
508 function setNewtalk( $val ) {
509 $this->loadFromDatabase();
510 $this->mNewtalk
= $val;
511 $this->invalidateCache();
514 function invalidateCache() {
515 $this->loadFromDatabase();
516 $this->mTouched
= wfTimestampNow();
517 # Don't forget to save the options after this or
518 # it won't take effect!
521 function validateCache( $timestamp ) {
522 $this->loadFromDatabase();
523 return ($timestamp >= $this->mTouched
);
528 * Will only be salted if $wgPasswordSalt is true
529 * @param string Password.
530 * @return string Salted password or clear password.
532 function addSalt( $p ) {
533 global $wgPasswordSalt;
535 return md5( "{$this->mId}-{$p}" );
541 * Encrypt a password.
542 * It can eventuall salt a password @see User::addSalt()
543 * @param string $p clear Password.
544 * @param string Encrypted password.
546 function encryptPassword( $p ) {
547 return $this->addSalt( md5( $p ) );
550 # Set the password and reset the random token
551 function setPassword( $str ) {
552 $this->loadFromDatabase();
554 $this->mPassword
= $this->encryptPassword( $str );
555 $this->mNewpassword
= '';
558 # Set the random token (used for persistent authentication)
559 function setToken( $token = false ) {
562 # Take random data from PRNG
563 # This is reasonably secure if the PRNG has been seeded correctly
564 for ($i = 0; $i<USER_TOKEN_LENGTH
/ 4; $i++
) {
565 $this->mToken
.= sprintf( "%04X", mt_rand( 0, 65535 ) );
568 $this->mToken
= $token;
573 function setCookiePassword( $str ) {
574 $this->loadFromDatabase();
575 $this->mCookiePassword
= md5( $str );
578 function setNewpassword( $str ) {
579 $this->loadFromDatabase();
580 $this->mNewpassword
= $this->encryptPassword( $str );
583 function getEmail() {
584 $this->loadFromDatabase();
585 return $this->mEmail
;
588 function getEmailAuthenticationtimestamp() {
589 $this->loadFromDatabase();
590 return $this->mEmailAuthenticationtimestamp
;
593 function setEmail( $str ) {
594 $this->loadFromDatabase();
595 $this->mEmail
= $str;
598 function getRealName() {
599 $this->loadFromDatabase();
600 return $this->mRealName
;
603 function setRealName( $str ) {
604 $this->loadFromDatabase();
605 $this->mRealName
= $str;
608 function getOption( $oname ) {
609 $this->loadFromDatabase();
610 if ( array_key_exists( $oname, $this->mOptions
) ) {
611 return $this->mOptions
[$oname];
617 function setOption( $oname, $val ) {
618 $this->loadFromDatabase();
619 if ( $oname == 'skin' ) {
620 # Clear cached skin, so the new one displays immediately in Special:Preferences
621 unset( $this->mSkin
);
623 $this->mOptions
[$oname] = $val;
624 $this->invalidateCache();
627 function getRights() {
628 $this->loadFromDatabase();
629 return $this->mRights
;
632 function addRight( $rname ) {
633 $this->loadFromDatabase();
634 array_push( $this->mRights
, $rname );
635 $this->invalidateCache();
638 function getGroups() {
639 $this->loadFromDatabase();
640 return $this->mGroups
;
643 function setGroups($groups) {
644 $this->loadFromDatabase();
645 $this->mGroups
= $groups;
646 $this->invalidateCache();
650 * Check if a user is sysop
651 * Die with backtrace. Use User:isAllowed() instead.
656 $this->loadFromDatabase();
657 if ( 0 == $this->mId ) { return false; }
659 return in_array( 'sysop', $this->mRights );
661 wfDebugDieBacktrace("User::isSysop() is deprecated. Use User::isAllowed() instead");
665 function isDeveloper() {
667 $this->loadFromDatabase();
668 if ( 0 == $this->mId ) { return false; }
670 return in_array( 'developer', $this->mRights );
672 wfDebugDieBacktrace("User::isDeveloper() is deprecated. Use User::isAllowed() instead");
676 function isBureaucrat() {
678 $this->loadFromDatabase();
679 if ( 0 == $this->mId ) { return false; }
681 return in_array( 'bureaucrat', $this->mRights );
683 wfDebugDieBacktrace("User::isBureaucrat() is deprecated. Use User::isAllowed() instead");
687 * Whether the user is a bot
688 * @todo need to be migrated to the new user level management sytem
691 $this->loadFromDatabase();
693 # Why was this here? I need a UID=0 conversion script [TS]
694 # if ( 0 == $this->mId ) { return false; }
696 return in_array( 'bot', $this->mRights
);
700 * Check if user is allowed to access a feature / make an action
701 * @param string $action Action to be checked (see $wgAvailableRights in Defines.php for possible actions).
702 * @return boolean True: action is allowed, False: action should not be allowed
704 function isAllowed($action='') {
705 $this->loadFromDatabase();
706 return in_array( $action , $this->mRights
);
710 * Load a skin if it doesn't exist or return it
711 * @todo FIXME : need to check the old failback system [AV]
713 function &getSkin() {
715 if ( ! isset( $this->mSkin
) ) {
716 $fname = 'User::getSkin';
717 wfProfileIn( $fname );
719 # get all skin names available
720 $skinNames = Skin
::getSkinNames();
723 $userSkin = $this->getOption( 'skin' );
724 if ( $userSkin == '' ) { $userSkin = 'standard'; }
726 if ( !isset( $skinNames[$userSkin] ) ) {
727 # in case the user skin could not be found find a replacement
732 # if phptal is enabled we should have monobook skin that
733 # superseed the good old SkinStandard.
734 if ( isset( $skinNames['monobook'] ) ) {
735 $fallback[0] = 'MonoBook';
738 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
739 $sn = $fallback[$userSkin];
744 # The user skin is available
745 $sn = $skinNames[$userSkin];
748 # Grab the skin class and initialise it. Each skin checks for PHPTal
749 # and will not load if it's not enabled.
750 require_once( $IP.'/skins/'.$sn.'.php' );
752 # Check if we got if not failback to default skin
753 $className = 'Skin'.$sn;
754 if( !class_exists( $className ) ) {
755 # DO NOT die if the class isn't found. This breaks maintenance
756 # scripts and can cause a user account to be unrecoverable
757 # except by SQL manipulation if a previously valid skin name
758 # is no longer valid.
759 $className = 'SkinStandard';
760 require_once( $IP.'/skins/Standard.php' );
762 $this->mSkin
=& new $className;
763 wfProfileOut( $fname );
769 * @param string $title Article title to look at
773 * Check watched status of an article
774 * @return bool True if article is watched
776 function isWatched( $title ) {
777 $wl = WatchedItem
::fromUserTitle( $this, $title );
778 return $wl->isWatched();
784 function addWatch( $title ) {
785 $wl = WatchedItem
::fromUserTitle( $this, $title );
787 $this->invalidateCache();
791 * Stop watching an article
793 function removeWatch( $title ) {
794 $wl = WatchedItem
::fromUserTitle( $this, $title );
796 $this->invalidateCache();
800 * Clear the user's notification timestamp for the given title.
801 * If e-notif e-mails are on, they will receive notification mails on
802 * the next change of the page if it's watched etc.
804 function clearNotification( $title ) {
805 $dbw =& wfGetDB( DB_MASTER
);
806 $success = $dbw->update( 'watchlist',
808 'wl_notificationtimestamp' => 0
809 ), array( /* WHERE */
810 'wl_title' => $title->getDBkey(),
811 'wl_namespace' => $title->getNamespace(),
812 'wl_user' => $this->getId()
813 ), 'User::clearLastVisited'
820 * Resets all of the given user's page-change notification timestamps.
821 * If e-notif e-mails are on, they will receive notification mails on
822 * the next change of any watched page.
824 * @param int $currentUser user ID number
827 function clearAllNotifications( $currentUser ) {
828 if( $currentUser != 0 ) {
830 $dbw =& wfGetDB( DB_MASTER
);
831 $success = $dbw->update( 'watchlist',
833 'wl_notificationtimestamp' => 0
834 ), array( /* WHERE */
835 'wl_user' => $currentUser
836 ), 'UserMailer::clearAll'
839 # we also need to clear here the "you have new message" notification for the own user_talk page
840 # This is cleared one page view later in Article::viewUpdates();
846 * @return string Encoding options
848 function encodeOptions() {
850 foreach ( $this->mOptions
as $oname => $oval ) {
851 array_push( $a, $oname.'='.$oval );
853 $s = implode( "\n", $a );
860 function decodeOptions( $str ) {
861 $a = explode( "\n", $str );
862 foreach ( $a as $s ) {
863 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
864 $this->mOptions
[$m[1]] = $m[2];
869 function setCookies() {
870 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
871 if ( 0 == $this->mId
) return;
872 $this->loadFromDatabase();
873 $exp = time() +
$wgCookieExpiration;
875 $_SESSION['wsUserID'] = $this->mId
;
876 setcookie( $wgDBname.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
878 $_SESSION['wsUserName'] = $this->mName
;
879 setcookie( $wgDBname.'UserName', $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
881 $_SESSION['wsToken'] = $this->mToken
;
882 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
883 setcookie( $wgDBname.'Token', $this->mToken
, $exp, $wgCookiePath, $wgCookieDomain );
885 setcookie( $wgDBname.'Token', '', time() - 3600 );
891 * It will clean the session cookie
894 global $wgCookiePath, $wgCookieDomain, $wgDBname, $wgIP;
895 $this->loadDefaults();
896 $this->setLoaded( true );
898 $_SESSION['wsUserID'] = 0;
900 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
901 setcookie( $wgDBname.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
905 * Save object settings into database
907 function saveSettings() {
908 global $wgMemc, $wgDBname;
909 $fname = 'User::saveSettings';
911 $dbw =& wfGetDB( DB_MASTER
);
912 if ( ! $this->getNewtalk() ) {
913 # Delete the watchlist entry for user_talk page X watched by user X
915 $dbw->delete( 'watchlist', array( 'wl_user' => $this->mId
, 'wl_title' => str_replace('_', ' ', $this->mName
) ,'wl_namespace' => NS_USER_TALK
), $fname );
917 $dbw->delete( 'watchlist', array( 'wl_user' => 0, 'wl_title' => $this->mName
, 'wl_namespace' => NS_USER_TALK
), $fname );
918 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
922 if ( 0 == $this->mId
) { return; }
924 $dbw->update( 'user',
926 'user_name' => $this->mName
,
927 'user_password' => $this->mPassword
,
928 'user_newpassword' => $this->mNewpassword
,
929 'user_real_name' => $this->mRealName
,
930 'user_email' => $this->mEmail
,
931 'user_emailauthenticationtimestamp' => $this->mEmailAuthenticationtimestamp
,
932 'user_options' => $this->encodeOptions(),
933 'user_touched' => $dbw->timestamp($this->mTouched
),
934 'user_token' => $this->mToken
935 ), array( /* WHERE */
936 'user_id' => $this->mId
939 $dbw->set( 'user_rights', 'ur_rights', implode( ',', $this->mRights
),
940 'ur_user='. $this->mId
, $fname );
941 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
944 $dbw->delete( 'user_groups', array( 'ug_user' => $this->mId
), $fname);
947 foreach ($this->mGroups
as $group) {
948 $dbw->replace( 'user_groups',
949 array(array('ug_user','ug_group')),
951 'ug_user' => $this->mId
,
960 * Checks if a user with the given name exists, returns the ID
962 function idForName() {
963 $fname = 'User::idForName';
966 $s = trim( $this->mName
);
967 if ( 0 == strcmp( '', $s ) ) return 0;
969 $dbr =& wfGetDB( DB_SLAVE
);
970 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
971 if ( $id === false ) {
978 * Add user object to the database
980 function addToDatabase() {
981 $fname = 'User::addToDatabase';
982 $dbw =& wfGetDB( DB_MASTER
);
983 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
984 $dbw->insert( 'user',
986 'user_id' => $seqVal,
987 'user_name' => $this->mName
,
988 'user_password' => $this->mPassword
,
989 'user_newpassword' => $this->mNewpassword
,
990 'user_email' => $this->mEmail
,
991 'user_emailauthenticationtimestamp' => $this->mEmailAuthenticationtimestamp
,
992 'user_real_name' => $this->mRealName
,
993 'user_options' => $this->encodeOptions(),
994 'user_token' => $this->mToken
997 $this->mId
= $dbw->insertId();
998 $dbw->insert( 'user_rights',
1000 'ur_user' => $this->mId
,
1001 'ur_rights' => implode( ',', $this->mRights
)
1005 foreach ($this->mGroups
as $group) {
1006 $dbw->insert( 'user_groups',
1008 'ug_user' => $this->mId
,
1009 'ug_group' => $group
1015 function spreadBlock() {
1017 # If the (non-anonymous) user is blocked, this function will block any IP address
1018 # that they successfully log on from.
1019 $fname = 'User::spreadBlock';
1021 wfDebug( "User:spreadBlock()\n" );
1022 if ( $this->mId
== 0 ) {
1026 $userblock = Block
::newFromDB( '', $this->mId
);
1027 if ( !$userblock->isValid() ) {
1031 # Check if this IP address is already blocked
1032 $ipblock = Block
::newFromDB( $wgIP );
1033 if ( $ipblock->isValid() ) {
1034 # Just update the timestamp
1035 $ipblock->updateTimestamp();
1039 # Make a new block object with the desired properties
1040 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
1041 $ipblock->mAddress
= $wgIP;
1042 $ipblock->mUser
= 0;
1043 $ipblock->mBy
= $userblock->mBy
;
1044 $ipblock->mReason
= wfMsg( 'autoblocker', $this->getName(), $userblock->mReason
);
1045 $ipblock->mTimestamp
= wfTimestampNow();
1046 $ipblock->mAuto
= 1;
1047 # If the user is already blocked with an expiry date, we don't
1048 # want to pile on top of that!
1049 if($userblock->mExpiry
) {
1050 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
1052 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
1060 function getPageRenderingHash() {
1063 return $this->mHash
;
1066 // stubthreshold is only included below for completeness,
1067 // it will always be 0 when this function is called by parsercache.
1069 $confstr = $this->getOption( 'math' );
1070 $confstr .= '!' . $this->getOption( 'highlightbroken' );
1071 $confstr .= '!' . $this->getOption( 'stubthreshold' );
1072 $confstr .= '!' . $this->getOption( 'editsection' );
1073 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
1074 $confstr .= '!' . $this->getOption( 'showtoc' );
1075 $confstr .= '!' . $this->getOption( 'date' );
1076 $confstr .= '!' . $this->getOption( 'numberheadings' );
1077 $confstr .= '!' . $this->getOption( 'language' );
1078 // add in language specific options, if any
1079 $extra = $wgContLang->getExtraHashOptions();
1080 foreach( $extra as $e ) {
1081 $confstr .= '!' . $this->getOption( $e );
1084 $this->mHash
= $confstr;
1088 function isAllowedToCreateAccount() {
1089 global $wgWhitelistAccount;
1092 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
1093 foreach ($wgWhitelistAccount as $right => $ok) {
1094 $userHasRight = (!strcmp($right, 'user') ||
in_array($right, $this->getRights()));
1095 $allowed |
= ($ok && $userHasRight);
1101 * Set mDataLoaded, return previous value
1102 * Use this to prevent DB access in command-line scripts or similar situations
1104 function setLoaded( $loaded ) {
1105 return wfSetVar( $this->mDataLoaded
, $loaded );
1108 function getUserPage() {
1109 return Title
::makeTitle( NS_USER
, $this->mName
);
1115 function getMaxID() {
1116 $dbr =& wfGetDB( DB_SLAVE
);
1117 return $dbr->selectField( 'user', 'max(user_id)', false );
1121 * Determine whether the user is a newbie. Newbies are either
1122 * anonymous IPs, or the 1% most recently created accounts.
1123 * Bots and sysops are excluded.
1124 * @return bool True if it is a newbie.
1126 function isNewbie() {
1127 return $this->mId
> User
::getMaxID() * 0.99 && !$this->isSysop() && !$this->isBot() ||
$this->getID() == 0;
1131 * Check to see if the given clear-text password is one of the accepted passwords
1132 * @param string $password User password.
1133 * @return bool True if the given password is correct otherwise False.
1135 function checkPassword( $password ) {
1137 $this->loadFromDatabase();
1139 if( $wgAuth->authenticate( $this->getName(), $password ) ) {
1141 } elseif( $wgAuth->strict() ) {
1142 /* Auth plugin doesn't allow local authentication */
1145 $ep = $this->encryptPassword( $password );
1146 if ( 0 == strcmp( $ep, $this->mPassword
) ) {
1148 } elseif ( ($this->mNewpassword
!= '') && (0 == strcmp( $ep, $this->mNewpassword
)) ) {
1149 $this->mEmailAuthenticationtimestamp
= wfTimestampNow();
1150 $this->mNewpassword
= ''; # use the temporary one-time password only once: clear it now !
1151 $this->saveSettings();
1153 } elseif ( function_exists( 'iconv' ) ) {
1154 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
1155 # Check for this with iconv
1156 /* $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
1157 if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {