|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blixter\IpGeolocation; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
* Model for IpGeoController |
|
8
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
|
9
|
|
|
*/ |
|
10
|
|
|
class IpGeoModel |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @var string @apiKey Init the procted API key |
|
15
|
|
|
* @var object @curlhandler Init the object |
|
16
|
|
|
* @var string @url Init the url |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $curlhandler; |
|
20
|
|
|
protected $apiKey; |
|
21
|
|
|
protected $url; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* Get and Set the API key |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
12 |
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
// Get the file where they key is stored |
|
32
|
12 |
|
$this->url = "http://api.ipstack.com/"; |
|
33
|
12 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* Set the curlhandler |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function setCurl(object $ch) |
|
42
|
|
|
{ |
|
43
|
12 |
|
$this->curlhandler = $ch; |
|
|
|
|
|
|
44
|
12 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* Set the curlhandler |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public function setUrl(string $url) |
|
53
|
|
|
{ |
|
54
|
6 |
|
$this->url = $url; |
|
55
|
6 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* |
|
59
|
|
|
* Set the ApiKey |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
12 |
|
public function setApi($key) |
|
64
|
|
|
{ |
|
65
|
12 |
|
$this->apiKey = $key; |
|
66
|
12 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Send request to Ip stack with given Ip adress |
|
70
|
|
|
* |
|
71
|
|
|
* @return array with information about the Ip address |
|
72
|
|
|
*/ |
|
73
|
5 |
|
public function fetchData($ipAddress) |
|
74
|
|
|
{ |
|
75
|
5 |
|
$json = true; |
|
76
|
|
|
|
|
77
|
5 |
|
$url = $this->url . $ipAddress . '?access_key=' . $this->apiKey; |
|
78
|
|
|
|
|
79
|
5 |
|
$jsonResponse = $this->curlhandler->curl($url, $json); |
|
80
|
|
|
|
|
81
|
|
|
// Adding MapLink to the JSON response |
|
82
|
5 |
|
$jsonResponse = $this->addMapLink($jsonResponse); |
|
83
|
|
|
|
|
84
|
5 |
|
return $jsonResponse; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Send request to Ip stack with given Ip adress |
|
89
|
|
|
* |
|
90
|
|
|
* @return array with information about the Ip address |
|
91
|
|
|
*/ |
|
92
|
5 |
|
public function addMapLink($json) |
|
93
|
|
|
{ |
|
94
|
5 |
|
$latitude = $json["latitude"] ?? null; |
|
95
|
5 |
|
$longitude = $json["longitude"] ?? null; |
|
96
|
5 |
|
$json["mapLink"] = "https://www.openstreetmap.org/#map=13/$latitude/$longitude"; |
|
97
|
|
|
|
|
98
|
5 |
|
return $json; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get ip from $request->getServer |
|
103
|
|
|
* |
|
104
|
|
|
* @return string with current usrs Ip address |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function getUserIpAddr($request) |
|
107
|
|
|
{ |
|
108
|
2 |
|
if (!empty($request->getServer('HTTP_CLIENT_IP'))) { |
|
109
|
|
|
//ip from share internet |
|
110
|
2 |
|
$ipAddr = $request->getServer('HTTP_CLIENT_IP'); |
|
111
|
1 |
|
} elseif (!empty($request->getServer('HTTP_X_FORWARDED_FOR'))) { |
|
112
|
|
|
//ip pass from proxy |
|
113
|
1 |
|
$ipAddr = $request->getServer('HTTP_X_FORWARDED_FOR'); |
|
114
|
|
|
} else { |
|
115
|
1 |
|
$ipAddr = $request->getServer('REMOTE_ADDR'); |
|
116
|
|
|
} |
|
117
|
2 |
|
return $ipAddr; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..