Make LocalisationCache a service
[lhc/web/wiklou.git] / includes / block / BlockManager.php
index 68141a1..03043e1 100644 (file)
 namespace MediaWiki\Block;
 
 use DateTime;
+use DateTimeZone;
 use DeferredUpdates;
+use Hooks;
 use IP;
 use MediaWiki\Config\ServiceOptions;
+use MediaWiki\Permissions\PermissionManager;
 use MediaWiki\User\UserIdentity;
 use MWCryptHash;
 use User;
@@ -45,12 +48,15 @@ class BlockManager {
        /** @var WebRequest */
        private $currentRequest;
 
+       /** @var PermissionManager */
+       private $permissionManager;
+
        /**
         * TODO Make this a const when HHVM support is dropped (T192166)
         *
         * @var array
         * @since 1.34
-        * */
+        */
        public static $constructorOptions = [
                'ApplyIpBlocksToXff',
                'CookieSetOnAutoblock',
@@ -67,16 +73,19 @@ class BlockManager {
         * @param ServiceOptions $options
         * @param User $currentUser
         * @param WebRequest $currentRequest
+        * @param PermissionManager $permissionManager
         */
        public function __construct(
                ServiceOptions $options,
                User $currentUser,
-               WebRequest $currentRequest
+               WebRequest $currentRequest,
+               PermissionManager $permissionManager
        ) {
                $options->assertRequiredOptions( self::$constructorOptions );
                $this->options = $options;
                $this->currentUser = $currentUser;
                $this->currentRequest = $currentRequest;
+               $this->permissionManager = $permissionManager;
        }
 
        /**
@@ -110,7 +119,8 @@ class BlockManager {
                $globalUserName = $sessionUser->isSafeToLoad()
                        ? $sessionUser->getName()
                        : IP::sanitizeIP( $this->currentRequest->getIP() );
-               if ( $user->getName() === $globalUserName && !$user->isAllowed( 'ipblock-exempt' ) ) {
+               if ( $user->getName() === $globalUserName &&
+                        !$this->permissionManager->userHasRight( $user, 'ipblock-exempt' ) ) {
                        $ip = $this->currentRequest->getIP();
                }
 
@@ -175,6 +185,7 @@ class BlockManager {
                // Filter out any duplicated blocks, e.g. from the cookie
                $blocks = $this->getUniqueBlocks( $blocks );
 
+               $block = null;
                if ( count( $blocks ) > 0 ) {
                        if ( count( $blocks ) === 1 ) {
                                $block = $blocks[ 0 ];
@@ -186,10 +197,11 @@ class BlockManager {
                                        'originalBlocks' => $blocks,
                                ] );
                        }
-                       return $block;
                }
 
-               return null;
+               Hooks::run( 'GetUserBlock', [ clone $user, $ip, &$block ] );
+
+               return $block;
        }
 
        /**
@@ -218,12 +230,12 @@ class BlockManager {
                        }
                }
 
-               return array_merge( $systemBlocks, $databaseBlocks );
+               return array_values( array_merge( $systemBlocks, $databaseBlocks ) );
        }
 
        /**
         * Try to load a block from an ID given in a cookie value. If the block is invalid
-        * or doesn't exist, remove the cookie.
+        * doesn't exist, or the cookie value is malformed, remove the cookie.
         *
         * @param UserIdentity $user
         * @param WebRequest $request
@@ -233,9 +245,13 @@ class BlockManager {
                UserIdentity $user,
                WebRequest $request
        ) {
-               $blockCookieId = $this->getIdFromCookieValue( $request->getCookie( 'BlockID' ) );
+               $cookieValue = $request->getCookie( 'BlockID' );
+               if ( is_null( $cookieValue ) ) {
+                       return false;
+               }
 
-               if ( $blockCookieId !== null ) {
+               $blockCookieId = $this->getIdFromCookieValue( $cookieValue );
+               if ( !is_null( $blockCookieId ) ) {
                        // TODO: remove dependency on DatabaseBlock
                        $block = DatabaseBlock::newFromID( $blockCookieId );
                        if (
@@ -244,9 +260,10 @@ class BlockManager {
                        ) {
                                return $block;
                        }
-                       $this->clearBlockCookie( $request->response() );
                }
 
+               $this->clearBlockCookie( $request->response() );
+
                return false;
        }
 
@@ -293,32 +310,7 @@ class BlockManager {
                        $proxyList = array_map( 'trim', file( $proxyList ) );
                }
 
-               $resultProxyList = [];
-               $deprecatedIPEntries = [];
-
-               // backward compatibility: move all ip addresses in keys to values
-               foreach ( $proxyList as $key => $value ) {
-                       $keyIsIP = IP::isIPAddress( $key );
-                       $valueIsIP = IP::isIPAddress( $value );
-                       if ( $keyIsIP && !$valueIsIP ) {
-                               $deprecatedIPEntries[] = $key;
-                               $resultProxyList[] = $key;
-                       } elseif ( $keyIsIP && $valueIsIP ) {
-                               $deprecatedIPEntries[] = $key;
-                               $resultProxyList[] = $key;
-                               $resultProxyList[] = $value;
-                       } else {
-                               $resultProxyList[] = $value;
-                       }
-               }
-
-               if ( $deprecatedIPEntries ) {
-                       wfDeprecated(
-                               'IP addresses in the keys of $wgProxyList (found the following IP addresses in keys: ' .
-                               implode( ', ', $deprecatedIPEntries ) . ', please move them to values)', '1.30' );
-               }
-
-               $proxyListIPSet = new IPSet( $resultProxyList );
+               $proxyListIPSet = new IPSet( $proxyList );
                return $proxyListIPSet->match( $ip );
        }
 
@@ -460,7 +452,11 @@ class BlockManager {
                }
 
                // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
-               $expiryValue = DateTime::createFromFormat( 'YmdHis', $expiryTime )->format( 'U' );
+               $expiryValue = DateTime::createFromFormat(
+                       'YmdHis',
+                       $expiryTime,
+                       new DateTimeZone( 'UTC' )
+               )->format( 'U' );
                $cookieOptions = [ 'httpOnly' => false ];
                $cookieValue = $this->getCookieValue( $block );
                $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );