7 # Number of characters in user_token field
8 define( 'USER_TOKEN_LENGTH', 32 );
10 # Serialized record version
11 define( 'MW_USER_VERSION', 6 );
13 # Some punctuation to prevent editing from broken text-mangling proxies.
14 define( 'EDIT_TOKEN_SUFFIX', '+\\' );
17 * Thrown by User::setPassword() on error
20 class PasswordError
extends MWException
{
25 * The User object encapsulates all of the user-specific settings (user_id,
26 * name, rights, password, email address, options, last login time). Client
27 * classes use the getXXX() functions to access these fields. These functions
28 * do all the work of determining whether the user is logged in,
29 * whether the requested option can be satisfied from cookies or
30 * whether a database query is needed. Most of the settings needed
31 * for rendering normal pages are set in the cookie to minimize use
37 * A list of default user toggles, i.e. boolean user preferences that are
38 * displayed by Special:Preferences as checkboxes. This list can be
39 * extended via the UserToggles hook or $wgContLang->getExtraUserToggles().
41 static public $mToggles = array(
51 'editsectiononrightclick',
63 'enotifwatchlistpages',
64 'enotifusertalkpages',
67 'shownumberswatching',
83 * List of member variables which are saved to the shared cache (memcached).
84 * Any operation which changes the corresponding database fields must
85 * call a cache-clearing function.
87 static $mCacheVars = array(
99 'mEmailAuthenticated',
101 'mEmailTokenExpires',
110 * Each of these should have a corresponding message of the form "right-$right"
112 static $mCoreRights = array(
153 static $mAllRights = false;
156 * The cache variable declarations
158 var $mId, $mName, $mRealName, $mPassword, $mNewpassword, $mNewpassTime,
159 $mEmail, $mOptions, $mTouched, $mToken, $mEmailAuthenticated,
160 $mEmailToken, $mEmailTokenExpires, $mRegistration, $mGroups;
163 * Whether the cache variables have been loaded
168 * Initialisation data source if mDataLoaded==false. May be one of:
169 * defaults anonymous user initialised from class defaults
170 * name initialise from mName
171 * id initialise from mId
172 * session log in from cookies or session if possible
174 * Use the User::newFrom*() family of functions to set this.
179 * Lazy-initialised variables, invalidated with clearInstanceCache
181 var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mSkin, $mRights,
182 $mBlockreason, $mBlock, $mEffectiveGroups;
185 * Lightweight constructor for anonymous user
186 * Use the User::newFrom* factory functions for other kinds of users
189 $this->clearInstanceCache( 'defaults' );
193 * Load the user table data for this object from the source given by mFrom
196 if ( $this->mDataLoaded
) {
199 wfProfileIn( __METHOD__
);
201 # Set it now to avoid infinite recursion in accessors
202 $this->mDataLoaded
= true;
204 switch ( $this->mFrom
) {
206 $this->loadDefaults();
209 $this->mId
= self
::idFromName( $this->mName
);
211 # Nonexistent user placeholder object
212 $this->loadDefaults( $this->mName
);
221 $this->loadFromSession();
224 throw new MWException( "Unrecognised value for User->mFrom: \"{$this->mFrom}\"" );
226 wfProfileOut( __METHOD__
);
230 * Load user table data given mId
231 * @return false if the ID does not exist, true otherwise
234 function loadFromId() {
236 if ( $this->mId
== 0 ) {
237 $this->loadDefaults();
242 $key = wfMemcKey( 'user', 'id', $this->mId
);
243 $data = $wgMemc->get( $key );
244 if ( !is_array( $data ) ||
$data['mVersion'] < MW_USER_VERSION
) {
245 # Object is expired, load from DB
250 wfDebug( "Cache miss for user {$this->mId}\n" );
252 if ( !$this->loadFromDatabase() ) {
253 # Can't load from ID, user is anonymous
256 $this->saveToCache();
258 wfDebug( "Got user {$this->mId} from cache\n" );
260 foreach ( self
::$mCacheVars as $name ) {
261 $this->$name = $data[$name];
268 * Save user data to the shared cache
270 function saveToCache() {
273 if ( $this->isAnon() ) {
274 // Anonymous users are uncached
278 foreach ( self
::$mCacheVars as $name ) {
279 $data[$name] = $this->$name;
281 $data['mVersion'] = MW_USER_VERSION
;
282 $key = wfMemcKey( 'user', 'id', $this->mId
);
284 $wgMemc->set( $key, $data );
288 * Static factory method for creation from username.
290 * This is slightly less efficient than newFromId(), so use newFromId() if
291 * you have both an ID and a name handy.
293 * @param string $name Username, validated by Title:newFromText()
294 * @param mixed $validate Validate username. Takes the same parameters as
295 * User::getCanonicalName(), except that true is accepted as an alias
296 * for 'valid', for BC.
298 * @return User object, or null if the username is invalid. If the username
299 * is not present in the database, the result will be a user object with
300 * a name, zero user ID and default settings.
303 static function newFromName( $name, $validate = 'valid' ) {
304 if ( $validate === true ) {
307 $name = self
::getCanonicalName( $name, $validate );
308 if ( $name === false ) {
311 # Create unloaded user object
319 static function newFromId( $id ) {
327 * Factory method to fetch whichever user has a given email confirmation code.
328 * This code is generated when an account is created or its e-mail address
331 * If the code is invalid or has expired, returns NULL.
333 * @param string $code
337 static function newFromConfirmationCode( $code ) {
338 $dbr = wfGetDB( DB_SLAVE
);
339 $id = $dbr->selectField( 'user', 'user_id', array(
340 'user_email_token' => md5( $code ),
341 'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
343 if( $id !== false ) {
344 return User
::newFromId( $id );
351 * Create a new user object using data from session or cookies. If the
352 * login credentials are invalid, the result is an anonymous user.
357 static function newFromSession() {
359 $user->mFrom
= 'session';
364 * Create a new user object from a user row.
365 * The row should have all fields from the user table in it.
367 static function newFromRow( $row ) {
369 $user->loadFromRow( $row );
374 * Get username given an id.
375 * @param integer $id Database user id
376 * @return string Nickname of a user
379 static function whoIs( $id ) {
380 $dbr = wfGetDB( DB_SLAVE
);
381 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
385 * Get the real name of a user given their identifier
387 * @param int $id Database user id
388 * @return string Real name of a user
390 static function whoIsReal( $id ) {
391 $dbr = wfGetDB( DB_SLAVE
);
392 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), __METHOD__
);
396 * Get database id given a user name
397 * @param string $name Nickname of a user
398 * @return integer|null Database user id (null: if non existent
401 static function idFromName( $name ) {
402 $nt = Title
::newFromText( $name );
403 if( is_null( $nt ) ) {
407 $dbr = wfGetDB( DB_SLAVE
);
408 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), __METHOD__
);
410 if ( $s === false ) {
418 * Does the string match an anonymous IPv4 address?
420 * This function exists for username validation, in order to reject
421 * usernames which are similar in form to IP addresses. Strings such
422 * as 300.300.300.300 will return true because it looks like an IP
423 * address, despite not being strictly valid.
425 * We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
426 * address because the usemod software would "cloak" anonymous IP
427 * addresses like this, if we allowed accounts like this to be created
428 * new users could get the old edits of these anonymous users.
431 * @param string $name Nickname of a user
434 static function isIP( $name ) {
435 return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name) || IP
::isIPv6($name);
439 * Is the input a valid username?
441 * Checks if the input is a valid username, we don't want an empty string,
442 * an IP address, anything that containins slashes (would mess up subpages),
443 * is longer than the maximum allowed username size or doesn't begin with
446 * @param string $name
450 static function isValidUserName( $name ) {
451 global $wgContLang, $wgMaxNameChars;
454 || User
::isIP( $name )
455 ||
strpos( $name, '/' ) !== false
456 ||
strlen( $name ) > $wgMaxNameChars
457 ||
$name != $wgContLang->ucfirst( $name ) ) {
458 wfDebugLog( 'username', __METHOD__
.
459 ": '$name' invalid due to empty, IP, slash, length, or lowercase" );
463 // Ensure that the name can't be misresolved as a different title,
464 // such as with extra namespace keys at the start.
465 $parsed = Title
::newFromText( $name );
466 if( is_null( $parsed )
467 ||
$parsed->getNamespace()
468 ||
strcmp( $name, $parsed->getPrefixedText() ) ) {
469 wfDebugLog( 'username', __METHOD__
.
470 ": '$name' invalid due to ambiguous prefixes" );
474 // Check an additional blacklist of troublemaker characters.
475 // Should these be merged into the title char list?
476 $unicodeBlacklist = '/[' .
477 '\x{0080}-\x{009f}' . # iso-8859-1 control chars
478 '\x{00a0}' . # non-breaking space
479 '\x{2000}-\x{200f}' . # various whitespace
480 '\x{2028}-\x{202f}' . # breaks and control chars
481 '\x{3000}' . # ideographic space
482 '\x{e000}-\x{f8ff}' . # private use
484 if( preg_match( $unicodeBlacklist, $name ) ) {
485 wfDebugLog( 'username', __METHOD__
.
486 ": '$name' invalid due to blacklisted characters" );
494 * Usernames which fail to pass this function will be blocked
495 * from user login and new account registrations, but may be used
496 * internally by batch processes.
498 * If an account already exists in this form, login will be blocked
499 * by a failure to pass this function.
501 * @param string $name
504 static function isUsableName( $name ) {
505 global $wgReservedUsernames;
507 // Must be a valid username, obviously ;)
508 self
::isValidUserName( $name ) &&
510 // Certain names may be reserved for batch processes.
511 !in_array( $name, $wgReservedUsernames );
515 * Usernames which fail to pass this function will be blocked
516 * from new account registrations, but may be used internally
517 * either by batch processes or by user accounts which have
518 * already been created.
520 * Additional character blacklisting may be added here
521 * rather than in isValidUserName() to avoid disrupting
524 * @param string $name
527 static function isCreatableName( $name ) {
529 self
::isUsableName( $name ) &&
531 // Registration-time character blacklisting...
532 strpos( $name, '@' ) === false;
536 * Is the input a valid password for this user?
538 * @param string $password Desired password
541 function isValidPassword( $password ) {
542 global $wgMinimalPasswordLength, $wgContLang;
545 if( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) )
547 if( $result === false )
550 // Password needs to be long enough, and can't be the same as the username
551 return strlen( $password ) >= $wgMinimalPasswordLength
552 && $wgContLang->lc( $password ) !== $wgContLang->lc( $this->mName
);
556 * Does a string look like an email address?
558 * There used to be a regular expression here, it got removed because it
559 * rejected valid addresses. Actually just check if there is '@' somewhere
560 * in the given address.
562 * @todo Check for RFC 2822 compilance (bug 959)
564 * @param string $addr email address
567 public static function isValidEmailAddr( $addr ) {
569 if( !wfRunHooks( 'isValidEmailAddr', array( $addr, &$result ) ) ) {
573 return strpos( $addr, '@' ) !== false;
577 * Given unvalidated user input, return a canonical username, or false if
578 * the username is invalid.
579 * @param string $name
580 * @param mixed $validate Type of validation to use:
581 * false No validation
582 * 'valid' Valid for batch processes
583 * 'usable' Valid for batch processes and login
584 * 'creatable' Valid for batch processes, login and account creation
586 static function getCanonicalName( $name, $validate = 'valid' ) {
587 # Force usernames to capital
589 $name = $wgContLang->ucfirst( $name );
591 # Reject names containing '#'; these will be cleaned up
592 # with title normalisation, but then it's too late to
594 if( strpos( $name, '#' ) !== false )
597 # Clean up name according to title rules
598 $t = Title
::newFromText( $name );
599 if( is_null( $t ) ) {
603 # Reject various classes of invalid names
604 $name = $t->getText();
606 $name = $wgAuth->getCanonicalName( $t->getText() );
608 switch ( $validate ) {
612 if ( !User
::isValidUserName( $name ) ) {
617 if ( !User
::isUsableName( $name ) ) {
622 if ( !User
::isCreatableName( $name ) ) {
627 throw new MWException( 'Invalid parameter value for $validate in '.__METHOD__
);
633 * Count the number of edits of a user
635 * It should not be static and some day should be merged as proper member function / deprecated -- domas
637 * @param int $uid The user ID to check
641 static function edits( $uid ) {
642 wfProfileIn( __METHOD__
);
643 $dbr = wfGetDB( DB_SLAVE
);
644 // check if the user_editcount field has been initialized
645 $field = $dbr->selectField(
646 'user', 'user_editcount',
647 array( 'user_id' => $uid ),
651 if( $field === null ) { // it has not been initialized. do so.
652 $dbw = wfGetDB( DB_MASTER
);
653 $count = $dbr->selectField(
654 'revision', 'count(*)',
655 array( 'rev_user' => $uid ),
660 array( 'user_editcount' => $count ),
661 array( 'user_id' => $uid ),
667 wfProfileOut( __METHOD__
);
672 * Return a random password. Sourced from mt_rand, so it's not particularly secure.
673 * @todo hash random numbers to improve security, like generateToken()
678 static function randomPassword() {
679 global $wgMinimalPasswordLength;
680 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
681 $l = strlen( $pwchars ) - 1;
683 $pwlength = max( 7, $wgMinimalPasswordLength );
684 $digit = mt_rand(0, $pwlength - 1);
686 for ( $i = 0; $i < $pwlength; $i++
) {
687 $np .= $i == $digit ?
chr( mt_rand(48, 57) ) : $pwchars{ mt_rand(0, $l)};
693 * Set cached properties to default. Note: this no longer clears
694 * uncached lazy-initialised properties. The constructor does that instead.
698 function loadDefaults( $name = false ) {
699 wfProfileIn( __METHOD__
);
701 global $wgCookiePrefix;
704 $this->mName
= $name;
705 $this->mRealName
= '';
706 $this->mPassword
= $this->mNewpassword
= '';
707 $this->mNewpassTime
= null;
709 $this->mOptions
= null; # Defer init
711 if ( isset( $_COOKIE[$wgCookiePrefix.'LoggedOut'] ) ) {
712 $this->mTouched
= wfTimestamp( TS_MW
, $_COOKIE[$wgCookiePrefix.'LoggedOut'] );
714 $this->mTouched
= '0'; # Allow any pages to be cached
717 $this->setToken(); # Random
718 $this->mEmailAuthenticated
= null;
719 $this->mEmailToken
= '';
720 $this->mEmailTokenExpires
= null;
721 $this->mRegistration
= wfTimestamp( TS_MW
);
722 $this->mGroups
= array();
724 wfRunHooks( 'UserLoadDefaults', array( $this, $name ) );
726 wfProfileOut( __METHOD__
);
730 * Initialise php session
731 * @deprecated use wfSetupSession()
733 function SetupSession() {
734 wfDeprecated( __METHOD__
);
739 * Load user data from the session or login cookie. If there are no valid
740 * credentials, initialises the user as an anon.
741 * @return true if the user is logged in, false otherwise
743 private function loadFromSession() {
744 global $wgMemc, $wgCookiePrefix;
747 wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
748 if ( $result !== null ) {
752 if ( isset( $_SESSION['wsUserID'] ) ) {
753 if ( 0 != $_SESSION['wsUserID'] ) {
754 $sId = $_SESSION['wsUserID'];
756 $this->loadDefaults();
759 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserID"] ) ) {
760 $sId = intval( $_COOKIE["{$wgCookiePrefix}UserID"] );
761 $_SESSION['wsUserID'] = $sId;
763 $this->loadDefaults();
766 if ( isset( $_SESSION['wsUserName'] ) ) {
767 $sName = $_SESSION['wsUserName'];
768 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserName"] ) ) {
769 $sName = $_COOKIE["{$wgCookiePrefix}UserName"];
770 $_SESSION['wsUserName'] = $sName;
772 $this->loadDefaults();
776 $passwordCorrect = FALSE;
778 if ( !$this->loadFromId() ) {
779 # Not a valid ID, loadFromId has switched the object to anon for us
783 if ( isset( $_SESSION['wsToken'] ) ) {
784 $passwordCorrect = $_SESSION['wsToken'] == $this->mToken
;
786 } else if ( isset( $_COOKIE["{$wgCookiePrefix}Token"] ) ) {
787 $passwordCorrect = $this->mToken
== $_COOKIE["{$wgCookiePrefix}Token"];
790 # No session or persistent login cookie
791 $this->loadDefaults();
795 if ( ( $sName == $this->mName
) && $passwordCorrect ) {
796 $_SESSION['wsToken'] = $this->mToken
;
797 wfDebug( "Logged in from $from\n" );
800 # Invalid credentials
801 wfDebug( "Can't log in from $from, invalid credentials\n" );
802 $this->loadDefaults();
808 * Load user and user_group data from the database
809 * $this->mId must be set, this is how the user is identified.
811 * @return true if the user exists, false if the user is anonymous
814 function loadFromDatabase() {
816 $this->mId
= intval( $this->mId
);
818 /** Anonymous user */
820 $this->loadDefaults();
824 $dbr = wfGetDB( DB_MASTER
);
825 $s = $dbr->selectRow( 'user', '*', array( 'user_id' => $this->mId
), __METHOD__
);
827 if ( $s !== false ) {
828 # Initialise user table data
829 $this->loadFromRow( $s );
830 $this->mGroups
= null; // deferred
831 $this->getEditCount(); // revalidation for nulls
836 $this->loadDefaults();
842 * Initialise the user object from a row from the user table
844 function loadFromRow( $row ) {
845 $this->mDataLoaded
= true;
847 if ( isset( $row->user_id
) ) {
848 $this->mId
= $row->user_id
;
850 $this->mName
= $row->user_name
;
851 $this->mRealName
= $row->user_real_name
;
852 $this->mPassword
= $row->user_password
;
853 $this->mNewpassword
= $row->user_newpassword
;
854 $this->mNewpassTime
= wfTimestampOrNull( TS_MW
, $row->user_newpass_time
);
855 $this->mEmail
= $row->user_email
;
856 $this->decodeOptions( $row->user_options
);
857 $this->mTouched
= wfTimestamp(TS_MW
,$row->user_touched
);
858 $this->mToken
= $row->user_token
;
859 $this->mEmailAuthenticated
= wfTimestampOrNull( TS_MW
, $row->user_email_authenticated
);
860 $this->mEmailToken
= $row->user_email_token
;
861 $this->mEmailTokenExpires
= wfTimestampOrNull( TS_MW
, $row->user_email_token_expires
);
862 $this->mRegistration
= wfTimestampOrNull( TS_MW
, $row->user_registration
);
863 $this->mEditCount
= $row->user_editcount
;
867 * Load the groups from the database if they aren't already loaded
870 function loadGroups() {
871 if ( is_null( $this->mGroups
) ) {
872 $dbr = wfGetDB( DB_MASTER
);
873 $res = $dbr->select( 'user_groups',
875 array( 'ug_user' => $this->mId
),
877 $this->mGroups
= array();
878 while( $row = $dbr->fetchObject( $res ) ) {
879 $this->mGroups
[] = $row->ug_group
;
885 * Clear various cached data stored in this object.
886 * @param string $reloadFrom Reload user and user_groups table data from a
887 * given source. May be "name", "id", "defaults", "session" or false for
890 function clearInstanceCache( $reloadFrom = false ) {
891 $this->mNewtalk
= -1;
892 $this->mDatePreference
= null;
893 $this->mBlockedby
= -1; # Unset
894 $this->mHash
= false;
896 $this->mRights
= null;
897 $this->mEffectiveGroups
= null;
900 $this->mDataLoaded
= false;
901 $this->mFrom
= $reloadFrom;
906 * Combine the language default options with any site-specific options
907 * and add the default language variants.
908 * Not really private cause it's called by Language class
913 static function getDefaultOptions() {
914 global $wgNamespacesToBeSearchedDefault;
916 * Site defaults will override the global/language defaults
918 global $wgDefaultUserOptions, $wgContLang;
919 $defOpt = $wgDefaultUserOptions +
$wgContLang->getDefaultUserOptionOverrides();
922 * default language setting
924 $variant = $wgContLang->getPreferredVariant( false );
925 $defOpt['variant'] = $variant;
926 $defOpt['language'] = $variant;
928 foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
929 $defOpt['searchNs'.$nsnum] = $val;
935 * Get a given default option value.
940 public static function getDefaultOption( $opt ) {
941 $defOpts = self
::getDefaultOptions();
942 if( isset( $defOpts[$opt] ) ) {
943 return $defOpts[$opt];
950 * Get a list of user toggle names
953 static function getToggles() {
955 $extraToggles = array();
956 wfRunHooks( 'UserToggles', array( &$extraToggles ) );
957 return array_merge( self
::$mToggles, $extraToggles, $wgContLang->getExtraUserToggles() );
962 * Get blocking information
964 * @param bool $bFromSlave Specify whether to check slave or master. To improve performance,
965 * non-critical checks are done against slaves. Check when actually saving should be done against
968 function getBlockedStatus( $bFromSlave = true ) {
969 global $wgEnableSorbs, $wgProxyWhitelist;
971 if ( -1 != $this->mBlockedby
) {
972 wfDebug( "User::getBlockedStatus: already loaded.\n" );
976 wfProfileIn( __METHOD__
);
977 wfDebug( __METHOD__
.": checking...\n" );
979 // Initialize data...
980 // Otherwise something ends up stomping on $this->mBlockedby when
981 // things get lazy-loaded later, causing false positive block hits
982 // due to -1 !== 0. Probably session-related... Nothing should be
983 // overwriting mBlockedby, surely?
986 $this->mBlockedby
= 0;
987 $this->mHideName
= 0;
990 if ($this->isAllowed( 'ipblock-exempt' ) ) {
991 # Exempt from all types of IP-block
996 $this->mBlock
= new Block();
997 $this->mBlock
->fromMaster( !$bFromSlave );
998 if ( $this->mBlock
->load( $ip , $this->mId
) ) {
999 wfDebug( __METHOD__
.": Found block.\n" );
1000 $this->mBlockedby
= $this->mBlock
->mBy
;
1001 $this->mBlockreason
= $this->mBlock
->mReason
;
1002 $this->mHideName
= $this->mBlock
->mHideName
;
1003 if ( $this->isLoggedIn() ) {
1004 $this->spreadBlock();
1007 $this->mBlock
= null;
1008 wfDebug( __METHOD__
.": No block.\n" );
1012 if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
1014 if ( wfIsLocallyBlockedProxy( $ip ) ) {
1015 $this->mBlockedby
= wfMsg( 'proxyblocker' );
1016 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
1020 if ( !$this->mBlockedby
&& $wgEnableSorbs && !$this->getID() ) {
1021 if ( $this->inSorbsBlacklist( $ip ) ) {
1022 $this->mBlockedby
= wfMsg( 'sorbs' );
1023 $this->mBlockreason
= wfMsg( 'sorbsreason' );
1029 wfRunHooks( 'GetBlockedStatus', array( &$this ) );
1031 wfProfileOut( __METHOD__
);
1034 function inSorbsBlacklist( $ip ) {
1035 global $wgEnableSorbs, $wgSorbsUrl;
1037 return $wgEnableSorbs &&
1038 $this->inDnsBlacklist( $ip, $wgSorbsUrl );
1041 function inDnsBlacklist( $ip, $base ) {
1042 wfProfileIn( __METHOD__
);
1048 if ( preg_match( '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $m ) ) {
1050 for ( $i=4; $i>=1; $i-- ) {
1051 $host .= $m[$i] . '.';
1056 $ipList = gethostbynamel( $host );
1059 wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
1062 wfDebug( "Requested $host, not found in $base.\n" );
1066 wfProfileOut( __METHOD__
);
1071 * Is this user subject to rate limiting?
1075 public function isPingLimitable() {
1076 global $wgRateLimitsExcludedGroups;
1077 if( array_intersect( $this->getEffectiveGroups(), $wgRateLimitsExcludedGroups ) ) {
1078 // Deprecated, but kept for backwards-compatibility config
1081 return !$this->isAllowed('noratelimit');
1085 * Primitive rate limits: enforce maximum actions per time period
1086 * to put a brake on flooding.
1088 * Note: when using a shared cache like memcached, IP-address
1089 * last-hit counters will be shared across wikis.
1091 * @return bool true if a rate limiter was tripped
1094 function pingLimiter( $action='edit' ) {
1096 # Call the 'PingLimiter' hook
1098 if( !wfRunHooks( 'PingLimiter', array( &$this, $action, $result ) ) ) {
1102 global $wgRateLimits;
1103 if( !isset( $wgRateLimits[$action] ) ) {
1107 # Some groups shouldn't trigger the ping limiter, ever
1108 if( !$this->isPingLimitable() )
1111 global $wgMemc, $wgRateLimitLog;
1112 wfProfileIn( __METHOD__
);
1114 $limits = $wgRateLimits[$action];
1116 $id = $this->getId();
1120 if( isset( $limits['anon'] ) && $id == 0 ) {
1121 $keys[wfMemcKey( 'limiter', $action, 'anon' )] = $limits['anon'];
1124 if( isset( $limits['user'] ) && $id != 0 ) {
1125 $userLimit = $limits['user'];
1127 if( $this->isNewbie() ) {
1128 if( isset( $limits['newbie'] ) && $id != 0 ) {
1129 $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['newbie'];
1131 if( isset( $limits['ip'] ) ) {
1132 $keys["mediawiki:limiter:$action:ip:$ip"] = $limits['ip'];
1135 if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $ip, $matches ) ) {
1136 $subnet = $matches[1];
1137 $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
1140 // Check for group-specific permissions
1141 // If more than one group applies, use the group with the highest limit
1142 foreach ( $this->getGroups() as $group ) {
1143 if ( isset( $limits[$group] ) ) {
1144 if ( $userLimit === false ||
$limits[$group] > $userLimit ) {
1145 $userLimit = $limits[$group];
1149 // Set the user limit key
1150 if ( $userLimit !== false ) {
1151 wfDebug( __METHOD__
.": effective user limit: $userLimit\n" );
1152 $keys[ wfMemcKey( 'limiter', $action, 'user', $id ) ] = $userLimit;
1156 foreach( $keys as $key => $limit ) {
1157 list( $max, $period ) = $limit;
1158 $summary = "(limit $max in {$period}s)";
1159 $count = $wgMemc->get( $key );
1161 if( $count > $max ) {
1162 wfDebug( __METHOD__
.": tripped! $key at $count $summary\n" );
1163 if( $wgRateLimitLog ) {
1164 @error_log
( wfTimestamp( TS_MW
) . ' ' . wfWikiID() . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
1168 wfDebug( __METHOD__
.": ok. $key at $count $summary\n" );
1171 wfDebug( __METHOD__
.": adding record for $key $summary\n" );
1172 $wgMemc->add( $key, 1, intval( $period ) );
1174 $wgMemc->incr( $key );
1177 wfProfileOut( __METHOD__
);
1182 * Check if user is blocked
1183 * @return bool True if blocked, false otherwise
1185 function isBlocked( $bFromSlave = true ) { // hacked from false due to horrible probs on site
1186 wfDebug( "User::isBlocked: enter\n" );
1187 $this->getBlockedStatus( $bFromSlave );
1188 return $this->mBlockedby
!== 0;
1192 * Check if user is blocked from editing a particular article
1194 function isBlockedFrom( $title, $bFromSlave = false ) {
1195 global $wgBlockAllowsUTEdit;
1196 wfProfileIn( __METHOD__
);
1197 wfDebug( __METHOD__
.": enter\n" );
1199 wfDebug( __METHOD__
.": asking isBlocked()\n" );
1200 $blocked = $this->isBlocked( $bFromSlave );
1201 # If a user's name is suppressed, they cannot make edits anywhere
1202 if ( !$this->mHideName
&& $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
1203 $title->getNamespace() == NS_USER_TALK
) {
1205 wfDebug( __METHOD__
.": self-talk page, ignoring any blocks\n" );
1207 wfProfileOut( __METHOD__
);
1212 * Get name of blocker
1213 * @return string name of blocker
1215 function blockedBy() {
1216 $this->getBlockedStatus();
1217 return $this->mBlockedby
;
1221 * Get blocking reason
1222 * @return string Blocking reason
1224 function blockedFor() {
1225 $this->getBlockedStatus();
1226 return $this->mBlockreason
;
1230 * Get the user ID. Returns 0 if the user is anonymous or nonexistent.
1233 if( $this->mId
=== null and $this->mName
!== null
1234 and User
::isIP( $this->mName
) ) {
1235 // Special case, we know the user is anonymous
1237 } elseif( $this->mId
=== null ) {
1238 // Don't load if this was initialized from an ID
1245 * Set the user and reload all fields according to that ID
1247 function setId( $v ) {
1249 $this->clearInstanceCache( 'id' );
1253 * Get the user name, or the IP for anons
1255 function getName() {
1256 if ( !$this->mDataLoaded
&& $this->mFrom
== 'name' ) {
1257 # Special case optimisation
1258 return $this->mName
;
1261 if ( $this->mName
=== false ) {
1263 $this->mName
= IP
::sanitizeIP( wfGetIP() );
1265 return $this->mName
;
1270 * Set the user name.
1272 * This does not reload fields from the database according to the given
1273 * name. Rather, it is used to create a temporary "nonexistent user" for
1274 * later addition to the database. It can also be used to set the IP
1275 * address for an anonymous user to something other than the current
1278 * User::newFromName() has rougly the same function, when the named user
1281 function setName( $str ) {
1283 $this->mName
= $str;
1287 * Return the title dbkey form of the name, for eg user pages.
1291 function getTitleKey() {
1292 return str_replace( ' ', '_', $this->getName() );
1295 function getNewtalk() {
1298 # Load the newtalk status if it is unloaded (mNewtalk=-1)
1299 if( $this->mNewtalk
=== -1 ) {
1300 $this->mNewtalk
= false; # reset talk page status
1302 # Check memcached separately for anons, who have no
1303 # entire User object stored in there.
1306 $key = wfMemcKey( 'newtalk', 'ip', $this->getName() );
1307 $newtalk = $wgMemc->get( $key );
1308 if( strval( $newtalk ) !== '' ) {
1309 $this->mNewtalk
= (bool)$newtalk;
1311 // Since we are caching this, make sure it is up to date by getting it
1313 $this->mNewtalk
= $this->checkNewtalk( 'user_ip', $this->getName(), true );
1314 $wgMemc->set( $key, (int)$this->mNewtalk
, 1800 );
1317 $this->mNewtalk
= $this->checkNewtalk( 'user_id', $this->mId
);
1321 return (bool)$this->mNewtalk
;
1325 * Return the talk page(s) this user has new messages on.
1327 function getNewMessageLinks() {
1329 if (!wfRunHooks('UserRetrieveNewTalks', array(&$this, &$talks)))
1332 if (!$this->getNewtalk())
1334 $up = $this->getUserPage();
1335 $utp = $up->getTalkPage();
1336 return array(array("wiki" => wfWikiID(), "link" => $utp->getLocalURL()));
1341 * Perform a user_newtalk check, uncached.
1342 * Use getNewtalk for a cached check.
1344 * @param string $field
1346 * @param bool $fromMaster True to fetch from the master, false for a slave
1350 function checkNewtalk( $field, $id, $fromMaster = false ) {
1351 if ( $fromMaster ) {
1352 $db = wfGetDB( DB_MASTER
);
1354 $db = wfGetDB( DB_SLAVE
);
1356 $ok = $db->selectField( 'user_newtalk', $field,
1357 array( $field => $id ), __METHOD__
);
1358 return $ok !== false;
1363 * @param string $field
1367 function updateNewtalk( $field, $id ) {
1368 $dbw = wfGetDB( DB_MASTER
);
1369 $dbw->insert( 'user_newtalk',
1370 array( $field => $id ),
1373 if ( $dbw->affectedRows() ) {
1374 wfDebug( __METHOD__
.": set on ($field, $id)\n" );
1377 wfDebug( __METHOD__
." already set ($field, $id)\n" );
1383 * Clear the new messages flag for the given user
1384 * @param string $field
1388 function deleteNewtalk( $field, $id ) {
1389 $dbw = wfGetDB( DB_MASTER
);
1390 $dbw->delete( 'user_newtalk',
1391 array( $field => $id ),
1393 if ( $dbw->affectedRows() ) {
1394 wfDebug( __METHOD__
.": killed on ($field, $id)\n" );
1397 wfDebug( __METHOD__
.": already gone ($field, $id)\n" );
1403 * Update the 'You have new messages!' status.
1406 function setNewtalk( $val ) {
1407 if( wfReadOnly() ) {
1412 $this->mNewtalk
= $val;
1414 if( $this->isAnon() ) {
1416 $id = $this->getName();
1419 $id = $this->getId();
1424 $changed = $this->updateNewtalk( $field, $id );
1426 $changed = $this->deleteNewtalk( $field, $id );
1429 if( $this->isAnon() ) {
1430 // Anons have a separate memcached space, since
1431 // user records aren't kept for them.
1432 $key = wfMemcKey( 'newtalk', 'ip', $id );
1433 $wgMemc->set( $key, $val ?
1 : 0, 1800 );
1436 $this->invalidateCache();
1441 * Generate a current or new-future timestamp to be stored in the
1442 * user_touched field when we update things.
1444 private static function newTouchedTimestamp() {
1445 global $wgClockSkewFudge;
1446 return wfTimestamp( TS_MW
, time() +
$wgClockSkewFudge );
1450 * Clear user data from memcached.
1451 * Use after applying fun updates to the database; caller's
1452 * responsibility to update user_touched if appropriate.
1454 * Called implicitly from invalidateCache() and saveSettings().
1456 private function clearSharedCache() {
1459 $wgMemc->delete( wfMemcKey( 'user', 'id', $this->mId
) );
1464 * Immediately touch the user data cache for this account.
1465 * Updates user_touched field, and removes account data from memcached
1466 * for reload on the next hit.
1468 function invalidateCache() {
1471 $this->mTouched
= self
::newTouchedTimestamp();
1473 $dbw = wfGetDB( DB_MASTER
);
1474 $dbw->update( 'user',
1475 array( 'user_touched' => $dbw->timestamp( $this->mTouched
) ),
1476 array( 'user_id' => $this->mId
),
1479 $this->clearSharedCache();
1483 function validateCache( $timestamp ) {
1485 return ($timestamp >= $this->mTouched
);
1489 * Set the password and reset the random token
1490 * Calls through to authentication plugin if necessary;
1491 * will have no effect if the auth plugin refuses to
1492 * pass the change through or if the legal password
1495 * As a special case, setting the password to null
1496 * wipes it, so the account cannot be logged in until
1497 * a new password is set, for instance via e-mail.
1499 * @param string $str
1500 * @throws PasswordError on failure
1502 function setPassword( $str ) {
1505 if( $str !== null ) {
1506 if( !$wgAuth->allowPasswordChange() ) {
1507 throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
1510 if( !$this->isValidPassword( $str ) ) {
1511 global $wgMinimalPasswordLength;
1512 throw new PasswordError( wfMsgExt( 'passwordtooshort', array( 'parsemag' ),
1513 $wgMinimalPasswordLength ) );
1517 if( !$wgAuth->setPassword( $this, $str ) ) {
1518 throw new PasswordError( wfMsg( 'externaldberror' ) );
1521 $this->setInternalPassword( $str );
1527 * Set the password and reset the random token no matter
1530 * @param string $str
1532 function setInternalPassword( $str ) {
1536 if( $str === null ) {
1537 // Save an invalid hash...
1538 $this->mPassword
= '';
1540 $this->mPassword
= self
::crypt( $str );
1542 $this->mNewpassword
= '';
1543 $this->mNewpassTime
= null;
1546 function getToken() {
1548 return $this->mToken
;
1552 * Set the random token (used for persistent authentication)
1553 * Called from loadDefaults() among other places.
1556 function setToken( $token = false ) {
1557 global $wgSecretKey, $wgProxyKey;
1560 if ( $wgSecretKey ) {
1561 $key = $wgSecretKey;
1562 } elseif ( $wgProxyKey ) {
1567 $this->mToken
= md5( $key . mt_rand( 0, 0x7fffffff ) . wfWikiID() . $this->mId
);
1569 $this->mToken
= $token;
1573 function setCookiePassword( $str ) {
1575 $this->mCookiePassword
= md5( $str );
1579 * Set the password for a password reminder or new account email
1580 * Sets the user_newpass_time field if $throttle is true
1582 function setNewpassword( $str, $throttle = true ) {
1584 $this->mNewpassword
= self
::crypt( $str );
1586 $this->mNewpassTime
= wfTimestampNow();
1591 * Returns true if a password reminder email has already been sent within
1592 * the last $wgPasswordReminderResendTime hours
1594 function isPasswordReminderThrottled() {
1595 global $wgPasswordReminderResendTime;
1597 if ( !$this->mNewpassTime ||
!$wgPasswordReminderResendTime ) {
1600 $expiry = wfTimestamp( TS_UNIX
, $this->mNewpassTime
) +
$wgPasswordReminderResendTime * 3600;
1601 return time() < $expiry;
1604 function getEmail() {
1606 wfRunHooks( 'UserGetEmail', array( $this, &$this->mEmail
) );
1607 return $this->mEmail
;
1610 function getEmailAuthenticationTimestamp() {
1612 wfRunHooks( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated
) );
1613 return $this->mEmailAuthenticated
;
1616 function setEmail( $str ) {
1618 $this->mEmail
= $str;
1619 wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail
) );
1622 function getRealName() {
1624 return $this->mRealName
;
1627 function setRealName( $str ) {
1629 $this->mRealName
= $str;
1633 * @param string $oname The option to check
1634 * @param string $defaultOverride A default value returned if the option does not exist
1637 function getOption( $oname, $defaultOverride = '' ) {
1640 if ( is_null( $this->mOptions
) ) {
1641 if($defaultOverride != '') {
1642 return $defaultOverride;
1644 $this->mOptions
= User
::getDefaultOptions();
1647 if ( array_key_exists( $oname, $this->mOptions
) ) {
1648 return trim( $this->mOptions
[$oname] );
1650 return $defaultOverride;
1655 * Get the user's date preference, including some important migration for
1658 function getDatePreference() {
1659 if ( is_null( $this->mDatePreference
) ) {
1661 $value = $this->getOption( 'date' );
1662 $map = $wgLang->getDatePreferenceMigrationMap();
1663 if ( isset( $map[$value] ) ) {
1664 $value = $map[$value];
1666 $this->mDatePreference
= $value;
1668 return $this->mDatePreference
;
1672 * @param string $oname The option to check
1673 * @return bool False if the option is not selected, true if it is
1675 function getBoolOption( $oname ) {
1676 return (bool)$this->getOption( $oname );
1680 * Get an option as an integer value from the source string.
1681 * @param string $oname The option to check
1682 * @param int $default Optional value to return if option is unset/blank.
1685 function getIntOption( $oname, $default=0 ) {
1686 $val = $this->getOption( $oname );
1690 return intval( $val );
1693 function setOption( $oname, $val ) {
1695 if ( is_null( $this->mOptions
) ) {
1696 $this->mOptions
= User
::getDefaultOptions();
1698 if ( $oname == 'skin' ) {
1699 # Clear cached skin, so the new one displays immediately in Special:Preferences
1700 unset( $this->mSkin
);
1702 // Filter out any newlines that may have passed through input validation.
1703 // Newlines are used to separate items in the options blob.
1705 $val = str_replace( "\r\n", "\n", $val );
1706 $val = str_replace( "\r", "\n", $val );
1707 $val = str_replace( "\n", " ", $val );
1709 // Explicitly NULL values should refer to defaults
1710 global $wgDefaultUserOptions;
1711 if( is_null($val) && isset($wgDefaultUserOptions[$oname]) ) {
1712 $val = $wgDefaultUserOptions[$oname];
1714 $this->mOptions
[$oname] = $val;
1717 function getRights() {
1718 if ( is_null( $this->mRights
) ) {
1719 $this->mRights
= self
::getGroupPermissions( $this->getEffectiveGroups() );
1720 wfRunHooks( 'UserGetRights', array( $this, &$this->mRights
) );
1721 // Force reindexation of rights when a hook has unset one of them
1722 $this->mRights
= array_values( $this->mRights
);
1724 return $this->mRights
;
1728 * Get the list of explicit group memberships this user has.
1729 * The implicit * and user groups are not included.
1730 * @return array of strings
1732 function getGroups() {
1734 return $this->mGroups
;
1738 * Get the list of implicit group memberships this user has.
1739 * This includes all explicit groups, plus 'user' if logged in,
1740 * '*' for all accounts and autopromoted groups
1741 * @param boolean $recache Don't use the cache
1742 * @return array of strings
1744 function getEffectiveGroups( $recache = false ) {
1745 if ( $recache ||
is_null( $this->mEffectiveGroups
) ) {
1746 $this->mEffectiveGroups
= $this->getGroups();
1747 $this->mEffectiveGroups
[] = '*';
1748 if( $this->getId() ) {
1749 $this->mEffectiveGroups
[] = 'user';
1751 $this->mEffectiveGroups
= array_unique( array_merge(
1752 $this->mEffectiveGroups
,
1753 Autopromote
::getAutopromoteGroups( $this )
1756 # Hook for additional groups
1757 wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups
) );
1760 return $this->mEffectiveGroups
;
1763 /* Return the edit count for the user. This is where User::edits should have been */
1764 function getEditCount() {
1766 if ( !isset( $this->mEditCount
) ) {
1767 /* Populate the count, if it has not been populated yet */
1768 $this->mEditCount
= User
::edits($this->mId
);
1770 return $this->mEditCount
;
1778 * Add the user to the given group.
1779 * This takes immediate effect.
1780 * @param string $group
1782 function addGroup( $group ) {
1783 $dbw = wfGetDB( DB_MASTER
);
1784 if( $this->getId() ) {
1785 $dbw->insert( 'user_groups',
1787 'ug_user' => $this->getID(),
1788 'ug_group' => $group,
1791 array( 'IGNORE' ) );
1794 $this->loadGroups();
1795 $this->mGroups
[] = $group;
1796 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups( true ) );
1798 $this->invalidateCache();
1802 * Remove the user from the given group.
1803 * This takes immediate effect.
1804 * @param string $group
1806 function removeGroup( $group ) {
1808 $dbw = wfGetDB( DB_MASTER
);
1809 $dbw->delete( 'user_groups',
1811 'ug_user' => $this->getID(),
1812 'ug_group' => $group,
1814 'User::removeGroup' );
1816 $this->loadGroups();
1817 $this->mGroups
= array_diff( $this->mGroups
, array( $group ) );
1818 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups( true ) );
1820 $this->invalidateCache();
1825 * A more legible check for non-anonymousness.
1826 * Returns true if the user is not an anonymous visitor.
1830 function isLoggedIn() {
1831 return $this->getID() != 0;
1835 * A more legible check for anonymousness.
1836 * Returns true if the user is an anonymous visitor.
1841 return !$this->isLoggedIn();
1845 * Whether the user is a bot
1849 wfDeprecated( __METHOD__
);
1850 return $this->isAllowed( 'bot' );
1854 * Check if user is allowed to access a feature / make an action
1855 * @param string $action Action to be checked
1856 * @return boolean True: action is allowed, False: action should not be allowed
1858 function isAllowed($action='') {
1859 if ( $action === '' )
1860 // In the spirit of DWIM
1863 return in_array( $action, $this->getRights() );
1867 * Check whether to enable recent changes patrol features for this user
1870 public function useRCPatrol() {
1871 global $wgUseRCPatrol;
1872 return( $wgUseRCPatrol && ($this->isAllowed('patrol') ||
$this->isAllowed('patrolmarks')) );
1876 * Check whether to enable recent changes patrol features for this user
1879 public function useNPPatrol() {
1880 global $wgUseRCPatrol, $wgUseNPPatrol;
1881 return( ($wgUseRCPatrol ||
$wgUseNPPatrol) && ($this->isAllowed('patrol') ||
$this->isAllowed('patrolmarks')) );
1885 * Load a skin if it doesn't exist or return it
1886 * @todo FIXME : need to check the old failback system [AV]
1888 function &getSkin() {
1890 if ( ! isset( $this->mSkin
) ) {
1891 wfProfileIn( __METHOD__
);
1894 $userSkin = $this->getOption( 'skin' );
1895 $userSkin = $wgRequest->getVal('useskin', $userSkin);
1897 $this->mSkin
=& Skin
::newFromKey( $userSkin );
1898 wfProfileOut( __METHOD__
);
1900 return $this->mSkin
;
1904 * @param string $title Article title to look at
1908 * Check watched status of an article
1909 * @return bool True if article is watched
1911 function isWatched( $title ) {
1912 $wl = WatchedItem
::fromUserTitle( $this, $title );
1913 return $wl->isWatched();
1919 function addWatch( $title ) {
1920 $wl = WatchedItem
::fromUserTitle( $this, $title );
1922 $this->invalidateCache();
1926 * Stop watching an article
1928 function removeWatch( $title ) {
1929 $wl = WatchedItem
::fromUserTitle( $this, $title );
1931 $this->invalidateCache();
1935 * Clear the user's notification timestamp for the given title.
1936 * If e-notif e-mails are on, they will receive notification mails on
1937 * the next change of the page if it's watched etc.
1939 function clearNotification( &$title ) {
1940 global $wgUser, $wgUseEnotif, $wgShowUpdatedMarker;
1942 # Do nothing if the database is locked to writes
1943 if( wfReadOnly() ) {
1947 if ($title->getNamespace() == NS_USER_TALK
&&
1948 $title->getText() == $this->getName() ) {
1949 if (!wfRunHooks('UserClearNewTalkNotification', array(&$this)))
1951 $this->setNewtalk( false );
1954 if( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
1958 if( $this->isAnon() ) {
1959 // Nothing else to do...
1963 // Only update the timestamp if the page is being watched.
1964 // The query to find out if it is watched is cached both in memcached and per-invocation,
1965 // and when it does have to be executed, it can be on a slave
1966 // If this is the user's newtalk page, we always update the timestamp
1967 if ($title->getNamespace() == NS_USER_TALK
&&
1968 $title->getText() == $wgUser->getName())
1971 } elseif ( $this->getId() == $wgUser->getId() ) {
1972 $watched = $title->userIsWatching();
1977 // If the page is watched by the user (or may be watched), update the timestamp on any
1978 // any matching rows
1980 $dbw = wfGetDB( DB_MASTER
);
1981 $dbw->update( 'watchlist',
1983 'wl_notificationtimestamp' => NULL
1984 ), array( /* WHERE */
1985 'wl_title' => $title->getDBkey(),
1986 'wl_namespace' => $title->getNamespace(),
1987 'wl_user' => $this->getID()
1988 ), 'User::clearLastVisited'
1996 * Resets all of the given user's page-change notification timestamps.
1997 * If e-notif e-mails are on, they will receive notification mails on
1998 * the next change of any watched page.
2000 * @param int $currentUser user ID number
2003 function clearAllNotifications( $currentUser ) {
2004 global $wgUseEnotif, $wgShowUpdatedMarker;
2005 if ( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
2006 $this->setNewtalk( false );
2009 if( $currentUser != 0 ) {
2010 $dbw = wfGetDB( DB_MASTER
);
2011 $dbw->update( 'watchlist',
2013 'wl_notificationtimestamp' => NULL
2014 ), array( /* WHERE */
2015 'wl_user' => $currentUser
2018 # We also need to clear here the "you have new message" notification for the own user_talk page
2019 # This is cleared one page view later in Article::viewUpdates();
2025 * @return string Encoding options
2027 function encodeOptions() {
2029 if ( is_null( $this->mOptions
) ) {
2030 $this->mOptions
= User
::getDefaultOptions();
2033 foreach ( $this->mOptions
as $oname => $oval ) {
2034 array_push( $a, $oname.'='.$oval );
2036 $s = implode( "\n", $a );
2043 function decodeOptions( $str ) {
2044 $this->mOptions
= array();
2045 $a = explode( "\n", $str );
2046 foreach ( $a as $s ) {
2048 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
2049 $this->mOptions
[$m[1]] = $m[2];
2054 protected function setCookie( $name, $value, $exp=0 ) {
2055 global $wgCookiePrefix,$wgCookieDomain,$wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
2057 $exp = time() +
$wgCookieExpiration;
2059 $httpOnlySafe = wfHttpOnlySafe();
2060 wfDebugLog( 'cookie',
2061 'setcookie: "' . implode( '", "',
2063 $wgCookiePrefix . $name,
2069 $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
2070 if( $httpOnlySafe && isset( $wgCookieHttpOnly ) ) {
2071 setcookie( $wgCookiePrefix . $name,
2077 $wgCookieHttpOnly );
2079 // setcookie() fails on PHP 5.1 if you give it future-compat paramters.
2081 setcookie( $wgCookiePrefix . $name,
2090 protected function clearCookie( $name ) {
2091 $this->setCookie( $name, '', time() - 86400 );
2094 function setCookies() {
2096 if ( 0 == $this->mId
) return;
2098 'wsUserID' => $this->mId
,
2099 'wsToken' => $this->mToken
,
2100 'wsUserName' => $this->getName()
2103 'UserID' => $this->mId
,
2104 'UserName' => $this->getName(),
2106 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
2107 $cookies['Token'] = $this->mToken
;
2109 $cookies['Token'] = false;
2112 wfRunHooks( 'UserSetCookies', array( $this, &$session, &$cookies ) );
2113 $_SESSION = $session +
$_SESSION;
2114 foreach ( $cookies as $name => $value ) {
2115 if ( $value === false ) {
2116 $this->clearCookie( $name );
2118 $this->setCookie( $name, $value );
2128 if( wfRunHooks( 'UserLogout', array(&$this) ) ) {
2134 * Really logout user
2135 * Clears the cookies and session, resets the instance cache
2137 function doLogout() {
2138 $this->clearInstanceCache( 'defaults' );
2140 $_SESSION['wsUserID'] = 0;
2142 $this->clearCookie( 'UserID' );
2143 $this->clearCookie( 'Token' );
2145 # Remember when user logged out, to prevent seeing cached pages
2146 $this->setCookie( 'LoggedOut', wfTimestampNow(), time() +
86400 );
2150 * Save object settings into database
2151 * @todo Only rarely do all these fields need to be set!
2153 function saveSettings() {
2155 if ( wfReadOnly() ) { return; }
2156 if ( 0 == $this->mId
) { return; }
2158 $this->mTouched
= self
::newTouchedTimestamp();
2160 $dbw = wfGetDB( DB_MASTER
);
2161 $dbw->update( 'user',
2163 'user_name' => $this->mName
,
2164 'user_password' => $this->mPassword
,
2165 'user_newpassword' => $this->mNewpassword
,
2166 'user_newpass_time' => $dbw->timestampOrNull( $this->mNewpassTime
),
2167 'user_real_name' => $this->mRealName
,
2168 'user_email' => $this->mEmail
,
2169 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
2170 'user_options' => $this->encodeOptions(),
2171 'user_touched' => $dbw->timestamp($this->mTouched
),
2172 'user_token' => $this->mToken
,
2173 'user_email_token' => $this->mEmailToken
,
2174 'user_email_token_expires' => $dbw->timestampOrNull( $this->mEmailTokenExpires
),
2175 ), array( /* WHERE */
2176 'user_id' => $this->mId
2179 wfRunHooks( 'UserSaveSettings', array( $this ) );
2180 $this->clearSharedCache();
2184 * Checks if a user with the given name exists, returns the ID.
2186 function idForName() {
2187 $s = trim( $this->getName() );
2188 if ( $s === '' ) return 0;
2190 $dbr = wfGetDB( DB_SLAVE
);
2191 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), __METHOD__
);
2192 if ( $id === false ) {
2199 * Add a user to the database, return the user object
2201 * @param string $name The user's name
2202 * @param array $params Associative array of non-default parameters to save to the database:
2203 * password The user's password. Password logins will be disabled if this is omitted.
2204 * newpassword A temporary password mailed to the user
2205 * email The user's email address
2206 * email_authenticated The email authentication timestamp
2207 * real_name The user's real name
2208 * options An associative array of non-default options
2209 * token Random authentication token. Do not set.
2210 * registration Registration timestamp. Do not set.
2212 * @return User object, or null if the username already exists
2214 static function createNew( $name, $params = array() ) {
2217 if ( isset( $params['options'] ) ) {
2218 $user->mOptions
= $params['options'] +
$user->mOptions
;
2219 unset( $params['options'] );
2221 $dbw = wfGetDB( DB_MASTER
);
2222 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
2224 'user_id' => $seqVal,
2225 'user_name' => $name,
2226 'user_password' => $user->mPassword
,
2227 'user_newpassword' => $user->mNewpassword
,
2228 'user_newpass_time' => $dbw->timestamp( $user->mNewpassTime
),
2229 'user_email' => $user->mEmail
,
2230 'user_email_authenticated' => $dbw->timestampOrNull( $user->mEmailAuthenticated
),
2231 'user_real_name' => $user->mRealName
,
2232 'user_options' => $user->encodeOptions(),
2233 'user_token' => $user->mToken
,
2234 'user_registration' => $dbw->timestamp( $user->mRegistration
),
2235 'user_editcount' => 0,
2237 foreach ( $params as $name => $value ) {
2238 $fields["user_$name"] = $value;
2240 $dbw->insert( 'user', $fields, __METHOD__
, array( 'IGNORE' ) );
2241 if ( $dbw->affectedRows() ) {
2242 $newUser = User
::newFromId( $dbw->insertId() );
2250 * Add an existing user object to the database
2252 function addToDatabase() {
2254 $dbw = wfGetDB( DB_MASTER
);
2255 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
2256 $dbw->insert( 'user',
2258 'user_id' => $seqVal,
2259 'user_name' => $this->mName
,
2260 'user_password' => $this->mPassword
,
2261 'user_newpassword' => $this->mNewpassword
,
2262 'user_newpass_time' => $dbw->timestamp( $this->mNewpassTime
),
2263 'user_email' => $this->mEmail
,
2264 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
2265 'user_real_name' => $this->mRealName
,
2266 'user_options' => $this->encodeOptions(),
2267 'user_token' => $this->mToken
,
2268 'user_registration' => $dbw->timestamp( $this->mRegistration
),
2269 'user_editcount' => 0,
2272 $this->mId
= $dbw->insertId();
2274 # Clear instance cache other than user table data, which is already accurate
2275 $this->clearInstanceCache();
2279 * If the (non-anonymous) user is blocked, this function will block any IP address
2280 * that they successfully log on from.
2282 function spreadBlock() {
2283 wfDebug( __METHOD__
."()\n" );
2285 if ( $this->mId
== 0 ) {
2289 $userblock = Block
::newFromDB( '', $this->mId
);
2290 if ( !$userblock ) {
2294 $userblock->doAutoblock( wfGetIp() );
2299 * Generate a string which will be different for any combination of
2300 * user options which would produce different parser output.
2301 * This will be used as part of the hash key for the parser cache,
2302 * so users will the same options can share the same cached data
2305 * Extensions which require it should install 'PageRenderingHash' hook,
2306 * which will give them a chance to modify this key based on their own
2311 function getPageRenderingHash() {
2312 global $wgContLang, $wgUseDynamicDates, $wgLang;
2314 return $this->mHash
;
2317 // stubthreshold is only included below for completeness,
2318 // it will always be 0 when this function is called by parsercache.
2320 $confstr = $this->getOption( 'math' );
2321 $confstr .= '!' . $this->getOption( 'stubthreshold' );
2322 if ( $wgUseDynamicDates ) {
2323 $confstr .= '!' . $this->getDatePreference();
2325 $confstr .= '!' . ($this->getOption( 'numberheadings' ) ?
'1' : '');
2326 $confstr .= '!' . $wgLang->getCode();
2327 $confstr .= '!' . $this->getOption( 'thumbsize' );
2328 // add in language specific options, if any
2329 $extra = $wgContLang->getExtraHashOptions();
2332 // Give a chance for extensions to modify the hash, if they have
2333 // extra options or other effects on the parser cache.
2334 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
2336 // Make it a valid memcached key fragment
2337 $confstr = str_replace( ' ', '_', $confstr );
2338 $this->mHash
= $confstr;
2342 function isBlockedFromCreateAccount() {
2343 $this->getBlockedStatus();
2344 return $this->mBlock
&& $this->mBlock
->mCreateAccount
;
2348 * Determine if the user is blocked from using Special:Emailuser.
2353 function isBlockedFromEmailuser() {
2354 $this->getBlockedStatus();
2355 return $this->mBlock
&& $this->mBlock
->mBlockEmail
;
2358 function isAllowedToCreateAccount() {
2359 return $this->isAllowed( 'createaccount' ) && !$this->isBlockedFromCreateAccount();
2365 function setLoaded( $loaded ) {
2366 wfDeprecated( __METHOD__
);
2370 * Get this user's personal page title.
2375 function getUserPage() {
2376 return Title
::makeTitle( NS_USER
, $this->getName() );
2380 * Get this user's talk page title.
2385 function getTalkPage() {
2386 $title = $this->getUserPage();
2387 return $title->getTalkPage();
2393 function getMaxID() {
2394 static $res; // cache
2396 if ( isset( $res ) )
2399 $dbr = wfGetDB( DB_SLAVE
);
2400 return $res = $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
2405 * Determine whether the user is a newbie. Newbies are either
2406 * anonymous IPs, or the most recently created accounts.
2407 * @return bool True if it is a newbie.
2409 function isNewbie() {
2410 return !$this->isAllowed( 'autoconfirmed' );
2414 * Is the user active? We check to see if they've made at least
2415 * X number of edits in the last Y days.
2417 * @return bool true if the user is active, false if not
2419 public function isActiveEditor() {
2420 global $wgActiveUserEditCount, $wgActiveUserDays;
2421 $dbr = wfGetDB( DB_SLAVE
);
2423 // Stolen without shame from RC
2424 $cutoff_unixtime = time() - ( $wgActiveUserDays * 86400 );
2425 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
2426 $oldTime = $dbr->addQuotes( $dbr->timestamp( $cutoff_unixtime ) );
2428 $res = $dbr->select( 'revision', '1',
2429 array( 'rev_user_text' => $this->getName(), "rev_timestamp > $oldTime"),
2431 array('LIMIT' => $wgActiveUserEditCount ) );
2433 $count = $dbr->numRows($res);
2434 $dbr->freeResult($res);
2436 return $count == $wgActiveUserEditCount;
2440 * Check to see if the given clear-text password is one of the accepted passwords
2441 * @param string $password User password.
2442 * @return bool True if the given password is correct otherwise False.
2444 function checkPassword( $password ) {
2448 // Even though we stop people from creating passwords that
2449 // are shorter than this, doesn't mean people wont be able
2450 // to. Certain authentication plugins do NOT want to save
2451 // domain passwords in a mysql database, so we should
2452 // check this (incase $wgAuth->strict() is false).
2453 if( !$this->isValidPassword( $password ) ) {
2457 if( $wgAuth->authenticate( $this->getName(), $password ) ) {
2459 } elseif( $wgAuth->strict() ) {
2460 /* Auth plugin doesn't allow local authentication */
2462 } elseif( $wgAuth->strictUserAuth( $this->getName() ) ) {
2463 /* Auth plugin doesn't allow local authentication for this user name */
2466 if ( self
::comparePasswords( $this->mPassword
, $password, $this->mId
) ) {
2468 } elseif ( function_exists( 'iconv' ) ) {
2469 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
2470 # Check for this with iconv
2471 $cp1252Password = iconv( 'UTF-8', 'WINDOWS-1252//TRANSLIT', $password );
2472 if ( self
::comparePasswords( $this->mPassword
, $cp1252Password, $this->mId
) ) {
2480 * Check if the given clear-text password matches the temporary password
2481 * sent by e-mail for password reset operations.
2484 function checkTemporaryPassword( $plaintext ) {
2485 return self
::comparePasswords( $this->mNewpassword
, $plaintext, $this->getId() );
2489 * Initialize (if necessary) and return a session token value
2490 * which can be used in edit forms to show that the user's
2491 * login credentials aren't being hijacked with a foreign form
2494 * @param mixed $salt - Optional function-specific data for hash.
2495 * Use a string or an array of strings.
2499 function editToken( $salt = '' ) {
2500 if ( $this->isAnon() ) {
2501 return EDIT_TOKEN_SUFFIX
;
2503 if( !isset( $_SESSION['wsEditToken'] ) ) {
2504 $token = $this->generateToken();
2505 $_SESSION['wsEditToken'] = $token;
2507 $token = $_SESSION['wsEditToken'];
2509 if( is_array( $salt ) ) {
2510 $salt = implode( '|', $salt );
2512 return md5( $token . $salt ) . EDIT_TOKEN_SUFFIX
;
2517 * Generate a hex-y looking random token for various uses.
2518 * Could be made more cryptographically sure if someone cares.
2521 function generateToken( $salt = '' ) {
2522 $token = dechex( mt_rand() ) . dechex( mt_rand() );
2523 return md5( $token . $salt );
2527 * Check given value against the token value stored in the session.
2528 * A match should confirm that the form was submitted from the
2529 * user's own login session, not a form submission from a third-party
2532 * @param string $val - the input value to compare
2533 * @param string $salt - Optional function-specific data for hash
2537 function matchEditToken( $val, $salt = '' ) {
2538 $sessionToken = $this->editToken( $salt );
2539 if ( $val != $sessionToken ) {
2540 wfDebug( "User::matchEditToken: broken session data\n" );
2542 return $val == $sessionToken;
2546 * Check whether the edit token is fine except for the suffix
2548 function matchEditTokenNoSuffix( $val, $salt = '' ) {
2549 $sessionToken = $this->editToken( $salt );
2550 return substr( $sessionToken, 0, 32 ) == substr( $val, 0, 32 );
2554 * Generate a new e-mail confirmation token and send a confirmation/invalidation
2555 * mail to the user's given address.
2557 * Calls saveSettings() internally; as it has side effects, not committing changes
2558 * would be pretty silly.
2560 * @return mixed True on success, a WikiError object on failure.
2562 function sendConfirmationMail() {
2564 $expiration = null; // gets passed-by-ref and defined in next line.
2565 $token = $this->confirmationToken( $expiration );
2566 $url = $this->confirmationTokenUrl( $token );
2567 $invalidateURL = $this->invalidationTokenUrl( $token );
2568 $this->saveSettings();
2570 return $this->sendMail( wfMsg( 'confirmemail_subject' ),
2571 wfMsg( 'confirmemail_body',
2575 $wgLang->timeanddate( $expiration, false ),
2580 * Send an e-mail to this user's account. Does not check for
2581 * confirmed status or validity.
2583 * @param string $subject
2584 * @param string $body
2585 * @param string $from Optional from address; default $wgPasswordSender will be used otherwise.
2586 * @return mixed True on success, a WikiError object on failure.
2588 function sendMail( $subject, $body, $from = null, $replyto = null ) {
2589 if( is_null( $from ) ) {
2590 global $wgPasswordSender;
2591 $from = $wgPasswordSender;
2594 $to = new MailAddress( $this );
2595 $sender = new MailAddress( $from );
2596 return UserMailer
::send( $to, $sender, $subject, $body, $replyto );
2600 * Generate, store, and return a new e-mail confirmation code.
2601 * A hash (unsalted since it's used as a key) is stored.
2603 * Call saveSettings() after calling this function to commit
2604 * this change to the database.
2606 * @param &$expiration mixed output: accepts the expiration time
2610 function confirmationToken( &$expiration ) {
2612 $expires = $now +
7 * 24 * 60 * 60;
2613 $expiration = wfTimestamp( TS_MW
, $expires );
2614 $token = $this->generateToken( $this->mId
. $this->mEmail
. $expires );
2615 $hash = md5( $token );
2617 $this->mEmailToken
= $hash;
2618 $this->mEmailTokenExpires
= $expiration;
2623 * Return a URL the user can use to confirm their email address.
2624 * @param $token: accepts the email confirmation token
2628 function confirmationTokenUrl( $token ) {
2629 return $this->getTokenUrl( 'ConfirmEmail', $token );
2632 * Return a URL the user can use to invalidate their email address.
2633 * @param $token: accepts the email confirmation token
2637 function invalidationTokenUrl( $token ) {
2638 return $this->getTokenUrl( 'Invalidateemail', $token );
2642 * Internal function to format the e-mail validation/invalidation URLs.
2643 * This uses $wgArticlePath directly as a quickie hack to use the
2644 * hardcoded English names of the Special: pages, for ASCII safety.
2646 * Since these URLs get dropped directly into emails, using the
2647 * short English names avoids insanely long URL-encoded links, which
2648 * also sometimes can get corrupted in some browsers/mailers
2649 * (bug 6957 with Gmail and Internet Explorer).
2651 protected function getTokenUrl( $page, $token ) {
2652 global $wgArticlePath;
2656 "Special:$page/$token",
2661 * Mark the e-mail address confirmed.
2663 * Call saveSettings() after calling this function to commit the change.
2665 function confirmEmail() {
2666 $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
2671 * Invalidate the user's email confirmation, unauthenticate the email
2672 * if it was already confirmed.
2674 * Call saveSettings() after calling this function to commit the change.
2676 function invalidateEmail() {
2678 $this->mEmailToken
= null;
2679 $this->mEmailTokenExpires
= null;
2680 $this->setEmailAuthenticationTimestamp( null );
2684 function setEmailAuthenticationTimestamp( $timestamp ) {
2686 $this->mEmailAuthenticated
= $timestamp;
2687 wfRunHooks( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated
) );
2691 * Is this user allowed to send e-mails within limits of current
2692 * site configuration?
2695 function canSendEmail() {
2696 $canSend = $this->isEmailConfirmed();
2697 wfRunHooks( 'UserCanSendEmail', array( &$this, &$canSend ) );
2702 * Is this user allowed to receive e-mails within limits of current
2703 * site configuration?
2706 function canReceiveEmail() {
2707 return $this->isEmailConfirmed() && !$this->getOption( 'disablemail' );
2711 * Is this user's e-mail address valid-looking and confirmed within
2712 * limits of the current site configuration?
2714 * If $wgEmailAuthentication is on, this may require the user to have
2715 * confirmed their address by returning a code or using a password
2716 * sent to the address from the wiki.
2720 function isEmailConfirmed() {
2721 global $wgEmailAuthentication;
2724 if( wfRunHooks( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
2725 if( $this->isAnon() )
2727 if( !self
::isValidEmailAddr( $this->mEmail
) )
2729 if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
2738 * Return true if there is an outstanding request for e-mail confirmation.
2741 function isEmailConfirmationPending() {
2742 global $wgEmailAuthentication;
2743 return $wgEmailAuthentication &&
2744 !$this->isEmailConfirmed() &&
2745 $this->mEmailToken
&&
2746 $this->mEmailTokenExpires
> wfTimestamp();
2750 * Get the timestamp of account creation, or false for
2751 * non-existent/anonymous user accounts
2755 public function getRegistration() {
2756 return $this->mId
> 0
2757 ?
$this->mRegistration
2762 * @param array $groups list of groups
2763 * @return array list of permission key names for given groups combined
2765 static function getGroupPermissions( $groups ) {
2766 global $wgGroupPermissions;
2768 foreach( $groups as $group ) {
2769 if( isset( $wgGroupPermissions[$group] ) ) {
2770 $rights = array_merge( $rights,
2771 array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
2778 * @param string $group key name
2779 * @return string localized descriptive name for group, if provided
2781 static function getGroupName( $group ) {
2782 global $wgMessageCache;
2783 $wgMessageCache->loadAllMessages();
2784 $key = "group-$group";
2785 $name = wfMsg( $key );
2786 return $name == '' ||
wfEmptyMsg( $key, $name )
2792 * @param string $group key name
2793 * @return string localized descriptive name for member of a group, if provided
2795 static function getGroupMember( $group ) {
2796 global $wgMessageCache;
2797 $wgMessageCache->loadAllMessages();
2798 $key = "group-$group-member";
2799 $name = wfMsg( $key );
2800 return $name == '' ||
wfEmptyMsg( $key, $name )
2806 * Return the set of defined explicit groups.
2807 * The implicit groups (by default *, 'user' and 'autoconfirmed')
2808 * are not included, as they are defined automatically,
2809 * not in the database.
2812 static function getAllGroups() {
2813 global $wgGroupPermissions;
2815 array_keys( $wgGroupPermissions ),
2816 self
::getImplicitGroups()
2821 * Get a list of all available permissions
2823 static function getAllRights() {
2824 if ( self
::$mAllRights === false ) {
2825 global $wgAvailableRights;
2826 if ( count( $wgAvailableRights ) ) {
2827 self
::$mAllRights = array_unique( array_merge( self
::$mCoreRights, $wgAvailableRights ) );
2829 self
::$mAllRights = self
::$mCoreRights;
2831 wfRunHooks( 'UserGetAllRights', array( &self
::$mAllRights ) );
2833 return self
::$mAllRights;
2837 * Get a list of implicit groups
2841 public static function getImplicitGroups() {
2842 global $wgImplicitGroups;
2843 $groups = $wgImplicitGroups;
2844 wfRunHooks( 'UserGetImplicitGroups', array( &$groups ) ); #deprecated, use $wgImplictGroups instead
2849 * Get the title of a page describing a particular group
2851 * @param $group Name of the group
2854 static function getGroupPage( $group ) {
2855 global $wgMessageCache;
2856 $wgMessageCache->loadAllMessages();
2857 $page = wfMsgForContent( 'grouppage-' . $group );
2858 if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
2859 $title = Title
::newFromText( $page );
2860 if( is_object( $title ) )
2867 * Create a link to the group in HTML, if available
2869 * @param $group Name of the group
2870 * @param $text The text of the link
2873 static function makeGroupLinkHTML( $group, $text = '' ) {
2875 $text = self
::getGroupName( $group );
2877 $title = self
::getGroupPage( $group );
2880 $sk = $wgUser->getSkin();
2881 return $sk->makeLinkObj( $title, htmlspecialchars( $text ) );
2888 * Create a link to the group in Wikitext, if available
2890 * @param $group Name of the group
2891 * @param $text The text of the link (by default, the name of the group)
2894 static function makeGroupLinkWiki( $group, $text = '' ) {
2896 $text = self
::getGroupName( $group );
2898 $title = self
::getGroupPage( $group );
2900 $page = $title->getPrefixedText();
2901 return "[[$page|$text]]";
2908 * Increment the user's edit-count field.
2909 * Will have no effect for anonymous users.
2911 function incEditCount() {
2912 if( !$this->isAnon() ) {
2913 $dbw = wfGetDB( DB_MASTER
);
2914 $dbw->update( 'user',
2915 array( 'user_editcount=user_editcount+1' ),
2916 array( 'user_id' => $this->getId() ),
2919 // Lazy initialization check...
2920 if( $dbw->affectedRows() == 0 ) {
2921 // Pull from a slave to be less cruel to servers
2922 // Accuracy isn't the point anyway here
2923 $dbr = wfGetDB( DB_SLAVE
);
2924 $count = $dbr->selectField( 'revision',
2926 array( 'rev_user' => $this->getId() ),
2929 // Now here's a goddamn hack...
2930 if( $dbr !== $dbw ) {
2931 // If we actually have a slave server, the count is
2932 // at least one behind because the current transaction
2933 // has not been committed and replicated.
2936 // But if DB_SLAVE is selecting the master, then the
2937 // count we just read includes the revision that was
2938 // just added in the working transaction.
2941 $dbw->update( 'user',
2942 array( 'user_editcount' => $count ),
2943 array( 'user_id' => $this->getId() ),
2947 // edit count in user cache too
2948 $this->invalidateCache();
2951 static function getRightDescription( $right ) {
2952 global $wgMessageCache;
2953 $wgMessageCache->loadAllMessages();
2954 $key = "right-$right";
2955 $name = wfMsg( $key );
2956 return $name == '' ||
wfEmptyMsg( $key, $name )
2962 * Make an old-style password hash
2964 * @param string $password Plain-text password
2965 * @param string $userId User ID
2967 static function oldCrypt( $password, $userId ) {
2968 global $wgPasswordSalt;
2969 if ( $wgPasswordSalt ) {
2970 return md5( $userId . '-' . md5( $password ) );
2972 return md5( $password );
2977 * Make a new-style password hash
2979 * @param string $password Plain-text password
2980 * @param string $salt Salt, may be random or the user ID. False to generate a salt.
2982 static function crypt( $password, $salt = false ) {
2983 global $wgPasswordSalt;
2985 if($wgPasswordSalt) {
2986 if ( $salt === false ) {
2987 $salt = substr( wfGenerateToken(), 0, 8 );
2989 return ':B:' . $salt . ':' . md5( $salt . '-' . md5( $password ) );
2991 return ':A:' . md5( $password);
2996 * Compare a password hash with a plain-text password. Requires the user
2997 * ID if there's a chance that the hash is an old-style hash.
2999 * @param string $hash Password hash
3000 * @param string $password Plain-text password to compare
3001 * @param string $userId User ID for old-style password salt
3003 static function comparePasswords( $hash, $password, $userId = false ) {
3005 $type = substr( $hash, 0, 3 );
3006 if ( $type == ':A:' ) {
3008 return md5( $password ) === substr( $hash, 3 );
3009 } elseif ( $type == ':B:' ) {
3011 list( $salt, $realHash ) = explode( ':', substr( $hash, 3 ), 2 );
3012 return md5( $salt.'-'.md5( $password ) ) == $realHash;
3015 return self
::oldCrypt( $password, $userId ) === $hash;