Changeset 23 for trunk/lib/FormValidator.inc.php
- Timestamp:
- Dec 1, 2005 8:45:15 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FormValidator.inc.php
r22 r23 374 374 375 375 /** 376 * Validates email address length, domain name existance, format. 377 * 378 * @param string $form_name The name of the incoming form variable 379 * @param boolean $allow_fullname Allow the use of rfc822 expanded email address with comment: Quinn Commie <quinn@strangecode.com> 380 * 381 * @return bool true if no errors found, false otherwise 382 */ 383 function validateEmail($form_name, $allow_fullname=false) 376 * Validates an email address based on the recommendations in RFC 3696. 377 * Is more loose than restrictive, to allow the many valid variants of 378 * email addresses while catching the most common mistakes. 379 * http://www.faqs.org/rfcs/rfc822.html 380 * http://www.faqs.org/rfcs/rfc2822.html 381 * http://www.faqs.org/rfcs/rfc3696.html 382 * http://www.faqs.org/rfcs/rfc1035.html 383 * 384 * @access public 385 * @param string $form_name The name of the incoming form variable. 386 * @return bool Validity of address. 387 * @author Quinn Comendant <quinn@strangecode.com> 388 */ 389 function validateEmail($form_name) 384 390 { 385 391 $email = getFormData($form_name); … … 387 393 return false; 388 394 } 389 395 396 require_once 'codebase/lib/Email.inc.php'; 397 $e = new Email(); 398 390 399 // Test email address format. 391 if ($allow_fullname) { 392 if (!$this->checkRegex($form_name, '/^[\w\s]*<?[^\s@\[\]<>]{1,}\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5}>?$/i', true, sprintf(_("<strong>%s</strong> is not a valid email address."), $email))) { 393 App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 394 return false; 395 } 396 } else { 397 if (!$this->checkRegex($form_name, '/^[^\s@\[\]<>]{1,}\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5}$/i', true, sprintf(_("<strong>%s</strong> is not a valid email address."), $email))) { 398 App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 399 return false; 400 } 401 } 402 400 if (!preg_match($e->getParam('regex'), $email, $e_parts)) { 401 $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email address."), oTxt($email))); 402 App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 403 return false; 404 } 405 406 // We have a match! Here are the captured subpatterns, on which further tests are run. 407 $local = $e_parts[2]; 408 // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets. 409 $domain = $e_parts[3]{0} == '[' ? substr($e_parts[3], 1, -1) : $e_parts[3]; 410 403 411 // Test length. 404 if (!$this->stringLength($form_name, 0, 255, sprintf(_("<strong>Email address</strong> must contain less than 256 characters."), $email))) { 412 if (strlen($local) > 64 || strlen($domain) > 191) { 413 $this->addError($form_name, sprintf(_("<strong>Email address</strong> must contain less than 256 characters."), oTxt($email))); 405 414 App::logMsg(sprintf('The email address %s must contain less than 256 characters.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 406 415 return false; 407 416 } 408 417 409 // Check domain exists and has valid MX record. 410 preg_match('/^[\w\s]*<?[^\s@\[\]<>]{1,}\@([A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/i', $email, $matches); 411 if (!empty($matches[1])) { 412 if (!checkdnsrr($matches[1] . '.', 'MX') && gethostbyname($matches[1]) == $matches[1]) { 413 $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email domain name"), $matches[1])); 414 App::logMsg(sprintf('The email address %s contains an invalid email domain name (%s).', getFormData($form_name), $matches[1]), LOG_DEBUG, __FILE__, __LINE__); 415 return false; 416 } 418 // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists. 419 if (ip2long($domain) == '-1' && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 420 $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email domain name"), oTxt($domain))); 421 App::logMsg(sprintf('The email address %s contains an invalid email domain name (%s).', getFormData($form_name), $domain), LOG_DEBUG, __FILE__, __LINE__); 422 return false; 417 423 } 418 424 419 425 return true; 420 426 } 427 // function validateEmail($form_name, $allow_fullname=false) 428 // { 429 // $email = getFormData($form_name); 430 // if ('' == trim($email)) { 431 // return false; 432 // } 433 // 434 // // Test email address format. 435 // if ($allow_fullname) { 436 // if (!$this->checkRegex($form_name, '/^[^<>@]*<?[^\s@\[\]<>()]+\@[A-Za-z0-9.-]+\.[A-Za-z]{2,5}>?$/i', true, sprintf(_("<strong>%s</strong> is not a valid email address."), $email))) { 437 // App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 438 // return false; 439 // } 440 // } else { 441 // if (!$this->checkRegex($form_name, '/^[^\s@\[\]<>()]+\@[A-Za-z0-9.-]+\.[A-Za-z]{2,5}$/i', true, sprintf(_("<strong>%s</strong> is not a valid email address."), $email))) { 442 // App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 443 // return false; 444 // } 445 // } 446 // 447 // // Test length. 448 // if (!$this->stringLength($form_name, 0, 255, sprintf(_("<strong>Email address</strong> must contain less than 256 characters."), $email))) { 449 // App::logMsg(sprintf('The email address %s must contain less than 256 characters.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 450 // return false; 451 // } 452 // 453 // // Check domain exists and has valid MX record. 454 // preg_match('/^[^<>@]*<?[^\s@\[\]<>()]+\@([A-Za-z0-9.-]+\.[A-Za-z]{2,5})>?$/i', $email, $matches); 455 // if (!empty($matches[1])) { 456 // if (!checkdnsrr($matches[1] . '.', 'MX') && gethostbyname($matches[1]) == $matches[1]) { 457 // $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email domain name"), $matches[1])); 458 // App::logMsg(sprintf('The email address %s contains an invalid email domain name (%s).', getFormData($form_name), $matches[1]), LOG_DEBUG, __FILE__, __LINE__); 459 // return false; 460 // } 461 // } 462 // 463 // return true; 464 // } 421 465 422 466 /**
Note: See TracChangeset
for help on using the changeset viewer.