Fix {{NUMBEROFADMINS}} magic word
[lhc/web/wiklou.git] / includes / User.php
index 8f4ca42..f695d1d 100644 (file)
@@ -5,11 +5,6 @@
  * @package MediaWiki
  */
 
-/**
- *
- */
-require_once( 'WatchedItem.php' );
-
 # Number of characters in user_token field
 define( 'USER_TOKEN_LENGTH', 32 );
 
@@ -21,24 +16,35 @@ define( 'MW_USER_VERSION', 3 );
  * @package MediaWiki
  */
 class User {
-       /**#@+
-        * @access private
-        */
-       var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
-       var $mEmailAuthenticated;
-       var $mRights, $mOptions;
-       var $mDataLoaded, $mNewpassword;
-       var $mSkin;
-       var $mBlockedby, $mBlockreason;
-       var $mTouched;
-       var $mToken;
-       var $mRealName;
-       var $mHash;
-       var $mGroups;
-       var $mVersion; // serialized version
-       var $mRegistration;
-
-       /** Construct using User:loadDefaults() */
+       /*
+        * When adding a new private variable, dont forget to add it to __sleep()
+        */
+       /**@{{
+        * @private
+        */
+       var $mBlockedby;        //!<
+       var $mBlockreason;      //!<
+       var $mDataLoaded;       //!<
+       var $mEmail;            //!<
+       var $mEmailAuthenticated; //!<
+       var $mGroups;           //!<
+       var $mHash;                     //!<
+       var $mId;                       //!<
+       var $mName;                     //!<
+       var $mNewpassword;      //!<
+       var $mNewtalk;          //!<
+       var $mOptions;          //!<
+       var $mPassword;         //!<
+       var $mRealName;         //!<
+       var $mRegistration;     //!<
+       var $mRights;           //!<
+       var $mSkin;                     //!<
+       var $mToken;            //!<
+       var $mTouched;          //!<
+       var $mVersion;          //!< serialized version
+       /**@}} */
+
+       /** Constructor using User:loadDefaults() */
        function User() {
                $this->loadDefaults();
                $this->mVersion = MW_USER_VERSION;
@@ -47,10 +53,11 @@ class User {
        /**
         * Static factory method
         * @param string $name Username, validated by Title:newFromText()
+        * @param bool $validate Validate username
         * @return User
         * @static
         */
-       function newFromName( $name ) {
+       function newFromName( $name, $validate = true ) {
                # Force usernames to capital
                global $wgContLang;
                $name = $wgContLang->ucfirst( $name );
@@ -66,7 +73,7 @@ class User {
                global $wgAuth;
                $canonicalName = $wgAuth->getCanonicalName( $t->getText() );
 
-               if( !User::isValidUserName( $canonicalName ) ) {
+               if( $validate && !User::isValidUserName( $canonicalName ) ) {
                        return null;
                }
 
@@ -102,13 +109,31 @@ class User {
 
        /**
         * Serialze sleep function, for better cache efficiency and avoidance of
-        * silly "incomplete type" errors when skins are cached
+        * silly "incomplete type" errors when skins are cached. The array should
+        * contain names of private variables (see at top of User.php).
         */
        function __sleep() {
-               return array( 'mId', 'mName', 'mPassword', 'mEmail', 'mNewtalk',
-                       'mEmailAuthenticated', 'mRights', 'mOptions', 'mDataLoaded',
-                       'mNewpassword', 'mBlockedby', 'mBlockreason', 'mTouched',
-                       'mToken', 'mRealName', 'mHash', 'mGroups', 'mRegistration' );
+               return array(
+'mBlockedby',
+'mBlockreason',
+'mDataLoaded',
+'mEmail',
+'mEmailAuthenticated',
+'mGroups',
+'mHash',
+'mId',
+'mName',
+'mNewpassword',
+'mNewtalk',
+'mOptions',
+'mPassword',
+'mRealName',
+'mRegistration',
+'mRights',
+'mToken',
+'mTouched',
+'mVersion',
+);
        }
 
        /**
@@ -158,9 +183,14 @@ class User {
        }
 
        /**
-        * does the string match an anonymous IPv4 address?
+        * Does the string match an anonymous IPv4 address?
         *
-        * Note: We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
+        * This function exists for username validation, in order to reject
+        * usernames which are similar in form to IP addresses. Strings such
+        * as 300.300.300.300 will return true because it looks like an IP 
+        * address, despite not being strictly valid.
+        * 
+        * We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
         * address because the usemod software would "cloak" anonymous IP
         * addresses like this, if we allowed accounts like this to be created
         * new users could get the old edits of these anonymous users.
@@ -347,7 +377,7 @@ class User {
         *
         * @return array
         * @static
-        * @access private
+        * @private
         */
        function getDefaultOptions() {
                /**
@@ -372,7 +402,7 @@ class User {
         * @param string $opt
         * @return string
         * @static
-        * @access public
+        * @public
         */
        function getDefaultOption( $opt ) {
                $defOpts = User::getDefaultOptions();
@@ -385,7 +415,7 @@ class User {
 
        /**
         * Get blocking information
-        * @access private
+        * @private
         * @param bool $bFromSlave Specify whether to check slave or master. To improve performance,
         *  non-critical checks are done against slaves. Check when actually saving should be done against
         *  master.
@@ -420,7 +450,8 @@ class User {
                }
 
                # Proxy blocking
-               if ( !$this->isSysop() && !in_array( $ip, $wgProxyWhitelist ) ) {
+               # FIXME ? proxyunbannable is to deprecate the old isSysop()
+               if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
 
                        # Local list
                        if ( wfIsLocallyBlockedProxy( $ip ) ) {
@@ -449,12 +480,6 @@ class User {
                        $this->inDnsBlacklist( $ip, 'http.dnsbl.sorbs.net.' );
        }
 
-       function inOpmBlacklist( $ip ) {
-               global $wgEnableOpm;
-               return $wgEnableOpm &&
-                       $this->inDnsBlacklist( $ip, 'opm.blitzed.org.' );
-       }
-
        function inDnsBlacklist( $ip, $base ) {
                $fname = 'User::inDnsBlacklist';
                wfProfileIn( $fname );
@@ -492,18 +517,20 @@ class User {
         * last-hit counters will be shared across wikis.
         *
         * @return bool true if a rate limiter was tripped
-        * @access public
+        * @public
         */
        function pingLimiter( $action='edit' ) {
-               global $wgRateLimits;
+               global $wgRateLimits, $wgRateLimitsExcludedGroups;
                if( !isset( $wgRateLimits[$action] ) ) {
                        return false;
                }
-               if( $this->isAllowed( 'delete' ) ) {
-                       // goddam cabal
-                       return false;
+               
+               # Some groups shouldn't trigger the ping limiter, ever
+               foreach( $this->getGroups() as $group ) {
+                       if( array_search( $group, $wgRateLimitsExcludedGroups ) !== false )
+                               return false;
                }
-
+               
                global $wgMemc, $wgDBname, $wgRateLimitLog;
                $fname = 'User::pingLimiter';
                wfProfileIn( $fname );
@@ -777,7 +804,7 @@ class User {
        /**
         * Return the title dbkey form of the name, for eg user pages.
         * @return string
-        * @access public
+        * @public
         */
        function getTitleKey() {
                return str_replace( ' ', '_', $this->getName() );
@@ -835,7 +862,7 @@ class User {
         * @param string $field
         * @param mixed $id
         * @return bool
-        * @access private
+        * @private
         */
        function checkNewtalk( $field, $id ) {
                $fname = 'User::checkNewtalk';
@@ -849,7 +876,7 @@ class User {
         * Add or update the
         * @param string $field
         * @param mixed $id
-        * @access private
+        * @private
         */
        function updateNewtalk( $field, $id ) {
                $fname = 'User::updateNewtalk';
@@ -870,7 +897,7 @@ class User {
         * Clear the new messages flag for the given user
         * @param string $field
         * @param mixed $id
-        * @access private
+        * @private
         */
        function deleteNewtalk( $field, $id ) {
                $fname = 'User::deleteNewtalk';
@@ -1037,6 +1064,20 @@ class User {
        function getBoolOption( $oname ) {
                return (bool)$this->getOption( $oname );
        }
+       
+       /**
+        * Get an option as an integer value from the source string.
+        * @param string $oname The option to check
+        * @param int $default Optional value to return if option is unset/blank.
+        * @return int
+        */
+       function getIntOption( $oname, $default=0 ) {
+               $val = $this->getOption( $oname );
+               if( $val == '' ) {
+                       $val = $default;
+               }
+               return intval( $val );
+       }
 
        function setOption( $oname, $val ) {
                $this->loadFromDatabase();
@@ -1044,6 +1085,11 @@ class User {
                        # Clear cached skin, so the new one displays immediately in Special:Preferences
                        unset( $this->mSkin );
                }
+               // Filter out any newlines that may have passed through input validation.
+               // Newlines are used to separate items in the options blob.
+               $val = str_replace( "\r\n", "\n", $val );
+               $val = str_replace( "\r", "\n", $val );
+               $val = str_replace( "\n", " ", $val );
                $this->mOptions[$oname] = $val;
                $this->invalidateCache();
        }
@@ -1142,21 +1188,30 @@ class User {
        }
 
        /**
-        * Check if a user is sysop
+        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
         * @deprecated
         */
        function isSysop() {
-               return $this->isAllowed( 'protect' );
+               throw new MWException( "Call to deprecated (v1.7) User::isSysop() method\n" );
+               #return $this->isAllowed( 'protect' );
        }
 
-       /** @deprecated */
+       /**
+        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
+        * @deprecated
+        */
        function isDeveloper() {
-               return $this->isAllowed( 'siteadmin' );
+               throw new MWException( "Call to deprecated (v1.7) User::isDeveloper() method\n" );
+               #return $this->isAllowed( 'siteadmin' );
        }
 
-       /** @deprecated */
+       /**
+        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
+        * @deprecated
+        */
        function isBureaucrat() {
-               return $this->isAllowed( 'makesysop' );
+               throw new MWException( "Call to deprecated (v1.7) User::isBureaucrat() method\n" );
+               #return $this->isAllowed( 'makesysop' );
        }
 
        /**
@@ -1296,7 +1351,7 @@ class User {
         * the next change of any watched page.
         *
         * @param int $currentUser user ID number
-        * @access public
+        * @public
         */
        function clearAllNotifications( $currentUser ) {
                global $wgUseEnotif;
@@ -1321,7 +1376,7 @@ class User {
        }
 
        /**
-        * @access private
+        * @private
         * @return string Encoding options
         */
        function encodeOptions() {
@@ -1334,7 +1389,7 @@ class User {
        }
 
        /**
-        * @access private
+        * @private
         */
        function decodeOptions( $str ) {
                $a = explode( "\n", $str );
@@ -1346,22 +1401,22 @@ class User {
        }
 
        function setCookies() {
-               global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgDBname;
+               global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
                if ( 0 == $this->mId ) return;
                $this->loadFromDatabase();
                $exp = time() + $wgCookieExpiration;
 
                $_SESSION['wsUserID'] = $this->mId;
-               setcookie( $wgDBname.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               setcookie( $wgCookiePrefix.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
 
                $_SESSION['wsUserName'] = $this->getName();
-               setcookie( $wgDBname.'UserName', $this->getName(), $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               setcookie( $wgCookiePrefix.'UserName', $this->getName(), $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
 
                $_SESSION['wsToken'] = $this->mToken;
                if ( 1 == $this->getOption( 'rememberpassword' ) ) {
-                       setcookie( $wgDBname.'Token', $this->mToken, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+                       setcookie( $wgCookiePrefix.'Token', $this->mToken, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
                } else {
-                       setcookie( $wgDBname.'Token', '', time() - 3600 );
+                       setcookie( $wgCookiePrefix.'Token', '', time() - 3600 );
                }
        }
 
@@ -1370,17 +1425,17 @@ class User {
         * It will clean the session cookie
         */
        function logout() {
-               global $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgDBname;
+               global $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
                $this->loadDefaults();
                $this->setLoaded( true );
 
                $_SESSION['wsUserID'] = 0;
 
-               setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
-               setcookie( $wgDBname.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               setcookie( $wgCookiePrefix.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               setcookie( $wgCookiePrefix.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
 
                # Remember when user logged out, to prevent seeing cached pages
-               setcookie( $wgDBname.'LoggedOut', wfTimestampNow(), time() + 86400, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               setcookie( $wgCookiePrefix.'LoggedOut', wfTimestampNow(), time() + 86400, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
        }
 
        /**
@@ -1562,7 +1617,7 @@ class User {
         * Get this user's personal page title.
         *
         * @return Title
-        * @access public
+        * @public
         */
        function getUserPage() {
                return Title::makeTitle( NS_USER, $this->getName() );
@@ -1572,7 +1627,7 @@ class User {
         * Get this user's talk page title.
         *
         * @return Title
-        * @access public
+        * @public
         */
        function getTalkPage() {
                $title = $this->getUserPage();
@@ -1651,7 +1706,7 @@ class User {
         * @param mixed $salt - Optional function-specific data for hash.
         *                      Use a string or an array of strings.
         * @return string
-        * @access public
+        * @public
         */
        function editToken( $salt = '' ) {
                if( !isset( $_SESSION['wsEditToken'] ) ) {
@@ -1685,27 +1740,15 @@ class User {
         * @param string $val - the input value to compare
         * @param string $salt - Optional function-specific data for hash
         * @return bool
-        * @access public
+        * @public
         */
        function matchEditToken( $val, $salt = '' ) {
                global $wgMemc;
-
-/*
-               if ( !isset( $_SESSION['wsEditToken'] ) ) {
-                       $logfile = '/home/wikipedia/logs/session_debug/session.log';
-                       $mckey = memsess_key( session_id() );
-                       $uname = @posix_uname();
-                       $msg = "wsEditToken not set!\n" .
-                       'apache server=' . $uname['nodename'] . "\n" .
-                       'session_id = ' . session_id() . "\n" .
-                       '$_SESSION=' . var_export( $_SESSION, true ) . "\n" .
-                       '$_COOKIE=' . var_export( $_COOKIE, true ) . "\n" .
-                       "mc get($mckey) = " . var_export( $wgMemc->get( $mckey ), true ) . "\n\n\n";
-
-                       @error_log( $msg, 3, $logfile );
+               $sessionToken = $this->editToken( $salt );
+               if ( $val != $sessionToken ) {
+                       wfDebug( "User::matchEditToken: broken session data\n" );
                }
-*/
-               return ( $val == $this->editToken( $salt ) );
+               return $val == $sessionToken;
        }
 
        /**
@@ -1757,7 +1800,7 @@ class User {
         * A hash (unsalted since it's used as a key) is stored.
         * @param &$expiration mixed output: accepts the expiration time
         * @return string
-        * @access private
+        * @private
         */
        function confirmationToken( &$expiration ) {
                $fname = 'User::confirmationToken';
@@ -1784,7 +1827,7 @@ class User {
         * the URL the user can use to confirm.
         * @param &$expiration mixed output: accepts the expiration time
         * @return string
-        * @access private
+        * @private
         */
        function confirmationTokenUrl( &$expiration ) {
                $token = $this->confirmationToken( $expiration );
@@ -1833,13 +1876,18 @@ class User {
        function isEmailConfirmed() {
                global $wgEmailAuthentication;
                $this->loadFromDatabase();
-               if( $this->isAnon() )
-                       return false;
-               if( !$this->isValidEmailAddr( $this->mEmail ) )
-                       return false;
-               if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
-                       return false;
-               return true;
+               $confirmed = true;
+               if( wfRunHooks( 'EmailConfirmed', array( &$this, &$confirmed ) ) ) {
+                       if( $this->isAnon() )
+                               return false;
+                       if( !$this->isValidEmailAddr( $this->mEmail ) )
+                               return false;
+                       if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
+                               return false;
+                       return true;
+               } else {
+                       return $confirmed;
+               }
        }
 
        /**
@@ -1861,11 +1909,11 @@ class User {
 
        /**
         * @param string $group key name
-        * @return string localized descriptive name, if provided
+        * @return string localized descriptive name for group, if provided
         * @static
         */
        function getGroupName( $group ) {
-               $key = "group-$group-name";
+               $key = "group-$group";
                $name = wfMsg( $key );
                if( $name == '' || $name == "&lt;$key&gt;" ) {
                        return $group;
@@ -1874,6 +1922,22 @@ class User {
                }
        }
 
+       /**
+        * @param string $group key name
+        * @return string localized descriptive name for member of a group, if provided
+        * @static
+        */
+       function getGroupMember( $group ) {
+               $key = "group-$group-member";
+               $name = wfMsg( $key );
+               if( $name == '' || $name == "&lt;$key&gt;" ) {
+                       return $group;
+               } else {
+                       return $name;
+               }
+       }
+
+
        /**
         * Return the set of defined explicit groups.
         * The * and 'user' groups are not included.
@@ -1886,6 +1950,24 @@ class User {
                        array_keys( $wgGroupPermissions ),
                        array( '*', 'user', 'autoconfirmed' ) );
        }
+       
+       /**
+        * Get the title of a page describing a particular group
+        *
+        * @param $group Name of the group
+        * @return mixed
+        */
+       function getGroupPage( $group ) {
+               $page = wfMsgForContent( 'grouppage-' . $group );
+               if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
+                       $title = Title::newFromText( $page );
+                       if( is_object( $title ) )
+                               return $title;
+               }
+               return false;
+       }
+       
+       
 }
 
 ?>