Subversion Repositories cheapmusic

Rev

Rev 121 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 - 1
<?php
2
/**
20 - 3
 * Bar codes checker - Checks if a barcode is valid and returns barcode type
4
 *
1 - 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
 
65 - 11
class clsLibGTIN {
12
    public static function GTINCheck($p_sGTINCode, $p_bReturnGTIN14 = false, $p_getType = 0) {
13
        //Setting return value
14
        /*
15
         * If TRUE it will retun al barcodes as 14 digit strings
16
         * If FALSE it will return only what is needed UPC -> 12 / EAN -> 13 /
17
        */
18
        $bReturnGTIN14 = $p_bReturnGTIN14;
20 - 19
 
65 - 20
        //Filter UPC coupon codes
21
        /*
22
         * If TRUE it will return false on UPC coupon codes
23
         * Type will always return UPC coupon code
24
        */
25
        $bSkipCouponCodes = true;
20 - 26
 
65 - 27
        //Trims parsed string to remove unwanted whitespace or characters
28
        $p_sGTINCode = (string)trim($p_sGTINCode);
29
        if (preg_match('/[^0-9]/', $p_sGTINCode)) return false;
1 - 30
 
65 - 31
        if (!is_string($p_sGTINCode)) {
32
            $p_sGTINCode = strval($p_sGTINCode);
33
        }
34
        $sGTINCode = trim($p_sGTINCode);
35
        $p_sGTINCode = null;
36
        $length = strlen($sGTINCode);
37
        if (($length > 11 && $length <= 14) || $length == 8) {
113 - 38
            $zeroes = 18 - $length;
65 - 39
            $length = null;
40
            $fill = "";
113 - 41
            for ($i = 0;$i < $zeroes;$i++) {
65 - 42
                $fill .= "0";
43
            }
113 - 44
            $zeroes = null;
65 - 45
            $i = null;
46
            $sGTINCode = $fill . $sGTINCode;
47
            $fill = null;
48
            if (!clsLibGTIN::GTINCheckDigit($sGTINCode)) {
49
                return false;
50
            }
51
            elseif (substr($sGTINCode, 5, 1) > 2) {
52
                //EAN / JAN / EAN-13 code
53
                if ($p_getType) {
54
                    if (in_array(substr($sGTINCode, 5, 3) , array(
55
                        "978",
56
                        "979"
57
                    ))) {
58
                        return 'ISBN';
59
                    }
60
                    else {
61
                        return 'EAN';
62
                    }
63
                }
64
                else {
65
                    return (string)substr($sGTINCode, ($bReturnGTIN14 ? -14 : -13));
66
                }
67
            }
68
            elseif (substr($sGTINCode, 6, 1) == 0 && substr($sGTINCode, 0, 10) == 0) {
69
                //EAN-8 / GTIN-8 code
70
                if ($p_getType) {
71
                    return 'EAN-8';
72
                }
73
                else {
74
                    return (string)substr($sGTINCode, ($bReturnGTIN14 ? -14 : -8));
75
                }
76
            }
77
            elseif (substr($sGTINCode, 5, 1) <= 0) {
78
                //UPC / UCC-12 GTIN-12 code
79
                if ($p_getType) {
80
                    if (substr($sGTINCode, 6, 1) == 5) return 'UPC coupon code';
81
                    else return 'UPC';
82
                }
83
                else {
84
                    if ($bSkipCouponCodes && substr($sGTINCode, 6, 1) == 5) return false;
20 - 85
 
65 - 86
                    return (string)substr($sGTINCode, ($bReturnGTIN14 ? -14 : -12));
87
                }
88
            }
89
            elseif (substr($sGTINCode, 0, 6) == 0) {
90
                //GTIN-14
91
                if ($p_getType) {
92
                    return 'GTIN-14';
93
                }
94
                else {
95
                    return (string)substr($sGTINCode, -14);
96
                }
97
            }
98
            else {
99
                //EAN code
100
                if ($p_getType) {
101
                    return 'EAN';
102
                }
103
                else {
104
                    return (string)substr($sGTINCode, ($bReturnGTIN14 ? -14 : -13));
105
                }
106
            }
107
        }
108
        else {
109
            return false;
110
        }
111
    }
1 - 112
 
65 - 113
    public static function GTINCheckDigit($p_sGTINCode) {
114
        $iCalculation = 0;
115
        for ($i = 0;$i < (strlen($p_sGTINCode) - 1);$i++) {
116
            $iCalculation += $i % 2 ? $p_sGTINCode[$i] * 1 : $p_sGTINCode[$i] * 3;
117
        }
20 - 118
 
65 - 119
        if (substr(10 - (substr($iCalculation, -1)) , -1) != substr($p_sGTINCode, -1)) {
120
            $iCalculation = null;
121
            $p_sGTINCode = null;
122
            return false;
123
        }
124
        else {
125
            $iCalculation = null;
126
            $p_sGTINCode = null;
127
            return true;
128
        }
129
    }
113 - 130
 
131
    public static function GTINCalcCheckDigit($p_sGTINCode) {
121 - 132
        $p_sGTINCode = (string)trim($p_sGTINCode);
133
        if (preg_match('/[^0-9]/', $p_sGTINCode)) return false;
134
 
134 - 135
        $p_sGTINCode .= 'X';
113 - 136
        $sGTINCode = trim($p_sGTINCode);
137
        $length = strlen($sGTINCode);
138
        if (($length > 11 && $length <= 14) || $length == 8) {
139
            $zeroes = 18 - $length;
140
            $length = null;
141
            $fill = "";
142
            for ($i = 0;$i < $zeroes;$i++) {
143
                $fill .= "0";
144
            }
145
            $zeroes = null;
146
            $sGTINCode = $fill . $sGTINCode;
147
            $fill = null;
134 - 148
        } else {
149
            return false;
113 - 150
        }
151
        $iCalculation = 0;
152
        for ($i = 0;$i < (strlen($sGTINCode) - 1);$i++) {
153
            $iCalculation += $i % 2 ? $sGTINCode[$i] * 1 : $sGTINCode[$i] * 3;
154
        }
155
        $iDigit = substr(10 - (substr($iCalculation, -1)) , -1);
156
        $iCalculation = null;
157
        $p_sGTINCode = null;
158
        return $iDigit;
159
    }
1 - 160
}
65 - 161
?>