Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.80 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Validation Class
  4.  *
  5.  * DISCLAIMER
  6.  *
  7.  * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8.  * versions in the future. If you wish to customise Jigoshop core for your needs,
  9.  * please use our GitHub repository to publish essential changes for consideration.
  10.  *
  11.  * @package             Jigoshop
  12.  * @category            Core
  13.  * @author              Jigoshop
  14.  * @copyright           Copyright © 2011-2014 Jigoshop.
  15.  * @license             GNU General Public License v3
  16.  */
  17. class jigoshop_validation {
  18.  
  19.     /**
  20.      * Validates a numeric integer value, positive or negative
  21.      * Must contain valid numeric characters [0-9]
  22.      *
  23.      * @param   string  value to determine if numeric integer value
  24.      * @return  boolean
  25.      */
  26.     public static function is_integer( $value ) {
  27.         if ( $value[0] == '-' || $value[0] == '+' ) {
  28.             return ctype_digit( substr( $value, 1 ) );
  29.         }
  30.         return ctype_digit( $value );
  31.     }
  32.  
  33.     /**
  34.      * Validates a numeric natural number, positive only
  35.      * Must contain valid numeric characters [0-9]
  36.      *
  37.      * @param   string  value to determine if natural number
  38.      * @return  boolean
  39.      */
  40.     public static function is_natural( $value ) {
  41.         return ctype_digit( $value );
  42.     }
  43.  
  44.     /**
  45.      * Validates a decimal value, positive or negative
  46.      * Must contain valid numeric characters [0-9]
  47.      * May contain optional decimal point followed by numeric characters [0-9]
  48.      *
  49.      * @param   string  value to determine if decimal value
  50.      * @return  boolean
  51.      */
  52.     public static function is_decimal( $value ) {
  53.         return is_numeric( $value );
  54.     }
  55.  
  56.     /**
  57.      * Validates an email using wordpress native is_email function
  58.      *
  59.      * @param   string  email address
  60.      * @return  boolean
  61.      */
  62.     public static function is_email( $email ) {
  63.         return is_email( $email );
  64.     }
  65.  
  66.     /**
  67.      * Validates a phone number using a regular expression
  68.      *
  69.      * @param   string  phone number
  70.      * @return  boolean
  71.      */
  72.     public static function is_phone( $phone ) {
  73.         if (strlen(trim(preg_replace('/[\s\#0-9_\-\+\(\)]/', '', $phone)))>0) return false;
  74.         return true;
  75.     }
  76.  
  77.     /**
  78.      * Checks for a valid postcode for a country
  79.      *
  80.      * @param   string  postcode
  81.      * @param   string  country
  82.      * @return  boolean
  83.      */
  84.     public static function is_postcode( $postcode, $country ) {
  85.         if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ))) > 0 ) return false;
  86.         $postcode = strtoupper( trim( $postcode ));
  87.         $regex = isset( self::$postcode_regex[$country] ) ? self::$postcode_regex[$country] : '';
  88.  
  89.         if ( Jigoshop_Base::get_options()->get_option( 'jigoshop_enable_postcode_validating' ) == 'yes'
  90.             && $regex <> '' ) {
  91.  
  92.             $regex = '/' . $regex . '/';
  93.             jigoshop_log( "VALIDATE POSTCODE: country = " . $country . " = regex = " . $regex );
  94.  
  95.             switch ( $country ) {
  96.                 case 'GB':
  97.                     return self::is_GB_postcode( $postcode );
  98.                 default:
  99.                     $match = preg_match( $regex, $postcode );
  100.                     if ( $match !== 1 ) return false;
  101.             }
  102.         }
  103.         return true;
  104.     }
  105.  
  106.     /* Author: John Gardner */
  107.     public static function is_GB_postcode( $toCheck ) {
  108.  
  109.         // Permitted letters depend upon their position in the postcode.
  110.         $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1
  111.         $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2
  112.         $alpha3 = "[abcdefghjkstuw]";                                   // Character 3
  113.         $alpha4 = "[abehmnprvwxy]";                                     // Character 4
  114.         $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5
  115.  
  116.         // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  117.         $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';
  118.  
  119.         // Expression for postcodes: ANA NAA
  120.         $pcexp[1] =  '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';
  121.  
  122.         // Expression for postcodes: AANA NAA
  123.         $pcexp[2] =  '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
  124.  
  125.         // Exception for the special postcode GIR 0AA
  126.         $pcexp[3] =  '^(gir)(0aa)$';
  127.  
  128.         // Standard BFPO numbers
  129.         $pcexp[4] = '^(bfpo)([0-9]{1,4})$';
  130.  
  131.         // c/o BFPO numbers
  132.         $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';
  133.  
  134.         // Load up the string to check, converting into lowercase and removing spaces
  135.         $postcode = strtolower($toCheck);
  136.         $postcode = str_replace (' ', '', $postcode);
  137.  
  138.         // Assume we are not going to find a valid postcode
  139.         $valid = false;
  140.  
  141.         // Check the string against the six types of postcodes
  142.         foreach ($pcexp as $regexp) {
  143.  
  144.             if (ereg($regexp,$postcode, $matches)) {
  145.  
  146.                 // Load new postcode back into the form element
  147.                 $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
  148.  
  149.                 // Take account of the special BFPO c/o format
  150.                 $toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
  151.  
  152.                 // Remember that we have found that the code is valid and break from loop
  153.                 $valid = true;
  154.                 break;
  155.             }
  156.         }
  157.  
  158.         if ($valid){return true;} else {return false;};
  159.     }
  160.  
  161.     /**
  162.      * Format the postcode according to the country and length of the postcode
  163.      *
  164.      * @param   string  postcode
  165.      * @param   string  country
  166.      * @return  string  formatted postcode
  167.      */
  168.     public static function format_postcode( $postcode, $country ) {
  169.         $postcode = strtoupper(trim($postcode));
  170.         $postcode = trim(preg_replace('/[\s]/', '', $postcode));
  171.  
  172.         if ($country=='GB') :
  173.             if (strlen($postcode)==7)
  174.                 $postcode = substr_replace($postcode, ' ', 4, 0);
  175.             else
  176.                 $postcode = substr_replace($postcode, ' ', 3, 0);
  177.         endif;
  178.         return $postcode;
  179.     }
  180.  
  181.     /**
  182.      * Postcode regexes for validation
  183.      *
  184.      * TODO: need to verify these country codes match ours, it doesn't appear all do
  185.      *
  186.      */
  187.     public static $postcode_regex = array(
  188.         "AD" => "AD\d{3}",
  189.         "AM" => "(37)?\d{4}",
  190.         "AR" => "^([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})$",
  191.         "AS" => "96799",
  192.         "AT" => "\d{4}",
  193.         "AU" => "^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
  194.         "AX" => "22\d{3}",
  195.         "AZ" => "\d{4}",
  196.         "BA" => "\d{5}",
  197.         "BB" => "(BB\d{5})?",
  198.         "BD" => "\d{4}",
  199.         "BE" => "^[1-9]{1}[0-9]{3}$",
  200.         "BG" => "\d{4}",
  201.         "BH" => "((1[0-2]|[2-9])\d{2})?",
  202.         "BM" => "[A-Z]{2}[ ]?[A-Z0-9]{2}",
  203.         "BN" => "[A-Z]{2}[ ]?\d{4}",
  204.         "BR" => "\d{5}[\-]?\d{3}",
  205.         "BY" => "\d{6}",
  206.         "CA" => "^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
  207.         "CC" => "6799",
  208.         "CH" => "^[1-9][0-9][0-9][0-9]$",
  209.         "CK" => "\d{4}",
  210.         "CL" => "\d{7}",
  211.         "CN" => "\d{6}",
  212.         "CR" => "\d{4,5}|\d{3}-\d{4}",
  213.         "CS" => "\d{5}",
  214.         "CV" => "\d{4}",
  215.         "CX" => "6798",
  216.         "CY" => "\d{4}",
  217.         "CZ" => "\d{3}[ ]?\d{2}",
  218.         "DE" => "\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
  219.         "DK" => "^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
  220.         "DO" => "\d{5}",
  221.         "DZ" => "\d{5}",
  222.         "EC" => "([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?",
  223.         "EE" => "\d{5}",
  224.         "EG" => "\d{5}",
  225.         "ES" => "^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
  226.         "ET" => "\d{4}",
  227.         "FI" => "\d{5}",
  228.         "FK" => "FIQQ 1ZZ",
  229.         "FM" => "(9694[1-4])([ \-]\d{4})?",
  230.         "FO" => "\d{3}",
  231.         "FR" => "^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
  232.         "GE" => "\d{4}",
  233.         "GF" => "9[78]3\d{2}",
  234.         "GL" => "39\d{2}",
  235.         "GN" => "\d{3}",
  236.         "GP" => "9[78][01]\d{2}",
  237.         "GR" => "\d{3}[ ]?\d{2}",
  238.         "GS" => "SIQQ 1ZZ",
  239.         "GT" => "\d{5}",
  240.         "GU" => "969[123]\d([ \-]\d{4})?",
  241.         "GW" => "\d{4}",
  242.         "HM" => "\d{4}",
  243.         "HN" => "(?:\d{5})?",
  244.         "HR" => "\d{5}",
  245.         "HT" => "\d{4}",
  246.         "HU" => "\d{4}",
  247.         "ID" => "\d{5}",
  248.         "IE" => "((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?",
  249.         "IL" => "\d{5}",
  250.         "IN"=> "^[1-9][0-9][0-9][0-9][0-9][0-9]$",
  251.         "IO" => "BBND 1ZZ",
  252.         "IQ" => "\d{5}",
  253.         "IS" => "\d{3}",
  254.         "IT" => "^(V-|I-)?[0-9]{5}$",
  255.         "JO" => "\d{5}",
  256.         "JP" => "\d{3}-\d{4}",
  257.         "KE" => "\d{5}",
  258.         "KG" => "\d{6}",
  259.         "KH" => "\d{5}",
  260.         "KR" => "\d{3}[\-]\d{3}",
  261.         "KW" => "\d{5}",
  262.         "KZ" => "\d{6}",
  263.         "LA" => "\d{5}",
  264.         "LB" => "(\d{4}([ ]?\d{4})?)?",
  265.         "LI" => "(948[5-9])|(949[0-7])",
  266.         "LK" => "\d{5}",
  267.         "LR" => "\d{4}",
  268.         "LS" => "\d{3}",
  269.         "LT" => "\d{5}",
  270.         "LU" => "\d{4}",
  271.         "LV" => "\d{4}",
  272.         "MA" => "\d{5}",
  273.         "MC" => "980\d{2}",
  274.         "MD" => "\d{4}",
  275.         "ME" => "8\d{4}",
  276.         "MG" => "\d{3}",
  277.         "MH" => "969[67]\d([ \-]\d{4})?",
  278.         "MK" => "\d{4}",
  279.         "MN" => "\d{6}",
  280.         "MP" => "9695[012]([ \-]\d{4})?",
  281.         "MQ" => "9[78]2\d{2}",
  282.         "MT" => "[A-Z]{3}[ ]?\d{2,4}",
  283.         "MU" => "(\d{3}[A-Z]{2}\d{3})?",
  284.         "MV" => "\d{5}",
  285.         "MX" => "\d{5}",
  286.         "MY" => "\d{5}",
  287.         "NC" => "988\d{2}",
  288.         "NE" => "\d{4}",
  289.         "NF" => "2899",
  290.         "NG" => "(\d{6})?",
  291.         "NI" => "((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?",
  292.         "NL" => "^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
  293.         "NO" => "\d{4}",
  294.         "NP" => "\d{5}",
  295.         "NZ" => "\d{4}",
  296.         "OM" => "(PC )?\d{3}",
  297.         "PF" => "987\d{2}",
  298.         "PG" => "\d{3}",
  299.         "PH" => "\d{4}",
  300.         "PK" => "\d{5}",
  301.         "PL" => "\d{2}-\d{3}",
  302.         "PM" => "9[78]5\d{2}",
  303.         "PN" => "PCRN 1ZZ",
  304.         "PR" => "00[679]\d{2}([ \-]\d{4})?",
  305.         "PT" => "\d{4}([\-]\d{3})?",
  306.         "PW" => "96940",
  307.         "PY" => "\d{4}",
  308.         "RE" => "9[78]4\d{2}",
  309.         "RO" => "\d{6}",
  310.         "RS" => "\d{6}",
  311.         "RU" => "\d{6}",
  312.         "SA" => "\d{5}",
  313.         "SE" => "^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
  314.         "SG" => "\d{6}",
  315.         "SH" => "(ASCN|STHL) 1ZZ",
  316.         "SI" => "\d{4}",
  317.         "SJ" => "\d{4}",
  318.         "SK" => "\d{3}[ ]?\d{2}",
  319.         "SM" => "4789\d",
  320.         "SN" => "\d{5}",
  321.         "SO" => "\d{5}",
  322.         "SZ" => "[HLMS]\d{3}",
  323.         "TC" => "TKCA 1ZZ",
  324.         "TH" => "\d{5}",
  325.         "TJ" => "\d{6}",
  326.         "TM" => "\d{6}",
  327.         "TN" => "\d{4}",
  328.         "TR" => "\d{5}",
  329.         "TW" => "\d{3}(\d{2})?",
  330.         "UA" => "\d{5}",
  331.         "UK" => "^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
  332.         "US" => "^\d{5}([\-]?\d{4})?$",
  333.         "UY" => "\d{5}",
  334.         "UZ" => "\d{6}",
  335.         "VA" => "00120",
  336.         "VE" => "\d{4}",
  337.         "VI" => "008(([0-4]\d)|(5[01]))([ \-]\d{4})?",
  338.         "WF" => "986\d{2}",
  339.         "YT" => "976\d{2}",
  340.         "YU" => "\d{5}",
  341.         "ZA" => "\d{4}",
  342.         "ZM" => "\d{5}",
  343.     );
  344.  
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement