00001 <?php 00008 abstract class SValidation extends SObject { 00009 00010 const TYPE_NUMBER = 10; 00011 const TYPE_INTEGER = 11; const TYPE_INT = 11; 00012 const TYPE_FLOAT = 12; 00013 const TYPE_STRING = 20; 00014 const TYPE_ALPHA = 21; 00015 const TYPE_ALPHANUMERIC = 22; 00016 const TYPE_EMAIL = 23; 00017 const TYPE_POSTAL_CODE = 24; 00018 const TYPE_URL = 25; 00019 const TYPE_SHORT_NAME = 26; 00020 const TYPE_TIME = 30; 00021 const TYPE_DATE = 31; 00022 const TYPE_XML = 40; 00023 const TYPE_HTML = 41; 00024 00034 public static function validateGETVars($attributes, $prm = null) { 00035 if ($prm == null) 00036 $prm = new PageRender2(); 00037 $safeVars = array(); 00038 foreach($attributes as $attribute => $properties) { 00039 // Set default values 00040 if (!isset($properties['null'])) 00041 $properties['null'] = true; 00042 if (!isset($properties['type'])) 00043 $properties['type'] = self::TYPE_STRING; 00044 // If attribute is not in GET vars 00045 if (!isset($_GET[$attribute])) { 00046 // If it's required, throw an error 00047 if ($properties['null'] === false) 00048 $prm->flashAndExit('No value set for required input ' . $attribute); 00049 // If it's not required, continue to process next attribute 00050 else 00051 continue; 00052 } 00053 // Validate the GET var 00054 $value = $_GET[$attribute]; 00055 if ($value == '' && $properties['null'] == true) { 00056 // if it's blank but allowed to be null, that's OK 00057 } else if(!self::validate($value, $properties['type'])) { 00058 // otherwise, validate it and report errors 00059 $prm->flashAndExit('Value for ' . $attribute . ' was not of the correct type.'); 00060 } 00061 $safeVars[$attribute] = $value; 00062 } 00063 return $safeVars; 00064 } 00065 00075 public function validate($input, $type) { 00076 switch($type) { 00077 case self::TYPE_NUMBER: 00078 return (ctype_digit((string) $input)); 00079 case self::TYPE_INTEGER: 00080 return (is_int($input)); 00081 case self::TYPE_FLOAT: 00082 return (is_float($input)); 00083 case self::TYPE_STRING: 00084 return (is_string($input)); 00085 case self::TYPE_ALPHA: 00086 return self::validateInputByPattern($input, '/^[a-zA-Z]+$/'); 00087 case self::TYPE_ALPHANUMERIC: 00088 return self::validateInputByPattern($input, '/^[a-zA-Z0-9]+$/'); 00089 default: 00090 return false; 00091 } 00092 } 00093 00103 public static function validateInputByPattern($input, $pattern) { 00104 return preg_match($pattern, $input, $matches); 00105 } 00106 00107 00108 /* 00109 00110 00111 public function validateInput ($input, $type ) { 00112 switch ($type) 00113 { 00114 case 'string': 00115 $pattern = "/^[a-zA-Z\s]+$/"; 00116 $errorCode = 100; 00117 break; 00118 case 'numbers': 00119 $pattern = "/^(\-)?[0-9\.]+(\s)?$/"; 00120 $errorCode = 101; 00121 break; 00122 case 'alphanumeric': 00123 $pattern = "/^[0-9a-zA-Z\s]+$/"; 00124 $errorCode = 102; 00125 break; 00126 case 'name': 00127 $pattern = "/^([a-zA-Z][A-Za-z\-\.\,]*(\s)?)+$/"; 00128 $errorCode = 103; 00129 break; 00130 case 'email': 00131 $pattern = "/^[a-zA-Z0-9\.\_\-]+\@[a-zA-Z0-9\-\_]+\.[a-z0-9\-\_\.]+(\s)?$/"; 00132 $errorCode = 104; 00133 break; 00134 case 'address': 00135 $pattern = "/^[a-zA-Z0-9\s\.\-\,\#]+(\s)?$/"; 00136 $errorCode = 105; 00137 break; 00138 case 'city': 00139 $pattern = "/^[a-zA-Z0-9\s\-\.]+(\s)?$/"; 00140 $errorCode = 106; 00141 break; 00142 case 'state': 00143 $pattern = "/^[a-zA-Z\s\.]+(\s)?$/"; 00144 $errorCode = 107; 00145 break; 00146 case 'zip': 00147 $pattern = "/^[0-9]{5,9}(\-[0-9]{4})?(\s)?$/"; 00148 $errorCode = 108; 00149 break; 00150 case 'website': 00151 $pattern = "/^[a-zA-Z0-9\.\-\:\~\/\_]+(\s)?$/"; 00152 $errorCode = 109; 00153 break; 00154 case 'checkbox': 00155 $pattern = "/^([a-zA-Z0-9\.\_\/\(\)]+(\s)?)+$/"; 00156 $errorCode = 110; 00157 break; 00158 case 'pulldown': 00159 $pattern = "/^[a-zA-Z\s\_]+$/"; 00160 $errorCode = 111; 00161 break; 00162 case 'phone': 00163 $pattern = "/^[0-9a-zA-Z\-\s\(\)\+]{7,20}$/"; 00164 $errorCode = 112; 00165 break; 00166 case 'birthday': 00167 $pattern = "/^[a-zA-Z0-9\s\,\-\/]+$/"; 00168 $errorCode = 113; 00169 break; 00170 case 'paragraph': 00171 $pattern = "//"; 00172 $errorCode = 115; 00173 break; 00174 case 'shortName': 00175 $pattern = "/^[a-zA-Z0-9\_\-\s\.]+$/"; 00176 $errorCode = 116; 00177 break; 00178 case 'organization': 00179 $pattern = "/^([A-Za-z0-9\-\.\,]*(\s)?)+$/"; 00180 $errorCode = 117; 00181 break; 00182 case 'username': 00183 $pattern = "/^[a-z][a-z0-9\_]{2,13}$/"; 00184 $errorCode = 130; 00185 break; 00186 case 'time': 00187 $pattern = "/^[0-9]{1,2}[hH](\s)?[0-9]{1,2}[mM](\s)?$/"; 00188 $errorCode = 131; 00189 break; 00190 case 'date': 00191 //Explode the date by the slash separator 00192 $explodedPatt = explode("/",$input); 00193 if(sizeof($explodedPatt) != 3) 00194 return 132; 00195 $day = $explodedPatt[1]; 00196 $month = $explodedPatt[0]; 00197 $year = $explodedPatt[2]; 00198 // Check the normal ranges for numbers of day, month, and year 00199 $truthiness = (strlen($day) == 2) 00200 && ($day <= 31); 00201 $truthiness = $truthiness 00202 && (strlen($month) == 2) 00203 && ($month <= 12); 00204 $truthiness = $truthiness 00205 && (strlen($year) == 4) 00206 && ($year > 0) 00207 && ($year < (date("Y") + 20)); 00208 // Check for 29-day and 30-day months 00209 if($day > 29 && $month == 2) 00210 $truthiness = false; 00211 else if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && ($day > 30)) 00212 $truthiness = false; 00213 // If true, return 0; if false, return error code 132 00214 return $truthiness; 00215 break; 00216 case 'password': 00217 $pattern = "/^[\S]{6,10}$/"; 00218 $errorCode = 118; 00219 break; 00220 case 'fileName': 00221 $pattern = "/^[a-zA-Z0-9\.\-]+$/"; 00222 $errorCode = 120; 00223 break; 00224 case 'xml': 00225 case 'mixedxml': 00226 $input = utf8_encode($input); 00227 00228 $old_error_handler = set_error_handler(array($this, "XMLParseErrorHandler")); 00229 if ($type == "mixedxml") { $input = "<root> $input </root>"; } 00230 //print("checking $input"); 00231 $check = simplexml_load_string("\n $input \n"); 00232 restore_error_handler(); 00233 if ($check === false) return 119; 00234 else return 0; 00235 break; 00236 default: 00237 $pattern = "//"; 00238 $errorCode = 0; 00239 break; 00240 } 00241 */ 00242 }
1.5.6