1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace artes\IP; |
4
|
|
|
|
5
|
|
|
use artes\IP\IP; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A class for validating IP addresses. |
9
|
|
|
* |
10
|
|
|
* @SuppressWarnings(PHPMD) |
11
|
|
|
*/ |
12
|
|
|
class IPCheck |
13
|
|
|
{ |
14
|
|
|
private $userinput; |
15
|
|
|
private $correctedinput; |
16
|
|
|
private $domain; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor to initiate an IP object, |
20
|
|
|
* |
21
|
|
|
* @param string $userinput |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
11 |
|
public function __construct(string $userinput) |
25
|
|
|
{ |
26
|
11 |
|
$this->userinput = $userinput; |
27
|
11 |
|
$this->correctedinput = ""; |
28
|
11 |
|
$this->domain = ""; |
29
|
11 |
|
$this->setDomainName(); |
30
|
11 |
|
$this->input2ip6(); |
31
|
11 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function validip() : bool |
34
|
|
|
{ |
35
|
1 |
|
if ($this->ipv4() || $this->ipv6()) { |
36
|
1 |
|
return true; |
37
|
|
|
} |
38
|
1 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
11 |
|
public function ipv4() : bool |
42
|
|
|
{ |
43
|
11 |
|
$ipv4 = "/^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/"; |
44
|
|
|
|
45
|
11 |
|
if (preg_match($ipv4, $this->userinput)) { |
46
|
6 |
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
public function ipv6() : bool |
53
|
|
|
{ |
54
|
10 |
|
$ipv6 = "/^(([0-9]|[a-f]|[A-F]){4}:){7}([0-9]|[a-f]|[A-F]){4}$/"; |
55
|
|
|
|
56
|
10 |
|
if (preg_match($ipv6, $this->correctedinput)) { |
57
|
6 |
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
10 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// private function corrected($ipinput): array |
64
|
|
|
// { |
65
|
|
|
// $newip6 = []; |
66
|
|
|
// if ($ipinput) { |
67
|
|
|
// $mymy = explode(":", $ipinput); |
68
|
|
|
// if ($mymy[0] === "") { |
69
|
|
|
// array_shift($mymy); |
70
|
|
|
// } elseif ($mymy[count($mymy) -1] === "") { |
71
|
|
|
// array_pop($mymy); |
72
|
|
|
// } |
73
|
|
|
// $mycount = count($mymy); |
74
|
|
|
// $missing = 8 - $mycount; // IPv6 has eight 16bit blocks |
75
|
|
|
// for ($i=0; $i < $mycount; $i++) { |
76
|
|
|
// array_push($newip6, str_pad($mymy[$i], 4, "0", STR_PAD_LEFT)); |
77
|
|
|
// if ($mymy[$i] === "") { |
78
|
|
|
// for ($j=0; $j < $missing; $j++) { |
79
|
|
|
// array_push($newip6, str_pad($mymy[$i], 4, "0", STR_PAD_LEFT)); |
80
|
|
|
// } |
81
|
|
|
// } |
82
|
|
|
// } |
83
|
|
|
// } |
84
|
|
|
// return $newip6; |
85
|
|
|
// } |
86
|
|
|
|
87
|
11 |
|
private function input2ip6() : string |
88
|
|
|
{ |
89
|
11 |
|
$ipclass = new IP(); |
90
|
11 |
|
$newip6 = $ipclass->corrected($this->userinput); |
91
|
|
|
// $newip6 = $this->corrected($this->userinput); |
92
|
|
|
// $newip6 = []; |
93
|
|
|
// if ($this->userinput) { |
94
|
|
|
// $mymy = explode(":", $this->userinput); |
95
|
|
|
// if ($mymy[0] === "") { |
96
|
|
|
// array_shift($mymy); |
97
|
|
|
// } elseif ($mymy[count($mymy) -1] === "") { |
98
|
|
|
// array_pop($mymy); |
99
|
|
|
// } |
100
|
|
|
// $mycount = count($mymy); |
101
|
|
|
// $missing = 8 - $mycount; // IPv6 has eight 16bit blocks |
102
|
|
|
// for ($i=0; $i < $mycount; $i++) { |
103
|
|
|
// array_push($newip6, str_pad($mymy[$i], 4, "0", STR_PAD_LEFT)); |
104
|
|
|
// if ($mymy[$i] === "") { |
105
|
|
|
// for ($j=0; $j < $missing; $j++) { |
106
|
|
|
// array_push($newip6, str_pad($mymy[$i], 4, "0", STR_PAD_LEFT)); |
107
|
|
|
// } |
108
|
|
|
// } |
109
|
|
|
// } |
110
|
|
|
// } |
111
|
11 |
|
$newip6str = implode(":", $newip6); |
112
|
11 |
|
$this->correctedinput = $newip6str; |
113
|
11 |
|
return $this->correctedinput; |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
public function getUserInput() : string |
117
|
|
|
{ |
118
|
5 |
|
return $this->userinput; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
public function getCorrectedInput() : string |
122
|
|
|
{ |
123
|
3 |
|
return $this->correctedinput; |
124
|
|
|
} |
125
|
|
|
|
126
|
11 |
|
public function setDomainName() : void |
127
|
|
|
{ |
128
|
11 |
|
if ($this->ipv4()) { |
129
|
6 |
|
if ($this->userinput != gethostbyaddr($this->userinput)) { |
130
|
6 |
|
$this->domain = gethostbyaddr($this->userinput); |
131
|
|
|
} |
132
|
8 |
|
} elseif ($this->ipv6()) { |
133
|
1 |
|
if ($this->correctedinput != gethostbyaddr($this->correctedinput)) { |
134
|
1 |
|
$this->domain = gethostbyaddr($this->correctedinput); |
135
|
|
|
} |
136
|
|
|
} |
137
|
11 |
|
} |
138
|
|
|
|
139
|
5 |
|
public function getDomainName() : string |
140
|
|
|
{ |
141
|
5 |
|
return $this->domain; |
142
|
|
|
} |
143
|
|
|
|
144
|
5 |
|
public function printIPMessage() : string |
145
|
|
|
{ |
146
|
5 |
|
if ($this->ipv4()) { |
147
|
4 |
|
$lastChunk = "enligt IPv4."; |
148
|
2 |
|
} elseif ($this->ipv6()) { |
149
|
2 |
|
$lastChunk = "enligt IPv6."; |
150
|
|
|
} else { |
151
|
1 |
|
$lastChunk = "inte."; |
152
|
|
|
} |
153
|
5 |
|
$ip = $this->getUserInput(); |
154
|
5 |
|
return "Den inmatade strängen ($ip) validerar $lastChunk"; |
155
|
|
|
} |
156
|
|
|
|
157
|
5 |
|
public function printDomainMessage() : string |
158
|
|
|
{ |
159
|
5 |
|
$msg = ""; |
160
|
5 |
|
if ($this->getDomainName()) { |
161
|
4 |
|
$msg = "Det tillhörande domännamnet är " . $this->getDomainName() . "."; |
162
|
2 |
|
} elseif ($this->ipv4() || $this->ipv6()) { |
163
|
2 |
|
$msg = "Men inget domännamn har hittats."; |
164
|
|
|
} else { |
165
|
1 |
|
$msg = "Det finns inget domännamn att visa."; |
166
|
|
|
} |
167
|
5 |
|
return $msg; |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
public function printAllMessages() : string |
171
|
|
|
{ |
172
|
2 |
|
if ($this->userinput) { |
173
|
2 |
|
$msg = "<h2>Resultat</h2><p>" . $this->printIPMessage() . "</p><p>" . $this->printDomainMessage() . "</p>"; |
174
|
|
|
} else { |
175
|
2 |
|
$msg = ""; |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
return $msg; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|