1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Ip verifier. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Anax\IpGeo; |
8
|
|
|
|
9
|
|
|
use Anax\IpVerify\IpVerify; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Showing off a standard class with methods and properties. |
13
|
|
|
*/ |
14
|
|
|
class IpGeo |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $apiKey; |
18
|
|
|
protected $ipVerify; |
19
|
|
|
protected $output; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor for object. |
23
|
|
|
* |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
33 |
|
public function __construct() |
28
|
|
|
{ |
29
|
33 |
|
$this->ipVerify = new IpVerify(); |
30
|
33 |
|
$this->output = [ |
31
|
|
|
"ip" => "", |
32
|
|
|
"protocol" => "", |
33
|
|
|
"isValid" => "", |
34
|
|
|
"domain" => "", |
35
|
|
|
"long" => "", |
36
|
|
|
"lat" => "", |
37
|
|
|
"country" => "", |
38
|
|
|
"city" => "", |
39
|
|
|
"map" => "", |
40
|
|
|
"flag" => "", |
41
|
|
|
"embed" => "", |
42
|
|
|
]; |
43
|
33 |
|
$allKeys = require ANAX_INSTALL_PATH . "/config/api_keys.php"; |
44
|
33 |
|
$this->apiKey = $allKeys["ipstack"]; |
45
|
33 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get locaation. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
|
53
|
23 |
|
public function getLocation(string $ipAdress) |
54
|
|
|
{ |
55
|
23 |
|
$this->output["ip"] = $ipAdress; |
56
|
|
|
|
57
|
23 |
|
if ($this->ipVerify->ipVerify($ipAdress)) { |
58
|
21 |
|
$geoData = $this->getData($ipAdress); |
59
|
|
|
|
60
|
21 |
|
$this->output["isValid"] = true; |
61
|
21 |
|
$this->output["protocol"] = $this->ipVerify->getIpInfo($ipAdress); |
62
|
21 |
|
$this->output["domain"] = $this->ipVerify->getDomain($ipAdress); |
63
|
21 |
|
if ($geoData["longitude"] && $geoData["latitude"]) { |
64
|
6 |
|
$this->output["map"] = $this->createMapLink($geoData["longitude"], $geoData["latitude"]); |
65
|
6 |
|
$this->output["long"] = $geoData["longitude"]; |
66
|
6 |
|
$this->output["lat"] = $geoData["latitude"]; |
67
|
6 |
|
$this->output["embed"] = $this->createEmbedMap($geoData["longitude"], $geoData["latitude"]); |
68
|
|
|
} else { |
69
|
15 |
|
$this->output["map"] = "Missing"; |
70
|
15 |
|
$this->output["long"] = "Missing"; |
71
|
15 |
|
$this->output["lat"] = "Missing"; |
72
|
|
|
} |
73
|
21 |
|
$this->output["country"] = $geoData["country_name"]; |
74
|
21 |
|
$this->output["city"] = $geoData["city"]; |
75
|
21 |
|
$this->output["flag"] = $geoData["location"]["country_flag"]; |
76
|
|
|
|
77
|
21 |
|
return $this->output; |
78
|
|
|
} else { |
79
|
2 |
|
$this->output["isValid"] = false; |
80
|
2 |
|
return $this->output; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fetch data. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
|
90
|
21 |
|
public function getData(string $ipAdress) |
91
|
|
|
{ |
92
|
21 |
|
$curl = curl_init(); |
93
|
|
|
|
94
|
21 |
|
if ($curl == false) { |
95
|
|
|
return array(); |
96
|
|
|
} |
97
|
21 |
|
curl_setopt($curl, CURLOPT_URL, "http://api.ipstack.com/$ipAdress?access_key=$this->apiKey"); |
98
|
|
|
|
99
|
21 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
100
|
21 |
|
$data = curl_exec($curl); |
101
|
21 |
|
curl_close($curl); |
102
|
|
|
|
103
|
21 |
|
return json_decode($data, true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create map link. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
|
112
|
6 |
|
public function createMapLink(string $long, string $lat) |
113
|
|
|
{ |
114
|
6 |
|
return "https://www.openstreetmap.org/#map=15/$lat/$long"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Create embeded map. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
|
123
|
11 |
|
public function createEmbedMap(string $long, string $lat) |
124
|
|
|
{ |
125
|
11 |
|
$latOffset = 0.01338; |
126
|
11 |
|
$longOffset = 0.01451; |
127
|
11 |
|
$lat1 = $lat + $latOffset; |
128
|
11 |
|
$lat2 = $lat - $latOffset; |
129
|
11 |
|
$long1 = $long + $longOffset; |
130
|
11 |
|
$long2 = $long - $longOffset; |
131
|
11 |
|
$box = "$long1%2C$lat1%2C$long2%2C$lat2"; |
132
|
|
|
|
133
|
11 |
|
return $box; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|