Skip to content

Commit

Permalink
bump copyright year, moving CreditCard functions into it's own file/s…
Browse files Browse the repository at this point in the history
…ubpackage, add package_*.xml for all new sub package that will be splitted from the main Validate package, removed contributors from package.xml since they're now leads on the country packages that they contributed to, updated changelog, removed files from package.xml which are now moved into their own sub packages.

git-svn-id: http://svn.php.net/repository/pear/packages/Validate/trunk@176872 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
helgi committed Jan 10, 2005
1 parent 0969dec commit 375850e
Show file tree
Hide file tree
Showing 15 changed files with 771 additions and 176 deletions.
126 changes: 3 additions & 123 deletions Validate.php
Expand Up @@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// | Copyright (c) 1997-2005 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
Expand Down Expand Up @@ -182,126 +182,6 @@ function uri($url, $options = null)
return false;
}

/**
* Validate 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
* @author Ondrej Jombik <nepto@pobox.sk>
*/
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;
}


/**
* Validate a number according to Luhn check algorithm
*
* If a type is passed, the number will be checked against it.
*
* @param string $creditCard number (only numeric chars will be considered)
* @param string $cardType card type ('visa', 'mastercard'...).
* Case-insensitive
* @return bool TRUE if number is valid, FALSE otherwise
* @access public
* @see luhn()
* @author Ondrej Jombik <nepto@pobox.sk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
*/
function creditCard($creditCard, $cardType = null)
{
$creditCard = preg_replace('/[^0-9]/', '', $creditCard);
if (empty($creditCard) || ($len_number = strlen($creditCard)) <= 0) {
return false;
}

// Only apply the Luhn algorithm for cards other than enRoute
// So check if we have a enRoute card now
if (strlen($creditCard) != 15
|| (substr($creditCard, 0, 4) != '2014'
&& substr($creditCard, 0, 4) != '2149')) {

if (!Validate::Luhn($creditCard)) {
return false;
}
}

if (!is_null($cardType)) {
return Validate::creditCardType($creditCard, $cardType);
}

return true;
}


/**
* Validates the credit card number against a type
*
* @param string $creditCard credit card number to check
* @param string $cardType Case-insensitive type (no spaces)
* @return boolean TRUE is type matches, FALSE otherwise
* @access public
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @link http://www.beachnet.com/~hstiles/cardtype.html
*/
function creditCardType($creditCard, $cardType) {

switch (strtoupper($cardType)) {
case 'MASTERCARD':
$regex = '/^5[1-5]\d{14}$/';
break;
case 'VISA':
$regex = '/^4(\d{12}|\d{15})$/';
break;
case 'AMEX':
case 'AMERICANEXPRESS':
$regex = '/^3[47]\d{13}$/';
break;
case 'DINERS':
case 'DINERSCLUB':
case 'CARTEBLANCHE':
$regex = '/^3(0[0-5]\d{11}|[68]\d{12})$/';
break;
case 'DISCOVER':
$regex = '/^6011\d{12}$/';
break;
case 'JCB':
$regex = '/^(3\d{15}|(2131|1800)\d{11})$/';
break;
case 'ENROUTE':
$regex = '/^2(014|149)\d{11}$/';
break;
default:
return false;
}

$creditCard = preg_replace('/[^0-9]/', '', $creditCard);
return (bool) preg_match($regex, $creditCard);
}


/**
* Validate date and times. Note that this method need the Date_Calc class
*
Expand Down Expand Up @@ -808,7 +688,7 @@ function multiple(&$data, &$val_type, $remove = false)
* "<class name><underscore><method name>"
* Ex: us_ssn will include class Validate/US.php and call method ssn()
*/
} elseif (strpos($opt['type'],'_') !== false) {
} elseif (strpos($opt['type'], '_') !== false) {
list($class, $method) = explode('_', $opt['type'], 2);
@include_once "Validate/$class.php";
if (!class_exists("Validate_$class") ||
Expand All @@ -817,7 +697,7 @@ function multiple(&$data, &$val_type, $remove = false)
trigger_error("Invalid validation type Validate_$class::$method", E_USER_WARNING);
continue;
}
$opt = array_slice($opt,1);
$opt = array_slice($opt, 1);
if (sizeof($opt) == 1) {
$opt = array_pop($opt);
}
Expand Down
143 changes: 143 additions & 0 deletions Validate/CreditCard.php
@@ -0,0 +1,143 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Ondrej Jombik <nepto@pobox.sk> |
// | Philippe Jausions <Philippe.Jausions@11abacus.com> |
// +----------------------------------------------------------------------+
//
// $Id$

class Validate_CreditCard
{
/**
* Validate 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
* @author Ondrej Jombik <nepto@pobox.sk>
*/
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;
}


/**
* Validate a number according to Luhn check algorithm
*
* If a type is passed, the number will be checked against it.
*
* @param string $creditCard number (only numeric chars will be considered)
* @param string $cardType card type ('visa', 'mastercard'...).
* Case-insensitive
* @return bool TRUE if number is valid, FALSE otherwise
* @access public
* @see luhn()
* @author Ondrej Jombik <nepto@pobox.sk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
*/
function creditCard($creditCard, $cardType = null)
{
$creditCard = preg_replace('/[^0-9]/', '', $creditCard);
if (empty($creditCard) || ($len_number = strlen($creditCard)) <= 0) {
return false;
}

// Only apply the Luhn algorithm for cards other than enRoute
// So check if we have a enRoute card now
if (strlen($creditCard) != 15
|| (substr($creditCard, 0, 4) != '2014'
&& substr($creditCard, 0, 4) != '2149')) {

if (!Validate_CreditCard::Luhn($creditCard)) {
return false;
}
}

if (!is_null($cardType)) {
return Validate_CreditCard::creditCardType($creditCard, $cardType);
}

return true;
}


/**
* Validates the credit card number against a type
*
* @param string $creditCard credit card number to check
* @param string $cardType Case-insensitive type (no spaces)
* @return boolean TRUE is type matches, FALSE otherwise
* @access public
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @link http://www.beachnet.com/~hstiles/cardtype.html
*/
function creditCardType($creditCard, $cardType) {

switch (strtoupper($cardType)) {
case 'MASTERCARD':
$regex = '/^5[1-5]\d{14}$/';
break;
case 'VISA':
$regex = '/^4(\d{12}|\d{15})$/';
break;
case 'AMEX':
case 'AMERICANEXPRESS':
$regex = '/^3[47]\d{13}$/';
break;
case 'DINERS':
case 'DINERSCLUB':
case 'CARTEBLANCHE':
$regex = '/^3(0[0-5]\d{11}|[68]\d{12})$/';
break;
case 'DISCOVER':
$regex = '/^6011\d{12}$/';
break;
case 'JCB':
$regex = '/^(3\d{15}|(2131|1800)\d{11})$/';
break;
case 'ENROUTE':
$regex = '/^2(014|149)\d{11}$/';
break;
default:
return false;
}

$creditCard = preg_replace('/[^0-9]/', '', $creditCard);
return (bool)preg_match($regex, $creditCard);
}
}
?>
58 changes: 5 additions & 53 deletions package.xml
Expand Up @@ -7,11 +7,8 @@
* email (syntax, domain check)
* string (predifined type alpha upper and/or lowercase, numeric,...)
* date (min, max)
* Credit cards
* uri (RFC2396)
* possibility valid multiple data with a single method call (::multiple)
* Locale validation for AT, CH, DE, ES, FR, NL, PL, ptBR, UK, US
* Finance (e.g. IBAN)
</description>
<license>PHP</license>
<maintainers>
Expand All @@ -33,24 +30,6 @@
<name>Stefan Neufeind</name>
<email>pear.neufeind@speedpartner.de</email>
</maintainer>
<maintainer>
<user>timmyg</user>
<role>contributor</role>
<name>Tim Gallagher</name>
<email>timg@sunflowerroad.com</email>
</maintainer>
<maintainer>
<user>busterb</user>
<role>contributor</role>
<name>Brent Cook</name>
<email>busterb@mail.utexas.edu</email>
</maintainer>
<maintainer>
<user>zyprexia</user>
<role>contributor</role>
<name>Dave Mertens</name>
<email>zyprexia@php.net</email>
</maintainer>
<maintainer>
<user>dufuz</user>
<role>developer</role>
Expand All @@ -60,56 +39,29 @@
</maintainers>
<release>

<version>0.4.2</version>
<version>0.5</version>
<state>alpha</state>
<date>2004-03-17</date>
<notes>
- Bug #2936, typo
- Better email validation
- CS fixes
- BC break!
Country validation unified, now all postcode validation funcs are named postalCode
- region() added in US (that is validation of the states) and FR (department)
- Splitted country classes and finance into their own sub packages of Validate
- New developer (Helgi)
- Request #2194 Improved CreditCard validation with CreditCard Type check, patch by Philippe
- Few more none alpha chars added
</notes>
<filelist>
<dir name="/">
<file role="php">Validate.php</file>
<dir name="Validate">
<file role="php">Finance.php</file>
<dir name="Finance">
<file role="php">IBAN.php</file>
</dir>
<file role="php" name="AT.php">
<replace type="pear-config" from="@DATADIR@" to="data_dir" />
</file>
<file role="php" name="CH.php">
<replace type="pear-config" from="@DATADIR@" to="data_dir" />
</file>
<file role="php">DE.php</file>
<file role="php">ES.php</file>
<file role="php">FR.php</file>
<file role="php">NL.php</file>
<file role="php">PL.php</file>
<file role="php">ptBR.php</file>
<file role="php">UK.php</file>
<file role="php">US.php</file>
</dir>
<dir name="tests" role="test">
<file>number.php</file>
<file>date.php</file>
<file>credit_card.php</file>
<file>validate_AT.php</file>
<file>validate_CH.php</file>
<file>validate_DE.php</file>
<file>validate_NL.php</file>
<file>validate_UK.php</file>
<file>validate_Finance.php</file>
</dir>
<dir name="docs" role="doc">
<file>sample_multiple.php</file>
</dir>
<file role="data" name="data/AT_postcodes.txt" install-as="AT_postcodes.txt" />
<file role="data" name="data/CH_postcodes.txt" install-as="CH_postcodes.txt" />
</dir>
</filelist>
<deps>
Expand Down

0 comments on commit 375850e

Please sign in to comment.