|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Moody\ControllerIP; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
14
|
|
|
* The controller will be injected with $di if implementing the interface |
|
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
17
|
|
|
* requests for that mount point. |
|
18
|
|
|
* |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class IpValidate implements ContainerInjectableInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ContainerInjectableTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Check if IP is valid or not. |
|
31
|
|
|
* GET ip |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
6 |
|
public function getProtocol($ipAddress) |
|
37
|
|
|
{ |
|
38
|
6 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
39
|
4 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
40
|
3 |
|
return "IPv4"; |
|
41
|
|
|
} |
|
42
|
1 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
43
|
1 |
|
return "IPv6"; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
2 |
|
return false; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
/** |
|
49
|
|
|
* Check if IP is valid or not. |
|
50
|
|
|
* GET domain |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function getDomain($ipAddress) |
|
55
|
|
|
{ |
|
56
|
3 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
57
|
1 |
|
return gethostbyaddr($ipAddress); |
|
58
|
|
|
} |
|
59
|
2 |
|
return "Not valid"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
public function getCurrentIp() |
|
63
|
|
|
{ |
|
64
|
2 |
|
return $_SERVER["193.11.187.229"] ?? '193.11.187.229'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
3 |
|
public function getAddress($ipAddress) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$the_access_key = "9ffbd83fca588b355bff399b8da7526f"; |
|
72
|
3 |
|
$theUrl = "http://api.ipstack.com/"; |
|
73
|
3 |
|
$req = curl_init($theUrl . $ipAddress . "?access_key=" . $the_access_key . ''); |
|
74
|
3 |
|
curl_setopt($req, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
|
|
75
|
3 |
|
$json = curl_exec($req); |
|
|
|
|
|
|
76
|
3 |
|
curl_close($req); |
|
|
|
|
|
|
77
|
3 |
|
$result = json_decode($json, 'JAON_PRETTY_PRINT'); |
|
|
|
|
|
|
78
|
3 |
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|