Validate_BE
[ class tree: Validate_BE ] [ index: Validate_BE ] [ all elements ]

Source for file BE.php

Documentation is available at BE.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Data validation class for Belgium
  5.  *
  6.  * PHP Versions 4 and 5
  7.  *
  8.  * This source file is subject to the New BSD license, That is bundled
  9.  * with this package in the file LICENSE, and is available through
  10.  * the world-wide-web at
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the new BSDlicense and are unable
  13.  * to obtain it through the world-wide-web, please send a note to
  14.  * pajoye@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category  Validate
  17.  * @package   Validate_BE
  18.  * @author    Christophe Gesché <moosh@php.net>
  19.  * @copyright 1997-2005 Christophe Gesché
  20.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  21.  * @version   CVS: $Id: BE.php,v 1.15 2009/01/18 21:38:28 clockwerx Exp $
  22.  * @link      http://pear.php.net/package/Validate_BE
  23.  */
  24. require_once 'Validate.php';
  25.  
  26. /**
  27.  * @constant VALIDATE_BE_PHONENUMBER_TYPE_ANY
  28.  * @constant VALIDATE_BE_PHONENUMBER_TYPE_NORMAL
  29.  * @constant VALIDATE_BE_PHONENUMBER_TYPE_MOBILE
  30.  */
  31. //Any belgian phonenumber
  32. define('VALIDATE_BE_PHONENUMBER_TYPE_ANY'0);     
  33. //only normal phonenumber (mobile numers are not allowed)
  34. define('VALIDATE_BE_PHONENUMBER_TYPE_NORMAL'1);     
  35. //only mobile numbers are allowed 
  36. define('VALIDATE_BE_PHONENUMBER_TYPE_MOBILE'2);    
  37. define('VALIDATE_BE_BANKCODE_MODULUS'97);
  38. define('VALIDATE_BE_BANK_TRANSFER_MESSAGE'97);
  39. define('VALIDATE_BE_SSN_MODULUS'97);
  40. define('VALIDATE_BE_VAT_MODULUS'97);
  41.  
  42. /**
  43.  * Data validation class for Belgium
  44.  *
  45.  * This class provides methods to validate:
  46.  *  - Postal code
  47.  *  - Belgian bank code
  48.  *
  49.  * @category  Validate
  50.  * @package   Validate_BE
  51.  * @author    Christophe Gesché <moosh@php.net>
  52.  * @copyright 1997-2005 Christophe Gesché
  53.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  54.  * @version   Release:@packageversion@
  55.  * @link      http://pear.php.net/package/Validate_BE
  56.  */
  57. {
  58.     /**
  59.      * Validate a Belgian social security number
  60.      *
  61.      * The belgian social security number is on the SIS card of all belgian.
  62.      *
  63.      * A check digit is the last one, computed the standard
  64.      * _get_control_number function.
  65.      *
  66.      * @param string $nationalId ssn to validate
  67.      *
  68.      * @static
  69.      * @access   public
  70.      * @return   bool    true on success
  71.      */
  72.     function nationalId($nationalId)
  73.     {
  74.         $nationalId strtr($nationalId'-/\ :''.....');
  75.         $nationalId str_replace('.'''$nationalId);
  76.         // RULE 1 : 11 digit.
  77.         if (!(bool) ereg('^[0-9]{11}$'$nationalId)) {
  78.             return false;
  79.         }
  80.  
  81.  
  82.         // RULE 2 : ssn begin with a reversed date
  83.         $year   substr($nationalId02);
  84.         $month  substr($nationalId22);
  85.         $day    substr($nationalId42);
  86.         $number substr($nationalId09);
  87.         $check  substr($nationalId92);
  88.  
  89.         /**
  90.          * Check that the date is valid
  91.          */
  92.         if (!Validate::date("$year-$month-$day"array('format' => '%y-%m-%d'))) {
  93.             return false;
  94.         }
  95.  
  96.         // RULE 3 check is 97 - modulo 97 of all or RULES 3BIS
  97.         if ((97 - Validate::_modf($numberVALIDATE_BE_SSN_MODULUS)) == $check{
  98.             return true;
  99.         }
  100.  
  101.         // RULE 3 BIS
  102.         // if RULE 3 check faild, prepend 2 to the number and retry
  103.         if ((97 - Validate::_modf(2000000000 + $number
  104.                         VALIDATE_BE_SSN_MODULUS)) == $check{
  105.             return true;
  106.         }
  107.  
  108.         return false;
  109.     }
  110.  
  111.     /**
  112.      * Validate a Belgian social security number
  113.      *
  114.      * The belgian social security number is on the SIS card of all belgian.
  115.      *
  116.      * A check digit is the last one, computed the standard
  117.      * _get_control_number function.
  118.      *
  119.      * @param string $ssn ssn to validate
  120.      *
  121.      * @access   public
  122.      * @return   bool    true on success
  123.      * @static
  124.      */
  125.     function ssn($ssn)
  126.     {
  127.         // RULE 1 : 11 digit.
  128.         if (!(bool) ereg('^[0-9]{11}$'$ssn)) {
  129.             return false;
  130.         }
  131.  
  132.  
  133.         // RULE 2 : ssn begin with a reversed date
  134.         $year   substr($ssn02);
  135.         $month  substr($ssn22);
  136.         $day    substr($ssn42);
  137.         $number substr($ssn09);
  138.         $check  substr($ssn92);
  139.  
  140.         /**
  141.          * Check that the date is valid
  142.          */
  143.         if (!Validate::date("$year-$month-$day"array('format' => '%y-%m-%d'))) {
  144.             return false;
  145.         }
  146.  
  147.         // RULE 3 check is 97 - modulo 97 of all or RULES 3BIS
  148.         if ((97 - Validate::_modf($numberVALIDATE_BE_SSN_MODULUS)) == $check{
  149.             return true;
  150.         }
  151.  
  152.         // RULE 3 BIS
  153.         // if RULE 3 check faild, prepend 2 to the number and retry
  154.         if ((97 - Validate::_modf(2000000000 + $number
  155.                         VALIDATE_BE_SSN_MODULUS)) == $check{
  156.             return true;
  157.         }
  158.  
  159.         return false;
  160.     }
  161.  
  162.     /**
  163.      * Validate a Belgian postcode
  164.      *
  165.      * @param string $postcode postcode to validate
  166.      * @param bool   $strong   optional; strong checks (e.g. against a list
  167.      *                          of postcodes)
  168.      * @param string $dataDir  optional; name of directory datafile can be
  169.      *                          found in. set during install process but may be
  170.      *                          overridden.
  171.      *
  172.      * @return  bool    true if postcode is ok, false otherwise
  173.      */
  174.     function postalCode($postcode$strong = false$dataDir = null)
  175.     {
  176.         static $postCodeList = null;
  177.  
  178.         $postcode = ltrim(ltrim(strtolower($postcode)'b')'-');
  179.         if ($strong{
  180.             $paths = array();
  181.             if (!empty($dataDir)) {
  182.                 $paths[$dataDir '/BE_postcodes.txt';
  183.             }
  184.             $paths['@DATADIR@/Validate_BE/BE_postcodes.txt';
  185.             $paths[dirname(dirname(__FILE__)) '/data/BE_postcodes.txt';
  186.  
  187.             if (!isset($postCodeList)) {
  188.                 $file = null;
  189.                 foreach ($paths as $path{
  190.                     if (file_exists($path)) {
  191.                         $file $path;
  192.                     }
  193.                 }
  194.  
  195.                 if (empty($file)) {
  196.                     return false;
  197.                 }
  198.  
  199.                 if (file_exists($file)) {
  200.                     foreach (file($fileas $line{
  201.                         $postCodeList[substr($line04);
  202.                     }
  203.                 }
  204.             }
  205.  
  206.             return (bool) in_array((int) $postcode$postCodeList);
  207.         }
  208.         return (bool) ereg('^[1-9][0-9]{3}$'$postcode);
  209.     }
  210.  
  211.     /**
  212.      * Validate a Belgian bank account number
  213.      *
  214.      * Belgian bankcodes consist of
  215.      *  - 3-figure number for the bank socity
  216.      *  - 7-figure number for the account number
  217.      *  - 2-figure number for mod 97
  218.      *
  219.      * @param string $bankCode Belgian bankcode to validate
  220.      *
  221.      * @return  bool    true if bankcode is ok, false otherwise
  222.      */
  223.     function bankCode($bankCode)
  224.     {
  225.         if ((bool) ereg('^[0-9]{3}[ ./-]?[0-9]{7}[ ./-]?[0-9]{2}$'$bankCode)) {
  226.             $bankCode str_replace(' '''strtr($bankCode'/-.''   '));
  227.             $num      substr($bankCode010);
  228.             $checksum substr($bankCode102);
  229.             if ($checksum == Validate::_modf($numVALIDATE_BE_BANKCODE_MODULUS)) {
  230.                 return true;
  231.             else {
  232.                 return false;
  233.             }
  234.         else {
  235.             return false;
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Validate a Belgian transfert message
  241.      *
  242.      * Belgian transfert (virement) can be done with a structured message
  243.      * 12 figure
  244.      *  -10-figure number for the message
  245.      *  - 2-figure number for mod 97
  246.      *
  247.      * @param string $bankTransferMessage Belgian bankcode to validate
  248.      *
  249.      * @return  bool    true if bankcode is ok, false otherwise
  250.      */
  251.     function bankTransferMessage($bankTransferMessage)
  252.     {
  253.         if ((bool) ereg('^[0-9]{3}[ ./-]?[0-9]{4}[ ./-]?[0-9]{5}$',
  254.                     $bankTransferMessage)) {
  255.             $message strtr($bankTransferMessage'/-.''   ');
  256.  
  257.             $bankTransferMessage str_replace(' '''$message);
  258.  
  259.             $num      substr($bankTransferMessage010);
  260.             $checksum substr($bankTransferMessage102);
  261.             if ($checksum == Validate::_modf($num
  262.                                              VALIDATE_BE_BANK_TRANSFER_MESSAGE)) {
  263.                 return true;
  264.             else {
  265.                 return false;
  266.             }
  267.         else {
  268.             return false;
  269.         }
  270.     }
  271.  
  272.     /**
  273.      * Validate a VAT account number
  274.      *
  275.      * Belgian VAT consist of
  276.      *  - 3-figure number
  277.      *
  278.      * Actually no doc was found about a checksum
  279.      *
  280.      * @param string $vat Belgian VAT to validate
  281.      *
  282.      * @return  bool    true if VAT is ok, false otherwise
  283.      */
  284.     function vat($vat)
  285.     {
  286.         if ((bool) ereg('^[0-9]{3}[ ./-]?[0-9]{3}[ ./-]?[0-9]{3}$'
  287.                           $vat))) {
  288.             $vat str_replace(' '''strtr($vat'/-.''   '));
  289.             if (strlen($vat== 9{
  290.                 $number   substr($vat07);
  291.                 $checksum substr($vat72);
  292.                 if (VALIDATE_BE_VAT_MODULUS - $checksum == 
  293.                     (Validate::_modf($numberVALIDATE_BE_VAT_MODULUS))) {
  294.                     return true;
  295.                 }
  296.             }
  297.         }
  298.         return false;
  299.     }
  300.  
  301.  
  302.     /**
  303.      * Validate a phonenumber
  304.      * 065 12 34 56
  305.      * 02 123 45 67
  306.      * O485 12 34 56
  307.      * 00 32 65 12 34 56
  308.      * 00 32 2 123 45 67
  309.      * 00 32 485 12 34 56
  310.      * + 32 65 12 34 56
  311.      * + 32 2 123 45 67
  312.      * + 32 485 12 34 56
  313.      * ++ 32 65 12 34 56
  314.      * ++ 32 2 123 45 67
  315.      * ++ 32 485 12 34 56
  316.      *
  317.      * 010 Waver
  318.      * 011 Hasselt
  319.      * 012 Tongeren
  320.      * 013 Diest
  321.      * 014 Herentals, Turnhout
  322.      * 015 Mechelen
  323.      * 016 Leuven, Tienen
  324.      * 019 Borgworm (Frans: Waremme)
  325.      * 02  Brussel
  326.      * 03  Antwerpen
  327.      * 04  Luik
  328.      * 050 Brugge, Zeebrugge
  329.      * 051 Roeselare
  330.      * 052 Dendermonde
  331.      * 053 Aalst
  332.      * 054 Ninove
  333.      * 055 Ronse
  334.      * 056 Kortrijk, Komen-Waasten
  335.      * 057 Ieper
  336.      * 058 Veurne
  337.      * 059 Oostende
  338.      * 060 Chimay
  339.      * 061 Libramont-Chevigny
  340.      * 063 Aarlen
  341.      * 064 La Louvière
  342.      * 065 Bergen (Frans: Mons)
  343.      * 067 Nijvel
  344.      * 068 Ath
  345.      * 069 Doornik
  346.      * 071 Charleroi
  347.      * 080 Stavelot
  348.      * 081 Namen
  349.      * 082 Dinant
  350.      * 083 Ciney
  351.      * 084 Marche-en-Famenne
  352.      * 085 Hoei
  353.      * 086 Durbuy
  354.      * 087 Verviers
  355.      * 089 Genk
  356.      * 09  Gent
  357.      *
  358.      * 0472-0479 mobiel (Proximus)
  359.      * 0485-0486 mobiel (Base)
  360.      * 0494-0499 mobiel (Mobistar)
  361.      *
  362.      * 070 commercieel
  363.      * 0800 gratis
  364.      * 0900-0905 commercieel
  365.      *
  366.      * NOTE : this validate want a BELGIAN phonenumber to return true,
  367.      * not a valid number to call FROM belgium
  368.      *
  369.      * @param string $phonenumber Belgian phonenumber (can be in international
  370.      *                             format (eg +32 or 0032)
  371.      * @param int    $type        Type of phonenumber to check / to attempt
  372.      *
  373.      * @return  bool    true if (phone) number is correct
  374.      * @see http://roamers.proximus.be/fr/Call_In_Belgium/CIB_AZ.html
  375.      */
  376.     function phoneNumber($phonenumber$type = VALIDATE_BE_PHONENUMBER_TYPE_ANY)
  377.     {
  378.         $zoneprefixes['littlezone'= array  ('010''011''012''013''014',
  379.                                               '015''016''019''050''051',
  380.                                               '052''053''054''055''056',
  381.                                               '057''058''059''060''061',
  382.                                               '063''064''065''067''068',
  383.                                               '069''071''080''081''082',
  384.                                               '083''084''085''086''087',
  385.                                               '089');
  386.         $zoneprefixes['bigzone']    = array('02''03''04''09');
  387.         $zoneprefixes['mobile']     = array('0472''0473''0474''0475'
  388.                                             '0476''0477''0478''0479 ',
  389.                                             '0485''0486 ''0494''0495',
  390.                                             '0496''0497''0498''0499');
  391.  
  392.         $result = false;
  393.         // Cleaning the phone number
  394.         $phonenumber trim($phonenumber);
  395.         // international numba can begin with + or ++
  396.         // If one, prepend a second
  397.         if ($phonenumber[0== '+' && $phonenumber[1!= '+'{
  398.             $phonenumber '+' $phonenumber;
  399.         }
  400.  
  401.         // if  phone number begin by a + replace it by 0
  402.         // replace other non numerical  charater witha  blancspace
  403.         // finaly remove all blank spaces.
  404.         $phonenumber str_replace(' '''strtr($phonenumber'+/-.''0   '));
  405.  
  406.  
  407.         // detect if it's a national or international numba
  408.         if (substr($phonenumber02== '00'{
  409.             //   $is_inter = TRUE;
  410.             // International Number
  411.             if (!substr($phonenumber22== '32'{
  412.                 // international number but not with belgian prefix
  413.                 // validate::failure ('not the international prefix');
  414.                 return false;
  415.             else {
  416.                 $phonenumber str_replace('0032''0'$phonenumber);
  417.             }
  418.  
  419.         }
  420.  
  421.         // search national prefix of numba (mobile, big zone, little zone)
  422.         $is_mobile (in_array(substr($phonenumber04)$zoneprefixes['mobile']));
  423.         if (!$is_mobile{
  424.             $in_bigzone    in_array(substr($phonenumber02),
  425.                                       $zoneprefixes['bigzone']);
  426.             $in_littlezone in_array(substr($phonenumber03),
  427.                                       $zoneprefixes['littlezone']);
  428.         }
  429.         // if national prefix not detected, it's a bad number
  430.         if $is_mobile && $in_bigzone && $in_littlezone{
  431.             return false; // wrong prefix
  432.         }
  433.  
  434.         // try to accept as Type VALIDATE_BE_PHONENUMBER_TYPE_NORMAL (not mobile)
  435.         if ($type == VALIDATE_BE_PHONENUMBER_TYPE_ANY || 
  436.             $type == VALIDATE_BE_PHONENUMBER_TYPE_NORMAL{
  437.             if (!$is_mobile && 
  438.                  strlen($phonenumber== 9 && 
  439.                  is_numeric($phonenumber)) {
  440.                  $result = true; //we have a 9 digit numeric number.
  441.             }
  442.         }
  443.  
  444.         // try to accept as Type VALIDATE_BE_PHONENUMBER_TYPE_MOBILE
  445.         if ($type == VALIDATE_BE_PHONENUMBER_TYPE_ANY ||
  446.             $type == VALIDATE_BE_PHONENUMBER_TYPE_MOBILE{
  447.             if ($is_mobile &&
  448.                 strlen($phonenumber== 10 &&
  449.                 is_numeric($phonenumber)) {
  450.                 $result = true;     //we have a 9 digit numeric number.
  451.             }
  452.         }
  453.  
  454.         //we need at least 9 digits
  455.         if (ereg('^[+0-9]{9,}$'$phonenumber)) {
  456.             $phonenumber substr($phonenumber-9);
  457.             //we only use the last 9 digits (so no troubles with 
  458.             //international numbers)
  459.             if (strlen($phonenumber>= 10{
  460.  
  461.             }
  462.         }
  463.         return $result;
  464.     }
  465. }
  466. ?>

Documentation generated on Mon, 23 Mar 2009 02:00:02 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.