Changeset 468 for trunk/lib/Validator.inc.php
- Timestamp:
- Feb 20, 2014 3:03:59 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
/branches/eli_branch merged eligible
-
Property
svn:mergeinfo
set to
-
trunk/lib/Validator.inc.php
r459 r468 31 31 */ 32 32 33 // Known credit card types.34 define('CC_TYPE_VISA', 1);35 define('CC_TYPE_MASTERCARD', 2);36 define('CC_TYPE_AMEX', 3);37 define('CC_TYPE_DISCOVER', 4);38 define('CC_TYPE_DINERS', 5);39 define('CC_TYPE_JCB', 6);40 41 // validateEmail return types.42 define('VALIDATE_EMAIL_SUCCESS', 0);43 define('VALIDATE_EMAIL_REGEX_FAIL', 1);44 define('VALIDATE_EMAIL_LENGTH_FAIL', 2);45 define('VALIDATE_EMAIL_MX_FAIL', 3);46 47 33 class Validator { 48 34 35 // Known credit card types. 36 const CC_TYPE_VISA = 1; 37 const CC_TYPE_MASTERCARD = 2; 38 const CC_TYPE_AMEX = 3; 39 const CC_TYPE_DISCOVER = 4; 40 const CC_TYPE_DINERS = 5; 41 const CC_TYPE_JCB = 6; 42 43 // Validator::validateEmail() return types. 44 const EMAIL_SUCCESS = 0; 45 const EMAIL_REGEX_FAIL = 1; 46 const EMAIL_LENGTH_FAIL = 2; 47 const EMAIL_MX_FAIL = 3; 48 49 // Validator::validatePhone() return types. 50 const PHONE_SUCCESS = 0; 51 const PHONE_REGEX_FAIL = 1; 52 const PHONE_LENGTH_FAIL = 2; 53 49 54 /** 50 55 * Ensures a value is empty. … … 53 58 * @return bool true if form is not empty, false otherwise. 54 59 */ 55 function notEmpty($val)60 static public function notEmpty($val) 56 61 { 57 62 return '' != trim((string)$val); … … 64 69 * @return bool true if form is empty, false otherwise. 65 70 */ 66 function isEmpty($val)71 static public function isEmpty($val) 67 72 { 68 73 return '' == trim((string)$val); … … 75 80 * @return bool true if form is a string, false otherwise. 76 81 */ 77 function isString($val)82 static public function isString($val) 78 83 { 79 84 return '' == trim((string)$val) || is_string($val); … … 86 91 * @return bool True if no errors found, false otherwise. 87 92 */ 88 function isNumber($val)93 static public function isNumber($val) 89 94 { 90 95 return '' == trim((string)$val) || is_numeric($val); … … 98 103 * @return bool true if value is an integer 99 104 */ 100 function isInteger($val, $negative_ok=false)105 static public function isInteger($val, $negative_ok=false) 101 106 { 102 107 $pattern = $negative_ok ? '/^-?[[:digit:]]+$/' : '/^[[:digit:]]+$/'; … … 112 117 * @return bool true if value is a float 113 118 */ 114 function isFloat($val, $negative_ok=false)119 static public function isFloat($val, $negative_ok=false) 115 120 { 116 121 $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/'; … … 124 129 * @return bool true if value is a float 125 130 */ 126 function isArray($val)131 static public function isArray($val) 127 132 { 128 133 return (is_string($val) && '' == trim((string)$val)) || is_array($val); … … 138 143 * @return bool true if value passes regex test 139 144 */ 140 function checkRegex($val, $regex, $valid_on_match=true)145 static public function checkRegex($val, $regex, $valid_on_match=true) 141 146 { 142 147 return $valid_on_match ? preg_match($regex, $val) : !preg_match($regex, $val); … … 151 156 * @return bool true if string length is within given boundaries 152 157 */ 153 function stringLength($val, $min, $max)158 static public function stringLength($val, $min, $max) 154 159 { 155 160 return mb_strlen(trim((string)$val)) >= $min && mb_strlen($val) <= $max; … … 164 169 * @return bool True if no errors found, false otherwise. 165 170 */ 166 function numericRange($val, $min, $max)171 static public function numericRange($val, $min, $max) 167 172 { 168 173 return '' == trim((string)$val) || (is_numeric($val) && $val >= $min && $val <= $max); … … 180 185 * @access public 181 186 * @param string $val The input data to validate.. 182 * @return const One of the constant values: VALIDATE_EMAIL_SUCCESS|VALIDATE_EMAIL_REGEX_FAIL|VALIDATE_EMAIL_LENGTH_FAIL|VALIDATE_EMAIL_MX_FAIL 187 * @param bool $strict Do we run strict tests? 188 * @return const One of the constant values: Validate::EMAIL_SUCCESS|Validate::EMAIL_REGEX_FAIL|Validate::EMAIL_LENGTH_FAIL|Validate::EMAIL_MX_FAIL 183 189 * @author Quinn Comendant <quinn@strangecode.com> 184 190 */ 185 function validateEmail($val)191 static public function validateEmail($val, $strict=false) 186 192 { 187 193 require_once 'codebase/lib/Email.inc.php'; … … 190 196 // Test email address format. 191 197 if (!preg_match($e->getParam('regex'), $val, $e_parts)) { 192 return VALIDATE_EMAIL_REGEX_FAIL;198 return self::EMAIL_REGEX_FAIL; 193 199 } 194 200 … … 203 209 // Test length. 204 210 if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) { 205 return VALIDATE_EMAIL_LENGTH_FAIL; 206 } 207 208 // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists. 209 // Compare ip2long twice for php4 backwards compat. 210 if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 211 // FIXME: Do we care? 212 // return VALIDATE_EMAIL_MX_FAIL; 213 } 214 215 return VALIDATE_EMAIL_SUCCESS; 211 return self::EMAIL_LENGTH_FAIL; 212 } 213 214 // Strict tests below. 215 216 // Check domain exists: It's a domain if ip2long fails; checkdnsrr ensures a MX record exists; gethostbyname() ensures the domain exists. 217 if ($strict && ip2long($domain) === false && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 218 return self::EMAIL_MX_FAIL; 219 } 220 221 return self::EMAIL_SUCCESS; 222 } 223 224 /** 225 * Check whether input is a valid phone number. Notice: it is now set 226 * to allow characters like - or () or + so people can type in a phone 227 * number that looks like: +1 (530) 555-1212 228 * 229 * @param string $form_name the name of the incoming form variable 230 * 231 * @return bool true if no errors found, false otherwise 232 */ 233 static public function validatePhone($val) 234 { 235 if (!self::checkRegex($val, '/^[0-9 +().-]*$/', true)) { 236 return self::PHONE_REGEX_FAIL; 237 } 238 if (!self::stringLength($val, 0, 25)) { 239 return self::PHONE_LENGTH_FAIL; 240 } 241 return self::PHONE_SUCCESS; 216 242 } 217 243 … … 223 249 * @return bool True if no errors found, false otherwise. 224 250 */ 225 function validateStrDate($val) 226 { 227 $app =& App::getInstance(); 228 251 static public function validateStrDate($val) 252 { 229 253 if (is_string($val) && '' === trim($val)) { 230 254 // Don't be too bothered about empty strings. … … 249 273 * @return bool True if no errors found, false otherwise. 250 274 */ 251 function validateCCNumber($val, $cc_type=null)252 253 254 255 256 257 258 caseCC_TYPE_VISA :259 260 261 caseCC_TYPE_MASTERCARD :262 263 264 caseCC_TYPE_AMEX :265 266 267 caseCC_TYPE_DISCOVER :268 269 270 caseCC_TYPE_DINERS :271 272 273 caseCC_TYPE_JCB :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 275 static public function validateCCNumber($val, $cc_type=null) 276 { 277 // Get rid of any non-digits 278 $cc_num = preg_replace('/[^\d]/', '', $val); 279 280 // Perform card-specific checks, if applicable 281 switch ($cc_type) { 282 case self::CC_TYPE_VISA : 283 $regex = '/^4\d{15}$|^4\d{12}$/'; 284 break; 285 case self::CC_TYPE_MASTERCARD : 286 $regex = '/^5[1-5]\d{14}$/'; 287 break; 288 case self::CC_TYPE_AMEX : 289 $regex = '/^3[47]\d{13}$/'; 290 break; 291 case self::CC_TYPE_DISCOVER : 292 $regex = '/^6011\d{12}$/'; 293 break; 294 case self::CC_TYPE_DINERS : 295 $regex = '/^30[0-5]\d{11}$|^3[68]\d{12}$/'; 296 break; 297 case self::CC_TYPE_JCB : 298 $regex = '/^3\d{15}$|^2131|1800\d{11}$/'; 299 break; 300 default : 301 $regex = ''; 302 break; 303 } 304 305 if ('' != $regex && !preg_match($regex, $cc_num)) { 306 // Invalid format. 307 return false; 308 } 309 310 // The Luhn formula works right to left, so reverse the number. 311 $cc_num = strrev($cc_num); 312 313 $luhn_total = 0; 314 315 $num = mb_strlen($cc_num); 316 for ($i=0; $i<$num; $i++) { 317 // Get each digit. 318 $digit = mb_substr($cc_num, $i, 1); 319 320 // If it's an odd digit, double it. 321 if ($i / 2 != floor($i / 2)) { 322 $digit *= 2; 323 } 324 325 // If the result is two digits, add them. 326 if (mb_strlen($digit) == 2) { 327 $digit = mb_substr($digit, 0, 1) + mb_substr($digit, 1, 1); 328 } 329 330 // Add the current digit to the $luhn_total. 331 $luhn_total += $digit; 332 } 333 334 // If the Total is evenly divisible by 10, it's cool! 335 return $luhn_total % 10 == 0; 336 } 313 337 314 338 /** … … 318 342 * @return bool True if no errors found, false otherwise. 319 343 */ 320 function fileUploaded($form_name)344 static public function fileUploaded($form_name) 321 345 { 322 346 if (!isset($_FILES[$form_name]['name']) || empty($_FILES[$form_name]['name'])) { … … 341 365 } // THE END 342 366 343 ?>
Note: See TracChangeset
for help on using the changeset viewer.