Changeset 568 for branches/1.1dev/lib


Ignore:
Timestamp:
Oct 29, 2016 2:15:08 AM (8 years ago)
Author:
anonymous
Message:

Backported improvements of email domain validation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/FormValidator.inc.php

    r409 r568  
    11<?php
    22/**
    3  * FormValidator.inc.php 
     3 * FormValidator.inc.php
    44 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
    55 */
    6  
     6
    77//     Examples of use:
    88//
     
    3535class FormValidator
    3636{
    37    
     37
    3838    /**
    3939     * Array filling with errors. The key will be the name of the form where
     
    4141     */
    4242    var $errors = array();
    43    
     43
    4444    /**
    4545     * Return the current list of errors.
     
    5353        return $this->errors;
    5454    }
    55    
     55
    5656    /**
    5757     * Add an error to the errors stack.
     
    7474        );
    7575    }
    76    
     76
    7777    /**
    7878     * Check whether any errors have been triggered.
     
    8080     * @param  string $form_name the name of the incoming form variable
    8181     *
    82      * @return bool   true if any errors were found, or if found for 
     82     * @return bool   true if any errors were found, or if found for
    8383     *                a variable of $form_name, false otherwise
    8484     */
     
    8888            foreach ($this->errors as $err) {
    8989                if ($err['name'] == $form_name) {
    90                     return true;   
     90                    return true;
    9191                }
    9292            }
     
    134134    function notEmpty($form_name, $msg='')
    135135    {
    136    
     136
    137137        $val = getFormData($form_name);
    138138        if (is_array($val)) {
     
    143143                return false;
    144144            }
    145         } else {           
     145        } else {
    146146            if (trim($val) != '') {
    147147                $this->addError($form_name, $msg);
     
    171171                return false;
    172172            }
    173         } else {           
     173        } else {
    174174            if (trim($val) == '') {
    175175                $this->addError($form_name, $msg);
     
    242242    /**
    243243     * Check whether input is a float. Don't just use is_float() because the
    244      * data coming from the user is *really* a string. Integers will also 
     244     * data coming from the user is *really* a string. Integers will also
    245245     * pass this test.
    246246     *
     
    280280        }
    281281    }
    282    
     282
    283283    /**
    284284     * Check whether input matches the specified perl regular expression
    285      * pattern. 
     285     * pattern.
    286286     *
    287287     * @param  string $form_name the name of the incoming form variable
     
    312312        }
    313313    }
    314    
     314
    315315    /**
    316316     * Tests if the string length is between specified values. Whitespace excluded for min.
     
    326326    {
    327327        $val = getFormData($form_name);
    328        
     328
    329329        if (strlen(trim($val)) < $min || strlen($val) > $max) {
    330330            $this->addError($form_name, $msg);
     
    361361
    362362    /**
    363      * Validates email address length, domain name existance, format.
     363     * Validates email address length, domain name existence, format.
    364364     *
    365365     * @param  string  $form_name       The name of the incoming form variable
    366      *
     366     * @param   bool   $strict Run strict tests (check if the domain exists and has an MX record assigned)
    367367     * @return bool    true if no errors found, false otherwise
    368368     */
    369     function validateEmail($form_name)
    370     {       
     369    function validateEmail($form_name, $strict=false)
     370    {
    371371        $email = getFormData($form_name);
    372372        if ('' == trim($email)) {
    373373            return false;
    374374        }
    375        
     375
    376376        $regex = '/^(?:[^,@]*\s+|[^,@]*(<)|)'                           // Display name
    377377        . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)'    // Local-part
     
    386386        . '|'
    387387        . '(?:|\s*|\s+\([^,@]+\)\s*))$/i';
    388        
     388
    389389        // Test email address format.
    390390        if (!preg_match($regex, getFormData($form_name), $e_parts)) {
     
    393393            return false;
    394394        }
    395        
     395
    396396        // We have a match! Here are the captured subpatterns, on which further tests are run.
    397         // The part before the @. 
     397        // The part before the @.
    398398        $local = $e_parts[2];
    399399
    400         // The part after the @. 
     400        // The part after the @.
    401401        // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets.
    402402        $domain = $e_parts[3]{0} == '[' ? mb_substr($e_parts[3], 1, -1) : $e_parts[3];
    403        
     403
    404404        // Test length.
    405405        if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) {
     
    408408            return false;
    409409        }
    410        
    411         // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists.
    412         // Compare ip2long twice for php4 backwards compat.
    413         if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) {
    414             $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name."), oTxt(getFormData($form_name))), MSG_ERR, __FILE__, __LINE__);
    415             logMsg(sprintf('The email address %s does not have a valid domain name.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
    416             return false;
    417         }
    418        
     410
     411        if ($strict) {
     412            // Strict tests.
     413            if (ip2long($domain) === false && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) {
     414                // Check domain exists: It's a domain if ip2long fails; checkdnsrr ensures a MX record exists; gethostbyname() ensures the domain exists.
     415                $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name."), oTxt(getFormData($form_name))), MSG_ERR, __FILE__, __LINE__);
     416                $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)));
     417                return false;
     418            }
     419        }
     420
    419421        return true;
    420422    }
     
    432434    {
    433435        $phone = getFormData($form_name);
    434        
     436
    435437        $this->checkRegex($form_name, '/^[0-9 +().-]*$/', true, sprintf(_("The phone number <strong>%s</strong> is not valid."), $phone));
    436438        $this->stringLength($form_name, 0, 25, sprintf(_("The phone number <strong>%s</strong> is too long"), $phone));
     
    455457        }
    456458    }
    457    
    458    
     459
     460
    459461    /**
    460462     * Verifies credit card number.
     
    471473            $cc_num = getFormData($form_name);
    472474        }
    473        
     475
    474476        if ('' == $cc_num) {
    475477            return false;
    476478        }
    477        
     479
    478480        // Innocent until proven guilty
    479481        $card_is_valid = true;
    480    
     482
    481483        // Get rid of any non-digits
    482484        $cc_num = preg_replace('/[^\d]/', '', $cc_num);
    483    
     485
    484486        // Perform card-specific checks, if applicable
    485487        switch (strtolower($cc_type)) {
     
    514516                break;
    515517        }
    516    
     518
    517519        // The Luhn formula works right to left, so reverse the number.
    518520        $cc_num = strrev($cc_num);
    519        
     521
    520522        $luhn_total = 0;
    521523
     
    529531                $digit *= 2;
    530532            }
    531    
     533
    532534            //  If the result is two digits, add them.
    533535            if (strlen($digit) == 2) {
    534536                $digit = substr($digit,0,1) + substr($digit,1,1);
    535537            }
    536    
     538
    537539            //  Add the current digit to the $luhn_total.
    538540            $luhn_total += $digit;
    539541        }
    540    
     542
    541543        // If it passed (or bypassed) the card-specific check and the Total is evenly divisible by 10, it's cool!
    542544        if ($card_is_valid && $luhn_total % 10 == 0) {
     
    565567        }
    566568    }
    567    
     569
    568570} // THE END
    569571
Note: See TracChangeset for help on using the changeset viewer.