1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validator |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view source file |
10
|
|
|
* that is bundled with this package in the file LICENSE |
11
|
|
|
* |
12
|
|
|
* @author Marcin Pudełek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Created by Marcin. |
17
|
|
|
* Date: 01.10.2017 |
18
|
|
|
* Time: 13:23 |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace mrcnpdlk\Validator\Types; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
use mrcnpdlk\Validator\Exception; |
25
|
|
|
use mrcnpdlk\Validator\TypeInterface; |
26
|
|
|
|
27
|
|
|
class IPv4 extends TypeAbstract implements TypeInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param mixed $checkedValue |
31
|
|
|
* @param bool $asEx |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
* @throws Exception |
35
|
|
|
*/ |
36
|
4 |
|
public static function isValid($checkedValue, bool $asEx = false): bool |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
4 |
|
if (is_int($checkedValue)) { |
40
|
1 |
|
$sIp = long2ip($checkedValue); |
41
|
4 |
|
} elseif (is_string($checkedValue)) { |
42
|
4 |
|
$sIp = static::clean($checkedValue); |
43
|
|
|
} else { |
44
|
|
|
throw new \Exception(sprintf('Invalid input argument type [%s]', gettype($checkedValue))); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if (filter_var($sIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) === false) { |
48
|
1 |
|
throw new \Exception(sprintf('Filter error!')); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
return true; |
52
|
1 |
|
} catch (\Exception $e) { |
53
|
1 |
|
if ($asEx) { |
54
|
1 |
|
throw new Exception(sprintf("Invalid IPv4 address [%s], reason: %s", $checkedValue, $e->getMessage())); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $checkedValue |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
4 |
|
public static function clean($checkedValue) |
68
|
|
|
{ |
69
|
4 |
|
if (is_string($checkedValue)) { |
70
|
|
|
//remove leading zeros from IP address |
71
|
4 |
|
$sIp = preg_replace('/\b0+\B/', '', $checkedValue); |
72
|
1 |
|
} elseif (is_int($checkedValue)) { |
73
|
1 |
|
$sIp = long2ip($checkedValue); |
74
|
|
|
} else { |
75
|
|
|
throw new Exception(sprintf('Invalid input argument type [%s]', gettype($checkedValue))); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return $sIp; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return IPv4 addres with leading zeros |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
2 |
|
public function getLeadingZeros() |
87
|
|
|
{ |
88
|
2 |
|
$parts = explode('.', $this->get()); |
89
|
2 |
|
foreach ($parts as &$part) { |
90
|
2 |
|
$part = str_pad($part, 3, '0', \STR_PAD_LEFT); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
return implode('.', $parts); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return IPv4 address as int representation |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getAsInt() |
102
|
|
|
{ |
103
|
|
|
return ip2long($this->get()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if IPv4 address is local |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
1 |
|
public function isLocalIPAddress() |
112
|
|
|
{ |
113
|
1 |
|
if (strpos($this->get(), '127.0.') === 0) { |
114
|
1 |
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return (!filter_var($this->get(), \FILTER_VALIDATE_IP, \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|