|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Validation class |
|
5
|
|
|
* |
|
6
|
|
|
* A small library for validation. |
|
7
|
|
|
* It has plenty of validation rules. |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
10
|
|
|
* @author Omar El Gabry <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Validation { |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* validation errors |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $errors = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Custom rule messages. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $ruleMessages = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Start the validation using values and rules passed in $data |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $data |
|
34
|
|
|
* @param bool $skip To skip validations as soon as one of the rules fails. |
|
35
|
|
|
* |
|
36
|
|
|
* @throws Exception if rule method doesn't exist |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function validate($data, $skip = false){ |
|
40
|
|
|
|
|
41
|
|
|
$passed = true; |
|
42
|
|
|
|
|
43
|
|
|
foreach($data as $placeholder => $rules){ |
|
44
|
|
|
|
|
45
|
|
|
$value = $rules[0]; |
|
46
|
|
|
$rules = explode('|', $rules[1]); |
|
47
|
|
|
|
|
48
|
|
|
// no need to validate the value if the value is empty and not required |
|
49
|
|
|
if(!$this->isRequired($rules) && $this->isEmpty($value)){ |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// it doesn't make sense to continue and validate the rest of rules on an empty & required value. |
|
54
|
|
|
// instead add error, and skip this value. |
|
55
|
|
|
if($this->isRequired($rules) && $this->isEmpty($value)){ |
|
56
|
|
|
$this->addError("required", $placeholder, $value); |
|
57
|
|
|
$passed = false; |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach($rules as $rule){ |
|
62
|
|
|
|
|
63
|
|
|
$method = $rule; |
|
64
|
|
|
$args = []; |
|
65
|
|
|
|
|
66
|
|
|
// if it was empty and required or not required, |
|
67
|
|
|
// it would be detected by the previous ifs |
|
68
|
|
|
if($rule === "required") { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if(self::isruleHasArgs($rule)){ |
|
73
|
|
|
|
|
74
|
|
|
// get arguments for rules like in max(), min(), ..etc. |
|
75
|
|
|
$method = $this->getRuleName($rule); |
|
76
|
|
|
$args = $this->getRuleArgs($rule); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if(!method_exists($this, $method)){ |
|
80
|
|
|
throw new Exception("Method doesnt exists: " . $method); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if(!call_user_func_array([$this, $method], [$value, $args])) { |
|
84
|
|
|
|
|
85
|
|
|
$this->addError($method, $placeholder, $value, $args); |
|
86
|
|
|
$passed = false; |
|
87
|
|
|
|
|
88
|
|
|
if($skip){ return false; } |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// possible change is to return the current validation object, |
|
94
|
|
|
// and use passes() instead. |
|
|
|
|
|
|
95
|
|
|
return $passed; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine if a given value is empty, |
|
100
|
|
|
* excluding '0', false, 0, 0.0, and files uploaded with UPLOAD_ERR_NO_FILE error, |
|
101
|
|
|
* because these could be perfectly valid values, |
|
102
|
|
|
* then, the validation methods has to decide if this value is valid or not. |
|
103
|
|
|
* |
|
104
|
|
|
* @param mixed $value |
|
105
|
|
|
* @return bool |
|
106
|
|
|
* |
|
107
|
|
|
*/ |
|
108
|
|
|
private function isEmpty($value){ |
|
109
|
|
|
|
|
110
|
|
|
if(is_null($value)) { |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
else if(is_string($value)){ |
|
114
|
|
|
if(trim($value) === '') return true; |
|
115
|
|
|
} |
|
116
|
|
|
else if (empty($value) && $value !== '0' && $value !== false && $value !== 0 && $value !== 0.0){ |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
else if (is_array($value) && isset($value['name'], $value['type'], $value['tmp_name'], $value['error'])) { |
|
120
|
|
|
return (int)$value['error'] === UPLOAD_ERR_NO_FILE; |
|
121
|
|
|
} |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Determine if a given rules has 'required' rule |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $rules |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
private function isRequired($rules){ |
|
132
|
|
|
return in_array("required", $rules, true); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Determine if a given rule has arguments, Ex: max(4) |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $rule |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
private function isruleHasArgs($rule) { |
|
142
|
|
|
return isset(explode('(', $rule)[1]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* get rule name for rules that have args |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $rule |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
private function getRuleName($rule){ |
|
152
|
|
|
return explode('(', $rule)[0]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* get arguments for rules that have args |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $rule |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
private function getRuleArgs($rule){ |
|
162
|
|
|
|
|
163
|
|
|
$argsWithBracketAtTheEnd = explode('(', $rule)[1]; |
|
164
|
|
|
$args = rtrim($argsWithBracketAtTheEnd, ')'); |
|
165
|
|
|
$args = preg_replace('/\s+/', '', $args); |
|
166
|
|
|
|
|
167
|
|
|
// as result of an empty array coming from user input |
|
168
|
|
|
// $args will be empty string, |
|
169
|
|
|
// So, using explode(',', empty string) will return array with size = 1 |
|
170
|
|
|
// return empty($args)? []: explode(',', $args); |
|
|
|
|
|
|
171
|
|
|
return explode(',', $args); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Add a custom rule message. |
|
176
|
|
|
* This message will be displayed instead of default. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $rule |
|
179
|
|
|
* @param string $message |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function addRuleMessage($rule, $message){ |
|
183
|
|
|
$this->ruleMessages[$rule] = $message; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Add an error |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $rule |
|
190
|
|
|
* @param string $placeholder for field |
|
191
|
|
|
* @param mixed $value |
|
192
|
|
|
* @param array $args |
|
193
|
|
|
* |
|
194
|
|
|
*/ |
|
195
|
|
|
private function addError($rule, $placeholder, $value, $args = []){ |
|
196
|
|
|
|
|
197
|
|
|
if(isset($this->ruleMessages[$rule])){ |
|
198
|
|
|
$this->errors[] = $this->ruleMessages[$rule]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
else{ |
|
202
|
|
|
|
|
203
|
|
|
// get the default message for the current $rule |
|
204
|
|
|
$message = self::defaultMessages($rule); |
|
205
|
|
|
|
|
206
|
|
|
if(isset($message)){ |
|
207
|
|
|
|
|
208
|
|
|
// if $message is set to empty string, |
|
209
|
|
|
// this means the error will be added inside the validation method itself |
|
210
|
|
|
// check attempts() |
|
211
|
|
|
if(trim($message) !== ""){ |
|
212
|
|
|
|
|
213
|
|
|
// replace placeholder, value, arguments with their values |
|
214
|
|
|
$replace = ['{placeholder}', '{value}']; |
|
215
|
|
|
$value = is_string($value)? $value: ""; |
|
216
|
|
|
$with = array_merge([$placeholder, $value], $args); |
|
217
|
|
|
$count = count($args); |
|
218
|
|
|
|
|
219
|
|
|
// arguments will take the shape of: {0} {1} {2} ... |
|
|
|
|
|
|
220
|
|
|
for($i = 0; $i < $count; $i++) $replace[] = "{{$i}}"; |
|
221
|
|
|
|
|
222
|
|
|
$this->errors[] = str_replace($replace, $with, $message); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
} else{ |
|
226
|
|
|
|
|
227
|
|
|
// if no message defined, then use this one. |
|
228
|
|
|
$this->errors[] = "The value you entered for " . $placeholder . " is invalid"; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Checks if validation has passed. |
|
235
|
|
|
* |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
public function passes(){ |
|
239
|
|
|
return empty($this->errors); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* get all errors |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
public function errors(){ |
|
248
|
|
|
return $this->errors; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* clear all existing errors |
|
253
|
|
|
* |
|
254
|
|
|
* @return bool |
|
255
|
|
|
*/ |
|
256
|
|
|
public function clearErrors(){ |
|
257
|
|
|
$this->errors = []; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** *********************************************** **/ |
|
261
|
|
|
/** ************** Validations ************** **/ |
|
262
|
|
|
/** *********************************************** **/ |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Is value not empty? |
|
266
|
|
|
* |
|
267
|
|
|
* @param mixed $value |
|
268
|
|
|
* @return bool |
|
269
|
|
|
*/ |
|
270
|
|
|
/*private function required($value){ |
|
|
|
|
|
|
271
|
|
|
return !$this->isEmpty($value); |
|
272
|
|
|
}*/ |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* min string length |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $str |
|
278
|
|
|
* @param array $args(min) |
|
|
|
|
|
|
279
|
|
|
* |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
private function minLen($str, $args){ |
|
283
|
|
|
return mb_strlen($str, 'UTF-8') >= (int)$args[0]; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* max string length |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $str |
|
290
|
|
|
* @param array $args(max) |
|
|
|
|
|
|
291
|
|
|
* |
|
292
|
|
|
* @return bool |
|
293
|
|
|
*/ |
|
294
|
|
|
private function maxLen($str, $args){ |
|
295
|
|
|
return mb_strlen($str, 'UTF-8') <= (int)$args[0]; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* check if number between given range of numbers |
|
300
|
|
|
* |
|
301
|
|
|
* @param int $num |
|
302
|
|
|
* @param array $args(min,max) |
|
|
|
|
|
|
303
|
|
|
* @return bool |
|
304
|
|
|
*/ |
|
305
|
|
|
private function rangeNum($num, $args){ |
|
306
|
|
|
return $num >= (int)$args[0] && $num <= (int)$args[1]; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* check if value is a valid number |
|
311
|
|
|
* |
|
312
|
|
|
* @param string|integer $value |
|
313
|
|
|
* @return bool |
|
314
|
|
|
*/ |
|
315
|
|
|
private function integer($value){ |
|
316
|
|
|
return filter_var($value, FILTER_VALIDATE_INT); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* check if value(s) is in a given array |
|
321
|
|
|
* |
|
322
|
|
|
* @param string|array $value |
|
323
|
|
|
* @param array $arr |
|
324
|
|
|
* @return bool |
|
325
|
|
|
*/ |
|
326
|
|
|
private function inArray($value, $arr){ |
|
327
|
|
|
|
|
328
|
|
|
if(is_array($value)){ |
|
329
|
|
|
foreach($value as $val){ |
|
330
|
|
|
if(!in_array($val, $arr, true)){ |
|
331
|
|
|
return false; |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
return true; |
|
335
|
|
|
} |
|
336
|
|
|
return in_array($value, $arr, true); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* check if value is contains alphabetic characters and numbers |
|
341
|
|
|
* |
|
342
|
|
|
* @param mixed $value |
|
343
|
|
|
* @return bool |
|
344
|
|
|
*/ |
|
345
|
|
|
private function alphaNum($value){ |
|
346
|
|
|
return preg_match('/\A[a-z0-9]+\z/i', $value); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* check if value is contains alphabetic characters, numbers and spaces |
|
351
|
|
|
* |
|
352
|
|
|
* @param mixed $value |
|
353
|
|
|
* @return bool |
|
354
|
|
|
*/ |
|
355
|
|
|
private function alphaNumWithSpaces($value){ |
|
356
|
|
|
return preg_match('/\A[a-z0-9 ]+\z/i', $value); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* check if password has at least |
|
361
|
|
|
* - one lowercase letter |
|
362
|
|
|
* - one uppercase letter |
|
363
|
|
|
* - one number |
|
364
|
|
|
* - one special(non-word) character |
|
365
|
|
|
* |
|
366
|
|
|
* @param mixed $value |
|
367
|
|
|
* @return bool |
|
368
|
|
|
* @see http://stackoverflow.com/questions/8141125/regex-for-password-php |
|
369
|
|
|
* @see http://code.runnable.com/UmrnTejI6Q4_AAIM/how-to-validate-complex-passwords-using-regular-expressions-for-php-and-pcre |
|
370
|
|
|
*/ |
|
371
|
|
|
private function password($value) { |
|
372
|
|
|
return preg_match_all('$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$', $value); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* check if value is equals to another value(strings) |
|
377
|
|
|
* |
|
378
|
|
|
* @param string $value |
|
379
|
|
|
* @param array $args(value) |
|
|
|
|
|
|
380
|
|
|
* @return bool |
|
381
|
|
|
*/ |
|
382
|
|
|
private function equals($value, $args){ |
|
383
|
|
|
return $value === $args[0]; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* check if value is not equal to another value(strings) |
|
388
|
|
|
* |
|
389
|
|
|
* @param string $value |
|
390
|
|
|
* @param array $args(value) |
|
|
|
|
|
|
391
|
|
|
* @return bool |
|
392
|
|
|
*/ |
|
393
|
|
|
private function notEqual($value, $args){ |
|
394
|
|
|
return $value !== $args[0]; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* check if value is a valid email |
|
399
|
|
|
* |
|
400
|
|
|
* @param string $email |
|
401
|
|
|
* @return bool |
|
402
|
|
|
*/ |
|
403
|
|
|
private function email($email){ |
|
404
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** *********************************************** **/ |
|
408
|
|
|
/** ************ Database Validations *********** **/ |
|
409
|
|
|
/** *********************************************** **/ |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* check if a value of a column is unique. |
|
413
|
|
|
* |
|
414
|
|
|
* @param string $value |
|
415
|
|
|
* @param array $args(table, column) |
|
|
|
|
|
|
416
|
|
|
* @return bool |
|
417
|
|
|
*/ |
|
418
|
|
|
private function unique($value, $args){ |
|
419
|
|
|
|
|
420
|
|
|
$table = $args[0]; |
|
421
|
|
|
$col = $args[1]; |
|
422
|
|
|
|
|
423
|
|
|
$database = Database::openConnection(); |
|
424
|
|
|
$database->prepare("SELECT * FROM {$table} WHERE {$col} = :{$col}"); |
|
425
|
|
|
$database->bindValue(":{$col}", $value); |
|
426
|
|
|
$database->execute(); |
|
427
|
|
|
|
|
428
|
|
|
return $database->countRows() === 0; |
|
429
|
|
|
|
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
/** |
|
433
|
|
|
* check if email is unique |
|
434
|
|
|
* This will check if email exists and activated. |
|
435
|
|
|
* |
|
436
|
|
|
* @param string $email |
|
437
|
|
|
* @return bool |
|
438
|
|
|
*/ |
|
439
|
|
|
private function emailUnique($email){ |
|
440
|
|
|
|
|
441
|
|
|
$database = Database::openConnection(); |
|
442
|
|
|
|
|
443
|
|
|
// email is unique in the database, So, we can't have more than 2 same emails |
|
444
|
|
|
$database->prepare("SELECT * FROM users WHERE email = :email LIMIT 1"); |
|
445
|
|
|
$database->bindValue(':email', $email); |
|
446
|
|
|
$database->execute(); |
|
447
|
|
|
$user = $database->fetchAssociative(); |
|
448
|
|
|
|
|
449
|
|
|
if ($database->countRows() === 1) { |
|
450
|
|
|
|
|
451
|
|
|
if(!empty($user["is_email_activated"])){ |
|
452
|
|
|
return false; |
|
453
|
|
|
|
|
454
|
|
|
} else { |
|
455
|
|
|
|
|
456
|
|
|
$expiry_time = (24 * 60 * 60); |
|
457
|
|
|
$time_elapsed = time() - $user['email_last_verification']; |
|
458
|
|
|
|
|
459
|
|
|
// If time elapsed exceeded the expiry time, it worth to reset the token, and the email as well. |
|
460
|
|
|
// This indicates the email of $user hasn't been verified, and token is expired. |
|
461
|
|
|
if($time_elapsed >= $expiry_time) { |
|
462
|
|
|
|
|
463
|
|
|
$login = new Login(); |
|
464
|
|
|
$login->resetEmailVerificationToken($user["id"], false); |
|
465
|
|
|
return true; |
|
466
|
|
|
|
|
467
|
|
|
}else { |
|
468
|
|
|
|
|
469
|
|
|
// TODO check if $email is same as current user's email(not-activated), |
|
470
|
|
|
// then ask the user to verify his email |
|
471
|
|
|
return false; |
|
472
|
|
|
} |
|
473
|
|
|
} |
|
474
|
|
|
} |
|
475
|
|
|
return true; |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** *********************************************** **/ |
|
479
|
|
|
/** ************ Login Validations *********** **/ |
|
480
|
|
|
/** *********************************************** **/ |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* check if user credentials are valid or not. |
|
484
|
|
|
* |
|
485
|
|
|
* @param array $user |
|
486
|
|
|
* @return bool |
|
487
|
|
|
* @see Login::doLogin() |
|
488
|
|
|
*/ |
|
489
|
|
|
private function credentials($user){ |
|
490
|
|
|
if(empty($user["hashed_password"]) || empty($user["user_id"])) { |
|
491
|
|
|
return false; |
|
492
|
|
|
} |
|
493
|
|
|
return password_verify($user["password"], $user["hashed_password"]); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* check if user has exceeded number of failed logins or number of forgotten password attempts. |
|
498
|
|
|
* |
|
499
|
|
|
* @param array $attempts |
|
500
|
|
|
* @return bool |
|
501
|
|
|
*/ |
|
502
|
|
|
private function attempts($attempts){ |
|
503
|
|
|
|
|
504
|
|
|
if(empty($attempts['last_time']) && empty($attempts['count'])) { |
|
505
|
|
|
return true; |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
$block_time = (10 * 60); |
|
509
|
|
|
$time_elapsed = time() - $attempts['last_time']; |
|
510
|
|
|
|
|
511
|
|
|
// TODO If user is Blocked, Update failed logins/forgotten passwords |
|
512
|
|
|
// to current time and optionally number of attempts to be incremented, |
|
513
|
|
|
// but, this will reset the last_time every time there is a failed attempt |
|
514
|
|
|
|
|
515
|
|
|
if ($attempts["count"] >= 5 && $time_elapsed < $block_time) { |
|
516
|
|
|
|
|
517
|
|
|
// here i can't define a default error message as in defaultMessages() |
|
518
|
|
|
// because the error message depends on variables like $block_time & $time_elapsed |
|
519
|
|
|
$this->errors[] = "You exceeded number of possible attempts, please try again later after " . |
|
520
|
|
|
date("i", $block_time - $time_elapsed) . " minutes"; |
|
521
|
|
|
return false; |
|
522
|
|
|
|
|
523
|
|
|
}else{ |
|
524
|
|
|
|
|
525
|
|
|
return true; |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** *********************************************** **/ |
|
530
|
|
|
/** ************ File Validations *********** **/ |
|
531
|
|
|
/** *********************************************** **/ |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* checks if file unique. |
|
535
|
|
|
* |
|
536
|
|
|
* @param array $path |
|
537
|
|
|
* @return bool |
|
538
|
|
|
* |
|
539
|
|
|
* @see |
|
540
|
|
|
*/ |
|
541
|
|
|
private function fileUnique($path){ |
|
542
|
|
|
return !file_exists($path); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* checks for file errors |
|
547
|
|
|
* |
|
548
|
|
|
* @param array $file |
|
549
|
|
|
* @return bool |
|
550
|
|
|
*/ |
|
551
|
|
|
private function fileErrors($file){ |
|
552
|
|
|
return (int)$file['error'] === UPLOAD_ERR_OK; |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
/** |
|
556
|
|
|
* checks if file uploaded successfully via HTTP POST |
|
557
|
|
|
* |
|
558
|
|
|
* @param array $file |
|
559
|
|
|
* @return bool |
|
560
|
|
|
* |
|
561
|
|
|
* @see |
|
562
|
|
|
*/ |
|
563
|
|
|
private function fileUploaded($file){ |
|
564
|
|
|
return is_uploaded_file($file["tmp_name"]); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
/** |
|
568
|
|
|
* checks from file size |
|
569
|
|
|
* |
|
570
|
|
|
* @param array $file |
|
571
|
|
|
* @param array $args(min,max) |
|
|
|
|
|
|
572
|
|
|
* @return bool |
|
573
|
|
|
*/ |
|
574
|
|
|
private function fileSize($file, $args){ |
|
575
|
|
|
|
|
576
|
|
|
// size in bytes, |
|
577
|
|
|
// 1 KB = 1024 bytes, and 1 MB = 1048,576 bytes. |
|
578
|
|
|
$size = array ("min" => (int)$args[0], "max" => (int)$args[1]); |
|
579
|
|
|
|
|
580
|
|
|
if ($file['size'] > $size['max']) { |
|
581
|
|
|
$this->errors[] = "File size can't exceed max limit (". ($size['max']/1048576) . " MB)"; |
|
582
|
|
|
return false; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
// better not to say the min limits. |
|
586
|
|
|
if($file['size'] < $size['min']){ |
|
587
|
|
|
$this->errors[] = "File size either is too small or corrupted"; |
|
588
|
|
|
return false; |
|
589
|
|
|
} |
|
590
|
|
|
return true; |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
/** |
|
594
|
|
|
* checks from image size(dimensions) |
|
595
|
|
|
* |
|
596
|
|
|
* @param array $file |
|
597
|
|
|
* @param array $dimensions(width,height) |
|
|
|
|
|
|
598
|
|
|
* @return bool |
|
599
|
|
|
*/ |
|
600
|
|
|
private function imageSize($file, $dimensions){ |
|
601
|
|
|
|
|
602
|
|
|
$imageSize = array('width' => 0, 'height' => 0); |
|
603
|
|
|
list($imageSize['width'], $imageSize['height']) = getimagesize($file["tmp_name"]); |
|
604
|
|
|
|
|
605
|
|
|
if($imageSize["width"] < 10 || $imageSize["height"] < 10){ |
|
606
|
|
|
$this->errors[] = "This image is too small or corrupted"; |
|
607
|
|
|
return false; |
|
608
|
|
|
} |
|
609
|
|
|
if($imageSize["width"] > $dimensions[0] || $imageSize["height"] > $dimensions[1]){ |
|
610
|
|
|
$this->errors[] = "Image width & height must be below ". $dimensions[0] ." pixels"; |
|
611
|
|
|
return false; |
|
612
|
|
|
} |
|
613
|
|
|
return true; |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
/** |
|
617
|
|
|
* validate mime type |
|
618
|
|
|
* |
|
619
|
|
|
* @param array $file |
|
620
|
|
|
* @param array $mimeTypes |
|
621
|
|
|
* @throws Exception if finfo_open() doesn't exists |
|
622
|
|
|
* @return bool |
|
623
|
|
|
*/ |
|
624
|
|
View Code Duplication |
private function mimeType($file, $mimeTypes){ |
|
|
|
|
|
|
625
|
|
|
|
|
626
|
|
|
if(!file_exists($file["tmp_name"])){ |
|
627
|
|
|
return false; |
|
628
|
|
|
} |
|
629
|
|
|
if(!function_exists('finfo_open')) { |
|
630
|
|
|
throw new Exception("Function finfo_open() doesn't exist"); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
$finfo_open = finfo_open(FILEINFO_MIME_TYPE); |
|
634
|
|
|
$finfo_file = finfo_file($finfo_open, $file["tmp_name"]); |
|
635
|
|
|
finfo_close($finfo_open); |
|
636
|
|
|
|
|
637
|
|
|
list($mime) = explode(';', $finfo_file); |
|
638
|
|
|
|
|
639
|
|
|
// in case of zip file it returns application/octet-stream |
|
640
|
|
|
return in_array($mime, $mimeTypes, true); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
/** |
|
644
|
|
|
* validate file extension returned from pathinfo() Vs mapped mime type to extension |
|
645
|
|
|
* |
|
646
|
|
|
* This reveal un desired errors in case of files with extension: zip, csv, ..etc |
|
647
|
|
|
* |
|
648
|
|
|
* @param array $file |
|
649
|
|
|
* @param array $extension |
|
650
|
|
|
* @return bool |
|
651
|
|
|
*/ |
|
652
|
|
|
private function fileExtension($file, $extension){ |
|
653
|
|
|
|
|
654
|
|
|
if(isset($extension[0])){ |
|
655
|
|
|
return $extension[0] === pathinfo($file['name'])['extension']; |
|
656
|
|
|
} |
|
657
|
|
|
return false; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** *********************************************** **/ |
|
661
|
|
|
/** ************ Default Messages *********** **/ |
|
662
|
|
|
/** *********************************************** **/ |
|
663
|
|
|
|
|
664
|
|
|
/** |
|
665
|
|
|
* get default message for a rule |
|
666
|
|
|
* |
|
667
|
|
|
* Instead of passing your custom message every time, |
|
668
|
|
|
* you can define a set of default messages. |
|
669
|
|
|
* |
|
670
|
|
|
* The pitfall of this method is, if you changed the validation method name, |
|
671
|
|
|
* you need to change it here as well. |
|
672
|
|
|
* |
|
673
|
|
|
* @param string $rule |
|
674
|
|
|
* @return mixed |
|
675
|
|
|
*/ |
|
676
|
|
|
private static function defaultMessages($rule){ |
|
677
|
|
|
$messages = [ |
|
678
|
|
|
"required" => "{placeholder} can't be empty", |
|
679
|
|
|
"minLen" => "{placeholder} can't be less than {0} character", |
|
680
|
|
|
"maxLen" => "{placeholder} can't be greater than {0} character", |
|
681
|
|
|
"rangeNum" => "{placeholder} must be between {0} and {1}", |
|
682
|
|
|
"integer" => "{placeholder} must be a valid number", |
|
683
|
|
|
"inArray" => "{placeholder} is not valid", |
|
684
|
|
|
"alphaNum" => "Only letters and numbers are allowed for {placeholder}", |
|
685
|
|
|
"alphaNumWithSpaces" => "Only letters, numbers and spaces are allowed for {placeholder}", |
|
686
|
|
|
"password" => "Passwords must contain at least one lowercase, uppercase, number and special character", |
|
687
|
|
|
"equals" => "{placeholder}s aren't equal", |
|
688
|
|
|
"notEqual" => "{placeholder} can't be equal to {0}", |
|
689
|
|
|
"email" => "Invalid email, Please enter a valid email address", |
|
690
|
|
|
"unique" => "{placeholder} already exists", |
|
691
|
|
|
"emailUnique" => "Email already exists", |
|
692
|
|
|
"credentials" => "User ID & Password combination doesn't exist", |
|
693
|
|
|
"attempts" => "", |
|
694
|
|
|
"fileUnique" => "File already exists", |
|
695
|
|
|
"fileUploaded" => "Your uploaded file is invalid!", |
|
696
|
|
|
"fileErrors" => "There was an error with the uploaded file", |
|
697
|
|
|
"fileSize" => "", |
|
698
|
|
|
"imageSize" => "", |
|
699
|
|
|
"mimeType" => "Your file format is invalid", |
|
700
|
|
|
"fileExtension" => "Your file format is invalid" |
|
701
|
|
|
]; |
|
702
|
|
|
|
|
703
|
|
|
return isset($messages[$rule])? $messages[$rule]: null; |
|
704
|
|
|
} |
|
705
|
|
|
} |
|
706
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.