Ignore:
Timestamp:
Feb 20, 2014 3:03:59 AM (10 years ago)
Author:
anonymous
Message:

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/lib/Validator.inc.php

    r459 r468  
    3131 */
    3232
    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 
    4733class Validator {
    4834
     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
    4954    /**
    5055     * Ensures a value is empty.
     
    5358     * @return bool   true if form is not empty, false otherwise.
    5459     */
    55     function notEmpty($val)
     60    static public function notEmpty($val)
    5661    {
    5762        return '' != trim((string)$val);
     
    6469     * @return bool   true if form is empty, false otherwise.
    6570     */
    66     function isEmpty($val)
     71    static public function isEmpty($val)
    6772    {
    6873        return '' == trim((string)$val);
     
    7580     * @return bool   true if form is a string, false otherwise.
    7681     */
    77     function isString($val)
     82    static public function isString($val)
    7883    {
    7984        return '' == trim((string)$val) || is_string($val);
     
    8691     * @return bool   True if no errors found, false otherwise.
    8792     */
    88     function isNumber($val)
     93    static public function isNumber($val)
    8994    {
    9095        return '' == trim((string)$val) || is_numeric($val);
     
    98103     * @return bool   true if value is an integer
    99104     */
    100     function isInteger($val, $negative_ok=false)
     105    static public function isInteger($val, $negative_ok=false)
    101106    {
    102107        $pattern = $negative_ok ? '/^-?[[:digit:]]+$/' : '/^[[:digit:]]+$/';
     
    112117     * @return bool   true if value is a float
    113118     */
    114     function isFloat($val, $negative_ok=false)
     119    static public function isFloat($val, $negative_ok=false)
    115120    {
    116121        $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/';
     
    124129     * @return bool   true if value is a float
    125130     */
    126     function isArray($val)
     131    static public function isArray($val)
    127132    {
    128133        return (is_string($val) && '' == trim((string)$val)) || is_array($val);
     
    138143     * @return bool   true if value passes regex test
    139144     */
    140     function checkRegex($val, $regex, $valid_on_match=true)
     145    static public function checkRegex($val, $regex, $valid_on_match=true)
    141146    {
    142147        return $valid_on_match ? preg_match($regex, $val) : !preg_match($regex, $val);
     
    151156     * @return bool   true if string length is within given boundaries
    152157     */
    153     function stringLength($val, $min, $max)
     158    static public function stringLength($val, $min, $max)
    154159    {
    155160        return mb_strlen(trim((string)$val)) >= $min && mb_strlen($val) <= $max;
     
    164169     * @return bool   True if no errors found, false otherwise.
    165170     */
    166     function numericRange($val, $min, $max)
     171    static public function numericRange($val, $min, $max)
    167172    {
    168173        return '' == trim((string)$val) || (is_numeric($val) && $val >= $min && $val <= $max);
     
    180185     * @access  public
    181186     * @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
    183189     * @author  Quinn Comendant <quinn@strangecode.com>
    184190     */
    185     function validateEmail($val)
     191    static public function validateEmail($val, $strict=false)
    186192    {
    187193        require_once 'codebase/lib/Email.inc.php';
     
    190196        // Test email address format.
    191197        if (!preg_match($e->getParam('regex'), $val, $e_parts)) {
    192             return VALIDATE_EMAIL_REGEX_FAIL;
     198            return self::EMAIL_REGEX_FAIL;
    193199        }
    194200
     
    203209        // Test length.
    204210        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;
    216242    }
    217243
     
    223249     * @return bool    True if no errors found, false otherwise.
    224250     */
    225     function validateStrDate($val)
    226     {
    227         $app =& App::getInstance();
    228 
     251    static public function validateStrDate($val)
     252    {
    229253        if (is_string($val) && '' === trim($val)) {
    230254            // Don't be too bothered about empty strings.
     
    249273     * @return bool    True if no errors found, false otherwise.
    250274     */
    251     function validateCCNumber($val, $cc_type=null)
    252      {
    253          // Get rid of any non-digits
    254          $cc_num = preg_replace('/[^\d]/', '', $val);
    255 
    256          // Perform card-specific checks, if applicable
    257          switch ($cc_type) {
    258              case CC_TYPE_VISA :
    259                  $regex = '/^4\d{15}$|^4\d{12}$/';
    260                  break;
    261              case CC_TYPE_MASTERCARD :
    262                  $regex = '/^5[1-5]\d{14}$/';
    263                  break;
    264              case CC_TYPE_AMEX :
    265                  $regex = '/^3[47]\d{13}$/';
    266                  break;
    267              case CC_TYPE_DISCOVER :
    268                  $regex = '/^6011\d{12}$/';
    269                  break;
    270              case CC_TYPE_DINERS :
    271                  $regex = '/^30[0-5]\d{11}$|^3[68]\d{12}$/';
    272                  break;
    273              case CC_TYPE_JCB :
    274                  $regex = '/^3\d{15}$|^2131|1800\d{11}$/';
    275                  break;
    276              default :
    277                  $regex = '';
    278                  break;
    279          }
    280 
    281          if ('' != $regex && !preg_match($regex, $cc_num)) {
    282              // Invalid format.
    283              return false;
    284          }
    285 
    286          // The Luhn formula works right to left, so reverse the number.
    287          $cc_num = strrev($cc_num);
    288 
    289          $luhn_total = 0;
    290 
    291          $num = mb_strlen($cc_num);
    292          for ($i=0; $i<$num; $i++) {
    293              // Get each digit.
    294              $digit = mb_substr($cc_num, $i, 1);
    295 
    296              //  If it's an odd digit, double it.
    297              if ($i / 2 != floor($i / 2)) {
    298                  $digit *= 2;
    299              }
    300 
    301              //  If the result is two digits, add them.
    302              if (mb_strlen($digit) == 2) {
    303                  $digit = mb_substr($digit, 0, 1) + mb_substr($digit, 1, 1);
    304              }
    305 
    306              //  Add the current digit to the $luhn_total.
    307              $luhn_total += $digit;
    308          }
    309 
    310          // If the Total is evenly divisible by 10, it's cool!
    311          return $luhn_total % 10 == 0;
    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    }
    313337
    314338    /**
     
    318342     * @return bool   True if no errors found, false otherwise.
    319343     */
    320     function fileUploaded($form_name)
     344    static public function fileUploaded($form_name)
    321345    {
    322346        if (!isset($_FILES[$form_name]['name']) || empty($_FILES[$form_name]['name'])) {
     
    341365} // THE END
    342366
    343 ?>
Note: See TracChangeset for help on using the changeset viewer.