|
@@ -0,0 +1,127 @@
|
|
1
|
+<?php
|
|
2
|
+/*
|
|
3
|
+ This script detects the client's IP address and fetches ISP info from ipinfo.io/
|
|
4
|
+ Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Contry and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled).
|
|
5
|
+ Client side, the output of this script can be treated as JSON or as regular text. If the output is regular text, it will be shown to the user as is.
|
|
6
|
+*/
|
|
7
|
+error_reporting(0);
|
|
8
|
+$ip = "";
|
|
9
|
+header('Content-Type: application/json; charset=utf-8');
|
|
10
|
+if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
11
|
+ $ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
12
|
+} elseif (!empty($_SERVER['X-Real-IP'])) {
|
|
13
|
+ $ip = $_SERVER['X-Real-IP'];
|
|
14
|
+} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
15
|
+ $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
16
|
+ $ip = preg_replace("/,.*/", "", $ip); # hosts are comma-separated, client is first
|
|
17
|
+} else {
|
|
18
|
+ $ip = $_SERVER['REMOTE_ADDR'];
|
|
19
|
+}
|
|
20
|
+
|
|
21
|
+$ip = preg_replace("/^::ffff:/", "", $ip);
|
|
22
|
+
|
|
23
|
+if ($ip == "::1") { // ::1/128 is the only localhost ipv6 address. there are no others, no need to strpos this
|
|
24
|
+ echo json_encode(['processedString' => $ip . " - localhost IPv6 access", 'rawIspInfo' => ""]);
|
|
25
|
+ die();
|
|
26
|
+}
|
|
27
|
+if (stripos($ip, 'fe80:') === 0) { // simplified IPv6 link-local address (should match fe80::/10)
|
|
28
|
+ echo json_encode(['processedString' => $ip . " - link-local IPv6 access", 'rawIspInfo' => ""]);
|
|
29
|
+ die();
|
|
30
|
+}
|
|
31
|
+if (strpos($ip, '127.') === 0) { //anything within the 127/8 range is localhost ipv4, the ip must start with 127.0
|
|
32
|
+ echo json_encode(['processedString' => $ip . " - localhost IPv4 access", 'rawIspInfo' => ""]);
|
|
33
|
+ die();
|
|
34
|
+}
|
|
35
|
+if (strpos($ip, '10.') === 0) { // 10/8 private IPv4
|
|
36
|
+ echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
|
|
37
|
+ die();
|
|
38
|
+}
|
|
39
|
+if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { // 172.16/12 private IPv4
|
|
40
|
+ echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
|
|
41
|
+ die();
|
|
42
|
+}
|
|
43
|
+if (strpos($ip, '192.168.') === 0) { // 192.168/16 private IPv4
|
|
44
|
+ echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
|
|
45
|
+ die();
|
|
46
|
+}
|
|
47
|
+if (strpos($ip, '169.254.') === 0) { // IPv4 link-local
|
|
48
|
+ echo json_encode(['processedString' => $ip . " - link-local IPv4 access", 'rawIspInfo' => ""]);
|
|
49
|
+ die();
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+/**
|
|
53
|
+ * Optimized algorithm from http://www.codexworld.com
|
|
54
|
+ *
|
|
55
|
+ * @param float $latitudeFrom
|
|
56
|
+ * @param float $longitudeFrom
|
|
57
|
+ * @param float $latitudeTo
|
|
58
|
+ * @param float $longitudeTo
|
|
59
|
+ *
|
|
60
|
+ * @return float [km]
|
|
61
|
+ */
|
|
62
|
+function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
|
|
63
|
+ $rad = M_PI / 180;
|
|
64
|
+ $theta = $longitudeFrom - $longitudeTo;
|
|
65
|
+ $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
|
|
66
|
+ return acos($dist) / $rad * 60 * 1.853;
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+if (isset($_GET["isp"])) {
|
|
70
|
+ $isp = "";
|
|
71
|
+ $rawIspInfo=null;
|
|
72
|
+ try {
|
|
73
|
+ $json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
|
|
74
|
+ $details = json_decode($json, true);
|
|
75
|
+ $rawIspInfo=$details;
|
|
76
|
+ if (array_key_exists("org", $details)){
|
|
77
|
+ $isp .= $details["org"];
|
|
78
|
+ $isp=preg_replace("/AS\d{1,}\s/","",$isp); //Remove AS##### from ISP name, if present
|
|
79
|
+ }else{
|
|
80
|
+ $isp .= "Unknown ISP";
|
|
81
|
+ }
|
|
82
|
+ if (array_key_exists("country", $details)){
|
|
83
|
+ $isp .= ", " . $details["country"];
|
|
84
|
+ }
|
|
85
|
+ $clientLoc = NULL;
|
|
86
|
+ $serverLoc = NULL;
|
|
87
|
+ if (array_key_exists("loc", $details)){
|
|
88
|
+ $clientLoc = $details["loc"];
|
|
89
|
+ }
|
|
90
|
+ if (isset($_GET["distance"])) {
|
|
91
|
+ if ($clientLoc) {
|
|
92
|
+ $json = file_get_contents("https://ipinfo.io/json");
|
|
93
|
+ $details = json_decode($json, true);
|
|
94
|
+ if (array_key_exists("loc", $details)){
|
|
95
|
+ $serverLoc = $details["loc"];
|
|
96
|
+ }
|
|
97
|
+ if ($serverLoc) {
|
|
98
|
+ try {
|
|
99
|
+ $clientLoc = explode(",", $clientLoc);
|
|
100
|
+ $serverLoc = explode(",", $serverLoc);
|
|
101
|
+ $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
|
|
102
|
+ if ($_GET["distance"] == "mi") {
|
|
103
|
+ $dist /= 1.609344;
|
|
104
|
+ $dist = round($dist, -1);
|
|
105
|
+ if ($dist < 15)
|
|
106
|
+ $dist = "<15";
|
|
107
|
+ $isp .= " (" . $dist . " mi)";
|
|
108
|
+ }else if ($_GET["distance"] == "km") {
|
|
109
|
+ $dist = round($dist, -1);
|
|
110
|
+ if ($dist < 20)
|
|
111
|
+ $dist = "<20";
|
|
112
|
+ $isp .= " (" . $dist . " km)";
|
|
113
|
+ }
|
|
114
|
+ } catch (Exception $e) {
|
|
115
|
+
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+ } catch (Exception $ex) {
|
|
121
|
+ $isp = "Unknown ISP";
|
|
122
|
+ }
|
|
123
|
+ echo json_encode(['processedString' => $ip . " - " . $isp, 'rawIspInfo' => $rawIspInfo]);
|
|
124
|
+} else {
|
|
125
|
+ echo json_encode(['processedString' => $ip, 'rawIspInfo' => ""]);
|
|
126
|
+}
|
|
127
|
+?>
|