1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace artes\IP; |
4
|
|
|
|
5
|
|
|
use artes\Curl\Curl; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A class for geotagging IP addresses. |
9
|
|
|
* |
10
|
|
|
* @SuppressWarnings(PHPMD) |
11
|
|
|
*/ |
12
|
|
|
class IPGeotag |
13
|
|
|
{ |
14
|
|
|
private $ipkey; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor to initiate an IP object, |
18
|
|
|
* |
19
|
|
|
* @param string $userinput |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
12 |
|
public function __construct(string $ipkey) |
23
|
|
|
{ |
24
|
12 |
|
$this->ipkey = $ipkey; |
25
|
12 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function checkuserip() : array |
28
|
|
|
{ |
29
|
1 |
|
$mycurl = new Curl(); |
30
|
1 |
|
$url = "http://api.ipstack.com/check?access_key=" . $this->ipkey; |
31
|
1 |
|
$jsonresp = $mycurl->curl($url); |
32
|
|
|
|
33
|
1 |
|
return $jsonresp; |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
public function checkdefaultip($input) : array |
37
|
|
|
{ |
38
|
8 |
|
$mycurl = new Curl(); |
39
|
8 |
|
$url = "http://api.ipstack.com/$input?access_key=" . $this->ipkey; |
40
|
8 |
|
$jsonresp = $mycurl->curl($url); |
41
|
|
|
|
42
|
8 |
|
if (!$jsonresp) { |
43
|
6 |
|
$jsonresp = ["ip" => ""]; |
44
|
|
|
} |
45
|
8 |
|
return $jsonresp; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
public function getmap($input) : string |
49
|
|
|
{ |
50
|
3 |
|
$myjson = $this->checkdefaultip($input); |
51
|
3 |
|
if (isset($myjson["latitude"])) { |
52
|
|
|
if ($myjson["latitude"]) { |
53
|
|
|
$map = "https://www.openstreetmap.org/?mlat=" . $myjson["latitude"] . "&mlon=" . $myjson["longitude"] . "#map=10/" . $myjson["latitude"] . "/" . $myjson["longitude"]; |
54
|
|
|
return $map; |
55
|
|
|
} |
56
|
|
|
} |
57
|
3 |
|
return ""; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function parseJson($ip, $arg1, $arg2 = null, $arg3 = null) : string |
61
|
|
|
{ |
62
|
3 |
|
$jsonresp = $this->checkdefaultip($ip); |
63
|
3 |
|
if ($arg3) { |
64
|
3 |
|
return $jsonresp[$arg1][$arg2][0][$arg3] ?? ""; |
65
|
|
|
} |
66
|
3 |
|
if ($arg2) { |
67
|
1 |
|
return $jsonresp[$arg1][$arg2] ?? ""; |
68
|
|
|
} |
69
|
3 |
|
return $jsonresp[$arg1] ?? ""; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public function checkinputip($input) : string |
73
|
|
|
{ |
74
|
3 |
|
if ($input) { |
75
|
3 |
|
$mycurl = new Curl(); |
76
|
3 |
|
$url = "http://api.ipstack.com/$input?access_key=" . $this->ipkey; |
77
|
3 |
|
$jsonresp = $mycurl->curl($url); |
78
|
3 |
|
if (isset($jsonresp["type"])) { |
79
|
|
|
if (($jsonresp["type"] === "ipv4" || $jsonresp["type"] === "ipv6") && $jsonresp["latitude"]) { |
80
|
|
|
return $this->printGeoDetails($jsonresp); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
3 |
|
return ""; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function printmap($lat, $lon) : string |
88
|
|
|
{ |
89
|
2 |
|
if (!is_numeric($lat) || !is_numeric($lon)) { |
90
|
1 |
|
return ""; |
91
|
|
|
} |
92
|
1 |
|
$msg = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.openstreetmap.org/export/embed.html?bbox='. ($lon - 1) . '%2C' . ($lat - 1) . '%2C' . ($lon + 1) . '%2C' . ($lat + 1); |
93
|
1 |
|
$msg = $msg . '&layer=mapnik&marker=' . $lat . "%2C" . $lon . '" style="border: 1px solid black"></iframe><br/>'; |
94
|
1 |
|
$msg = $msg . "<p>Click here for a <a href='https://www.openstreetmap.org/?mlat=" . $lat . "&mlon=" . $lon . "#map=10/" . $lat . "/" . $lon . "' target='_blank'> larger map</a>.</p>"; |
95
|
1 |
|
return $msg; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function printGeoDetails($myjson) : string |
99
|
|
|
{ |
100
|
1 |
|
$msg = "<h3>Ytterligare information från Ipstacks API</h3>"; |
101
|
1 |
|
$msg = $msg . "<p><strong>Continent</strong>: " . $myjson["continent_name"] . "</p>"; |
102
|
1 |
|
$msg = $msg . "<p><strong>Country</strong>: " . $myjson["country_name"] . " (" . $myjson["country_code"] . " +" . $myjson["location"]["calling_code"] . ") <img class='inlineimg' src='" . $myjson["location"]["country_flag"] . "' alt='" . $myjson["country_name"] . "'></p>"; |
103
|
1 |
|
$msg = $msg . "<p><strong>City</strong>: " . $myjson["city"] . "</p>"; |
104
|
1 |
|
$msg = $msg . "<p><strong>Zip</strong>: " . $myjson["zip"] . "</p>"; |
105
|
1 |
|
$msg = $msg . "<p><strong>Language</strong>: " . $myjson["location"]["languages"][0]["name"] . "</p>"; |
106
|
1 |
|
$msg = $msg . "<p><strong>Coordinates</strong>: " . $myjson["latitude"] . "°, " . $myjson["longitude"] . "°</p>"; |
107
|
1 |
|
$msg = $msg . '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.openstreetmap.org/export/embed.html?bbox='. ($myjson["longitude"] - 1) . '%2C' . ($myjson["latitude"] - 1) . '%2C' . ($myjson["longitude"] + 1) . '%2C' . ($myjson["latitude"] + 1); |
108
|
1 |
|
$msg = $msg . '&layer=mapnik&marker=' . $myjson["latitude"] . "%2C" . $myjson["longitude"] . '" style="border: 1px solid black"></iframe><br/>'; |
109
|
1 |
|
$msg = $msg . "<p>Click here for a <a href='https://www.openstreetmap.org/?mlat=" . $myjson["latitude"] . "&mlon=" . $myjson["longitude"] . "#map=10/" . $myjson["latitude"] . "/" . $myjson["longitude"] . "' target='_blank'> larger map</a>.</p>"; |
110
|
1 |
|
return $msg; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|