1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.4.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Validation\Rules; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Captcha\CaptchaManager; |
18
|
|
|
use ReCaptcha\RequestMethod\CurlPost; |
|
|
|
|
19
|
|
|
use Quantum\Factory\ModelFactory; |
20
|
|
|
use Quantum\Di\Di; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Trait General |
24
|
|
|
* @package Quantum\Libraries\Validation |
25
|
|
|
*/ |
26
|
|
|
trait General |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Adds validation Error |
31
|
|
|
* @param string $field |
32
|
|
|
* @param string $rule |
33
|
|
|
* @param mixed|null $param |
34
|
|
|
*/ |
35
|
|
|
abstract protected function addError(string $field, string $rule, $param = null); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Checks Field Required |
39
|
|
|
* @param string $field |
40
|
|
|
* @param string $value |
41
|
|
|
* @param null|mixed $param |
42
|
|
|
*/ |
43
|
|
|
protected function required(string $field, string $value, $param = null) |
44
|
|
|
{ |
45
|
|
|
if ($value == false || empty($value)) { |
|
|
|
|
46
|
|
|
$this->addError($field, 'required', $param); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks Email |
52
|
|
|
* @param string $field |
53
|
|
|
* @param string $value |
54
|
|
|
* @param null|mixed $param |
55
|
|
|
*/ |
56
|
|
|
protected function email(string $field, string $value, $param = null) |
57
|
|
|
{ |
58
|
|
|
if (!empty($value)) { |
59
|
|
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
60
|
|
|
$this->addError($field, 'email', $param); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check Captcha |
67
|
|
|
* @param string $field |
68
|
|
|
* @param string $value |
69
|
|
|
* @param null|mixed $param |
70
|
|
|
*/ |
71
|
|
|
protected function captcha(string $field, string $value, $param = null) |
72
|
|
|
{ |
73
|
|
|
if (!empty($value)) { |
74
|
|
|
$captcha = CaptchaManager::getCaptcha(); |
75
|
|
|
if (!$captcha->verifyResponse($value)){ |
76
|
|
|
$this->addError($field, 'captcha', $param); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks for a valid credit card number |
83
|
|
|
* @param string $field |
84
|
|
|
* @param string $value |
85
|
|
|
* @param null|mixed $param |
86
|
|
|
*/ |
87
|
|
|
protected function creditCard(string $field, string $value, $param = null) |
88
|
|
|
{ |
89
|
|
|
if (!empty($value)) { |
90
|
|
|
$number = preg_replace('/\D/', '', $value); |
91
|
|
|
|
92
|
|
|
if (function_exists('mb_strlen')) { |
93
|
|
|
$number_length = mb_strlen($number); |
94
|
|
|
} else { |
95
|
|
|
$number_length = strlen($number); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($number_length == 0) { |
99
|
|
|
$this->addError($field, 'creditCard', $param); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$parity = $number_length % 2; |
103
|
|
|
|
104
|
|
|
$total = 0; |
105
|
|
|
|
106
|
|
|
for ($i = 0; $i < $number_length; ++$i) { |
107
|
|
|
$digit = $number[$i]; |
108
|
|
|
|
109
|
|
|
if ($i % 2 == $parity) { |
110
|
|
|
$digit *= 2; |
111
|
|
|
|
112
|
|
|
if ($digit > 9) { |
113
|
|
|
$digit -= 9; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$total += $digit; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($total % 10 != 0) { |
121
|
|
|
$this->addError($field, 'creditCard', $param); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Checks for a valid IBAN |
128
|
|
|
* @param string $field |
129
|
|
|
* @param string $value |
130
|
|
|
* @param null|mixed $param |
131
|
|
|
*/ |
132
|
|
|
protected function iban(string $field, string $value, $param = null) |
133
|
|
|
{ |
134
|
|
|
if (!empty($value)) { |
135
|
|
|
static $character = [ |
136
|
|
|
'A' => 10, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, |
137
|
|
|
'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, |
138
|
|
|
'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, |
139
|
|
|
'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, |
140
|
|
|
'Z' => 35, 'B' => 11, |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
if (!preg_match("/\A[A-Z]{2}\d{2} ?[A-Z\d]{4}( ?\d{4}){1,} ?\d{1,4}\z/", $value)) { |
144
|
|
|
$this->addError($field, 'iban', $param); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$iban = str_replace(' ', '', $value); |
148
|
|
|
$iban = substr($iban, 4) . substr($iban, 0, 4); |
149
|
|
|
$iban = strtr($iban, $character); |
150
|
|
|
|
151
|
|
|
if (bcmod($iban, 97) != 1) { |
152
|
|
|
$this->addError($field, 'iban', $param); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Checks for a valid format human name |
161
|
|
|
* @param string $field |
162
|
|
|
* @param string $value |
163
|
|
|
* @param null|mixed $param |
164
|
|
|
*/ |
165
|
|
|
protected function name(string $field, string $value, $param = null) |
166
|
|
|
{ |
167
|
|
|
if (!empty($value)) { |
168
|
|
|
if (preg_match("/^([a-z \p{L} '-])+$/i", $value) === 0) { |
169
|
|
|
$this->addError($field, 'name', $param); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Checks that the provided string is a likely street address. |
176
|
|
|
* @param string $field |
177
|
|
|
* @param string $value |
178
|
|
|
* @param null|mixed $param |
179
|
|
|
*/ |
180
|
|
|
protected function streetAddress(string $field, string $value, $param = null) |
181
|
|
|
{ |
182
|
|
|
if (!empty($value)) { |
183
|
|
|
$hasLetter = preg_match('/[a-zA-Z]/', $value); |
184
|
|
|
$hasDigit = preg_match('/\d/', $value); |
185
|
|
|
$hasSpace = preg_match('/\s/', $value); |
186
|
|
|
|
187
|
|
|
$passes = $hasLetter && $hasDigit && $hasSpace; |
188
|
|
|
|
189
|
|
|
if (!$passes) { |
190
|
|
|
$this->addError($field, 'streetAddress', $param); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Validates the phone number // 555-555-5555 , 5555425555, 555 555 5555, 1(519) 555-4444, 1 (519) 555-4422, +1-555-555-5555 |
197
|
|
|
* @param string $field |
198
|
|
|
* @param string $value |
199
|
|
|
* @param null|mixed $param |
200
|
|
|
*/ |
201
|
|
|
protected function phoneNumber(string $field, string $value, $param = null) |
202
|
|
|
{ |
203
|
|
|
if (!empty($value)) { |
204
|
|
|
$regex = '/^(\+*\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i'; |
205
|
|
|
|
206
|
|
|
if (!preg_match($regex, $value)) { |
207
|
|
|
$this->addError($field, 'phoneNumber', $param); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Determines if the provided input is a valid date |
214
|
|
|
* @param string $field |
215
|
|
|
* @param string $value |
216
|
|
|
* @param string|null $param format |
217
|
|
|
*/ |
218
|
|
|
protected function date(string $field, string $value, string $param = null) |
219
|
|
|
{ |
220
|
|
|
if (!empty($value)) { |
221
|
|
|
if (!$param) { |
222
|
|
|
$cdate1 = date('Y-m-d', strtotime($value)); |
223
|
|
|
$cdate2 = date('Y-m-d H:i:s', strtotime($value)); |
224
|
|
|
|
225
|
|
|
if ($cdate1 != $value && $cdate2 != $value) { |
226
|
|
|
$this->addError($field, 'date', $param); |
227
|
|
|
} |
228
|
|
|
} else { |
229
|
|
|
$date = \DateTime::createFromFormat($param, $value); |
230
|
|
|
|
231
|
|
|
if ($date === false || $value != date($param, $date->getTimestamp())) { |
232
|
|
|
$this->addError($field, 'date', $param); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Ensures the value starts with a certain character / set of character |
240
|
|
|
* @param string $field |
241
|
|
|
* @param string $value |
242
|
|
|
* @param string $param |
243
|
|
|
*/ |
244
|
|
|
protected function starts(string $field, string $value, string $param) |
245
|
|
|
{ |
246
|
|
|
if (!empty($value)) { |
247
|
|
|
if (strpos($value, $param) !== 0) { |
248
|
|
|
$this->addError($field, 'starts', $param); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Custom regex validator |
255
|
|
|
* @param string $field |
256
|
|
|
* @param string $value |
257
|
|
|
* @param string $param |
258
|
|
|
*/ |
259
|
|
|
protected function regex(string $field, string $value, string $param) |
260
|
|
|
{ |
261
|
|
|
if (!empty($value)) { |
262
|
|
|
if (!preg_match($param, $value)) { |
263
|
|
|
$this->addError($field, 'regex', $param); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Validates JSON string |
270
|
|
|
* @param string $field |
271
|
|
|
* @param string $value |
272
|
|
|
* @param null|mixed $param |
273
|
|
|
*/ |
274
|
|
|
protected function jsonString(string $field, string $value, $param = null) |
275
|
|
|
{ |
276
|
|
|
if (!empty($value)) { |
277
|
|
|
$value = htmlspecialchars_decode($value, ENT_QUOTES); |
278
|
|
|
|
279
|
|
|
if (!is_object(json_decode($value))) { |
280
|
|
|
$this->addError($field, 'jsonString', $param); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Validates same value for both fields |
287
|
|
|
* @param string $field |
288
|
|
|
* @param string $value |
289
|
|
|
* @param null|mixed $param |
290
|
|
|
*/ |
291
|
|
|
protected function same(string $field, string $value, $param = null) |
292
|
|
|
{ |
293
|
|
|
if (!empty($value)) { |
294
|
|
|
if ($value != $this->data[$param]) { |
295
|
|
|
$this->addError($field, 'same', $param); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Validates uniqueness |
302
|
|
|
* @param string $field |
303
|
|
|
* @param mixed $value |
304
|
|
|
* @param string $param |
305
|
|
|
* @throws \Quantum\Exceptions\DiException |
306
|
|
|
*/ |
307
|
|
|
protected function unique(string $field, $value, string $param) |
308
|
|
|
{ |
309
|
|
|
if (!empty($value)) { |
310
|
|
|
$model = ModelFactory::get(ucfirst($param)); |
311
|
|
|
|
312
|
|
|
$row = $model->findOneBy($field, $value); |
313
|
|
|
|
314
|
|
|
if ($row->count()) { |
315
|
|
|
$this->addError($field, 'unique', null); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths