Merge "Replace misplaced strlen() checks with strict `false` comparisons"
[lhc/web/wiklou.git] / includes / user / User.php
index 277731a..3fcba46 100644 (file)
@@ -1403,27 +1403,10 @@ class User implements IDBAccessObject, UserIdentity {
         */
        public function trackBlockWithCookie() {
                $block = $this->getBlock();
-               if ( $block && $this->getRequest()->getCookie( 'BlockID' ) === null ) {
-                       $config = RequestContext::getMain()->getConfig();
-                       $shouldSetCookie = false;
-
-                       if ( $this->isAnon() && $config->get( 'CookieSetOnIpBlock' ) ) {
-                               // If user is logged-out, set a cookie to track the Block
-                               $shouldSetCookie = in_array( $block->getType(), [
-                                       Block::TYPE_IP, Block::TYPE_RANGE
-                               ] );
-                               if ( $shouldSetCookie ) {
-                                       $block->setCookie( $this->getRequest()->response() );
 
-                                       // temporary measure the use of cookies on ip blocks
-                                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-                                       $stats->increment( 'block.ipblock.setCookie.success' );
-                               }
-                       } elseif ( $this->isLoggedIn() && $config->get( 'CookieSetOnAutoblock' ) ) {
-                               $shouldSetCookie = $block->getType() === Block::TYPE_USER && $block->isAutoblocking();
-                               if ( $shouldSetCookie ) {
-                                       $block->setCookie( $this->getRequest()->response() );
-                               }
+               if ( $block && $this->getRequest()->getCookie( 'BlockID' ) === null ) {
+                       if ( $block->shouldTrackWithCookie( $this->isAnon() ) ) {
+                               $block->setCookie( $this->getRequest()->response() );
                        }
                }
        }
@@ -1824,12 +1807,12 @@ class User implements IDBAccessObject, UserIdentity {
 
        /**
         * Get blocking information
-        * @param bool $bFromReplica Whether to check the replica DB first.
+        * @param bool $fromReplica Whether to check the replica DB first.
         *   To improve performance, non-critical checks are done against replica DBs.
         *   Check when actually saving should be done against master.
         */
-       private function getBlockedStatus( $bFromReplica = true ) {
-               global $wgProxyWhitelist, $wgUser, $wgApplyIpBlocksToXff, $wgSoftBlockRanges;
+       private function getBlockedStatus( $fromReplica = true ) {
+               global $wgProxyWhitelist, $wgApplyIpBlocksToXff, $wgSoftBlockRanges;
 
                if ( $this->mBlockedby != -1 ) {
                        return;
@@ -1848,19 +1831,18 @@ class User implements IDBAccessObject, UserIdentity {
                # user is not immune to autoblocks/hardblocks, and they are the current user so we
                # know which IP address they're actually coming from
                $ip = null;
-               if ( !$this->isAllowed( 'ipblock-exempt' ) ) {
-                       // $wgUser->getName() only works after the end of Setup.php. Until
-                       // then, assume it's a logged-out user.
-                       $globalUserName = $wgUser->isSafeToLoad()
-                               ? $wgUser->getName()
-                               : IP::sanitizeIP( $wgUser->getRequest()->getIP() );
-                       if ( $this->getName() === $globalUserName ) {
-                               $ip = $this->getRequest()->getIP();
-                       }
+               $sessionUser = RequestContext::getMain()->getUser();
+               // the session user is set up towards the end of Setup.php. Until then,
+               // assume it's a logged-out user.
+               $globalUserName = $sessionUser->isSafeToLoad()
+                       ? $sessionUser->getName()
+                       : IP::sanitizeIP( $sessionUser->getRequest()->getIP() );
+               if ( $this->getName() === $globalUserName && !$this->isAllowed( 'ipblock-exempt' ) ) {
+                       $ip = $this->getRequest()->getIP();
                }
 
                // User/IP blocking
-               $block = Block::newFromTarget( $this, $ip, !$bFromReplica );
+               $block = Block::newFromTarget( $this, $ip, !$fromReplica );
 
                // Cookie blocking
                if ( !$block instanceof Block ) {
@@ -1896,12 +1878,12 @@ class User implements IDBAccessObject, UserIdentity {
                        $xff = $this->getRequest()->getHeader( 'X-Forwarded-For' );
                        $xff = array_map( 'trim', explode( ',', $xff ) );
                        $xff = array_diff( $xff, [ $ip ] );
-                       $xffblocks = Block::getBlocksForIPList( $xff, $this->isAnon(), !$bFromReplica );
+                       $xffblocks = Block::getBlocksForIPList( $xff, $this->isAnon(), !$fromReplica );
                        $block = Block::chooseBlock( $xffblocks, $xff );
                        if ( $block instanceof Block ) {
                                # Mangle the reason to alert the user that the block
                                # originated from matching the X-Forwarded-For header.
-                               $block->mReason = wfMessage( 'xffblockreason', $block->mReason )->plain();
+                               $block->setReason( wfMessage( 'xffblockreason', $block->getReason() )->plain() );
                        }
                }
 
@@ -1923,8 +1905,8 @@ class User implements IDBAccessObject, UserIdentity {
                        wfDebug( __METHOD__ . ": Found block.\n" );
                        $this->mBlock = $block;
                        $this->mBlockedby = $block->getByName();
-                       $this->mBlockreason = $block->mReason;
-                       $this->mHideName = $block->mHideName;
+                       $this->mBlockreason = $block->getReason();
+                       $this->mHideName = $block->getHideName();
                        $this->mAllowUsertalk = $block->isUsertalkEditAllowed();
                } else {
                        $this->mBlock = null;
@@ -1935,9 +1917,9 @@ class User implements IDBAccessObject, UserIdentity {
                }
 
                // Avoid PHP 7.1 warning of passing $this by reference
-               $user = $this;
+               $thisUser = $this;
                // Extensions
-               Hooks::run( 'GetBlockedStatus', [ &$user ] );
+               Hooks::run( 'GetBlockedStatus', [ &$thisUser ] );
        }
 
        /**
@@ -2268,23 +2250,23 @@ class User implements IDBAccessObject, UserIdentity {
        /**
         * Check if user is blocked
         *
-        * @param bool $bFromReplica Whether to check the replica DB instead of
+        * @param bool $fromReplica Whether to check the replica DB instead of
         *   the master. Hacked from false due to horrible probs on site.
         * @return bool True if blocked, false otherwise
         */
-       public function isBlocked( $bFromReplica = true ) {
-               return $this->getBlock( $bFromReplica ) instanceof Block &&
+       public function isBlocked( $fromReplica = true ) {
+               return $this->getBlock( $fromReplica ) instanceof Block &&
                        $this->getBlock()->appliesToRight( 'edit' );
        }
 
        /**
         * Get the block affecting the user, or null if the user is not blocked
         *
-        * @param bool $bFromReplica Whether to check the replica DB instead of the master
+        * @param bool $fromReplica Whether to check the replica DB instead of the master
         * @return Block|null
         */
-       public function getBlock( $bFromReplica = true ) {
-               $this->getBlockedStatus( $bFromReplica );
+       public function getBlock( $fromReplica = true ) {
+               $this->getBlockedStatus( $fromReplica );
                return $this->mBlock instanceof Block ? $this->mBlock : null;
        }
 
@@ -2762,17 +2744,16 @@ class User implements IDBAccessObject, UserIdentity {
        /**
         * Generate a current or new-future timestamp to be stored in the
         * user_touched field when we update things.
+        *
         * @return string Timestamp in TS_MW format
         */
        private function newTouchedTimestamp() {
-               global $wgClockSkewFudge;
-
-               $time = wfTimestamp( TS_MW, time() + $wgClockSkewFudge );
-               if ( $this->mTouched && $time <= $this->mTouched ) {
-                       $time = wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $this->mTouched ) + 1 );
+               $time = time();
+               if ( $this->mTouched ) {
+                       $time = max( $time, wfTimestamp( TS_UNIX, $this->mTouched ) + 1 );
                }
 
-               return $time;
+               return wfTimestamp( TS_MW, $time );
        }
 
        /**