00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00020 class StandardValidator {
00021
00022 private $errorList = array();
00023 protected $customCodes = array();
00024 protected $extensions = array(
00025 'image/jpg' => 'jpg',
00026 'image/pjpeg' => 'jpg',
00027 'image/jpeg' => 'jpg',
00028 'image/gif' => 'gif',
00029 'image/png' => 'png',
00030 'application/pdf' =>'pdf',
00031 'application/msword' =>'doc',
00032 'application/x-compressed' => 'tgz',
00033 'application/zip' => 'zip',
00034 'application/x-zip' => 'zip',
00035 'application/x-zip-compressed' => 'zip',
00036 'application/x-gzip' => 'gz',
00037 'application/xml' =>'xml',
00038 'text/xml' =>'xml',
00039 'audio/mpeg' =>'mp3',
00040 'application/x-latex' => 'tex',
00041 'text/plain' => 'txt',
00042 'text/html' => 'html',
00043 'text/comma-separated-values' => 'csv',
00044 'text/csv' => 'csv',
00045 'application/csv' => 'csv',
00046 'application/excel' => 'csv',
00047 'application/vnd.ms-excel' => 'csv',
00048 'application/vnd.msexcel' => 'csv'
00049 );
00050
00051
00061 public function validateInput ( $input, $type )
00062 {
00063 switch ($type)
00064 {
00065 case 'string':
00066 $pattern = "/^[a-zA-Z\s]+$/";
00067 $errorCode = 100;
00068 break;
00069 case 'numbers':
00070 $pattern = "/^(\-)?[0-9\.]+(\s)?$/";
00071 $errorCode = 101;
00072 break;
00073 case 'alphanumeric':
00074 $pattern = "/^[0-9a-zA-Z\s]+$/";
00075 $errorCode = 102;
00076 break;
00077 case 'money':
00078 $pattern = "/^([0-9\-\.\,\$])$/";
00079 $errorCode = 137;
00080 case 'name':
00081 $pattern = "/^([a-zA-Z][0-9A-Za-z\-\.\,\']*(\s)?)+$/";
00082 $errorCode = 103;
00083 break;
00084 case 'email':
00085 $pattern = "/^[a-zA-Z0-9\.\_\-]+\@[a-zA-Z0-9\-\_]+\.[a-z0-9\-\_\.]+(\s)?$/";
00086 $errorCode = 104;
00087 break;
00088 case 'address':
00089 $pattern = "/^[a-zA-Z0-9\s\.\-\,\#]+(\s)?$/";
00090 $errorCode = 105;
00091 break;
00092 case 'city':
00093 $pattern = "/^[a-zA-Z0-9\s\-\.]+(\s)?$/";
00094 $errorCode = 106;
00095 break;
00096 case 'state':
00097 $pattern = "/^[a-zA-Z\s\.]+(\s)?$/";
00098 $errorCode = 107;
00099 break;
00100 case 'zip':
00101 $pattern = "/^[0-9]{5,9}(\-[0-9]{4})?(\s)?$/";
00102 $errorCode = 108;
00103 break;
00104 case 'website':
00105 # source: http://www.mattfarina.com/2009/01/08/rfc-3986-url-validation
00106 $pattern = "/^([a-z][a-z0-9\*\-\.]*):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:(?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?](?:[\w#!:\.\?\+=&@!$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/i";
00107 $errorCode = 109;
00108 break;
00109 case 'checkbox':
00110 $pattern = "/^([a-zA-Z0-9\.\_\/\(\)]+(\s)?)+$/";
00111 $errorCode = 110;
00112 break;
00113 case 'pulldown':
00114 $pattern = "/^[a-zA-Z\s\_]+$/";
00115 $errorCode = 111;
00116 break;
00117 case 'phone':
00118 $pattern = "/^[0-9a-zA-Z\-\s\(\)\+]{7,20}$/";
00119 $errorCode = 112;
00120 break;
00121 case 'birthday':
00122 $pattern = "/^[a-zA-Z0-9\s\,\-\/]+$/";
00123 $errorCode = 113;
00124 break;
00125 case 'paragraph':
00126 case 'textlist':
00127 $pattern = "//";
00128 $errorCode = 115;
00129 break;
00130 case 'shortname':
00131 case 'shortName':
00132 $pattern = "/^[a-zA-Z0-9\_\-\.]+$/";
00133 $errorCode = 116;
00134 break;
00135 case 'organization':
00136 $pattern = "/^([A-Za-z0-9\-\.\,]*(\s)?)+$/";
00137 $errorCode = 117;
00138 break;
00139 case 'username':
00140 $pattern = "/^[a-z][a-z0-9\_]{2,13}$/";
00141 $errorCode = 130;
00142 break;
00143 case 'time':
00144 $pattern = "/^[0-9]{1,2}[hH](\s)?[0-9]{1,2}[mM](\s)?$/";
00145 $errorCode = 131;
00146 break;
00147 case 'date':
00148
00149 $explodedPatt = explode("/",$input);
00150 if(sizeof($explodedPatt) == 3){
00151 $day = $explodedPatt[1];
00152 $month = $explodedPatt[0];
00153 $year = $explodedPatt[2];
00154
00155 $truthiness = (strlen($day) == 2) && ($day <= 31);
00156 $truthiness = $truthiness && (strlen($month) == 2) && ($month <= 12);
00157 $truthiness = $truthiness && (strlen($year) == 4) && ($year > 0) && ($year < (date("Y") + 20));
00158
00159 if($day > 29 && $month == 2){
00160 $truthiness = false;
00161 } else if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && ($day > 30)){
00162 $truthiness = false;
00163 }
00164
00165 if(!$truthiness){
00166 return 132;
00167 } else {
00168 return 0;
00169 }
00170
00171 } else {
00172 return 132;
00173 }
00174 break;
00175 case 'password':
00176 $pattern = "/^[\S]{6,10}$/";
00177 $errorCode = 118;
00178 break;
00179 case 'fileName':
00180 $pattern = "/^[a-zA-Z0-9\.\-]+$/";
00181 $errorCode = 120;
00182 break;
00183 case 'xml':
00184 case 'mixedxml':
00185 $input = utf8_encode($input);
00186
00187 $old_error_handler = set_error_handler(array($this, "XMLParseErrorHandler"));
00188 if ($type == "mixedxml") { $input = "<root> $input </root>"; }
00189
00190 $check = simplexml_load_string("\n $input \n");
00191 restore_error_handler();
00192 if ($check === false) return 119;
00193 else return 0;
00194 break;
00195 default:
00196 $pattern = "//";
00197 $errorCode = 0;
00198 break;
00199 }
00200
00201 $match = preg_match($pattern, $input, $matches);
00202
00203 if( ! $match) {
00204
00205 return($errorCode);
00206
00207 }
00208 return (0);
00209 }
00210
00221 public function validateFile($input, $type, $mime) {
00222
00223
00224 switch($type) {
00225 case 'jpg':
00226 if(! ($mime == "image/jpeg" || $mime == "image/pjpeg" || $mime == "image/jpg"))
00227 return(120);
00228 break;
00229 case 'png':
00230 if(!($mime == "image/png"))
00231 return(121);
00232 break;
00233 case 'gif':
00234 if(!($mime == "image/gif"))
00235 return(122);
00236 break;
00237 case 'pdf':
00238 if(!($mime == "application/pdf"))
00239 return(123);
00240 break;
00241 case 'wordDoc':
00242 if(!($mime == "application/msword"))
00243 return(124);
00244 break;
00245 case 'compressed':
00246 if(! ($mime == "application/x-compressed" || $mime == "application/x-zip" ||
00247 $mime == "application/zip" || $mime == "application/x-zip-compressed" || $mime == "application/x-gzip"))
00248 return (134);
00249 break;
00250 case 'image':
00251 if(! ($mime == "image/jpeg" || $mime == "image/pjpeg" || $mime == "image/jpg" || $mime == "image/png" || $mime == "image/gif"))
00252 return (125);
00253 break;
00254 case 'xml':
00255 if(! ($mime == "text/xml" || $mime == "application/xml"))
00256 return (126);
00257 break;
00258 case 'mp3':
00259 if(!($mime == "audio/mpeg"))
00260 return (127);
00261 break;
00262 case 'latex':
00263 if(!($mime == "application/x-latex"))
00264 return (128);
00265 case "text":
00266 if(!($mime == "text/plain"))
00267 return (129);
00268 break;
00269 case "html":
00270 if(!($mime == "text/html"))
00271 return (133);
00272 break;
00273 case "csv":
00274 if(!($mime == "text/comma-separated-values" || $mime == "text/plain" ||
00275 $mime == "text/csv" || $mime == "application/csv" || $mime == "application/excel" ||
00276 $mime == "application/vnd.ms-excel" || $mime == "application/vnd.msexcel"))
00277 return (136);
00278 break;
00279 default:
00280
00281 return (300);
00282 break;
00283 }
00284
00285 return(0);
00286
00287 }
00288
00297 protected function resolveFileExtension($mime) {
00298 if ($mime == '') return '';
00299 return($this->extensions[$mime]);
00300 }
00301
00311 public function fixExtension($input, $mime) {
00312
00313 $pattern = "/.([a-zA-Z]+)$/";
00314 $replacement = "." . $this->resolveFileExtension($mime);
00315 $input = preg_replace($pattern, $replacement, $input, 1);
00316
00317
00318 return $input;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00339 public function getErrorMessage($code) {
00340
00341 return $this->getErrorCode($code);
00342
00343 }
00344
00353 public function getErrorCode($code) {
00354
00355 if (!ctype_digit((string) $code)) {
00356 return ("");
00357 }
00358
00359
00360 return ($this->getErrorListing($code));
00361
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00384 protected function getErrorListing($code) {
00385
00386 if (!ctype_digit((string) $code)) {
00387 return ("");
00388 }
00389
00390
00391 if (isset($this->customCodes[$code])) return $this->customCodes[$code];
00392
00393
00394
00395
00396
00397
00398 $errorArray = $this->getErrorArray();
00399
00400 if (count($errorArray) > 0) {
00401
00402 if (@$errorArray[$code] != "") {
00403 return ($errorArray[$code]);
00404 }
00405
00406 }
00407
00408
00409
00410
00411
00412
00413 $xmlData = file_get_contents(dirname(__FILE__) . '/validationErrors.xml');
00414
00415 if ($xmlData) {
00416
00417
00418 $xmlData = simplexml_load_string($xmlData);
00419
00420 $tempErrorArray = array();
00421
00422 foreach ($xmlData AS $key) {
00423 $tempErrorArray[(int)$key->code] = (String)$key->mesg;
00424 }
00425
00426 $this->setErrorArray($tempErrorArray);
00427
00428
00429
00430
00431 $errorArray = $this->getErrorArray();
00432
00433 if (count($errorArray) > 0) {
00434
00435 if (isset($errorArray[$code]) && $errorArray[$code] != "") {
00436 return ($errorArray[$code]);
00437 } else {
00438 return ("Unknown error");
00439 }
00440
00441 }
00442 } else {
00443 return ("");
00444 }
00445
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00465 protected function getErrorArray() {
00466
00467 if ($this->errorList == "") {
00468 return (array());
00469 } else {
00470 return ($this->errorList);
00471 }
00472
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00494 protected function setErrorArray($array) {
00495 $this->errorList = $array;
00496 }
00497
00507 public function setCustomCode ($code, $value)
00508 {
00509 if (!ctype_digit((string) $code)) return false;
00510 $this->customCodes[$code] = $value;
00511 }
00512
00513
00514 function XMLParseErrorHandler($errno, $errstr, $errfile, $errline)
00515 {
00516 switch ($errno) {
00517 case 2:
00518 $this->setCustomCode(120, "Your XML did not parse for the following reasons. " . preg_replace("/\s/", " ", $errstr));
00519 break;
00520 default:
00521 break;
00522 }
00523 }
00524
00525
00526 }
00527
00528 ?>