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(
152 static $mAllRights = false;
155 * The cache variable declarations
157 var $mId, $mName, $mRealName, $mPassword, $mNewpassword, $mNewpassTime,
158 $mEmail, $mOptions, $mTouched, $mToken, $mEmailAuthenticated,
159 $mEmailToken, $mEmailTokenExpires, $mRegistration, $mGroups;
162 * Whether the cache variables have been loaded
167 * Initialisation data source if mDataLoaded==false. May be one of:
168 * defaults anonymous user initialised from class defaults
169 * name initialise from mName
170 * id initialise from mId
171 * session log in from cookies or session if possible
173 * Use the User::newFrom*() family of functions to set this.
178 * Lazy-initialised variables, invalidated with clearInstanceCache
180 var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mSkin, $mRights,
181 $mBlockreason, $mBlock, $mEffectiveGroups;
184 * Lightweight constructor for anonymous user
185 * Use the User::newFrom* factory functions for other kinds of users
188 $this->clearInstanceCache( 'defaults' );
192 * Load the user table data for this object from the source given by mFrom
195 if ( $this->mDataLoaded
) {
198 wfProfileIn( __METHOD__
);
200 # Set it now to avoid infinite recursion in accessors
201 $this->mDataLoaded
= true;
203 switch ( $this->mFrom
) {
205 $this->loadDefaults();
208 $this->mId
= self
::idFromName( $this->mName
);
210 # Nonexistent user placeholder object
211 $this->loadDefaults( $this->mName
);
220 $this->loadFromSession();
223 throw new MWException( "Unrecognised value for User->mFrom: \"{$this->mFrom}\"" );
225 wfProfileOut( __METHOD__
);
229 * Load user table data given mId
230 * @return false if the ID does not exist, true otherwise
233 function loadFromId() {
235 if ( $this->mId
== 0 ) {
236 $this->loadDefaults();
241 $key = wfMemcKey( 'user', 'id', $this->mId
);
242 $data = $wgMemc->get( $key );
243 if ( !is_array( $data ) ||
$data['mVersion'] < MW_USER_VERSION
) {
244 # Object is expired, load from DB
249 wfDebug( "Cache miss for user {$this->mId}\n" );
251 if ( !$this->loadFromDatabase() ) {
252 # Can't load from ID, user is anonymous
255 $this->saveToCache();
257 wfDebug( "Got user {$this->mId} from cache\n" );
259 foreach ( self
::$mCacheVars as $name ) {
260 $this->$name = $data[$name];
267 * Save user data to the shared cache
269 function saveToCache() {
272 if ( $this->isAnon() ) {
273 // Anonymous users are uncached
277 foreach ( self
::$mCacheVars as $name ) {
278 $data[$name] = $this->$name;
280 $data['mVersion'] = MW_USER_VERSION
;
281 $key = wfMemcKey( 'user', 'id', $this->mId
);
283 $wgMemc->set( $key, $data );
287 * Static factory method for creation from username.
289 * This is slightly less efficient than newFromId(), so use newFromId() if
290 * you have both an ID and a name handy.
292 * @param string $name Username, validated by Title:newFromText()
293 * @param mixed $validate Validate username. Takes the same parameters as
294 * User::getCanonicalName(), except that true is accepted as an alias
295 * for 'valid', for BC.
297 * @return User object, or null if the username is invalid. If the username
298 * is not present in the database, the result will be a user object with
299 * a name, zero user ID and default settings.
302 static function newFromName( $name, $validate = 'valid' ) {
303 if ( $validate === true ) {
306 $name = self
::getCanonicalName( $name, $validate );
307 if ( $name === false ) {
310 # Create unloaded user object
318 static function newFromId( $id ) {
326 * Factory method to fetch whichever user has a given email confirmation code.
327 * This code is generated when an account is created or its e-mail address
330 * If the code is invalid or has expired, returns NULL.
332 * @param string $code
336 static function newFromConfirmationCode( $code ) {
337 $dbr = wfGetDB( DB_SLAVE
);
338 $id = $dbr->selectField( 'user', 'user_id', array(
339 'user_email_token' => md5( $code ),
340 'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
342 if( $id !== false ) {
343 return User
::newFromId( $id );
350 * Create a new user object using data from session or cookies. If the
351 * login credentials are invalid, the result is an anonymous user.
356 static function newFromSession() {
358 $user->mFrom
= 'session';
363 * Create a new user object from a user row.
364 * The row should have all fields from the user table in it.
366 static function newFromRow( $row ) {
368 $user->loadFromRow( $row );
373 * Get username given an id.
374 * @param integer $id Database user id
375 * @return string Nickname of a user
378 static function whoIs( $id ) {
379 $dbr = wfGetDB( DB_SLAVE
);
380 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
384 * Get the real name of a user given their identifier
386 * @param int $id Database user id
387 * @return string Real name of a user
389 static function whoIsReal( $id ) {
390 $dbr = wfGetDB( DB_SLAVE
);
391 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), __METHOD__
);
395 * Get database id given a user name
396 * @param string $name Nickname of a user
397 * @return integer|null Database user id (null: if non existent
400 static function idFromName( $name ) {
401 $nt = Title
::newFromText( $name );
402 if( is_null( $nt ) ) {
406 $dbr = wfGetDB( DB_SLAVE
);
407 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), __METHOD__
);
409 if ( $s === false ) {
417 * Does the string match an anonymous IPv4 address?
419 * This function exists for username validation, in order to reject
420 * usernames which are similar in form to IP addresses. Strings such
421 * as 300.300.300.300 will return true because it looks like an IP
422 * address, despite not being strictly valid.
424 * We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
425 * address because the usemod software would "cloak" anonymous IP
426 * addresses like this, if we allowed accounts like this to be created
427 * new users could get the old edits of these anonymous users.
430 * @param string $name Nickname of a user
433 static function isIP( $name ) {
434 return IP
::isIPAddress($name);
435 /*return preg_match("/^
436 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
437 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
438 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
439 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))
444 * Is the input a valid username?
446 * Checks if the input is a valid username, we don't want an empty string,
447 * an IP address, anything that containins slashes (would mess up subpages),
448 * is longer than the maximum allowed username size or doesn't begin with
451 * @param string $name
455 static function isValidUserName( $name ) {
456 global $wgContLang, $wgMaxNameChars;
459 || User
::isIP( $name )
460 ||
strpos( $name, '/' ) !== false
461 ||
strlen( $name ) > $wgMaxNameChars
462 ||
$name != $wgContLang->ucfirst( $name ) ) {
463 wfDebugLog( 'username', __METHOD__
.
464 ": '$name' invalid due to empty, IP, slash, length, or lowercase" );
468 // Ensure that the name can't be misresolved as a different title,
469 // such as with extra namespace keys at the start.
470 $parsed = Title
::newFromText( $name );
471 if( is_null( $parsed )
472 ||
$parsed->getNamespace()
473 ||
strcmp( $name, $parsed->getPrefixedText() ) ) {
474 wfDebugLog( 'username', __METHOD__
.
475 ": '$name' invalid due to ambiguous prefixes" );
479 // Check an additional blacklist of troublemaker characters.
480 // Should these be merged into the title char list?
481 $unicodeBlacklist = '/[' .
482 '\x{0080}-\x{009f}' . # iso-8859-1 control chars
483 '\x{00a0}' . # non-breaking space
484 '\x{2000}-\x{200f}' . # various whitespace
485 '\x{2028}-\x{202f}' . # breaks and control chars
486 '\x{3000}' . # ideographic space
487 '\x{e000}-\x{f8ff}' . # private use
489 if( preg_match( $unicodeBlacklist, $name ) ) {
490 wfDebugLog( 'username', __METHOD__
.
491 ": '$name' invalid due to blacklisted characters" );
499 * Usernames which fail to pass this function will be blocked
500 * from user login and new account registrations, but may be used
501 * internally by batch processes.
503 * If an account already exists in this form, login will be blocked
504 * by a failure to pass this function.
506 * @param string $name
509 static function isUsableName( $name ) {
510 global $wgReservedUsernames;
512 // Must be a valid username, obviously ;)
513 self
::isValidUserName( $name ) &&
515 // Certain names may be reserved for batch processes.
516 !in_array( $name, $wgReservedUsernames );
520 * Usernames which fail to pass this function will be blocked
521 * from new account registrations, but may be used internally
522 * either by batch processes or by user accounts which have
523 * already been created.
525 * Additional character blacklisting may be added here
526 * rather than in isValidUserName() to avoid disrupting
529 * @param string $name
532 static function isCreatableName( $name ) {
534 self
::isUsableName( $name ) &&
536 // Registration-time character blacklisting...
537 strpos( $name, '@' ) === false;
541 * Is the input a valid password for this user?
543 * @param string $password Desired password
546 function isValidPassword( $password ) {
547 global $wgMinimalPasswordLength, $wgContLang;
550 if( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) )
552 if( $result === false )
555 // Password needs to be long enough, and can't be the same as the username
556 return strlen( $password ) >= $wgMinimalPasswordLength
557 && $wgContLang->lc( $password ) !== $wgContLang->lc( $this->mName
);
561 * Does a string look like an email address?
563 * There used to be a regular expression here, it got removed because it
564 * rejected valid addresses. Actually just check if there is '@' somewhere
565 * in the given address.
567 * @todo Check for RFC 2822 compilance (bug 959)
569 * @param string $addr email address
572 public static function isValidEmailAddr( $addr ) {
574 if( !wfRunHooks( 'isValidEmailAddr', array( $addr, &$result ) ) ) {
578 return strpos( $addr, '@' ) !== false;
582 * Given unvalidated user input, return a canonical username, or false if
583 * the username is invalid.
584 * @param string $name
585 * @param mixed $validate Type of validation to use:
586 * false No validation
587 * 'valid' Valid for batch processes
588 * 'usable' Valid for batch processes and login
589 * 'creatable' Valid for batch processes, login and account creation
591 static function getCanonicalName( $name, $validate = 'valid' ) {
592 # Force usernames to capital
594 $name = $wgContLang->ucfirst( $name );
596 # Reject names containing '#'; these will be cleaned up
597 # with title normalisation, but then it's too late to
599 if( strpos( $name, '#' ) !== false )
602 # Clean up name according to title rules
603 $t = Title
::newFromText( $name );
604 if( is_null( $t ) ) {
608 # Reject various classes of invalid names
609 $name = $t->getText();
611 $name = $wgAuth->getCanonicalName( $t->getText() );
613 switch ( $validate ) {
617 if ( !User
::isValidUserName( $name ) ) {
622 if ( !User
::isUsableName( $name ) ) {
627 if ( !User
::isCreatableName( $name ) ) {
632 throw new MWException( 'Invalid parameter value for $validate in '.__METHOD__
);
638 * Count the number of edits of a user
640 * It should not be static and some day should be merged as proper member function / deprecated -- domas
642 * @param int $uid The user ID to check
646 static function edits( $uid ) {
647 wfProfileIn( __METHOD__
);
648 $dbr = wfGetDB( DB_SLAVE
);
649 // check if the user_editcount field has been initialized
650 $field = $dbr->selectField(
651 'user', 'user_editcount',
652 array( 'user_id' => $uid ),
656 if( $field === null ) { // it has not been initialized. do so.
657 $dbw = wfGetDB( DB_MASTER
);
658 $count = $dbr->selectField(
659 'revision', 'count(*)',
660 array( 'rev_user' => $uid ),
665 array( 'user_editcount' => $count ),
666 array( 'user_id' => $uid ),
672 wfProfileOut( __METHOD__
);
677 * Return a random password. Sourced from mt_rand, so it's not particularly secure.
678 * @todo hash random numbers to improve security, like generateToken()
683 static function randomPassword() {
684 global $wgMinimalPasswordLength;
685 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
686 $l = strlen( $pwchars ) - 1;
688 $pwlength = max( 7, $wgMinimalPasswordLength );
689 $digit = mt_rand(0, $pwlength - 1);
691 for ( $i = 0; $i < $pwlength; $i++
) {
692 $np .= $i == $digit ?
chr( mt_rand(48, 57) ) : $pwchars{ mt_rand(0, $l)};
698 * Set cached properties to default. Note: this no longer clears
699 * uncached lazy-initialised properties. The constructor does that instead.
703 function loadDefaults( $name = false ) {
704 wfProfileIn( __METHOD__
);
706 global $wgCookiePrefix;
709 $this->mName
= $name;
710 $this->mRealName
= '';
711 $this->mPassword
= $this->mNewpassword
= '';
712 $this->mNewpassTime
= null;
714 $this->mOptions
= null; # Defer init
716 if ( isset( $_COOKIE[$wgCookiePrefix.'LoggedOut'] ) ) {
717 $this->mTouched
= wfTimestamp( TS_MW
, $_COOKIE[$wgCookiePrefix.'LoggedOut'] );
719 $this->mTouched
= '0'; # Allow any pages to be cached
722 $this->setToken(); # Random
723 $this->mEmailAuthenticated
= null;
724 $this->mEmailToken
= '';
725 $this->mEmailTokenExpires
= null;
726 $this->mRegistration
= wfTimestamp( TS_MW
);
727 $this->mGroups
= array();
729 wfRunHooks( 'UserLoadDefaults', array( $this, $name ) );
731 wfProfileOut( __METHOD__
);
735 * Initialise php session
736 * @deprecated use wfSetupSession()
738 function SetupSession() {
739 wfDeprecated( __METHOD__
);
744 * Load user data from the session or login cookie. If there are no valid
745 * credentials, initialises the user as an anon.
746 * @return true if the user is logged in, false otherwise
748 private function loadFromSession() {
749 global $wgMemc, $wgCookiePrefix;
752 wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
753 if ( $result !== null ) {
757 if ( isset( $_SESSION['wsUserID'] ) ) {
758 if ( 0 != $_SESSION['wsUserID'] ) {
759 $sId = $_SESSION['wsUserID'];
761 $this->loadDefaults();
764 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserID"] ) ) {
765 $sId = intval( $_COOKIE["{$wgCookiePrefix}UserID"] );
766 $_SESSION['wsUserID'] = $sId;
768 $this->loadDefaults();
771 if ( isset( $_SESSION['wsUserName'] ) ) {
772 $sName = $_SESSION['wsUserName'];
773 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserName"] ) ) {
774 $sName = $_COOKIE["{$wgCookiePrefix}UserName"];
775 $_SESSION['wsUserName'] = $sName;
777 $this->loadDefaults();
781 $passwordCorrect = FALSE;
783 if ( !$this->loadFromId() ) {
784 # Not a valid ID, loadFromId has switched the object to anon for us
788 if ( isset( $_SESSION['wsToken'] ) ) {
789 $passwordCorrect = $_SESSION['wsToken'] == $this->mToken
;
791 } else if ( isset( $_COOKIE["{$wgCookiePrefix}Token"] ) ) {
792 $passwordCorrect = $this->mToken
== $_COOKIE["{$wgCookiePrefix}Token"];
795 # No session or persistent login cookie
796 $this->loadDefaults();
800 if ( ( $sName == $this->mName
) && $passwordCorrect ) {
801 $_SESSION['wsToken'] = $this->mToken
;
802 wfDebug( "Logged in from $from\n" );
805 # Invalid credentials
806 wfDebug( "Can't log in from $from, invalid credentials\n" );
807 $this->loadDefaults();
813 * Load user and user_group data from the database
814 * $this->mId must be set, this is how the user is identified.
816 * @return true if the user exists, false if the user is anonymous
819 function loadFromDatabase() {
821 $this->mId
= intval( $this->mId
);
823 /** Anonymous user */
825 $this->loadDefaults();
829 $dbr = wfGetDB( DB_MASTER
);
830 $s = $dbr->selectRow( 'user', '*', array( 'user_id' => $this->mId
), __METHOD__
);
832 if ( $s !== false ) {
833 # Initialise user table data
834 $this->loadFromRow( $s );
835 $this->mGroups
= null; // deferred
836 $this->getEditCount(); // revalidation for nulls
841 $this->loadDefaults();
847 * Initialise the user object from a row from the user table
849 function loadFromRow( $row ) {
850 $this->mDataLoaded
= true;
852 if ( isset( $row->user_id
) ) {
853 $this->mId
= $row->user_id
;
855 $this->mName
= $row->user_name
;
856 $this->mRealName
= $row->user_real_name
;
857 $this->mPassword
= $row->user_password
;
858 $this->mNewpassword
= $row->user_newpassword
;
859 $this->mNewpassTime
= wfTimestampOrNull( TS_MW
, $row->user_newpass_time
);
860 $this->mEmail
= $row->user_email
;
861 $this->decodeOptions( $row->user_options
);
862 $this->mTouched
= wfTimestamp(TS_MW
,$row->user_touched
);
863 $this->mToken
= $row->user_token
;
864 $this->mEmailAuthenticated
= wfTimestampOrNull( TS_MW
, $row->user_email_authenticated
);
865 $this->mEmailToken
= $row->user_email_token
;
866 $this->mEmailTokenExpires
= wfTimestampOrNull( TS_MW
, $row->user_email_token_expires
);
867 $this->mRegistration
= wfTimestampOrNull( TS_MW
, $row->user_registration
);
868 $this->mEditCount
= $row->user_editcount
;
872 * Load the groups from the database if they aren't already loaded
875 function loadGroups() {
876 if ( is_null( $this->mGroups
) ) {
877 $dbr = wfGetDB( DB_MASTER
);
878 $res = $dbr->select( 'user_groups',
880 array( 'ug_user' => $this->mId
),
882 $this->mGroups
= array();
883 while( $row = $dbr->fetchObject( $res ) ) {
884 $this->mGroups
[] = $row->ug_group
;
890 * Clear various cached data stored in this object.
891 * @param string $reloadFrom Reload user and user_groups table data from a
892 * given source. May be "name", "id", "defaults", "session" or false for
895 function clearInstanceCache( $reloadFrom = false ) {
896 $this->mNewtalk
= -1;
897 $this->mDatePreference
= null;
898 $this->mBlockedby
= -1; # Unset
899 $this->mHash
= false;
901 $this->mRights
= null;
902 $this->mEffectiveGroups
= null;
905 $this->mDataLoaded
= false;
906 $this->mFrom
= $reloadFrom;
911 * Combine the language default options with any site-specific options
912 * and add the default language variants.
913 * Not really private cause it's called by Language class
918 static function getDefaultOptions() {
919 global $wgNamespacesToBeSearchedDefault;
921 * Site defaults will override the global/language defaults
923 global $wgDefaultUserOptions, $wgContLang;
924 $defOpt = $wgDefaultUserOptions +
$wgContLang->getDefaultUserOptionOverrides();
927 * default language setting
929 $variant = $wgContLang->getPreferredVariant( false );
930 $defOpt['variant'] = $variant;
931 $defOpt['language'] = $variant;
933 foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
934 $defOpt['searchNs'.$nsnum] = $val;
940 * Get a given default option value.
945 public static function getDefaultOption( $opt ) {
946 $defOpts = self
::getDefaultOptions();
947 if( isset( $defOpts[$opt] ) ) {
948 return $defOpts[$opt];
955 * Get a list of user toggle names
958 static function getToggles() {
960 $extraToggles = array();
961 wfRunHooks( 'UserToggles', array( &$extraToggles ) );
962 return array_merge( self
::$mToggles, $extraToggles, $wgContLang->getExtraUserToggles() );
967 * Get blocking information
969 * @param bool $bFromSlave Specify whether to check slave or master. To improve performance,
970 * non-critical checks are done against slaves. Check when actually saving should be done against
973 function getBlockedStatus( $bFromSlave = true ) {
974 global $wgEnableSorbs, $wgProxyWhitelist;
976 if ( -1 != $this->mBlockedby
) {
977 wfDebug( "User::getBlockedStatus: already loaded.\n" );
981 wfProfileIn( __METHOD__
);
982 wfDebug( __METHOD__
.": checking...\n" );
984 // Initialize data...
985 // Otherwise something ends up stomping on $this->mBlockedby when
986 // things get lazy-loaded later, causing false positive block hits
987 // due to -1 !== 0. Probably session-related... Nothing should be
988 // overwriting mBlockedby, surely?
991 $this->mBlockedby
= 0;
992 $this->mHideName
= 0;
995 if ($this->isAllowed( 'ipblock-exempt' ) ) {
996 # Exempt from all types of IP-block
1001 $this->mBlock
= new Block();
1002 $this->mBlock
->fromMaster( !$bFromSlave );
1003 if ( $this->mBlock
->load( $ip , $this->mId
) ) {
1004 wfDebug( __METHOD__
.": Found block.\n" );
1005 $this->mBlockedby
= $this->mBlock
->mBy
;
1006 $this->mBlockreason
= $this->mBlock
->mReason
;
1007 $this->mHideName
= $this->mBlock
->mHideName
;
1008 if ( $this->isLoggedIn() ) {
1009 $this->spreadBlock();
1012 $this->mBlock
= null;
1013 wfDebug( __METHOD__
.": No block.\n" );
1017 if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
1019 if ( wfIsLocallyBlockedProxy( $ip ) ) {
1020 $this->mBlockedby
= wfMsg( 'proxyblocker' );
1021 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
1025 if ( !$this->mBlockedby
&& $wgEnableSorbs && !$this->getID() ) {
1026 if ( $this->inSorbsBlacklist( $ip ) ) {
1027 $this->mBlockedby
= wfMsg( 'sorbs' );
1028 $this->mBlockreason
= wfMsg( 'sorbsreason' );
1034 wfRunHooks( 'GetBlockedStatus', array( &$this ) );
1036 wfProfileOut( __METHOD__
);
1039 function inSorbsBlacklist( $ip ) {
1040 global $wgEnableSorbs, $wgSorbsUrl;
1042 return $wgEnableSorbs &&
1043 $this->inDnsBlacklist( $ip, $wgSorbsUrl );
1046 function inDnsBlacklist( $ip, $base ) {
1047 wfProfileIn( __METHOD__
);
1053 if ( preg_match( '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $m ) ) {
1055 for ( $i=4; $i>=1; $i-- ) {
1056 $host .= $m[$i] . '.';
1061 $ipList = gethostbynamel( $host );
1064 wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
1067 wfDebug( "Requested $host, not found in $base.\n" );
1071 wfProfileOut( __METHOD__
);
1076 * Is this user subject to rate limiting?
1080 public function isPingLimitable() {
1081 global $wgRateLimitsExcludedGroups;
1082 if( array_intersect( $this->getEffectiveGroups(), $wgRateLimitsExcludedGroups ) ) {
1083 // Deprecated, but kept for backwards-compatibility config
1086 return !$this->isAllowed('noratelimit');
1090 * Primitive rate limits: enforce maximum actions per time period
1091 * to put a brake on flooding.
1093 * Note: when using a shared cache like memcached, IP-address
1094 * last-hit counters will be shared across wikis.
1096 * @return bool true if a rate limiter was tripped
1099 function pingLimiter( $action='edit' ) {
1101 # Call the 'PingLimiter' hook
1103 if( !wfRunHooks( 'PingLimiter', array( &$this, $action, $result ) ) ) {
1107 global $wgRateLimits;
1108 if( !isset( $wgRateLimits[$action] ) ) {
1112 # Some groups shouldn't trigger the ping limiter, ever
1113 if( !$this->isPingLimitable() )
1116 global $wgMemc, $wgRateLimitLog;
1117 wfProfileIn( __METHOD__
);
1119 $limits = $wgRateLimits[$action];
1121 $id = $this->getId();
1125 if( isset( $limits['anon'] ) && $id == 0 ) {
1126 $keys[wfMemcKey( 'limiter', $action, 'anon' )] = $limits['anon'];
1129 if( isset( $limits['user'] ) && $id != 0 ) {
1130 $userLimit = $limits['user'];
1132 if( $this->isNewbie() ) {
1133 if( isset( $limits['newbie'] ) && $id != 0 ) {
1134 $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['newbie'];
1136 if( isset( $limits['ip'] ) ) {
1137 $keys["mediawiki:limiter:$action:ip:$ip"] = $limits['ip'];
1140 if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $ip, $matches ) ) {
1141 $subnet = $matches[1];
1142 $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
1145 // Check for group-specific permissions
1146 // If more than one group applies, use the group with the highest limit
1147 foreach ( $this->getGroups() as $group ) {
1148 if ( isset( $limits[$group] ) ) {
1149 if ( $userLimit === false ||
$limits[$group] > $userLimit ) {
1150 $userLimit = $limits[$group];
1154 // Set the user limit key
1155 if ( $userLimit !== false ) {
1156 wfDebug( __METHOD__
.": effective user limit: $userLimit\n" );
1157 $keys[ wfMemcKey( 'limiter', $action, 'user', $id ) ] = $userLimit;
1161 foreach( $keys as $key => $limit ) {
1162 list( $max, $period ) = $limit;
1163 $summary = "(limit $max in {$period}s)";
1164 $count = $wgMemc->get( $key );
1166 if( $count > $max ) {
1167 wfDebug( __METHOD__
.": tripped! $key at $count $summary\n" );
1168 if( $wgRateLimitLog ) {
1169 @error_log
( wfTimestamp( TS_MW
) . ' ' . wfWikiID() . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
1173 wfDebug( __METHOD__
.": ok. $key at $count $summary\n" );
1176 wfDebug( __METHOD__
.": adding record for $key $summary\n" );
1177 $wgMemc->add( $key, 1, intval( $period ) );
1179 $wgMemc->incr( $key );
1182 wfProfileOut( __METHOD__
);
1187 * Check if user is blocked
1188 * @return bool True if blocked, false otherwise
1190 function isBlocked( $bFromSlave = true ) { // hacked from false due to horrible probs on site
1191 wfDebug( "User::isBlocked: enter\n" );
1192 $this->getBlockedStatus( $bFromSlave );
1193 return $this->mBlockedby
!== 0;
1197 * Check if user is blocked from editing a particular article
1199 function isBlockedFrom( $title, $bFromSlave = false ) {
1200 global $wgBlockAllowsUTEdit;
1201 wfProfileIn( __METHOD__
);
1202 wfDebug( __METHOD__
.": enter\n" );
1204 wfDebug( __METHOD__
.": asking isBlocked()\n" );
1205 $blocked = $this->isBlocked( $bFromSlave );
1206 # If a user's name is suppressed, they cannot make edits anywhere
1207 if ( !$this->mHideName
&& $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
1208 $title->getNamespace() == NS_USER_TALK
) {
1210 wfDebug( __METHOD__
.": self-talk page, ignoring any blocks\n" );
1212 wfProfileOut( __METHOD__
);
1217 * Get name of blocker
1218 * @return string name of blocker
1220 function blockedBy() {
1221 $this->getBlockedStatus();
1222 return $this->mBlockedby
;
1226 * Get blocking reason
1227 * @return string Blocking reason
1229 function blockedFor() {
1230 $this->getBlockedStatus();
1231 return $this->mBlockreason
;
1235 * Get the user ID. Returns 0 if the user is anonymous or nonexistent.
1238 if( $this->mId
=== null and $this->mName
!== null
1239 and User
::isIP( $this->mName
) ) {
1240 // Special case, we know the user is anonymous
1242 } elseif( $this->mId
=== null ) {
1243 // Don't load if this was initialized from an ID
1250 * Set the user and reload all fields according to that ID
1252 function setId( $v ) {
1254 $this->clearInstanceCache( 'id' );
1258 * Get the user name, or the IP for anons
1260 function getName() {
1261 if ( !$this->mDataLoaded
&& $this->mFrom
== 'name' ) {
1262 # Special case optimisation
1263 return $this->mName
;
1266 if ( $this->mName
=== false ) {
1268 $this->mName
= IP
::sanitizeIP( wfGetIP() );
1270 return $this->mName
;
1275 * Set the user name.
1277 * This does not reload fields from the database according to the given
1278 * name. Rather, it is used to create a temporary "nonexistent user" for
1279 * later addition to the database. It can also be used to set the IP
1280 * address for an anonymous user to something other than the current
1283 * User::newFromName() has rougly the same function, when the named user
1286 function setName( $str ) {
1288 $this->mName
= $str;
1292 * Return the title dbkey form of the name, for eg user pages.
1296 function getTitleKey() {
1297 return str_replace( ' ', '_', $this->getName() );
1300 function getNewtalk() {
1303 # Load the newtalk status if it is unloaded (mNewtalk=-1)
1304 if( $this->mNewtalk
=== -1 ) {
1305 $this->mNewtalk
= false; # reset talk page status
1307 # Check memcached separately for anons, who have no
1308 # entire User object stored in there.
1311 $key = wfMemcKey( 'newtalk', 'ip', $this->getName() );
1312 $newtalk = $wgMemc->get( $key );
1313 if( strval( $newtalk ) !== '' ) {
1314 $this->mNewtalk
= (bool)$newtalk;
1316 // Since we are caching this, make sure it is up to date by getting it
1318 $this->mNewtalk
= $this->checkNewtalk( 'user_ip', $this->getName(), true );
1319 $wgMemc->set( $key, (int)$this->mNewtalk
, 1800 );
1322 $this->mNewtalk
= $this->checkNewtalk( 'user_id', $this->mId
);
1326 return (bool)$this->mNewtalk
;
1330 * Return the talk page(s) this user has new messages on.
1332 function getNewMessageLinks() {
1334 if (!wfRunHooks('UserRetrieveNewTalks', array(&$this, &$talks)))
1337 if (!$this->getNewtalk())
1339 $up = $this->getUserPage();
1340 $utp = $up->getTalkPage();
1341 return array(array("wiki" => wfWikiID(), "link" => $utp->getLocalURL()));
1346 * Perform a user_newtalk check, uncached.
1347 * Use getNewtalk for a cached check.
1349 * @param string $field
1351 * @param bool $fromMaster True to fetch from the master, false for a slave
1355 function checkNewtalk( $field, $id, $fromMaster = false ) {
1356 if ( $fromMaster ) {
1357 $db = wfGetDB( DB_MASTER
);
1359 $db = wfGetDB( DB_SLAVE
);
1361 $ok = $db->selectField( 'user_newtalk', $field,
1362 array( $field => $id ), __METHOD__
);
1363 return $ok !== false;
1368 * @param string $field
1372 function updateNewtalk( $field, $id ) {
1373 $dbw = wfGetDB( DB_MASTER
);
1374 $dbw->insert( 'user_newtalk',
1375 array( $field => $id ),
1378 if ( $dbw->affectedRows() ) {
1379 wfDebug( __METHOD__
.": set on ($field, $id)\n" );
1382 wfDebug( __METHOD__
." already set ($field, $id)\n" );
1388 * Clear the new messages flag for the given user
1389 * @param string $field
1393 function deleteNewtalk( $field, $id ) {
1394 $dbw = wfGetDB( DB_MASTER
);
1395 $dbw->delete( 'user_newtalk',
1396 array( $field => $id ),
1398 if ( $dbw->affectedRows() ) {
1399 wfDebug( __METHOD__
.": killed on ($field, $id)\n" );
1402 wfDebug( __METHOD__
.": already gone ($field, $id)\n" );
1408 * Update the 'You have new messages!' status.
1411 function setNewtalk( $val ) {
1412 if( wfReadOnly() ) {
1417 $this->mNewtalk
= $val;
1419 if( $this->isAnon() ) {
1421 $id = $this->getName();
1424 $id = $this->getId();
1429 $changed = $this->updateNewtalk( $field, $id );
1431 $changed = $this->deleteNewtalk( $field, $id );
1434 if( $this->isAnon() ) {
1435 // Anons have a separate memcached space, since
1436 // user records aren't kept for them.
1437 $key = wfMemcKey( 'newtalk', 'ip', $id );
1438 $wgMemc->set( $key, $val ?
1 : 0, 1800 );
1441 $this->invalidateCache();
1446 * Generate a current or new-future timestamp to be stored in the
1447 * user_touched field when we update things.
1449 private static function newTouchedTimestamp() {
1450 global $wgClockSkewFudge;
1451 return wfTimestamp( TS_MW
, time() +
$wgClockSkewFudge );
1455 * Clear user data from memcached.
1456 * Use after applying fun updates to the database; caller's
1457 * responsibility to update user_touched if appropriate.
1459 * Called implicitly from invalidateCache() and saveSettings().
1461 private function clearSharedCache() {
1464 $wgMemc->delete( wfMemcKey( 'user', 'id', $this->mId
) );
1469 * Immediately touch the user data cache for this account.
1470 * Updates user_touched field, and removes account data from memcached
1471 * for reload on the next hit.
1473 function invalidateCache() {
1476 $this->mTouched
= self
::newTouchedTimestamp();
1478 $dbw = wfGetDB( DB_MASTER
);
1479 $dbw->update( 'user',
1480 array( 'user_touched' => $dbw->timestamp( $this->mTouched
) ),
1481 array( 'user_id' => $this->mId
),
1484 $this->clearSharedCache();
1488 function validateCache( $timestamp ) {
1490 return ($timestamp >= $this->mTouched
);
1494 * Set the password and reset the random token
1495 * Calls through to authentication plugin if necessary;
1496 * will have no effect if the auth plugin refuses to
1497 * pass the change through or if the legal password
1500 * As a special case, setting the password to null
1501 * wipes it, so the account cannot be logged in until
1502 * a new password is set, for instance via e-mail.
1504 * @param string $str
1505 * @throws PasswordError on failure
1507 function setPassword( $str ) {
1510 if( $str !== null ) {
1511 if( !$wgAuth->allowPasswordChange() ) {
1512 throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
1515 if( !$this->isValidPassword( $str ) ) {
1516 global $wgMinimalPasswordLength;
1517 throw new PasswordError( wfMsgExt( 'passwordtooshort', array( 'parsemag' ),
1518 $wgMinimalPasswordLength ) );
1522 if( !$wgAuth->setPassword( $this, $str ) ) {
1523 throw new PasswordError( wfMsg( 'externaldberror' ) );
1526 $this->setInternalPassword( $str );
1532 * Set the password and reset the random token no matter
1535 * @param string $str
1537 function setInternalPassword( $str ) {
1541 if( $str === null ) {
1542 // Save an invalid hash...
1543 $this->mPassword
= '';
1545 $this->mPassword
= self
::crypt( $str );
1547 $this->mNewpassword
= '';
1548 $this->mNewpassTime
= null;
1551 function getToken() {
1553 return $this->mToken
;
1557 * Set the random token (used for persistent authentication)
1558 * Called from loadDefaults() among other places.
1561 function setToken( $token = false ) {
1562 global $wgSecretKey, $wgProxyKey;
1565 if ( $wgSecretKey ) {
1566 $key = $wgSecretKey;
1567 } elseif ( $wgProxyKey ) {
1572 $this->mToken
= md5( $key . mt_rand( 0, 0x7fffffff ) . wfWikiID() . $this->mId
);
1574 $this->mToken
= $token;
1578 function setCookiePassword( $str ) {
1580 $this->mCookiePassword
= md5( $str );
1584 * Set the password for a password reminder or new account email
1585 * Sets the user_newpass_time field if $throttle is true
1587 function setNewpassword( $str, $throttle = true ) {
1589 $this->mNewpassword
= self
::crypt( $str );
1591 $this->mNewpassTime
= wfTimestampNow();
1596 * Returns true if a password reminder email has already been sent within
1597 * the last $wgPasswordReminderResendTime hours
1599 function isPasswordReminderThrottled() {
1600 global $wgPasswordReminderResendTime;
1602 if ( !$this->mNewpassTime ||
!$wgPasswordReminderResendTime ) {
1605 $expiry = wfTimestamp( TS_UNIX
, $this->mNewpassTime
) +
$wgPasswordReminderResendTime * 3600;
1606 return time() < $expiry;
1609 function getEmail() {
1611 wfRunHooks( 'UserGetEmail', array( $this, &$this->mEmail
) );
1612 return $this->mEmail
;
1615 function getEmailAuthenticationTimestamp() {
1617 wfRunHooks( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated
) );
1618 return $this->mEmailAuthenticated
;
1621 function setEmail( $str ) {
1623 $this->mEmail
= $str;
1624 wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail
) );
1627 function getRealName() {
1629 return $this->mRealName
;
1632 function setRealName( $str ) {
1634 $this->mRealName
= $str;
1638 * @param string $oname The option to check
1639 * @param string $defaultOverride A default value returned if the option does not exist
1642 function getOption( $oname, $defaultOverride = '' ) {
1645 if ( is_null( $this->mOptions
) ) {
1646 if($defaultOverride != '') {
1647 return $defaultOverride;
1649 $this->mOptions
= User
::getDefaultOptions();
1652 if ( array_key_exists( $oname, $this->mOptions
) ) {
1653 return trim( $this->mOptions
[$oname] );
1655 return $defaultOverride;
1660 * Get the user's date preference, including some important migration for
1663 function getDatePreference() {
1664 if ( is_null( $this->mDatePreference
) ) {
1666 $value = $this->getOption( 'date' );
1667 $map = $wgLang->getDatePreferenceMigrationMap();
1668 if ( isset( $map[$value] ) ) {
1669 $value = $map[$value];
1671 $this->mDatePreference
= $value;
1673 return $this->mDatePreference
;
1677 * @param string $oname The option to check
1678 * @return bool False if the option is not selected, true if it is
1680 function getBoolOption( $oname ) {
1681 return (bool)$this->getOption( $oname );
1685 * Get an option as an integer value from the source string.
1686 * @param string $oname The option to check
1687 * @param int $default Optional value to return if option is unset/blank.
1690 function getIntOption( $oname, $default=0 ) {
1691 $val = $this->getOption( $oname );
1695 return intval( $val );
1698 function setOption( $oname, $val ) {
1700 if ( is_null( $this->mOptions
) ) {
1701 $this->mOptions
= User
::getDefaultOptions();
1703 if ( $oname == 'skin' ) {
1704 # Clear cached skin, so the new one displays immediately in Special:Preferences
1705 unset( $this->mSkin
);
1707 // Filter out any newlines that may have passed through input validation.
1708 // Newlines are used to separate items in the options blob.
1710 $val = str_replace( "\r\n", "\n", $val );
1711 $val = str_replace( "\r", "\n", $val );
1712 $val = str_replace( "\n", " ", $val );
1714 // Explicitly NULL values should refer to defaults
1715 global $wgDefaultUserOptions;
1716 if( is_null($val) && isset($wgDefaultUserOptions[$oname]) ) {
1717 $val = $wgDefaultUserOptions[$oname];
1719 $this->mOptions
[$oname] = $val;
1722 function getRights() {
1723 if ( is_null( $this->mRights
) ) {
1724 $this->mRights
= self
::getGroupPermissions( $this->getEffectiveGroups() );
1725 wfRunHooks( 'UserGetRights', array( $this, &$this->mRights
) );
1726 // Force reindexation of rights when a hook has unset one of them
1727 $this->mRights
= array_values( $this->mRights
);
1729 return $this->mRights
;
1733 * Get the list of explicit group memberships this user has.
1734 * The implicit * and user groups are not included.
1735 * @return array of strings
1737 function getGroups() {
1739 return $this->mGroups
;
1743 * Get the list of implicit group memberships this user has.
1744 * This includes all explicit groups, plus 'user' if logged in,
1745 * '*' for all accounts and autopromoted groups
1746 * @param boolean $recache Don't use the cache
1747 * @return array of strings
1749 function getEffectiveGroups( $recache = false ) {
1750 if ( $recache ||
is_null( $this->mEffectiveGroups
) ) {
1751 $this->mEffectiveGroups
= $this->getGroups();
1752 $this->mEffectiveGroups
[] = '*';
1753 if( $this->getId() ) {
1754 $this->mEffectiveGroups
[] = 'user';
1756 $this->mEffectiveGroups
= array_unique( array_merge(
1757 $this->mEffectiveGroups
,
1758 Autopromote
::getAutopromoteGroups( $this )
1761 # Hook for additional groups
1762 wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups
) );
1765 return $this->mEffectiveGroups
;
1768 /* Return the edit count for the user. This is where User::edits should have been */
1769 function getEditCount() {
1771 if ( !isset( $this->mEditCount
) ) {
1772 /* Populate the count, if it has not been populated yet */
1773 $this->mEditCount
= User
::edits($this->mId
);
1775 return $this->mEditCount
;
1783 * Add the user to the given group.
1784 * This takes immediate effect.
1785 * @param string $group
1787 function addGroup( $group ) {
1788 $dbw = wfGetDB( DB_MASTER
);
1789 if( $this->getId() ) {
1790 $dbw->insert( 'user_groups',
1792 'ug_user' => $this->getID(),
1793 'ug_group' => $group,
1796 array( 'IGNORE' ) );
1799 $this->loadGroups();
1800 $this->mGroups
[] = $group;
1801 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups( true ) );
1803 $this->invalidateCache();
1807 * Remove the user from the given group.
1808 * This takes immediate effect.
1809 * @param string $group
1811 function removeGroup( $group ) {
1813 $dbw = wfGetDB( DB_MASTER
);
1814 $dbw->delete( 'user_groups',
1816 'ug_user' => $this->getID(),
1817 'ug_group' => $group,
1819 'User::removeGroup' );
1821 $this->loadGroups();
1822 $this->mGroups
= array_diff( $this->mGroups
, array( $group ) );
1823 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups( true ) );
1825 $this->invalidateCache();
1830 * A more legible check for non-anonymousness.
1831 * Returns true if the user is not an anonymous visitor.
1835 function isLoggedIn() {
1836 return $this->getID() != 0;
1840 * A more legible check for anonymousness.
1841 * Returns true if the user is an anonymous visitor.
1846 return !$this->isLoggedIn();
1850 * Whether the user is a bot
1854 wfDeprecated( __METHOD__
);
1855 return $this->isAllowed( 'bot' );
1859 * Check if user is allowed to access a feature / make an action
1860 * @param string $action Action to be checked
1861 * @return boolean True: action is allowed, False: action should not be allowed
1863 function isAllowed($action='') {
1864 if ( $action === '' )
1865 // In the spirit of DWIM
1868 return in_array( $action, $this->getRights() );
1872 * Check whether to enable recent changes patrol features for this user
1875 public function useRCPatrol() {
1876 global $wgUseRCPatrol;
1877 return( $wgUseRCPatrol && ($this->isAllowed('patrol') ||
$this->isAllowed('patrolmarks')) );
1881 * Check whether to enable recent changes patrol features for this user
1884 public function useNPPatrol() {
1885 global $wgUseRCPatrol, $wgUseNPPatrol;
1886 return( ($wgUseRCPatrol ||
$wgUseNPPatrol) && ($this->isAllowed('patrol') ||
$this->isAllowed('patrolmarks')) );
1890 * Load a skin if it doesn't exist or return it
1891 * @todo FIXME : need to check the old failback system [AV]
1893 function &getSkin() {
1895 if ( ! isset( $this->mSkin
) ) {
1896 wfProfileIn( __METHOD__
);
1899 $userSkin = $this->getOption( 'skin' );
1900 $userSkin = $wgRequest->getVal('useskin', $userSkin);
1902 $this->mSkin
=& Skin
::newFromKey( $userSkin );
1903 wfProfileOut( __METHOD__
);
1905 return $this->mSkin
;
1909 * @param string $title Article title to look at
1913 * Check watched status of an article
1914 * @return bool True if article is watched
1916 function isWatched( $title ) {
1917 $wl = WatchedItem
::fromUserTitle( $this, $title );
1918 return $wl->isWatched();
1924 function addWatch( $title ) {
1925 $wl = WatchedItem
::fromUserTitle( $this, $title );
1927 $this->invalidateCache();
1931 * Stop watching an article
1933 function removeWatch( $title ) {
1934 $wl = WatchedItem
::fromUserTitle( $this, $title );
1936 $this->invalidateCache();
1940 * Clear the user's notification timestamp for the given title.
1941 * If e-notif e-mails are on, they will receive notification mails on
1942 * the next change of the page if it's watched etc.
1944 function clearNotification( &$title ) {
1945 global $wgUser, $wgUseEnotif, $wgShowUpdatedMarker;
1947 # Do nothing if the database is locked to writes
1948 if( wfReadOnly() ) {
1952 if ($title->getNamespace() == NS_USER_TALK
&&
1953 $title->getText() == $this->getName() ) {
1954 if (!wfRunHooks('UserClearNewTalkNotification', array(&$this)))
1956 $this->setNewtalk( false );
1959 if( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
1963 if( $this->isAnon() ) {
1964 // Nothing else to do...
1968 // Only update the timestamp if the page is being watched.
1969 // The query to find out if it is watched is cached both in memcached and per-invocation,
1970 // and when it does have to be executed, it can be on a slave
1971 // If this is the user's newtalk page, we always update the timestamp
1972 if ($title->getNamespace() == NS_USER_TALK
&&
1973 $title->getText() == $wgUser->getName())
1976 } elseif ( $this->getId() == $wgUser->getId() ) {
1977 $watched = $title->userIsWatching();
1982 // If the page is watched by the user (or may be watched), update the timestamp on any
1983 // any matching rows
1985 $dbw = wfGetDB( DB_MASTER
);
1986 $dbw->update( 'watchlist',
1988 'wl_notificationtimestamp' => NULL
1989 ), array( /* WHERE */
1990 'wl_title' => $title->getDBkey(),
1991 'wl_namespace' => $title->getNamespace(),
1992 'wl_user' => $this->getID()
1993 ), 'User::clearLastVisited'
2001 * Resets all of the given user's page-change notification timestamps.
2002 * If e-notif e-mails are on, they will receive notification mails on
2003 * the next change of any watched page.
2005 * @param int $currentUser user ID number
2008 function clearAllNotifications( $currentUser ) {
2009 global $wgUseEnotif, $wgShowUpdatedMarker;
2010 if ( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
2011 $this->setNewtalk( false );
2014 if( $currentUser != 0 ) {
2015 $dbw = wfGetDB( DB_MASTER
);
2016 $dbw->update( 'watchlist',
2018 'wl_notificationtimestamp' => NULL
2019 ), array( /* WHERE */
2020 'wl_user' => $currentUser
2023 # We also need to clear here the "you have new message" notification for the own user_talk page
2024 # This is cleared one page view later in Article::viewUpdates();
2030 * @return string Encoding options
2032 function encodeOptions() {
2034 if ( is_null( $this->mOptions
) ) {
2035 $this->mOptions
= User
::getDefaultOptions();
2038 foreach ( $this->mOptions
as $oname => $oval ) {
2039 array_push( $a, $oname.'='.$oval );
2041 $s = implode( "\n", $a );
2048 function decodeOptions( $str ) {
2049 $this->mOptions
= array();
2050 $a = explode( "\n", $str );
2051 foreach ( $a as $s ) {
2053 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
2054 $this->mOptions
[$m[1]] = $m[2];
2059 protected function setCookie( $name, $value, $exp=0 ) {
2060 global $wgCookiePrefix,$wgCookieDomain,$wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
2062 $exp = time() +
$wgCookieExpiration;
2064 $httpOnlySafe = wfHttpOnlySafe();
2065 wfDebugLog( 'cookie',
2066 'setcookie: "' . implode( '", "',
2068 $wgCookiePrefix . $name,
2074 $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
2075 if( $httpOnlySafe && isset( $wgCookieHttpOnly ) ) {
2076 setcookie( $wgCookiePrefix . $name,
2082 $wgCookieHttpOnly );
2084 // setcookie() fails on PHP 5.1 if you give it future-compat paramters.
2086 setcookie( $wgCookiePrefix . $name,
2095 protected function clearCookie( $name ) {
2096 $this->setCookie( $name, '', time() - 86400 );
2099 function setCookies() {
2101 if ( 0 == $this->mId
) return;
2103 'wsUserID' => $this->mId
,
2104 'wsToken' => $this->mToken
,
2105 'wsUserName' => $this->getName()
2108 'UserID' => $this->mId
,
2109 'UserName' => $this->getName(),
2111 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
2112 $cookies['Token'] = $this->mToken
;
2114 $cookies['Token'] = false;
2117 wfRunHooks( 'UserSetCookies', array( $this, &$session, &$cookies ) );
2118 $_SESSION = $session +
$_SESSION;
2119 foreach ( $cookies as $name => $value ) {
2120 if ( $value === false ) {
2121 $this->clearCookie( $name );
2123 $this->setCookie( $name, $value );
2133 if( wfRunHooks( 'UserLogout', array(&$this) ) ) {
2139 * Really logout user
2140 * Clears the cookies and session, resets the instance cache
2142 function doLogout() {
2143 $this->clearInstanceCache( 'defaults' );
2145 $_SESSION['wsUserID'] = 0;
2147 $this->clearCookie( 'UserID' );
2148 $this->clearCookie( 'Token' );
2150 # Remember when user logged out, to prevent seeing cached pages
2151 $this->setCookie( 'LoggedOut', wfTimestampNow(), time() +
86400 );
2155 * Save object settings into database
2156 * @todo Only rarely do all these fields need to be set!
2158 function saveSettings() {
2160 if ( wfReadOnly() ) { return; }
2161 if ( 0 == $this->mId
) { return; }
2163 $this->mTouched
= self
::newTouchedTimestamp();
2165 $dbw = wfGetDB( DB_MASTER
);
2166 $dbw->update( 'user',
2168 'user_name' => $this->mName
,
2169 'user_password' => $this->mPassword
,
2170 'user_newpassword' => $this->mNewpassword
,
2171 'user_newpass_time' => $dbw->timestampOrNull( $this->mNewpassTime
),
2172 'user_real_name' => $this->mRealName
,
2173 'user_email' => $this->mEmail
,
2174 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
2175 'user_options' => $this->encodeOptions(),
2176 'user_touched' => $dbw->timestamp($this->mTouched
),
2177 'user_token' => $this->mToken
,
2178 'user_email_token' => $this->mEmailToken
,
2179 'user_email_token_expires' => $dbw->timestampOrNull( $this->mEmailTokenExpires
),
2180 ), array( /* WHERE */
2181 'user_id' => $this->mId
2184 wfRunHooks( 'UserSaveSettings', array( $this ) );
2185 $this->clearSharedCache();
2189 * Checks if a user with the given name exists, returns the ID.
2191 function idForName() {
2192 $s = trim( $this->getName() );
2193 if ( $s === '' ) return 0;
2195 $dbr = wfGetDB( DB_SLAVE
);
2196 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), __METHOD__
);
2197 if ( $id === false ) {
2204 * Add a user to the database, return the user object
2206 * @param string $name The user's name
2207 * @param array $params Associative array of non-default parameters to save to the database:
2208 * password The user's password. Password logins will be disabled if this is omitted.
2209 * newpassword A temporary password mailed to the user
2210 * email The user's email address
2211 * email_authenticated The email authentication timestamp
2212 * real_name The user's real name
2213 * options An associative array of non-default options
2214 * token Random authentication token. Do not set.
2215 * registration Registration timestamp. Do not set.
2217 * @return User object, or null if the username already exists
2219 static function createNew( $name, $params = array() ) {
2222 if ( isset( $params['options'] ) ) {
2223 $user->mOptions
= $params['options'] +
$user->mOptions
;
2224 unset( $params['options'] );
2226 $dbw = wfGetDB( DB_MASTER
);
2227 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
2229 'user_id' => $seqVal,
2230 'user_name' => $name,
2231 'user_password' => $user->mPassword
,
2232 'user_newpassword' => $user->mNewpassword
,
2233 'user_newpass_time' => $dbw->timestamp( $user->mNewpassTime
),
2234 'user_email' => $user->mEmail
,
2235 'user_email_authenticated' => $dbw->timestampOrNull( $user->mEmailAuthenticated
),
2236 'user_real_name' => $user->mRealName
,
2237 'user_options' => $user->encodeOptions(),
2238 'user_token' => $user->mToken
,
2239 'user_registration' => $dbw->timestamp( $user->mRegistration
),
2240 'user_editcount' => 0,
2242 foreach ( $params as $name => $value ) {
2243 $fields["user_$name"] = $value;
2245 $dbw->insert( 'user', $fields, __METHOD__
, array( 'IGNORE' ) );
2246 if ( $dbw->affectedRows() ) {
2247 $newUser = User
::newFromId( $dbw->insertId() );
2255 * Add an existing user object to the database
2257 function addToDatabase() {
2259 $dbw = wfGetDB( DB_MASTER
);
2260 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
2261 $dbw->insert( 'user',
2263 'user_id' => $seqVal,
2264 'user_name' => $this->mName
,
2265 'user_password' => $this->mPassword
,
2266 'user_newpassword' => $this->mNewpassword
,
2267 'user_newpass_time' => $dbw->timestamp( $this->mNewpassTime
),
2268 'user_email' => $this->mEmail
,
2269 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
2270 'user_real_name' => $this->mRealName
,
2271 'user_options' => $this->encodeOptions(),
2272 'user_token' => $this->mToken
,
2273 'user_registration' => $dbw->timestamp( $this->mRegistration
),
2274 'user_editcount' => 0,
2277 $this->mId
= $dbw->insertId();
2279 # Clear instance cache other than user table data, which is already accurate
2280 $this->clearInstanceCache();
2284 * If the (non-anonymous) user is blocked, this function will block any IP address
2285 * that they successfully log on from.
2287 function spreadBlock() {
2288 wfDebug( __METHOD__
."()\n" );
2290 if ( $this->mId
== 0 ) {
2294 $userblock = Block
::newFromDB( '', $this->mId
);
2295 if ( !$userblock ) {
2299 $userblock->doAutoblock( wfGetIp() );
2304 * Generate a string which will be different for any combination of
2305 * user options which would produce different parser output.
2306 * This will be used as part of the hash key for the parser cache,
2307 * so users will the same options can share the same cached data
2310 * Extensions which require it should install 'PageRenderingHash' hook,
2311 * which will give them a chance to modify this key based on their own
2316 function getPageRenderingHash() {
2317 global $wgContLang, $wgUseDynamicDates, $wgLang;
2319 return $this->mHash
;
2322 // stubthreshold is only included below for completeness,
2323 // it will always be 0 when this function is called by parsercache.
2325 $confstr = $this->getOption( 'math' );
2326 $confstr .= '!' . $this->getOption( 'stubthreshold' );
2327 if ( $wgUseDynamicDates ) {
2328 $confstr .= '!' . $this->getDatePreference();
2330 $confstr .= '!' . ($this->getOption( 'numberheadings' ) ?
'1' : '');
2331 $confstr .= '!' . $wgLang->getCode();
2332 $confstr .= '!' . $this->getOption( 'thumbsize' );
2333 // add in language specific options, if any
2334 $extra = $wgContLang->getExtraHashOptions();
2337 // Give a chance for extensions to modify the hash, if they have
2338 // extra options or other effects on the parser cache.
2339 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
2341 // Make it a valid memcached key fragment
2342 $confstr = str_replace( ' ', '_', $confstr );
2343 $this->mHash
= $confstr;
2347 function isBlockedFromCreateAccount() {
2348 $this->getBlockedStatus();
2349 return $this->mBlock
&& $this->mBlock
->mCreateAccount
;
2353 * Determine if the user is blocked from using Special:Emailuser.
2358 function isBlockedFromEmailuser() {
2359 $this->getBlockedStatus();
2360 return $this->mBlock
&& $this->mBlock
->mBlockEmail
;
2363 function isAllowedToCreateAccount() {
2364 return $this->isAllowed( 'createaccount' ) && !$this->isBlockedFromCreateAccount();
2370 function setLoaded( $loaded ) {
2371 wfDeprecated( __METHOD__
);
2375 * Get this user's personal page title.
2380 function getUserPage() {
2381 return Title
::makeTitle( NS_USER
, $this->getName() );
2385 * Get this user's talk page title.
2390 function getTalkPage() {
2391 $title = $this->getUserPage();
2392 return $title->getTalkPage();
2398 function getMaxID() {
2399 static $res; // cache
2401 if ( isset( $res ) )
2404 $dbr = wfGetDB( DB_SLAVE
);
2405 return $res = $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
2410 * Determine whether the user is a newbie. Newbies are either
2411 * anonymous IPs, or the most recently created accounts.
2412 * @return bool True if it is a newbie.
2414 function isNewbie() {
2415 return !$this->isAllowed( 'autoconfirmed' );
2419 * Is the user active? We check to see if they've made at least
2420 * X number of edits in the last Y days.
2422 * @return bool true if the user is active, false if not
2424 public function isActiveEditor() {
2425 global $wgActiveUserEditCount, $wgActiveUserDays;
2426 $dbr = wfGetDB( DB_SLAVE
);
2428 // Stolen without shame from RC
2429 $cutoff_unixtime = time() - ( $wgActiveUserDays * 86400 );
2430 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
2431 $oldTime = $dbr->addQuotes( $dbr->timestamp( $cutoff_unixtime ) );
2433 $res = $dbr->select( 'revision', '1',
2434 array( 'rev_user_text' => $this->getName(), "rev_timestamp > $oldTime"),
2436 array('LIMIT' => $wgActiveUserEditCount ) );
2438 $count = $dbr->numRows($res);
2439 $dbr->freeResult($res);
2441 return $count == $wgActiveUserEditCount;
2445 * Check to see if the given clear-text password is one of the accepted passwords
2446 * @param string $password User password.
2447 * @return bool True if the given password is correct otherwise False.
2449 function checkPassword( $password ) {
2453 // Even though we stop people from creating passwords that
2454 // are shorter than this, doesn't mean people wont be able
2455 // to. Certain authentication plugins do NOT want to save
2456 // domain passwords in a mysql database, so we should
2457 // check this (incase $wgAuth->strict() is false).
2458 if( !$this->isValidPassword( $password ) ) {
2462 if( $wgAuth->authenticate( $this->getName(), $password ) ) {
2464 } elseif( $wgAuth->strict() ) {
2465 /* Auth plugin doesn't allow local authentication */
2467 } elseif( $wgAuth->strictUserAuth( $this->getName() ) ) {
2468 /* Auth plugin doesn't allow local authentication for this user name */
2471 if ( self
::comparePasswords( $this->mPassword
, $password, $this->mId
) ) {
2473 } elseif ( function_exists( 'iconv' ) ) {
2474 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
2475 # Check for this with iconv
2476 $cp1252Password = iconv( 'UTF-8', 'WINDOWS-1252//TRANSLIT', $password );
2477 if ( self
::comparePasswords( $this->mPassword
, $cp1252Password, $this->mId
) ) {
2485 * Check if the given clear-text password matches the temporary password
2486 * sent by e-mail for password reset operations.
2489 function checkTemporaryPassword( $plaintext ) {
2490 return self
::comparePasswords( $this->mNewpassword
, $plaintext, $this->getId() );
2494 * Initialize (if necessary) and return a session token value
2495 * which can be used in edit forms to show that the user's
2496 * login credentials aren't being hijacked with a foreign form
2499 * @param mixed $salt - Optional function-specific data for hash.
2500 * Use a string or an array of strings.
2504 function editToken( $salt = '' ) {
2505 if ( $this->isAnon() ) {
2506 return EDIT_TOKEN_SUFFIX
;
2508 if( !isset( $_SESSION['wsEditToken'] ) ) {
2509 $token = $this->generateToken();
2510 $_SESSION['wsEditToken'] = $token;
2512 $token = $_SESSION['wsEditToken'];
2514 if( is_array( $salt ) ) {
2515 $salt = implode( '|', $salt );
2517 return md5( $token . $salt ) . EDIT_TOKEN_SUFFIX
;
2522 * Generate a hex-y looking random token for various uses.
2523 * Could be made more cryptographically sure if someone cares.
2526 function generateToken( $salt = '' ) {
2527 $token = dechex( mt_rand() ) . dechex( mt_rand() );
2528 return md5( $token . $salt );
2532 * Check given value against the token value stored in the session.
2533 * A match should confirm that the form was submitted from the
2534 * user's own login session, not a form submission from a third-party
2537 * @param string $val - the input value to compare
2538 * @param string $salt - Optional function-specific data for hash
2542 function matchEditToken( $val, $salt = '' ) {
2543 $sessionToken = $this->editToken( $salt );
2544 if ( $val != $sessionToken ) {
2545 wfDebug( "User::matchEditToken: broken session data\n" );
2547 return $val == $sessionToken;
2551 * Check whether the edit token is fine except for the suffix
2553 function matchEditTokenNoSuffix( $val, $salt = '' ) {
2554 $sessionToken = $this->editToken( $salt );
2555 return substr( $sessionToken, 0, 32 ) == substr( $val, 0, 32 );
2559 * Generate a new e-mail confirmation token and send a confirmation/invalidation
2560 * mail to the user's given address.
2562 * Calls saveSettings() internally; as it has side effects, not committing changes
2563 * would be pretty silly.
2565 * @return mixed True on success, a WikiError object on failure.
2567 function sendConfirmationMail() {
2569 $expiration = null; // gets passed-by-ref and defined in next line.
2570 $token = $this->confirmationToken( $expiration );
2571 $url = $this->confirmationTokenUrl( $token );
2572 $invalidateURL = $this->invalidationTokenUrl( $token );
2573 $this->saveSettings();
2575 return $this->sendMail( wfMsg( 'confirmemail_subject' ),
2576 wfMsg( 'confirmemail_body',
2580 $wgLang->timeanddate( $expiration, false ),
2585 * Send an e-mail to this user's account. Does not check for
2586 * confirmed status or validity.
2588 * @param string $subject
2589 * @param string $body
2590 * @param string $from Optional from address; default $wgPasswordSender will be used otherwise.
2591 * @return mixed True on success, a WikiError object on failure.
2593 function sendMail( $subject, $body, $from = null, $replyto = null ) {
2594 if( is_null( $from ) ) {
2595 global $wgPasswordSender;
2596 $from = $wgPasswordSender;
2599 $to = new MailAddress( $this );
2600 $sender = new MailAddress( $from );
2601 return UserMailer
::send( $to, $sender, $subject, $body, $replyto );
2605 * Generate, store, and return a new e-mail confirmation code.
2606 * A hash (unsalted since it's used as a key) is stored.
2608 * Call saveSettings() after calling this function to commit
2609 * this change to the database.
2611 * @param &$expiration mixed output: accepts the expiration time
2615 function confirmationToken( &$expiration ) {
2617 $expires = $now +
7 * 24 * 60 * 60;
2618 $expiration = wfTimestamp( TS_MW
, $expires );
2619 $token = $this->generateToken( $this->mId
. $this->mEmail
. $expires );
2620 $hash = md5( $token );
2622 $this->mEmailToken
= $hash;
2623 $this->mEmailTokenExpires
= $expiration;
2628 * Return a URL the user can use to confirm their email address.
2629 * @param $token: accepts the email confirmation token
2633 function confirmationTokenUrl( $token ) {
2634 return $this->getTokenUrl( 'ConfirmEmail', $token );
2637 * Return a URL the user can use to invalidate their email address.
2638 * @param $token: accepts the email confirmation token
2642 function invalidationTokenUrl( $token ) {
2643 return $this->getTokenUrl( 'Invalidateemail', $token );
2647 * Internal function to format the e-mail validation/invalidation URLs.
2648 * This uses $wgArticlePath directly as a quickie hack to use the
2649 * hardcoded English names of the Special: pages, for ASCII safety.
2651 * Since these URLs get dropped directly into emails, using the
2652 * short English names avoids insanely long URL-encoded links, which
2653 * also sometimes can get corrupted in some browsers/mailers
2654 * (bug 6957 with Gmail and Internet Explorer).
2656 protected function getTokenUrl( $page, $token ) {
2657 global $wgArticlePath;
2661 "Special:$page/$token",
2666 * Mark the e-mail address confirmed.
2668 * Call saveSettings() after calling this function to commit the change.
2670 function confirmEmail() {
2671 $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
2676 * Invalidate the user's email confirmation, unauthenticate the email
2677 * if it was already confirmed.
2679 * Call saveSettings() after calling this function to commit the change.
2681 function invalidateEmail() {
2683 $this->mEmailToken
= null;
2684 $this->mEmailTokenExpires
= null;
2685 $this->setEmailAuthenticationTimestamp( null );
2689 function setEmailAuthenticationTimestamp( $timestamp ) {
2691 $this->mEmailAuthenticated
= $timestamp;
2692 wfRunHooks( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated
) );
2696 * Is this user allowed to send e-mails within limits of current
2697 * site configuration?
2700 function canSendEmail() {
2701 $canSend = $this->isEmailConfirmed();
2702 wfRunHooks( 'UserCanSendEmail', array( &$this, &$canSend ) );
2707 * Is this user allowed to receive e-mails within limits of current
2708 * site configuration?
2711 function canReceiveEmail() {
2712 return $this->isEmailConfirmed() && !$this->getOption( 'disablemail' );
2716 * Is this user's e-mail address valid-looking and confirmed within
2717 * limits of the current site configuration?
2719 * If $wgEmailAuthentication is on, this may require the user to have
2720 * confirmed their address by returning a code or using a password
2721 * sent to the address from the wiki.
2725 function isEmailConfirmed() {
2726 global $wgEmailAuthentication;
2729 if( wfRunHooks( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
2730 if( $this->isAnon() )
2732 if( !self
::isValidEmailAddr( $this->mEmail
) )
2734 if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
2743 * Return true if there is an outstanding request for e-mail confirmation.
2746 function isEmailConfirmationPending() {
2747 global $wgEmailAuthentication;
2748 return $wgEmailAuthentication &&
2749 !$this->isEmailConfirmed() &&
2750 $this->mEmailToken
&&
2751 $this->mEmailTokenExpires
> wfTimestamp();
2755 * Get the timestamp of account creation, or false for
2756 * non-existent/anonymous user accounts
2760 public function getRegistration() {
2761 return $this->mId
> 0
2762 ?
$this->mRegistration
2767 * @param array $groups list of groups
2768 * @return array list of permission key names for given groups combined
2770 static function getGroupPermissions( $groups ) {
2771 global $wgGroupPermissions;
2773 foreach( $groups as $group ) {
2774 if( isset( $wgGroupPermissions[$group] ) ) {
2775 $rights = array_merge( $rights,
2776 array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
2783 * @param string $group key name
2784 * @return string localized descriptive name for group, if provided
2786 static function getGroupName( $group ) {
2787 global $wgMessageCache;
2788 $wgMessageCache->loadAllMessages();
2789 $key = "group-$group";
2790 $name = wfMsg( $key );
2791 return $name == '' ||
wfEmptyMsg( $key, $name )
2797 * @param string $group key name
2798 * @return string localized descriptive name for member of a group, if provided
2800 static function getGroupMember( $group ) {
2801 global $wgMessageCache;
2802 $wgMessageCache->loadAllMessages();
2803 $key = "group-$group-member";
2804 $name = wfMsg( $key );
2805 return $name == '' ||
wfEmptyMsg( $key, $name )
2811 * Return the set of defined explicit groups.
2812 * The implicit groups (by default *, 'user' and 'autoconfirmed')
2813 * are not included, as they are defined automatically,
2814 * not in the database.
2817 static function getAllGroups() {
2818 global $wgGroupPermissions;
2820 array_keys( $wgGroupPermissions ),
2821 self
::getImplicitGroups()
2826 * Get a list of all available permissions
2828 static function getAllRights() {
2829 if ( self
::$mAllRights === false ) {
2830 global $wgAvailableRights;
2831 if ( count( $wgAvailableRights ) ) {
2832 self
::$mAllRights = array_unique( array_merge( self
::$mCoreRights, $wgAvailableRights ) );
2834 self
::$mAllRights = self
::$mCoreRights;
2836 wfRunHooks( 'UserGetAllRights', array( &self
::$mAllRights ) );
2838 return self
::$mAllRights;
2842 * Get a list of implicit groups
2846 public static function getImplicitGroups() {
2847 global $wgImplicitGroups;
2848 $groups = $wgImplicitGroups;
2849 wfRunHooks( 'UserGetImplicitGroups', array( &$groups ) ); #deprecated, use $wgImplictGroups instead
2854 * Get the title of a page describing a particular group
2856 * @param $group Name of the group
2859 static function getGroupPage( $group ) {
2860 global $wgMessageCache;
2861 $wgMessageCache->loadAllMessages();
2862 $page = wfMsgForContent( 'grouppage-' . $group );
2863 if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
2864 $title = Title
::newFromText( $page );
2865 if( is_object( $title ) )
2872 * Create a link to the group in HTML, if available
2874 * @param $group Name of the group
2875 * @param $text The text of the link
2878 static function makeGroupLinkHTML( $group, $text = '' ) {
2880 $text = self
::getGroupName( $group );
2882 $title = self
::getGroupPage( $group );
2885 $sk = $wgUser->getSkin();
2886 return $sk->makeLinkObj( $title, htmlspecialchars( $text ) );
2893 * Create a link to the group in Wikitext, if available
2895 * @param $group Name of the group
2896 * @param $text The text of the link (by default, the name of the group)
2899 static function makeGroupLinkWiki( $group, $text = '' ) {
2901 $text = self
::getGroupName( $group );
2903 $title = self
::getGroupPage( $group );
2905 $page = $title->getPrefixedText();
2906 return "[[$page|$text]]";
2913 * Increment the user's edit-count field.
2914 * Will have no effect for anonymous users.
2916 function incEditCount() {
2917 if( !$this->isAnon() ) {
2918 $dbw = wfGetDB( DB_MASTER
);
2919 $dbw->update( 'user',
2920 array( 'user_editcount=user_editcount+1' ),
2921 array( 'user_id' => $this->getId() ),
2924 // Lazy initialization check...
2925 if( $dbw->affectedRows() == 0 ) {
2926 // Pull from a slave to be less cruel to servers
2927 // Accuracy isn't the point anyway here
2928 $dbr = wfGetDB( DB_SLAVE
);
2929 $count = $dbr->selectField( 'revision',
2931 array( 'rev_user' => $this->getId() ),
2934 // Now here's a goddamn hack...
2935 if( $dbr !== $dbw ) {
2936 // If we actually have a slave server, the count is
2937 // at least one behind because the current transaction
2938 // has not been committed and replicated.
2941 // But if DB_SLAVE is selecting the master, then the
2942 // count we just read includes the revision that was
2943 // just added in the working transaction.
2946 $dbw->update( 'user',
2947 array( 'user_editcount' => $count ),
2948 array( 'user_id' => $this->getId() ),
2952 // edit count in user cache too
2953 $this->invalidateCache();
2956 static function getRightDescription( $right ) {
2957 global $wgMessageCache;
2958 $wgMessageCache->loadAllMessages();
2959 $key = "right-$right";
2960 $name = wfMsg( $key );
2961 return $name == '' ||
wfEmptyMsg( $key, $name )
2967 * Make an old-style password hash
2969 * @param string $password Plain-text password
2970 * @param string $userId User ID
2972 static function oldCrypt( $password, $userId ) {
2973 global $wgPasswordSalt;
2974 if ( $wgPasswordSalt ) {
2975 return md5( $userId . '-' . md5( $password ) );
2977 return md5( $password );
2982 * Make a new-style password hash
2984 * @param string $password Plain-text password
2985 * @param string $salt Salt, may be random or the user ID. False to generate a salt.
2987 static function crypt( $password, $salt = false ) {
2988 global $wgPasswordSalt;
2990 if($wgPasswordSalt) {
2991 if ( $salt === false ) {
2992 $salt = substr( wfGenerateToken(), 0, 8 );
2994 return ':B:' . $salt . ':' . md5( $salt . '-' . md5( $password ) );
2996 return ':A:' . md5( $password);
3001 * Compare a password hash with a plain-text password. Requires the user
3002 * ID if there's a chance that the hash is an old-style hash.
3004 * @param string $hash Password hash
3005 * @param string $password Plain-text password to compare
3006 * @param string $userId User ID for old-style password salt
3008 static function comparePasswords( $hash, $password, $userId = false ) {
3010 $type = substr( $hash, 0, 3 );
3011 if ( $type == ':A:' ) {
3013 return md5( $password ) === substr( $hash, 3 );
3014 } elseif ( $type == ':B:' ) {
3016 list( $salt, $realHash ) = explode( ':', substr( $hash, 3 ), 2 );
3017 return md5( $salt.'-'.md5( $password ) ) == $realHash;
3020 return self
::oldCrypt( $password, $userId ) === $hash;