* Forbid usernames that can be interpreted as titles with namespaces, as that leads...
[lhc/web/wiklou.git] / includes / User.php
index 9022831..1e3809d 100644 (file)
@@ -50,8 +50,6 @@ class User {
         * @static
         */
        function newFromName( $name ) {
-               $u = new User();
-
                # Force usernames to capital
                global $wgContLang;
                $name = $wgContLang->ucfirst( $name );
@@ -71,6 +69,7 @@ class User {
                        return null;
                }
 
+               $u = new User();
                $u->setName( $canonicalName );
                $u->setId( $u->idFromName( $canonicalName ) );
                return $u;
@@ -119,7 +118,7 @@ class User {
         */
        function whoIs( $id )   {
                $dbr =& wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ) );
+               return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
        }
 
        /**
@@ -130,7 +129,7 @@ class User {
         */
        function whoIsReal( $id )       {
                $dbr =& wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ) );
+               return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), 'User::whoIsReal' );
        }
 
        /**
@@ -160,12 +159,19 @@ class User {
        /**
         * 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
+        * 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.
+        *
+        * @bug 3631
+        *
         * @static
         * @param string $name Nickname of a user
         * @return bool
         */
        function isIP( $name ) {
-               return preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/",$name);
+               return preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/",$name);
                /*return preg_match("/^
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
@@ -195,6 +201,14 @@ class User {
                || strlen( $name ) > $wgMaxNameChars
                || $name != $wgContLang->ucfirst( $name ) )
                        return false;
+               
+               // Ensure that the name can't be misresolved as a different title,
+               // such as with extra namespace keys at the start.
+               $parsed = Title::newFromText( $name );
+               if( is_null( $parsed )
+                       || $parsed->getNamespace()
+                       || strcmp( $name, $parsed->getPrefixedText() ) )
+                       return false;
                else
                        return true;
        }
@@ -212,7 +226,11 @@ class User {
        }
 
        /**
-        * does the string match roughly an email address ?
+        * Does the string match roughly an email address ?
+        *
+        * There used to be a regular expression here, it got removed because it
+        * rejected valid addresses. Actually just check if there is '@' somewhere
+        * in the given address.
         *
         * @todo Check for RFC 2822 compilance
         * @bug 959
@@ -222,8 +240,6 @@ class User {
         * @return bool
         */
        function isValidEmailAddr ( $addr ) {
-               # There used to be a regular expression here, it got removed because it
-               # rejected valid addresses.
                return ( trim( $addr ) != '' ) &&
                        (false !== strpos( $addr, '@' ) );
        }
@@ -235,7 +251,7 @@ class User {
         * @return int
         */
        function edits( $uid ) {
-               $fname = 'User::editCount';
+               $fname = 'User::edits';
 
                $dbr =& wfGetDB( DB_SLAVE );
                return $dbr->selectField(
@@ -273,12 +289,12 @@ class User {
                $fname = 'User::loadDefaults' . $n;
                wfProfileIn( $fname );
 
-               global $wgContLang, $wgIP, $wgDBname;
+               global $wgContLang, $wgDBname;
                global $wgNamespacesToBeSearchedDefault;
 
                $this->mId = 0;
                $this->mNewtalk = -1;
-               $this->mName = $wgIP;
+               $this->mName = false;
                $this->mRealName = $this->mEmail = '';
                $this->mEmailAuthenticated = null;
                $this->mPassword = $this->mNewpassword = '';
@@ -360,32 +376,44 @@ class User {
         * And it's cheaper to check slave first, then master if needed, than master always.
         */
        function getBlockedStatus( $bFromSlave = true ) {
-               global $wgIP, $wgBlockCache, $wgProxyList, $wgEnableSorbs, $wgProxyWhitelist;
+               global $wgBlockCache, $wgProxyList, $wgEnableSorbs, $wgProxyWhitelist;
 
-               if ( -1 != $this->mBlockedby ) { return; }
+               if ( -1 != $this->mBlockedby ) {
+                       wfDebug( "User::getBlockedStatus: already loaded.\n" );
+                       return;
+               }
 
-               $this->mBlockedby = 0;
+               $fname = 'User::getBlockedStatus';
+               wfProfileIn( $fname );
+               wfDebug( "$fname: checking...\n" );
 
-               # User blocking
-               if ( $this->mId ) {
-                       $block = new Block();
-                       $block->forUpdate( $bFromSlave );
-                       if ( $block->load( $wgIP , $this->mId ) ) {
-                               $this->mBlockedby = $block->mBy;
-                               $this->mBlockreason = $block->mReason;
+               $this->mBlockedby = 0;
+               $ip = wfGetIP();
+
+               # User/IP blocking
+               $block = new Block();
+               $block->forUpdate( $bFromSlave );
+               if ( $block->load( $ip , $this->mId ) ) {
+                       wfDebug( "$fname: Found block.\n" );
+                       $this->mBlockedby = $block->mBy;
+                       $this->mBlockreason = $block->mReason;
+                       if ( $this->isLoggedIn() ) {
                                $this->spreadBlock();
                        }
+               } else {
+                       wfDebug( "$fname: No block.\n" );
                }
 
-               # IP/range blocking
+               # Range blocking
                if ( !$this->mBlockedby ) {
                        # Check first against slave, and optionally from master.
-                       $block = $wgBlockCache->get( $wgIP, true );
+                       wfDebug( "$fname: Checking range blocks\n" );
+                       $block = $wgBlockCache->get( $ip, true );
                        if ( !$block && !$bFromSlave )
                                {
                                # Not blocked: check against master, to make sure.
                                $wgBlockCache->clearLocal( );
-                               $block = $wgBlockCache->get( $wgIP, false );
+                               $block = $wgBlockCache->get( $ip, false );
                                }
                        if ( $block !== false ) {
                                $this->mBlockedby = $block->mBy;
@@ -394,22 +422,27 @@ class User {
                }
 
                # Proxy blocking
-               if ( !$this->isSysop() && !in_array( $wgIP, $wgProxyWhitelist ) ) {
+               if ( !$this->isSysop() && !in_array( $ip, $wgProxyWhitelist ) ) {
 
                        # Local list
-                       if ( array_key_exists( $wgIP, $wgProxyList ) ) {
+                       if ( array_key_exists( $ip, $wgProxyList ) ) {
                                $this->mBlockedby = wfMsg( 'proxyblocker' );
                                $this->mBlockreason = wfMsg( 'proxyblockreason' );
                        }
 
                        # DNSBL
                        if ( !$this->mBlockedby && $wgEnableSorbs && !$this->getID() ) {
-                               if ( $this->inSorbsBlacklist( $wgIP ) ) {
+                               if ( $this->inSorbsBlacklist( $ip ) ) {
                                        $this->mBlockedby = wfMsg( 'sorbs' );
                                        $this->mBlockreason = wfMsg( 'sorbsreason' );
                                }
                        }
                }
+
+               # Extensions
+               wfRunHooks( 'GetBlockedStatus', array( &$this ) );
+
+               wfProfileOut( $fname );
        }
 
        function inSorbsBlacklist( $ip ) {
@@ -473,11 +506,14 @@ class User {
                        return false;
                }
 
-               global $wgMemc, $wgIP, $wgDBname, $wgRateLimitLog;
+               global $wgMemc, $wgDBname, $wgRateLimitLog;
                $fname = 'User::pingLimiter';
+               wfProfileIn( $fname );
+
                $limits = $wgRateLimits[$action];
                $keys = array();
                $id = $this->getId();
+               $ip = wfGetIP();
 
                if( isset( $limits['anon'] ) && $id == 0 ) {
                        $keys["$wgDBname:limiter:$action:anon"] = $limits['anon'];
@@ -491,9 +527,9 @@ class User {
                                $keys["$wgDBname:limiter:$action:user:$id"] = $limits['newbie'];
                        }
                        if( isset( $limits['ip'] ) ) {
-                               $keys["mediawiki:limiter:$action:ip:$wgIP"] = $limits['ip'];
+                               $keys["mediawiki:limiter:$action:ip:$ip"] = $limits['ip'];
                        }
-                       if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $wgIP, $matches ) ) {
+                       if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $ip, $matches ) ) {
                                $subnet = $matches[1];
                                $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
                        }
@@ -516,11 +552,12 @@ class User {
                                }
                        } else {
                                wfDebug( "$fname: adding record for $key $summary\n" );
-                               $wgMemc->add( $key, 1, IntVal( $period ) );
+                               $wgMemc->add( $key, 1, intval( $period ) );
                        }
                        $wgMemc->incr( $key );
                }
 
+               wfProfileOut( $fname );
                return $triggered;
        }
 
@@ -529,6 +566,7 @@ class User {
         * @return bool True if blocked, false otherwise
         */
        function isBlocked( $bFromSlave = true ) { // hacked from false due to horrible probs on site
+               wfDebug( "User::isBlocked: enter\n" );
                $this->getBlockedStatus( $bFromSlave );
                return $this->mBlockedby !== 0;
        }
@@ -538,13 +576,21 @@ class User {
         */
        function isBlockedFrom( $title, $bFromSlave = false ) {
                global $wgBlockAllowsUTEdit;
+               $fname = 'User::isBlockedFrom';
+               wfProfileIn( $fname );
+               wfDebug( "$fname: enter\n" );
+
                if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
                  $title->getNamespace() == NS_USER_TALK )
                {
-                       return false;
+                       $blocked = false;
+                       wfDebug( "$fname: self-talk page, ignoring any blocks\n" );
                } else {
-                       return $this->isBlocked( $bFromSlave );
+                       wfDebug( "$fname: asking isBlocked()\n" );
+                       $blocked = $this->isBlocked( $bFromSlave );
                }
+               wfProfileOut( $fname );
+               return $blocked;
        }
 
        /**
@@ -583,7 +629,7 @@ class User {
        }
 
        /**
-        * Read datas from session
+        * Create a new user object using data from session
         * @static
         */
        function loadFromSession() {
@@ -596,7 +642,7 @@ class User {
                                return new User();
                        }
                } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
-                       $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
+                       $sId = intval( $_COOKIE["{$wgDBname}UserID"] );
                        $_SESSION['wsUserID'] = $sId;
                } else {
                        return new User();
@@ -660,7 +706,7 @@ class User {
                }
 
                # Paranoia
-               $this->mId = IntVal( $this->mId );
+               $this->mId = intval( $this->mId );
 
                /** Anonymous user */
                if( !$this->mId ) {
@@ -710,6 +756,9 @@ class User {
 
        function getName() {
                $this->loadFromDatabase();
+               if ( $this->mName === false ) {
+                       $this->mName = wfGetIP();
+               }
                return $this->mName;
        }
 
@@ -741,7 +790,7 @@ class User {
                        # entire User object stored in there.
                        if( !$this->mId ) {
                                global $wgDBname, $wgMemc;
-                               $key = "$wgDBname:newtalk:ip:{$this->mName}";
+                               $key = "$wgDBname:newtalk:ip:" . $this->getName();
                                $newtalk = $wgMemc->get( $key );
                                if( is_integer( $newtalk ) ) {
                                        $this->mNewtalk = $newtalk ? 1 : 0;
@@ -756,7 +805,7 @@ class User {
                                        array( 'wl_title'     => $this->getTitleKey(),
                                                   'wl_namespace' => NS_USER_TALK,
                                                   'wl_user'      => $this->mId,
-                                                  'wl_notificationtimestamp != 0' ),
+                                                  'wl_notificationtimestamp ' . $dbr->notNullTimestamp() ),
                                        'User::getNewtalk' );
                                if( $dbr->numRows($res) > 0 ) {
                                        $this->mNewtalk = 1;
@@ -770,7 +819,7 @@ class User {
                                }
                                $dbr->freeResult( $res );
                        } else {
-                               $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName ), $fname );
+                               $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->getName() ), $fname );
                                $this->mNewtalk = $dbr->numRows( $res ) > 0 ? 1 : 0;
                                $dbr->freeResult( $res );
                        }
@@ -987,7 +1036,6 @@ class User {
 
        /**
         * Check if a user is sysop
-        * Die with backtrace. Use User:isAllowed() instead.
         * @deprecated
         */
        function isSysop() {
@@ -1151,7 +1199,7 @@ class User {
                        $dbw =& wfGetDB( DB_MASTER );
                        $success = $dbw->update( 'watchlist',
                                        array( /* SET */
-                                               'wl_notificationtimestamp' => 0
+                                               'wl_notificationtimestamp' => NULL
                                        ), array( /* WHERE */
                                                'wl_title' => $title->getDBkey(),
                                                'wl_namespace' => $title->getNamespace(),
@@ -1226,8 +1274,8 @@ class User {
                $_SESSION['wsUserID'] = $this->mId;
                setcookie( $wgDBname.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
 
-               $_SESSION['wsUserName'] = $this->mName;
-               setcookie( $wgDBname.'UserName', $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
+               $_SESSION['wsUserName'] = $this->getName();
+               setcookie( $wgDBname.'UserName', $this->getName(), $exp, $wgCookiePath, $wgCookieDomain );
 
                $_SESSION['wsToken'] = $this->mToken;
                if ( 1 == $this->getOption( 'rememberpassword' ) ) {
@@ -1242,7 +1290,7 @@ class User {
         * It will clean the session cookie
         */
        function logout() {
-               global $wgCookiePath, $wgCookieDomain, $wgDBname, $wgIP;
+               global $wgCookiePath, $wgCookieDomain, $wgDBname;
                $this->loadDefaults();
                $this->setLoaded( true );
 
@@ -1313,7 +1361,7 @@ class User {
                                if( !$this->mId ) {
                                        # Anon users have a separate memcache space for newtalk
                                        # since they don't store their own info. Trim...
-                                       $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
+                                       $wgMemc->delete( "$wgDBname:newtalk:ip:" . $this->getName() );
                                }
                        }
                } else {
@@ -1323,8 +1371,8 @@ class User {
                                $key = false;
                        } else {
                                $field = 'user_ip';
-                               $value = $this->mName;
-                               $key = "$wgDBname:newtalk:ip:$this->mName";
+                               $value = $this->getName();
+                               $key = "$wgDBname:newtalk:ip:$value";
                        }
 
                        $dbr =& wfGetDB( DB_SLAVE );
@@ -1366,7 +1414,7 @@ class User {
                $fname = 'User::idForName';
 
                $gotid = 0;
-               $s = trim( $this->mName );
+               $s = trim( $this->getName() );
                if ( 0 == strcmp( '', $s ) ) return 0;
 
                $dbr =& wfGetDB( DB_SLAVE );
@@ -1401,7 +1449,6 @@ class User {
        }
 
        function spreadBlock() {
-               global $wgIP;
                # If the (non-anonymous) user is blocked, this function will block any IP address
                # that they successfully log on from.
                $fname = 'User::spreadBlock';
@@ -1417,16 +1464,23 @@ class User {
                }
 
                # Check if this IP address is already blocked
-               $ipblock = Block::newFromDB( $wgIP );
+               $ipblock = Block::newFromDB( wfGetIP() );
                if ( $ipblock->isValid() ) {
+                       # If the user is already blocked. Then check if the autoblock would
+                       # excede the user block. If it would excede, then do nothing, else
+                       # prolong block time
+                       if ($userblock->mExpiry &&
+                               ($userblock->mExpiry < Block::getAutoblockExpiry($ipblock->mTimestamp))) {
+                               return;
+                       }
                        # Just update the timestamp
                        $ipblock->updateTimestamp();
                        return;
                }
 
                # Make a new block object with the desired properties
-               wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
-               $ipblock->mAddress = $wgIP;
+               wfDebug( "Autoblocking {$this->mName}@" . wfGetIP() . "\n" );
+               $ipblock->mAddress = wfGetIP();
                $ipblock->mUser = 0;
                $ipblock->mBy = $userblock->mBy;
                $ipblock->mReason = wfMsg( 'autoblocker', $this->getName(), $userblock->mReason );
@@ -1469,7 +1523,7 @@ class User {
        }
 
        function isAllowedToCreateAccount() {
-               return $this->isAllowed( 'createaccount' );
+               return $this->isAllowed( 'createaccount' ) && !$this->isBlocked();
        }
 
        /**
@@ -1487,7 +1541,7 @@ class User {
         * @access public
         */
        function getUserPage() {
-               return Title::makeTitle( NS_USER, $this->mName );
+               return Title::makeTitle( NS_USER, $this->getName() );
        }
 
        /**
@@ -1506,7 +1560,7 @@ class User {
         */
        function getMaxID() {
                $dbr =& wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'user', 'max(user_id)', false );
+               return $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
        }
 
        /**
@@ -1632,11 +1686,11 @@ class User {
         * @return mixed True on success, a WikiError object on failure.
         */
        function sendConfirmationMail() {
-               global $wgIP, $wgContLang;
+               global $wgContLang;
                $url = $this->confirmationTokenUrl( $expiration );
                return $this->sendMail( wfMsg( 'confirmemail_subject' ),
                        wfMsg( 'confirmemail_body',
-                               $wgIP,
+                               wfGetIP(),
                                $this->getName(),
                                $url,
                                $wgContLang->timeanddate( $expiration, false ) ) );