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; |
|||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
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); |
|||
0 ignored issues
–
show
It seems like
$req can also be of type false ; however, parameter $ch of curl_setopt() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
75 | 3 | $json = curl_exec($req); |
|||
0 ignored issues
–
show
It seems like
$req can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
76 | 3 | curl_close($req); |
|||
0 ignored issues
–
show
It seems like
$req can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
77 | 3 | $result = json_decode($json, 'JAON_PRETTY_PRINT'); |
|||
0 ignored issues
–
show
'JAON_PRETTY_PRINT' of type string is incompatible with the type boolean expected by parameter $assoc of json_decode() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
78 | 3 | return $result; |
|||
79 | } |
||||
80 | } |
||||
81 |