| 94 | | protected static $useFilterExtension = true; |
| 95 | | |
| 96 | | /** |
| 97 | | * Returns the $_SERVER data wrapped in an Inspekt_Cage object |
| 98 | | * |
| 99 | | * This utilizes a singleton pattern to get around scoping issues |
| 100 | | * |
| 101 | | * @param string $config_file |
| 102 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 103 | | * @return Inspekt_Cage |
| 104 | | * |
| 105 | | * @assert() |
| 106 | | */ |
| 107 | | static public function makeServerCage($config_file = null, $strict = true) |
| 108 | | { |
| 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 | | } |
| 120 | | |
| 121 | | /** |
| 122 | | * Returns the $_GET data wrapped in an Inspekt_Cage object |
| 123 | | * |
| 124 | | * This utilizes a singleton pattern to get around scoping issues |
| 125 | | * |
| 126 | | * @param string $config_file |
| 127 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 128 | | * @return Inspekt_Cage |
| 129 | | */ |
| 130 | | static public function makeGetCage($config_file = null, $strict = true) |
| 131 | | { |
| 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 | | } |
| 143 | | |
| 144 | | /** |
| 145 | | * Returns the $_POST data wrapped in an Inspekt_Cage object |
| 146 | | * |
| 147 | | * This utilizes a singleton pattern to get around scoping issues |
| 148 | | * |
| 149 | | * @param string $config_file |
| 150 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 151 | | * @return Inspekt_Cage |
| 152 | | */ |
| 153 | | static public function makePostCage($config_file = null, $strict = true) |
| 154 | | { |
| 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 | | } |
| 166 | | |
| 167 | | /** |
| 168 | | * Returns the $_COOKIE data wrapped in an Inspekt_Cage object |
| 169 | | * |
| 170 | | * This utilizes a singleton pattern to get around scoping issues |
| 171 | | * |
| 172 | | * @param string $config_file |
| 173 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 174 | | * @return Inspekt_Cage |
| 175 | | */ |
| 176 | | static public function makeCookieCage($config_file = null, $strict = true) |
| 177 | | { |
| 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 | | } |
| 189 | | |
| 190 | | /** |
| 191 | | * Returns the $_ENV data wrapped in an Inspekt_Cage object |
| 192 | | * |
| 193 | | * This utilizes a singleton pattern to get around scoping issues |
| 194 | | * |
| 195 | | * @param string $config_file |
| 196 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 197 | | * @return Inspekt_Cage |
| 198 | | */ |
| 199 | | static public function makeEnvCage($config_file = null, $strict = true) |
| 200 | | { |
| 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 | | } |
| 212 | | |
| 213 | | /** |
| 214 | | * Returns the $_FILES data wrapped in an Inspekt_Cage object |
| 215 | | * |
| 216 | | * This utilizes a singleton pattern to get around scoping issues |
| 217 | | * |
| 218 | | * @param string $config_file |
| 219 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 220 | | * @return Inspekt_Cage |
| 221 | | */ |
| 222 | | static public function makeFilesCage($config_file = null, $strict = true) |
| 223 | | { |
| 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 | | } |
| 235 | | |
| 236 | | /** |
| 237 | | * Returns the $_SESSION data wrapped in an Inspekt_Cage object |
| 238 | | * |
| 239 | | * This utilizes a singleton pattern to get around scoping issues |
| 240 | | * |
| 241 | | * @param string $config_file |
| 242 | | * @param boolean $strict whether or not to nullify the superglobal array |
| 243 | | * @return Inspekt_Cage |
| 244 | | * @deprecated |
| 245 | | */ |
| 246 | | static public function makeSessionCage($config_file = null, $strict = true) |
| 247 | | { |
| 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 | | } |
| 265 | | |
| 266 | | /** |
| 267 | | * Returns a Supercage object, which wraps ALL input superglobals |
| 268 | | * |
| 269 | | * @param string $config_file |
| 270 | | * @param boolean $strict whether or not to nullify the superglobal |
| 271 | | * @return Inspekt_Supercage |
| 272 | | */ |
| 273 | | static public function makeSuperCage($config_file = null, $strict = true) |
| 274 | | { |
| 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 | | } |
| 285 | | |
| 286 | | /** |
| 287 | | * Sets and/or retrieves whether we should use the PHP filter extensions where possible |
| 288 | | * If a param is passed, it will set the state in addition to returning it |
| 289 | | * |
| 290 | | * We use this method of storing in a static class property so that we can access the value outside of class instances |
| 291 | | * |
| 292 | | * @param boolean $state optional |
| 293 | | * @return boolean |
| 294 | | */ |
| 295 | | static public function useFilterExt($state = null) |
| 296 | | { |
| 297 | | if (isset($state)) { |
| 298 | | Inspekt::$useFilterExtension = (bool) $state; |
| 299 | | } |
| 300 | | return Inspekt::$useFilterExtension; |
| 301 | | } |
| 302 | | |
| 303 | | /** |
| 304 | | * Recursively walks an array and applies a given filter method to |
| 305 | | * every value in the array. |
| 306 | | * |
| 307 | | * This should be considered a "protected" method, and not be called |
| 308 | | * outside of the class |
| 309 | | * |
| 310 | | * @param array|ArrayObject $input |
| 311 | | * @param string $inspektor The name of a static filtering method, like get* or no* |
| 312 | | * @return array |
| 313 | | */ |
| 314 | | static protected function _walkArray($input, $method, $classname = null) |
| 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; |
| 339 | | } |
| 340 | | |
| 341 | | /** |
| 342 | | * Checks to see if this is an ArrayObject |
| 343 | | * @param mixed |
| 344 | | * @return boolean |
| 345 | | * @deprecated |
| 346 | | * @link http://php.net/arrayobject |
| 347 | | */ |
| 348 | | static public function isArrayObject($obj) |
| 349 | | { |
| 350 | | $is = false; |
| 351 | | //$is = (is_object($obj) && get_class($obj) === 'ArrayObject'); |
| 352 | | $is = $obj instanceof ArrayObject; |
| 353 | | return $is; |
| 354 | | } |
| 355 | | |
| 356 | | /** |
| 357 | | * Checks to see if this is an array or an ArrayObject |
| 358 | | * @param mixed |
| 359 | | * @return boolean |
| 360 | | * @link http://php.net/arrayobject |
| 361 | | * @link http://php.net/array |
| 362 | | */ |
| 363 | | static public function isArrayOrArrayObject($arr) |
| 364 | | { |
| 365 | | $is = false; |
| 366 | | $is = $arr instanceof ArrayObject || is_array($arr); |
| 367 | | return $is; |
| 368 | | } |
| 369 | | |
| 370 | | /** |
| 371 | | * Converts an array into an ArrayObject. We use ArrayObjects when walking arrays in Inspekt |
| 372 | | * @param array |
| 373 | | * @return ArrayObject |
| 374 | | */ |
| 375 | | static public function convertArrayToArrayObject(&$arr) |
| 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); |
| 387 | | } |
| 388 | | |
| 389 | | /** |
| 390 | | * Returns only the alphabetic characters in value. |
| 391 | | * |
| 392 | | * @param mixed $value |
| 393 | | * @return mixed |
| 394 | | * |
| 395 | | * @tag filter |
| 396 | | */ |
| 397 | | static public function getAlpha($value) |
| 398 | | { |
| 399 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 400 | | return Inspekt::_walkArray($value, 'getAlpha'); |
| 401 | | } else { |
| 402 | | return preg_replace('/[^[:alpha:]]/', '', $value); |
| 403 | | } |
| 404 | | } |
| 405 | | |
| 406 | | /** |
| 407 | | * Returns only the alphabetic characters and digits in value. |
| 408 | | * |
| 409 | | * @param mixed $value |
| 410 | | * @return mixed |
| 411 | | * |
| 412 | | * @tag filter |
| 413 | | * |
| 414 | | * @assert('1)@*(&UR)HQ)W(*(HG))') === '1URHQWHG' |
| 415 | | */ |
| 416 | | static public function getAlnum($value) |
| 417 | | { |
| 418 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 419 | | return Inspekt::_walkArray($value, 'getAlnum'); |
| 420 | | } else { |
| 421 | | return preg_replace('/[^[:alnum:]]/', '', $value); |
| 422 | | } |
| 423 | | } |
| 424 | | |
| 425 | | /** |
| 426 | | * Returns only the digits in value. |
| 427 | | * |
| 428 | | * @param mixed $value |
| 429 | | * @return mixed |
| 430 | | * |
| 431 | | * @tag filter |
| 432 | | * |
| 433 | | * @assert('1)@*(&UR)HQ)56W(*(HG))') === '156' |
| 434 | | */ |
| 435 | | static public function getDigits($value) |
| 436 | | { |
| 437 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 438 | | return Inspekt::_walkArray($value, 'getDigits'); |
| 439 | | } else { |
| 440 | | return preg_replace('/[^[:digit:]]/', '', $value); |
| 441 | | } |
| 442 | | } |
| 443 | | |
| 444 | | /** |
| 445 | | * Returns dirname(value). |
| 446 | | * |
| 447 | | * @param mixed $value |
| 448 | | * @return mixed |
| 449 | | * |
| 450 | | * @tag filter |
| 451 | | * |
| 452 | | * @assert('/usr/lib/php/Pear.php') === '/usr/lib/php' |
| 453 | | */ |
| 454 | | static public function getDir($value) |
| 455 | | { |
| 456 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 457 | | return Inspekt::_walkArray($value, 'getDir'); |
| 458 | | } else { |
| 459 | | return dirname($value); |
| 460 | | } |
| 461 | | } |
| 462 | | |
| 463 | | /** |
| 464 | | * Returns (int) value. |
| 465 | | * |
| 466 | | * @param mixed $value |
| 467 | | * @return int |
| 468 | | * |
| 469 | | * @tag filter |
| 470 | | * |
| 471 | | * @assert('1)45@*(&UR)HQ)W.0000(*(HG))') === 1 |
| 472 | | * @assert('A1)45@*(&UR)HQ)W.0000(*(HG))') === 0 |
| 473 | | */ |
| 474 | | static public function getInt($value) |
| 475 | | { |
| 476 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 477 | | return Inspekt::_walkArray($value, 'getInt'); |
| 478 | | } else { |
| 479 | | return (int) $value; |
| 480 | | } |
| 481 | | } |
| 482 | | |
| 483 | | /** |
| 484 | | * Returns realpath(value). |
| 485 | | * |
| 486 | | * @param mixed $value |
| 487 | | * @return mixed |
| 488 | | * |
| 489 | | * @tag filter |
| 490 | | */ |
| 491 | | static public function getPath($value) |
| 492 | | { |
| 493 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 494 | | return Inspekt::_walkArray($value, 'getPath'); |
| 495 | | } else { |
| 496 | | return realpath($value); |
| 497 | | } |
| 498 | | } |
| 499 | | |
| 500 | | /** |
| 501 | | * Returns the value encoded as ROT13 (or decoded, if already was ROT13) |
| 502 | | * |
| 503 | | * @param mixed $value |
| 504 | | * @return mixed |
| 505 | | * |
| 506 | | * @link http://php.net/manual/en/function.str-rot13.php |
| 507 | | */ |
| 508 | | static public function getROT13($value) |
| 509 | | { |
| 510 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 511 | | return Inspekt::_walkArray($value, 'getROT13'); |
| 512 | | } else { |
| 513 | | return str_rot13($value); |
| 514 | | } |
| 515 | | } |
| 516 | | |
| 517 | | /** |
| 518 | | * Returns true if every character is alphabetic or a digit, |
| 519 | | * false otherwise. |
| 520 | | * |
| 521 | | * @param mixed $value |
| 522 | | * @return boolean |
| 523 | | * |
| 524 | | * @tag validator |
| 525 | | * |
| 526 | | * @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === true |
| 527 | | * @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false |
| 528 | | * @assert('funkatron') === true |
| 529 | | * @assert('funkatron_user') === false |
| 530 | | * @assert('funkatron-user') === false |
| 531 | | * @assert('_funkatronuser') === false |
| 532 | | */ |
| 533 | | static public function isAlnum($value) |
| 534 | | { |
| 535 | | return ctype_alnum($value); |
| 536 | | } |
| 537 | | |
| 538 | | /** |
| 539 | | * Returns true if every character is alphabetic, false |
| 540 | | * otherwise. |
| 541 | | * |
| 542 | | * @param mixed $value |
| 543 | | * @return boolean |
| 544 | | * |
| 545 | | * @tag validator |
| 546 | | * |
| 547 | | * @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === false |
| 548 | | * @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false |
| 549 | | * @assert('funkatron') === true |
| 550 | | * @assert('funkatron_user') === false |
| 551 | | * @assert('funkatron-user') === false |
| 552 | | * @assert('_funkatronuser') === false |
| 553 | | */ |
| 554 | | static public function isAlpha($value) |
| 555 | | { |
| 556 | | return ctype_alpha($value); |
| 557 | | } |
| 558 | | |
| 559 | | /** |
| 560 | | * Returns true if value is greater than or equal to $min and less |
| 561 | | * than or equal to $max, false otherwise. If $inc is set to |
| 562 | | * false, then the value must be strictly greater than $min and |
| 563 | | * strictly less than $max. |
| 564 | | * |
| 565 | | * @param mixed $value |
| 566 | | * @param mixed $min |
| 567 | | * @param mixed $max |
| 568 | | * @return boolean |
| 569 | | * |
| 570 | | * @tag validator |
| 571 | | * |
| 572 | | * @assert(12, 0, 12) === true |
| 573 | | * @assert(12, 0, 12, false) === false |
| 574 | | * @assert('f', 'a', 'm', false) === true |
| 575 | | * @assert('p', 'a', 'm', false) === false |
| 576 | | */ |
| 577 | | static public function isBetween($value, $min, $max, $inc = true) |
| 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; |
| 591 | | } |
| 592 | | |
| 593 | | /** |
| 594 | | * Returns true if it is a valid credit card number format. The |
| 595 | | * optional second argument allows developers to indicate the |
| 596 | | * type. |
| 597 | | * |
| 598 | | * @param mixed $value |
| 599 | | * @param mixed $type |
| 600 | | * @return boolean |
| 601 | | * |
| 602 | | * @tag validator |
| 603 | | */ |
| 604 | | static public function isCcnum($value, $type = null) |
| 605 | | { |
| 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 | | } |
| 633 | | |
| 634 | | /** |
| 635 | | * Returns true if value is a valid date, false otherwise. The |
| 636 | | * date is required to be in ISO 8601 format. |
| 637 | | * |
| 638 | | * @param mixed $value |
| 639 | | * @return boolean |
| 640 | | * |
| 641 | | * @tag validator |
| 642 | | * |
| 643 | | * @assert('2009-06-30') === true |
| 644 | | * @assert('2009-06-31') === false |
| 645 | | * @assert('2009-6-30') === true |
| 646 | | * @assert('2-6-30') === true |
| 647 | | */ |
| 648 | | static public function isDate($value) |
| 649 | | { |
| 650 | | list($year, $month, $day) = sscanf($value, '%d-%d-%d'); |
| 651 | | |
| 652 | | return checkdate($month, $day, $year); |
| 653 | | } |
| 654 | | |
| 655 | | /** |
| 656 | | * Returns true if every character is a digit, false otherwise. |
| 657 | | * This is just like isInt(), except there is no upper limit. |
| 658 | | * |
| 659 | | * @param mixed $value |
| 660 | | * @return boolean |
| 661 | | * |
| 662 | | * @tag validator |
| 663 | | * |
| 664 | | * @assert('1029438750192730t91740987023948') === false |
| 665 | | * @assert('102943875019273091740987023948') === true |
| 666 | | * @assert(102943875019273091740987023948) === false |
| 667 | | */ |
| 668 | | static public function isDigits($value) |
| 669 | | { |
| 670 | | return ctype_digit((string) $value); |
| 671 | | } |
| 672 | | |
| 673 | | /** |
| 674 | | * Returns true if value is a valid email format, false otherwise. |
| 675 | | * |
| 676 | | * @param string $value |
| 677 | | * @return boolean |
| 678 | | * @see http://www.regular-expressions.info/email.html |
| 679 | | * @see ISPK_EMAIL_VALID |
| 680 | | * |
| 681 | | * @tag validator |
| 682 | | * |
| 683 | | * @assert('coj@poop.com') === true |
| 684 | | * @assert('coj+booboo@poop.com') === true |
| 685 | | * @assert('coj!booboo@poop.com') === false |
| 686 | | * @assert('@poop.com') === false |
| 687 | | * @assert('a@b') === false |
| 688 | | * @assert('webmaster') === false |
| 689 | | */ |
| 690 | | static public function isEmail($value) |
| 691 | | { |
| 692 | | return (bool) preg_match(ISPK_EMAIL_VALID, $value); |
| 693 | | } |
| 694 | | |
| 695 | | /** |
| 696 | | * Returns true if value is a valid float value, false otherwise. |
| 697 | | * |
| 698 | | * @param string $value |
| 699 | | * @return boolean |
| 700 | | * |
| 701 | | * @assert(10244578109.234451) === true |
| 702 | | * @assert('10244578109.234451') === false |
| 703 | | * @assert('10,244,578,109.234451') === false |
| 704 | | * |
| 705 | | * @tag validator |
| 706 | | */ |
| 707 | | static public function isFloat($value) |
| 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); |
| 714 | | } |
| 715 | | |
| 716 | | /** |
| 717 | | * Returns true if value is greater than $min, false otherwise. |
| 718 | | * |
| 719 | | * @param mixed $value |
| 720 | | * @param mixed $min |
| 721 | | * @return boolean |
| 722 | | * |
| 723 | | * @tag validator |
| 724 | | * |
| 725 | | * @assert(5, 0) === true |
| 726 | | * @assert(2, 10) === false |
| 727 | | * @assert('b', 'a') === true |
| 728 | | * @assert('a', 'b') === false |
| 729 | | */ |
| 730 | | static public function isGreaterThan($value, $min) |
| 731 | | { |
| 732 | | return ($value > $min); |
| 733 | | } |
| 734 | | |
| 735 | | /** |
| 736 | | * Returns true if value is a valid hexadecimal format, false |
| 737 | | * otherwise. |
| 738 | | * |
| 739 | | * @param mixed $value |
| 740 | | * @return boolean |
| 741 | | * |
| 742 | | * @tag validator |
| 743 | | * |
| 744 | | * @assert('6F') === true |
| 745 | | * @assert('F6') === true |
| 746 | | * |
| 747 | | */ |
| 748 | | static public function isHex($value) |
| 749 | | { |
| 750 | | return ctype_xdigit($value); |
| 751 | | } |
| 752 | | |
| 753 | | /** |
| 754 | | * Returns true if value is a valid hostname, false otherwise. |
| 755 | | * Depending upon the value of $allow, Internet domain names, IP |
| 756 | | * addresses, and/or local network names are considered valid. |
| 757 | | * The default is HOST_ALLOW_ALL, which considers all of the |
| 758 | | * above to be valid. |
| 759 | | * |
| 760 | | * @param mixed $value |
| 761 | | * @param integer $allow bitfield for ISPK_HOST_ALLOW_DNS, ISPK_HOST_ALLOW_IP, ISPK_HOST_ALLOW_LOCAL |
| 762 | | * @return boolean |
| 763 | | * |
| 764 | | * @tag validator |
| 765 | | */ |
| 766 | | static public function isHostname($value, $allow = ISPK_HOST_ALLOW_ALL) |
| 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 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) { |
| | 93 | |
| | 94 | protected static $useFilterExtension = true; |
| | 95 | |
| | 96 | /** |
| | 97 | * Returns the $_SERVER data wrapped in an Inspekt_Cage object |
| | 98 | * |
| | 99 | * This utilizes a singleton pattern to get around scoping issues |
| | 100 | * |
| | 101 | * @param string $config_file |
| | 102 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 103 | * @return Inspekt_Cage |
| | 104 | * |
| | 105 | * @assert() |
| | 106 | */ |
| | 107 | static public function makeServerCage($config_file = null, $strict = true) |
| | 108 | { |
| | 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 | } |
| | 120 | |
| | 121 | /** |
| | 122 | * Returns the $_GET data wrapped in an Inspekt_Cage object |
| | 123 | * |
| | 124 | * This utilizes a singleton pattern to get around scoping issues |
| | 125 | * |
| | 126 | * @param string $config_file |
| | 127 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 128 | * @return Inspekt_Cage |
| | 129 | */ |
| | 130 | static public function makeGetCage($config_file = null, $strict = true) |
| | 131 | { |
| | 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 | } |
| | 143 | |
| | 144 | /** |
| | 145 | * Returns the $_POST data wrapped in an Inspekt_Cage object |
| | 146 | * |
| | 147 | * This utilizes a singleton pattern to get around scoping issues |
| | 148 | * |
| | 149 | * @param string $config_file |
| | 150 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 151 | * @return Inspekt_Cage |
| | 152 | */ |
| | 153 | static public function makePostCage($config_file = null, $strict = true) |
| | 154 | { |
| | 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 | } |
| | 166 | |
| | 167 | /** |
| | 168 | * Returns the $_COOKIE data wrapped in an Inspekt_Cage object |
| | 169 | * |
| | 170 | * This utilizes a singleton pattern to get around scoping issues |
| | 171 | * |
| | 172 | * @param string $config_file |
| | 173 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 174 | * @return Inspekt_Cage |
| | 175 | */ |
| | 176 | static public function makeCookieCage($config_file = null, $strict = true) |
| | 177 | { |
| | 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 | } |
| | 189 | |
| | 190 | /** |
| | 191 | * Returns the $_ENV data wrapped in an Inspekt_Cage object |
| | 192 | * |
| | 193 | * This utilizes a singleton pattern to get around scoping issues |
| | 194 | * |
| | 195 | * @param string $config_file |
| | 196 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 197 | * @return Inspekt_Cage |
| | 198 | */ |
| | 199 | static public function makeEnvCage($config_file = null, $strict = true) |
| | 200 | { |
| | 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 | } |
| | 212 | |
| | 213 | /** |
| | 214 | * Returns the $_FILES data wrapped in an Inspekt_Cage object |
| | 215 | * |
| | 216 | * This utilizes a singleton pattern to get around scoping issues |
| | 217 | * |
| | 218 | * @param string $config_file |
| | 219 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 220 | * @return Inspekt_Cage |
| | 221 | */ |
| | 222 | static public function makeFilesCage($config_file = null, $strict = true) |
| | 223 | { |
| | 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 | } |
| | 235 | |
| | 236 | /** |
| | 237 | * Returns the $_SESSION data wrapped in an Inspekt_Cage object |
| | 238 | * |
| | 239 | * This utilizes a singleton pattern to get around scoping issues |
| | 240 | * |
| | 241 | * @param string $config_file |
| | 242 | * @param boolean $strict whether or not to nullify the superglobal array |
| | 243 | * @return Inspekt_Cage |
| | 244 | * @deprecated |
| | 245 | */ |
| | 246 | static public function makeSessionCage($config_file = null, $strict = true) |
| | 247 | { |
| | 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 | } |
| | 265 | |
| | 266 | /** |
| | 267 | * Returns a Supercage object, which wraps ALL input superglobals |
| | 268 | * |
| | 269 | * @param string $config_file |
| | 270 | * @param boolean $strict whether or not to nullify the superglobal |
| | 271 | * @return Inspekt_Supercage |
| | 272 | */ |
| | 273 | static public function makeSuperCage($config_file = null, $strict = true) |
| | 274 | { |
| | 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 | } |
| | 285 | |
| | 286 | /** |
| | 287 | * Sets and/or retrieves whether we should use the PHP filter extensions where possible |
| | 288 | * If a param is passed, it will set the state in addition to returning it |
| | 289 | * |
| | 290 | * We use this method of storing in a static class property so that we can access the value outside of class instances |
| | 291 | * |
| | 292 | * @param boolean $state optional |
| | 293 | * @return boolean |
| | 294 | */ |
| | 295 | static public function useFilterExt($state = null) |
| | 296 | { |
| | 297 | if (isset($state)) { |
| | 298 | Inspekt::$useFilterExtension = (bool) $state; |
| | 299 | } |
| | 300 | return Inspekt::$useFilterExtension; |
| | 301 | } |
| | 302 | |
| | 303 | /** |
| | 304 | * Recursively walks an array and applies a given filter method to |
| | 305 | * every value in the array. |
| | 306 | * |
| | 307 | * This should be considered a "protected" method, and not be called |
| | 308 | * outside of the class |
| | 309 | * |
| | 310 | * @param array|ArrayObject $input |
| | 311 | * @param string $inspektor The name of a static filtering method, like get* or no* |
| | 312 | * @return array |
| | 313 | */ |
| | 314 | static protected function _walkArray($input, $method, $classname = null) |
| | 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; |
| | 339 | } |
| | 340 | |
| | 341 | /** |
| | 342 | * Checks to see if this is an ArrayObject |
| | 343 | * @param mixed |
| | 344 | * @return boolean |
| | 345 | * @deprecated |
| | 346 | * @link http://php.net/arrayobject |
| | 347 | */ |
| | 348 | static public function isArrayObject($obj) |
| | 349 | { |
| | 350 | $is = false; |
| | 351 | //$is = (is_object($obj) && get_class($obj) === 'ArrayObject'); |
| | 352 | $is = $obj instanceof ArrayObject; |
| | 353 | return $is; |
| | 354 | } |
| | 355 | |
| | 356 | /** |
| | 357 | * Checks to see if this is an array or an ArrayObject |
| | 358 | * @param mixed |
| | 359 | * @return boolean |
| | 360 | * @link http://php.net/arrayobject |
| | 361 | * @link http://php.net/array |
| | 362 | */ |
| | 363 | static public function isArrayOrArrayObject($arr) |
| | 364 | { |
| | 365 | $is = false; |
| | 366 | $is = $arr instanceof ArrayObject || is_array($arr); |
| | 367 | return $is; |
| | 368 | } |
| | 369 | |
| | 370 | /** |
| | 371 | * Converts an array into an ArrayObject. We use ArrayObjects when walking arrays in Inspekt |
| | 372 | * @param array |
| | 373 | * @return ArrayObject |
| | 374 | */ |
| | 375 | static public function convertArrayToArrayObject(&$arr) |
| | 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); |
| | 387 | } |
| | 388 | |
| | 389 | /** |
| | 390 | * Returns only the alphabetic characters in value. |
| | 391 | * |
| | 392 | * @param mixed $value |
| | 393 | * @return mixed |
| | 394 | * |
| | 395 | * @tag filter |
| | 396 | */ |
| | 397 | static public function getAlpha($value) |
| | 398 | { |
| | 399 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 400 | return Inspekt::_walkArray($value, 'getAlpha'); |
| | 401 | } else { |
| | 402 | return preg_replace('/[^[:alpha:]]/', '', $value); |
| | 403 | } |
| | 404 | } |
| | 405 | |
| | 406 | /** |
| | 407 | * Returns only the alphabetic characters and digits in value. |
| | 408 | * |
| | 409 | * @param mixed $value |
| | 410 | * @return mixed |
| | 411 | * |
| | 412 | * @tag filter |
| | 413 | * |
| | 414 | * @assert('1)@*(&UR)HQ)W(*(HG))') === '1URHQWHG' |
| | 415 | */ |
| | 416 | static public function getAlnum($value) |
| | 417 | { |
| | 418 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 419 | return Inspekt::_walkArray($value, 'getAlnum'); |
| | 420 | } else { |
| | 421 | return preg_replace('/[^[:alnum:]]/', '', $value); |
| | 422 | } |
| | 423 | } |
| | 424 | |
| | 425 | /** |
| | 426 | * Returns only the digits in value. |
| | 427 | * |
| | 428 | * @param mixed $value |
| | 429 | * @return mixed |
| | 430 | * |
| | 431 | * @tag filter |
| | 432 | * |
| | 433 | * @assert('1)@*(&UR)HQ)56W(*(HG))') === '156' |
| | 434 | */ |
| | 435 | static public function getDigits($value) |
| | 436 | { |
| | 437 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 438 | return Inspekt::_walkArray($value, 'getDigits'); |
| | 439 | } else { |
| | 440 | return preg_replace('/[^[:digit:]]/', '', $value); |
| | 441 | } |
| | 442 | } |
| | 443 | |
| | 444 | /** |
| | 445 | * Returns dirname(value). |
| | 446 | * |
| | 447 | * @param mixed $value |
| | 448 | * @return mixed |
| | 449 | * |
| | 450 | * @tag filter |
| | 451 | * |
| | 452 | * @assert('/usr/lib/php/Pear.php') === '/usr/lib/php' |
| | 453 | */ |
| | 454 | static public function getDir($value) |
| | 455 | { |
| | 456 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 457 | return Inspekt::_walkArray($value, 'getDir'); |
| | 458 | } else { |
| | 459 | return dirname($value); |
| | 460 | } |
| | 461 | } |
| | 462 | |
| | 463 | /** |
| | 464 | * Returns (int) value. |
| | 465 | * |
| | 466 | * @param mixed $value |
| | 467 | * @return int |
| | 468 | * |
| | 469 | * @tag filter |
| | 470 | * |
| | 471 | * @assert('1)45@*(&UR)HQ)W.0000(*(HG))') === 1 |
| | 472 | * @assert('A1)45@*(&UR)HQ)W.0000(*(HG))') === 0 |
| | 473 | */ |
| | 474 | static public function getInt($value) |
| | 475 | { |
| | 476 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 477 | return Inspekt::_walkArray($value, 'getInt'); |
| | 478 | } else { |
| | 479 | return (int) $value; |
| | 480 | } |
| | 481 | } |
| | 482 | |
| | 483 | /** |
| | 484 | * Returns realpath(value). |
| | 485 | * |
| | 486 | * @param mixed $value |
| | 487 | * @return mixed |
| | 488 | * |
| | 489 | * @tag filter |
| | 490 | */ |
| | 491 | static public function getPath($value) |
| | 492 | { |
| | 493 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 494 | return Inspekt::_walkArray($value, 'getPath'); |
| | 495 | } else { |
| | 496 | return realpath($value); |
| | 497 | } |
| | 498 | } |
| | 499 | |
| | 500 | /** |
| | 501 | * Returns the value encoded as ROT13 (or decoded, if already was ROT13) |
| | 502 | * |
| | 503 | * @param mixed $value |
| | 504 | * @return mixed |
| | 505 | * |
| | 506 | * @link http://php.net/manual/en/function.str-rot13.php |
| | 507 | */ |
| | 508 | static public function getROT13($value) |
| | 509 | { |
| | 510 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 511 | return Inspekt::_walkArray($value, 'getROT13'); |
| | 512 | } else { |
| | 513 | return str_rot13($value); |
| | 514 | } |
| | 515 | } |
| | 516 | |
| | 517 | /** |
| | 518 | * Returns true if every character is alphabetic or a digit, |
| | 519 | * false otherwise. |
| | 520 | * |
| | 521 | * @param mixed $value |
| | 522 | * @return boolean |
| | 523 | * |
| | 524 | * @tag validator |
| | 525 | * |
| | 526 | * @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === true |
| | 527 | * @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false |
| | 528 | * @assert('funkatron') === true |
| | 529 | * @assert('funkatron_user') === false |
| | 530 | * @assert('funkatron-user') === false |
| | 531 | * @assert('_funkatronuser') === false |
| | 532 | */ |
| | 533 | static public function isAlnum($value) |
| | 534 | { |
| | 535 | return ctype_alnum($value); |
| | 536 | } |
| | 537 | |
| | 538 | /** |
| | 539 | * Returns true if every character is alphabetic, false |
| | 540 | * otherwise. |
| | 541 | * |
| | 542 | * @param mixed $value |
| | 543 | * @return boolean |
| | 544 | * |
| | 545 | * @tag validator |
| | 546 | * |
| | 547 | * @assert('NCOFWIERNVOWIEBHV12047057y0650ytg0314') === false |
| | 548 | * @assert('NCOFWIERNVOWIEBHV2@12047057y0650ytg0314') === false |
| | 549 | * @assert('funkatron') === true |
| | 550 | * @assert('funkatron_user') === false |
| | 551 | * @assert('funkatron-user') === false |
| | 552 | * @assert('_funkatronuser') === false |
| | 553 | */ |
| | 554 | static public function isAlpha($value) |
| | 555 | { |
| | 556 | return ctype_alpha($value); |
| | 557 | } |
| | 558 | |
| | 559 | /** |
| | 560 | * Returns true if value is greater than or equal to $min and less |
| | 561 | * than or equal to $max, false otherwise. If $inc is set to |
| | 562 | * false, then the value must be strictly greater than $min and |
| | 563 | * strictly less than $max. |
| | 564 | * |
| | 565 | * @param mixed $value |
| | 566 | * @param mixed $min |
| | 567 | * @param mixed $max |
| | 568 | * @return boolean |
| | 569 | * |
| | 570 | * @tag validator |
| | 571 | * |
| | 572 | * @assert(12, 0, 12) === true |
| | 573 | * @assert(12, 0, 12, false) === false |
| | 574 | * @assert('f', 'a', 'm', false) === true |
| | 575 | * @assert('p', 'a', 'm', false) === false |
| | 576 | */ |
| | 577 | static public function isBetween($value, $min, $max, $inc = true) |
| | 578 | { |
| | 579 | if ($value > $min && $value < $max) { |
| | 580 | return true; |
| | 581 | } |
| | 582 | |
| | 583 | if ($inc && $value >= $min && $value <= $max) { |
| | 584 | return true; |
| | 585 | } |
| | 586 | |
| 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 | | } |
| 820 | | |
| 821 | | /** |
| 822 | | * Returns true if value is a valid integer value, false otherwise. |
| 823 | | * |
| 824 | | * @param string|array $value |
| 825 | | * @return boolean |
| 826 | | * |
| 827 | | * @tag validator |
| 828 | | * |
| 829 | | * @todo better handling of diffs b/t 32-bit and 64-bit |
| 830 | | */ |
| 831 | | static public function isInt($value) |
| 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 | | );*/ |
| | 588 | } |
| | 589 | |
| | 590 | /** |
| | 591 | * Returns true if it is a valid credit card number format. The |
| | 592 | * optional second argument allows developers to indicate the |
| | 593 | * type. |
| | 594 | * |
| | 595 | * @param mixed $value |
| | 596 | * @param mixed $type |
| | 597 | * @return boolean |
| | 598 | * |
| | 599 | * @tag validator |
| | 600 | */ |
| | 601 | static public function isCcnum($value, $type = null) |
| | 602 | { |
| | 603 | /** |
| | 604 | * @todo Type-specific checks |
| | 605 | */ |
| | 606 | if (isset($type)) { |
| | 607 | Inspekt_Error::raiseError('Type-specific cc checks are not yet supported'); |
| | 608 | } |
| | 609 | |
| | 610 | $value = self::getDigits($value); |
| | 611 | $length = strlen($value); |
| | 612 | |
| | 613 | if ($length < 13 || $length > 19) { |
| | 614 | return false; |
| | 615 | } |
| | 616 | |
| | 617 | $sum = 0; |
| | 618 | $weight = 2; |
| | 619 | |
| | 620 | for ($i = $length - 2; $i >= 0; $i--) { |
| | 621 | $digit = $weight * $value[$i]; |
| | 622 | $sum += floor($digit / 10) + $digit % 10; |
| | 623 | $weight = $weight % 2 + 1; |
| | 624 | } |
| | 625 | |
| | 626 | $mod = (10 - $sum % 10) % 10; |
| | 627 | |
| | 628 | return ($mod == $value[$length - 1]); |
| | 629 | } |
| | 630 | |
| | 631 | /** |
| | 632 | * Returns true if value is a valid date, false otherwise. The |
| | 633 | * date is required to be in ISO 8601 format. |
| | 634 | * |
| | 635 | * @param mixed $value |
| | 636 | * @return boolean |
| | 637 | * |
| | 638 | * @tag validator |
| | 639 | * |
| | 640 | * @assert('2009-06-30') === true |
| | 641 | * @assert('2009-06-31') === false |
| | 642 | * @assert('2009-6-30') === true |
| | 643 | * @assert('2-6-30') === true |
| | 644 | */ |
| | 645 | static public function isDate($value) |
| | 646 | { |
| | 647 | list($year, $month, $day) = sscanf($value, '%d-%d-%d'); |
| | 648 | |
| | 649 | return checkdate($month, $day, $year); |
| | 650 | } |
| | 651 | |
| | 652 | /** |
| | 653 | * Returns true if every character is a digit, false otherwise. |
| | 654 | * This is just like isInt(), except there is no upper limit. |
| | 655 | * |
| | 656 | * @param mixed $value |
| | 657 | * @return boolean |
| | 658 | * |
| | 659 | * @tag validator |
| | 660 | * |
| | 661 | * @assert('1029438750192730t91740987023948') === false |
| | 662 | * @assert('102943875019273091740987023948') === true |
| | 663 | * @assert(102943875019273091740987023948) === false |
| | 664 | */ |
| | 665 | static public function isDigits($value) |
| | 666 | { |
| | 667 | return ctype_digit((string) $value); |
| | 668 | } |
| | 669 | |
| | 670 | /** |
| | 671 | * Returns true if value is a valid email format, false otherwise. |
| | 672 | * |
| | 673 | * @param string $value |
| | 674 | * @return boolean |
| | 675 | * @see http://www.regular-expressions.info/email.html |
| | 676 | * @see ISPK_EMAIL_VALID |
| | 677 | * |
| | 678 | * @tag validator |
| | 679 | * |
| | 680 | * @assert('coj@poop.com') === true |
| | 681 | * @assert('coj+booboo@poop.com') === true |
| | 682 | * @assert('coj!booboo@poop.com') === false |
| | 683 | * @assert('@poop.com') === false |
| | 684 | * @assert('a@b') === false |
| | 685 | * @assert('webmaster') === false |
| | 686 | */ |
| | 687 | static public function isEmail($value) |
| | 688 | { |
| | 689 | return (bool) preg_match(ISPK_EMAIL_VALID, $value); |
| | 690 | } |
| | 691 | |
| | 692 | /** |
| | 693 | * Returns true if value is a valid float value, false otherwise. |
| | 694 | * |
| | 695 | * @param string $value |
| | 696 | * @return boolean |
| | 697 | * |
| | 698 | * @assert(10244578109.234451) === true |
| | 699 | * @assert('10244578109.234451') === false |
| | 700 | * @assert('10,244,578,109.234451') === false |
| | 701 | * |
| | 702 | * @tag validator |
| | 703 | */ |
| | 704 | static public function isFloat($value) |
| | 705 | { |
| | 706 | $locale = localeconv(); |
| | 707 | $value = str_replace($locale['decimal_point'], '.', $value); |
| | 708 | $value = str_replace($locale['thousands_sep'], '', $value); |
| | 709 | |
| | 710 | return (strval(floatval($value)) == $value); |
| | 711 | } |
| | 712 | |
| | 713 | /** |
| | 714 | * Returns true if value is greater than $min, false otherwise. |
| | 715 | * |
| | 716 | * @param mixed $value |
| | 717 | * @param mixed $min |
| | 718 | * @return boolean |
| | 719 | * |
| | 720 | * @tag validator |
| | 721 | * |
| | 722 | * @assert(5, 0) === true |
| | 723 | * @assert(2, 10) === false |
| | 724 | * @assert('b', 'a') === true |
| | 725 | * @assert('a', 'b') === false |
| | 726 | */ |
| | 727 | static public function isGreaterThan($value, $min) |
| | 728 | { |
| | 729 | return ($value > $min); |
| | 730 | } |
| | 731 | |
| | 732 | /** |
| | 733 | * Returns true if value is a valid hexadecimal format, false |
| | 734 | * otherwise. |
| | 735 | * |
| | 736 | * @param mixed $value |
| | 737 | * @return boolean |
| | 738 | * |
| | 739 | * @tag validator |
| | 740 | * |
| | 741 | * @assert('6F') === true |
| | 742 | * @assert('F6') === true |
| | 743 | * |
| | 744 | */ |
| | 745 | static public function isHex($value) |
| | 746 | { |
| | 747 | return ctype_xdigit($value); |
| | 748 | } |
| | 749 | |
| | 750 | /** |
| | 751 | * Returns true if value is a valid hostname, false otherwise. |
| | 752 | * Depending upon the value of $allow, Internet domain names, IP |
| | 753 | * addresses, and/or local network names are considered valid. |
| | 754 | * The default is HOST_ALLOW_ALL, which considers all of the |
| | 755 | * above to be valid. |
| | 756 | * |
| | 757 | * @param mixed $value |
| | 758 | * @param integer $allow bitfield for ISPK_HOST_ALLOW_DNS, ISPK_HOST_ALLOW_IP, ISPK_HOST_ALLOW_LOCAL |
| | 759 | * @return boolean |
| | 760 | * |
| | 761 | * @tag validator |
| | 762 | */ |
| | 763 | static public function isHostname($value, $allow = ISPK_HOST_ALLOW_ALL) |
| | 764 | { |
| | 765 | if (!is_numeric($allow) || !is_int($allow)) { |
| | 766 | Inspekt_Error::raiseError('Illegal value for $allow; expected an integer', E_USER_WARNING); |
| | 767 | } |
| | 768 | |
| | 769 | if ($allow < ISPK_HOST_ALLOW_DNS || ISPK_HOST_ALLOW_ALL < $allow) { |
| | 770 | Inspekt_Error::raiseError('Illegal value for $allow; expected integer between '.ISPK_HOST_ALLOW_DNS.' and '.ISPK_HOST_ALLOW_ALL, E_USER_WARNING); |
| | 771 | } |
| | 772 | |
| | 773 | // determine whether the input is formed as an IP address |
| | 774 | $status = self::isIp($value); |
| | 775 | |
| | 776 | // if the input looks like an IP address |
| | 777 | if ($status) { |
| | 778 | // if IP addresses are not allowed, then fail validation |
| | 779 | if (($allow & ISPK_HOST_ALLOW_IP) == 0) { |
| | 780 | return false; |
| | 781 | } |
| | 782 | |
| | 783 | // IP passed validation |
| | 784 | return true; |
| | 785 | } |
| | 786 | |
| | 787 | // check input against domain name schema |
| | 788 | $status = @preg_match('/^(?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?$/', $value); |
| | 789 | if ($status === false) { |
| | 790 | Inspekt_Error::raiseError('Internal error: DNS validation failed', E_USER_WARNING); |
| | 791 | } |
| | 792 | |
| | 793 | // if the input passes as an Internet domain name, and domain names are allowed, then the hostname |
| | 794 | // passes validation |
| | 795 | if ($status == 1 && ($allow & ISPK_HOST_ALLOW_DNS) != 0) { |
| | 796 | return true; |
| | 797 | } |
| | 798 | |
| | 799 | // if local network names are not allowed, then fail validation |
| | 800 | if (($allow & ISPK_HOST_ALLOW_LOCAL) == 0) { |
| | 801 | return false; |
| | 802 | } |
| | 803 | |
| | 804 | // check input against local network name schema; last chance to pass validation |
| | 805 | $status = @preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)*(?:[^\W_](?:[^\W_]|-){0,61}[^\W_])\.?$/', |
| | 806 | $value); |
| | 807 | if ($status === false) { |
| | 808 | Inspekt_Error::raiseError('Internal error: local network name validation failed', E_USER_WARNING); |
| | 809 | } |
| | 810 | |
| | 811 | if ($status == 0) { |
| | 812 | return false; |
| | 813 | } else { |
| | 814 | return true; |
| | 815 | } |
| | 816 | } |
| | 817 | |
| | 818 | /** |
| | 819 | * Returns true if value is a valid integer value, false otherwise. |
| | 820 | * |
| | 821 | * @param string|array $value |
| | 822 | * @return boolean |
| | 823 | * |
| | 824 | * @tag validator |
| | 825 | * |
| | 826 | * @todo better handling of diffs b/t 32-bit and 64-bit |
| | 827 | */ |
| | 828 | static public function isInt($value) |
| | 829 | { |
| | 830 | $locale = localeconv(); |
| | 831 | |
| | 832 | $value = str_replace($locale['decimal_point'], '.', $value); |
| | 833 | $value = str_replace($locale['thousands_sep'], '', $value); |
| | 834 | |
| | 835 | /* $is_valid = ( |
| | 836 | is_numeric($value) // Must be able to be converted to a number |
| | 837 | && preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers) |
| | 838 | && bccomp($value, "-9223372036854775807") >= 0 // Must be greater than than min of 64-bit |
| | 839 | && bccomp($value, "9223372036854775807") <= 0 // Must be less than max of 64-bit |
| | 840 | ); */ |
| 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); |
| 858 | | } |
| 859 | | |
| 860 | | /** |
| 861 | | * Returns true if value is a valid IP format, false otherwise. |
| 862 | | * |
| 863 | | * @param mixed $value |
| 864 | | * @return boolean |
| 865 | | * |
| 866 | | * @tag validator |
| 867 | | */ |
| 868 | | static public function isIp($value) |
| 869 | | { |
| 870 | | return (bool) ip2long($value); |
| 871 | | } |
| 872 | | |
| 873 | | /** |
| 874 | | * Returns true if value is less than $max, false otherwise. |
| 875 | | * |
| 876 | | * @param mixed $value |
| 877 | | * @param mixed $max |
| 878 | | * @return boolean |
| 879 | | * |
| 880 | | * @tag validator |
| 881 | | */ |
| 882 | | static public function isLessThan($value, $max) |
| 883 | | { |
| 884 | | return ($value < $max); |
| 885 | | } |
| 886 | | |
| 887 | | /** |
| 888 | | * Returns true if value is one of $allowed, false otherwise. |
| 889 | | * |
| 890 | | * @param mixed $value |
| 891 | | * @param array|string $allowed |
| 892 | | * @return boolean |
| 893 | | * |
| 894 | | * @tag validator |
| 895 | | */ |
| 896 | | static public function isOneOf($value, $allowed) |
| 897 | | { |
| 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); |
| 909 | | } |
| 910 | | |
| 911 | | /** |
| 912 | | * Returns true if value is a valid phone number format, false |
| 913 | | * otherwise. The optional second argument indicates the country. |
| 914 | | * This method requires that the value consist of only digits. |
| 915 | | * |
| 916 | | * @param mixed $value |
| 917 | | * @return boolean |
| 918 | | * |
| 919 | | * @tag validator |
| 920 | | */ |
| 921 | | static public function isPhone($value, $country = 'US') |
| 922 | | { |
| 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 | | } |
| 986 | | } |
| 987 | | |
| 988 | | /** |
| 989 | | * Returns true if value matches $pattern, false otherwise. Uses |
| 990 | | * preg_match() for the matching. |
| 991 | | * |
| 992 | | * @param mixed $value |
| 993 | | * @param mixed $pattern |
| 994 | | * @return mixed |
| 995 | | * |
| 996 | | * @tag validator |
| 997 | | */ |
| 998 | | static public function isRegex($value, $pattern) |
| 999 | | { |
| 1000 | | return (bool) preg_match($pattern, $value); |
| 1001 | | } |
| 1002 | | |
| 1003 | | /** |
| 1004 | | * Enter description here... |
| 1005 | | * |
| 1006 | | * @param string $value |
| 1007 | | * @param integer $mode |
| 1008 | | * @return boolean |
| 1009 | | * |
| 1010 | | * @link http://www.ietf.org/rfc/rfc2396.txt |
| 1011 | | * |
| 1012 | | * @tag validator |
| 1013 | | */ |
| 1014 | | static public function isUri($value, $mode = ISPK_URI_ALLOW_COMMON) |
| 1015 | | { |
| 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; |
| 1047 | | |
| 1048 | | // $regex .= '&'; |
| 1049 | | // $regex .= '^(ftp|http|https):'; // protocol |
| 1050 | | // $regex .= '(//)'; // authority-start |
| 1051 | | // $regex .= '([-a-z0-9/~;:@=+$,.!*()\']+@)?'; // userinfo |
| 1052 | | // $regex .= '('; |
| 1053 | | // $regex .= '((?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?)'; // domain name |
| 1054 | | // $regex .= '|'; |
| 1055 | | // $regex .= '([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?(\.[0-9]{1,3})?)'; // OR ipv4 |
| 1056 | | // $regex .= ')'; |
| 1057 | | // $regex .= '(:([0-9]*))?'; // port |
| 1058 | | // $regex .= '(/((%[0-9a-f]{2}|[-a-z0-9/~;:@=+$,.!*()\'\&]*)*)/?)?'; // path |
| 1059 | | // $regex .= '(\?[^#]*)?'; // query |
| 1060 | | // $regex .= '(#([-a-z0-9_]*))?'; // anchor (fragment) |
| 1061 | | // $regex .= '$&i'; |
| 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) { |
| | 844 | is_numeric($value) // Must be able to be converted to a number |
| | 845 | && preg_replace("/^-?([0-9]+)$/", "", $value) == "" // Must be an integer (no floats or e-powers) |
| | 846 | && ($value >= -9223372036854775807) // Must be greater than than min of 64-bit |
| | 847 | && ($value <= 9223372036854775807) // Must be less than max of 64-bit |
| | 848 | ); |
| | 849 | if (!$is_valid) { |
| | 850 | return false; |
| | 851 | } else { |
| | 852 | return true; |
| | 853 | } |
| | 854 | // return (strval(intval($value)) === $value); |
| | 855 | } |
| | 856 | |
| | 857 | /** |
| | 858 | * Returns true if value is a valid IP format, false otherwise. |
| | 859 | * |
| | 860 | * @param mixed $value |
| | 861 | * @return boolean |
| | 862 | * |
| | 863 | * @tag validator |
| | 864 | */ |
| | 865 | static public function isIp($value) |
| | 866 | { |
| | 867 | return (bool) ip2long($value); |
| | 868 | } |
| | 869 | |
| | 870 | /** |
| | 871 | * Returns true if value is less than $max, false otherwise. |
| | 872 | * |
| | 873 | * @param mixed $value |
| | 874 | * @param mixed $max |
| | 875 | * @return boolean |
| | 876 | * |
| | 877 | * @tag validator |
| | 878 | */ |
| | 879 | static public function isLessThan($value, $max) |
| | 880 | { |
| | 881 | return ($value < $max); |
| | 882 | } |
| | 883 | |
| | 884 | /** |
| | 885 | * Returns true if value is one of $allowed, false otherwise. |
| | 886 | * |
| | 887 | * @param mixed $value |
| | 888 | * @param array|string $allowed |
| | 889 | * @return boolean |
| | 890 | * |
| | 891 | * @tag validator |
| | 892 | */ |
| | 893 | static public function isOneOf($value, $allowed) |
| | 894 | { |
| | 895 | /** |
| | 896 | * @todo: Consider allowing a string for $allowed, where each |
| | 897 | * character in the string is an allowed character in the |
| | 898 | * value. |
| | 899 | */ |
| | 900 | if (is_string($allowed)) { |
| | 901 | $allowed = str_split($allowed); |
| | 902 | } |
| | 903 | |
| | 904 | return in_array($value, $allowed); |
| | 905 | } |
| | 906 | |
| | 907 | /** |
| | 908 | * Returns true if value is a valid phone number format, false |
| | 909 | * otherwise. The optional second argument indicates the country. |
| | 910 | * This method requires that the value consist of only digits. |
| | 911 | * |
| | 912 | * @param mixed $value |
| | 913 | * @return boolean |
| | 914 | * |
| | 915 | * @tag validator |
| | 916 | */ |
| | 917 | static public function isPhone($value, $country = 'US') |
| | 918 | { |
| | 919 | if (!ctype_digit($value)) { |
| | 920 | return false; |
| | 921 | } |
| | 922 | |
| | 923 | switch ($country) { |
| | 924 | case 'US': |
| | 925 | if (strlen($value) != 10) { |
| | 926 | return false; |
| | 927 | } |
| | 928 | |
| | 929 | $areaCode = substr($value, 0, 3); |
| | 930 | |
| | 931 | $areaCodes = array(201, 202, 203, 204, 205, 206, 207, 208, |
| | 932 | 209, 210, 212, 213, 214, 215, 216, 217, |
| | 933 | 218, 219, 224, 225, 226, 228, 229, 231, |
| | 934 | 234, 239, 240, 242, 246, 248, 250, 251, |
| | 935 | 252, 253, 254, 256, 260, 262, 264, 267, |
| | 936 | 268, 269, 270, 276, 281, 284, 289, 301, |
| | 937 | 302, 303, 304, 305, 306, 307, 308, 309, |
| | 938 | 310, 312, 313, 314, 315, 316, 317, 318, |
| | 939 | 319, 320, 321, 323, 325, 330, 334, 336, |
| | 940 | 337, 339, 340, 345, 347, 351, 352, 360, |
| | 941 | 361, 386, 401, 402, 403, 404, 405, 406, |
| | 942 | 407, 408, 409, 410, 412, 413, 414, 415, |
| | 943 | 416, 417, 418, 419, 423, 424, 425, 430, |
| | 944 | 432, 434, 435, 438, 440, 441, 443, 445, |
| | 945 | 450, 469, 470, 473, 475, 478, 479, 480, |
| | 946 | 484, 501, 502, 503, 504, 505, 506, 507, |
| | 947 | 508, 509, 510, 512, 513, 514, 515, 516, |
| | 948 | 517, 518, 519, 520, 530, 540, 541, 555, |
| | 949 | 559, 561, 562, 563, 564, 567, 570, 571, |
| | 950 | 573, 574, 580, 585, 586, 600, 601, 602, |
| | 951 | 603, 604, 605, 606, 607, 608, 609, 610, |
| | 952 | 612, 613, 614, 615, 616, 617, 618, 619, |
| | 953 | 620, 623, 626, 630, 631, 636, 641, 646, |
| | 954 | 647, 649, 650, 651, 660, 661, 662, 664, |
| | 955 | 670, 671, 678, 682, 684, 700, 701, 702, |
| | 956 | 703, 704, 705, 706, 707, 708, 709, 710, |
| | 957 | 712, 713, 714, 715, 716, 717, 718, 719, |
| | 958 | 720, 724, 727, 731, 732, 734, 740, 754, |
| | 959 | 757, 758, 760, 763, 765, 767, 769, 770, |
| | 960 | 772, 773, 774, 775, 778, 780, 781, 784, |
| | 961 | 785, 786, 787, 800, 801, 802, 803, 804, |
| | 962 | 805, 806, 807, 808, 809, 810, 812, 813, |
| | 963 | 814, 815, 816, 817, 818, 819, 822, 828, |
| | 964 | 829, 830, 831, 832, 833, 835, 843, 844, |
| | 965 | 845, 847, 848, 850, 855, 856, 857, 858, |
| | 966 | 859, 860, 863, 864, 865, 866, 867, 868, |
| | 967 | 869, 870, 876, 877, 878, 888, 900, 901, |
| | 968 | 902, 903, 904, 905, 906, 907, 908, 909, |
| | 969 | 910, 912, 913, 914, 915, 916, 917, 918, |
| | 970 | 919, 920, 925, 928, 931, 936, 937, 939, |
| | 971 | 940, 941, 947, 949, 951, 952, 954, 956, |
| | 972 | 959, 970, 971, 972, 973, 978, 979, 980, |
| | 973 | 985, 989); |
| | 974 | |
| | 975 | return in_array($areaCode, $areaCodes); |
| | 976 | break; |
| | 977 | default: |
| | 978 | Inspekt_Error::raiseError('isPhone() does not yet support this country.', E_USER_WARNING); |
| | 979 | return false; |
| | 980 | break; |
| | 981 | } |
| | 982 | } |
| | 983 | |
| | 984 | /** |
| | 985 | * Returns true if value matches $pattern, false otherwise. Uses |
| | 986 | * preg_match() for the matching. |
| | 987 | * |
| | 988 | * @param mixed $value |
| | 989 | * @param mixed $pattern |
| | 990 | * @return mixed |
| | 991 | * |
| | 992 | * @tag validator |
| | 993 | */ |
| | 994 | static public function isRegex($value, $pattern) |
| | 995 | { |
| | 996 | return (bool) preg_match($pattern, $value); |
| | 997 | } |
| | 998 | |
| | 999 | /** |
| | 1000 | * Enter description here... |
| | 1001 | * |
| | 1002 | * @param string $value |
| | 1003 | * @param integer $mode |
| | 1004 | * @return boolean |
| | 1005 | * |
| | 1006 | * @link http://www.ietf.org/rfc/rfc2396.txt |
| | 1007 | * |
| | 1008 | * @tag validator |
| | 1009 | */ |
| | 1010 | static public function isUri($value, $mode = ISPK_URI_ALLOW_COMMON) |
| | 1011 | { |
| | 1012 | /** |
| | 1013 | * @todo |
| | 1014 | */ |
| | 1015 | $regex = ''; |
| | 1016 | switch ($mode) { |
| | 1017 | |
| | 1018 | // a common absolute URI: ftp, http or https |
| | 1019 | case ISPK_URI_ALLOW_COMMON: |
| | 1020 | |
| | 1021 | $regex .= '&'; |
| | 1022 | $regex .= '^(ftp|http|https):'; // protocol |
| | 1023 | $regex .= '(//)'; // authority-start |
| | 1024 | $regex .= '([-a-z0-9/~;:@=+$,.!*()\']+@)?'; // userinfo |
| | 1025 | $regex .= '('; |
| | 1026 | $regex .= '((?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?)'; // domain name |
| | 1027 | $regex .= '|'; |
| | 1028 | $regex .= '([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?(\.[0-9]{1,3})?)'; // OR ipv4 |
| | 1029 | $regex .= ')'; |
| | 1030 | $regex .= '(:([0-9]*))?'; // port |
| | 1031 | $regex .= '(/((%[0-9a-f]{2}|[-_a-z0-9/~;:@=+$,.!*()\'\&]*)*)/?)?'; // path |
| | 1032 | $regex .= '(\?[^#]*)?'; // query |
| | 1033 | $regex .= '(#([-a-z0-9_]*))?'; // anchor (fragment) |
| | 1034 | $regex .= '$&i'; |
| | 1035 | //echo "<pre>"; echo print_r($regex, true); echo "</pre>\n"; |
| | 1036 | |
| | 1037 | break; |
| | 1038 | |
| | 1039 | case ISPK_URI_ALLOW_ABSOLUTE: |
| | 1040 | |
| | 1041 | Inspekt_Error::raiseError('isUri() for ISPK_URI_ALLOW_ABSOLUTE has not been implemented.', E_USER_WARNING); |
| | 1042 | return false; |
| | 1043 | break; |
| | 1044 | } |
| | 1045 | $result = preg_match($regex, $value); |
| | 1046 | |
| | 1047 | if ($result === 1) { |
| 1072 | | return true; |
| 1073 | | } else { |
| 1074 | | return false; |
| 1075 | | } |
| 1076 | | } |
| 1077 | | |
| 1078 | | /** |
| 1079 | | * Returns true if value is a valid US ZIP, false otherwise. |
| 1080 | | * |
| 1081 | | * @param mixed $value |
| 1082 | | * @return boolean |
| 1083 | | * |
| 1084 | | * @tag validator |
| 1085 | | */ |
| 1086 | | static public function isZip($value) |
| 1087 | | { |
| 1088 | | return (bool) preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $value); |
| 1089 | | } |
| 1090 | | |
| 1091 | | /** |
| 1092 | | * Returns value with all tags removed. |
| 1093 | | * |
| 1094 | | * This will utilize the PHP Filter extension if available |
| 1095 | | * |
| 1096 | | * @param mixed $value |
| 1097 | | * @return mixed |
| 1098 | | * |
| 1099 | | * @tag filter |
| 1100 | | */ |
| 1101 | | static public function noTags($value) |
| 1102 | | { |
| 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 | | } |
| 1112 | | } |
| 1113 | | |
| 1114 | | /** |
| 1115 | | * returns value with tags stripped and the chars '"&<> and all ascii chars under 32 encoded as html entities |
| 1116 | | * |
| 1117 | | * This will utilize the PHP Filter extension if available |
| 1118 | | * |
| 1119 | | * @param mixed $value |
| 1120 | | * @return @mixed |
| 1121 | | * |
| 1122 | | * @tag filter |
| 1123 | | * |
| 1124 | | */ |
| 1125 | | static public function noTagsOrSpecial($value) |
| 1126 | | { |
| 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 | | /* |
| 1139 | | convert low ascii chars to entities |
| 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 | | } |
| 1153 | | } |
| 1154 | | |
| 1155 | | /** |
| 1156 | | * Returns basename(value). |
| 1157 | | * |
| 1158 | | * @param mixed $value |
| 1159 | | * @return mixed |
| 1160 | | * |
| 1161 | | * @tag filter |
| 1162 | | */ |
| 1163 | | static public function noPath($value) |
| 1164 | | { |
| 1165 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 1166 | | return Inspekt::_walkArray($value, 'noPath'); |
| 1167 | | } else { |
| 1168 | | return basename($value); |
| 1169 | | } |
| 1170 | | } |
| 1171 | | |
| 1172 | | /** |
| 1173 | | * Escapes the value given with mysql_real_escape_string |
| 1174 | | * |
| 1175 | | * @param mixed $value |
| 1176 | | * @param resource $conn the mysql connection. If none is given, it will use the last link opened, per behavior of mysql_real_escape_string |
| 1177 | | * @return mixed |
| 1178 | | * |
| 1179 | | * @link http://php.net/manual/en/function.mysql-real-escape-string.php |
| 1180 | | * |
| 1181 | | * @tag filter |
| 1182 | | */ |
| 1183 | | static public function escMySQL($value, $conn = null) |
| 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 mysqli_real_escape_string( $conn, $value); |
| 1191 | | } |
| 1192 | | } |
| 1193 | | } |
| 1194 | | |
| 1195 | | /** |
| 1196 | | * Escapes the value given with pg_escape_string |
| 1197 | | * |
| 1198 | | * If the data is for a column of the type bytea, use Inspekt::escPgSQLBytea() |
| 1199 | | * |
| 1200 | | * @param mixed $value |
| 1201 | | * @param resource $conn the postgresql connection. If none is given, it will use the last link opened, per behavior of pg_escape_string |
| 1202 | | * @return mixed |
| 1203 | | * |
| 1204 | | * @link http://php.net/manual/en/function.pg-escape-string.php |
| 1205 | | */ |
| 1206 | | static public function escPgSQL($value, $conn = null) |
| 1207 | | { |
| 1208 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 1209 | | return Inspekt::_walkArray($value, 'escPgSQL'); |
| 1210 | | } else { |
| 1211 | | //might also check is_resource if pg_connection_status is too much |
| 1212 | | if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) { |
| 1213 | | return pg_escape_string($conn, $value); |
| 1214 | | } else { |
| 1215 | | return pg_escape_string($value); |
| 1216 | | } |
| 1217 | | } |
| 1218 | | } |
| 1219 | | |
| 1220 | | /** |
| 1221 | | * Escapes the value given with pg_escape_bytea |
| 1222 | | * |
| 1223 | | * @param mixed $value |
| 1224 | | * @param resource $conn the postgresql connection. If none is given, it will use the last link opened, per behavior of pg_escape_bytea |
| 1225 | | * @return mixed |
| 1226 | | * |
| 1227 | | * @link http://php.net/manual/en/function.pg-escape-bytea.php |
| 1228 | | */ |
| 1229 | | static public function escPgSQLBytea($value, $conn = null) |
| 1230 | | { |
| 1231 | | if (Inspekt::isArrayOrArrayObject($value)) { |
| 1232 | | return Inspekt::_walkArray($value, 'escPgSQL'); |
| 1233 | | } else { |
| 1234 | | //might also check is_resource if pg_connection_status is too much |
| 1235 | | if (isset($conn) && pg_connection_status($conn) === PGSQL_CONNECTION_OK) { |
| 1236 | | return pg_escape_bytea($conn, $value); |
| 1237 | | } else { |
| 1238 | | return pg_escape_bytea($value); |
| 1239 | | } |
| 1240 | | } |
| 1241 | | } |
| | 1050 | return true; |
| | 1051 | } else { |
| | 1052 | return false; |
| | 1053 | } |
| | 1054 | } |
| | 1055 | |
| | 1056 | /** |
| | 1057 | * Returns value with all tags removed. |
| | 1058 | * |
| | 1059 | * This will utilize the PHP Filter extension if available |
| | 1060 | * |
| | 1061 | * @param mixed $value |
| | 1062 | * @return mixed |
| | 1063 | * |
| | 1064 | * @tag filter |
| | 1065 | */ |
| | 1066 | static public function noTags($value) |
| | 1067 | { |
| | 1068 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 1069 | return Inspekt::_walkArray($value, 'noTags'); |
| | 1070 | } else { |
| | 1071 | if (Inspekt::useFilterExt()) { |
| | 1072 | return filter_var($value, FILTER_SANITIZE_STRING); |
| | 1073 | } else { |
| | 1074 | return strip_tags($value); |
| | 1075 | } |
| | 1076 | } |
| | 1077 | } |
| | 1078 | |
| | 1079 | /** |
| | 1080 | * returns value with tags stripped and the chars '"&<> and all ascii chars under 32 encoded as html entities |
| | 1081 | * |
| | 1082 | * This will utilize the PHP Filter extension if available |
| | 1083 | * |
| | 1084 | * @param mixed $value |
| | 1085 | * @return @mixed |
| | 1086 | * |
| | 1087 | * @tag filter |
| | 1088 | * |
| | 1089 | */ |
| | 1090 | static public function noTagsOrSpecial($value) |
| | 1091 | { |
| | 1092 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 1093 | return Inspekt::_walkArray($value, 'noTagsOrSpecial'); |
| | 1094 | } else { |
| | 1095 | if (Inspekt::useFilterExt()) { |
| | 1096 | $newval = filter_var($value, FILTER_SANITIZE_STRING); |
| | 1097 | $newval = filter_var($newval, FILTER_SANITIZE_SPECIAL_CHARS); |
| | 1098 | return $newval; |
| | 1099 | } else { |
| | 1100 | $newval = strip_tags($value); |
| | 1101 | $newval = htmlspecialchars($newval, ENT_QUOTES, 'UTF-8'); // for sake of simplicity and safety we assume UTF-8 |
| | 1102 | |
| | 1103 | /* |
| | 1104 | convert low ascii chars to entities |
| | 1105 | */ |
| | 1106 | $newval = str_split($newval); |
| | 1107 | for ($i = 0; $i < count($newval); $i++) { |
| | 1108 | $ascii_code = ord($newval[$i]); |
| | 1109 | if ($ascii_code < 32) { |
| | 1110 | $newval[$i] = "&#{$ascii_code};"; |
| | 1111 | } |
| | 1112 | } |
| | 1113 | $newval = implode($newval); |
| | 1114 | |
| | 1115 | return $newval; |
| | 1116 | } |
| | 1117 | } |
| | 1118 | } |
| | 1119 | |
| | 1120 | /** |
| | 1121 | * Returns basename(value). |
| | 1122 | * |
| | 1123 | * @param mixed $value |
| | 1124 | * @return mixed |
| | 1125 | * |
| | 1126 | * @tag filter |
| | 1127 | */ |
| | 1128 | static public function noPath($value) |
| | 1129 | { |
| | 1130 | if (Inspekt::isArrayOrArrayObject($value)) { |
| | 1131 | return Inspekt::_walkArray($value, 'noPath'); |
| | 1132 | } else { |
| | 1133 | return basename($value); |
| | 1134 | } |
| | 1135 | } |
| | 1136 | |