Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit 2ab9573

Browse files
committed
Improve performance of ip lookup
Components for ip lookup/location have been updated.
1 parent 6430e5e commit 2ab9573

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

webanalytics.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class web_analytics {
167167
private $a_language = null;
168168
private $ubid = null;
169169
private $session_id = null;
170-
170+
171171
function get_country_by_host($host) {
172172
if(isset($host) && filter_var($host, FILTER_VALIDATE_IP) == false) {
173173
$domain_parts = explode(".", $host);
@@ -178,7 +178,7 @@ function get_country_by_host($host) {
178178
}
179179
return null;
180180
}
181-
181+
182182
function get_country_by_ip($ip) {
183183
if (filter_var($ip, FILTER_VALIDATE_IP)) {
184184
$host = gethostbyaddr($ip);
@@ -197,16 +197,19 @@ function get_country_by_ip($ip) {
197197
}
198198
return null;
199199
}
200-
201-
function get_country_by_rdap($query) {
200+
201+
function get_country_by_rdap($query, $real_time_data = false) {
202202
if(filter_var($query, FILTER_VALIDATE_IP)) {
203203
$ip = $query;
204204
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
205-
$iana_ipv4 = file_get_contents("http://data.iana.org/rdap/ipv4.json");
206-
if(is_bool($iana_ipv4)) {
207-
return null;
205+
$iana_ipv4 = ["description"=>"RDAP bootstrap file for IPv4 address allocations","publication"=>"2019-06-07T19:00:02Z","services"=>[[["41.0.0.0/8","102.0.0.0/8","105.0.0.0/8","154.0.0.0/8","196.0.0.0/8","197.0.0.0/8"],["https://rdap.afrinic.net/rdap/","http://rdap.afrinic.net/rdap/"]],[["1.0.0.0/8", "14.0.0.0/8", "27.0.0.0/8", "36.0.0.0/8", "39.0.0.0/8", "42.0.0.0/8", "43.0.0.0/8", "49.0.0.0/8", "58.0.0.0/8", "59.0.0.0/8", "60.0.0.0/8", "61.0.0.0/8", "101.0.0.0/8", "103.0.0.0/8", "106.0.0.0/8", "110.0.0.0/8", "111.0.0.0/8", "112.0.0.0/8", "113.0.0.0/8", "114.0.0.0/8", "115.0.0.0/8", "116.0.0.0/8", "117.0.0.0/8", "118.0.0.0/8", "119.0.0.0/8", "120.0.0.0/8", "121.0.0.0/8", "122.0.0.0/8", "123.0.0.0/8", "124.0.0.0/8", "125.0.0.0/8", "126.0.0.0/8", "133.0.0.0/8", "150.0.0.0/8", "153.0.0.0/8", "163.0.0.0/8", "171.0.0.0/8", "175.0.0.0/8", "180.0.0.0/8", "182.0.0.0/8", "183.0.0.0/8", "202.0.0.0/8", "203.0.0.0/8", "210.0.0.0/8", "211.0.0.0/8", "218.0.0.0/8", "219.0.0.0/8", "220.0.0.0/8", "221.0.0.0/8", "222.0.0.0/8", "223.0.0.0/8"], ["https://rdap.apnic.net/"]], [["3.0.0.0/8", "4.0.0.0/8", "6.0.0.0/8", "7.0.0.0/8", "8.0.0.0/8", "9.0.0.0/8", "11.0.0.0/8", "12.0.0.0/8", "13.0.0.0/8", "15.0.0.0/8", "16.0.0.0/8", "17.0.0.0/8", "18.0.0.0/8", "19.0.0.0/8", "20.0.0.0/8", "21.0.0.0/8", "22.0.0.0/8", "23.0.0.0/8", "24.0.0.0/8", "26.0.0.0/8", "28.0.0.0/8", "29.0.0.0/8", "30.0.0.0/8", "32.0.0.0/8", "33.0.0.0/8", "34.0.0.0/8", "35.0.0.0/8", "38.0.0.0/8", "40.0.0.0/8", "44.0.0.0/8", "45.0.0.0/8", "47.0.0.0/8", "48.0.0.0/8", "50.0.0.0/8", "52.0.0.0/8", "54.0.0.0/8", "55.0.0.0/8", "56.0.0.0/8", "63.0.0.0/8", "64.0.0.0/8", "65.0.0.0/8", "66.0.0.0/8", "67.0.0.0/8", "68.0.0.0/8", "69.0.0.0/8", "70.0.0.0/8", "71.0.0.0/8", "72.0.0.0/8", "73.0.0.0/8", "74.0.0.0/8", "75.0.0.0/8", "76.0.0.0/8", "96.0.0.0/8", "97.0.0.0/8", "98.0.0.0/8", "99.0.0.0/8", "100.0.0.0/8", "104.0.0.0/8", "107.0.0.0/8", "108.0.0.0/8", "128.0.0.0/8", "129.0.0.0/8", "130.0.0.0/8", "131.0.0.0/8", "132.0.0.0/8", "134.0.0.0/8", "135.0.0.0/8", "136.0.0.0/8", "137.0.0.0/8", "138.0.0.0/8", "139.0.0.0/8", "140.0.0.0/8", "142.0.0.0/8", "143.0.0.0/8", "144.0.0.0/8", "146.0.0.0/8", "147.0.0.0/8", "148.0.0.0/8", "149.0.0.0/8", "152.0.0.0/8", "155.0.0.0/8", "156.0.0.0/8", "157.0.0.0/8", "158.0.0.0/8", "159.0.0.0/8", "160.0.0.0/8", "161.0.0.0/8", "162.0.0.0/8", "164.0.0.0/8", "165.0.0.0/8", "166.0.0.0/8", "167.0.0.0/8", "168.0.0.0/8", "169.0.0.0/8", "170.0.0.0/8", "172.0.0.0/8", "173.0.0.0/8", "174.0.0.0/8", "184.0.0.0/8", "192.0.0.0/8", "198.0.0.0/8", "199.0.0.0/8", "204.0.0.0/8", "205.0.0.0/8", "206.0.0.0/8", "207.0.0.0/8", "208.0.0.0/8", "209.0.0.0/8", "214.0.0.0/8", "215.0.0.0/8", "216.0.0.0/8"], ["https://rdap.arin.net/registry/", "http://rdap.arin.net/registry/"]], [["2.0.0.0/8", "5.0.0.0/8", "25.0.0.0/8", "31.0.0.0/8", "37.0.0.0/8", "46.0.0.0/8", "51.0.0.0/8", "53.0.0.0/8", "57.0.0.0/8", "62.0.0.0/8", "77.0.0.0/8", "78.0.0.0/8", "79.0.0.0/8", "80.0.0.0/8", "81.0.0.0/8", "82.0.0.0/8", "83.0.0.0/8", "84.0.0.0/8", "85.0.0.0/8", "86.0.0.0/8", "87.0.0.0/8", "88.0.0.0/8", "89.0.0.0/8", "90.0.0.0/8", "91.0.0.0/8", "92.0.0.0/8", "93.0.0.0/8", "94.0.0.0/8", "95.0.0.0/8", "109.0.0.0/8", "141.0.0.0/8", "145.0.0.0/8", "151.0.0.0/8", "176.0.0.0/8", "178.0.0.0/8", "185.0.0.0/8", "188.0.0.0/8", "193.0.0.0/8", "194.0.0.0/8", "195.0.0.0/8", "212.0.0.0/8", "213.0.0.0/8", "217.0.0.0/8"], ["https://rdap.db.ripe.net/"]], [["177.0.0.0/8", "179.0.0.0/8", "181.0.0.0/8", "186.0.0.0/8", "187.0.0.0/8", "189.0.0.0/8", "190.0.0.0/8", "191.0.0.0/8", "200.0.0.0/8", "201.0.0.0/8"], ["https://rdap.lacnic.net/rdap/"]]], "version"=> "1.0"];
206+
if($real_time_data) {
207+
$iana_ipv4 = file_get_contents("http://data.iana.org/rdap/ipv4.json");
208+
if(is_bool($iana_ipv4)) {
209+
return null;
210+
}
211+
$iana_ipv4 = json_decode($iana_ipv4, true);
208212
}
209-
$iana_ipv4 = json_decode($iana_ipv4, true);
210213
$ip_parts = explode(".", $ip);
211214
foreach ($iana_ipv4["services"] as $service) {
212215
foreach ($service[0] as $iprange) {
@@ -225,11 +228,14 @@ function get_country_by_rdap($query) {
225228
}
226229
}
227230
} else if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
228-
$iana_ipv6 = file_get_contents("http://data.iana.org/rdap/ipv6.json");
229-
if(is_bool($iana_ipv6)) {
230-
return null;
231+
$iana_ipv6 = ["description"=> "RDAP bootstrap file for IPv6 address allocations", "publication"=> "2019-11-06T19:00:04Z", "services"=> [[["2001:4200::/23", "2c00::/12"], ["https://rdap.afrinic.net/rdap/", "http://rdap.afrinic.net/rdap/"]], [["2001:200::/23", "2001:4400::/23", "2001:8000::/19", "2001:a000::/20", "2001:b000::/20", "2001:c00::/23", "2001:e00::/23", "2400::/12"], ["https://rdap.apnic.net/"]], [["2001:1800::/23", "2001:400::/23", "2001:4800::/23", "2600::/12", "2610::/23", "2620::/23", "2630::/12"], ["https://rdap.arin.net/registry/", "http://rdap.arin.net/registry/"]], [["2001:1400::/22", "2001:1a00::/23", "2001:1c00::/22", "2001:2000::/19", "2001:4000::/23", "2001:4600::/23", "2001:4a00::/23", "2001:4c00::/23", "2001:5000::/20", "2001:600::/23", "2001:800::/22", "2003::/18", "2a00::/12", "2a10::/12"], ["https://rdap.db.ripe.net/"]], [["2001:1200::/23", "2800::/12"], ["https://rdap.lacnic.net/rdap/"]]], "version"=>"1.0"];
232+
if($real_time_data) {
233+
$iana_ipv6 = file_get_contents("http://data.iana.org/rdap/ipv6.json");
234+
if (is_bool($iana_ipv6)) {
235+
return null;
236+
}
237+
$iana_ipv6 = json_decode($iana_ipv6, true);
231238
}
232-
$iana_ipv6 = json_decode($iana_ipv6, true);
233239
$ip_parts = explode(":", $ip);
234240
foreach ($iana_ipv6["services"] as $service) {
235241
foreach ($service[0] as $iprange) {

0 commit comments

Comments
 (0)