Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordfenc.../vendor/wordfenc.../mmdb-rea.../src
File: Database.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Wordfence\MmdbReader;
[2] Fix | Delete
[3] Fix | Delete
use Wordfence\MmdbReader\Exception\FormatException;
[4] Fix | Delete
use Wordfence\MmdbReader\Exception\IncompatibleIpVersionException;
[5] Fix | Delete
use Wordfence\MmdbReader\Exception\IncompatibleVersionException;
[6] Fix | Delete
use Wordfence\MmdbReader\Exception\InvalidIpAddressException;
[7] Fix | Delete
use Wordfence\MmdbReader\Exception\IoException;
[8] Fix | Delete
use Wordfence\MmdbReader\Io\FileHandle;
[9] Fix | Delete
[10] Fix | Delete
class Database {
[11] Fix | Delete
[12] Fix | Delete
const SUPPORTED_MAJOR_VERSION = 2;
[13] Fix | Delete
const DELIMITER_METADATA = "\xab\xcd\xefMaxMind.com";
[14] Fix | Delete
[15] Fix | Delete
private $handle;
[16] Fix | Delete
private $metadata;
[17] Fix | Delete
private $nodeSize;
[18] Fix | Delete
private $nodeReader;
[19] Fix | Delete
private $dataSectionParser;
[20] Fix | Delete
private $startingNodes = array();
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Bind to a MaxMind database using the provided handle
[24] Fix | Delete
* @param resource $resource a valid stream resource that can be used to read the database
[25] Fix | Delete
* @param bool $closeAutomtically if true, the provided resource will be closed automatically
[26] Fix | Delete
* @throws MmdbThrowable if an error occurs while interacting with the database
[27] Fix | Delete
*/
[28] Fix | Delete
protected function __construct($resource, $closeAutomatically) {
[29] Fix | Delete
$this->handle = new FileHandle($resource, $closeAutomatically);
[30] Fix | Delete
$this->initialize();
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
private function initialize() {
[34] Fix | Delete
$this->loadMetadata();
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
private function loadMetadata() {
[38] Fix | Delete
$this->handle->seek(0, SEEK_END);
[39] Fix | Delete
$position = $this->handle->locateString(self::DELIMITER_METADATA, FileHandle::DIRECTION_REVERSE, DatabaseMetadata::MAX_LENGTH, true);
[40] Fix | Delete
if ($position === null)
[41] Fix | Delete
throw new FormatException("Unable to locate metadata in MMDB file");
[42] Fix | Delete
$this->metadata = DatabaseMetadata::parse($this->handle);
[43] Fix | Delete
if ($this->metadata->getMajorVersion() !== self::SUPPORTED_MAJOR_VERSION)
[44] Fix | Delete
throw new IncompatibleVersionException(sprintf('This library only supports parsing version %d of the MMDB format, a version %d database was provided', self::SUPPORTED_MAJOR_VERSION, $this->metadata->getMajorVersion()));
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
private function computeNodeSize() {
[48] Fix | Delete
$nodeSize = ($this->metadata->getRecordSize() * 2) / 8;
[49] Fix | Delete
if (!is_int($nodeSize))
[50] Fix | Delete
throw new FormatException("Node size must be an even number of bytes, computed {$this->nodeSize}");
[51] Fix | Delete
return $nodeSize;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
private function getNodeReader() {
[55] Fix | Delete
if ($this->nodeReader === null)
[56] Fix | Delete
$this->nodeReader = new NodeReader($this->handle, $this->computeNodeSize(), $this->metadata->getNodeCount());
[57] Fix | Delete
return $this->nodeReader;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
private function getDataSectionParser() {
[61] Fix | Delete
if ($this->dataSectionParser === null) {
[62] Fix | Delete
$offset = $this->getNodeReader()->getSearchTreeSectionSize() + 16; //16 null bytes separate the two sections
[63] Fix | Delete
$this->dataSectionParser = new DataFieldParser($this->handle, $offset);
[64] Fix | Delete
}
[65] Fix | Delete
return $this->dataSectionParser;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Retrieve the metadata for this database
[70] Fix | Delete
* @return DatabaseMetadata
[71] Fix | Delete
*/
[72] Fix | Delete
public function getMetadata() {
[73] Fix | Delete
return $this->metadata;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Search the database for the given IP address
[78] Fix | Delete
* @param IpAddressInterface|string $ip the IP address for which to search
[79] Fix | Delete
* A human readable (as accepted by inet_pton) or binary (as accepted by inet_ntop) string may be provided or an instance of IpAddressInterface
[80] Fix | Delete
* @return array|null the matched record or null if no record was found
[81] Fix | Delete
* @throws InvalidIpAddressException if $ip is a string that cannot be parsed as a valid IP address
[82] Fix | Delete
* @throws IncompatibleVersionException if the database IP version and the version of the provided IP address are incompatible (specifically, if an IPv6 address is passed and the database only supports IPv4)
[83] Fix | Delete
*/
[84] Fix | Delete
public function search($ip) {
[85] Fix | Delete
if (is_string($ip)) {
[86] Fix | Delete
$ip = IpAddress::createFromString($ip);
[87] Fix | Delete
}
[88] Fix | Delete
elseif (!$ip instanceof IpAddressInterface) {
[89] Fix | Delete
throw new InvalidIpAddressException('IP address must be either a human readable string (presentation format), a binary string (network format), or an instance of Wordfence\MmdbReader\IpAddressInterface, received: ' . print_r($ip, true));
[90] Fix | Delete
}
[91] Fix | Delete
if ($this->metadata->getIpVersion() === IpAddress::TYPE_IPV4 && $ip->getType() === IpAddress::TYPE_IPV6)
[92] Fix | Delete
throw new IncompatibleIpVersionException('This database only support IPv4 addresses, but the provided address is an IPv6 address');
[93] Fix | Delete
return $this->searchNodes($ip);
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
private function resolveStartingNode($type) {
[97] Fix | Delete
$node = $this->getNodeReader()->read(0);
[98] Fix | Delete
if ($type === IpAddress::TYPE_IPV4 && $this->metadata->getIpVersion() === IpAddress::TYPE_IPV6) {
[99] Fix | Delete
$skippedBits = (IpAddress::LENGTH_IPV6 - IpAddress::LENGTH_IPV4) * 8;
[100] Fix | Delete
while ($skippedBits-- > 0) {
[101] Fix | Delete
$record = $node->getLeft();
[102] Fix | Delete
if ($record->isNodePointer()) {
[103] Fix | Delete
$node = $record->getNextNode();
[104] Fix | Delete
}
[105] Fix | Delete
else {
[106] Fix | Delete
return $record;
[107] Fix | Delete
}
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
return $node;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
private function getStartingNode($type) {
[114] Fix | Delete
if (!array_key_exists($type, $this->startingNodes)) {
[115] Fix | Delete
$this->startingNodes[$type] = $this->resolveStartingNode($type);
[116] Fix | Delete
}
[117] Fix | Delete
return $this->startingNodes[$type];
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
private function searchNodes($ip) {
[121] Fix | Delete
$key = $ip->getBinary();
[122] Fix | Delete
$byteCount = strlen($key);
[123] Fix | Delete
$nodeReader = $this->getNodeReader();
[124] Fix | Delete
$node = $this->getStartingNode($ip->getType());
[125] Fix | Delete
$bits = '';
[126] Fix | Delete
$record = null;
[127] Fix | Delete
if ($node instanceof Node) {
[128] Fix | Delete
for ($byteIndex = 0; $byteIndex < $byteCount; $byteIndex++) {
[129] Fix | Delete
$byte = ord($key[$byteIndex]);
[130] Fix | Delete
for ($bitOffset = 7; $bitOffset >= 0; $bitOffset--) {
[131] Fix | Delete
$bit = ($byte >> $bitOffset) & 1;
[132] Fix | Delete
$record = $node->getRecord($bit);
[133] Fix | Delete
if ($record->isNodePointer()) {
[134] Fix | Delete
$node = $record->getNextNode();
[135] Fix | Delete
}
[136] Fix | Delete
else {
[137] Fix | Delete
break 2;
[138] Fix | Delete
}
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
}
[142] Fix | Delete
else {
[143] Fix | Delete
$record = $node;
[144] Fix | Delete
}
[145] Fix | Delete
if ($record->isNullPointer()) {
[146] Fix | Delete
return null;
[147] Fix | Delete
}
[148] Fix | Delete
elseif ($record->isDataPointer()) {
[149] Fix | Delete
$this->handle->seek($record->getDataAddress(), SEEK_SET);
[150] Fix | Delete
$data = $this->getDataSectionParser()->parseField();
[151] Fix | Delete
return $data;
[152] Fix | Delete
}
[153] Fix | Delete
else {
[154] Fix | Delete
return null;
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Open the MMDB file at the given path
[160] Fix | Delete
* @param string $path the path of an MMDB file
[161] Fix | Delete
* @throws IoException if unable to open the file at the provided path
[162] Fix | Delete
* @throws MmdbThrowable if an error occurs while initializing the database
[163] Fix | Delete
*/
[164] Fix | Delete
public static function open($path) {
[165] Fix | Delete
$handle = fopen($path, 'rb');
[166] Fix | Delete
if ($handle === false)
[167] Fix | Delete
throw new IoException("Unable to open MMDB file at {$path}");
[168] Fix | Delete
return new self($handle, true);
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
}
[172] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function