Skip to content

Commit 083b459

Browse files
committed
Updated to IP2Location PHP 8.0.2 library.
1 parent b8462e6 commit 083b459

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed

IP2Location/Lib/IP2Location.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Database {
3232
*
3333
* @var string
3434
*/
35-
const VERSION = '7.2.4';
35+
const VERSION = '8.0.2';
3636

3737
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3838
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -609,7 +609,14 @@ class Database {
609609
* @var array
610610
*/
611611
private $ipBase = [];
612-
612+
613+
614+
//hjlim
615+
private $indexBaseAddr = [];
616+
private $year;
617+
private $month;
618+
private $day;
619+
613620
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
614621
// Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
615622
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -735,15 +742,17 @@ public function __construct($file = null, $mode = self::FILE_IO, $defaultFields
735742
$this->offset[4] = -4;
736743
$this->offset[6] = 8;
737744
//
738-
$year = 2000 + $this->readByte(3);
739-
$month = $this->readByte(4);
740-
$day = $this->readByte(5);
741-
$this->date = date('Y-m-d', strtotime("{$year}-{$month}-{$day}"));
745+
$this->year = 2000 + $this->readByte(3);
746+
$this->month = $this->readByte(4);
747+
$this->day = $this->readByte(5);
748+
$this->date = date('Y-m-d', strtotime("{$this->year}-{$this->month}-{$this->day}"));
742749
//
743750
$this->ipCount[4] = $this->readWord(6);
744-
$this->ipBase[4] = $this->readByte(10);
751+
$this->ipBase[4] = $this->readWord(10); //hjlim readword
745752
$this->ipCount[6] = $this->readWord(14);
746753
$this->ipBase[6] = $this->readWord(18);
754+
$this->indexBaseAddr[4] = $this->readWord(22); //hjlim
755+
$this->indexBaseAddr[6] = $this->readWord(26); //hjlim
747756
}
748757

749758
/**
@@ -1473,18 +1482,43 @@ private function binSearch($version, $ipNumber) {
14731482
$high = $this->ipCount[$version];
14741483
$low = 0;
14751484

1485+
//hjlim
1486+
$indexBaseStart = $this->indexBaseAddr[$version];
1487+
if ($indexBaseStart > 0){
1488+
$indexPos = 0;
1489+
switch($version){
1490+
case 4:
1491+
$ipNum1_2 = intval($ipNumber / 65536);
1492+
$indexPos = $indexBaseStart + ($ipNum1_2 << 3);
1493+
1494+
break;
1495+
1496+
case 6:
1497+
$ipNum1 = intval(bcdiv($ipNumber, bcpow('2', '112')));
1498+
$indexPos = $indexBaseStart + ($ipNum1 << 3);
1499+
1500+
break;
1501+
1502+
default:
1503+
return false;
1504+
}
1505+
1506+
$low = $this->readWord($indexPos);
1507+
$high = $this->readWord($indexPos + 4);
1508+
}
1509+
14761510
// as long as we can narrow down the search...
1477-
while ($low <= $high) {
1478-
$mid = (int) ($low + (($high - $low) / 2));
1479-
1511+
while ($low <= $high) {
1512+
$mid = (int) ($low + (($high - $low) >> 1));
1513+
14801514
// Read IP ranges to get boundaries
14811515
$ip_from = $this->readIp($version, $base + $width * $mid);
14821516
$ip_to = $this->readIp($version, $base + $width * ($mid + 1));
1483-
1517+
14841518
// determine whether to return, repeat on the lower half, or repeat on the upper half
14851519
switch (self::ipBetween($version, $ipNumber, $ip_from, $ip_to)) {
14861520
case 0:
1487-
return $base + $offset + $mid * $width;
1521+
return $base + $offset + $mid * $width;
14881522
case -1:
14891523
$high = $mid - 1;
14901524
break;
@@ -1544,6 +1578,20 @@ public function getFields($asNames = false) {
15441578
}
15451579
}
15461580

1581+
/**
1582+
* Return the version of module
1583+
*/
1584+
public function getModuleVersion() {
1585+
return self::VERSION;
1586+
}
1587+
1588+
/**
1589+
* Return the version of module
1590+
*/
1591+
public function getDatabaseVersion() {
1592+
return $this->year . '.' . $this->month . '.' . $this->day;
1593+
}
1594+
15471595
/**
15481596
* This function will look the given IP address up in the database and return the result(s) asked for
15491597
*
@@ -1853,4 +1901,4 @@ public function lookup($ip, $fields = null, $asNamed = true) {
18531901
}
18541902
}
18551903

1856-
}
1904+
}

IP2Location/Model/IP2LocationCore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ class IP2LocationCore extends AppModel
5757
*/
5858
public function get($ip, $query = array())
5959
{
60-
$obj = new IP2Location(dirname(dirname(__FILE__)) . DS . 'data' . DS . 'IP2LOCATION.BIN', IP2Location::FILE_IO);
60+
$obj = new \IP2Location\Database(dirname(dirname(__FILE__)) . DS . 'data' . DS . 'IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);
6161

6262
try {
63-
$records = $obj->lookup($ip, IP2Location::ALL);
63+
$records = $obj->lookup($ip, \IP2Location\Database::ALL);
6464
} catch (Exception $e) {
6565
return null;
6666
}

README

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ App::uses('IP2LocationCore', 'IP2Location.Model');
3030
$IP2Location = new IP2LocationCore();
3131
$record = $IP2Location->get($this->request->clientIp());
3232

33-
echo 'IP Address: ' . $record->ipAddress . '<br />';
34-
echo 'IP Number: ' . $record->ipNumber . '<br />';
35-
echo 'ISO Country Code: ' . $record->countryCode . '<br />';
36-
echo 'Country Name: ' . $record->countryName . '<br />';
37-
echo 'Region Name: ' . $record->regionName . '<br />';
38-
echo 'City Name: ' . $record->cityName . '<br />';
39-
echo 'Latitude: ' . $record->latitude . '<br />';
40-
echo 'Longitude: ' . $record->longitude . '<br />';
41-
echo 'ZIP Code: ' . $record->zipCode . '<br />';
42-
echo 'Time Zone: ' . $record->timeZone . '<br />';
43-
echo 'ISP Name: ' . $record->isp . '<br />';
44-
echo 'Domain Name: ' . $record->domainName . '<br />';
45-
echo 'Net Speed: ' . $record->netSpeed . '<br />';
46-
echo 'IDD Code: ' . $record->iddCode . '<br />';
47-
echo 'Area Code: ' . $record->areaCode . '<br />';
48-
echo 'Weather Station Code: ' . $record->weatherStationCode . '<br />';
49-
echo 'Weather Station Name: ' . $record->weatherStationName . '<br />';
50-
echo 'MCC: ' . $record->mcc . '<br />';
51-
echo 'MNC: ' . $record->mnc . '<br />';
52-
echo 'Mobile Carrier Name: ' . $record->mobileCarrierName . '<br />';
53-
echo 'Elevation: ' . $record->elevation . '<br />';
54-
echo 'Usage Type: ' . $record->usageType . '<br />';
33+
echo 'IP Address: ' . $record['ipAddress'] . '<br />';
34+
echo 'IP Number: ' . $record['ipNumber'] . '<br />';
35+
echo 'ISO Country Code: ' . $record['countryCode'] . '<br />';
36+
echo 'Country Name: ' . $record['countryName'] . '<br />';
37+
echo 'Region Name: ' . $record['regionName'] . '<br />';
38+
echo 'City Name: ' . $record['cityName'] . '<br />';
39+
echo 'Latitude: ' . $record['latitude'] . '<br />';
40+
echo 'Longitude: ' . $record['longitude'] . '<br />';
41+
echo 'ZIP Code: ' . $record['zipCode'] . '<br />';
42+
echo 'Time Zone: ' . $record['timeZone'] . '<br />';
43+
echo 'ISP Name: ' . $record['isp'] . '<br />';
44+
echo 'Domain Name: ' . $record['domainName'] . '<br />';
45+
echo 'Net Speed: ' . $record['netSpeed'] . '<br />';
46+
echo 'IDD Code: ' . $record['iddCode'] . '<br />';
47+
echo 'Area Code: ' . $record['areaCode'] . '<br />';
48+
echo 'Weather Station Code: ' . $record['weatherStationCode'] . '<br />';
49+
echo 'Weather Station Name: ' . $record['weatherStationName'] . '<br />';
50+
echo 'MCC: ' . $record['mcc'] . '<br />';
51+
echo 'MNC: ' . $record['mnc'] . '<br />';
52+
echo 'Mobile Carrier Name: ' . $record['mobileCarrierName'] . '<br />';
53+
echo 'Elevation: ' . $record['elevation'] . '<br />';
54+
echo 'Usage Type: ' . $record['usageType'] . '<br />';
5555

5656

5757
SUPPORT

0 commit comments

Comments
 (0)