1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 895: 896: 897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911:
<?php
namespace Nethgui\System;
interface MandatoryValidatorInterface extends ValidatorInterface
{
public function isMandatory();
}
class Validator implements \Nethgui\System\MandatoryValidatorInterface
{
private $chain = array();
private $failureInfo;
private $platform;
public function __construct(PlatformInterface $platform)
{
$this->platform = $platform;
}
public function orValidator(\Nethgui\System\ValidatorInterface $v1, \Nethgui\System\ValidatorInterface $v2)
{
$this->chain[] = new OrValidator($v1, $v2);
return $this;
}
public function memberOf()
{
$args = func_get_args();
if (isset($args[0]) && is_array($args[0]) && count($args) == 1) {
$set = $args[0];
} else {
$set = $args;
}
if (count($set) > 5) {
$setToShow = array_slice($set, 0, 3);
$setToShow[] = '... ';
$setToShow = array_merge($setToShow, array_slice($set, -2, 2));
} else {
$setToShow = $set;
}
$messageTemplate = array('valid_memberOf', array(implode(', ', $setToShow)));
return $this->addToChain(__FUNCTION__, $messageTemplate, $set);
}
public function regexp($e, $meaning='valid_regexp')
{
$messageTemplate = array($meaning, array($e));
return $this->addToChain(__FUNCTION__, $messageTemplate, $e);
}
public function notEmpty()
{
return $this->addToChain(__FUNCTION__);
}
public function isEmpty()
{
return $this->addToChain(__FUNCTION__);
}
public function forceResult($result)
{
$this->chain[] = ($result === TRUE);
return $this;
}
public function ipV4Address()
{
return $this->addToChain(__FUNCTION__);
}
public function hostname($minDots = 0, $maxDots = PHP_INT_MAX)
{
if ($minDots > $maxDots) {
throw new \LogicException(sprintf('%s: hostname validator: the minDots argument must be less than or equal to maxDots', __CLASS__), 1334737472);
}
$message = 'valid_hostname';
if ($minDots === 0 && $maxDots === 0) {
$message = 'valid_hostname_simple';
} elseif ($minDots === 0 && $maxDots === PHP_INT_MAX) {
$message = 'valid_hostname';
} elseif ($minDots === 1 && $maxDots === PHP_INT_MAX) {
$message = 'valid_hostname_fqdn';
} elseif ($minDots > 1 && $maxDots === PHP_INT_MAX) {
$message = 'valid_hostname_fqdn_long';
} else {
$message = 'valid_hostname_fqdn_exact';
}
return $this
->minLength(1)
->maxLength(255)
->addToChain(__FUNCTION__, array($message, array($minDots, $maxDots)), $minDots, $maxDots)
;
}
public function ipV6Address()
{
return $this->notImplemented(__FUNCTION__);
}
public function ipV4Netmask()
{
return $this->addToChain(__FUNCTION__);
}
public function macAddress()
{
return $this->addToChain(__FUNCTION__);
}
public function ipV6Netmask()
{
return $this->notImplemented(__FUNCTION__);
}
public function integer()
{
return $this->addToChain(__FUNCTION__);
}
public function positive()
{
return $this->addToChain(__FUNCTION__);
}
public function negative()
{
return $this->addToChain(__FUNCTION__);
}
public function lessThan($cmp)
{
$template = array('valid_lessThan', array($cmp));
return $this->addToChain(__FUNCTION__, $template, $cmp);
}
public function greatThan($cmp)
{
$template = array('valid_greatThan', array($cmp));
return $this->addToChain(__FUNCTION__, $template, $cmp);
}
public function equalTo($cmp)
{
$template = array('valid_equalTo', array($cmp));
return $this->addToChain(__FUNCTION__, $template, $cmp);
}
public function minLength($length)
{
$template = array('valid_minLength', array($length));
return $this->addToChain(__FUNCTION__, $template, $length);
}
public function maxLength($length)
{
$template = array('valid_maxLength', array($length));
return $this->addToChain(__FUNCTION__, $template, $length);
}
public function not()
{
$this->chain[] = -1;
return $this;
}
public function username()
{
return $this->addToChain(__FUNCTION__);
}
public function date($format = NULL)
{
if (is_null($format)) {
$format = $this->platform->getDateFormat();
}
$template = array('valid_date', array($format));
return $this->addToChain(__FUNCTION__, $template, $format);
}
public function time()
{
return $this->addToChain(__FUNCTION__);
}
public function collectionValidator(Validator $v)
{
$this->chain[] = new CollectionValidator($v);
return $this;
}
public function platform($validatorName)
{
$arguments = func_get_args();
if ( ! is_string($validatorName) && $validatorName !== '') {
throw new \InvalidArgumentException(sprintf("%s: must supply a validator name argument", __CLASS__), 1333012309);
}
array_unshift($arguments, __FUNCTION__, FALSE);
return call_user_func_array(array($this, 'addToChain'), $arguments);
}
public function email()
{
return $this->addToChain(__FUNCTION__, FALSE);
}
public function cidrBlock()
{
return $this->addToChain(__FUNCTION__);
}
public function getFailureInfo()
{
return $this->failureInfo;
}
public function evaluate($value)
{
$this->failureInfo = array();
if (empty($this->chain)) {
return FALSE;
}
$notFlag = FALSE;
foreach ($this->chain as $expression) {
if (is_integer($expression) && $expression < 0) {
$notFlag = TRUE;
continue;
} elseif (is_array($expression) && is_callable($expression[1])) {
list($methodName, $evalFunction, $argList, $errorTemplate) = $expression;
if ( ! is_array($argList)) {
$argList = array();
}
array_unshift($argList, $value);
$isValid = call_user_func_array($evalFunction, $argList);
if (($isValid XOR $notFlag) === FALSE) {
if (is_array($errorTemplate)) {
$this->addFailureInfo($errorTemplate[0], $errorTemplate[1]);
}
return FALSE;
}
} elseif ($expression instanceof \Nethgui\System\ValidatorInterface) {
$isValid = $expression->evaluate($value);
if (($isValid XOR $notFlag) === FALSE) {
$this->mergeFailureInfo($expression);
return FALSE;
}
} elseif ($expression === FALSE) {
$this->addFailureInfo('valid_forced_failure');
return FALSE;
} elseif ($expression === TRUE) {
break;
}
$notFlag = FALSE;
}
return TRUE;
}
protected function mergeFailureInfo(\Nethgui\System\ValidatorInterface $validator)
{
foreach ($validator->getFailureInfo() as $elem) {
$this->addFailureInfo($elem[0], $elem[1]);
}
return $this;
}
protected function addFailureInfo($template, $args = array())
{
if ( ! is_string($template)) {
throw new \InvalidArgumentException(sprintf('%s: $template argument must be a string', __CLASS__), 1337766431);
} elseif ( ! is_array($args)) {
throw new \InvalidArgumentException(sprintf('%s: $args argument must be an array', __CLASS__), 1337766432);
}
$this->failureInfo[] = array($template, $args);
return $this;
}
private function notImplemented($method)
{
throw new \LogicException(sprintf('%s: %s() is not implemented.', get_class($this), $method), 1322150359);
}
private function addToChain($originalMethodName, $errorMessageTemplate = NULL)
{
$args = array_slice(func_get_args(), 2);
if (is_null($errorMessageTemplate)) {
$errorMessageTemplate = array('valid_' . $originalMethodName, array());
}
$methodName = 'eval' . ucfirst($originalMethodName);
$this->chain[] = array(
$originalMethodName,
array($this, $methodName),
$args,
$errorMessageTemplate,
);
return $this;
}
private function evalNotEmpty($value)
{
if($value instanceof \Countable) {
return $value->count() > 0;
}
return empty($value) ? FALSE : TRUE;
}
private function evalIsEmpty($value)
{
if($value instanceof \Countable) {
return $value->count() == 0;
}
return empty($value) ? TRUE : FALSE;
}
private function evalRegexp($value, $exp)
{
return (preg_match($exp, $value) > 0);
}
private function evalMemberOf($value, $args)
{
return in_array($value, $args, TRUE);
}
private function evalIpV4Address($value)
{
$ip_segments = explode('.', $value);
if (count($ip_segments) != 4) {
return FALSE;
}
foreach ($ip_segments as $segment) {
if ($segment == '' OR ! preg_match("/^(0|[1-9][0-9]?[0-9]?)$/", $segment) OR $segment > 255 OR strlen($segment) > 3) {
return FALSE;
}
}
return TRUE;
}
private function evalUsername($value)
{
return strlen($value) < 256 && $this->evalRegexp($value, '/^[a-z][-_\.a-z0-9]*$/');
}
private function evalInteger($value)
{
return preg_match('/^(0|-?[1-9][0-9]*)$/', $value) === 1;
}
private function evalNumeric($value)
{
return $this->evalInteger($value) || preg_match('/^-?(0|[1-9][0-9]*)\.[0-9]+$/', $value) === 1;
}
private function evalPositive($value)
{
return $this->evalNumeric($value) && $value > 0;
}
private function evalNegative($value)
{
return $this->evalNumeric($value) && $value < 0;
}
private function evalLessThan($value, $cmp)
{
return $this->evalNumeric($value) && ($value < $cmp);
}
private function evalGreatThan($value, $cmp)
{
return $this->evalNumeric($value) && ($value > $cmp);
}
private function evalEqualTo($value, $cmp)
{
return ($this->evalNumeric($value) && $value == $cmp) || strcmp($value, $cmp) === 0;
}
private function evalMinLength($s, $min)
{
if ( ! is_string($s)) {
throw new \InvalidArgumentException(sprintf("%s: Invalid type `%s`. Value must be a string.", get_class($this), gettype($s)), 1322150360);
}
return strlen($s) >= $min;
}
private function evalMaxLength($s, $max)
{
if ( ! is_string($s)) {
throw new \InvalidArgumentException(sprintf("%s: Invalid type `%s`. Value must be a string.", get_class($this), gettype($s)), 1322150361);
}
return strlen($s) <= $max;
}
private function evalHostname($value, $minDots, $maxDots)
{
$parts = explode('.', $value);
$dotsCount = count($parts) - 1;
if ($dotsCount < $minDots || $dotsCount > $maxDots) {
return FALSE;
}
$pattern = '/^[a-z0-9]([-a-z0-9]*)$/i';
foreach ($parts as $part) {
if (strlen($part) > 63) {
return FALSE;
}
if (preg_match($pattern, $part) == 0) {
return FALSE;
}
}
return TRUE;
}
private function evalDate($value, $format)
{
if ($format == 'dd/mm/YYYY') {
list($day, $month, $year) = explode('/', $value) + array(0, 0, 0);
} elseif ($format == 'mm-dd-YYYY') {
list($month, $day, $year) = explode('-', $value) + array(0, 0, 0);
} elseif ($format == 'YYYY-mm-dd') {
list($year, $month, $day) = explode('-', $value) + array(0, 0, 0);
} else {
throw new \InvalidArgumentException(sprintf("%s: Unknown date format `%s`", get_class($this), $format), 1322150419);
}
return checkdate(intval($month), intval($day), intval($year));
}
private function evalTime($value)
{
$parts = array();
$pattern = '|^(\d\d):(\d\d)(?:\:(\d\d))?$|';
if (preg_match($pattern, $value, $parts) == 0) {
return FALSE;
};
$parts = $parts + array('00', '00', '00');
list($h, $m, $s) = array_map('intval', $parts);
if (($h >= 0 && $h < 24)
&& ($m >= 0 && $m < 60)
&& ($s >= 0 && $s < 60)) {
return TRUE;
}
return FALSE;
}
private function evalIpV4Netmask($value)
{
$pattern = '/^(((0|128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254|255)))$/i';
return preg_match($pattern, $value);
}
private function evalMacAddress($value)
{
$pattern = '/^[0-9a-f]{2}(:[0-9a-f]{2}){5}$/i';
return preg_match($pattern, $value);
}
private function evalPlatform($value)
{
$args = func_get_args();
array_shift($args);
$args[] = $value;
$process = $this->platform->exec('/usr/bin/sudo /sbin/e-smith/validate ${@}', $args);
if ($process->getExitCode() !== 0 && $this->platform instanceof \Nethgui\Log\LogConsumerInterface) {
NETHGUI_DEBUG && $this->platform->getLog()->notice(sprintf('platformValidator (%s): %s', implode(', ', $args), strtr($process->getOutput(), "\n", " ")));
$outputArray = $process->getOutputArray();
$reason = array_pop($outputArray);
$args['${reason}'] = substr(implode("\n", $outputArray), 0, 64);
$this->addFailureInfo('valid_platform,' . $reason, $args);
}
return $process->getExitCode() === 0;
}
private function evalEmail($value)
{
if (\filter_var($value, \FILTER_VALIDATE_EMAIL) === FALSE) {
$this->addFailureInfo('valid_email,generic_failure');
return FALSE;
}
return TRUE;
}
private function evalCidrBlock($value)
{
$parts = explode('/', $value);
$validMask = isset($parts[1]) && preg_match('/^[0-9]+$/', $parts[1]) && (($parts[1] > 1 && $parts[1] < 33) || $parts[1] === "0");
return $validMask && $this->evalIpV4Address($parts[0]);
}
protected function setMandatoryConditional($value)
{
if ( ! isset($this->mandatory)) {
$this->mandatory = $value;
}
return $this;
}
public function isMandatory()
{
$notFlag = FALSE;
foreach ($this->chain as $expression) {
if (is_integer($expression) && $expression < 0) {
$notFlag = TRUE;
continue;
} elseif (is_array($expression) && is_callable($expression[1])) {
list($methodName, $evalFunction, $argList, $errorTemplate) = $expression;
if ( ! is_array($argList)) {
$argList = array();
}
if ($methodName === 'maxLength' && $argList[0] >= 0 ) {
return FALSE;
} else {
return (preg_match('/^isEmpty$/', $methodName) === 0) XOR $notFlag;
}
} elseif ($expression instanceof \Nethgui\System\MandatoryValidatorInterface) {
return $expression->isMandatory();
} elseif (is_bool($expression)) {
return FALSE;
}
$notFlag = FALSE;
}
return TRUE;
}
}
class CollectionValidator implements \Nethgui\System\MandatoryValidatorInterface
{
private $memberValidator;
private $failureInfo;
private $iterator;
public function __construct(\Nethgui\System\ValidatorInterface $memberValidator)
{
$this->memberValidator = $memberValidator;
}
public function evaluate($iterableObject)
{
$this->failureInfo = array();
if (is_array($iterableObject)) {
$iterableObject = new \ArrayObject($iterableObject);
$this->iterator = $iterableObject->getIterator();
} elseif ($iterableObject instanceof \IteratorAggregate) {
$this->iterator = $iterableObject->getIterator();
} elseif ($iterableObject instanceof \Iterator) {
$this->iterator = $iterableObject;
} else {
$this->failureInfo[] = array("Not a collection", array());
return FALSE;
}
foreach ($this->iterator as $e) {
if ($this->memberValidator->evaluate($e) === FALSE) {
$this->failureInfo = array_merge($this->failureInfo, $this->memberValidator->getFailureInfo());
return FALSE;
}
}
return TRUE;
}
public function getFailureInfo()
{
return $this->failureInfo;
}
public function isMandatory()
{
if ($this->memberValidator instanceof MandatoryValidatorInterface) {
return $this->memberValidator->isMandatory();
}
return TRUE;
}
}
class OrValidator implements \Nethgui\System\MandatoryValidatorInterface
{
private $v1;
private $v2;
private $failureInfo;
public function __construct(\Nethgui\System\ValidatorInterface $v1, \Nethgui\System\ValidatorInterface $v2)
{
$this->v1 = $v1;
$this->v2 = $v2;
}
public function evaluate($value)
{
$this->failureInfo = array();
$e1 = $this->v1->evaluate($value);
if ($e1 === FALSE) {
$e2 = $this->v2->evaluate($value);
if ($e2 === FALSE) {
$this->failureInfo = array_merge($this->failureInfo, $this->v1->getFailureInfo(), $this->v2->getFailureInfo());
return FALSE;
}
return TRUE;
}
return TRUE;
}
public function getFailureInfo()
{
return $this->failureInfo;
}
public function isMandatory()
{
$m1 = ($this->v1 instanceof MandatoryValidatorInterface) ? $this->v1->isMandatory() : TRUE;
$m2 = ($this->v2 instanceof MandatoryValidatorInterface) ? $this->v2->isMandatory() : TRUE;
return ($m1 && $m2);
}
}