Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 - 1
<?php
2
/**
3
 * Bar codes checker - Checks if a barcode can be valid en returns type of barcode
4
 *
5
 * @link		http://www.phpclasses.org/package/8560-PHP-Detect-type-and-check-EAN-and-UPC-barcodes.html
6
 * @author		Ferry Bouwhuis
7
 * @version		1.0.1
8
 * @LastChange	2014-04-13
9
 */
10
 
11
class clsLibGTIN
12
{
13
	public static function GTINCheck($p_sGTINCode, $p_bReturnGTIN14 = FALSE, $p_getType = 0)
14
	{
15
		//Setting return value
16
		/*
17
		 * If TRUE it will retun al barcodes as 14 digit strings
18
		 * If FALSE it will return only what is needed UPC -> 12 / EAN -> 13 /
19
		 */
20
		$bReturnGTIN14 = $p_bReturnGTIN14;
21
 
22
		//Filter UPC coupon codes
23
		/*
24
		 * If TRUE it will return false on UPC coupon codes
25
		 * Type will always return UPC coupon code
26
		 */
27
		$bSkipCouponCodes = true;
28
 
29
		//Trims parsed string to remove unwanted whitespace or characters
30
		$p_sGTINCode = (string)trim($p_sGTINCode);
31
		if(preg_match('/[^0-9]/', $p_sGTINCode))
32
			return false;
33
 
34
		if(!is_string($p_sGTINCode)){
35
			$p_sGTINCode = strval($p_sGTINCode);
36
		}
37
		$sGTINCode = trim($p_sGTINCode);
38
		$p_sGTINCode = null;
39
		$length = strlen($sGTINCode);
40
		if(($length > 11 && $length <= 14) || $length == 8){
41
			$zerros = 18-$length;
42
			$length = null;
43
			$fill = "";
44
			for($i = 0; $i < $zerros; $i++){
45
				$fill .= "0";
46
			}
47
			$zerros = null;
48
			$i = null;
49
			$sGTINCode = $fill . $sGTINCode;
50
			$fill = null;
51
			if(!clsLibGTIN::GTINCheckDigit($sGTINCode))
52
	     	{
53
				return FALSE;
54
			}elseif(substr($sGTINCode,5,1) > 2){
55
				//EAN / JAN / EAN-13 code
56
				if($p_getType){
57
					return 'EAN';
58
				}else{
59
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -13));
60
				}
61
			}elseif(substr($sGTINCode,6,1) == 0 && substr($sGTINCode,0,10) == 0){
62
				//EAN-8 / GTIN-8 code
63
				if($p_getType){
64
					return 'EAN-8';
65
				}else{
66
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -8));
67
				}
68
			}elseif(substr($sGTINCode,5,1) <= 0){
69
				//UPC / UCC-12 GTIN-12 code
70
				if($p_getType){
71
				    if(substr($sGTINCode,6,1) == 5)
72
				        return 'UPC coupon code';
73
				    else
74
    					return 'UPC';
75
				}else{
76
				    if($bSkipCouponCodes && substr($sGTINCode,6,1) == 5)
77
				        return false;
78
 
79
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -12));
80
				}
81
			}elseif(substr($sGTINCode,0,6) == 0){
82
				//GTIN-14
83
				if($p_getType){
84
					return 'GTIN-14';
85
				}else{
86
					return (string)substr($sGTINCode,-14);
87
				}
88
			}else{
89
				//EAN code
90
				if($p_getType){
91
					return 'EAN';
92
				}else{
93
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -13));
94
				}
95
			}
96
		}else{
97
			return FALSE;
98
		}
99
	}
100
 
101
	public static function GTINCheckDigit($p_sGTINCode)
102
	{
103
		$iCalculation = 0;
104
		for($i = 0; $i < (strlen($p_sGTINCode)-1); $i++){
105
			$iCalculation += $i % 2 ? $p_sGTINCode[$i] * 1 :  $p_sGTINCode[$i] * 3;
106
		}
107
 
108
		if(substr(10-(substr($iCalculation,-1)), -1) != substr($p_sGTINCode, -1)){
109
			$iCalculation = null;
110
			$p_sGTINCode = null;
111
			return FALSE;
112
		}else{
113
			$iCalculation = null;
114
			$p_sGTINCode = null;
115
			return TRUE;
116
		}
117
	}
118
}
119
?>