Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 - 1
<?php
2
/**
3
 * Bar codes checker - Checks if a barcode is valid and returns barcode type
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
				    if (in_array(substr($sGTINCode,5,3), array("978", "979"))){
58
					    return 'ISBN';
59
				    }else{
60
					    return 'EAN';
61
				    }
62
				}else{
63
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -13));
64
				}
65
			}elseif(substr($sGTINCode,6,1) == 0 && substr($sGTINCode,0,10) == 0){
66
				//EAN-8 / GTIN-8 code
67
				if($p_getType){
68
					return 'EAN-8';
69
				}else{
70
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -8));
71
				}
72
			}elseif(substr($sGTINCode,5,1) <= 0){
73
				//UPC / UCC-12 GTIN-12 code
74
				if($p_getType){
75
				    if(substr($sGTINCode,6,1) == 5)
76
				        return 'UPC coupon code';
77
				    else
78
    					return 'UPC';
79
				}else{
80
				    if($bSkipCouponCodes && substr($sGTINCode,6,1) == 5)
81
				        return false;
82
 
83
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -12));
84
				}
85
			}elseif(substr($sGTINCode,0,6) == 0){
86
				//GTIN-14
87
				if($p_getType){
88
					return 'GTIN-14';
89
				}else{
90
					return (string)substr($sGTINCode,-14);
91
				}
92
			}else{
93
				//EAN code
94
				if($p_getType){
95
				    return 'EAN';
96
				}else{
97
					return (string)substr($sGTINCode,($bReturnGTIN14 ? -14 : -13));
98
				}
99
			}
100
		}else{
101
			return FALSE;
102
		}
103
	}
104
 
105
	public static function GTINCheckDigit($p_sGTINCode)
106
	{
107
		$iCalculation = 0;
108
		for($i = 0; $i < (strlen($p_sGTINCode)-1); $i++){
109
			$iCalculation += $i % 2 ? $p_sGTINCode[$i] * 1 :  $p_sGTINCode[$i] * 3;
110
		}
111
 
112
		if(substr(10-(substr($iCalculation,-1)), -1) != substr($p_sGTINCode, -1)){
113
			$iCalculation = null;
114
			$p_sGTINCode = null;
115
			return FALSE;
116
		}else{
117
			$iCalculation = null;
118
			$p_sGTINCode = null;
119
			return TRUE;
120
		}
121
	}
122
}
123
?>