MediaWiki  master
MediaWiki\Block\BlockManager Class Reference

A service class for checking blocks. More...

Collaboration diagram for MediaWiki\Block\BlockManager:

Public Member Functions

 __construct (ServiceOptions $options, User $currentUser, WebRequest $currentRequest)
 
 getCookieValue (DatabaseBlock $block)
 Get the BlockID cookie's value for this block. More...
 
 getIdFromCookieValue ( $cookieValue)
 Get the stored ID from the 'BlockID' cookie. More...
 
 getUserBlock (User $user, $fromReplica)
 Get the blocks that apply to a user. More...
 
 isDnsBlacklisted ( $ip, $checkWhitelist=false)
 Whether the given IP is in a DNS blacklist. More...
 
 setBlockCookie (DatabaseBlock $block, WebResponse $response)
 Set the 'BlockID' cookie to this block's ID and expiry time. More...
 
 trackBlockWithCookie (User $user)
 Set the 'BlockID' cookie depending on block type and user authentication status. More...
 

Static Public Member Functions

static clearBlockCookie (WebResponse $response)
 Unset the 'BlockID' cookie. More...
 

Static Public Attributes

static array $constructorOptions
 TODO Make this a const when HHVM support is dropped (T192166) More...
 

Protected Member Functions

 checkHost ( $hostname)
 Wrapper for mocking in tests. More...
 

Private Member Functions

 getBlockFromCookieValue (UserIdentity $user, WebRequest $request)
 Try to load a block from an ID given in a cookie value. More...
 
 getUniqueBlocks (array $blocks)
 Given a list of blocks, return a list of unique blocks. More...
 
 inDnsBlacklist ( $ip, array $bases)
 Whether the given IP is in a given DNS blacklist. More...
 
 isLocallyBlockedProxy ( $ip)
 Check if an IP address is in the local proxy list. More...
 
 shouldApplyCookieBlock (DatabaseBlock $block, $isAnon)
 Check if the block loaded from the cookie should be applied. More...
 
 shouldTrackBlockWithCookie (AbstractBlock $block, $isAnon)
 Check if the block should be tracked with a cookie. More...
 

Private Attributes

WebRequest $currentRequest
 
User $currentUser
 

Detailed Description

A service class for checking blocks.

To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().

Since
1.34 Refactored from User and Block.

Definition at line 40 of file BlockManager.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Block\BlockManager::__construct ( ServiceOptions  $options,
User  $currentUser,
WebRequest  $currentRequest 
)

Member Function Documentation

◆ checkHost()

MediaWiki\Block\BlockManager::checkHost (   $hostname)
protected

Wrapper for mocking in tests.

Parameters
string$hostnameDNSBL query
Returns
string[]|bool IPv4 array, or false if the IP is not blacklisted

Definition at line 397 of file BlockManager.php.

Referenced by MediaWiki\Block\BlockManager\inDnsBlacklist().

◆ clearBlockCookie()

static MediaWiki\Block\BlockManager::clearBlockCookie ( WebResponse  $response)
static

Unset the 'BlockID' cookie.

Since
1.34
Parameters
WebResponse$response

Definition at line 499 of file BlockManager.php.

References WebResponse\clearCookie().

Referenced by MediaWiki\Block\BlockManager\getBlockFromCookieValue().

◆ getBlockFromCookieValue()

MediaWiki\Block\BlockManager::getBlockFromCookieValue ( UserIdentity  $user,
WebRequest  $request 
)
private

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.

Parameters
UserIdentity$user
WebRequest$request
Returns
DatabaseBlock|bool The block object, or false if none could be loaded.

Definition at line 232 of file BlockManager.php.

References MediaWiki\Block\BlockManager\clearBlockCookie(), WebRequest\getCookie(), MediaWiki\Block\BlockManager\getIdFromCookieValue(), MediaWiki\Block\DatabaseBlock\newFromID(), null, WebRequest\response(), and MediaWiki\Block\BlockManager\shouldApplyCookieBlock().

Referenced by MediaWiki\Block\BlockManager\getUserBlock().

◆ getCookieValue()

MediaWiki\Block\BlockManager::getCookieValue ( DatabaseBlock  $block)

Get the BlockID cookie's value for this block.

This is usually the block ID concatenated with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just be the block ID.

Since
1.34

Definition at line 546 of file BlockManager.php.

References false, MediaWiki\Block\DatabaseBlock\getId(), MWCryptHash\hmac(), and options().

Referenced by MediaWiki\Block\BlockManager\setBlockCookie().

◆ getIdFromCookieValue()

MediaWiki\Block\BlockManager::getIdFromCookieValue (   $cookieValue)

Get the stored ID from the 'BlockID' cookie.

The cookie's value is usually a combination of the ID and a HMAC (see DatabaseBlock::setCookie), but will sometimes only be the ID.

Since
1.34

Definition at line 513 of file BlockManager.php.

References false, MWCryptHash\hmac(), null, and options().

Referenced by MediaWiki\Block\BlockManager\getBlockFromCookieValue().

◆ getUniqueBlocks()

MediaWiki\Block\BlockManager::getUniqueBlocks ( array  $blocks)
private

Given a list of blocks, return a list of unique blocks.

This usually means that each block has a unique ID. For a block with ID null, if it's an autoblock, it will be filtered out if the parent block is present; if not, it is assumed to be a unique system block, and kept.

Parameters
AbstractBlock[]$blocks
Returns
AbstractBlock[]

Definition at line 205 of file BlockManager.php.

References as, and MediaWiki\Block\AbstractBlock\TYPE_AUTO.

Referenced by MediaWiki\Block\BlockManager\getUserBlock().

◆ getUserBlock()

MediaWiki\Block\BlockManager::getUserBlock ( User  $user,
  $fromReplica 
)

◆ inDnsBlacklist()

MediaWiki\Block\BlockManager::inDnsBlacklist (   $ip,
array  $bases 
)
private

Whether the given IP is in a given DNS blacklist.

Parameters
string$ipIP to check
array$basesArray of Strings: URL of the DNS blacklist
Returns
bool True if blacklisted.

Definition at line 349 of file BlockManager.php.

References $base, as, MediaWiki\Block\BlockManager\checkHost(), IP\isIPv4(), and wfDebugLog().

Referenced by MediaWiki\Block\BlockManager\isDnsBlacklisted().

◆ isDnsBlacklisted()

MediaWiki\Block\BlockManager::isDnsBlacklisted (   $ip,
  $checkWhitelist = false 
)

Whether the given IP is in a DNS blacklist.

Parameters
string$ipIP to check
bool$checkWhitelistWhether to check the whitelist first
Returns
bool True if blacklisted.

Definition at line 332 of file BlockManager.php.

References MediaWiki\Block\BlockManager\inDnsBlacklist(), and options().

Referenced by MediaWiki\Block\BlockManager\getUserBlock().

◆ isLocallyBlockedProxy()

MediaWiki\Block\BlockManager::isLocallyBlockedProxy (   $ip)
private

Check if an IP address is in the local proxy list.

Parameters
string$ip
Returns
bool

Definition at line 285 of file BlockManager.php.

References $value, as, file, IP\isIPAddress(), options(), and wfDeprecated().

Referenced by MediaWiki\Block\BlockManager\getUserBlock().

◆ setBlockCookie()

MediaWiki\Block\BlockManager::setBlockCookie ( DatabaseBlock  $block,
WebResponse  $response 
)

Set the 'BlockID' cookie to this block's ID and expiry time.

The cookie's expiry will be the same as the block's, to a maximum of 24 hours.

Since
1.34

Definition at line 452 of file BlockManager.php.

References $expiryTime, false, MediaWiki\Block\BlockManager\getCookieValue(), MediaWiki\Block\AbstractBlock\getExpiry(), WebResponse\setCookie(), and wfTimestamp().

Referenced by MediaWiki\Block\BlockManager\trackBlockWithCookie().

◆ shouldApplyCookieBlock()

MediaWiki\Block\BlockManager::shouldApplyCookieBlock ( DatabaseBlock  $block,
  $isAnon 
)
private

Check if the block loaded from the cookie should be applied.

Parameters
DatabaseBlock$block
bool$isAnonThe user is logged out
Returns
bool The block sould be applied

Definition at line 260 of file BlockManager.php.

References MediaWiki\Block\DatabaseBlock\getType(), MediaWiki\Block\DatabaseBlock\isAutoblocking(), MediaWiki\Block\DatabaseBlock\isExpired(), options(), MediaWiki\Block\AbstractBlock\TYPE_IP, MediaWiki\Block\AbstractBlock\TYPE_RANGE, and MediaWiki\Block\AbstractBlock\TYPE_USER.

Referenced by MediaWiki\Block\BlockManager\getBlockFromCookieValue().

◆ shouldTrackBlockWithCookie()

MediaWiki\Block\BlockManager::shouldTrackBlockWithCookie ( AbstractBlock  $block,
  $isAnon 
)
private

Check if the block should be tracked with a cookie.

Parameters
AbstractBlock$block
bool$isAnonThe user is logged out
Returns
bool The block sould be tracked with a cookie

Definition at line 476 of file BlockManager.php.

References MediaWiki\Block\AbstractBlock\getType(), options(), MediaWiki\Block\AbstractBlock\TYPE_IP, MediaWiki\Block\AbstractBlock\TYPE_RANGE, and MediaWiki\Block\AbstractBlock\TYPE_USER.

Referenced by MediaWiki\Block\BlockManager\trackBlockWithCookie().

◆ trackBlockWithCookie()

MediaWiki\Block\BlockManager::trackBlockWithCookie ( User  $user)

Set the 'BlockID' cookie depending on block type and user authentication status.

Since
1.34
Parameters
User$user

Definition at line 407 of file BlockManager.php.

References $request, $response, DeferredUpdates\addCallableUpdate(), as, User\getBlock(), User\getRequest(), User\isAnon(), null, DeferredUpdates\PRESEND, MediaWiki\Block\BlockManager\setBlockCookie(), MediaWiki\Block\BlockManager\shouldTrackBlockWithCookie(), and use.

Member Data Documentation

◆ $constructorOptions

array MediaWiki\Block\BlockManager::$constructorOptions
static
Initial value:
= [
'ApplyIpBlocksToXff',
'CookieSetOnAutoblock',
'CookieSetOnIpBlock',
'DnsBlacklistUrls',
'EnableDnsBlacklist',
'ProxyList',
'ProxyWhitelist',
'SecretKey',
'SoftBlockRanges',
]

TODO Make this a const when HHVM support is dropped (T192166)

Since
1.34

Definition at line 54 of file BlockManager.php.

◆ $currentRequest

WebRequest MediaWiki\Block\BlockManager::$currentRequest
private

Definition at line 46 of file BlockManager.php.

Referenced by MediaWiki\Block\BlockManager\__construct().

◆ $currentUser

User MediaWiki\Block\BlockManager::$currentUser
private

The documentation for this class was generated from the following file: