8 # Number of characters in user_token field
9 define( 'USER_TOKEN_LENGTH', 32 );
11 # Serialized record version
12 define( 'MW_USER_VERSION', 3 );
20 * When adding a new private variable, dont forget to add it to __sleep()
26 var $mBlockreason; //!<
28 var $mDataLoaded; //!<
30 var $mEmailAuthenticated; //!<
35 var $mNewpassword; //!<
40 var $mRegistration; //!<
45 var $mDatePreference; // !<
46 var $mVersion; //!< serialized version
50 * Default user options
51 * To change this array at runtime, use a UserDefaultOptions hook
53 static public $mDefaultOptions = array(
66 'highlightbroken' => 1,
70 'editsectiononrightclick'=> 0,
76 'rememberpassword' => 0,
77 'enotifwatchlistpages' => 0,
78 'enotifusertalkpages' => 1,
79 'enotifminoredits' => 0,
80 'enotifrevealaddr' => 0,
81 'shownumberswatching' => 1,
83 'externaleditor' => 0,
86 'numberheadings' => 0,
87 'uselivepreview' => 0,
88 'watchlistdays' => 3.0,
91 static public $mToggles = array(
101 'editsectiononrightclick',
111 'enotifwatchlistpages',
112 'enotifusertalkpages',
115 'shownumberswatching',
127 /** Constructor using User:loadDefaults() */
129 $this->loadDefaults();
130 $this->mVersion
= MW_USER_VERSION
;
134 * Static factory method
135 * @param string $name Username, validated by Title:newFromText()
136 * @param bool $validate Validate username
140 function newFromName( $name, $validate = true ) {
141 # Force usernames to capital
143 $name = $wgContLang->ucfirst( $name );
145 # Clean up name according to title rules
146 $t = Title
::newFromText( $name );
147 if( is_null( $t ) ) {
151 # Reject various classes of invalid names
152 $canonicalName = $t->getText();
154 $canonicalName = $wgAuth->getCanonicalName( $t->getText() );
156 if( $validate && !User
::isValidUserName( $canonicalName ) ) {
161 $u->setName( $canonicalName );
162 $u->setId( $u->idFromName( $canonicalName ) );
167 * Factory method to fetch whichever use has a given email confirmation code.
168 * This code is generated when an account is created or its e-mail address
171 * If the code is invalid or has expired, returns NULL.
173 * @param string $code
177 function newFromConfirmationCode( $code ) {
178 $dbr =& wfGetDB( DB_SLAVE
);
179 $name = $dbr->selectField( 'user', 'user_name', array(
180 'user_email_token' => md5( $code ),
181 'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
183 if( is_string( $name ) ) {
184 return User
::newFromName( $name );
191 * Serialze sleep function, for better cache efficiency and avoidance of
192 * silly "incomplete type" errors when skins are cached. The array should
193 * contain names of private variables (see at top of User.php).
199 'mEmailAuthenticated',
218 * Get username given an id.
219 * @param integer $id Database user id
220 * @return string Nickname of a user
223 function whoIs( $id ) {
224 $dbr =& wfGetDB( DB_SLAVE
);
225 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
229 * Get real username given an id.
230 * @param integer $id Database user id
231 * @return string Realname of a user
234 function whoIsReal( $id ) {
235 $dbr =& wfGetDB( DB_SLAVE
);
236 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), 'User::whoIsReal' );
240 * Get database id given a user name
241 * @param string $name Nickname of a user
242 * @return integer|null Database user id (null: if non existent
245 function idFromName( $name ) {
246 $fname = "User::idFromName";
248 $nt = Title
::newFromText( $name );
249 if( is_null( $nt ) ) {
253 $dbr =& wfGetDB( DB_SLAVE
);
254 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), $fname );
256 if ( $s === false ) {
264 * Does the string match an anonymous IPv4 address?
266 * This function exists for username validation, in order to reject
267 * usernames which are similar in form to IP addresses. Strings such
268 * as 300.300.300.300 will return true because it looks like an IP
269 * address, despite not being strictly valid.
271 * We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
272 * address because the usemod software would "cloak" anonymous IP
273 * addresses like this, if we allowed accounts like this to be created
274 * new users could get the old edits of these anonymous users.
279 * @param string $name Nickname of a user
282 function isIP( $name ) {
283 return preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/",$name);
284 /*return preg_match("/^
285 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
286 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
287 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
288 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))
293 * Is the input a valid username?
295 * Checks if the input is a valid username, we don't want an empty string,
296 * an IP address, anything that containins slashes (would mess up subpages),
297 * is longer than the maximum allowed username size or doesn't begin with
300 * @param string $name
304 function isValidUserName( $name ) {
305 global $wgContLang, $wgMaxNameChars;
308 || User
::isIP( $name )
309 ||
strpos( $name, '/' ) !== false
310 ||
strlen( $name ) > $wgMaxNameChars
311 ||
$name != $wgContLang->ucfirst( $name ) )
314 // Ensure that the name can't be misresolved as a different title,
315 // such as with extra namespace keys at the start.
316 $parsed = Title
::newFromText( $name );
317 if( is_null( $parsed )
318 ||
$parsed->getNamespace()
319 ||
strcmp( $name, $parsed->getPrefixedText() ) )
322 // Check an additional blacklist of troublemaker characters.
323 // Should these be merged into the title char list?
324 $unicodeBlacklist = '/[' .
325 '\x{0080}-\x{009f}' . # iso-8859-1 control chars
326 '\x{00a0}' . # non-breaking space
327 '\x{2000}-\x{200f}' . # various whitespace
328 '\x{2028}-\x{202f}' . # breaks and control chars
329 '\x{3000}' . # ideographic space
330 '\x{e000}-\x{f8ff}' . # private use
332 if( preg_match( $unicodeBlacklist, $name ) ) {
340 * Is the input a valid password?
342 * @param string $password
346 function isValidPassword( $password ) {
347 global $wgMinimalPasswordLength;
348 return strlen( $password ) >= $wgMinimalPasswordLength;
352 * Does the string match roughly an email address ?
354 * There used to be a regular expression here, it got removed because it
355 * rejected valid addresses. Actually just check if there is '@' somewhere
356 * in the given address.
358 * @todo Check for RFC 2822 compilance
361 * @param string $addr email address
365 function isValidEmailAddr ( $addr ) {
366 return ( trim( $addr ) != '' ) &&
367 (false !== strpos( $addr, '@' ) );
371 * Count the number of edits of a user
373 * @param int $uid The user ID to check
376 function edits( $uid ) {
377 $fname = 'User::edits';
379 $dbr =& wfGetDB( DB_SLAVE
);
380 return $dbr->selectField(
381 'revision', 'count(*)',
382 array( 'rev_user' => $uid ),
388 * probably return a random password
389 * @return string probably a random password
391 * @todo Check what is doing really [AV]
393 function randomPassword() {
394 global $wgMinimalPasswordLength;
395 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
396 $l = strlen( $pwchars ) - 1;
398 $pwlength = max( 7, $wgMinimalPasswordLength );
399 $digit = mt_rand(0, $pwlength - 1);
401 for ( $i = 0; $i < $pwlength; $i++
) {
402 $np .= $i == $digit ?
chr( mt_rand(48, 57) ) : $pwchars{ mt_rand(0, $l)};
408 * Set properties to default
409 * Used at construction. It will load per language default settings only
410 * if we have an available language object.
412 function loadDefaults() {
415 $fname = 'User::loadDefaults' . $n;
416 wfProfileIn( $fname );
418 global $wgCookiePrefix;
419 global $wgNamespacesToBeSearchedDefault;
422 $this->mNewtalk
= -1;
423 $this->mName
= false;
424 $this->mRealName
= $this->mEmail
= '';
425 $this->mEmailAuthenticated
= null;
426 $this->mPassword
= $this->mNewpassword
= '';
427 $this->mRights
= array();
428 $this->mGroups
= array();
429 $this->mOptions
= null;
430 $this->mDatePreference
= null;
432 unset( $this->mSkin
);
433 $this->mDataLoaded
= false;
434 $this->mBlockedby
= -1; # Unset
435 $this->setToken(); # Random
436 $this->mHash
= false;
438 if ( isset( $_COOKIE[$wgCookiePrefix.'LoggedOut'] ) ) {
439 $this->mTouched
= wfTimestamp( TS_MW
, $_COOKIE[$wgCookiePrefix.'LoggedOut'] );
442 $this->mTouched
= '0'; # Allow any pages to be cached
445 $this->mRegistration
= wfTimestamp( TS_MW
);
447 wfProfileOut( $fname );
451 * Combine the language default options with any site-specific options
452 * and add the default language variants.
458 function getDefaultOptions() {
459 global $wgNamespacesToBeSearchedDefault;
461 * Site defaults will override the global/language defaults
464 $defOpt = self
::$mDefaultOptions +
$wgContLang->getDefaultUserOptionOverrides();
467 * default language setting
469 $variant = $wgContLang->getPreferredVariant( false );
470 $defOpt['variant'] = $variant;
471 $defOpt['language'] = $variant;
473 foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
474 $defOpt['searchNs'.$nsnum] = $val;
480 * Get a given default option value.
487 function getDefaultOption( $opt ) {
488 $defOpts = User
::getDefaultOptions();
489 if( isset( $defOpts[$opt] ) ) {
490 return $defOpts[$opt];
497 * Get a list of user toggle names
500 static function getToggles() {
502 $extraToggles = array();
503 wfRunHooks( 'UserToggles', array( &$extraToggles ) );
504 return array_merge( self
::$mToggles, $extraToggles, $wgContLang->getExtraUserToggles() );
509 * Get blocking information
511 * @param bool $bFromSlave Specify whether to check slave or master. To improve performance,
512 * non-critical checks are done against slaves. Check when actually saving should be done against
515 function getBlockedStatus( $bFromSlave = true ) {
516 global $wgEnableSorbs, $wgProxyWhitelist;
518 if ( -1 != $this->mBlockedby
) {
519 wfDebug( "User::getBlockedStatus: already loaded.\n" );
523 $fname = 'User::getBlockedStatus';
524 wfProfileIn( $fname );
525 wfDebug( "$fname: checking...\n" );
527 $this->mBlockedby
= 0;
531 $this->mBlock
= new Block();
532 $this->mBlock
->fromMaster( !$bFromSlave );
533 if ( $this->mBlock
->load( $ip , $this->mId
) ) {
534 wfDebug( "$fname: Found block.\n" );
535 $this->mBlockedby
= $this->mBlock
->mBy
;
536 $this->mBlockreason
= $this->mBlock
->mReason
;
537 if ( $this->isLoggedIn() ) {
538 $this->spreadBlock();
541 $this->mBlock
= null;
542 wfDebug( "$fname: No block.\n" );
546 if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
549 if ( wfIsLocallyBlockedProxy( $ip ) ) {
550 $this->mBlockedby
= wfMsg( 'proxyblocker' );
551 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
555 if ( !$this->mBlockedby
&& $wgEnableSorbs && !$this->getID() ) {
556 if ( $this->inSorbsBlacklist( $ip ) ) {
557 $this->mBlockedby
= wfMsg( 'sorbs' );
558 $this->mBlockreason
= wfMsg( 'sorbsreason' );
564 wfRunHooks( 'GetBlockedStatus', array( &$this ) );
566 wfProfileOut( $fname );
569 function inSorbsBlacklist( $ip ) {
570 global $wgEnableSorbs;
571 return $wgEnableSorbs &&
572 $this->inDnsBlacklist( $ip, 'http.dnsbl.sorbs.net.' );
575 function inDnsBlacklist( $ip, $base ) {
576 $fname = 'User::inDnsBlacklist';
577 wfProfileIn( $fname );
582 if ( preg_match( '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $m ) ) {
584 for ( $i=4; $i>=1; $i-- ) {
585 $host .= $m[$i] . '.';
590 $ipList = gethostbynamel( $host );
593 wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
596 wfDebug( "Requested $host, not found in $base.\n" );
600 wfProfileOut( $fname );
605 * Primitive rate limits: enforce maximum actions per time period
606 * to put a brake on flooding.
608 * Note: when using a shared cache like memcached, IP-address
609 * last-hit counters will be shared across wikis.
611 * @return bool true if a rate limiter was tripped
614 function pingLimiter( $action='edit' ) {
615 global $wgRateLimits, $wgRateLimitsExcludedGroups;
616 if( !isset( $wgRateLimits[$action] ) ) {
620 # Some groups shouldn't trigger the ping limiter, ever
621 foreach( $this->getGroups() as $group ) {
622 if( array_search( $group, $wgRateLimitsExcludedGroups ) !== false )
626 global $wgMemc, $wgDBname, $wgRateLimitLog;
627 $fname = 'User::pingLimiter';
628 wfProfileIn( $fname );
630 $limits = $wgRateLimits[$action];
632 $id = $this->getId();
635 if( isset( $limits['anon'] ) && $id == 0 ) {
636 $keys["$wgDBname:limiter:$action:anon"] = $limits['anon'];
639 if( isset( $limits['user'] ) && $id != 0 ) {
640 $keys["$wgDBname:limiter:$action:user:$id"] = $limits['user'];
642 if( $this->isNewbie() ) {
643 if( isset( $limits['newbie'] ) && $id != 0 ) {
644 $keys["$wgDBname:limiter:$action:user:$id"] = $limits['newbie'];
646 if( isset( $limits['ip'] ) ) {
647 $keys["mediawiki:limiter:$action:ip:$ip"] = $limits['ip'];
649 if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $ip, $matches ) ) {
650 $subnet = $matches[1];
651 $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
656 foreach( $keys as $key => $limit ) {
657 list( $max, $period ) = $limit;
658 $summary = "(limit $max in {$period}s)";
659 $count = $wgMemc->get( $key );
661 if( $count > $max ) {
662 wfDebug( "$fname: tripped! $key at $count $summary\n" );
663 if( $wgRateLimitLog ) {
664 @error_log
( wfTimestamp( TS_MW
) . ' ' . $wgDBname . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
668 wfDebug( "$fname: ok. $key at $count $summary\n" );
671 wfDebug( "$fname: adding record for $key $summary\n" );
672 $wgMemc->add( $key, 1, intval( $period ) );
674 $wgMemc->incr( $key );
677 wfProfileOut( $fname );
682 * Check if user is blocked
683 * @return bool True if blocked, false otherwise
685 function isBlocked( $bFromSlave = true ) { // hacked from false due to horrible probs on site
686 wfDebug( "User::isBlocked: enter\n" );
687 $this->getBlockedStatus( $bFromSlave );
688 return $this->mBlockedby
!== 0;
692 * Check if user is blocked from editing a particular article
694 function isBlockedFrom( $title, $bFromSlave = false ) {
695 global $wgBlockAllowsUTEdit;
696 $fname = 'User::isBlockedFrom';
697 wfProfileIn( $fname );
698 wfDebug( "$fname: enter\n" );
700 if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
701 $title->getNamespace() == NS_USER_TALK
)
704 wfDebug( "$fname: self-talk page, ignoring any blocks\n" );
706 wfDebug( "$fname: asking isBlocked()\n" );
707 $blocked = $this->isBlocked( $bFromSlave );
709 wfProfileOut( $fname );
714 * Get name of blocker
715 * @return string name of blocker
717 function blockedBy() {
718 $this->getBlockedStatus();
719 return $this->mBlockedby
;
723 * Get blocking reason
724 * @return string Blocking reason
726 function blockedFor() {
727 $this->getBlockedStatus();
728 return $this->mBlockreason
;
732 * Initialise php session
733 * @deprecated use wfSetupSession()
735 function SetupSession() {
740 * Create a new user object using data from session
743 function loadFromSession() {
744 global $wgMemc, $wgDBname, $wgCookiePrefix;
746 if ( isset( $_SESSION['wsUserID'] ) ) {
747 if ( 0 != $_SESSION['wsUserID'] ) {
748 $sId = $_SESSION['wsUserID'];
752 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserID"] ) ) {
753 $sId = intval( $_COOKIE["{$wgCookiePrefix}UserID"] );
754 $_SESSION['wsUserID'] = $sId;
758 if ( isset( $_SESSION['wsUserName'] ) ) {
759 $sName = $_SESSION['wsUserName'];
760 } else if ( isset( $_COOKIE["{$wgCookiePrefix}UserName"] ) ) {
761 $sName = $_COOKIE["{$wgCookiePrefix}UserName"];
762 $_SESSION['wsUserName'] = $sName;
767 $passwordCorrect = FALSE;
768 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
769 if( !is_object( $user ) ||
$user->mVersion
< MW_USER_VERSION
) {
770 # Expire old serialized objects; they may be corrupt.
773 if($makenew = !$user) {
774 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
777 $user->loadFromDatabase();
779 wfDebug( "User::loadFromSession() got from cache!\n" );
780 # Set block status to unloaded, that should be loaded every time
781 $user->mBlockedby
= -1;
784 if ( isset( $_SESSION['wsToken'] ) ) {
785 $passwordCorrect = $_SESSION['wsToken'] == $user->mToken
;
786 } else if ( isset( $_COOKIE["{$wgCookiePrefix}Token"] ) ) {
787 $passwordCorrect = $user->mToken
== $_COOKIE["{$wgCookiePrefix}Token"];
789 return new User(); # Can't log in from session
792 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
794 if($wgMemc->set( $key, $user ))
795 wfDebug( "User::loadFromSession() successfully saved user\n" );
797 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
801 return new User(); # Can't log in from session
805 * Load a user from the database
807 function loadFromDatabase() {
808 $fname = "User::loadFromDatabase";
810 # Counter-intuitive, breaks various things, use User::setLoaded() if you want to suppress
811 # loading in a command line script, don't assume all command line scripts need it like this
812 #if ( $this->mDataLoaded || $wgCommandLineMode ) {
813 if ( $this->mDataLoaded
) {
818 $this->mId
= intval( $this->mId
);
820 /** Anonymous user */
823 $this->mRights
= $this->getGroupPermissions( array( '*' ) );
824 $this->mDataLoaded
= true;
826 } # the following stuff is for non-anonymous users only
828 $dbr =& wfGetDB( DB_SLAVE
);
829 $s = $dbr->selectRow( 'user', array( 'user_name','user_password','user_newpassword','user_email',
830 'user_email_authenticated',
831 'user_real_name','user_options','user_touched', 'user_token', 'user_registration' ),
832 array( 'user_id' => $this->mId
), $fname );
834 if ( $s !== false ) {
835 $this->mName
= $s->user_name
;
836 $this->mEmail
= $s->user_email
;
837 $this->mEmailAuthenticated
= wfTimestampOrNull( TS_MW
, $s->user_email_authenticated
);
838 $this->mRealName
= $s->user_real_name
;
839 $this->mPassword
= $s->user_password
;
840 $this->mNewpassword
= $s->user_newpassword
;
841 $this->decodeOptions( $s->user_options
);
842 $this->mTouched
= wfTimestamp(TS_MW
,$s->user_touched
);
843 $this->mToken
= $s->user_token
;
844 $this->mRegistration
= wfTimestampOrNull( TS_MW
, $s->user_registration
);
846 $res = $dbr->select( 'user_groups',
848 array( 'ug_user' => $this->mId
),
850 $this->mGroups
= array();
851 while( $row = $dbr->fetchObject( $res ) ) {
852 $this->mGroups
[] = $row->ug_group
;
854 $implicitGroups = array( '*', 'user' );
856 global $wgAutoConfirmAge;
857 $accountAge = time() - wfTimestampOrNull( TS_UNIX
, $this->mRegistration
);
858 if( $accountAge >= $wgAutoConfirmAge ) {
859 $implicitGroups[] = 'autoconfirmed';
862 # Implicit group for users whose email addresses are confirmed
863 global $wgEmailAuthentication;
864 if( $this->isValidEmailAddr( $this->mEmail
) ) {
865 if( $wgEmailAuthentication ) {
866 if( $this->mEmailAuthenticated
)
867 $implicitGroups[] = 'emailconfirmed';
869 $implicitGroups[] = 'emailconfirmed';
873 $effectiveGroups = array_merge( $implicitGroups, $this->mGroups
);
874 $this->mRights
= $this->getGroupPermissions( $effectiveGroups );
877 $this->mDataLoaded
= true;
880 function getID() { return $this->mId
; }
881 function setID( $v ) {
883 $this->mDataLoaded
= false;
887 $this->loadFromDatabase();
888 if ( $this->mName
=== false ) {
889 $this->mName
= wfGetIP();
894 function setName( $str ) {
895 $this->loadFromDatabase();
901 * Return the title dbkey form of the name, for eg user pages.
905 function getTitleKey() {
906 return str_replace( ' ', '_', $this->getName() );
909 function getNewtalk() {
910 $this->loadFromDatabase();
912 # Load the newtalk status if it is unloaded (mNewtalk=-1)
913 if( $this->mNewtalk
=== -1 ) {
914 $this->mNewtalk
= false; # reset talk page status
916 # Check memcached separately for anons, who have no
917 # entire User object stored in there.
919 global $wgDBname, $wgMemc;
920 $key = "$wgDBname:newtalk:ip:" . $this->getName();
921 $newtalk = $wgMemc->get( $key );
922 if( is_integer( $newtalk ) ) {
923 $this->mNewtalk
= (bool)$newtalk;
925 $this->mNewtalk
= $this->checkNewtalk( 'user_ip', $this->getName() );
926 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
929 $this->mNewtalk
= $this->checkNewtalk( 'user_id', $this->mId
);
933 return (bool)$this->mNewtalk
;
937 * Return the talk page(s) this user has new messages on.
939 function getNewMessageLinks() {
942 if (!wfRunHooks('UserRetrieveNewTalks', array(&$this, &$talks)))
945 if (!$this->getNewtalk())
947 $up = $this->getUserPage();
948 $utp = $up->getTalkPage();
949 return array(array("wiki" => $wgDBname, "link" => $utp->getLocalURL()));
954 * Perform a user_newtalk check on current slaves; if the memcached data
955 * is funky we don't want newtalk state to get stuck on save, as that's
958 * @param string $field
963 function checkNewtalk( $field, $id ) {
964 $fname = 'User::checkNewtalk';
965 $dbr =& wfGetDB( DB_SLAVE
);
966 $ok = $dbr->selectField( 'user_newtalk', $field,
967 array( $field => $id ), $fname );
968 return $ok !== false;
973 * @param string $field
977 function updateNewtalk( $field, $id ) {
978 $fname = 'User::updateNewtalk';
979 if( $this->checkNewtalk( $field, $id ) ) {
980 wfDebug( "$fname already set ($field, $id), ignoring\n" );
983 $dbw =& wfGetDB( DB_MASTER
);
984 $dbw->insert( 'user_newtalk',
985 array( $field => $id ),
988 wfDebug( "$fname: set on ($field, $id)\n" );
993 * Clear the new messages flag for the given user
994 * @param string $field
998 function deleteNewtalk( $field, $id ) {
999 $fname = 'User::deleteNewtalk';
1000 if( !$this->checkNewtalk( $field, $id ) ) {
1001 wfDebug( "$fname: already gone ($field, $id), ignoring\n" );
1004 $dbw =& wfGetDB( DB_MASTER
);
1005 $dbw->delete( 'user_newtalk',
1006 array( $field => $id ),
1008 wfDebug( "$fname: killed on ($field, $id)\n" );
1013 * Update the 'You have new messages!' status.
1016 function setNewtalk( $val ) {
1017 if( wfReadOnly() ) {
1021 $this->loadFromDatabase();
1022 $this->mNewtalk
= $val;
1024 $fname = 'User::setNewtalk';
1026 if( $this->isAnon() ) {
1028 $id = $this->getName();
1031 $id = $this->getId();
1035 $changed = $this->updateNewtalk( $field, $id );
1037 $changed = $this->deleteNewtalk( $field, $id );
1041 if( $this->isAnon() ) {
1042 // Anons have a separate memcached space, since
1043 // user records aren't kept for them.
1044 global $wgDBname, $wgMemc;
1045 $key = "$wgDBname:newtalk:ip:$val";
1046 $wgMemc->set( $key, $val ?
1 : 0 );
1049 // Make sure the user page is watched, so a notification
1050 // will be sent out if enabled.
1051 $this->addWatch( $this->getTalkPage() );
1054 $this->invalidateCache();
1055 $this->saveSettings();
1059 function invalidateCache() {
1060 global $wgClockSkewFudge;
1061 $this->loadFromDatabase();
1062 $this->mTouched
= wfTimestamp(TS_MW
, time() +
$wgClockSkewFudge );
1063 # Don't forget to save the options after this or
1064 # it won't take effect!
1067 function validateCache( $timestamp ) {
1068 $this->loadFromDatabase();
1069 return ($timestamp >= $this->mTouched
);
1073 * Encrypt a password.
1074 * It can eventuall salt a password @see User::addSalt()
1075 * @param string $p clear Password.
1076 * @return string Encrypted password.
1078 function encryptPassword( $p ) {
1079 return wfEncryptPassword( $this->mId
, $p );
1082 # Set the password and reset the random token
1083 function setPassword( $str ) {
1084 $this->loadFromDatabase();
1086 $this->mPassword
= $this->encryptPassword( $str );
1087 $this->mNewpassword
= '';
1090 # Set the random token (used for persistent authentication)
1091 function setToken( $token = false ) {
1092 global $wgSecretKey, $wgProxyKey, $wgDBname;
1094 if ( $wgSecretKey ) {
1095 $key = $wgSecretKey;
1096 } elseif ( $wgProxyKey ) {
1101 $this->mToken
= md5( $key . mt_rand( 0, 0x7fffffff ) . $wgDBname . $this->mId
);
1103 $this->mToken
= $token;
1108 function setCookiePassword( $str ) {
1109 $this->loadFromDatabase();
1110 $this->mCookiePassword
= md5( $str );
1113 function setNewpassword( $str ) {
1114 $this->loadFromDatabase();
1115 $this->mNewpassword
= $this->encryptPassword( $str );
1118 function getEmail() {
1119 $this->loadFromDatabase();
1120 return $this->mEmail
;
1123 function getEmailAuthenticationTimestamp() {
1124 $this->loadFromDatabase();
1125 return $this->mEmailAuthenticated
;
1128 function setEmail( $str ) {
1129 $this->loadFromDatabase();
1130 $this->mEmail
= $str;
1133 function getRealName() {
1134 $this->loadFromDatabase();
1135 return $this->mRealName
;
1138 function setRealName( $str ) {
1139 $this->loadFromDatabase();
1140 $this->mRealName
= $str;
1144 * @param string $oname The option to check
1147 function getOption( $oname ) {
1148 $this->loadFromDatabase();
1149 if ( is_null( $this->mOptions
) ) {
1150 $this->mOptions
= User
::getDefaultOptions();
1152 if ( array_key_exists( $oname, $this->mOptions
) ) {
1153 return trim( $this->mOptions
[$oname] );
1160 * Get the user's date preference, including some important migration for
1163 function getDatePreference() {
1164 if ( is_null( $this->mDatePreference
) ) {
1166 $value = $this->getOption( 'date' );
1167 $map = $wgLang->getDatePreferenceMigrationMap();
1168 if ( isset( $map[$value] ) ) {
1169 $value = $map[$value];
1171 $this->mDatePreference
= $value;
1173 return $this->mDatePreference
;
1177 * @param string $oname The option to check
1178 * @return bool False if the option is not selected, true if it is
1180 function getBoolOption( $oname ) {
1181 return (bool)$this->getOption( $oname );
1185 * Get an option as an integer value from the source string.
1186 * @param string $oname The option to check
1187 * @param int $default Optional value to return if option is unset/blank.
1190 function getIntOption( $oname, $default=0 ) {
1191 $val = $this->getOption( $oname );
1195 return intval( $val );
1198 function setOption( $oname, $val ) {
1199 $this->loadFromDatabase();
1200 if ( is_null( $this->mOptions
) ) {
1201 $this->mOptions
= User
::getDefaultOptions();
1203 if ( $oname == 'skin' ) {
1204 # Clear cached skin, so the new one displays immediately in Special:Preferences
1205 unset( $this->mSkin
);
1207 // Filter out any newlines that may have passed through input validation.
1208 // Newlines are used to separate items in the options blob.
1209 $val = str_replace( "\r\n", "\n", $val );
1210 $val = str_replace( "\r", "\n", $val );
1211 $val = str_replace( "\n", " ", $val );
1212 $this->mOptions
[$oname] = $val;
1213 $this->invalidateCache();
1216 function getRights() {
1217 $this->loadFromDatabase();
1218 return $this->mRights
;
1222 * Get the list of explicit group memberships this user has.
1223 * The implicit * and user groups are not included.
1224 * @return array of strings
1226 function getGroups() {
1227 $this->loadFromDatabase();
1228 return $this->mGroups
;
1232 * Get the list of implicit group memberships this user has.
1233 * This includes all explicit groups, plus 'user' if logged in
1234 * and '*' for all accounts.
1235 * @return array of strings
1237 function getEffectiveGroups() {
1238 $base = array( '*' );
1239 if( $this->isLoggedIn() ) {
1242 return array_merge( $base, $this->getGroups() );
1246 * Add the user to the given group.
1247 * This takes immediate effect.
1250 function addGroup( $group ) {
1251 $dbw =& wfGetDB( DB_MASTER
);
1252 $dbw->insert( 'user_groups',
1254 'ug_user' => $this->getID(),
1255 'ug_group' => $group,
1258 array( 'IGNORE' ) );
1260 $this->mGroups
= array_merge( $this->mGroups
, array( $group ) );
1261 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups() );
1263 $this->invalidateCache();
1264 $this->saveSettings();
1268 * Remove the user from the given group.
1269 * This takes immediate effect.
1272 function removeGroup( $group ) {
1273 $dbw =& wfGetDB( DB_MASTER
);
1274 $dbw->delete( 'user_groups',
1276 'ug_user' => $this->getID(),
1277 'ug_group' => $group,
1279 'User::removeGroup' );
1281 $this->mGroups
= array_diff( $this->mGroups
, array( $group ) );
1282 $this->mRights
= User
::getGroupPermissions( $this->getEffectiveGroups() );
1284 $this->invalidateCache();
1285 $this->saveSettings();
1290 * A more legible check for non-anonymousness.
1291 * Returns true if the user is not an anonymous visitor.
1295 function isLoggedIn() {
1296 return( $this->getID() != 0 );
1300 * A more legible check for anonymousness.
1301 * Returns true if the user is an anonymous visitor.
1306 return !$this->isLoggedIn();
1310 * Whether the user is a bot
1314 return $this->isAllowed( 'bot' );
1318 * Check if user is allowed to access a feature / make an action
1319 * @param string $action Action to be checked
1320 * @return boolean True: action is allowed, False: action should not be allowed
1322 function isAllowed($action='') {
1323 if ( $action === '' )
1324 // In the spirit of DWIM
1327 $this->loadFromDatabase();
1328 return in_array( $action , $this->mRights
);
1332 * Load a skin if it doesn't exist or return it
1333 * @todo FIXME : need to check the old failback system [AV]
1335 function &getSkin() {
1336 global $IP, $wgRequest;
1337 if ( ! isset( $this->mSkin
) ) {
1338 $fname = 'User::getSkin';
1339 wfProfileIn( $fname );
1342 $userSkin = $this->getOption( 'skin' );
1343 $userSkin = $wgRequest->getVal('useskin', $userSkin);
1345 $this->mSkin
=& Skin
::newFromKey( $userSkin );
1346 wfProfileOut( $fname );
1348 return $this->mSkin
;
1352 * @param string $title Article title to look at
1356 * Check watched status of an article
1357 * @return bool True if article is watched
1359 function isWatched( $title ) {
1360 $wl = WatchedItem
::fromUserTitle( $this, $title );
1361 return $wl->isWatched();
1367 function addWatch( $title ) {
1368 $wl = WatchedItem
::fromUserTitle( $this, $title );
1370 $this->invalidateCache();
1374 * Stop watching an article
1376 function removeWatch( $title ) {
1377 $wl = WatchedItem
::fromUserTitle( $this, $title );
1379 $this->invalidateCache();
1383 * Clear the user's notification timestamp for the given title.
1384 * If e-notif e-mails are on, they will receive notification mails on
1385 * the next change of the page if it's watched etc.
1387 function clearNotification( &$title ) {
1388 global $wgUser, $wgUseEnotif;
1391 if ($title->getNamespace() == NS_USER_TALK
&&
1392 $title->getText() == $this->getName() ) {
1393 if (!wfRunHooks('UserClearNewTalkNotification', array(&$this)))
1395 $this->setNewtalk( false );
1398 if( !$wgUseEnotif ) {
1402 if( $this->isAnon() ) {
1403 // Nothing else to do...
1407 // Only update the timestamp if the page is being watched.
1408 // The query to find out if it is watched is cached both in memcached and per-invocation,
1409 // and when it does have to be executed, it can be on a slave
1410 // If this is the user's newtalk page, we always update the timestamp
1411 if ($title->getNamespace() == NS_USER_TALK
&&
1412 $title->getText() == $wgUser->getName())
1415 } elseif ( $this->getID() == $wgUser->getID() ) {
1416 $watched = $title->userIsWatching();
1421 // If the page is watched by the user (or may be watched), update the timestamp on any
1422 // any matching rows
1424 $dbw =& wfGetDB( DB_MASTER
);
1425 $success = $dbw->update( 'watchlist',
1427 'wl_notificationtimestamp' => NULL
1428 ), array( /* WHERE */
1429 'wl_title' => $title->getDBkey(),
1430 'wl_namespace' => $title->getNamespace(),
1431 'wl_user' => $this->getID()
1432 ), 'User::clearLastVisited'
1440 * Resets all of the given user's page-change notification timestamps.
1441 * If e-notif e-mails are on, they will receive notification mails on
1442 * the next change of any watched page.
1444 * @param int $currentUser user ID number
1447 function clearAllNotifications( $currentUser ) {
1448 global $wgUseEnotif;
1449 if ( !$wgUseEnotif ) {
1450 $this->setNewtalk( false );
1453 if( $currentUser != 0 ) {
1455 $dbw =& wfGetDB( DB_MASTER
);
1456 $success = $dbw->update( 'watchlist',
1458 'wl_notificationtimestamp' => 0
1459 ), array( /* WHERE */
1460 'wl_user' => $currentUser
1461 ), 'UserMailer::clearAll'
1464 # we also need to clear here the "you have new message" notification for the own user_talk page
1465 # This is cleared one page view later in Article::viewUpdates();
1471 * @return string Encoding options
1473 function encodeOptions() {
1474 if ( is_null( $this->mOptions
) ) {
1475 $this->mOptions
= User
::getDefaultOptions();
1478 foreach ( $this->mOptions
as $oname => $oval ) {
1479 array_push( $a, $oname.'='.$oval );
1481 $s = implode( "\n", $a );
1488 function decodeOptions( $str ) {
1491 $this->mOptions
= array();
1492 $a = explode( "\n", $str );
1493 foreach ( $a as $s ) {
1494 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
1495 $this->mOptions
[$m[1]] = $m[2];
1500 function setCookies() {
1501 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
1502 if ( 0 == $this->mId
) return;
1503 $this->loadFromDatabase();
1504 $exp = time() +
$wgCookieExpiration;
1506 $_SESSION['wsUserID'] = $this->mId
;
1507 setcookie( $wgCookiePrefix.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1509 $_SESSION['wsUserName'] = $this->getName();
1510 setcookie( $wgCookiePrefix.'UserName', $this->getName(), $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1512 $_SESSION['wsToken'] = $this->mToken
;
1513 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
1514 setcookie( $wgCookiePrefix.'Token', $this->mToken
, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1516 setcookie( $wgCookiePrefix.'Token', '', time() - 3600 );
1522 * It will clean the session cookie
1525 global $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
1526 $this->loadDefaults();
1527 $this->setLoaded( true );
1529 $_SESSION['wsUserID'] = 0;
1531 setcookie( $wgCookiePrefix.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1532 setcookie( $wgCookiePrefix.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1534 # Remember when user logged out, to prevent seeing cached pages
1535 setcookie( $wgCookiePrefix.'LoggedOut', wfTimestampNow(), time() +
86400, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
1539 * Save object settings into database
1541 function saveSettings() {
1542 global $wgMemc, $wgDBname;
1543 $fname = 'User::saveSettings';
1545 if ( wfReadOnly() ) { return; }
1546 if ( 0 == $this->mId
) { return; }
1548 $dbw =& wfGetDB( DB_MASTER
);
1549 $dbw->update( 'user',
1551 'user_name' => $this->mName
,
1552 'user_password' => $this->mPassword
,
1553 'user_newpassword' => $this->mNewpassword
,
1554 'user_real_name' => $this->mRealName
,
1555 'user_email' => $this->mEmail
,
1556 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
1557 'user_options' => $this->encodeOptions(),
1558 'user_touched' => $dbw->timestamp($this->mTouched
),
1559 'user_token' => $this->mToken
1560 ), array( /* WHERE */
1561 'user_id' => $this->mId
1564 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
1569 * Checks if a user with the given name exists, returns the ID
1571 function idForName() {
1572 $fname = 'User::idForName';
1575 $s = trim( $this->getName() );
1576 if ( 0 == strcmp( '', $s ) ) return 0;
1578 $dbr =& wfGetDB( DB_SLAVE
);
1579 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
1580 if ( $id === false ) {
1587 * Add user object to the database
1589 function addToDatabase() {
1590 $fname = 'User::addToDatabase';
1591 $dbw =& wfGetDB( DB_MASTER
);
1592 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
1593 $dbw->insert( 'user',
1595 'user_id' => $seqVal,
1596 'user_name' => $this->mName
,
1597 'user_password' => $this->mPassword
,
1598 'user_newpassword' => $this->mNewpassword
,
1599 'user_email' => $this->mEmail
,
1600 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated
),
1601 'user_real_name' => $this->mRealName
,
1602 'user_options' => $this->encodeOptions(),
1603 'user_token' => $this->mToken
,
1604 'user_registration' => $dbw->timestamp( $this->mRegistration
),
1607 $this->mId
= $dbw->insertId();
1610 function spreadBlock() {
1611 # If the (non-anonymous) user is blocked, this function will block any IP address
1612 # that they successfully log on from.
1613 $fname = 'User::spreadBlock';
1615 wfDebug( "User:spreadBlock()\n" );
1616 if ( $this->mId
== 0 ) {
1620 $userblock = Block
::newFromDB( '', $this->mId
);
1621 if ( !$userblock ) {
1625 # Check if this IP address is already blocked
1626 $ipblock = Block
::newFromDB( wfGetIP() );
1628 # If the user is already blocked. Then check if the autoblock would
1629 # excede the user block. If it would excede, then do nothing, else
1630 # prolong block time
1631 if ($userblock->mExpiry
&&
1632 ($userblock->mExpiry
< Block
::getAutoblockExpiry($ipblock->mTimestamp
))) {
1635 # Just update the timestamp
1636 $ipblock->updateTimestamp();
1639 $ipblock = new Block
;
1642 # Make a new block object with the desired properties
1643 wfDebug( "Autoblocking {$this->mName}@" . wfGetIP() . "\n" );
1644 $ipblock->mAddress
= wfGetIP();
1645 $ipblock->mUser
= 0;
1646 $ipblock->mBy
= $userblock->mBy
;
1647 $ipblock->mReason
= wfMsg( 'autoblocker', $this->getName(), $userblock->mReason
);
1648 $ipblock->mTimestamp
= wfTimestampNow();
1649 $ipblock->mAuto
= 1;
1650 # If the user is already blocked with an expiry date, we don't
1651 # want to pile on top of that!
1652 if($userblock->mExpiry
) {
1653 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
1655 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
1664 * Generate a string which will be different for any combination of
1665 * user options which would produce different parser output.
1666 * This will be used as part of the hash key for the parser cache,
1667 * so users will the same options can share the same cached data
1670 * Extensions which require it should install 'PageRenderingHash' hook,
1671 * which will give them a chance to modify this key based on their own
1676 function getPageRenderingHash() {
1677 global $wgContLang, $wgUseDynamicDates;
1679 return $this->mHash
;
1682 // stubthreshold is only included below for completeness,
1683 // it will always be 0 when this function is called by parsercache.
1685 $confstr = $this->getOption( 'math' );
1686 $confstr .= '!' . $this->getOption( 'stubthreshold' );
1687 if ( $wgUseDynamicDates ) {
1688 $confstr .= '!' . $this->getDatePreference();
1690 $confstr .= '!' . ($this->getOption( 'numberheadings' ) ?
'1' : '');
1691 $confstr .= '!' . $this->getOption( 'language' );
1692 $confstr .= '!' . $this->getOption( 'thumbsize' );
1693 // add in language specific options, if any
1694 $extra = $wgContLang->getExtraHashOptions();
1697 // Give a chance for extensions to modify the hash, if they have
1698 // extra options or other effects on the parser cache.
1699 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
1701 $this->mHash
= $confstr;
1705 function isBlockedFromCreateAccount() {
1706 $this->getBlockedStatus();
1707 return $this->mBlock
&& $this->mBlock
->mCreateAccount
;
1710 function isAllowedToCreateAccount() {
1711 return $this->isAllowed( 'createaccount' ) && !$this->isBlockedFromCreateAccount();
1715 * Set mDataLoaded, return previous value
1716 * Use this to prevent DB access in command-line scripts or similar situations
1718 function setLoaded( $loaded ) {
1719 return wfSetVar( $this->mDataLoaded
, $loaded );
1723 * Get this user's personal page title.
1728 function getUserPage() {
1729 return Title
::makeTitle( NS_USER
, $this->getName() );
1733 * Get this user's talk page title.
1738 function getTalkPage() {
1739 $title = $this->getUserPage();
1740 return $title->getTalkPage();
1746 function getMaxID() {
1747 static $res; // cache
1749 if ( isset( $res ) )
1752 $dbr =& wfGetDB( DB_SLAVE
);
1753 return $res = $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
1758 * Determine whether the user is a newbie. Newbies are either
1759 * anonymous IPs, or the most recently created accounts.
1760 * @return bool True if it is a newbie.
1762 function isNewbie() {
1763 return !$this->isAllowed( 'autoconfirmed' );
1767 * Check to see if the given clear-text password is one of the accepted passwords
1768 * @param string $password User password.
1769 * @return bool True if the given password is correct otherwise False.
1771 function checkPassword( $password ) {
1772 global $wgAuth, $wgMinimalPasswordLength;
1773 $this->loadFromDatabase();
1775 // Even though we stop people from creating passwords that
1776 // are shorter than this, doesn't mean people wont be able
1777 // to. Certain authentication plugins do NOT want to save
1778 // domain passwords in a mysql database, so we should
1779 // check this (incase $wgAuth->strict() is false).
1780 if( strlen( $password ) < $wgMinimalPasswordLength ) {
1784 if( $wgAuth->authenticate( $this->getName(), $password ) ) {
1786 } elseif( $wgAuth->strict() ) {
1787 /* Auth plugin doesn't allow local authentication */
1790 $ep = $this->encryptPassword( $password );
1791 if ( 0 == strcmp( $ep, $this->mPassword
) ) {
1793 } elseif ( ($this->mNewpassword
!= '') && (0 == strcmp( $ep, $this->mNewpassword
)) ) {
1795 } elseif ( function_exists( 'iconv' ) ) {
1796 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
1797 # Check for this with iconv
1798 $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
1799 if ( 0 == strcmp( $cp1252hash, $this->mPassword
) ) {
1807 * Initialize (if necessary) and return a session token value
1808 * which can be used in edit forms to show that the user's
1809 * login credentials aren't being hijacked with a foreign form
1812 * @param mixed $salt - Optional function-specific data for hash.
1813 * Use a string or an array of strings.
1817 function editToken( $salt = '' ) {
1818 if( !isset( $_SESSION['wsEditToken'] ) ) {
1819 $token = $this->generateToken();
1820 $_SESSION['wsEditToken'] = $token;
1822 $token = $_SESSION['wsEditToken'];
1824 if( is_array( $salt ) ) {
1825 $salt = implode( '|', $salt );
1827 return md5( $token . $salt );
1831 * Generate a hex-y looking random token for various uses.
1832 * Could be made more cryptographically sure if someone cares.
1835 function generateToken( $salt = '' ) {
1836 $token = dechex( mt_rand() ) . dechex( mt_rand() );
1837 return md5( $token . $salt );
1841 * Check given value against the token value stored in the session.
1842 * A match should confirm that the form was submitted from the
1843 * user's own login session, not a form submission from a third-party
1846 * @param string $val - the input value to compare
1847 * @param string $salt - Optional function-specific data for hash
1851 function matchEditToken( $val, $salt = '' ) {
1853 $sessionToken = $this->editToken( $salt );
1854 if ( $val != $sessionToken ) {
1855 wfDebug( "User::matchEditToken: broken session data\n" );
1857 return $val == $sessionToken;
1861 * Generate a new e-mail confirmation token and send a confirmation
1862 * mail to the user's given address.
1864 * @return mixed True on success, a WikiError object on failure.
1866 function sendConfirmationMail() {
1868 $url = $this->confirmationTokenUrl( $expiration );
1869 return $this->sendMail( wfMsg( 'confirmemail_subject' ),
1870 wfMsg( 'confirmemail_body',
1874 $wgContLang->timeanddate( $expiration, false ) ) );
1878 * Send an e-mail to this user's account. Does not check for
1879 * confirmed status or validity.
1881 * @param string $subject
1882 * @param string $body
1883 * @param strong $from Optional from address; default $wgPasswordSender will be used otherwise.
1884 * @return mixed True on success, a WikiError object on failure.
1886 function sendMail( $subject, $body, $from = null ) {
1887 if( is_null( $from ) ) {
1888 global $wgPasswordSender;
1889 $from = $wgPasswordSender;
1892 require_once( 'UserMailer.php' );
1893 $to = new MailAddress( $this );
1894 $sender = new MailAddress( $from );
1895 $error = userMailer( $to, $sender, $subject, $body );
1897 if( $error == '' ) {
1900 return new WikiError( $error );
1905 * Generate, store, and return a new e-mail confirmation code.
1906 * A hash (unsalted since it's used as a key) is stored.
1907 * @param &$expiration mixed output: accepts the expiration time
1911 function confirmationToken( &$expiration ) {
1912 $fname = 'User::confirmationToken';
1915 $expires = $now +
7 * 24 * 60 * 60;
1916 $expiration = wfTimestamp( TS_MW
, $expires );
1918 $token = $this->generateToken( $this->mId
. $this->mEmail
. $expires );
1919 $hash = md5( $token );
1921 $dbw =& wfGetDB( DB_MASTER
);
1922 $dbw->update( 'user',
1923 array( 'user_email_token' => $hash,
1924 'user_email_token_expires' => $dbw->timestamp( $expires ) ),
1925 array( 'user_id' => $this->mId
),
1932 * Generate and store a new e-mail confirmation token, and return
1933 * the URL the user can use to confirm.
1934 * @param &$expiration mixed output: accepts the expiration time
1938 function confirmationTokenUrl( &$expiration ) {
1939 $token = $this->confirmationToken( $expiration );
1940 $title = Title
::makeTitle( NS_SPECIAL
, 'Confirmemail/' . $token );
1941 return $title->getFullUrl();
1945 * Mark the e-mail address confirmed and save.
1947 function confirmEmail() {
1948 $this->loadFromDatabase();
1949 $this->mEmailAuthenticated
= wfTimestampNow();
1950 $this->saveSettings();
1955 * Is this user allowed to send e-mails within limits of current
1956 * site configuration?
1959 function canSendEmail() {
1960 return $this->isEmailConfirmed();
1964 * Is this user allowed to receive e-mails within limits of current
1965 * site configuration?
1968 function canReceiveEmail() {
1969 return $this->canSendEmail() && !$this->getOption( 'disablemail' );
1973 * Is this user's e-mail address valid-looking and confirmed within
1974 * limits of the current site configuration?
1976 * If $wgEmailAuthentication is on, this may require the user to have
1977 * confirmed their address by returning a code or using a password
1978 * sent to the address from the wiki.
1982 function isEmailConfirmed() {
1983 global $wgEmailAuthentication;
1984 $this->loadFromDatabase();
1986 if( wfRunHooks( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
1987 if( $this->isAnon() )
1989 if( !$this->isValidEmailAddr( $this->mEmail
) )
1991 if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
2000 * @param array $groups list of groups
2001 * @return array list of permission key names for given groups combined
2004 function getGroupPermissions( $groups ) {
2005 global $wgGroupPermissions;
2007 foreach( $groups as $group ) {
2008 if( isset( $wgGroupPermissions[$group] ) ) {
2009 $rights = array_merge( $rights,
2010 array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
2017 * @param string $group key name
2018 * @return string localized descriptive name for group, if provided
2021 function getGroupName( $group ) {
2022 $key = "group-$group";
2023 $name = wfMsg( $key );
2024 if( $name == '' ||
$name == "<$key>" ) {
2032 * @param string $group key name
2033 * @return string localized descriptive name for member of a group, if provided
2036 function getGroupMember( $group ) {
2037 $key = "group-$group-member";
2038 $name = wfMsg( $key );
2039 if( $name == '' ||
$name == "<$key>" ) {
2048 * Return the set of defined explicit groups.
2049 * The *, 'user', 'autoconfirmed' and 'emailconfirmed'
2050 * groups are not included, as they are defined
2051 * automatically, not in the database.
2055 function getAllGroups() {
2056 global $wgGroupPermissions;
2058 array_keys( $wgGroupPermissions ),
2059 array( '*', 'user', 'autoconfirmed', 'emailconfirmed' ) );
2063 * Get the title of a page describing a particular group
2065 * @param $group Name of the group
2068 function getGroupPage( $group ) {
2069 $page = wfMsgForContent( 'grouppage-' . $group );
2070 if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
2071 $title = Title
::newFromText( $page );
2072 if( is_object( $title ) )