namespace MediaWiki\Block;
-use Block;
use IP;
use MediaWiki\User\UserIdentity;
use User;
// we should not look for XFF or cookie blocks.
$request = $user->getRequest();
- # We only need to worry about passing the IP address to the Block generator if the
+ # We only need to worry about passing the IP address to the block generator if the
# 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;
}
// User/IP blocking
- // TODO: remove dependency on Block
- $block = Block::newFromTarget( $user, $ip, !$fromReplica );
+ // TODO: remove dependency on DatabaseBlock
+ $block = DatabaseBlock::newFromTarget( $user, $ip, !$fromReplica );
// Cookie blocking
if ( !$block instanceof AbstractBlock ) {
$xff = $request->getHeader( 'X-Forwarded-For' );
$xff = array_map( 'trim', explode( ',', $xff ) );
$xff = array_diff( $xff, [ $ip ] );
- // TODO: remove dependency on Block
- $xffblocks = Block::getBlocksForIPList( $xff, $isAnon, !$fromReplica );
- // TODO: remove dependency on Block
- $block = Block::chooseBlock( $xffblocks, $xff );
+ // TODO: remove dependency on DatabaseBlock
+ $xffblocks = DatabaseBlock::getBlocksForIPList( $xff, $isAnon, !$fromReplica );
+ // TODO: remove dependency on DatabaseBlock
+ $block = DatabaseBlock::chooseBlock( $xffblocks, $xff );
if ( $block instanceof AbstractBlock ) {
# Mangle the reason to alert the user that the block
# originated from matching the X-Forwarded-For header.
}
/**
- * Try to load a Block from an ID given in a cookie value.
+ * Try to load a block from an ID given in a cookie value.
*
* @param UserIdentity $user
* @param WebRequest $request
- * @return Block|bool The Block object, or false if none could be loaded.
+ * @return DatabaseBlock|bool The block object, or false if none could be loaded.
*/
private function getBlockFromCookieValue(
UserIdentity $user,
if ( strlen( $blockCookieVal ) < 1 || !is_numeric( substr( $blockCookieVal, 0, 1 ) ) ) {
return false;
}
- // Load the Block from the ID in the cookie.
- // TODO: remove dependency on Block
- $blockCookieId = Block::getIdFromCookieValue( $blockCookieVal );
+ // Load the block from the ID in the cookie.
+ // TODO: remove dependency on DatabaseBlock
+ $blockCookieId = DatabaseBlock::getIdFromCookieValue( $blockCookieVal );
if ( $blockCookieId !== null ) {
// An ID was found in the cookie.
- // TODO: remove dependency on Block
- $tmpBlock = Block::newFromID( $blockCookieId );
- if ( $tmpBlock instanceof Block ) {
+ // TODO: remove dependency on DatabaseBlock
+ $tmpBlock = DatabaseBlock::newFromID( $blockCookieId );
+ if ( $tmpBlock instanceof DatabaseBlock ) {
switch ( $tmpBlock->getType() ) {
- case Block::TYPE_USER:
+ case DatabaseBlock::TYPE_USER:
$blockIsValid = !$tmpBlock->isExpired() && $tmpBlock->isAutoblocking();
$useBlockCookie = ( $this->cookieSetOnAutoblock === true );
break;
- case Block::TYPE_IP:
- case Block::TYPE_RANGE:
+ case DatabaseBlock::TYPE_IP:
+ case DatabaseBlock::TYPE_RANGE:
// If block is type IP or IP range, load only if user is not logged in (T152462)
$blockIsValid = !$tmpBlock->isExpired() && $user->getId() === 0;
$useBlockCookie = ( $this->cookieSetOnIpBlock === true );
}
// If the block is not valid, remove the cookie.
- // TODO: remove dependency on Block
- Block::clearCookie( $response );
+ // TODO: remove dependency on DatabaseBlock
+ DatabaseBlock::clearCookie( $response );
} else {
// If the block doesn't exist, remove the cookie.
- // TODO: remove dependency on Block
- Block::clearCookie( $response );
+ // TODO: remove dependency on DatabaseBlock
+ DatabaseBlock::clearCookie( $response );
}
}
return false;
if ( is_array( $base ) ) {
if ( count( $base ) >= 2 ) {
// Access key is 1, base URL is 0
- $host = "{$base[1]}.$ipReversed.{$base[0]}";
+ $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
} else {
- $host = "$ipReversed.{$base[0]}";
+ $hostname = "$ipReversed.{$base[0]}";
}
$basename = $base[0];
} else {
- $host = "$ipReversed.$base";
+ $hostname = "$ipReversed.$base";
}
// Send query
- $ipList = gethostbynamel( $host );
+ $ipList = $this->checkHost( $hostname );
if ( $ipList ) {
- wfDebugLog( 'dnsblacklist', "Hostname $host is {$ipList[0]}, it's a proxy says $basename!" );
+ wfDebugLog(
+ 'dnsblacklist',
+ "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
+ );
$found = true;
break;
}
- wfDebugLog( 'dnsblacklist', "Requested $host, not found in $basename." );
+ wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
}
}
return $found;
}
+ /**
+ * Wrapper for mocking in tests.
+ *
+ * @param string $hostname DNSBL query
+ * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
+ */
+ protected function checkHost( $hostname ) {
+ return gethostbynamel( $hostname );
+ }
+
}