Changeset 2258
- Timestamp:
- 11/26/10 16:01:21 (18 months ago)
- Files:
-
- 1 modified
-
branches/1.5/libs/extensions/Inspekt/Inspekt.php (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.5/libs/extensions/Inspekt/Inspekt.php
r2164 r2258 107 107 static public function makeServerCage($config_file = null, $strict = true) 108 108 { 109 /**110 * @staticvar $_instance111 */112 static $_instance;113 114 if (!isset($_instance)) {115 $_instance = Inspekt_Cage::Factory($_SERVER, $config_file, '_SERVER', $strict);116 }117 $GLOBALS['HTTP_SERVER_VARS'] = null;118 return $_instance;109 /** 110 * @staticvar $_instance 111 */ 112 static $_instance; 113 114 if (!isset($_instance)) { 115 $_instance = Inspekt_Cage::Factory($_SERVER, $config_file, '_SERVER', $strict); 116 } 117 $GLOBALS['HTTP_SERVER_VARS'] = null; 118 return $_instance; 119 119 } 120 120 … … 130 130 static public function makeGetCage($config_file = null, $strict = true) 131 131 { 132 /**133 * @staticvar $_instance134 */135 static $_instance;136 137 if (!isset($_instance)) {138 $_instance = Inspekt_Cage::Factory($_GET, $config_file, '_GET', $strict);139 }140 $GLOBALS['HTTP_GET_VARS'] = null;141 return $_instance;132 /** 133 * @staticvar $_instance 134 */ 135 static $_instance; 136 137 if (!isset($_instance)) { 138 $_instance = Inspekt_Cage::Factory($_GET, $config_file, '_GET', $strict); 139 } 140 $GLOBALS['HTTP_GET_VARS'] = null; 141 return $_instance; 142 142 } 143 143 … … 153 153 static public function makePostCage($config_file = null, $strict = true) 154 154 { 155 /**156 * @staticvar $_instance157 */158 static $_instance;159 160 if (!isset($_instance)) {161 $_instance = Inspekt_Cage::Factory($_POST, $config_file, '_POST', $strict);162 }163 $GLOBALS['HTTP_POST_VARS'] = null;164 return $_instance;155 /** 156 * @staticvar $_instance 157 */ 158 static $_instance; 159 160 if (!isset($_instance)) { 161 $_instance = Inspekt_Cage::Factory($_POST, $config_file, '_POST', $strict); 162 } 163 $GLOBALS['HTTP_POST_VARS'] = null; 164 return $_instance; 165 165 } 166 166 … … 176 176 static public function makeCookieCage($config_file = null, $strict = true) 177 177 { 178 /**179 * @staticvar $_instance180 */181 static $_instance;182 183 if (!isset($_instance)) {184 $_instance = Inspekt_Cage::Factory($_COOKIE, $config_file, '_COOKIE', $strict);185 }186 $GLOBALS['HTTP_COOKIE_VARS'] = null;187 return $_instance;178 /** 179 * @staticvar $_instance 180 */ 181 static $_instance; 182 183 if (!isset($_instance)) { 184 $_instance = Inspekt_Cage::Factory($_COOKIE, $config_file, '_COOKIE', $strict); 185 } 186 $GLOBALS['HTTP_COOKIE_VARS'] = null; 187 return $_instance; 188 188 } 189 189 … … 199 199 static public function makeEnvCage($config_file = null, $strict = true) 200 200 { 201 /**202 * @staticvar $_instance203 */204 static $_instance;205 206 if (!isset($_instance)) {207 $_instance = Inspekt_Cage::Factory($_ENV, $config_file, '_ENV', $strict);208 }209 $GLOBALS['HTTP_ENV_VARS'] = null;210 return $_instance;201 /** 202 * @staticvar $_instance 203 */ 204 static $_instance; 205 206 if (!isset($_instance)) { 207 $_instance = Inspekt_Cage::Factory($_ENV, $config_file, '_ENV', $strict); 208 } 209 $GLOBALS['HTTP_ENV_VARS'] = null; 210 return $_instance; 211 211 } 212 212 … … 222 222 static public function makeFilesCage($config_file = null, $strict = true) 223 223 { 224 /**225 * @staticvar $_instance226 */227 static $_instance;228 229 if (!isset($_instance)) {230 $_instance = Inspekt_Cage::Factory($_FILES, $config_file, '_FILES', $strict);231 }232 $GLOBALS['HTTP_POST_FILES'] = null;233 return $_instance;224 /** 225 * @staticvar $_instance 226 */ 227 static $_instance; 228 229 if (!isset($_instance)) { 230 $_instance = Inspekt_Cage::Factory($_FILES, $config_file, '_FILES', $strict); 231 } 232 $GLOBALS['HTTP_POST_FILES'] = null; 233 return $_instance; 234 234 } 235 235 … … 246 246 static public function makeSessionCage($config_file = null, $strict = true) 247 247 { 248 Inspekt_Error::raiseError('makeSessionCage is disabled in this version', E_USER_ERROR);249 250 /**251 * @staticvar $_instance252 */253 static $_instance;254 255 if (!isset($_SESSION)) {256 return null;257 }258 259 if (!isset($_instance)) {260 $_instance = Inspekt_Cage_Session::Factory($_SESSION, $config_file, '_SESSION', $strict);261 }262 $GLOBALS['HTTP_SESSION_VARS'] = null;263 return $_instance;248 Inspekt_Error::raiseError('makeSessionCage is disabled in this version', E_USER_ERROR); 249 250 /** 251 * @staticvar $_instance 252 */ 253 static $_instance; 254 255 if (!isset($_SESSION)) { 256 return null; 257 } 258 259 if (!isset($_instance)) { 260 $_instance = Inspekt_Cage_Session::Factory($_SESSION, $config_file, '_SESSION', $strict); 261 } 262 $GLOBALS['HTTP_SESSION_VARS'] = null; 263 return $_instance; 264 264 } 265 265 … … 273 273 static public function makeSuperCage($config_file = null, $strict = true) 274 274 { 275 /**276 * @staticvar $_instance277 */278 static $_scinstance;279 280 if (!isset($_scinstance)) {281 $_scinstance = Inspekt_Supercage::Factory($config_file, $strict);282 }283 return $_scinstance;275 /** 276 * @staticvar $_instance 277 */ 278 static $_scinstance; 279 280 if (!isset($_scinstance)) { 281 $_scinstance = Inspekt_Supercage::Factory($config_file, $strict); 282 } 283 return $_scinstance; 284 284 } 285 285 … … 295 295 static public function useFilterExt($state = null) 296 296 { 297 if (isset($state)) {298 Inspekt::$useFilterExtension = (bool) $state;299 }300 return Inspekt::$useFilterExtension;297 if (isset($state)) { 298 Inspekt::$useFilterExtension = (bool) $state; 299 } 300 return Inspekt::$useFilterExtension; 301 301 } 302 302 … … 314 314 static protected function _walkArray($input, $method, $classname = null) 315 315 { 316 if (!isset($classname)) {317 $classname = __CLASS__;318 }319 320 if (!self::isArrayOrArrayObject($input) ) {321 Inspekt_Error::raiseError('$input must be an array or ArrayObject', E_USER_ERROR);322 return false;323 }324 325 if (!is_callable(array($classname, $method))) {326 Inspekt_Error::raiseError('Inspektor ' . $classname . '::' . $method . ' is invalid', E_USER_ERROR);327 return false;328 }329 330 foreach ($input as $key => $val) {331 if (is_array($val)) {332 $input[$key]=self::_walkArray($val, $method, $classname);333 } else {334 $val = call_user_func(array($classname, $method), $val);335 $input[$key] = $val;336 }337 }338 return $input;316 if (!isset($classname)) { 317 $classname = __CLASS__; 318 } 319 320 if (!self::isArrayOrArrayObject($input) ) { 321 Inspekt_Error::raiseError('$input must be an array or ArrayObject', E_USER_ERROR); 322 return false; 323 } 324 325 if (!is_callable(array($classname, $method))) { 326 Inspekt_Error::raiseError('Inspektor ' . $classname . '::' . $method . ' is invalid', E_USER_ERROR); 327 return false; 328 } 329 330 foreach ($input as $key => $val) { 331 if (is_array($val)) { 332 $input[$key]=self::_walkArray($val, $method, $classname); 333 } else { 334 $val = call_user_func(array($classname, $method), $val); 335 $input[$key] = $val; 336 } 337 } 338 return $input; 339 339 } 340 340 … … 348 348 static public function isArrayObject($obj) 349 349 { 350 $is = false;351 //$is = (is_object($obj) && get_class($obj) === 'ArrayObject');352 $is = $obj instanceof ArrayObject;353 return $is;350 $is = false; 351 //$is = (is_object($obj) && get_class($obj) === 'ArrayObject'); 352 $is = $obj instanceof ArrayObject; 353 return $is; 354 354 } 355 355 … … 363 363 static public function isArrayOrArrayObject($arr) 364 364 { 365 $is = false;366 $is = $arr instanceof ArrayObject || is_array($arr);367 return $is;365 $is = false; 366 $is = $arr instanceof ArrayObject || is_array($arr); 367 return $is; 368 368 } 369 369 … … 375 375 static public function convertArrayToArrayObject(&$arr) 376 376 { 377 foreach ($arr as $key => $value) {378 if (is_array($value)) {379 $value = new ArrayObject($value);380 $arr[$key] = $value;381 //echo $key." is an array\n";382 Inspekt::convertArrayToArrayObject($arr[$key]);383 }384 }385 386 return new ArrayObject($arr);377 foreach ($arr as $key => $value) { 378 if (is_array($value)) { 379 $value = new ArrayObject($value); 380 $arr[$key] = $value; 381 //echo $key." is an array\n"; 382 Inspekt::convertArrayToArrayObject($arr[$key]); 383 } 384 } 385 386 return new ArrayObject($arr); 387 387 } 388 388 … … 397 397 static public function getAlpha($value) 398 398 { 399 if (Inspekt::isArrayOrArrayObject($value)) {400 return Inspekt::_walkArray($value, 'getAlpha');401 } else {402 return preg_replace('/[^[:alpha:]]/', '', $value);403 }399 if (Inspekt::isArrayOrArrayObject($value)) { 400 return Inspekt::_walkArray($value, 'getAlpha'); 401 } else { 402 return preg_replace('/[^[:alpha:]]/', '', $value); 403 } 404 404 } 405 405 … … 416 416 static public function getAlnum($value) 417 417 { 418 if (Inspekt::isArrayOrArrayObject($value)) {419 return Inspekt::_walkArray($value, 'getAlnum');420 } else {421 return preg_replace('/[^[:alnum:]]/', '', $value);422 }418 if (Inspekt::isArrayOrArrayObject($value)) { 419 return Inspekt::_walkArray($value, 'getAlnum'); 420 } else { 421 return preg_replace('/[^[:alnum:]]/', '', $value); 422 } 423 423 } 424 424 … … 435 435 static public function getDigits($value) 436 436 { 437 if (Inspekt::isArrayOrArrayObject($value)) {438 return Inspekt::_walkArray($value, 'getDigits');439 } else {440 return preg_replace('/[^[:digit:]]/', '', $value);441 }437 if (Inspekt::isArrayOrArrayObject($value)) { 438 return Inspekt::_walkArray($value, 'getDigits'); 439 } else { 440 return preg_replace('/[^[:digit:]]/', '', $value); 441 } 442 442 } 443 443 … … 454 454 static public function getDir($value) 455 455 { 456 if (Inspekt::isArrayOrArrayObject($value)) {457 return Inspekt::_walkArray($value, 'getDir');458 } else {459 return dirname($value);460 }456 if (Inspekt::isArrayOrArrayObject($value)) { 457 return Inspekt::_walkArray($value, 'getDir'); 458 } else { 459 return dirname($value); 460 } 461 461 } 462 462 … … 474 474 static public function getInt($value) 475 475 { 476 if (Inspekt::isArrayOrArrayObject($value)) {477 return Inspekt::_walkArray($value, 'getInt');478 } else {479 return (int) $value;480 }476 if (Inspekt::isArrayOrArrayObject($value)) { 477 return Inspekt::_walkArray($value, 'getInt'); 478 } else { 479 return (int) $value; 480 } 481 481 } 482 482 … … 491 491 static public function getPath($value) 492 492 { 493 if (Inspekt::isArrayOrArrayObject($value)) {494 return Inspekt::_walkArray($value, 'getPath');495 } else {496 return realpath($value);497 }493 if (Inspekt::isArrayOrArrayObject($value)) { 494 return Inspekt::_walkArray($value, 'getPath'); 495 } else { 496 return realpath($value); 497 } 498 498 } 499 499 … … 508 508 static public function getROT13($value) 509 509 { 510 if (Inspekt::isArrayOrArrayObject($value)) {511 return Inspekt::_walkArray($value, 'getROT13');512 } else {513 return str_rot13($value);514 }510 if (Inspekt::isArrayOrArrayObject($value)) { 511 return Inspekt::_walkArray($value, 'getROT13'); 512 } else { 513 return str_rot13($value); 514 } 515 515 } 516 516 … … 533 533 static public function isAlnum($value) 534 534 { 535 return ctype_alnum($value);535 return ctype_alnum($value); 536 536 } 537 537 … … 554 554 static public function isAlpha($value) 555 555 { 556 return ctype_alpha($value);556 return ctype_alpha($value); 557 557 } 558 558 … … 577 577 static public function isBetween($value, $min, $max, $inc = true) 578 578 { 579 if ($value > $min &&580 $value < $max) {581 return true;582 }583 584 if ($inc &&585 $value >= $min &&586 $value <= $max) {587 return true;588 }589 590 return false;579 if ($value > $min && 580 $value < $max) { 581 return true; 582 } 583 584 if ($inc && 585 $value >= $min && 586 $value <= $max) { 587 return true; 588 } 589 590 return false; 591 591 } 592 592 … … 604 604 static public function isCcnum($value, $type = null) 605 605 { 606 /**607 * @todo Type-specific checks608 */609 if (isset($type)) {610 Inspekt_Error::raiseError('Type-specific cc checks are not yet supported');611 }612 613 $value = self::getDigits($value);614 $length = strlen($value);615 616 if ($length < 13 || $length > 19) {617 return false;618 }619 620 $sum = 0;621 $weight = 2;622 623 for ($i = $length - 2; $i >= 0; $i--) {624 $digit = $weight * $value[$i];625 $sum += floor($digit / 10) + $digit % 10;626 $weight = $weight % 2 + 1;627 }628 629 $mod = (10 - $sum % 10) % 10;630 631 return ($mod == $value[$length - 1]);606 /** 607 * @todo Type-specific checks 608 */ 609 if (isset($type)) { 610 Inspekt_Error::raiseError('Type-specific cc checks are not yet supported'); 611 } 612 613 $value = self::getDigits($value); 614 $length = strlen($value); 615 616 if ($length < 13 || $length > 19) { 617 return false; 618 } 619 620 $sum = 0; 621 $weight = 2; 622 623 for ($i = $length - 2; $i >= 0; $i--) { 624 $digit = $weight * $value[$i]; 625 $sum += floor($digit / 10) + $digit % 10; 626 $weight = $weight % 2 + 1; 627 } 628 629 $mod = (10 - $sum % 10) % 10; 630 631 return ($mod == $value[$length - 1]); 632 632 } 633 633 … … 648 648 static public function isDate($value) 649 649 { 650 list($year, $month, $day) = sscanf($value, '%d-%d-%d');651 652 return checkdate($month, $day, $year);650 list($year, $month, $day) = sscanf($value, '%d-%d-%d'); 651 652 return checkdate($month, $day, $year); 653 653 } 654 654 … … 668 668 static public function isDigits($value) 669 669 { 670 return ctype_digit((string) $value);670 return ctype_digit((string) $value); 671 671 } 672 672 … … 690 690 static public function isEmail($value) 691 691 { 692 return (bool) preg_match(ISPK_EMAIL_VALID, $value);692 return (bool) preg_match(ISPK_EMAIL_VALID, $value); 693 693 } 694 694 … … 707 707 static public function isFloat($value) 708 708 { 709 $locale = localeconv();710 $value = str_replace($locale['decimal_point'], '.', $value);711 $value = str_replace($locale['thousands_sep'], '', $value);712 713 return (strval(floatval($value)) == $value);709 $locale = localeconv(); 710 $value = str_replace($locale['decimal_point'], '.', $value); 711 $value = str_replace($locale['thousands_sep'], '', $value); 712 713 return (strval(floatval($value)) == $value); 714 714 } 715 715 … … 730 730 static public function isGreaterThan($value, $min) 731 731 { 732 return ($value > $min);732 return ($value > $min); 733 733 } 734 734 … … 748 748 static public function isHex($value) 749 749 { 750 return ctype_xdigit($value);750 return ctype_xdigit($value); 751 751 } 752 752 … … 766 766 static public function isHostname($value, $allow = ISPK_HOST_ALLOW_ALL) 767 767 { 768 if (!is_numeric($allow) || !is_int($allow)) {769 Inspekt_Error::raiseError('Illegal value for $allow; expected an integer', E_USER_WARNING);770 }771 772 if ($allow < ISPK_HOST_ALLOW_DNS || ISPK_HOST_ALLOW_ALL < $allow) {773 Inspekt_Error::raiseError('Illegal value for $allow; expected integer between ' . ISPK_HOST_ALLOW_DNS . ' and ' . ISPK_HOST_ALLOW_ALL, E_USER_WARNING);774 }775 776 // determine whether the input is formed as an IP address777 $status = self::isIp($value);778 779 // if the input looks like an IP address780 if ($status) {781 // if IP addresses are not allowed, then fail validation782 if (($allow & ISPK_HOST_ALLOW_IP) == 0) {783 return false;784 }785 786 // IP passed validation787 return true;788 }789 790 // check input against domain name schema791 $status = @preg_match('/^(?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?$/', $value);792 if ($status === false) {793 Inspekt_Error::raiseError('Internal error: DNS validation failed', E_USER_WARNING);794 }795 796 // if the input passes as an Internet domain name, and domain names are allowed, then the hostname797 // passes validation798 if ($status == 1 && ($allow & ISPK_HOST_ALLOW_DNS) != 0) {799 return true;800 }801 802 // if local network names are not allowed, then fail validation803 if (($allow & ISPK_HOST_ALLOW_LOCAL) == 0) {804 return false;805 }806 807 // check input against local network name schema; last chance to pass validation808 $status = @preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)*(?:[^\W_](?:[^\W_]|-){0,61}[^\W_])\.?$/',809 $value);810 if ($status === false) {811 Inspekt_Error::raiseError('Internal error: local network name validation failed', E_USER_WARNING);812 }813 814 if ($status == 0) {815 return false;816 } else {817 return true;818 }768 if (!is_numeric($allow) || !is_int($allow)) { 769 Inspekt_Error::raiseError('Illegal value for $allow; expected an integer', E_USER_WARNING); 770 } 771 772 if ($allow < ISPK_HOST_ALLOW_DNS || ISPK_HOST_ALLOW_ALL < $allow) { 773 Inspekt_Error::raiseError('Illegal value for $allow; expected integer between ' . ISPK_HOST_ALLOW_DNS . ' and ' . ISPK_HOST_ALLOW_ALL, E_USER_WARNING); 774 } 775 776 // determine whether the input is formed as an IP address 777 $status = self::isIp($value); 778 779 // if the input looks like an IP address 780 if ($status) { 781 // if IP addresses are not allowed, then fail validation 782 if (($allow & ISPK_HOST_ALLOW_IP) == 0) { 783 return false; 784 } 785 786 // IP passed validation 787 return true; 788 } 789 790 // check input against domain name schema 791 $status = @preg_match('/^(?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?$/', $value); 792 if ($status === false) { 793 Inspekt_Error::raiseError('Internal error: DNS validation failed', E_USER_WARNING); 794 } 795 796 // if the input passes as an Internet domain name, and domain names are allowed, then the hostname 797 // passes validation 798 if ($status == 1 && ($allow & ISPK_HOST_ALLOW_DNS) != 0) { 799 return true; 800 } 801 802 // if local network names are not allowed, then fail validation 803 if (($allow & ISPK_HOST_ALLOW_LOCAL) == 0) { 804 return false; 805 } 806 807 // check input against local network name schema; last chance to pass validation 808 $status = @preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)*(?:[^\W_](?:[^\W_]|-){0,61}[^\W_])\.?$/', 809 $value); 810 if ($status === false) { 811 Inspekt_Error::raiseError('Internal error: local network name validation failed', E_USER_WARNING); 812 } 813 814 if ($status == 0) { 815 return false; 816 } else { 817 return true; 818 } 819 819 } 820 820 … … 831 831 static public function isInt($value) 832 832 { 833 $locale = localeconv(); 834 835 $value = str_replace($locale['decimal_point'], '.', $value); 836 $value = str_replace($locale['thousands_sep'], '', $value); 837 838 $is_valid = ( 839 is_numeric($value) // Must be able to be converted to a number 840 && preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers) 841 && bccomp($value, "-9223372036854775807") >= 0 // Must be greater than than min of 64-bit 842 && bccomp($value, "9223372036854775807") <= 0 // Must be less than max of 64-bit 843 ); 844 if (!$is_valid) { 845 return false; 846 } else { 847 return true; 848 } 849 // return (strval(intval($value)) === $value); 833 $locale = localeconv(); 834 835 $value = str_replace($locale['decimal_point'], '.', $value); 836 $value = str_replace($locale['thousands_sep'], '', $value); 837 838 /* $is_valid = ( 839 is_numeric($value) // Must be able to be converted to a number 840 && preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers) 841 && bccomp($value, "-9223372036854775807") >= 0 // Must be greater than than min of 64-bit 842 && bccomp($value, "9223372036854775807") <= 0 // Must be less than max of 64-bit 843 );*/ 844 845 /* remove Hotaru's dependency on BC Math */ 846 $is_valid = ( 847 is_numeric($value) // Must be able to be converted to a number 848 && preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers) 849 && ($value >= -9223372036854775807) // Must be greater than than min of 64-bit 850 && ($value <= 9223372036854775807) // Must be less than max of 64-bit 851 ); 852 if (!$is_valid) { 853 return false; 854 } else { 855 return true; 856 } 857 // return (strval(intval($value)) === $value); 850 858 } 851 859 … … 860 868 static public function isIp($value) 861 869 { 862 return (bool) ip2long($value);870 return (bool) ip2long($value); 863 871 } 864 872 … … 874 882 static public function isLessThan($value, $max) 875 883 { 876 return ($value < $max);884 return ($value < $max); 877 885 } 878 886 … … 888 896 static public function isOneOf($value, $allowed) 889 897 { 890 /**891 * @todo: Consider allowing a string for $allowed, where each892 * character in the string is an allowed character in the893 * value.894 */895 896 if (is_string($allowed)) {897 $allowed = str_split($allowed);898 }899 900 return in_array($value, $allowed);898 /** 899 * @todo: Consider allowing a string for $allowed, where each 900 * character in the string is an allowed character in the 901 * value. 902 */ 903 904 if (is_string($allowed)) { 905 $allowed = str_split($allowed); 906 } 907 908 return in_array($value, $allowed); 901 909 } 902 910 … … 913 921 static public function isPhone($value, $country = 'US') 914 922 { 915 if (!ctype_digit($value)) {916 return false;917 }918 919 switch ($country) {920 case 'US':921 if (strlen($value) != 10) {922 return false;923 }924 925 $areaCode = substr($value, 0, 3);926 927 $areaCodes = array(201, 202, 203, 204, 205, 206, 207, 208,928 209, 210, 212, 213, 214, 215, 216, 217,929 218, 219, 224, 225, 226, 228, 229, 231,930 234, 239, 240, 242, 246, 248, 250, 251,931 252, 253, 254, 256, 260, 262, 264, 267,932 268, 269, 270, 276, 281, 284, 289, 301,933 302, 303, 304, 305, 306, 307, 308, 309,934 310, 312, 313, 314, 315, 316, 317, 318,935 319, 320, 321, 323, 325, 330, 334, 336,936 337, 339, 340, 345, 347, 351, 352, 360,937 361, 386, 401, 402, 403, 404, 405, 406,938 407, 408, 409, 410, 412, 413, 414, 415,939 416, 417, 418, 419, 423, 424, 425, 430,940 432, 434, 435, 438, 440, 441, 443, 445,941 450, 469, 470, 473, 475, 478, 479, 480,942 484, 501, 502, 503, 504, 505, 506, 507,943 508, 509, 510, 512, 513, 514, 515, 516,944 517, 518, 519, 520, 530, 540, 541, 555,945 559, 561, 562, 563, 564, 567, 570, 571,946 573, 574, 580, 585, 586, 600, 601, 602,947 603, 604, 605, 606, 607, 608, 609, 610,948 612, 613, 614, 615, 616, 617, 618, 619,949 620, 623, 626, 630, 631, 636, 641, 646,950 647, 649, 650, 651, 660, 661, 662, 664,951 670, 671, 678, 682, 684, 700, 701, 702,952 703, 704, 705, 706, 707, 708, 709, 710,953 712, 713, 714, 715, 716, 717, 718, 719,954 720, 724, 727, 731, 732, 734, 740, 754,955 757, 758, 760, 763, 765, 767, 769, 770,956 772, 773, 774, 775, 778, 780, 781, 784,957 785, 786, 787, 800, 801, 802, 803, 804,958 805, 806, 807, 808, 809, 810, 812, 813,959 814, 815, 816, 817, 818, 819, 822, 828,960 829, 830, 831, 832, 833, 835, 843, 844,961 845, 847, 848, 850, 855, 856, 857, 858,962 859, 860, 863, 864, 865, 866, 867, 868,963 869, 870, 876, 877, 878, 888, 900, 901,964 902, 903, 904, 905, 906, 907, 908, 909,965 910, 912, 913, 914, 915, 916, 917, 918,966 919, 920, 925, 928, 931, 936, 937, 939,967 940, 941, 947, 949, 951, 952, 954, 956,968 959, 970, 971, 972, 973, 978, 979, 980,969 985, 989);970 971 return in_array($areaCode, $areaCodes);972 break;973 default:974 Inspekt_Error::raiseError('isPhone() does not yet support this country.', E_USER_WARNING);975 return false;976 break;977 }923 if (!ctype_digit($value)) { 924 return false; 925 } 926 927 switch ($country) { 928 case 'US': 929 if (strlen($value) != 10) { 930 return false; 931 } 932 933 $areaCode = substr($value, 0, 3); 934 935 $areaCodes = array(201, 202, 203, 204, 205, 206, 207, 208, 936 209, 210, 212, 213, 214, 215, 216, 217, 937 218, 219, 224, 225, 226, 228, 229, 231, 938 234, 239, 240, 242, 246, 248, 250, 251, 939 252, 253, 254, 256, 260, 262, 264, 267, 940 268, 269, 270, 276, 281, 284, 289, 301, 941 302, 303, 304, 305, 306, 307, 308, 309, 942 310, 312, 313, 314, 315, 316, 317, 318, 943 319, 320, 321, 323, 325, 330, 334, 336, 944 337, 339, 340, 345, 347, 351, 352, 360, 945 361, 386, 401, 402, 403, 404, 405, 406, 946 407, 408, 409, 410, 412, 413, 414, 415, 947 416, 417, 418, 419, 423, 424, 425, 430, 948 432, 434, 435, 438, 440, 441, 443, 445, 949 450, 469, 470, 473, 475, 478, 479, 480, 950 484, 501, 502, 503, 504, 505, 506, 507, 951 508, 509, 510, 512, 513, 514, 515, 516, 952 517, 518, 519, 520, 530, 540, 541, 555, 953 559, 561, 562, 563, 564, 567, 570, 571, 954 573, 574, 580, 585, 586, 600, 601, 602, 955 603, 604, 605, 606, 607, 608, 609, 610, 956 612, 613, 614, 615, 616, 617, 618, 619, 957 620, 623, 626, 630, 631, 636, 641, 646, 958 647, 649, 650, 651, 660, 661, 662, 664, 959 670, 671, 678, 682, 684, 700, 701, 702, 960 703, 704, 705, 706, 707, 708, 709, 710, 961 712, 713, 714, 715, 716, 717, 718, 719, 962 720, 724, 727, 731, 732, 734, 740, 754, 963 757, 758, 760, 763, 765, 767, 769, 770, 964 772, 773, 774, 775, 778, 780, 781, 784, 965 785, 786, 787, 800, 801, 802, 803, 804, 966 805, 806, 807, 808, 809, 810, 812, 813, 967 814, 815, 816, 817, 818, 819, 822, 828, 968 829, 830, 831, 832, 833, 835, 843, 844, 969 845, 847, 848, 850, 855, 856, 857, 858, 970 859, 860, 863, 864, 865, 866, 867, 868, 971 869, 870, 876, 877, 878, 888, 900, 901, 972 902, 903, 904, 905, 906, 907, 908, 909, 973 910, 912, 913, 914, 915, 916, 917, 918, 974 919, 920, 925, 928, 931, 936, 937, 939, 975 940, 941, 947, 949, 951, 952, 954, 956, 976 959, 970, 971, 972, 973, 978, 979, 980, 977 985, 989); 978 979 return in_array($areaCode, $areaCodes); 980 break; 981 default: 982 Inspekt_Error::raiseError('isPhone() does not yet support this country.', E_USER_WARNING); 983 return false; 984 break; 985 } 978 986 } 979 987 … … 990 998 static public function isRegex($value, $pattern) 991 999 { 992 return (bool) preg_match($pattern, $value);1000 return (bool) preg_match($pattern, $value); 993 1001 } 994 1002 … … 1006 1014 static public function isUri($value, $mode = ISPK_URI_ALLOW_COMMON) 1007 1015 { 1008 /**1009 * @todo1010 */1011 $regex = '';1012 switch ($mode) {1013 1014 // a common absolute URI: ftp, http or https1015 case ISPK_URI_ALLOW_COMMON:1016 1017 $regex .= '&';1018 $regex .= '^(ftp|http|https):'; // protocol1019 $regex .= '(//)'; // authority-start1020 $regex .= '([-a-z0-9/~;:@=+$,.!*()\']+@)?'; // userinfo1021 $regex .= '(';1022 $regex .= '((?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?)'; // domain name1023 $regex .= '|';1024 $regex .= '([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?(\.[0-9]{1,3})?)'; // OR ipv41025 $regex .= ')';1026 $regex .= '(:([0-9]*))?'; // port1027 $regex .= '(/((%[0-9a-f]{2}|[-_a-z0-9/~;:@=+$,.!*()\'\&]*)*)/?)?'; // path1028 $regex .= '(\?[^#]*)?'; // query1029 $regex .= '(#([-a-z0-9_]*))?'; // anchor (fragment)1030 $regex .= '$&i';1031 //echo "<pre>"; echo print_r($regex, true); echo "</pre>\n";1032 1033 break;1034 1035 case ISPK_URI_ALLOW_ABSOLUTE:1036 1037 Inspekt_Error::raiseError('isUri() for ISPK_URI_ALLOW_ABSOLUTE has not been implemented.', E_USER_WARNING);1038 return false;1016 /** 1017 * @todo 1018 */ 1019 $regex = ''; 1020 switch ($mode) { 1021 1022 // a common absolute URI: ftp, http or https 1023 case ISPK_URI_ALLOW_COMMON: 1024 1025 $regex .= '&'; 1026 $regex .= '^(ftp|http|https):'; // protocol 1027 $regex .= '(//)'; // authority-start 1028 $regex .= '([-a-z0-9/~;:@=+$,.!*()\']+@)?'; // userinfo 1029 $regex .= '('; 1030 $regex .= '((?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?)'; // domain name 1031 $regex .= '|'; 1032 $regex .= '([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?(\.[0-9]{1,3})?)'; // OR ipv4 1033 $regex .= ')'; 1034 $regex .= '(:([0-9]*))?'; // port 1035 $regex .= '(/((%[0-9a-f]{2}|[-_a-z0-9/~;:@=+$,.!*()\'\&]*)*)/?)?'; // path 1036 $regex .= '(\?[^#]*)?'; // query 1037 $regex .= '(#([-a-z0-9_]*))?'; // anchor (fragment) 1038 $regex .= '$&i'; 1039 //echo "<pre>"; echo print_r($regex, true); echo "</pre>\n"; 1040 1041 break; 1042 1043 case ISPK_URI_ALLOW_ABSOLUTE: 1044 1045 Inspekt_Error::raiseError('isUri() for ISPK_URI_ALLOW_ABSOLUTE has not been implemented.', E_USER_WARNING); 1046 return false; 1039 1047 1040 1048 // $regex .= '&'; … … 1052 1060 // $regex .= '(#([-a-z0-9_]*))?'; // anchor (fragment) 1053 1061 // $regex .= '$&i'; 1054 //echo "<pre>"; echo print_r($regex, true); echo "</pre>\n"; 1055 1056 break; 1057 1058 } 1059 $result = preg_match($regex, $value); 1060 1061 if ($result === 1) { 1062 return true; 1063 } else { 1064 return false; 1065 } 1062 //echo "<pre>"; echo print_r($regex, true); echo "</pre>\n"; 1063 1064 break; 1065 1066 } 1067 $result = preg_match($regex, $value); 1068 1069 if ($result === 1) { 1070 return true; 1071 } elseif (strstr($value, "http://localhost")) { // allow urls from localhost 1072 return true; 1073 } else { 1074 return false; 1075 } 1066 1076 } 1067 1077 … … 1076 1086 static public function isZip($value) 1077 1087 { 1078 return (bool) preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $value);1088 return (bool) preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $value); 1079 1089 } 1080 1090 … … 1091 1101 static public function noTags($value) 1092 1102 { 1093 if (Inspekt::isArrayOrArrayObject($value)) {1094 return Inspekt::_walkArray($value, 'noTags');1095 } else {1096 if (Inspekt::useFilterExt()) {1097 return filter_var($value, FILTER_SANITIZE_STRING);1098 } else {1099 return strip_tags($value);1100 }1101 }1103 if (Inspekt::isArrayOrArrayObject($value)) { 1104 return Inspekt::_walkArray($value, 'noTags'); 1105 } else { 1106 if (Inspekt::useFilterExt()) { 1107 return filter_var($value, FILTER_SANITIZE_STRING); 1108 } else { 1109 return strip_tags($value); 1110 } 1111 } 1102 1112 } 1103 1113 … … 1115 1125 static public function noTagsOrSpecial($value) 1116 1126 { 1117 if (Inspekt::isArrayOrArrayObject($value)) {1118 return Inspekt::_walkArray($value, 'noTagsOrSpecial');1119 } else {1120 if (Inspekt::useFilterExt()) {1121 $newval = filter_var($value, FILTER_SANITIZE_STRING);1122 $newval = filter_var($newval, FILTER_SANITIZE_SPECIAL_CHARS);1123 return $newval;1124 } else {1125 $newval = strip_tags($value);1126 $newval = htmlspecialchars($newval, ENT_QUOTES, 'UTF-8'); // for sake of simplicity and safety we assume UTF-81127 1128 /*1127 if (Inspekt::isArrayOrArrayObject($value)) { 1128 return Inspekt::_walkArray($value, 'noTagsOrSpecial'); 1129 } else { 1130 if (Inspekt::useFilterExt()) { 1131 $newval = filter_var($value, FILTER_SANITIZE_STRING); 1132 $newval = filter_var($newval, FILTER_SANITIZE_SPECIAL_CHARS); 1133 return $newval; 1134 } else { 1135 $newval = strip_tags($value); 1136 $newval = htmlspecialchars($newval, ENT_QUOTES, 'UTF-8'); // for sake of simplicity and safety we assume UTF-8 1137 1138 /* 1129 1139 convert low ascii chars to entities 1130 */1131 $newval = str_split($newval);1132 for ($i=0; $i < count($newval); $i++) {1133 $ascii_code = ord($newval[$i]);1134 if ($ascii_code < 32) {1135 $newval[$i] = "&#{$ascii_code};";1136 }1137 }1138 $newval = implode($newval);1139 1140 return $newval;1141 }1142 }1140 */ 1141 $newval = str_split($newval); 1142 for ($i=0; $i < count($newval); $i++) { 1143 $ascii_code = ord($newval[$i]); 1144 if ($ascii_code < 32) { 1145 $newval[$i] = "&#{$ascii_code};"; 1146 } 1147 } 1148 $newval = implode($newval); 1149 1150 return $newval; 1151 } 1152 } 1143 1153 } 1144 1154 … … 1153 1163 static public function noPath($value) 1154 1164 { 1155 if (Inspekt::isArrayOrArrayObject($value)) {1156 return Inspekt::_walkArray($value, 'noPath');1157 } else {1158 return basename($value);1159 }1165 if (Inspekt::isArrayOrArrayObject($value)) { 1166 return Inspekt::_walkArray($value, 'noPath'); 1167 } else { 1168 return basename($value); 1169 } 1160 1170 } 1161 1171 … … 1173 1183 static public function escMySQL($value, $conn = null) 1174 1184 { 1175 if (Inspekt::isArrayOrArrayObject($value)) {1176 return Inspekt::_walkArray($value, 'escMySQL');1177 } else {1178 //no explicit func to check if the connection is live, but if it's not $conn would be false1179 if (isset($conn) && is_resource($conn)) {1180 return mysql_real_escape_string($value, $conn);1181 } else {1182 return mysql_real_escape_string($value);1183 }1184 }1185 if (Inspekt::isArrayOrArrayObject($value)) { 1186 return Inspekt::_walkArray($value, 'escMySQL'); 1187 } else { 1188 //no explicit func to check if the connection is live, but if it's not $conn would be false 1189 if (isset($conn) && is_resource($conn)) { 1190 return mysql_real_escape_string($value, $conn); 1191 } else { 1192 return mysql_real_escape_string($value); 1193 } 1194 } 1185 1195 } 1186 1196 … … 1198 1208 static public function escPgSQL($value, $conn = null) 1199 1209 { 1200 if (Inspekt::isArrayOrArrayObject($value)) {1201 return Inspekt::_walkArray($value, 'escPgSQL');1202 } else {1203 //might also check is_resource if pg_connection_status is too much1204 if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) {1205 return pg_escape_string($conn, $value);1206 } else {1207 return pg_escape_string($value);1208 }1209 }1210 if (Inspekt::isArrayOrArrayObject($value)) { 1211 return Inspekt::_walkArray($value, 'escPgSQL'); 1212 } else { 1213 //might also check is_resource if pg_connection_status is too much 1214 if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) { 1215 return pg_escape_string($conn, $value); 1216 } else { 1217 return pg_escape_string($value); 1218 } 1219 } 1210 1220 } 1211 1221 … … 1221 1231 static public function escPgSQLBytea($value, $conn = null) 1222 1232 { 1223 if (Inspekt::isArrayOrArrayObject($value)) {1224 return Inspekt::_walkArray($value, 'escPgSQL');1225 } else {1226 //might also check is_resource if pg_connection_status is too much1227 if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) {1228 return pg_escape_bytea($conn, $value);1229 } else {1230 return pg_escape_bytea($value);1231 }1232 }1233 if (Inspekt::isArrayOrArrayObject($value)) { 1234 return Inspekt::_walkArray($value, 'escPgSQL'); 1235 } else { 1236 //might also check is_resource if pg_connection_status is too much 1237 if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) { 1238 return pg_escape_bytea($conn, $value); 1239 } else { 1240 return pg_escape_bytea($value); 1241 } 1242 } 1233 1243 } 1234 1244 }