Changeset 23


Ignore:
Timestamp:
Dec 1, 2005 8:45:15 PM (18 years ago)
Author:
scdev
Message:

Added Email() class to work with verification, and sending of emails and templates. Updated Formvalidator to use the regex in Email().

Location:
trunk/lib
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r22 r23  
    479479        $event['type'] = $this->logPriorityToString($priority);
    480480        $event['file:line'] = "$file : $line";
    481         $event['message'] = strip_tags(preg_replace('/\s{2,}/', ' ', $message));
    482    
     481        preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow); // <...> with lots of chars maybe we don't want stripped.
     482        $event['message'] = strip_tags(preg_replace('/\s{2,}/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null));
    483483        $event_str = '[' . join('] [', $event) . ']';
    484484       
  • trunk/lib/FormValidator.inc.php

    r22 r23  
    374374
    375375    /**
    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)
    384390    {
    385391        $email = getFormData($form_name);
     
    387393            return false;
    388394        }
    389        
     395
     396        require_once 'codebase/lib/Email.inc.php';
     397        $e = new Email();
     398
    390399        // 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
    403411        // 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)));
    405414            App::logMsg(sprintf('The email address %s must contain less than 256 characters.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
    406415            return false;
    407416        }
    408417       
    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;
    417423        }
    418424       
    419425        return true;
    420426    }
     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//     }
    421465
    422466    /**
Note: See TracChangeset for help on using the changeset viewer.