forked from benedmunds/inspekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inspekt.php
1244 lines (1136 loc) · 36.3 KB
/
Inspekt.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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Inspekt - main source file
*
* @author Chris Shiflett <[email protected]>
* @author Ed Finkler <[email protected]>
*
* @package Inspekt
*/
/**
* Inspekt_Error
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Inspekt/Error.php');
/**
* Inspekt_Cage
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Inspekt/Cage.php');
/**
* Inspekt_Cage_Session
*/
//require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Inspekt/Cage/Session.php');
/**
* Inspekt_Supercage
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Inspekt/Supercage.php');
/**
* Options for isHostname() that specify which types of hostnames
* to allow.
*
* HOST_ALLOW_DNS: Allows Internet domain names (e.g.,
* example.com).
*/
define('ISPK_HOST_ALLOW_DNS', 1);
/**
* Options for isHostname() that specify which types of hostnames
* to allow.
*
* HOST_ALLOW_IP: Allows IP addresses.
*/
define('ISPK_HOST_ALLOW_IP', 2);
/**
* Options for isHostname() that specify which types of hostnames
* to allow.
*
* HOST_ALLOW_LOCAL: Allows local network names (e.g., localhost,
* www.localdomain) and Internet domain names.
*/
define('ISPK_HOST_ALLOW_LOCAL', 4);
/**
* Options for isHostname() that specify which types of hostnames
* to allow.
*
* HOST_ALLOW_ALL: Allows all of the above types of hostnames.
*/
define('ISPK_HOST_ALLOW_ALL', 7);
/**
* Options for isUri that specify which types of URIs to allow.
*
* URI_ALLOW_COMMON: Allow only "common" hostnames: http, https, ftp
*/
define('ISPK_URI_ALLOW_COMMON', 1);
/**
* @package Inspekt
*/
class Inspekt
{
protected static $useFilterExtension = true;
/**
* regex used to define what we're calling a valid domain name
*/
const VALID_DNS_REGEX = '/^(?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?$/';
/**
* regex used to define what we're calling a valid email
*
* we're taking a "match 99%" approach here, rather than a strict
* interpretation of the RFC.
*
* @see http://www.regular-expressions.info/email.html
*/
const VALID_EMAIL_REGEX = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/';
/**
* regex used to validate a US postal (zip) code, ZIP or ZIP+4 allowed
*/
const VALID_POSTAL_CODE_REGEX = '/(^\d{5}$)|(^\d{5}-\d{4}$)/';
/**
* Returns the $_SERVER data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*
* @assert()
*/
static public function makeServerCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_SERVER, $config_file, '_SERVER', $strict);
}
$GLOBALS['HTTP_SERVER_VARS'] = null;
return $_instance;
}
/**
* Returns the $_GET data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*/
static public function makeGetCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_GET, $config_file, '_GET', $strict);
}
$GLOBALS['HTTP_GET_VARS'] = null;
return $_instance;
}
/**
* Returns the $_POST data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*/
static public function makePostCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_POST, $config_file, '_POST', $strict);
}
$GLOBALS['HTTP_POST_VARS'] = null;
return $_instance;
}
/**
* Returns the $_COOKIE data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*/
static public function makeCookieCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_COOKIE, $config_file, '_COOKIE', $strict);
}
$GLOBALS['HTTP_COOKIE_VARS'] = null;
return $_instance;
}
/**
* Returns the $_ENV data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*/
static public function makeEnvCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_ENV, $config_file, '_ENV', $strict);
}
$GLOBALS['HTTP_ENV_VARS'] = null;
return $_instance;
}
/**
* Returns the $_FILES data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
*/
static public function makeFilesCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_instance)) {
$_instance = Inspekt_Cage::Factory($_FILES, $config_file, '_FILES', $strict);
}
$GLOBALS['HTTP_POST_FILES'] = null;
return $_instance;
}
/**
* Returns the $_SESSION data wrapped in an Inspekt_Cage object
*
* This utilizes a singleton pattern to get around scoping issues
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal array
* @return Inspekt_Cage
* @deprecated
*/
static public function makeSessionCage($config_file = null, $strict = true)
{
Inspekt_Error::raiseError('makeSessionCage is disabled in this version', E_USER_ERROR);
/**
* @staticvar $_instance
*/
static $_instance;
if (!isset($_SESSION)) {
return null;
}
if (!isset($_instance)) {
$_instance = Inspekt_Cage_Session::Factory($_SESSION, $config_file, '_SESSION', $strict);
}
$GLOBALS['HTTP_SESSION_VARS'] = null;
return $_instance;
}
/**
* Returns a Supercage object, which wraps ALL input superglobals
*
* @param string $config_file
* @param boolean $strict whether or not to nullify the superglobal
* @return Inspekt_Supercage
*/
static public function makeSuperCage($config_file = null, $strict = true)
{
/**
* @staticvar $_instance
*/
static $_scinstance;
if (!isset($_scinstance)) {
$_scinstance = Inspekt_Supercage::Factory($config_file, $strict);
}
return $_scinstance;
}
/**
* Sets and/or retrieves whether we should use the PHP filter extensions where possible
* If a param is passed, it will set the state in addition to returning it
*
* We use this method of storing in a static class property so that we can access the value outside of class instances
*
* @param boolean $state optional
* @return boolean
*/
static public function useFilterExt($state = null)
{
if (isset($state)) {
Inspekt::$useFilterExtension = (bool) $state;
}
return Inspekt::$useFilterExtension;
}
/**
* Recursively walks an array and applies a given filter method to
* every value in the array.
*
* This should be considered a "protected" method, and not be called
* outside of the class
*
* @param array|ArrayObject $input
* @param string $inspektor The name of a static filtering method, like get* or no*
* @return array
*/
static protected function _walkArray($input, $method, $classname = null)
{
if (!isset($classname)) {
$classname = __CLASS__;
}
if (!self::isArrayOrArrayObject($input) ) {
Inspekt_Error::raiseError('$input must be an array or ArrayObject', E_USER_ERROR);
return false;
}
if (!is_callable(array($classname, $method))) {
Inspekt_Error::raiseError('Inspektor ' . $classname . '::' . $method . ' is invalid', E_USER_ERROR);
return false;
}
foreach ($input as $key => $val) {
if (is_array($val)) {
$input[$key]=self::_walkArray($val, $method, $classname);
} else {
$val = call_user_func(array($classname, $method), $val);
$input[$key] = $val;
}
}
return $input;
}
/**
* Checks to see if this is an ArrayObject
* @param mixed
* @return boolean
* @deprecated
* @link http://php.net/arrayobject
*/
static public function isArrayObject($obj)
{
$is = false;
//$is = (is_object($obj) && get_class($obj) === 'ArrayObject');
$is = $obj instanceof ArrayObject;
return $is;
}
/**
* Checks to see if this is an array or an ArrayObject
* @param mixed
* @return boolean
* @link http://php.net/arrayobject
* @link http://php.net/array
*/
static public function isArrayOrArrayObject($arr)
{
$is = false;
$is = $arr instanceof ArrayObject || is_array($arr);
return $is;
}
/**
* Converts an array into an ArrayObject. We use ArrayObjects when walking arrays in Inspekt
* @param array
* @return ArrayObject
*/
static public function convertArrayToArrayObject(&$arr)
{
foreach ($arr as $key => $value) {
if (is_array($value)) {
$value = new ArrayObject($value);
$arr[$key] = $value;
//echo $key." is an array\n";
Inspekt::convertArrayToArrayObject($arr[$key]);
}
}
return new ArrayObject($arr);
}
/**
* Returns only the alphabetic characters in value.
*
* @param mixed $value
* @return mixed
*
* @tag filter
*/
static public function getAlpha($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getAlpha');
} else {
return preg_replace('/[^[:alpha:]]/', '', $value);
}
}
/**
* Returns only the alphabetic characters and digits in value.
*
* @param mixed $value
* @return mixed
*
* @tag filter
*
* @assert('1)@*(&UR)HQ)W(*(HG))') === '1URHQWHG'
*/
static public function getAlnum($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getAlnum');
} else {
return preg_replace('/[^[:alnum:]]/', '', $value);
}
}
/**
* Returns only the digits in value.
*
* @param mixed $value
* @return mixed
*
* @tag filter
*
* @assert('1)@*(&UR)HQ)56W(*(HG))') === '156'
*/
static public function getDigits($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getDigits');
} else {
return preg_replace('/[^[:digit:]]/', '', $value);
}
}
/**
* Returns dirname(value).
*
* @param mixed $value
* @return mixed
*
* @tag filter
*
* @assert('/usr/lib/php/Pear.php') === '/usr/lib/php'
*/
static public function getDir($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getDir');
} else {
return dirname($value);
}
}
/**
* Returns (int) value.
*
* @param mixed $value
* @return int
*
* @tag filter
*
* @assert('1)45@*(&UR)HQ)W.0000(*(HG))') === 1
* @assert('A1)45@*(&UR)HQ)W.0000(*(HG))') === 0
*/
static public function getInt($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getInt');
} else {
return (int) $value;
}
}
/**
* Returns realpath(value).
*
* @param mixed $value
* @return mixed
*
* @tag filter
*/
static public function getPath($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getPath');
} else {
return realpath($value);
}
}
/**
* Returns the value encoded as ROT13 (or decoded, if already was ROT13)
*
* @param mixed $value
* @return mixed
*
* @link http://php.net/manual/en/function.str-rot13.php
*/
static public function getROT13($value)
{
if (Inspekt::isArrayOrArrayObject($value)) {
return Inspekt::_walkArray($value, 'getROT13');
} else {
return str_rot13($value);
}
}
/**
* Returns true if every character is alphabetic or a digit,
* false otherwise.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*
* @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === true
* @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false
* @assert('funkatron') === true
* @assert('funkatron_user') === false
* @assert('funkatron-user') === false
* @assert('_funkatronuser') === false
*/
static public function isAlnum($value)
{
return ctype_alnum($value);
}
/**
* Returns true if every character is alphabetic, false
* otherwise.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*
* @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === false
* @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false
* @assert('funkatron') === true
* @assert('funkatron_user') === false
* @assert('funkatron-user') === false
* @assert('_funkatronuser') === false
*/
static public function isAlpha($value)
{
return ctype_alpha($value);
}
/**
* Returns true if value is greater than or equal to $min and less
* than or equal to $max, false otherwise. If $inc is set to
* false, then the value must be strictly greater than $min and
* strictly less than $max.
*
* @param mixed $value
* @param mixed $min
* @param mixed $max
* @return boolean
*
* @tag validator
*
* @assert(12, 0, 12) === true
* @assert(12, 0, 12, false) === false
* @assert('f', 'a', 'm', false) === true
* @assert('p', 'a', 'm', false) === false
*/
static public function isBetween($value, $min, $max, $inc = true)
{
if ($value > $min &&
$value < $max) {
return true;
}
if ($inc &&
$value >= $min &&
$value <= $max) {
return true;
}
return false;
}
/**
* Returns true if it is a valid credit card number format. The
* optional second argument allows developers to indicate the
* type.
*
* @param mixed $value
* @param mixed $type
* @return boolean
*
* @tag validator
*/
static public function isCcnum($value, $type = null)
{
/**
* @todo Type-specific checks
*/
if (isset($type)) {
Inspekt_Error::raiseError('Type-specific cc checks are not yet supported');
}
$value = self::getDigits($value);
$length = strlen($value);
if ($length < 13 || $length > 19) {
return false;
}
$sum = 0;
$weight = 2;
for ($i = $length - 2; $i >= 0; $i--) {
$digit = $weight * $value[$i];
$sum += floor($digit / 10) + $digit % 10;
$weight = $weight % 2 + 1;
}
$mod = (10 - $sum % 10) % 10;
return ($mod == $value[$length - 1]);
}
/**
* Returns true if value is a valid date, false otherwise. The
* date is required to be in ISO 8601 format.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*
* @assert('2009-06-30') === true
* @assert('2009-06-31') === false
* @assert('2009-6-30') === true
* @assert('2-6-30') === true
*/
static public function isDate($value)
{
list($year, $month, $day) = sscanf($value, '%d-%d-%d');
return checkdate($month, $day, $year);
}
/**
* Returns true if every character is a digit, false otherwise.
* This is just like isInt(), except there is no upper limit.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*
* @assert('1029438750192730t91740987023948') === false
* @assert('102943875019273091740987023948') === true
* @assert(102943875019273091740987023948) === false
*/
static public function isDigits($value)
{
return ctype_digit((string) $value);
}
/**
* Returns true if value is a valid email format, false otherwise.
*
* @param string $value
* @return boolean
* @see http://www.regular-expressions.info/email.html
* @see Inspekt::VALID_EMAIL_REGEX
*
* @tag validator
*
* @assert('[email protected]') === true
* @assert('[email protected]') === true
* @assert('[email protected]') === false
* @assert('@poop.com') === false
* @assert('a@b') === false
* @assert('webmaster') === false
*/
static public function isEmail($value)
{
return (bool) preg_match(self::VALID_EMAIL_REGEX, $value);
}
/**
* Returns true if value is a valid float value, false otherwise.
*
* @param string $value
* @return boolean
*
* @assert(10244578109.234451) === true
* @assert('10244578109.234451') === false
* @assert('10,244,578,109.234451') === false
*
* @tag validator
*/
static public function isFloat($value)
{
$locale = localeconv();
$value = str_replace($locale['decimal_point'], '.', $value);
$value = str_replace($locale['thousands_sep'], '', $value);
return (strval(floatval($value)) == $value);
}
/**
* Returns true if value is greater than $min, false otherwise. Note that
* comparisons with NULL do not work the same way as SQL, ex. "1 > null" is
* true
*
* @param mixed $value
* @param mixed $min
* @return boolean
*
* @tag validator
*
* @assert(5, 0) === true
* @assert(2, 10) === false
* @assert('b', 'a') === true
* @assert('a', 'b') === false
*
* @todo missing $min is a really bad idea considering the odd null behavior. should that throw an error?
*/
static public function isGreaterThan($value, $min)
{
return ($value > $min);
}
/**
* Returns true if value is a valid hexadecimal format, false
* otherwise.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*
* @assert('6F') === true
* @assert('F6') === true
*
*/
static public function isHex($value)
{
return ctype_xdigit($value);
}
/**
* Returns true if value is a valid hostname, false otherwise.
* Depending upon the value of $allow, Internet domain names, IP
* addresses, and/or local network names are considered valid.
* The default is HOST_ALLOW_ALL, which considers all of the
* above to be valid.
*
* @param mixed $value
* @param integer $allow bitfield for ISPK_HOST_ALLOW_DNS, ISPK_HOST_ALLOW_IP, ISPK_HOST_ALLOW_LOCAL
* @return boolean
*
* @tag validator
*/
static public function isHostname($value, $allow = ISPK_HOST_ALLOW_ALL)
{
if (!is_numeric($allow) || !is_int($allow)) {
Inspekt_Error::raiseError('Illegal value for $allow; expected an integer', E_USER_WARNING);
}
if ($allow < ISPK_HOST_ALLOW_DNS || ISPK_HOST_ALLOW_ALL < $allow) {
Inspekt_Error::raiseError('Illegal value for $allow; expected integer between ' . ISPK_HOST_ALLOW_DNS . ' and ' . ISPK_HOST_ALLOW_ALL, E_USER_WARNING);
}
// determine whether the input is formed as an IP address
$status = self::isIp($value);
// if the input looks like an IP address
if ($status) {
// if IP addresses are not allowed, then fail validation
if (($allow & ISPK_HOST_ALLOW_IP) == 0) {
return false;
}
// IP passed validation
return true;
}
// check input against domain name schema
$status = @preg_match(ISPK_DNS_VALID, $value);
if ($status === false) {
Inspekt_Error::raiseError('Internal error: DNS validation failed', E_USER_WARNING);
}
// if the input passes as an Internet domain name, and domain names are allowed, then the hostname
// passes validation
if ($status == 1 && ($allow & ISPK_HOST_ALLOW_DNS) != 0) {
return true;
}
// if local network names are not allowed, then fail validation
if (($allow & ISPK_HOST_ALLOW_LOCAL) == 0) {
return false;
}
// check input against local network name schema; last chance to pass validation
$status = @preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)*(?:[^\W_](?:[^\W_]|-){0,61}[^\W_])\.?$/',
$value);
if ($status === false) {
Inspekt_Error::raiseError('Internal error: local network name validation failed', E_USER_WARNING);
}
if ($status == 0) {
return false;
} else {
return true;
}
}
/**
* Returns true if value is a valid integer value, false otherwise.
*
* @param string|array $value
* @return boolean
*
* @tag validator
*
* @todo better handling of diffs b/t 32-bit and 64-bit
*/
static public function isInt($value)
{
$locale = localeconv();
$value = str_replace($locale['decimal_point'], '.', $value);
$value = str_replace($locale['thousands_sep'], '', $value);
$is_valid = (
is_numeric($value) // Must be able to be converted to a number
&& preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers)
&& bccomp($value, "-9223372036854775807") >= 0 // Must be greater than than min of 64-bit
&& bccomp($value, "9223372036854775807") <= 0 // Must be less than max of 64-bit
);
if (!$is_valid) {
return false;
} else {
return true;
}
// return (strval(intval($value)) === $value);
}
/**
* Returns true if value is a valid IP format, false otherwise.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*/
static public function isIp($value)
{
return (bool) ip2long($value);
}
/**
* Returns true if value is less than $max, false otherwise.
*
* @param mixed $value
* @param mixed $max
* @return boolean
*
* @tag validator
*/
static public function isLessThan($value, $max)
{
return ($value < $max);
}
/**
* Returns true if value is one of $allowed, false otherwise.
*
* @param mixed $value
* @param array|string $allowed
* @return boolean
*
* @tag validator
*/
static public function isOneOf($value, $allowed)
{
/**
* @todo: Consider allowing a string for $allowed, where each
* character in the string is an allowed character in the
* value.
*/
if (is_string($allowed)) {
$allowed = str_split($allowed);
}
return in_array($value, $allowed);
}
/**
* Returns true if value is a valid phone number format, false
* otherwise. The optional second argument indicates the country.
* This method requires that the value consist of only digits.
*
* @param mixed $value
* @return boolean
*
* @tag validator
*/
static public function isPhone($value, $country = 'US')
{
if (!ctype_digit($value)) {
return false;
}
switch ($country) {
case 'US':
if (strlen($value) != 10) {
return false;
}
$areaCode = substr($value, 0, 3);
$areaCodes = array(201, 202, 203, 204, 205, 206, 207, 208,
209, 210, 212, 213, 214, 215, 216, 217,
218, 219, 224, 225, 226, 228, 229, 231,
234, 239, 240, 242, 246, 248, 250, 251,
252, 253, 254, 256, 260, 262, 264, 267,
268, 269, 270, 276, 281, 284, 289, 301,
302, 303, 304, 305, 306, 307, 308, 309,
310, 312, 313, 314, 315, 316, 317, 318,
319, 320, 321, 323, 325, 330, 334, 336,
337, 339, 340, 345, 347, 351, 352, 360,
361, 386, 401, 402, 403, 404, 405, 406,
407, 408, 409, 410, 412, 413, 414, 415,
416, 417, 418, 419, 423, 424, 425, 430,
432, 434, 435, 438, 440, 441, 443, 445,
450, 469, 470, 473, 475, 478, 479, 480,
484, 501, 502, 503, 504, 505, 506, 507,
508, 509, 510, 512, 513, 514, 515, 516,
517, 518, 519, 520, 530, 540, 541, 555,
559, 561, 562, 563, 564, 567, 570, 571,
573, 574, 580, 585, 586, 600, 601, 602,
603, 604, 605, 606, 607, 608, 609, 610,
612, 613, 614, 615, 616, 617, 618, 619,
620, 623, 626, 630, 631, 636, 641, 646,
647, 649, 650, 651, 660, 661, 662, 664,
670, 671, 678, 682, 684, 700, 701, 702,
703, 704, 705, 706, 707, 708, 709, 710,
712, 713, 714, 715, 716, 717, 718, 719,
720, 724, 727, 731, 732, 734, 740, 754,
757, 758, 760, 763, 765, 767, 769, 770,
772, 773, 774, 775, 778, 780, 781, 784,
785, 786, 787, 800, 801, 802, 803, 804,
805, 806, 807, 808, 809, 810, 812, 813,
814, 815, 816, 817, 818, 819, 822, 828,
829, 830, 831, 832, 833, 835, 843, 844,
845, 847, 848, 850, 855, 856, 857, 858,
859, 860, 863, 864, 865, 866, 867, 868,
869, 870, 876, 877, 878, 888, 900, 901,
902, 903, 904, 905, 906, 907, 908, 909,
910, 912, 913, 914, 915, 916, 917, 918,
919, 920, 925, 928, 931, 936, 937, 939,
940, 941, 947, 949, 951, 952, 954, 956,
959, 970, 971, 972, 973, 978, 979, 980,
985, 989);
return in_array($areaCode, $areaCodes);
break;
default:
Inspekt_Error::raiseError('isPhone() does not yet support this country.', E_USER_WARNING);
return false;
break;
}
}
/**
* Returns true if value matches $pattern, false otherwise. Uses
* preg_match() for the matching.
*
* @param mixed $value
* @param mixed $pattern
* @return mixed
*
* @tag validator
*/
static public function isRegex($value, $pattern)
{
return (bool) preg_match($pattern, $value);
}
/**