Go to the documentation of this file.00001 <?php
00002
00007 class oxCcValidator
00008 {
00014 protected $_aCardsInfo = array(
00015 "amx" => '/^3[47].{13}$/',
00016 "dlt" => '/^4.{15}$/',
00017 "dnc" => '/^30[0-5].{11}$|^3[68].{12}$/',
00018 "dsc" => '/^6011.{12}$/',
00019 "enr" => '/^2014.{11}$|^2149.{11}$/',
00020 "jcb" => '/^3.{15}$|^2131|1800.{11}$/',
00021 "mcd" => '/^5[1-5].{14}$/',
00022 "swi" => '/^[456].{15}$|^[456].{17,18}$/',
00023 "vis" => '/^4.{15}$|^4.{12}$/',
00024 );
00025
00034 protected function _isValidType( $sType, $sNumber )
00035 {
00036 $blValid = true;
00037
00038
00039 if ( isset( $this->_aCardsInfo[$sType] ) ) {
00040 $blValid = preg_match( $this->_aCardsInfo[$sType], $sNumber );
00041 }
00042 return $blValid;
00043 }
00044
00052 protected function _isExpired( $sDate )
00053 {
00054 $blExpired = false;
00055
00056 if ( $sDate ) {
00057 $sYears = substr( $sDate, 2, 2 );
00058 $sMonth = substr( $sDate, 0, 2 );
00059 $sDay = date( "t", mktime( 11, 59, 59, $sMonth, 1, $sYears ) );
00060
00061 $iExpDate = mktime( 23, 59, 59, $sMonth, $sDay, $sYears );
00062 if ( time() > $iExpDate ) {
00063 $blExpired = true;
00064 }
00065 }
00066
00067 return $blExpired;
00068 }
00069
00077 protected function _isValidNumer( $sNumber )
00078 {
00079 $blValid = false;
00080 if ( ( $iLength = strlen( $sNumber ) ) ) {
00081 $iModSum = 0;
00082 $iMod = $iLength % 2;
00083
00084
00085 for ( $iPos = 0; $iPos < $iLength; $iPos++ ) {
00086
00087
00088 $iCurrDigit = ( int ) $sNumber{$iPos};
00089
00090
00091 $iAddValue = ( ( $iPos % 2 == $iMod ) ? 2 : 1 ) * $iCurrDigit;
00092
00093
00094 $iModSum += ( $iAddValue > 9 ) ? $iAddValue - 9 : $iAddValue;
00095 }
00096
00097 $blValid = ( $iModSum % 10 ) == 0;
00098 }
00099 return $blValid;
00100 }
00101
00111 public function isValidCard( $sNumber, $sType = "", $sDate = "" )
00112 {
00113
00114 $sNumber = preg_replace( "/[^0-9]/", "", $sNumber );
00115 return ( !$this->_isExpired( $sDate ) && $this->_isValidType( $sType, $sNumber ) && $this->_isValidNumer( $sNumber ) );
00116 }
00117 }