1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hab\MeModule; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
class ValidateIP implements ContainerInjectableInterface |
9
|
|
|
{ |
10
|
|
|
use ContainerInjectableTrait; |
11
|
|
|
private $ip; |
12
|
|
|
private $type; |
13
|
|
|
|
14
|
16 |
|
public function __construct($ip = "") |
15
|
|
|
{ |
16
|
16 |
|
$this->ip = $ip; |
17
|
16 |
|
$this->type = "unknown"; |
18
|
16 |
|
} |
19
|
|
|
|
20
|
7 |
|
public function setIP(String $ip = "") : void |
21
|
|
|
{ |
22
|
7 |
|
$this->ip = $ip; |
23
|
7 |
|
} |
24
|
|
|
|
25
|
16 |
|
public function getIP() : String |
26
|
|
|
{ |
27
|
16 |
|
return $this->ip; |
28
|
|
|
} |
29
|
|
|
|
30
|
11 |
|
public function setType(String $type) |
31
|
|
|
{ |
32
|
11 |
|
$this->type = $type; |
33
|
11 |
|
} |
34
|
|
|
|
35
|
16 |
|
public function getType() : String |
36
|
|
|
{ |
37
|
16 |
|
return $this->type; |
38
|
|
|
} |
39
|
|
|
|
40
|
16 |
|
public function validateIPV4(String $ip) |
41
|
|
|
{ |
42
|
16 |
|
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
public function validateIPV6(String $ip) |
46
|
|
|
{ |
47
|
9 |
|
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
public function getHost() |
51
|
|
|
{ |
52
|
11 |
|
return gethostbyaddr($this->ip); |
53
|
|
|
} |
54
|
|
|
|
55
|
16 |
|
public function validate() |
56
|
|
|
{ |
57
|
16 |
|
if ($this->validateIPV4($this->ip)) { |
58
|
8 |
|
$this->setType("ipv4"); |
59
|
8 |
|
return true; |
60
|
|
|
} |
61
|
9 |
|
if ($this->validateIPV6($this->ip)) { |
62
|
3 |
|
$this->setType("ipv6"); |
63
|
3 |
|
return true; |
64
|
|
|
} |
65
|
6 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
16 |
|
public function sendRes() |
69
|
|
|
{ |
70
|
16 |
|
$res = []; |
71
|
16 |
|
if (!$this->validate()) { |
72
|
6 |
|
$res["text"] = $this->ip . " is not a valid format"; |
73
|
6 |
|
$res["isValid"] = false; |
74
|
|
|
} else { |
75
|
11 |
|
$res["isValid"] = true; |
76
|
11 |
|
$res["text"] = $this->ip . " is a valid " . $this->getType() . " adress"; |
77
|
11 |
|
$res["host"] = $this->getHost(); |
78
|
|
|
} |
79
|
16 |
|
$res["ip"] = $this->getIP(); |
80
|
16 |
|
$res["type"] = $this->getType(); |
81
|
16 |
|
return $res; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// public function multiValidate(Array $ipArray = []) |
85
|
|
|
// { |
86
|
|
|
// $temp = []; |
87
|
|
|
// foreach ($ipArray as $ip) { |
88
|
|
|
// array_push($temp, ltrim($this->sendRes($ip))); |
89
|
|
|
// } |
90
|
|
|
// return $temp; |
91
|
|
|
// } |
92
|
|
|
} |
93
|
|
|
|