Merged in feature/280-dev-dev01 (pull request #21)
auto-patch 280-dev-dev01-2024-01-19T16_41_58 * auto-patch 280-dev-dev01-2024-01-19T16_41_58
This commit is contained in:
848
wp/wp-content/ip-geo-api/ip2location/IP2Location.php
Normal file
848
wp/wp-content/ip-geo-api/ip2location/IP2Location.php
Normal file
@@ -0,0 +1,848 @@
|
||||
<?php
|
||||
/**
|
||||
* This is the modified version for IP Geo Block
|
||||
* based on IP2Location PHP Module Version: 7.0.0
|
||||
* from https://www.ip2location.com/developers/php
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright (C) 2005-2015 IP2Location.com
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation, either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
class IP2LocationRecord {
|
||||
public $ipAddress;
|
||||
public $ipNumber;
|
||||
public $countryCode;
|
||||
public $countryName;
|
||||
public $regionName;
|
||||
public $cityName;
|
||||
public $latitude;
|
||||
public $longitude;
|
||||
public $isp;
|
||||
public $domainName;
|
||||
public $zipCode;
|
||||
public $timeZone;
|
||||
public $netSpeed;
|
||||
public $iddCode;
|
||||
public $areaCode;
|
||||
public $weatherStationCode;
|
||||
public $weatherStationName;
|
||||
public $mcc;
|
||||
public $mnc;
|
||||
public $mobileCarrierName;
|
||||
public $elevation;
|
||||
public $usageType;
|
||||
}
|
||||
|
||||
class IP2Location {
|
||||
// Current version.
|
||||
const VERSION = '7.0.0';
|
||||
|
||||
// Database storage method.
|
||||
const FILE_IO = 0;
|
||||
const MEMORY_CACHE = 1;
|
||||
const SHARED_MEMORY = 2;
|
||||
|
||||
// Unpack method.
|
||||
const ENDIAN = 0;
|
||||
const BIG_ENDIAN = 1;
|
||||
|
||||
// Record field.
|
||||
const ALL = 100;
|
||||
const COUNTRY_CODE = 1;
|
||||
const COUNTRY_NAME = 2;
|
||||
const REGION_NAME = 3;
|
||||
const CITY_NAME = 4;
|
||||
const LATITUDE = 5;
|
||||
const LONGITUDE = 6;
|
||||
const ISP = 7;
|
||||
const DOMAIN_NAME = 8;
|
||||
const ZIP_CODE = 9;
|
||||
const TIME_ZONE = 10;
|
||||
const NET_SPEED = 11;
|
||||
const IDD_CODE = 12;
|
||||
const AREA_CODE = 13;
|
||||
const WEATHER_STATION_CODE = 14;
|
||||
const WEATHER_STATION_NAME = 15;
|
||||
const MCC = 16;
|
||||
const MNC = 17;
|
||||
const MOBILE_CARRIER_NAME = 18;
|
||||
const ELEVATION = 19;
|
||||
const USAGE_TYPE = 20;
|
||||
|
||||
// IP version.
|
||||
const IPV4 = 0;
|
||||
const IPV6 = 1;
|
||||
|
||||
// SHMOP memory address.
|
||||
const SHM_KEY = 4194500608;
|
||||
|
||||
// Message.
|
||||
const FIELD_NOT_SUPPORTED = 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.';
|
||||
const INVALID_IP_ADDRESS = 'Invalid IP address.';
|
||||
|
||||
private $columns = array(
|
||||
'COUNTRY_CODE' => array(
|
||||
0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
),
|
||||
'COUNTRY_NAME' => array(
|
||||
0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
),
|
||||
'REGION_NAME' => array(
|
||||
0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
||||
),
|
||||
'CITY_NAME' => array(
|
||||
0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
|
||||
),
|
||||
'LATITUDE' => array(
|
||||
0,0,0,0,0,5,5,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
),
|
||||
'LONGITUDE' => array(
|
||||
0,0,0,0,0,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
),
|
||||
'ISP' => array(
|
||||
0,0,3,0,5,0,7,5,7,0,8,0,9,0,9,0,9,0,9,7,9,0,9,7,9,
|
||||
),
|
||||
'DOMAIN_NAME' => array(
|
||||
0,0,0,0,0,0,0,6,8,0,9,0,10,0,10,0,10,0,10,8,10,0,10,8,10,
|
||||
),
|
||||
'ZIP_CODE' => array(
|
||||
0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,0,7,0,7,7,7,0,7,
|
||||
),
|
||||
'TIME_ZONE' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,8,8,7,8,8,8,7,8,0,8,8,8,0,8,
|
||||
),
|
||||
'NET_SPEED' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,8,11,0,11,8,11,0,11,0,11,0,11,
|
||||
),
|
||||
'IDD_CODE' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,12,0,12,0,12,9,12,0,12,
|
||||
),
|
||||
'AREA_CODE' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,13,0,13,0,13,10,13,0,13,
|
||||
),
|
||||
'WEATHER_STATION_CODE' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,14,0,14,0,14,0,14,
|
||||
),
|
||||
'WEATHER_STATION_NAME' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,15,0,15,0,15,0,15,
|
||||
),
|
||||
'MCC' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,16,0,16,9,16,
|
||||
),
|
||||
'MNC' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,17,0,17,10,17,
|
||||
),
|
||||
'MOBILE_CARRIER_NAME' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,18,0,18,11,18,
|
||||
),
|
||||
'ELEVATION' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,19,0,19,
|
||||
),
|
||||
'USAGE_TYPE' => array(
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,20,
|
||||
),
|
||||
);
|
||||
|
||||
private $shmId = '';
|
||||
private $database = array();
|
||||
private $unpackMethod;
|
||||
private $buffer;
|
||||
private $mode;
|
||||
private $resource;
|
||||
private $result;
|
||||
|
||||
/**
|
||||
* Added for IP Geo Block
|
||||
*/
|
||||
public function get_database_type() {
|
||||
return ( $this->database['ipv4_count'] ? IP_GEO_BLOCK_API_TYPE_IPV4 : 0 ) |
|
||||
( $this->database['ipv6_count'] ? IP_GEO_BLOCK_API_TYPE_IPV6 : 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct($file = NULL, $mode = self::FILE_IO) {
|
||||
if (!file_exists($file)) {
|
||||
throw new Exception('IP2Location.class.php: Unable to open file "' . $file . '".');
|
||||
}
|
||||
|
||||
// Define system unpack method.
|
||||
list($test) = array_values(unpack('L1L', pack('V', 1)));
|
||||
|
||||
// Use Big Endian Unpack if endian test failed.
|
||||
$this->unpackMethod = (($test != 1)) ? self::BIG_ENDIAN : self::ENDIAN;
|
||||
|
||||
switch ($mode) {
|
||||
case self::SHARED_MEMORY:
|
||||
if (!function_exists('shmop_open')) {
|
||||
throw new Exception('IP2Location.class.php: Please make sure your PHP setup has "php_shmop" enabled.');
|
||||
}
|
||||
|
||||
$this->mode = self::SHARED_MEMORY;
|
||||
|
||||
$this->shmId = @shmop_open(self::SHM_KEY, 'a', 0, 0);
|
||||
|
||||
if ($this->shmId === FALSE) {
|
||||
// First execution, load database into memory.
|
||||
if (($fp = fopen($file, 'rb')) === FALSE) {
|
||||
throw new Exception('IP2Location.class.php: Unable to open file "' . $file . '".');
|
||||
}
|
||||
|
||||
flock($fp, LOCK_SH); // @since 1.1.6
|
||||
$stats = fstat($fp);
|
||||
|
||||
if ($shm_id = @shmop_open(self::SHM_KEY, 'w', 0, 0)) {
|
||||
shmop_delete($shm_id);
|
||||
shmop_close($shm_id);
|
||||
}
|
||||
|
||||
if ($shm_id = @shmop_open(self::SHM_KEY, 'c', 0644, $stats['size'])) {
|
||||
$pointer = 0;
|
||||
while ($pointer < $stats['size']) {
|
||||
$buf = fread($fp, 524288);
|
||||
shmop_write($shm_id, $buf, $pointer);
|
||||
$pointer += 524288;
|
||||
}
|
||||
shmop_close($shm_id);
|
||||
}
|
||||
flock($fp, LOCK_UN); // @since 1.1.6
|
||||
fclose($fp);
|
||||
|
||||
$this->shmId = @shmop_open(self::SHM_KEY, 'a', 0, 0);
|
||||
|
||||
if ($this->shmId === FALSE) {
|
||||
throw new Exception('IP2Location.class.php: Unable to access shared memory block.');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->mode = self::FILE_IO;
|
||||
$this->resource = fopen($file, 'rb');
|
||||
flock($this->resource, LOCK_SH); // @since 1.1.6
|
||||
|
||||
if ($mode == self::MEMORY_CACHE) {
|
||||
$this->mode = self::MEMORY_CACHE;
|
||||
$stats = fstat($this->resource);
|
||||
$this->buffer = fread($this->resource, $stats['size']);
|
||||
$this->close(); // @since 1.1.6
|
||||
}
|
||||
}
|
||||
|
||||
$this->database['type'] = $this->readByte(1, '8');
|
||||
$this->database['column'] = $this->readByte(2, '8');
|
||||
$this->database['year'] = $this->readByte(3, '8');
|
||||
$this->database['month'] = $this->readByte(4, '8');
|
||||
$this->database['day'] = $this->readByte(5, '8');
|
||||
$this->database['ipv4_count'] = $this->readByte(6, '32');
|
||||
$this->database['ipv4_base_address'] = $this->readByte(10, '32');
|
||||
$this->database['ipv6_count'] = $this->readByte(14, '32');
|
||||
$this->database['ipv6_base_address'] = $this->readByte(18, '32');
|
||||
|
||||
$this->result = new IP2LocationRecord();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close resource.
|
||||
*
|
||||
* @since 1.1.6
|
||||
*/
|
||||
public function close() {
|
||||
if ($this->resource) {
|
||||
flock($this->resource, LOCK_UN);
|
||||
fclose($this->resource);
|
||||
$this->resource = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bytes.
|
||||
*/
|
||||
private function readByte($pos, $mode = 'string', $auto_size = FALSE) {
|
||||
$result = '';
|
||||
|
||||
switch ($this->mode) {
|
||||
case self::SHARED_MEMORY:
|
||||
if ($mode == 'string') {
|
||||
$data = shmop_read($this->shmId, $pos, ($auto_size) ? shmop_size($this->shmId) - $pos : 100);
|
||||
}
|
||||
else {
|
||||
$data = shmop_read($this->shmId, $pos - 1, 50);
|
||||
}
|
||||
break;
|
||||
|
||||
case self::MEMORY_CACHE:
|
||||
$data = substr($this->buffer, (($mode == 'string') ? $pos : $pos - 1), 100);
|
||||
break;
|
||||
|
||||
default:
|
||||
if ($mode == 'string') {
|
||||
fseek($this->resource, $pos, SEEK_SET);
|
||||
$data = @fread($this->resource, 1);
|
||||
}
|
||||
else {
|
||||
fseek($this->resource, $pos - 1, SEEK_SET);
|
||||
$data = @fread($this->resource, 50);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($mode) {
|
||||
case '8':
|
||||
$out = $this->readBinary('C', $data);
|
||||
$result = $out[1];
|
||||
|
||||
break;
|
||||
|
||||
case '32':
|
||||
$out = $this->readBinary('V', $data);
|
||||
if ($out[1] < 0) {
|
||||
$out[1] += 4294967296;
|
||||
}
|
||||
|
||||
$result = (int) $out[1];
|
||||
|
||||
break;
|
||||
|
||||
case '128':
|
||||
$array = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
if (count($array) != 16) {
|
||||
$result = 0;
|
||||
}
|
||||
|
||||
$ip96_127 = $this->readBinary('V', $array[0] . $array[1] . $array[2] . $array[3]);
|
||||
$ip64_95 = $this->readBinary('V', $array[4] . $array[5] . $array[6] . $array[7]);
|
||||
$ip32_63 = $this->readBinary('V', $array[8] . $array[9] . $array[10] . $array[11]);
|
||||
$ip1_31 = $this->readBinary('V', $array[12] . $array[13] . $array[14] . $array[15]);
|
||||
|
||||
if ($ip96_127[1] < 0) {
|
||||
$ip96_127[1] += 4294967296;
|
||||
}
|
||||
if ($ip64_95[1] < 0) {
|
||||
$ip64_95[1] += 4294967296;
|
||||
}
|
||||
if ($ip32_63[1] < 0) {
|
||||
$ip32_63[1] += 4294967296;
|
||||
}
|
||||
if ($ip1_31[1] < 0) {
|
||||
$ip1_31[1] += 4294967296;
|
||||
}
|
||||
|
||||
$result = bcadd(bcadd(bcmul($ip1_31[1], bcpow(4294967296, 3)), bcmul($ip32_63[1], bcpow(4294967296, 2))), bcadd(bcmul($ip64_95[1], 4294967296), $ip96_127[1]));
|
||||
|
||||
break;
|
||||
|
||||
case 'float':
|
||||
$out = $this->readBinary('f', $data);
|
||||
|
||||
$result = $out[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$out = $this->readBinary('C', $data);
|
||||
$result = (in_array($this->mode, array(self::SHARED_MEMORY, self::MEMORY_CACHE))) ? substr($data, 1, $out[1]) : @fread($this->resource, $out[1]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read binary.
|
||||
*/
|
||||
private function readBinary($format, $data) {
|
||||
if ($this->unpackMethod == self::BIG_ENDIAN) {
|
||||
$ar = unpack($format, $data);
|
||||
$vals = array_values($ar);
|
||||
$f = explode('/', $format);
|
||||
$i = 0;
|
||||
|
||||
foreach ($f as $f_value) {
|
||||
$repeater = intval(substr($f_value, 1));
|
||||
|
||||
if ($repeater == 0) {
|
||||
$repeater = 1;
|
||||
}
|
||||
if ($f_value{1} == '*') {
|
||||
$repeater = count($ar) - $i;
|
||||
}
|
||||
if ($f_value{0} != 'd') {
|
||||
$i += $repeater;
|
||||
continue;
|
||||
}
|
||||
|
||||
$j = $i + $repeater;
|
||||
|
||||
for ($a = $i; $a < $j; ++$a) {
|
||||
$p = pack('d', $vals[$i]);
|
||||
$p = strrev($p);
|
||||
list($vals[$i]) = array_values(unpack('d1d', $p));
|
||||
++$i;
|
||||
}
|
||||
}
|
||||
|
||||
$a = 0;
|
||||
foreach ($ar as $ar_key => $ar_value) {
|
||||
$ar[$ar_key] = $vals[$a];
|
||||
++$a;
|
||||
}
|
||||
return $ar;
|
||||
}
|
||||
return unpack($format, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert IPv6 into long integer.
|
||||
*/
|
||||
private function ipv6Numeric($ipv6) {
|
||||
|
||||
if ( extension_loaded('gmp') ):
|
||||
|
||||
$ip_n = inet_pton($ipv6);
|
||||
$bits = 15;
|
||||
|
||||
// 16 x 8 bit = 128bit
|
||||
$ipv6long = 0;
|
||||
|
||||
while ($bits >= 0) {
|
||||
$bin = sprintf("%08b", (ord($ip_n[$bits])));
|
||||
|
||||
if ($ipv6long) {
|
||||
$ipv6long = $bin . $ipv6long;
|
||||
}
|
||||
else {
|
||||
$ipv6long = $bin;
|
||||
}
|
||||
$bits--;
|
||||
}
|
||||
return gmp_strval(gmp_init($ipv6long, 2), 10);
|
||||
|
||||
else:
|
||||
|
||||
/**
|
||||
* Added for IP Geo Block (code from ip2location.class.php)
|
||||
*/
|
||||
$n = substr_count($ipv6, ':');
|
||||
|
||||
if($n < 7){
|
||||
$expanded = '::';
|
||||
|
||||
while($n < 7){
|
||||
$expanded .= ':';
|
||||
$n++;
|
||||
}
|
||||
$ipv6 = preg_replace('/::/', $expanded, $ipv6);
|
||||
}
|
||||
|
||||
$subLoc = 8;
|
||||
$ipv6No = '0';
|
||||
|
||||
foreach(preg_split('/:/', $ipv6) as $ipSub){
|
||||
$subLoc--;
|
||||
|
||||
if($ipSub == '') continue;
|
||||
$ipv6No = bcadd( $ipv6No, bcmul(hexdec($ipSub), bcpow('65536', $subLoc))); // hexdec('0x10000')
|
||||
}
|
||||
return $ipv6No;
|
||||
|
||||
endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate IP address.
|
||||
*/
|
||||
private function validate($ip) {
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
||||
return 6;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Core function to lookup geolocation data.
|
||||
*/
|
||||
public function lookup($ip, $fields = self::ALL) {
|
||||
$this->result->ipAddress = $ip;
|
||||
|
||||
if (($version = $this->validate($ip)) === FALSE) {
|
||||
foreach ($this->result as &$obj) {
|
||||
if ($obj) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$obj = self::INVALID_IP_ADDRESS;
|
||||
}
|
||||
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
if ($version == 4) {
|
||||
return $this->ipv4Lookup($ip, $fields);
|
||||
}
|
||||
|
||||
if ($version == 6) {
|
||||
return $this->ipv6Lookup($ip, $fields);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup for IPv4 records.
|
||||
*/
|
||||
private function ipv4Lookup($ip, $fields) {
|
||||
$keys = array_keys($this->columns);
|
||||
|
||||
$base_address = $this->database['ipv4_base_address'];
|
||||
$high = $this->database['ipv4_count'];
|
||||
$ip_number = sprintf('%u', ip2long($ip));
|
||||
$ip_number = ($ip_number >= 4294967295) ? ($ip_number - 1) : $ip_number;
|
||||
$this->result->ipNumber = $ip_number;
|
||||
|
||||
$low = 0;
|
||||
$mid = 0;
|
||||
$ip_from = 0;
|
||||
$ip_to = 0;
|
||||
|
||||
while ($low <= $high) {
|
||||
$mid = (int) (($low + $high) / 2);
|
||||
$ip_from = $this->readByte($base_address + $mid * ($this->database['column'] * 4), 32);
|
||||
$ip_to = $this->readByte($base_address + ($mid + 1) * ($this->database['column'] * 4), 32);
|
||||
|
||||
if ($ip_from < 0) {
|
||||
$ip_from += pow(2, 32);
|
||||
}
|
||||
|
||||
if ($ip_to < 0) {
|
||||
$ip_to += pow(2, 32);
|
||||
}
|
||||
|
||||
if (($ip_number >= $ip_from) && ($ip_number < $ip_to)) {
|
||||
$return = self::FIELD_NOT_SUPPORTED;
|
||||
$pointer = $base_address + ($mid * $this->database['column'] * 4);
|
||||
|
||||
switch ($fields) {
|
||||
case self::COUNTRY_CODE:
|
||||
case self::REGION_NAME:
|
||||
case self::CITY_NAME:
|
||||
case self::ISP:
|
||||
case self::DOMAIN_NAME:
|
||||
case self::ZIP_CODE:
|
||||
case self::TIME_ZONE:
|
||||
case self::NET_SPEED:
|
||||
case self::IDD_CODE:
|
||||
case self::AREA_CODE:
|
||||
case self::WEATHER_STATION_CODE:
|
||||
case self::WEATHER_STATION_NAME:
|
||||
case self::MCC:
|
||||
case self::MNC:
|
||||
case self::MOBILE_CARRIER_NAME:
|
||||
case self::ELEVATION:
|
||||
if($this->columns[$keys[$fields - 1]][$this->database['type']] != 0){
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case self::COUNTRY_NAME:
|
||||
if($this->columns[$keys[$fields - 1]][$this->database['type']] != 0){
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']] - 1), '32') + 3, 'string', TRUE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case self::LATITUDE:
|
||||
case self::LONGITUDE:
|
||||
if($this->columns[$keys[$fields - 1]][$this->database['type']] != 0){
|
||||
$return = $this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']] - 1), 'float', TRUE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case self::USAGE_TYPE:
|
||||
if($this->columns[$keys[$fields - 1]][$this->database['type']] != 0){
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->result->regionName = $this->result->cityName = $this->result->latitude = $this->result->longitude = $this->result->isp = $this->result->domainName = $this->result->zipCode = $this->result->timeZone = $this->result->netSpeed = $this->result->iddCode = $this->result->areaCode = $this->result->weatherStationCode = $this->result->weatherStationName = $this->result->mcc = $this->result->mnc = $this->result->mobileCarrierName = $this->result->elevation = $this->result->usageType = self::FIELD_NOT_SUPPORTED;
|
||||
|
||||
$this->result->countryCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::COUNTRY_CODE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
|
||||
$this->result->countryName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::COUNTRY_NAME - 1]][$this->database['type']] - 1), '32') + 3, 'string', TRUE);
|
||||
|
||||
if ($this->columns[$keys[self::REGION_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->regionName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::REGION_NAME - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::CITY_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->cityName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::CITY_NAME - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::LATITUDE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->latitude = $this->readByte($pointer + 4 * ($this->columns[$keys[self::LATITUDE - 1]][$this->database['type']] - 1), 'float', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::LONGITUDE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->longitude = $this->readByte($pointer + 4 * ($this->columns[$keys[self::LONGITUDE - 1]][$this->database['type']] - 1), 'float', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ISP - 1]][$this->database['type']] != 0) {
|
||||
$this->result->isp = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ISP - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::DOMAIN_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->domainName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::DOMAIN_NAME - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ZIP_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->zipCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ZIP_CODE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::TIME_ZONE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->timeZone = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::TIME_ZONE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::NET_SPEED - 1]][$this->database['type']] != 0) {
|
||||
$this->result->netSpeed = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::NET_SPEED - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::IDD_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->iddCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::IDD_CODE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::AREA_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->areaCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::AREA_CODE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::WEATHER_STATION_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->weatherStationCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::WEATHER_STATION_CODE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::WEATHER_STATION_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->weatherStationName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::WEATHER_STATION_NAME - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MCC - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mcc = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MCC - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MNC - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mnc = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MNC - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MOBILE_CARRIER_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mobileCarrierName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MOBILE_CARRIER_NAME - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ELEVATION - 1]][$this->database['type']] != 0) {
|
||||
$this->result->elevation = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ELEVATION - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::USAGE_TYPE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->usageType = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::USAGE_TYPE - 1]][$this->database['type']] - 1), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
return $this->result;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
else {
|
||||
if ($ip_number < $ip_from) {
|
||||
$high = $mid - 1;
|
||||
}
|
||||
else {
|
||||
$low = $mid + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup for IPv6 records.
|
||||
*/
|
||||
private function ipv6Lookup($ip, $fields) {
|
||||
$keys = array_keys($this->columns);
|
||||
|
||||
$base_address = $this->database['ipv6_base_address'];
|
||||
$ip_number = $this->ipv6Numeric($ip);
|
||||
$this->result->ipNumber = $ip_number;
|
||||
|
||||
$low = 0;
|
||||
$mid = 0;
|
||||
$high = $this->database['ipv6_count'];
|
||||
$ip_from = 0;
|
||||
$ip_to = 0;
|
||||
|
||||
while ($low <= $high) {
|
||||
$mid = (int) (($low + $high) / 2);
|
||||
$ip_from = $this->readByte($base_address + $mid * ($this->database['column'] * 4 + 12), 128);
|
||||
$ip_to = $this->readByte($base_address + ($mid + 1) * ($this->database['column'] * 4 + 12), 128);
|
||||
|
||||
if ($ip_from < 0) {
|
||||
$ip_from += pow(2, 32);
|
||||
}
|
||||
|
||||
if ($ip_to < 0) {
|
||||
$ip_to += pow(2, 32);
|
||||
}
|
||||
|
||||
if (($ip_number >= $ip_from) && ($ip_number < $ip_to)) {
|
||||
$return = '';
|
||||
$pointer = $base_address + ($mid * ($this->database['column'] * 4 + 12)) + 8;
|
||||
|
||||
switch ($fields) {
|
||||
case self::COUNTRY_CODE:
|
||||
case self::REGION_NAME:
|
||||
case self::CITY_NAME:
|
||||
case self::ISP:
|
||||
case self::DOMAIN_NAME:
|
||||
case self::ZIP_CODE:
|
||||
case self::TIME_ZONE:
|
||||
case self::NET_SPEED:
|
||||
case self::IDD_CODE:
|
||||
case self::AREA_CODE:
|
||||
case self::WEATHER_STATION_CODE:
|
||||
case self::WEATHER_STATION_NAME:
|
||||
case self::MCC:
|
||||
case self::MNC:
|
||||
case self::MOBILE_CARRIER_NAME:
|
||||
case self::ELEVATION:
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
|
||||
break;
|
||||
|
||||
case self::COUNTRY_NAME:
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']]), '32') + 3, 'string', TRUE);
|
||||
|
||||
break;
|
||||
|
||||
case self::LATITUDE:
|
||||
case self::LONGITUDE:
|
||||
$return = $this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']]), 'float', TRUE);
|
||||
|
||||
break;
|
||||
|
||||
case self::USAGE_TYPE:
|
||||
$return = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[$fields - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->result->regionName = $this->result->cityName = $this->result->latitude = $this->result->longitude = $this->result->isp = $this->result->domainName = $this->result->zipCode = $this->result->timeZone = $this->result->netSpeed = $this->result->iddCode = $this->result->areaCode = $this->result->weatherStationCode = $this->result->weatherStationName = $this->result->mcc = $this->result->mnc = $this->result->mobileCarrierName = $this->result->elevation = $this->result->usageType = self::FIELD_NOT_SUPPORTED;
|
||||
|
||||
if ($this->columns[$keys[self::COUNTRY_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->countryCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::COUNTRY_CODE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
|
||||
$this->result->countryName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::COUNTRY_CODE - 1]][$this->database['type']]), '32') + 3, 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::REGION_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->regionName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::REGION_NAME - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::CITY_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->cityName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::CITY_NAME - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::LATITUDE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->latitude = $this->readByte($pointer + 4 * ($this->columns[$keys[self::LATITUDE - 1]][$this->database['type']]), 'float', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::LONGITUDE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->longitude = $this->readByte($pointer + 4 * ($this->columns[$keys[self::LONGITUDE - 1]][$this->database['type']]), 'float', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ISP - 1]][$this->database['type']] != 0) {
|
||||
$this->result->isp = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ISP - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::DOMAIN_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->domainName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::DOMAIN_NAME - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ZIP_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->zipCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ZIP_CODE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::TIME_ZONE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->timeZone = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::TIME_ZONE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::NET_SPEED - 1]][$this->database['type']] != 0) {
|
||||
$this->result->netSpeed = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::NET_SPEED - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::IDD_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->iddCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::IDD_CODE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::AREA_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->areaCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::AREA_CODE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::WEATHER_STATION_CODE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->weatherStationCode = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::WEATHER_STATION_CODE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::WEATHER_STATION_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->weatherStationName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::WEATHER_STATION_NAME - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MCC - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mcc = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MCC - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MNC - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mnc = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MNC - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::MOBILE_CARRIER_NAME - 1]][$this->database['type']] != 0) {
|
||||
$this->result->mobileCarrierName = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::MOBILE_CARRIER_NAME - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::ELEVATION - 1]][$this->database['type']] != 0) {
|
||||
$this->result->elevation = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::ELEVATION - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
if ($this->columns[$keys[self::USAGE_TYPE - 1]][$this->database['type']] != 0) {
|
||||
$this->result->usageType = $this->readByte($this->readByte($pointer + 4 * ($this->columns[$keys[self::USAGE_TYPE - 1]][$this->database['type']]), '32'), 'string', TRUE);
|
||||
}
|
||||
|
||||
return $this->result;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
else {
|
||||
if ($ip_number < $ip_from) {
|
||||
$high = $mid - 1;
|
||||
}
|
||||
else {
|
||||
$low = $mid + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
216
wp/wp-content/ip-geo-api/ip2location/bcmath.php
Normal file
216
wp/wp-content/ip-geo-api/ip2location/bcmath.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
/**
|
||||
* Alternative library for BCMath extension
|
||||
*
|
||||
*/
|
||||
if ( extension_loaded('gmp') ):
|
||||
|
||||
if ( ! function_exists('bcadd') ):
|
||||
function bcadd( $Num1 = '0', $Num2 = '0', $Scale = null ) {
|
||||
return gmp_strval( gmp_add( $Num1, $Num2 ), 10 );
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists('bcsub') ):
|
||||
function bcsub( $Num1 = '0', $Num2 = '0', $Scale = null ) {
|
||||
return gmp_strval( gmp_sub( $Num1, $Num2 ), 10 );
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists('bcmul') ):
|
||||
function bcmul( $Num1 = '0', $Num2 = '0' ) {
|
||||
return gmp_strval( gmp_mul( $Num1, $Num2 ), 10 );
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists('bcpow') ):
|
||||
function bcpow( $num, $power ) {
|
||||
return gmp_strval( gmp_pow( $num, $power ), 10 );
|
||||
}
|
||||
endif;
|
||||
|
||||
else: // extension_loaded('gmp')
|
||||
|
||||
if ( ! function_exists('bcadd') ):
|
||||
/**
|
||||
* bcadd - Add two arbitrary precision numbers.
|
||||
* @link https://php.net/manual/en/function.bcadd.php
|
||||
*/
|
||||
function bcadd($Num1='0',$Num2='0',$Scale=null) {
|
||||
// check if they're valid positive numbers, extract the whole numbers and decimals
|
||||
if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
|
||||
!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');
|
||||
|
||||
// this is where the result is stored
|
||||
$Output=array();
|
||||
|
||||
// remove ending zeroes from decimals and remove point
|
||||
$Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
|
||||
$Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';
|
||||
|
||||
// calculate the longest length of decimals
|
||||
$DLen=max(strlen($Dec1),strlen($Dec2));
|
||||
|
||||
// if $Scale is null, automatically set it to the amount of decimal places for accuracy
|
||||
if($Scale==null) $Scale=$DLen;
|
||||
|
||||
// remove leading zeroes and reverse the whole numbers, then append padded decimals on the end
|
||||
$Num1=strrev(ltrim($Tmp1[1],'0').str_pad($Dec1,$DLen,'0'));
|
||||
$Num2=strrev(ltrim($Tmp2[1],'0').str_pad($Dec2,$DLen,'0'));
|
||||
|
||||
// calculate the longest length we need to process
|
||||
$MLen=max(strlen($Num1),strlen($Num2));
|
||||
|
||||
// pad the two numbers so they are of equal length (both equal to $MLen)
|
||||
$Num1=str_pad($Num1,$MLen,'0');
|
||||
$Num2=str_pad($Num2,$MLen,'0');
|
||||
|
||||
// process each digit, keep the ones, carry the tens (remainders)
|
||||
for($i=0;$i<$MLen;$i++) {
|
||||
$Sum=((int)$Num1{$i}+(int)$Num2{$i});
|
||||
if(isset($Output[$i])) $Sum+=$Output[$i];
|
||||
$Output[$i]=$Sum%10;
|
||||
if($Sum>9) $Output[$i+1]=1;
|
||||
}
|
||||
|
||||
// convert the array to string and reverse it
|
||||
$Output=strrev(implode($Output));
|
||||
|
||||
// substring the decimal digits from the result, pad if necessary (if $Scale > amount of actual decimals)
|
||||
// next, since actual zero values can cause a problem with the substring values, if so, just simply give '0'
|
||||
// next, append the decimal value, if $Scale is defined, and return result
|
||||
// $Decimal=str_pad(substr($Output,-$DLen,$Scale),$Scale,'0');
|
||||
// $Output=(($MLen-$DLen<1)?'0':substr($Output,0,-$DLen));
|
||||
// $Output.=(($Scale>0)?".{$Decimal}":'');
|
||||
// return($Output);
|
||||
return( $Output ? $Output : '0' );
|
||||
}
|
||||
endif; // ! function_exists('bcadd')
|
||||
|
||||
if ( ! function_exists('bcsub') ):
|
||||
/**
|
||||
* bcsub - Subtract one arbitrary precision number from another.
|
||||
* @link https://php.net/manual/en/function.bcsub.php
|
||||
*/
|
||||
function bcsub($Num1='0',$Num2='0',$Scale=null) {
|
||||
// check if they're valid positive numbers, extract the whole numbers and decimals
|
||||
if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
|
||||
!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');
|
||||
|
||||
// this is where the result is stored
|
||||
$Output=array();
|
||||
|
||||
// remove ending zeroes from decimals and remove point
|
||||
$Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
|
||||
$Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';
|
||||
|
||||
// calculate the longest length of decimals
|
||||
$DLen=max(strlen($Dec1),strlen($Dec2));
|
||||
|
||||
// if $Scale is null, automatically set it to the amount of decimal places for accuracy
|
||||
if($Scale==null) $Scale=$DLen;
|
||||
|
||||
// remove leading zeroes and reverse the whole numbers, then append padded decimals on the end
|
||||
$Num1=strrev(ltrim($Tmp1[1],'0').str_pad($Dec1,$DLen,'0'));
|
||||
$Num2=strrev(ltrim($Tmp2[1],'0').str_pad($Dec2,$DLen,'0'));
|
||||
|
||||
// calculate the longest length we need to process
|
||||
$MLen=max(strlen($Num1),strlen($Num2));
|
||||
|
||||
// pad the two numbers so they are of equal length (both equal to $MLen)
|
||||
$Num1=str_pad($Num1,$MLen,'0');
|
||||
$Num2=str_pad($Num2,$MLen,'0');
|
||||
|
||||
// process each digit, keep the ones, carry the tens (remainders)
|
||||
for($i=0;$i<$MLen;$i++) {
|
||||
$Sum=((int)$Num1{$i}-(int)$Num2{$i});
|
||||
if(isset($Output[$i])) $Sum+=$Output[$i];
|
||||
$Output[$i]=$Sum%10;
|
||||
if($Sum>9) $Output[$i+1]=1;
|
||||
}
|
||||
|
||||
// convert the array to string and reverse it
|
||||
$Output=strrev(implode($Output));
|
||||
|
||||
// substring the decimal digits from the result, pad if necessary (if $Scale > amount of actual decimals)
|
||||
// next, since actual zero values can cause a problem with the substring values, if so, just simply give '0'
|
||||
// next, append the decimal value, if $Scale is defined, and return result
|
||||
// $Decimal=str_pad(substr($Output,-$DLen,$Scale),$Scale,'0');
|
||||
// $Output=(($MLen-$DLen<1)?'0':substr($Output,0,-$DLen));
|
||||
// $Output.=(($Scale>0)?".{$Decimal}":'');
|
||||
// return($Output);
|
||||
return( $Output ? $Output : '0' );
|
||||
}
|
||||
endif; // ! function_exists('bcsub')
|
||||
|
||||
if ( ! function_exists('bcmul') ):
|
||||
/**
|
||||
* bcmul - Multiply two arbitrary precision numbers.
|
||||
* @link https://php.net/manual/en/function.bcmul.php
|
||||
*/
|
||||
function bcmul($Num1='0',$Num2='0') {
|
||||
// check if they're both plain numbers
|
||||
if(!preg_match("/^\d+$/",$Num1)||!preg_match("/^\d+$/",$Num2)) return(0);
|
||||
|
||||
// remove zeroes from beginning of numbers
|
||||
for($i=0;$i<strlen($Num1);$i++) {if(@$Num1{$i}!='0') {$Num1=substr($Num1,$i);break;}}
|
||||
for($i=0;$i<strlen($Num2);$i++) {if(@$Num2{$i}!='0') {$Num2=substr($Num2,$i);break;}}
|
||||
|
||||
// get both number lengths
|
||||
$Len1=strlen($Num1);
|
||||
$Len2=strlen($Num2);
|
||||
|
||||
// $Rema is for storing the calculated numbers and $Rema2 is for carrying the remainders
|
||||
$Rema=$Rema2=array();
|
||||
|
||||
// we start by making a $Len1 by $Len2 table (array)
|
||||
for($y=$i=0;$y<$Len1;$y++)
|
||||
for($x=0;$x<$Len2;$x++)
|
||||
// we use the classic lattice method for calculating the multiplication..
|
||||
// this will multiply each number in $Num1 with each number in $Num2 and store it accordingly
|
||||
@$Rema[$i++%$Len2].=sprintf('%02d',(int)$Num1{$y}*(int)$Num2{$x});
|
||||
|
||||
// cycle through each stored number
|
||||
for($y=0;$y<$Len2;$y++)
|
||||
for($x=0;$x<$Len1*2;$x++)
|
||||
// add up the numbers in the diagonal fashion the lattice method uses
|
||||
@$Rema2[Floor(($x-1)/2)+1+$y]+=(int)$Rema[$y]{$x};
|
||||
|
||||
// reverse the results around
|
||||
$Rema2=array_reverse($Rema2);
|
||||
|
||||
// cycle through all the results again
|
||||
for($i=0;$i<count($Rema2);$i++) {
|
||||
// reverse this item, split, keep the first digit, spread the other digits down the array
|
||||
$Rema3=str_split(strrev($Rema2[$i]));
|
||||
for($o=0;$o<count($Rema3);$o++)
|
||||
if($o==0) @$Rema2[$i+$o]=$Rema3[$o];
|
||||
else @$Rema2[$i+$o]+=$Rema3[$o];
|
||||
}
|
||||
// implode $Rema2 so it's a string and reverse it, this is the result!
|
||||
$Rema2=strrev(implode($Rema2));
|
||||
|
||||
// just to make sure, we delete the zeros from the beginning of the result and return
|
||||
while(strlen($Rema2)>1&&$Rema2{0}=='0') $Rema2=substr($Rema2,1);
|
||||
|
||||
return($Rema2);
|
||||
}
|
||||
endif; // ! function_exists('bcmul')
|
||||
|
||||
if ( ! function_exists('bcpow') ):
|
||||
/**
|
||||
* bcpow - Raise an arbitrary precision number to another.
|
||||
* @link https://php.net/manual/en/function.bcmul.php
|
||||
*/
|
||||
function bcpow($num, $power) {
|
||||
$awnser = "1";
|
||||
while ($power) {
|
||||
$awnser = bcmul($awnser, $num, 100);
|
||||
$power = bcsub($power, "1");
|
||||
}
|
||||
return rtrim($awnser, '0.');
|
||||
}
|
||||
endif; // ! function_exists('bcpow')
|
||||
|
||||
endif; // extension_loaded('gmp')
|
||||
?>
|
||||
226
wp/wp-content/ip-geo-api/ip2location/class-ip2location.php
Normal file
226
wp/wp-content/ip-geo-api/ip2location/class-ip2location.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* IP Geo Block API class library for IP2Location
|
||||
*
|
||||
* @version 1.1.15
|
||||
* @author tokkonopapa <tokkonopapa@yahoo.com>
|
||||
* @license GPL-3.0
|
||||
* @link https://www.ipgeoblock.com/
|
||||
* @copyright 2013-2018 tokkonopapa
|
||||
*/
|
||||
|
||||
class_exists( 'IP_Geo_Block_API', FALSE ) or die;
|
||||
|
||||
/**
|
||||
* URL and Path for IP2Location database
|
||||
*
|
||||
*/
|
||||
define( 'IP_GEO_BLOCK_IP2LOC_IPV4_DAT', 'IP2LOCATION-LITE-DB1.BIN' );
|
||||
define( 'IP_GEO_BLOCK_IP2LOC_IPV6_DAT', 'IP2LOCATION-LITE-DB1.IPV6.BIN' );
|
||||
define( 'IP_GEO_BLOCK_IP2LOC_IPV4_ZIP', 'https://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.BIN.ZIP' );
|
||||
define( 'IP_GEO_BLOCK_IP2LOC_IPV6_ZIP', 'https://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP' );
|
||||
define( 'IP_GEO_BLOCK_IP2LOC_DOWNLOAD', 'https://lite.ip2location.com/database/ip-country' );
|
||||
|
||||
/**
|
||||
* Class for IP2Location
|
||||
*
|
||||
* URL : https://www.ip2location.com/
|
||||
* Term of use : https://www.ip2location.com/terms
|
||||
* Licence fee : Creative Commons Attribution-ShareAlike 4.0 Unported License
|
||||
* Input type : IP address (IPv4)
|
||||
* Output type : array
|
||||
*/
|
||||
class IP_Geo_Block_API_IP2Location extends IP_Geo_Block_API {
|
||||
protected $transform_table = array(
|
||||
'countryCode' => 'countryCode',
|
||||
'countryName' => 'countryName',
|
||||
'regionName' => 'regionName',
|
||||
'cityName' => 'cityName',
|
||||
'latitude' => 'latitude',
|
||||
'longitude' => 'longitude',
|
||||
);
|
||||
|
||||
public function get_location( $ip, $args = array() ) {
|
||||
$settings = IP_Geo_Block::get_option();
|
||||
|
||||
if ( ! extension_loaded('bcmath') )
|
||||
require_once( 'bcmath.php' );
|
||||
|
||||
if ( ! class_exists( 'IP2Location', FALSE ) )
|
||||
require_once( 'IP2Location.php' );
|
||||
|
||||
// setup database file and function
|
||||
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
|
||||
$type = IP_GEO_BLOCK_API_TYPE_IPV4;
|
||||
$file = apply_filters(
|
||||
IP_Geo_Block::PLUGIN_NAME . '-ip2location-path',
|
||||
empty( $settings['IP2Location']['ipv4_path'] ) ?
|
||||
$this->get_db_dir() . IP_GEO_BLOCK_IP2LOC_IPV4_DAT :
|
||||
$settings['IP2Location']['ipv4_path']
|
||||
);
|
||||
}
|
||||
|
||||
elseif ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
|
||||
$type = IP_GEO_BLOCK_API_TYPE_IPV6;
|
||||
$file = empty( $settings['IP2Location']['ipv6_path'] ) ?
|
||||
$this->get_db_dir() . IP_GEO_BLOCK_IP2LOC_IPV6_DAT :
|
||||
$settings['IP2Location']['ipv6_path'];
|
||||
}
|
||||
|
||||
else {
|
||||
return array( 'errorMessage' => 'illegal format' );
|
||||
}
|
||||
|
||||
try {
|
||||
$geo = new IP2Location( $file );
|
||||
if ( $geo && ( $geo->get_database_type() & $type ) ) {
|
||||
$data = $geo->lookup( $ip );
|
||||
$geo->close(); // @since 1.1.6
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ( $this->transform_table as $key => $val ) {
|
||||
if ( isset( $data->$val ) && IP2Location::FIELD_NOT_SUPPORTED !== $data->$val )
|
||||
$res[ $key ] = $data->$val;
|
||||
}
|
||||
|
||||
if ( isset( $res['countryCode'] ) && strlen( $res['countryCode'] ) === 2 )
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception $e) {
|
||||
return array( 'errorMessage' => $e->getMessage() );
|
||||
}
|
||||
|
||||
return array( 'errorMessage' => 'Not supported' );
|
||||
}
|
||||
|
||||
private function get_db_dir() {
|
||||
return IP_Geo_Block_Util::slashit( apply_filters(
|
||||
IP_Geo_Block::PLUGIN_NAME . '-ip2location-dir', dirname( __FILE__ )
|
||||
) );
|
||||
}
|
||||
|
||||
public function download( &$db, $args ) {
|
||||
$dir = $this->get_db_dir();
|
||||
|
||||
// IPv4
|
||||
if ( $dir !== dirname( $db['ipv4_path'] ) . '/' )
|
||||
$db['ipv4_path'] = $dir . IP_GEO_BLOCK_IP2LOC_IPV4_DAT;
|
||||
|
||||
$res['ipv4'] = IP_Geo_Block_Util::download_zip(
|
||||
apply_filters(
|
||||
IP_Geo_Block::PLUGIN_NAME . '-ip2location-zip-ipv4',
|
||||
IP_GEO_BLOCK_IP2LOC_IPV4_ZIP
|
||||
),
|
||||
$args,
|
||||
$db['ipv4_path'],
|
||||
$db['ipv4_last']
|
||||
);
|
||||
|
||||
// IPv6
|
||||
if ( $dir !== dirname( $db['ipv6_path'] ) . '/' )
|
||||
$db['ipv6_path'] = $dir . IP_GEO_BLOCK_IP2LOC_IPV6_DAT;
|
||||
|
||||
$res['ipv6'] = IP_Geo_Block_Util::download_zip(
|
||||
apply_filters(
|
||||
IP_Geo_Block::PLUGIN_NAME . '-ip2location-zip-ipv6',
|
||||
IP_GEO_BLOCK_IP2LOC_IPV6_ZIP
|
||||
),
|
||||
$args,
|
||||
$db['ipv6_path'],
|
||||
$db['ipv6_last']
|
||||
);
|
||||
|
||||
! empty( $res['ipv4']['filename'] ) and $db['ipv4_path'] = $res['ipv4']['filename'];
|
||||
! empty( $res['ipv6']['filename'] ) and $db['ipv6_path'] = $res['ipv6']['filename'];
|
||||
! empty( $res['ipv4']['modified'] ) and $db['ipv4_last'] = $res['ipv4']['modified'];
|
||||
! empty( $res['ipv6']['modified'] ) and $db['ipv6_last'] = $res['ipv6']['modified'];
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function get_attribution() {
|
||||
return 'This site or product includes IP2Location LITE data available from <a class="ip-geo-block-link" href="https://lite.ip2location.com" rel=noreferrer target=_blank>https://lite.ip2location.com</a>. (<a href="https://creativecommons.org/licenses/by-sa/4.0/" title="Creative Commons — Attribution-ShareAlike 4.0 International — CC BY-SA 4.0" rel=noreferrer target=_blank>CC BY-SA 4.0</a>)';
|
||||
}
|
||||
|
||||
public function add_settings_field( $field, $section, $option_slug, $option_name, $options, $callback, $str_path, $str_last ) {
|
||||
require_once IP_GEO_BLOCK_PATH . 'classes/class-ip-geo-block-file.php';
|
||||
$fs = IP_Geo_Block_FS::init( __FILE__ . '(' . __FUNCTION__ . ')' );
|
||||
|
||||
$db = $options[ $field ];
|
||||
$dir = $this->get_db_dir();
|
||||
$msg = __( 'Database file does not exist.', 'ip-geo-block' );
|
||||
|
||||
// IPv4
|
||||
if ( $dir !== dirname( $db['ipv4_path'] ) . '/' )
|
||||
$db['ipv4_path'] = $dir . IP_GEO_BLOCK_IP2LOC_IPV4_DAT;
|
||||
|
||||
// filter database file
|
||||
$db['ipv4_path'] = apply_filters( IP_Geo_Block::PLUGIN_NAME . '-ip2location-path', $db['ipv4_path'] );
|
||||
|
||||
if ( $fs->exists( $db['ipv4_path'] ) )
|
||||
$date = sprintf( $str_last, IP_Geo_Block_Util::localdate( $db['ipv4_last'] ) );
|
||||
else
|
||||
$date = $msg;
|
||||
|
||||
add_settings_field(
|
||||
$option_name . $field . '_ipv4',
|
||||
"$field $str_path<br />(<a rel='noreferrer' href='" . IP_GEO_BLOCK_IP2LOC_DOWNLOAD . "' title='" . IP_GEO_BLOCK_IP2LOC_IPV4_ZIP . "'>IPv4</a>)",
|
||||
$callback,
|
||||
$option_slug,
|
||||
$section,
|
||||
array(
|
||||
'type' => 'text',
|
||||
'option' => $option_name,
|
||||
'field' => $field,
|
||||
'sub-field' => 'ipv4_path',
|
||||
'value' => $db['ipv4_path'],
|
||||
'disabled' => TRUE,
|
||||
'after' => '<br /><p id="ip-geo-block-' . $field . '-ipv4" style="margin-left: 0.2em">' . $date . '</p>',
|
||||
)
|
||||
);
|
||||
|
||||
// IPv6
|
||||
if ( $dir !== dirname( $db['ipv6_path'] ) . '/' )
|
||||
$db['ipv6_path'] = $dir . IP_GEO_BLOCK_IP2LOC_IPV6_DAT;
|
||||
|
||||
// filter database file
|
||||
$db['ipv6_path'] = apply_filters( IP_Geo_Block::PLUGIN_NAME . '-ip2location-path-ipv6', $db['ipv6_path'] );
|
||||
|
||||
if ( $fs->exists( $db['ipv6_path'] ) )
|
||||
$date = sprintf( $str_last, IP_Geo_Block_Util::localdate( $db['ipv6_last'] ) );
|
||||
else
|
||||
$date = $msg;
|
||||
|
||||
add_settings_field(
|
||||
$option_name . $field . '_ipv6',
|
||||
"$field $str_path<br />(<a rel='noreferrer' href='" . IP_GEO_BLOCK_IP2LOC_DOWNLOAD . "' title='" . IP_GEO_BLOCK_IP2LOC_IPV6_ZIP . "'>IPv6</a>)",
|
||||
$callback,
|
||||
$option_slug,
|
||||
$section,
|
||||
array(
|
||||
'type' => 'text',
|
||||
'option' => $option_name,
|
||||
'field' => $field,
|
||||
'sub-field' => 'ipv6_path',
|
||||
'value' => $db['ipv6_path'],
|
||||
'disabled' => TRUE,
|
||||
'after' => '<br /><p id="ip-geo-block-' . $field . '-ipv6" style="margin-left: 0.2em">' . $date . '</p>',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register API
|
||||
*
|
||||
*/
|
||||
IP_Geo_Block_Provider::register_addon( array(
|
||||
'IP2Location' => array(
|
||||
'key' => NULL,
|
||||
'type' => 'IPv4, IPv6 / LGPLv3',
|
||||
'link' => '<a class="ip-geo-block-link" href="https://lite.ip2location.com/" title="Free IP Geolocation Database" rel=noreferrer target=_blank>https://lite.ip2location.com/</a> (IPv4, IPv6 / LGPLv3)',
|
||||
),
|
||||
) );
|
||||
2
wp/wp-content/ip-geo-api/ip2location/index.php
Normal file
2
wp/wp-content/ip-geo-api/ip2location/index.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// Silence is golden.
|
||||
Reference in New Issue
Block a user