-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate.php
891 lines (804 loc) · 23.5 KB
/
Validate.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
<?php
namespace futuretek\shared;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
/**
* Class Validate
*
* @package futuretek\shared
* @author Lukas Cerny <[email protected]>
* @license Apache-2.0
* @link http://www.futuretek.cz
*/
class Validate
{
const PASSWORD_COMPLEXITY_WEAK = 2;
const PASSWORD_COMPLEXITY_MEDIUM = 3;
const PASSWORD_COMPLEXITY_STRONG = 4;
const PASSWORD_COMPLEXITY_VERY_STRONG = 6;
/**
* Administrator password length
*/
const ADMIN_PASSWORD_LENGTH = 8;
/**
* User password length
*/
const PASSWORD_LENGTH = 5;
/**
* Check for e-mail validity
*
* @param string $email e-mail address to validate
*
* @return boolean Validity is ok or not
*/
public static function isEmail($email)
{
return preg_match(
'/^[a-z\p{L}0-9!#$%&\'*+\/=?^`{}|~_-]+[.a-z\p{L}0-9!#$%&\'*+\/=?^`{}|~_-]*@[a-z\p{L}0-9]+(?:[.]?[_a-z\p{L}0-9-])*\.[a-z\p{L}0-9]+$/ui',
$email
);
}
/**
* Check for MD5 string validity
*
* @param string $md5 MD5 string to validate
*
* @return boolean Validity is ok or not
*/
public static function isMd5($md5)
{
return preg_match('/^[a-f0-9A-F]{32}$/', $md5);
}
/**
* Check for SHA1 string validity
*
* @param string $sha1 SHA1 string to validate
*
* @return boolean Validity is ok or not
*/
public static function isSha1($sha1)
{
return preg_match('/^[a-fA-F0-9]{40}$/', $sha1);
}
/**
* Check for a float number validity
*
* @param float $float Float number to validate
*
* @return boolean Validity is ok or not
*/
public static function isFloat($float)
{
return (string)(float)$float === (string)$float;
}
/**
* Check for a unsigned float number validity
*
* @param float $float Float number to validate
*
* @return boolean Validity is ok or not
*/
public static function isUnsignedFloat($float)
{
return (string)(float)$float === (string)$float && $float >= 0;
}
/**
* Check for a float number validity (or empty)
*
* @param float $float Float number to validate
*
* @return boolean Validity is ok or not
*/
public static function isOptFloat($float)
{
return $float === null || self::isFloat($float);
}
/**
* Check for sender name validity
*
* @param string $mail_name Sender name to validate
*
* @return boolean Validity is ok or not
*/
public static function isMailName($mail_name)
{
return (\is_string($mail_name) && preg_match('/^[^<>;=#{}]*$/u', $mail_name));
}
/**
* Check for e-mail subject validity
*
* @param string $mail_subject e-mail subject to validate
*
* @return boolean Validity is ok or not
*/
public static function isMailSubject($mail_subject)
{
return preg_match('/^[^<>]*$/u', $mail_subject);
}
/**
* Check for price validity
*
* @param string $price Price to validate
*
* @return boolean Validity is ok or not
*/
public static function isPrice($price)
{
return preg_match('/^\d{1,10}(\.\d{1,9})?$/', $price);
}
/**
* Check for price validity (including negative price)
*
* @param string $price Price to validate
*
* @return boolean Validity is ok or not
*/
public static function isNegativePrice($price)
{
return preg_match('/^[-]?\d{1,10}(\.\d{1,9})?$/', $price);
}
/**
* Check for language code (ISO) validity
*
* @param string $iso_code Language code (ISO) to validate
*
* @return boolean Validity is ok or not
*/
public static function isLanguageIsoCode($iso_code)
{
return preg_match('/^[a-zA-Z]{2,3}$/', $iso_code);
}
/**
* Check for language code validity (eg. cs_CZ)
*
* @param string $s Language code to validate
*
* @return boolean Validity is ok or not
*/
public static function isLanguageCode($s)
{
return preg_match('/^[a-zA-Z]{2}(-[a-zA-Z]{2})?$/', $s);
}
public static function isStateIsoCode($iso_code)
{
return preg_match('/^[a-zA-Z0-9]{1,4}((-)[a-zA-Z0-9]{1,4})?$/', $iso_code);
}
public static function isNumericIsoCode($iso_code)
{
return preg_match('/^\d{2,3}$/', $iso_code);
}
/**
* Check for a postal address validity
*
* @param string $address Address to validate
*
* @return boolean Validity is ok or not
*/
public static function isAddress($address)
{
return preg_match('/^[^!<>?=+@{}_$%]*$/u', $address);
}
/**
* Check for city name validity
*
* @param string $city City name to validate
*
* @return boolean Validity is ok or not
*/
public static function isCityName($city)
{
return preg_match('/^[^!<>;?=+@#"°{}_$%]*$/u', $city);
}
/**
* Check for search query validity
*
* @param string $search Query to validate
*
* @return boolean Validity is ok or not
*/
public static function isValidSearch($search)
{
return preg_match('/^[^<>;=#{}]{0,64}$/u', $search);
}
/**
* Check for standard name validity
*
* @param string $name Name to validate
*
* @return boolean Validity is ok or not
*/
public static function isGenericName($name)
{
return preg_match('/^[^<>={}]*$/u', $name);
}
/**
* Check for HTML field validity (no XSS please !)
*
* @param string $html HTML field to validate
* @param bool $allowIframe Allow iframe HTML tag
*
* @return bool Validity is ok or not
*/
public static function isCleanHtml($html, $allowIframe = false)
{
$events = 'onmousedown|onmousemove|onmmouseup|onmouseover|onmouseout|onload|onunload|onfocus|onblur|onchange';
$events .= '|onsubmit|ondblclick|onclick|onkeydown|onkeyup|onkeypress|onmouseenter|onmouseleave|onerror|onselect|onreset|onabort|ondragdrop|onresize|onactivate|onafterprint|onmoveend';
$events .= '|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onmove';
$events .= '|onbounce|oncellchange|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondeactivate|ondrag|ondragend|ondragenter|onmousewheel';
$events .= '|ondragleave|ondragover|ondragstart|ondrop|onerrorupdate|onfilterchange|onfinish|onfocusin|onfocusout|onhashchange|onhelp|oninput|onlosecapture|onmessage|onmouseup|onmovestart';
$events .= '|onoffline|ononline|onpaste|onpropertychange|onreadystatechange|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onsearch|onselectionchange';
$events .= '|onselectstart|onstart|onstop';
if (preg_match('/<[\s]*script/im', $html) || preg_match('/(' . $events . ')[\s]*=/im', $html) ||
preg_match('/script\:/im', $html)
) {
return false;
}
return !(!$allowIframe && preg_match('/<[\s]*(i?frame|form|input|embed|object)/im', $html));
}
/**
* Check for password validity
*
* @param string $passwd Password to validate
* @param int $size
*
* @return boolean Validity is ok or not
*/
public static function isPasswd($passwd, $size = Validate::PASSWORD_LENGTH)
{
return self::getPasswordComplexity($passwd, $size) >= self::PASSWORD_COMPLEXITY_MEDIUM;
}
public static function isPasswdAdmin($passwd)
{
return self::getPasswordComplexity($passwd, self::ADMIN_PASSWORD_LENGTH) >= self::PASSWORD_COMPLEXITY_STRONG;
}
/**
* Check for configuration key validity
*
* @param string $config_name Configuration key to validate
*
* @return boolean Validity is ok or not
*/
public static function isConfigName($config_name)
{
return preg_match('/^[a-zA-Z_0-9-]+$/', $config_name);
}
/**
* Check for date format
*
* @param string $date Date to validate
*
* @return boolean Validity is ok or not
*/
public static function isDateFormat($date)
{
return preg_match('/^(\d{4})-((0?\d)|(1[0-2]))-((0?\d)|([1-2]\d)|(3[01]))( \d{2}:\d{2}:\d{2})?$/', $date);
}
/**
* Check for date validity
*
* @param string $date Date to validate
*
* @return boolean Validity is ok or not
*/
public static function isDate($date)
{
if (!preg_match('/^(\d{4})-((?:0?\d)|(?:1[0-2]))-((?:0?\d)|(?:[1-2]\d)|(?:3[01]))( \d{2}:\d{2}:\d{2})?$/', $date, $matches)) {
return false;
}
return checkdate((int)$matches[2], (int)$matches[3], (int)$matches[1]);
}
/**
* Check for birthDate validity
*
* @param string $date birthdate to validate
*
* @return boolean Validity is ok or not
*/
public static function isBirthDate($date)
{
if ($date === null || $date === '0000-00-00') {
return true;
}
if (preg_match('/^(\d{4})-((?:0?[1-9])|(?:1[0-2]))-((?:0?[1-9])|(?:[1-2]\d)|(?:3[01]))(\d{2}:\d{2}:\d{2})?$/', $date, $birth_date)) {
return !(($birth_date[1] > date('Y') && $birth_date[2] > date('m') && $birth_date[3] > date('d')) ||
($birth_date[1] === date('Y') && $birth_date[2] === date('m') && $birth_date[3] > date('d')) ||
($birth_date[1] === date('Y') && $birth_date[2] > date('m'))
);
}
return false;
}
/**
* Check for boolean validity
*
* @param boolean $bool Boolean to validate
*
* @return boolean Validity is ok or not
*/
public static function isBool($bool)
{
return $bool === null || \is_bool($bool) || preg_match('/^0|1$/', $bool);
}
/**
* Check for phone number validity
*
* @param string $number Phone number to validate
* @param string $country Two digit uppercase country code (CZ, US, AU)
*
* @return boolean Validity is ok or not
*/
public static function isPhoneNumber($number, $country)
{
return self::isPhoneNumber2($number, $country);
}
/**
* Extended check for phone number validity (including specific country format)
*
* @param string $number Phone number to validate
* @param string $country Two digit uppercase country code (CZ, US, AU)
* @return bool Validity is ok or not
*/
public static function isPhoneNumber2($number, $country)
{
if (empty($number)) {
return false;
}
$phoneUtil = PhoneNumberUtil::getInstance();
try {
$numberProto = $phoneUtil->parse($number, strtoupper($country));
return $phoneUtil->isValidNumber($numberProto);
} catch (NumberParseException $e) {
return false;
}
}
/**
* Check for time in hh:mm:ss format validity
*
* @param string $time Time
*
* @return boolean Validity is ok or not
*/
public static function isTime($time)
{
return preg_match('/^(([0-1]\d)|([2][0-3])):([0-5]\d):([0-5]\d)$/', $time);
}
/**
* Check for time in hh:mm:ss format validity. Accepts also null value
*
* @param string $time Time or null
*
* @return boolean Validity is ok or not
*/
public static function isTimeOrNull($time)
{
return $time === null or preg_match('/^(([0-1]\d)|([2][0-3])):([0-5]\d):([0-5]\d)$/', $time);
}
/**
* Check for barcode validity (EAN-13)
*
* @param string $ean13 Barcode to validate
*
* @return boolean Validity is ok or not
*/
public static function isEan13($ean13)
{
return preg_match('/^\d{0,13}$/', $ean13);
}
/**
* Check for barcode validity (UPC)
*
* @param string $upc Barcode to validate
*
* @return boolean Validity is ok or not
*/
public static function isUpc($upc)
{
return preg_match('/^\d{0,12}$/', $upc);
}
/**
* Check for postal code validity
*
* @param string $postcode Postal code to validate
*
* @return boolean Validity is ok or not
*/
public static function isPostCode($postcode)
{
return preg_match('/^[a-zA-Z 0-9-]+$/', $postcode);
}
/**
* Check for zip code format validity
*
* @param string $zip_code zip code format to validate
*
* @return boolean Validity is ok or not
*/
public static function isZipCodeFormat($zip_code)
{
return preg_match('/^[NLCnlc 0-9-]+$/', $zip_code);
}
/**
* Check for table or identifier validity
* Mostly used in database for ordering : ASC / DESC
*
* @param string $way Keyword to validate
*
* @return boolean Validity is ok or not
*/
public static function isOrderWay($way)
{
return ($way === 'ASC' | $way === 'DESC' | $way === 'asc' | $way === 'desc');
}
/**
* Check for table or identifier validity
* Mostly used in database for ordering : ORDER BY field
*
* @param string $order Field to validate
*
* @return boolean Validity is ok or not
*/
public static function isOrderBy($order)
{
return preg_match('/^[a-zA-Z0-9.!_-]+$/', $order);
}
/**
* Check for tags list validity
*
* @param string $list List to validate
*
* @return boolean Validity is ok or not
*/
public static function isTagsList($list)
{
return preg_match('/^[^!<>;?=+#"°{}_$%]*$/u', $list);
}
/**
* Check for an integer validity
*
* @param integer $value Integer to validate
*
* @return boolean Validity is ok or not
*/
public static function isInt($value)
{
return ((string)(int)$value === (string)$value || $value === false);
}
/**
* Check for an integer validity (unsigned)
*
* @param integer $value Integer to validate
*
* @return boolean Validity is ok or not
*/
public static function isUnsignedInt($value)
{
return (preg_match('#^\d+$#', (string)$value) && $value < 4294967296 && $value >= 0);
}
/**
* Check for an percentage validity (between 0 and 100)
*
* @param float $value Float to validate
*
* @return boolean Validity is ok or not
*/
public static function isPercentage($value)
{
return (self::isFloat($value) && $value >= 0 && $value <= 100);
}
/**
* Check for an integer validity (unsigned)
* Mostly used in database for auto-increment
*
* @param integer $id Integer to validate
*
* @return boolean Validity is ok or not
*/
public static function isUnsignedId($id)
{
return self::isUnsignedInt($id); /* Because an id could be equal to zero when there is no association */
}
public static function isNullOrUnsignedId($id)
{
return $id === null || self::isUnsignedId($id);
}
/**
* Check object validity
*
* @param object $object Object to validate
*
* @return boolean Validity is ok or not
*/
public static function isLoadedObject($object)
{
return \is_object($object) && $object->id;
}
/**
* Check hexadecimal color validity
*
* @param string $color Hexadecimal color to validate
*
* @return boolean Validity is ok or not
*/
public static function isColor($color)
{
return preg_match('/^(#[0-9a-fA-F]{6}|[a-zA-Z0-9-]*)$/', $color);
}
/**
* Check url validity (disallowed empty string)
*
* @param string $url Url to validate
*
* @return boolean Validity is ok or not
*/
public static function isUrl($url)
{
return preg_match('/^[~:#,$%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
}
/**
* Check tracking number validity (disallowed empty string)
*
* @param string $tracking_number Tracking number to validate
*
* @return boolean Validity is ok or not
*/
public static function isTrackingNumber($tracking_number)
{
return preg_match('/^[~:#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/', $tracking_number);
}
/**
* Check url validity (allowed empty string)
*
* @param string $url Url to validate
*
* @return boolean Validity is ok or not
*/
public static function isUrlOrEmpty($url)
{
return $url === null || self::isUrl($url);
}
/**
* Check if URL is absolute
*
* @param string $url URL to validate
*
* @return boolean Validity is ok or not
*/
public static function isAbsoluteUrl($url)
{
return preg_match('/^https?:\/\/[$~:;#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
}
public static function isUnixName($data)
{
return preg_match('/^[a-z0-9\._-]+$/ui', $data);
}
public static function isTablePrefix($data)
{
// Even if "-" is theoretically allowed, it will be considered a syntax error if you do not add back quotes (`) around the table name
return preg_match('/^[a-z0-9_]+$/ui', $data);
}
/**
* Check for standard name file validity
*
* @param string $name Name to validate
*
* @return boolean Validity is ok or not
*/
public static function isFileName($name)
{
return preg_match('/^[a-zA-Z0-9_.-]+$/', $name);
}
/**
* Check for standard name directory validity
*
* @param string $dir Directory to validate
*
* @return boolean Validity is ok or not
*/
public static function isDirName($dir)
{
return (bool)preg_match('/^[a-zA-Z0-9_.-]*$/', $dir);
}
public static function isWeightUnit($unit)
{
return (self::isGenericName($unit) & (\strlen($unit) < 5));
}
public static function isDistanceUnit($unit)
{
return (self::isGenericName($unit) & (\strlen($unit) < 5));
}
public static function isSubDomainName($domain)
{
return preg_match('/^[a-zA-Z0-9-_]*$/', $domain);
}
/**
* Price display method validity
*
* @param string $data Data to validate
*
* @return boolean Validity is ok or not
*/
public static function isString($data)
{
return \is_string($data);
}
/**
* Check for PHP serialized data
*
* @param string $data Serialized data to validate
*
* @return boolean Validity is ok or not
*/
public static function isSerializedArray($data)
{
return $data === null || (\is_string($data) && preg_match('/^a:\d+:{.*;}$/s', $data));
}
/**
* Check for Latitude/Longitude
*
* @param string $data Coordinate to validate
*
* @return boolean Validity is ok or not
*/
public static function isCoordinate($data)
{
return $data === null || preg_match('/^\-?\d{1,8}\.\d{1,8}$/', $data);
}
/**
* Check for Language Iso Code
*
* @param string $iso_code
*
* @return boolean Validity is ok or not
*/
public static function isLangIsoCode($iso_code)
{
return (bool)preg_match('/^[a-zA-Z]{2,3}$/', $iso_code);
}
/**
*
* @param array $ids
*
* @return boolean return true if the array contain only unsigned int value
*/
public static function isArrayWithIds($ids)
{
if (\count($ids)) {
foreach ($ids as $id) {
if ((int)$id === 0 || !self::isUnsignedInt($id)) {
return false;
}
}
}
return true;
}
/**
* Check if the phone number is mobile
*
* @param string $phone Phone number
*
* @return bool Is phone number mobile
* @static
*/
public static function isMobilePhoneNumber($phone)
{
$phoneNumber = substr(Tools::removeSpace($phone), -9, 1);
return (!self::isCzechPhoneNumber($phoneNumber) || ($phoneNumber === '6' || $phoneNumber === '7'));
}
/**
* Determine whether the phone number is from Czech Republic
*
* @param string $phone Phone number
*
* @return bool Is Czech phone number
* @static
*/
public static function isCzechPhoneNumber($phone)
{
return (bool)preg_match("/^(\\+420)? ?\\d{3} ?\\d{3} ?\\d{3}$/", $phone);
}
/**
* Check if the GUID is valid
*
* @param string $uuid GUID
*
* @return bool Whether the GUID is valid
* @static
*/
public static function isGuid($uuid)
{
return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
}
/**
* Check if the birth number is valid
*
* @param string $no Birth number
* @return bool Is valid
*/
public static function isBirthNumber($no)
{
if (!preg_match('#^\s*(\d\d)(\d\d)(\d\d)[ /]*(\d\d\d)(\d?)\s*$#', $no, $matches)) {
return false;
}
list(, $year, $month, $day, $ext, $c) = $matches;
if ($c === '') {
$year += $year < 54 ? 1900 : 1800;
} else {
$mod = ($year . $month . $day . $ext) % 11;
if ($mod === 10) {
$mod = 0;
}
if ($mod !== (int)$c) {
return false;
}
$year += $year < 54 ? 2000 : 1900;
}
if ($year > 2003) {
if ($month > 70) {
$month -= 70;
}
if ($month > 20 && $month < 50) {
$month -= 20;
}
}
if ($month > 50) {
$month -= 50;
}
return checkdate($month, $day, $year);
}
/**
* Check if specified string is valid pretty time
*
* @param string $time Pretty time
* @return bool Is valid
*/
public static function isPrettyTime($time)
{
return (bool)preg_match('/^(?:(?<weeks>\d+)w\s*)?(?:(?<days>\d+)d\s*)?(?:(?<hours>\d+)h\s*)?(?:(?<minutes>\d+)m\s*)?(?:(?<seconds>\d+)s\s*)?$/', $time);
}
/**
* Check if input string is link-rewrite
*
* @param string $name Name to validate
*
* @return boolean Validity is ok or not
*/
public static function isLinkRewrite($name)
{
return preg_match('/^[a-z0-9-]*$/u', $name);
}
/**
* Get password complexity
*
* @param string $password Password
* @param int $minLength Minimal length
* @return int Password score
*/
public static function getPasswordComplexity($password, $minLength)
{
$group = [
'upper' => '/[A-Z]/',
'lower' => '/[a-z]/',
'number' => '/[0-9]/',
'special' => '/[^A-Za-z0-9]/',
];
$score = 0;
$length = \strlen($password);
if ($length < $minLength) {
return 0;
}
// Increment the score for each of these conditions
foreach ($group as $pattern) {
if (preg_match($pattern, $password)) {
$score++;
}
}
// Penalize if there aren't at least three char types
if ($score < 3) {
$score--;
}
// Increment the score for every 2 chars longer than the minimum
if ($length > $minLength) {
$score += (int)floor(($length - $minLength) / 2);
}
return $score;
}
}