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