|
1
|
|
|
<?php |
|
2
|
|
|
namespace Anax\models; |
|
3
|
|
|
|
|
4
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
5
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
6
|
|
|
|
|
7
|
|
|
class IpAdress |
|
8
|
|
|
{ |
|
9
|
|
|
use ContainerInjectableTrait; |
|
10
|
|
|
|
|
11
|
|
|
public $apiKey = "613d9afc10fd24328b5dfbf2528604a7"; |
|
12
|
|
|
public $ipAdress; |
|
13
|
|
|
|
|
14
|
11 |
|
public function __construct($ipAdress = null) |
|
15
|
|
|
{ |
|
16
|
11 |
|
$this->ipAdress = $ipAdress; |
|
17
|
11 |
|
} |
|
18
|
|
|
|
|
19
|
2 |
|
public function getUserIp($request) |
|
20
|
|
|
{ |
|
21
|
2 |
|
if (!empty($request->getServer('HTTP_CLIENT_IP'))) { |
|
22
|
1 |
|
$this->ipAdress = $request->getServer('HTTP_CLIENT_IP'); |
|
23
|
1 |
|
} elseif (!empty($request->getServer('HTTP_X_FORWARDED_FOR'))) { |
|
24
|
|
|
$this->ipAdress = $request->getServer('HTTP_X_FORWARDED_FOR'); |
|
25
|
|
|
} else { |
|
26
|
1 |
|
$this->ipAdress = $request->getServer('REMOTE_ADDR'); |
|
27
|
|
|
} |
|
28
|
2 |
|
return $this->ipAdress; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
7 |
|
public function validateIp() |
|
32
|
|
|
{ |
|
33
|
7 |
|
if (filter_var($this->ipAdress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
34
|
3 |
|
$ipRes = $this->ipAdress . " är en giltig IPv6 adress!"; |
|
35
|
3 |
|
$host = gethostbyaddr($this->ipAdress); |
|
36
|
4 |
|
} elseif (filter_var($this->ipAdress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
37
|
2 |
|
$ipRes = $this->ipAdress . " är en giltig IPv4 adress!"; |
|
38
|
2 |
|
$host = gethostbyaddr($this->ipAdress); |
|
39
|
|
|
} else { |
|
40
|
2 |
|
$ipRes = $this->ipAdress . " är inte en giltig IP adress"; |
|
41
|
2 |
|
$host = null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
7 |
|
$resArray = array($ipRes, $host); |
|
45
|
7 |
|
return $resArray; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function getLocation() |
|
49
|
|
|
{ |
|
50
|
1 |
|
$location = 'http://api.ipstack.com/' . $this->ipAdress . '?access_key=' . $this->apiKey; |
|
51
|
1 |
|
$locationJSON = file_get_contents($location); |
|
52
|
1 |
|
$locationJSONObject = json_decode($locationJSON, true); |
|
53
|
|
|
|
|
54
|
1 |
|
return $locationJSONObject; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|