TimeTrex Community Edition v16.2.0

This commit is contained in:
2022-12-13 07:10:06 +01:00
commit 472f000c1b
6810 changed files with 2636142 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 Stefan Neufeind |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSDlicense and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | pajoye@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Tomas V.V.Cox <cox@idecnet.com> |
// | Pierre-Alain Joye <pajoye@php.net> |
// +----------------------------------------------------------------------+
//
/**
* Financial functions for validation and calculation
*
* @category Validate
* @package Validate_Finance
* @author Stefan Neufeind <pear.neufeind@speedpartner.de>
* @copyright 2005 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version CVS: $Id: Finance.php,v 1.11 2005/11/01 13:12:30 pajoye Exp $
* @link http://pear.php.net/package/Validate_Finance
*/
/**
* Requires the IBAN Finance Validate class
*/
require_once 'Validate/Finance/IBAN.php';
/**
* Financial functions for validation and calculation
*
* This class provides methods to validate:
* - IBAN (international bankaccount number)
* - Euro banknote id
*
* @category Validate
* @package Validate_Finance
* @author Stefan Neufeind <neufeind@speedpartner.de>
* @copyright 1997-2005 Stefan Neufeind
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_Finance
*/
class Validate_Finance
{
/**
* Validation of an IBAN (international bankaccount number)
*
* @param string $iban IBAN to be validated
* @access public
* @since 0.1
* @return boolean true if IBAN is okay
*/
function iban($iban = '')
{
$validate_finance_iban = new Validate_Finance_IBAN($iban);
return $validate_finance_iban->validate($iban);
} // end func iban
/**
* Validation of a Euro banknote id
*
* @param string $banknote Euro banknote id to be validated
* @access public
* @since 0.1
* @return boolean true if Euro banknote id is okay
*/
function banknoteEuro($banknote = '')
{
$euro_countrycode = array('J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
if (strlen($banknote) != 12) {
return false;
}
if (!in_array($banknote[0], $euro_countrycode)) {
return false;
}
// build checksum, preparation
$banknote_replace_chars = range('A', 'Z');
foreach (range(10, 35) as $tempvalue) {
$banknote_replace_values[] = strval($tempvalue);
}
// build checksum, substitute and calc
$tempbanknote = str_replace($banknote_replace_chars, $banknote_replace_values, substr($banknote, 0, -1));
$tempcheckvalue = 0;
for ($strcounter = 0; $strcounter < strlen($tempbanknote); $strcounter++) {
$tempcheckvalue += intval($tempbanknote[$strcounter]);
}
$tempcheckvalue %= 9; // modulo 9
$tempcheckvalue = 8 - $tempcheckvalue;
return (intval($banknote[strlen($banknote)-1]) == $tempcheckvalue);
} // end func banknoteEuro
} // end class Validate_Finance
?>
@@ -0,0 +1,257 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 Stefan Neufeind |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSDlicense and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | pajoye@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Stefan Neufeind <pear.neufeind@speedpartner.de> |
// +----------------------------------------------------------------------+
//
/**
* Validation methods for credit card related data
*
* @category Validate
* @package Validate_Finance_CreditCard
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 1997-2005 Stefan Neufeind
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: CreditCard.php,v 1.12 2005/11/01 13:12:31 pajoye Exp $
* @link http://pear.php.net/package/Validate_Finance_CreditCard
*/
/**
* Credit card related information validation class
*
* This class provides methods to validate:
* - Credit card number
* - Card security code
* - Card type (i.e. Visa, Mastercard...)
*
* The methods only check the format of the data. For instance
* the package does NOT check if a card is a legitimate card registered
* with a card issuer, or if the card is reported stolen, etc...
*
* @category Validate
* @package Validate_Finance_CreditCard
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @author Ondrej Jombik <nepto@pobox.sk>
* @copyright 1997-2005 Stefan Neufeind
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_Finance_CreditCard
*/
class Validate_Finance_CreditCard
{
/**
* Validates a number according to Luhn check algorithm
*
* This function checks given number according Luhn check
* algorithm. It is published on several places, also here:
*
* @link http://www.webopedia.com/TERM/L/Luhn_formula.html
* @link http://www.merriampark.com/anatomycc.htm
* @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
* @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
*
* @param string $number to check
* @return bool TRUE if number is valid, FALSE otherwise
* @access public
* @static
* @author Ondrej Jombik <nepto@pobox.sk>
*/
static function Luhn($number)
{
$len_number = strlen($number);
$sum = 0;
for ($k = $len_number % 2; $k < $len_number; $k += 2) {
if ((intval($number[$k]) * 2) > 9) {
$sum += (intval($number[$k]) * 2) - 9;
} else {
$sum += intval($number[$k]) * 2;
}
}
for ($k = ($len_number % 2) ^ 1; $k < $len_number; $k += 2) {
$sum += intval($number[$k]);
}
return ($sum % 10) ? false : true;
}
/**
* Validates a credit card number
*
* If a type is passed, the card will be checked against it.
* This method only checks the number locally. No banks or payment
* gateways are involved.
* This method doesn't guarantee that the card is legitimate. It merely
* checks the card number passes a mathematical algorithm.
*
* @param string $creditCard number (spaces and dashes tolerated)
* @param string $cardType type/brand of card (case insensitive)
* "MasterCard", "Visa", "AMEX", "AmericanExpress",
* "American Express", "Diners", "DinersClub", "Diners Club",
* "CarteBlanche", "Carte Blanche", "Discover", "JCB",
* "EnRoute", "Eurocard", "Eurocard/MasterCard".
* @return bool TRUE if number is valid, FALSE otherwise
* @access public
* @static
* @see Luhn()
*/
static function number($creditCard, $cardType = null)
{
$cc = str_replace(array('-', ' '), '', $creditCard);
if (($len = strlen($cc)) < 13
|| strspn($cc, '0123456789') != $len) {
return false;
}
// Only apply the Luhn algorithm for cards other than enRoute
// So check if we have a enRoute card now
if (strlen($cc) != 15
|| (substr($cc, 0, 4) != '2014'
&& substr($cc, 0, 4) != '2149')) {
if (!Validate_Finance_CreditCard::Luhn($cc)) {
return false;
}
}
if (is_string($cardType)) {
return Validate_Finance_CreditCard::type($cc, $cardType);
}
return true;
}
/**
* Validates the credit card number against a type
*
* This method only checks for the type marker. It doesn't
* validate the card number. Some card "brands" share the same
* numbering system, so checking the card type against any of the
* sister brand will return the same result.
*
* For instance, if a $card is a MasterCard, type($card, 'EuroCard')
* will also return true.
*
* @param string $creditCard number (spaces and dashes tolerated)
* @param string $cardType type/brand of card (case insensitive)
* "MasterCard", "Visa", "AMEX", "AmericanExpress",
* "American Express", "Diners", "DinersClub", "Diners Club",
* "CarteBlanche", "Carte Blanche", "Discover", "JCB",
* "EnRoute", "Eurocard", "Eurocard/MasterCard".
* @return bool TRUE is type matches, FALSE otherwise
* @access public
* @static
* @link http://www.beachnet.com/~hstiles/cardtype.html
*/
static function type($creditCard, $cardType)
{
switch (strtoupper($cardType)) {
case 'MASTERCARD':
case 'EUROCARD':
case 'EUROCARD/MASTERCARD':
$regex = '5[1-5][0-9]{14}';
break;
case 'VISA':
$regex = '4([0-9]{12}|[0-9]{15})';
break;
case 'AMEX':
case 'AMERICANEXPRESS':
case 'AMERICAN EXPRESS':
$regex = '3[47][0-9]{13}';
break;
case 'DINERS':
case 'DINERSCLUB':
case 'DINERS CLUB':
case 'CARTEBLANCHE':
case 'CARTE BLANCHE':
$regex = '3(0[0-5][0-9]{11}|[68][0-9]{12})';
break;
case 'DISCOVER':
$regex = '6011[0-9]{12}';
break;
case 'JCB':
$regex = '(3[0-9]{15}|(2131|1800)[0-9]{11})';
break;
case 'ENROUTE':
$regex = '2(014|149)[0-9]{11}';
break;
default:
return false;
}
$regex = '/^' . $regex . '$/';
$cc = str_replace(array('-', ' '), '', $creditCard);
return (bool)preg_match($regex, $cc);
}
/**
* Validates a card verification value format
*
* This method only checks for the format. It doesn't
* validate that the value is the one on the card.
*
* CVV is also known as
* - CVV2 Card Validation Value 2 (Visa)
* - CVC Card Validation Code (MasterCard)
* - CID Card Identification (American Express and Discover)
* - CIN Card Identification Number
* - CSC Card Security Code
*
* Important information regarding CVV:
* If you happen to have to store credit card information, you must
* NOT retain the CVV after transaction is complete. Usually this
* means you cannot store it in a database, not even in an encrypted
* form.
*
* This method returns FALSE for card types that don't support CVV.
*
* @param string $cvv value to verify
* @param string $cardType type/brand of card (case insensitive)
* "MasterCard", "Visa", "AMEX", "AmericanExpress",
* "American Express", "Discover", "Eurocard/MasterCard",
* "Eurocard"
* @return bool TRUE if format is correct, FALSE otherwise
* @access public
* @static
*/
static function cvv($cvv, $cardType)
{
switch (strtoupper($cardType)) {
case 'MASTERCARD':
case 'EUROCARD':
case 'EUROCARD/MASTERCARD':
case 'VISA':
case 'DISCOVER':
$digits = 3;
break;
case 'AMEX':
case 'AMERICANEXPRESS':
case 'AMERICAN EXPRESS':
$digits = 4;
break;
default:
return false;
}
if (strlen($cvv) == $digits
&& strspn($cvv, '0123456789') == $digits) {
return true;
}
return false;
}
}
?>
+571
View File
@@ -0,0 +1,571 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 Stefan Neufeind |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSDlicense and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | pajoye@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Stefan Neufeind <pear.neufeind@speedpartner.de> |
// +----------------------------------------------------------------------+
//
/**
* Methods for common data validations
*
* @category Validate
* @package Validate_Finance_IBAN
* @author Stefan Neufeind <pear.neufeind@speedpartner.de>
* @copyright 2005 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: IBAN.php,v 1.18 2006/06/09 16:04:29 neufeind Exp $
* @link http://pear.php.net/package/Validate_Finance_IBAN
*/
/*
* Error codes for the IBAN interface, which will be mapped to textual messages
* in the IBAN::errorMessage() function. If you are to add a new error code, be
* sure to add the textual messages to the IBAN::errorMessage() function as well
*/
define('VALIDATE_FINANCE_IBAN_OK', 1);
define('VALIDATE_FINANCE_IBAN_ERROR', -1);
define('VALIDATE_FINANCE_IBAN_GENERAL_INVALID', -2);
define('VALIDATE_FINANCE_IBAN_TOO_SHORT', -4);
define('VALIDATE_FINANCE_IBAN_TOO_LONG', -5);
define('VALIDATE_FINANCE_IBAN_COUNTRY_INVALID', -6);
define('VALIDATE_FINANCE_IBAN_INVALID_FORMAT', -7); // tested via regex; e.g. if un-allowed characters in IBAN
define('VALIDATE_FINANCE_IBAN_CHECKSUM_INVALID', -8);
/**
* Validate and process IBAN (international bank account numbers)
*
* @category Validate
* @package Validate_Finance_IBAN
* @author Stefan Neufeind <neufeind@speedpartner.de>
* @copyright 2005 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate
*/
class Validate_Finance_IBAN
{
/**
* String containing the IBAN to be processed
* @var string
* @access private
*/
var $_iban = '';
/**
* Integer containing errorcode of last validation
* @var integer
* @access private
*/
var $_errorcode = 0;
/**
* List of all IBAN countrycodes; also gives corresponding countrynames (in long form)
* @return array
* @access private
*/
function _getCountrycodeCountryname()
{
static $_iban_countrycode_countryname;
if (!isset($_iban_countrycode_countryname)) {
$_iban_countrycode_countryname =
array(
'AD' => 'Andorra',
'AT' => 'Austria',
'BA' => 'Bosnia and Herzegovina',
'BE' => 'Belgium',
'BG' => 'Bulgaria',
'CH' => 'Swiss',
'CS' => 'Serbia and Montenegro',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DE' => 'Germany',
'DK' => 'Denmark',
'EE' => 'Estonia',
'ES' => 'Spain',
'FR' => 'France',
'FI' => 'Finland',
'GB' => 'Great Britain',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'HR' => 'Croatia',
'HU' => 'Hungary',
'IE' => 'Ireland',
'IS' => 'Iceland',
'IT' => 'Italy',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'LV' => 'Latvia',
'MK' => 'Macedonia',
'MT' => 'Malta',
'NL' => 'The Netherlands',
'NO' => 'Norwegian',
'PL' => 'Poland',
'PT' => 'Portugal',
'RO' => 'Romania',
'SE' => 'Sweden',
'SI' => 'Slovenia',
'SK' => 'Slovak Republic',
'TN' => 'Tunisia',
'TR' => 'Turkey',
);
}
return $_iban_countrycode_countryname;
}
/**
* List of IBAN length; can be used for a quick check
* @return array
* @access private
*/
function _getCountrycodeIBANLength()
{
static $_iban_countrycode_length;
if (!isset($_iban_countrycode_length)) {
$_iban_countrycode_length =
array(
'AD' => 24,
'AT' => 20,
'BA' => 20,
'BE' => 16,
'BG' => 22,
'CH' => 21,
'CS' => 22,
'CY' => 28,
'CZ' => 24,
'DE' => 22,
'DK' => 18,
'EE' => 20,
'ES' => 24,
'FR' => 27,
'FI' => 18,
'GB' => 22,
'GI' => 23,
'GR' => 27,
'HR' => 21,
'HU' => 28,
'IE' => 22,
'IS' => 26,
'IT' => 27,
'LI' => 21,
'LT' => 20,
'LU' => 20,
'LV' => 21,
'MK' => 19,
'MT' => 31,
'NL' => 18,
'NO' => 15,
'PL' => 28,
'PT' => 25,
'RO' => 24,
'SE' => 24,
'SI' => 19,
'SK' => 24,
'TN' => 24,
'TR' => 26,
);
}
return $_iban_countrycode_length;
}
/**
* List of where the bankcode inside an IBAN starts (starting from 0) and its length
* @return array
* @access private
*/
function _getCountrycodeBankcode()
{
static $_iban_countrycode_bankcode;
if (!isset($_iban_countrycode_bankcode)) {
$_iban_countrycode_bankcode =
array(
'AD' => array('start' => 4, 'length' => 8), // first 4 chars bankcode, last 4 chars branch
'AT' => array('start' => 4, 'length' => 5),
'BA' => array('start' => 4, 'length' => 6), // first 3 chars bankcode, last 3 chars branch
'BE' => array('start' => 4, 'length' => 3),
'BG' => array('start' => 4, 'length' => 8),
'CH' => array('start' => 4, 'length' => 5),
'CS' => array('start' => 4, 'length' => 3),
'CY' => array('start' => 4, 'length' => 8), // first 3 chars bankcode, last 5 chars branch
'CZ' => array('start' => 4, 'length' => 4),
'DE' => array('start' => 4, 'length' => 8),
'DK' => array('start' => 4, 'length' => 4),
'EE' => array('start' => 4, 'length' => 4), // first 2 chars bankidentifier, last 2 chars bankcode
'ES' => array('start' => 4, 'length' => 8), // followed by 2 chars (checksum)
'FR' => array('start' => 4, 'length' => 10),
'FI' => array('start' => 4, 'length' => 6),
'GB' => array('start' => 4, 'length' => 10), // first 4 chars bankidentifier, last 6 chars bank-branchcode
'GI' => array('start' => 4, 'length' => 4),
'GR' => array('start' => 4, 'length' => 7), // first 3 chars bankcode, last 4 chars branch
'HR' => array('start' => 4, 'length' => 7),
'HU' => array('start' => 4, 'length' => 7), // first 3 chars bankcode, last 4 chars branch, followed by 1 char (checksum)
'IE' => array('start' => 4, 'length' => 10), // first 4 chars bankcode, last 6 chars branch
'IS' => array('start' => 4, 'length' => 4),
'IT' => array('start' => 4, 'length' => 11),
'LI' => array('start' => 4, 'length' => 5), // bankcode and branch
'LT' => array('start' => 4, 'length' => 5),
'LU' => array('start' => 4, 'length' => 3),
'LV' => array('start' => 4, 'length' => 4),
'MK' => array('start' => 4, 'length' => 3),
'MT' => array('start' => 4, 'length' => 9), // first 4 chars bankidentifier, last 5 chars bank sort code
'NL' => array('start' => 4, 'length' => 4),
'NO' => array('start' => 4, 'length' => 4),
'PL' => array('start' => 4, 'length' => 8),
'PT' => array('start' => 4, 'length' => 8),
'RO' => array('start' => 4, 'length' => 4),
'SE' => array('start' => 4, 'length' => 3), // bankcode and branch
'SI' => array('start' => 4, 'length' => 5),
'SK' => array('start' => 4, 'length' => 4),
'TN' => array('start' => 4, 'length' => 5), // first 2 chars bankcode, last 3 chars branch
'TR' => array('start' => 4, 'length' => 5), // followed by 1 char (reserved field)
);
}
return $_iban_countrycode_bankcode;
}
/**
* List of where the bankaccount-number inside an IBAN starts (starting from 0) and its length
* @return array
* @access private
*/
function _getCountrycodeBankaccount()
{
static $_iban_countrycode_bankaccount;
if (!isset($_iban_countrycode_bankaccount)) {
$_iban_countrycode_bankaccount =
array(
'AD' => array('start' => 12, 'length' => 12),
'AT' => array('start' => 9, 'length' => 11),
'BA' => array('start' => 10, 'length' => 8), // followed by 2 chars (checksum)
'BE' => array('start' => 7, 'length' => 7), // followed by 2 chars (checksum)
'BG' => array('start' => 12, 'length' => 10),
'CH' => array('start' => 9, 'length' => 12),
'CS' => array('start' => 7, 'length' => 13), // followed by 2 chars (checksum)
'CY' => array('start' => 12, 'length' => 16),
'CZ' => array('start' => 8, 'length' => 16),
'DE' => array('start' => 12, 'length' => 10),
'DK' => array('start' => 8, 'length' => 9), // followed by 1 char (checksum)
'EE' => array('start' => 8, 'length' => 11), // followed by 1 char (checksum)
'ES' => array('start' => 14, 'length' => 10),
'FR' => array('start' => 14, 'length' => 11), // followed by 2 chars (checksum)
'FI' => array('start' => 10, 'length' => 7), // followed by 1 char (checksum)
'GB' => array('start' => 14, 'length' => 8),
'GI' => array('start' => 8, 'length' => 15),
'GR' => array('start' => 11, 'length' => 16),
'HR' => array('start' => 11, 'length' => 10),
'HU' => array('start' => 12, 'length' => 15), // followed by 1 char (checksum)
'IE' => array('start' => 14, 'length' => 8),
'IS' => array('start' => 8, 'length' => 18), // 2 accounttype, 6 account number, 10 identification number
'IT' => array('start' => 15, 'length' => 12),
'LI' => array('start' => 9, 'length' => 12),
'LT' => array('start' => 9, 'length' => 11),
'LU' => array('start' => 7, 'length' => 13),
'LV' => array('start' => 8, 'length' => 13),
'MK' => array('start' => 7, 'length' => 10), // followed by 2 chars (checksum)
'MT' => array('start' => 13, 'length' => 18),
'NL' => array('start' => 8, 'length' => 10),
'NO' => array('start' => 8, 'length' => 6), // followed by 1 char (checksum)
'PL' => array('start' => 12, 'length' => 16),
'PT' => array('start' => 12, 'length' => 11), // followed by 2 chars (checksum)
'RO' => array('start' => 8, 'length' => 16), // branch and client account identifier
'SE' => array('start' => 7, 'length' => 16), // followed by 1 char (checksum)
'SI' => array('start' => 9, 'length' => 8), // followed by 2 chars (checksum)
'SK' => array('start' => 8, 'length' => 16),
'TN' => array('start' => 9, 'length' => 13), // followed by 2 chars (checksum)
'TR' => array('start' => 10, 'length' => 16),
);
}
return $_iban_countrycode_bankaccount;
}
/**
* List of regex for validating an IBAN according to standards for each country
* @return array
* @access private
*/
function _getCountrycodeRegex()
{
static $_iban_countrycode_regex;
if (!isset($_iban_countrycode_regex)) {
$_iban_countrycode_regex =
array(
'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/',
'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/',
'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/',
'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/',
'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$/',
'CH' => '/^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
'CS' => '/^CS[0-9]{2}[0-9]{3}[0-9]{15}$/',
'CY' => '/^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
'CZ' => '/^CZ[0-9]{2}[0-9]{4}[0-9]{16}$/',
'DE' => '/^DE[0-9]{2}[0-9]{8}[0-9]{10}$/',
'DK' => '/^DK[0-9]{2}[0-9]{4}[0-9]{10}$/',
'EE' => '/^EE[0-9]{2}[0-9]{4}[0-9]{12}$/',
'ES' => '/^ES[0-9]{2}[0-9]{8}[0-9]{12}$/',
'FR' => '/^FR[0-9]{2}[0-9]{10}[A-Z0-9]{13}$/',
'FI' => '/^FI[0-9]{2}[0-9]{6}[0-9]{8}$/',
'GB' => '/^GB[0-9]{2}[A-Z]{4}[0-9]{14}$/',
'GI' => '/^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$/',
'GR' => '/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/',
'HR' => '/^HR[0-9]{2}[0-9]{7}[0-9]{10}$/',
'HU' => '/^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$/',
'IE' => '/^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$/',
'IS' => '/^IS[0-9]{2}[0-9]{4}[0-9]{18}$/',
'IT' => '/^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$/',
'LI' => '/^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
'LU' => '/^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
'LT' => '/^LT[0-9]{2}[0-9]{5}[0-9]{11}$/',
'LV' => '/^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$/',
'MK' => '/^MK[0-9]{2}[A-Z]{3}[A-Z0-9]{10}[0-9]{2}$/',
'MT' => '/^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$/',
'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/',
'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/',
'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/',
'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/',
'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/',
'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/',
'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/',
'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/',
'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/',
'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/',
);
}
return $_iban_countrycode_regex;
}
/**
* Class constructor
* @param string $iban IBAN to be validated / processed
* @access public
*/
function __construct($iban = '')
{
$iban = strtoupper($iban);
$this->_iban = $iban;
} // end constructor
/**
* Returns the current IBAN
*
* @access public
* @return string
*/
function getIBAN()
{
return $this->_iban;
} // end func getIBAN
/**
* Sets the current IBAN to a new value
*
* @param string $iban IBAN to be validated / processed
* @access public
* @return void
*/
function setIBAN($iban = '')
{
$iban = strtoupper($iban);
$this->_iban = $iban;
} // end func setIBAN
/**
* Performs validation of the IBAN
*
* @param string $arg optional parameter for calling validate as a static function
* @access public
* @return boolean true if no error found
*/
function validate($arg = null)
{
if ( isset($this) && is_a($this, 'Validate_Finance_IBAN') ) {
$iban = $this->_iban;
} else {
$iban = $arg;
}
$errorcode=VALIDATE_FINANCE_IBAN_OK;
static $_iban_countrycode_countryname;
if (!isset($_iban_countrycode_countryname)) {
$_iban_countrycode_countryname = Validate_Finance_IBAN::_getCountrycodeCountryname();
}
static $_iban_countrycode_length;
if (!isset($_iban_countrycode_length)) {
$_iban_countrycode_ibanlength = Validate_Finance_IBAN::_getCountrycodeIBANLength();
}
static $_iban_countrycode_regex;
if (!isset($_iban_countrycode_regex)) {
$_iban_countrycode_regex = Validate_Finance_IBAN::_getCountrycodeRegex();
}
if (strlen($iban) <= 4) {
$errorcode = VALIDATE_FINANCE_IBAN_TOO_SHORT;
} elseif (!isset( $_iban_countrycode_countryname[ substr($iban,0,2) ] )) {
$errorcode = VALIDATE_FINANCE_IBAN_COUNTRY_INVALID;
} elseif (strlen($iban) < $_iban_countrycode_ibanlength[ substr($iban,0,2) ]) {
$errorcode = VALIDATE_FINANCE_IBAN_TOO_SHORT;
} elseif (strlen($iban) > $_iban_countrycode_ibanlength[ substr($iban,0,2) ]) {
$errorcode = VALIDATE_FINANCE_IBAN_TOO_LONG;
} elseif (!preg_match($_iban_countrycode_regex[ substr($iban,0,2) ],$iban)) {
$errorcode = VALIDATE_FINANCE_IBAN_INVALID_FORMAT;
} else {
// todo: maybe implement direct checks for bankcodes of certain countries
// let's see if checksum is also correct
$iban_replace_chars = range('A','Z');
foreach (range(10,35) as $tempvalue) {
$iban_replace_values[]=strval($tempvalue);
}
// move first 4 chars (countrycode and checksum) to the end of the string
$tempiban = substr($iban, 4).substr($iban, 0, 4);
$tempiban = str_replace($iban_replace_chars, $iban_replace_values, $tempiban);
$tempcheckvalue = intval(substr($tempiban, 0, 1));
for ($strcounter = 1; $strcounter < strlen($tempiban); $strcounter++) {
$tempcheckvalue *= 10;
$tempcheckvalue += intval(substr($tempiban,$strcounter,1));
$tempcheckvalue %= 97;
}
// checkvalue of 1 indicates correct IBAN checksum
if ($tempcheckvalue != 1) {
$errorcode=VALIDATE_FINANCE_IBAN_CHECKSUM_INVALID;
} else {
$errorcode=VALIDATE_FINANCE_IBAN_OK;
}
}
if (isset($this)) {
$this->_errorcode=$errorcode;
}
return ($errorcode == VALIDATE_FINANCE_IBAN_OK);
} // end func validate
/**
* Returns errorcode corresponding to last validation
*
* @access public
* @return integer errorcode
*/
function getErrorcode()
{
return $this->_errorcode;
} // end func getErrorcode
/**
* Returns the countrycode of the IBAN
*
* @access public
* @return string
*/
function getCountrycode()
{
if (strlen($this->_iban)>4) {
// return first two characters
return substr($this->_iban,0,2);
} else {
$this->_errorcode = VALIDATE_FINANCE_IBAN_TOO_SHORT;
return PEAR::raiseError($this->errorMessage($this->_errorcode), $this->_errorcode, PEAR_ERROR_TRIGGER, E_USER_WARNING, $this->errorMessage($this->_errorcode)." in VALIDATE_FINANCE_IBAN::getCountrycode()");
}
} // end func getCountrycode
/**
* Returns the countryname of the IBAN
*
* @access public
* @return string
*/
function getCountryname()
{
$countrycode = $this->getCountrycode();
if (is_string($countrycode)) {
$_iban_countrycode_countryname = Validate_Finance_IBAN::_getCountrycodeCountryname();
return $_iban_countrycode_countryname[$countrycode];
} else { // e.g. if it's an error
return $countrycode;
}
} // end func getCountryname
/**
* Returns the bankcode of the IBAN
*
* @access public
* @return string
*/
function getBankcode()
{
if (!$this->validate()) {
$this->_errorcode = VALIDATE_FINANCE_IBAN_GENERAL_INVALID;
return PEAR::raiseError($this->errorMessage($this->_errorcode), $this->_errorcode, PEAR_ERROR_TRIGGER, E_USER_WARNING, $this->errorMessage($this->_errorcode)." in VALIDATE_FINANCE_IBAN::getBankcode()");
} else {
$_iban_countrycode_bankcode = Validate_Finance_IBAN::_getCountrycodeBankcode();
$currCountrycodeBankcode = $_iban_countrycode_bankcode[ substr($this->_iban,0,2) ];
return substr($this->_iban, $currCountrycodeBankcode['start'], $currCountrycodeBankcode['length']);
}
} // end func getBankcode
/**
* Returns the bankaccount of the IBAN
*
* @access public
* @return string
*/
function getBankaccount()
{
if (!$this->validate()) {
$this->_errorcode = VALIDATE_FINANCE_IBAN_GENERAL_INVALID;
return PEAR::raiseError($this->errorMessage($this->_errorcode), $this->_errorcode, PEAR_ERROR_TRIGGER, E_USER_WARNING, $this->errorMessage($this->_errorcode)." in VALIDATE_FINANCE_IBAN::getBankaccount()");
} else {
$_iban_countrycode_bankaccount = Validate_Finance_IBAN::_getCountrycodeBankaccount();
$currCountrycodeBankaccount = $_iban_countrycode_bankaccount[ substr($iban,0,2) ];
return substr($this->_iban, $currCountrycodeBankaccount['start'], $currCountrycodeBankaccount['length']);
}
} // end func getAccount
/**
* Return a textual error message for an IBAN error code
*
* @access public
* @param int error code
* @return string error message
*/
function errorMessage($value)
{
// make the variable static so that it only has to do the defining on the first call
static $errorMessages;
// define the varies error messages
if (!isset($errorMessages)) {
$errorMessages = array(
VALIDATE_FINANCE_IBAN_OK => 'no error',
VALIDATE_FINANCE_IBAN_ERROR => 'unknown error',
VALIDATE_FINANCE_IBAN_GENERAL_INVALID => 'IBAN generally invalid',
VALIDATE_FINANCE_IBAN_TOO_SHORT => 'IBAN is too short',
VALIDATE_FINANCE_IBAN_TOO_LONG => 'IBAN is too long',
VALIDATE_FINANCE_IBAN_COUNTRY_INVALID => 'IBAN countrycode is invalid',
VALIDATE_FINANCE_IBAN_INVALID_FORMAT => 'IBAN has invalid format',
VALIDATE_FINANCE_IBAN_CHECKSUM_INVALID => 'IBAN checksum is invalid'
);
}
// If this is an error object, then grab the corresponding error code
if (VALIDATE_FINANCE_IBAN::isError($value)) {
$value = $value->getCode();
}
// return the textual error message corresponding to the code
return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[VALIDATE_FINANCE_IBAN_ERROR];
} // end func errorMessage
} // end class VALIDATE_FINANCE_IBAN
?>
+581
View File
@@ -0,0 +1,581 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* Specific validation methods for data used in the United States
*
* PHP Versions 4 and 5
*
* This source file is subject to the New BSD license, That is bundled
* with this package in the file LICENSE, and is available through
* the world-wide-web at
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the new BSDlicense and are unable
* to obtain it through the world-wide-web, please send a note to
* pajoye@php.net so we can mail you a copy immediately.
*
* @category Validate
* @package Validate_US
* @author Brent Cook <busterbcook@yahoo.com>
* @author Tim Gallagher <timg@sunflowerroad.com>
* @copyright 1997-2005 Brent Cook
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version CVS: $Id$
* @link http://pear.php.net/package/Validate_US
*/
/**
* Data validation class for the United States
*
* This class provides methods to validate:
* - Social insurance number (aka SSN)
* - Region (state code)
* - Postal code
* - Telephone number
*
* @category Validate
* @package Validate_US
* @author Brent Cook <busterbcook@yahoo.com>
* @author Tim Gallagher <timg@sunflowerroad.com>
* @copyright 1997-2005 Brent Cook
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_US
*/
class Validate_US
{
/**
* Validates a social security number
*
* @param string $ssn number to validate
* @param array $high_groups array of highest issued SSN group numbers
*
* @return bool
*/
function ssn($ssn, $high_groups = null)
{
// remove any dashes, spaces, returns, tabs or slashes
$ssn = str_replace(array('-','/',' ',"\t","\n"), '', $ssn);
// check if this is a 9-digit number
if (!is_numeric($ssn) || strlen($ssn) != 9) {
return false;
}
$area = substr($ssn, 0, 3);
$group = intval(substr($ssn, 3, 2));
$serial = intval(substr($ssn, 5, 4));
if (!$high_groups) {
$high_groups = Validate_US::ssnGetHighGroups();
}
return Validate_US::ssnCheck($area, $group, $serial, $high_groups);
}
/**
* Returns a range for a supplied group number, which
* is the middle, two-digit field of a SSN.
* Group numbers are defined as follows:
* 1 - Odd numbers, 01 to 09
* 2 - Even numbers, 10 to 98
* 3 - Even numbers, 02 to 08
* 4 - Odd numbers, 11 to 99
*
* @param int $groupNumber a group number to check, 00-99
*
* @return int
*/
function ssnGroupRange($groupNumber)
{
if (is_array($groupNumber)) {
extract($groupNumber);
}
if ($groupNumber < 10) {
// is the number odd?
if ($groupNumber % 2) {
return 1;
} else {
return 3;
}
} else {
// is the number odd?
if ($groupNumber % 2) {
return 4;
} else {
return 2;
}
}
}
/**
* checks if a Social Security Number is valid
* needs the first three digits and first two digits and the
* final four digits as separate integer parameters
*
* @param int $area 3-digit group in a SSN
* @param int $group 2-digit group in a SSN
* @param int $serial 4-digit group in a SSN
* @param array $high_groups array of highest issued group numbers
* area number=>group number
*
* @return bool true if valid
*/
function ssnCheck($area, $group, $serial, $high_groups)
{
if (is_array($area)) {
extract($area);
}
// perform trivial checks
// no field should contain all zeros
if (!($area && $group && $serial)) {
return false;
}
// check if this area has been assigned yet
if (!isset($high_groups[$area])) {
return false;
}
$high_group = $high_groups[$area];
$high_group_range = Validate_US::ssnGroupRange($high_group);
$group_range = Validate_US::ssnGroupRange($group);
// if the assigned range is higher than this group number, we're OK
if ($high_group_range > $group_range) {
return true;
} elseif ($high_group_range < $group_range) {
// if the assigned range is lower than the group number, that's bad
return false;
} elseif ($high_group >= $group) {
// we must be in the same range, check the actual numbers
return true;
}
return false;
}
/**
* Gets the most current list the highest SSN group numbers issued
* from the Social Security Administration website. This info can be
* cached for performance (and to lessen the load on the SSA website)
*
* @param string $uri Path to the SSA highgroup.txt file
* @param bool $is_text Take the $uri param as directly the contents
*
* @return array
*/
function ssnGetHighGroups($uri = 'http://www.ssa.gov/employer/highgroup.txt',
$is_text = false)
{
/**
* Stores high groups that have been fetched from any given web page to
* keep the load down if having to validate more then one ssn in a row
*/
static $high_groups = array();
static $lastUri = '';
if ($lastUri == $uri && !empty($high_groups)) {
return $high_groups;
}
$lastUri = $uri;
if ($is_text) {
$source = $uri;
} else {
if (!$fd = @fopen($uri, 'r')) {
$lastUri = '';
trigger_error('Could not access the SSA High Groups file',
E_USER_WARNING);
return array();
}
$source = '';
while ($data = fread($fd, 2048)) {
$source .= $data;
}
fclose($fd);
}
$lines = explode("\n", preg_replace("/[^\n0-9]/", '', $source));
$high_groups = array();
foreach ($lines as $line) {
if (preg_match('/^[0-9]+$/', $line) && !(($len = strlen($line)) % 5)) {
for ($x=0; $x<$len; $x+=5) {
$index = substr($line, $x, 3);
$value = substr($line, $x+3, 2);
$high_groups[$index] = $value;
}
}
}
return $high_groups;
}
/**
* Validates a US Postal Code format (ZIP code)
*
* @param string $postalCode the ZIP code to validate
* @param bool $strong optional; strong checks (e.g. against a list
* of postcodes) (not implanted)
*
* @return boolean TRUE if code is valid, FALSE otherwise
* @access public
* @static
* @todo Integrate with USPS web API
*/
function postalCode($postalCode, $strong = false)
{
return (bool)preg_match('/^[0-9]{5}((-| )[0-9]{4})?$/', $postalCode);
}
/**
* Validates a US ZIP code by region (i.e. state)
*
* Note: Some ZIP codes overlap between states. Do not use this data for
* reverse lookup of states.
*
* @param string $postalCode the ZIP code to validate.
* @param stirng $region the 2-letter region code of the state.
*
* @return boolean true if the ZIP code is valid for the specified region
* code, false otherwise.
*
* @access public
* @static
*/
function postalCodeByRegion($postalCode, $region)
{
/*
* Start and end ZIP codes by state taken from Wikipedia:
* http://en.wikipedia.org/wiki/Image:ZIP_code_zones.png
* and
* http://en.wikipedia.org/wiki/List_of_ZIP_Codes_in_the_United_States
*
* NOTE: Some codes overlap. Do not use this for reverse lookup of
* states.
*/
switch ($region) {
case 'PW': // Palau
case 'FM': // Micronesia
case 'MH': // Marshall Islands
case 'MP': // North Marina Islands
case 'GU': // Guam
$ranges = array('969' => '969');
break;
case 'AS': // American Samoa
$ranges = array('96799' => '96799');
break;
case 'AP': // American Forces (Pacific)
$ranges = array('962' => '966');
break;
case 'WA': // Washington
$ranges = array('980' => '994');
break;
case 'OR': // Oregon
$ranges = array('97' => '97');
break;
case 'HI': // Hawii
$ranges = array('967' => '968');
break;
case 'CA': // California
$ranges = array('900' => '961');
break;
case 'AK': // Alaska
$ranges = array('995' => '999');
break;
case 'WY': // Wyoming
$ranges = array('820' => '831', '83414' => '83414');
break;
case 'UT': // Utah
$ranges = array('84' => '84');
break;
case 'NM': // New Mexico
$ranges = array('870' => '884');
break;
case 'NV': // Nevada
$ranges = array('889' => '899');
break;
case 'ID': // Idaho
$ranges = array('832' => '839');
break;
case 'CO': // Colorado
$ranges = array('80' => '81');
break;
case 'AZ': // Arizona
$ranges = array('85' => '86');
break;
case 'TX': // Texas
$ranges = array('75' => '79', '885' => '885', '73301' => '73301',
'73344' => '73344');
break;
case 'OK': // Oklahoma
$ranges = array('73' => '74');
break;
case 'LA': // Louisiana
$ranges = array('700' => '715');
break;
case 'AR': // Arkansas
$ranges = array('716' => '729');
break;
case 'NE': // Nebraska
$ranges = array('68' => '69');
break;
case 'MO': // Missouri
$ranges = array('63' => '65');
break;
case 'KS': // Kansas
$ranges = array('66' => '67');
break;
case 'IL': // Illinois
$ranges = array('60' => '62');
break;
case 'WI': // Wisconsin
$ranges = array('53' => '54');
break;
case 'SD': // South Dakota
$ranges = array('57' => '57');
break;
case 'ND': // North Dakota
$ranges = array('58' => '58');
break;
case 'MT': // Montana
$ranges = array('59' => '59');
break;
case 'MN': // Minnesota
$ranges = array('550' => '567');
break;
case 'IA': // Iowa
$ranges = array('50' => '52');
break;
case 'OH': // Ohio
$ranges = array('43' => '45');
break;
case 'MI': // Michigan
$ranges = array('48' => '49');
break;
case 'KY': // Kentucky
$ranges = array('400' => '427');
break;
case 'IN': // Indiana
$ranges = array('46' => '47');
break;
case 'AA': // American Forces (Central and South America)
$ranges = array('340' => '340');
break;
case 'TN': // Tennessee
$ranges = array('370' => '385');
break;
case 'MS': // Mississippi
$ranges = array('386' => '397');
break;
case 'GA': // Georgia
$ranges = array('30' => '31', '398' => '398', '39901' => '39901');
break;
case 'FL': // Flordia
$ranges = array('32' => '34');
break;
case 'AL': // Alabama
$ranges = array('35' => '36');
break;
case 'WV': // West Virginia
$ranges = array('247' => '269');
break;
case 'VA': // Virginia (partially overlaps with DC)
$ranges = array('220' => '246', '200' => '201');
break;
case 'SC': // South Carolina
$ranges = array('29' => '29');
break;
case 'NC': // North Carolina
$ranges = array('27' => '28');
break;
case 'MD': // Maryland
$ranges = array('206' => '219');
break;
case 'DC': // District of Columbia
$ranges = array('200' => '200', '202' => '205', '569' => '569');
break;
case 'PA': // Pennsylvania
$ranges = array('150' => '196');
break;
case 'NY': // New York
$ranges = array('10' => '14', '06390' => '06390',
'00501' => '00501', '00544' => '00544');
break;
case 'DE': // Delaware
$ranges = array('197' => '199');
break;
case 'VI': // Virgin Islands
$ranges = array('008' => '008');
break;
case 'PR': // Puerto Rico
$ranges = array('006' => '007', '009' => '009');
break;
case 'AE': // American Forces (Europe)
$ranges = array('09' => '09');
break;
case 'VT': // Vermont
$ranges = array('05' => '05');
break;
case 'RI': // Rhode Island
$ranges = array('028' => '029');
break;
case 'NJ': // New Jersey
$ranges = array('07' => '08');
break;
case 'NH': // New Hampshire
$ranges = array('030' => '038');
break;
case 'MA': // Massachusetts
$ranges = array('010' => '027', '05501' => '05501',
'05544' => '05544');
break;
case 'ME': // Maine
$ranges = array('039' => '049');
break;
case 'CT': // Connecticut
$ranges = array('06' => '06');
break;
case 'UM': // U.S. Minor Outlying Islands
default: // Not Found
$ranges = array('' => '');
break;
}
// truncate code if longer than 5 characters
if (strlen($postalCode) > 5) {
$postalCode = substr($postalCode, 0, 5);
}
// prepend code with zeros if shorter than 5 characters
if (strlen($postalCode) < 5) {
$postalCode = str_repeat('0', 5 - strlen($postalCode)).$postalCode;
}
// is code between some start and end range?
$valid = false;
foreach ($ranges as $start => $end) {
$zip_start = substr($postalCode, 0, strlen($start));
if ((integer)$zip_start >= (integer)$start &&
(integer)$zip_start <= (integer)$end) {
$valid = true;
break;
}
}
return $valid;
}
/**
* Validates a "region" (i.e. state) code
*
* @param string $region 2-letter state code
*
* @return bool Whether the code is a valid state
* @static
*/
function region($region)
{
switch (strtoupper($region)) {
case 'AL':
case 'AK':
case 'AZ':
case 'AR':
case 'CA':
case 'CO':
case 'CT':
case 'DE':
case 'DC':
case 'FL':
case 'GA':
case 'HI':
case 'ID':
case 'IL':
case 'IN':
case 'IA':
case 'KS':
case 'KY':
case 'LA':
case 'ME':
case 'MD':
case 'MA':
case 'MI':
case 'MN':
case 'MS':
case 'MO':
case 'MT':
case 'NE':
case 'NV':
case 'NH':
case 'NJ':
case 'NM':
case 'NY':
case 'NC':
case 'ND':
case 'OH':
case 'OK':
case 'OR':
case 'PA':
case 'RI':
case 'SC':
case 'SD':
case 'TN':
case 'TX':
case 'UT':
case 'VT':
case 'VA':
case 'WA':
case 'WV':
case 'WI':
case 'WY':
return true;
}
return false;
}
/**
* Validate a US phone number.
*
* Allowed formats
* <ul>
* <li>xxxxxxx <-> 7 digits format</li>
* <li>(xxx) xxx-xxxx <-> area code with brackets around it (or not) +
* phone number with dash or not </li>
* <li>xxx xxx-xxxx <-> area code + number +- dash/space + 4 digits</li>
* <li>(1|0) xxx xxx-xxxx <-> 1 or 0 + area code + 3 digits +- dash/space
* + 4 digits</li>
* <li>xxxxxxxxxx <-> 10 digits</li>
* </ul>
*
* or various combination without spaces or dashes.
* THIS SHOULD EVENTUALLY take a FORMAT in the options, instead
*
* @param string $number phone to validate
* @param bool $requireAreaCode require the area code? (default: true)
*
* @return bool The valid or invalid phone number
* @access public
*/
function phoneNumber($number, $requireAreaCode = true)
{
if (strlen(trim($number)) <= 6) {
return false;
}
if (!$requireAreaCode) {
// just seven digits, maybe a space or dash
if (preg_match('/^[2-9]\d{2}[- ]?\d{4}$/', $number)) {
return true;
}
} else {
// ten digits, maybe spaces and/or dashes and/or parentheses
// maybe a 1 or a 0...
$reg = '/^[0-1]?[- ]?(\()?[2-9]\d{2}(?(1)\))[- ]?[2-9]\d{2}[- ]?\d{4}$/';
if (preg_match($reg,
$number)) {
return true;
}
}
return false;
}
}