-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Ip.zep
144 lines (124 loc) · 3.98 KB
/
Ip.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Validation\Validator;
use Phalcon\Validation;
use Phalcon\Validation\AbstractValidator;
use Phalcon\Messages\Message;
/**
* Check for IP addresses
*
* ```php
* use Phalcon\Validation\Validator\Ip as IpValidator;
*
* $validator->add(
* "ip_address",
* new IpValidator(
* [
* "message" => ":field must contain only ip addresses",
* "version" => IP::VERSION_4 | IP::VERSION_6, // v6 and v4. The same if not specified
* "allowReserved" => false, // False if not specified. Ignored for v6
* "allowPrivate" => false, // False if not specified
* "allowEmpty" => false,
* ]
* )
* );
*
* $validator->add(
* [
* "source_address",
* "destination_address",
* ],
* new IpValidator(
* [
* "message" => [
* "source_address" => "source_address must be a valid IP address",
* "destination_address" => "destination_address must be a valid IP address",
* ],
* "version" => [
* "source_address" => Ip::VERSION_4 | IP::VERSION_6,
* "destination_address" => Ip::VERSION_4,
* ],
* "allowReserved" => [
* "source_address" => false,
* "destination_address" => true,
* ],
* "allowPrivate" => [
* "source_address" => false,
* "destination_address" => true,
* ],
* "allowEmpty" => [
* "source_address" => false,
* "destination_address" => true,
* ],
* ]
* )
* );
* ```
*/
class Ip extends AbstractValidator
{
const VERSION_4 = FILTER_FLAG_IPV4;
const VERSION_6 = FILTER_FLAG_IPV6;
protected template = "Field :field must be a valid IP address";
/**
* Constructor
*
* @param array options = [
* 'message' => '',
* 'template' => '',
* 'allowPrivate' => false,
* 'allowReserved' => false,
* 'allowEmpty' => false
* ]
*/
public function __construct(array! options = [])
{
parent::__construct(options);
}
/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, version, allowPrivate, allowReserved, allowEmpty, options;
let value = validation->getValue(field),
version = this->getOption("version", FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
if typeof version == "array" {
let version = version[field];
}
let allowPrivate = this->getOption("allowPrivate") ? 0 : FILTER_FLAG_NO_PRIV_RANGE;
if typeof allowPrivate == "array" {
let allowPrivate = allowPrivate[field];
}
let allowReserved = this->getOption("allowReserved") ? 0 : FILTER_FLAG_NO_RES_RANGE;
if typeof allowReserved == "array" {
let allowReserved = allowReserved[field];
}
let allowEmpty = this->getOption("allowEmpty", false);
if typeof allowEmpty == "array" {
let allowEmpty = isset allowEmpty[field] ? allowEmpty[field] : false;
}
if allowEmpty && empty value {
return true;
}
let options = [
"options": [
"default": false
],
"flags": version | allowPrivate | allowReserved
];
if !filter_var(value, FILTER_VALIDATE_IP, options) {
validation->appendMessage(
this->messageFactory(validation, field)
);
return false;
}
return true;
}
}