@@ -31,7 +31,7 @@ class Database
31
31
*
32
32
* @var string
33
33
*/
34
- public const VERSION = '2.0.1 ' ;
34
+ public const VERSION = '2.1.0 ' ;
35
35
36
36
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37
37
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -481,6 +481,9 @@ class Database
481
481
private $ year ;
482
482
private $ month ;
483
483
private $ day ;
484
+
485
+ // This variable will be used to hold the raw row of columns's positions
486
+ private $ raw_positions_row ;
484
487
485
488
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
486
489
// Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -855,6 +858,14 @@ protected function lookup($ip, $fields = null, $asNamed = true)
855
858
if ($ fields === null ) {
856
859
$ fields = $ this ->defaultFields ;
857
860
}
861
+
862
+ // Get the entire row based on the pointer value.
863
+ // The length of the row differs based on the IP version.
864
+ if (4 === $ ipVersion ) {
865
+ $ this ->raw_positions_row = $ this ->read ($ pointer - 1 , $ this ->columnWidth [4 ] + 4 );
866
+ } elseif (6 === $ ipVersion ) {
867
+ $ this ->raw_positions_row = $ this ->read ($ pointer - 1 , $ this ->columnWidth [6 ]);
868
+ }
858
869
859
870
// turn fields into an array in case it wasn't already
860
871
$ ifields = (array ) $ fields ;
@@ -1251,7 +1262,7 @@ private static function ipVersionAndNumber($ip)
1251
1262
} elseif (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
1252
1263
// 6to4 Address - 2002::/16
1253
1264
if (substr ($ ip , 0 , 4 ) == '2002 ' ) {
1254
- return [4 , sprintf ('%u ' , long2ip (gmp_import (inet_pton ('::FFFF: ' . substr ($ ip , 5 , 9 )))))];
1265
+ return [4 , sprintf ('%u ' , long2ip (gmp_intval ( gmp_import (inet_pton ('::FFFF: ' . substr ($ ip , 5 , 9 ) )))))];
1255
1266
}
1256
1267
1257
1268
// Teredo Address - 2001:0::/32
@@ -1342,9 +1353,9 @@ private function read($pos, $len)
1342
1353
* @return string
1343
1354
*/
1344
1355
private function readString ($ pos , $ additional = 0 )
1345
- {
1346
- // Get the actual pointer to the string's head
1347
- $ spos = $ this ->readWord ( $ pos) + $ additional ;
1356
+ {
1357
+ // Get the actual pointer to the string's head by extract from raw row data.
1358
+ $ spos = unpack ( ' V ' , substr ( $ this ->raw_positions_row , $ pos, 4 ))[ 1 ] + $ additional ;
1348
1359
1349
1360
// Read as much as the length (first "string" byte) indicates
1350
1361
return $ this ->read ($ spos + 1 , $ this ->readByte ($ spos + 1 ));
@@ -1427,8 +1438,8 @@ private function readCountryNameAndCode($pointer)
1427
1438
// Read the country code and name (the name shares the country's pointer,
1428
1439
// but it must be artificially displaced 3 bytes ahead: 2 for the country code, one
1429
1440
// for the country name's length)
1430
- $ countryCode = $ this ->readString ($ pointer + self ::$ columns [self ::COUNTRY_CODE ][$ this ->type ]);
1431
- $ countryName = $ this ->readString ($ pointer + self ::$ columns [self ::COUNTRY_NAME ][$ this ->type ], 3 );
1441
+ $ countryCode = $ this ->readString (self ::$ columns [self ::COUNTRY_CODE ][$ this ->type ]);
1442
+ $ countryName = $ this ->readString (self ::$ columns [self ::COUNTRY_NAME ][$ this ->type ], 3 );
1432
1443
}
1433
1444
1434
1445
return [$ countryName , $ countryCode ];
@@ -1451,7 +1462,7 @@ private function readRegionName($pointer)
1451
1462
$ regionName = self ::FIELD_NOT_SUPPORTED ;
1452
1463
} else {
1453
1464
// Read the region name
1454
- $ regionName = $ this ->readString ($ pointer + self ::$ columns [self ::REGION_NAME ][$ this ->type ]);
1465
+ $ regionName = $ this ->readString (self ::$ columns [self ::REGION_NAME ][$ this ->type ]);
1455
1466
}
1456
1467
1457
1468
return $ regionName ;
@@ -1474,7 +1485,7 @@ private function readCityName($pointer)
1474
1485
$ cityName = self ::FIELD_NOT_SUPPORTED ;
1475
1486
} else {
1476
1487
// Read the city name
1477
- $ cityName = $ this ->readString ($ pointer + self ::$ columns [self ::CITY_NAME ][$ this ->type ]);
1488
+ $ cityName = $ this ->readString (self ::$ columns [self ::CITY_NAME ][$ this ->type ]);
1478
1489
}
1479
1490
1480
1491
return $ cityName ;
@@ -1497,7 +1508,7 @@ private function readIsp($pointer)
1497
1508
$ isp = self ::FIELD_NOT_SUPPORTED ;
1498
1509
} else {
1499
1510
// Read isp name
1500
- $ isp = $ this ->readString ($ pointer + self ::$ columns [self ::ISP ][$ this ->type ]);
1511
+ $ isp = $ this ->readString (self ::$ columns [self ::ISP ][$ this ->type ]);
1501
1512
}
1502
1513
1503
1514
return $ isp ;
@@ -1520,7 +1531,7 @@ private function readProxyType($pointer)
1520
1531
$ proxyType = self ::FIELD_NOT_SUPPORTED ;
1521
1532
} else {
1522
1533
// Read proxy type
1523
- $ proxyType = $ this ->readString ($ pointer + self ::$ columns [self ::PROXY_TYPE ][$ this ->type ]);
1534
+ $ proxyType = $ this ->readString (self ::$ columns [self ::PROXY_TYPE ][$ this ->type ]);
1524
1535
}
1525
1536
1526
1537
return $ proxyType ;
@@ -1543,7 +1554,7 @@ private function readDomain($pointer)
1543
1554
$ domain = self ::FIELD_NOT_SUPPORTED ;
1544
1555
} else {
1545
1556
// Read the domain
1546
- $ domain = $ this ->readString ($ pointer + self ::$ columns [self ::DOMAIN ][$ this ->type ]);
1557
+ $ domain = $ this ->readString (self ::$ columns [self ::DOMAIN ][$ this ->type ]);
1547
1558
}
1548
1559
1549
1560
return $ domain ;
@@ -1566,7 +1577,7 @@ private function readUsageType($pointer)
1566
1577
$ usageType = self ::FIELD_NOT_SUPPORTED ;
1567
1578
} else {
1568
1579
// Read the domain
1569
- $ usageType = $ this ->readString ($ pointer + self ::$ columns [self ::USAGE_TYPE ][$ this ->type ]);
1580
+ $ usageType = $ this ->readString (self ::$ columns [self ::USAGE_TYPE ][$ this ->type ]);
1570
1581
}
1571
1582
1572
1583
return $ usageType ;
@@ -1589,7 +1600,7 @@ private function readAsn($pointer)
1589
1600
$ asn = self ::FIELD_NOT_SUPPORTED ;
1590
1601
} else {
1591
1602
// Read the domain
1592
- $ asn = $ this ->readString ($ pointer + self ::$ columns [self ::ASN ][$ this ->type ]);
1603
+ $ asn = $ this ->readString (self ::$ columns [self ::ASN ][$ this ->type ]);
1593
1604
}
1594
1605
1595
1606
return $ asn ;
@@ -1612,7 +1623,7 @@ private function readAs($pointer)
1612
1623
$ as = self ::FIELD_NOT_SUPPORTED ;
1613
1624
} else {
1614
1625
// Read the domain
1615
- $ as = $ this ->readString ($ pointer + self ::$ columns [self ::_AS ][$ this ->type ]);
1626
+ $ as = $ this ->readString (self ::$ columns [self ::_AS ][$ this ->type ]);
1616
1627
}
1617
1628
1618
1629
return $ as ;
@@ -1635,7 +1646,7 @@ private function readLastSeen($pointer)
1635
1646
$ lastSeen = self ::FIELD_NOT_SUPPORTED ;
1636
1647
} else {
1637
1648
// Read the domain
1638
- $ lastSeen = $ this ->readString ($ pointer + self ::$ columns [self ::LAST_SEEN ][$ this ->type ]);
1649
+ $ lastSeen = $ this ->readString (self ::$ columns [self ::LAST_SEEN ][$ this ->type ]);
1639
1650
}
1640
1651
1641
1652
return $ lastSeen ;
0 commit comments