File: PHPCueCat.class.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (400 lines) | stat: -rw-r--r-- 9,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
 * Project:     PHPCueCat - A PHP CueCat Decoding Library
 * File:        PHPCueCat.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For questions, help, comments, discussion, etc., please join the
 * Lyceum mailing list. Send a blank e-mail to
 * lyceum-users-subscribe@lists.sourceforge.net
 *
 * You may contact the authors of PhatCat, by e-mail at:
 * sbw@ibiblio.org
 *
 * Or, write to:
 * Blake Watters
 * Ibiblio.org
 * CB #3456, Manning Hall
 * UNC-Chapel Hill
 * Chapel Hill, North Carolina 27599-34
 *
 * The latest version of PHPCueCat can be obtained from:
 * http://www.ibiblio.org/sbw/phpcuecat/
 *
 * @link http://www.ibiblio.org/sbw/phpcuecat/
 * @copyright 2003 Blake Watters
 * @author Blake Watters <sbw@ibiblio.org>
 * @package PHPCueCat
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 1.0
 */

/**
 * Provides a high level interface for accessing data scanned by the popular
 * CueCat line of PS/2 barcode scanners.
 *
 * Portions of this code was derived from Dustin Sallings' kittycode.js
 * @link http://bleu.west.spy.net/~dustin/kittycode/kittycode.js
 *
 * Portions of this code was derived from Dustin Grau's phatcat.php
 * 
 * @package PHPCueCat
 */
class PHPCueCat {
  
  /**
   * The unique id of the CueCat used to scan the input
   *
   * @var int
   */
  var $cat_id;
  
  /**
   * The type of barcode that was scanned
   *
   * @var string
   */  
  var $code_type;
  
  /**
   * The raw decoded barcode value
   *
   * @var string
   */
  var $bar_code;
  
  /**
   * Whether or not the instance has parsed a scan yet
   *
   * @access private
   * @var bool
   */
  var $_parsed = false;
  
  /**
   * Whether or not the instance has parsed a valid barcode
   *
   * @access private
   * @var bool
   */
  var $_valid = false;
  
  /**
   * Attempt to parse a string read from a CueCat scanner out
   * into the component parts
   *
   * @var string $cc_str the string read from the CueCat
   * @return bool the success of the parsing operation
   */
  function parse ($cc_str) {
    $parts = explode('.', $cc_str);
    
    if (count ($parts) != 5) {
      return (false);
    }
    
    /**
     * Decode the component pieces of the scanner
     */
    $this->cat_id = $this->decode_part ($parts[1]);
    $this->code_type = $this->decode_part ($parts[2]);
    $this->bar_code = $this->decode_part ($parts[3]);
    
    /**
     * Set the flags
     */
    $this->_parsed = true;
    $this->_valid = $this->check_barcode ($this->bar_code);
    
    return (true);
  }
  
  /**
   * Decode a part of a CueCat code and return the result
   *
   * @param string $part the string portion we are examining
   * @return string the result of the decoding operation
   */
  function decode_part ($part) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-";
    $result = "";
    $packer = 0;
    $count = 0;    
    
    for ($i = 0; $i < strlen ($part); $i++) {
      /**
       * Get the offset to the current character in our map
       */
      $x = strpos($chars, substr($part, $i, 1));
      
      /**
       * For invalid characters, point them out really well!
       */
      if($x < 0) {
        $result .= " > " . substr($code, $i, 1) . " < ";
        continue;
      }
  
      /**
       * Only count valid characters
       */
      $count++;
  
      /**
       * Pack them bits.
       */
      $packer = ($packer << 6 | $x);
  
      /**
       * Every four bytes, we have three valid characters.
       */
      if ($count == 4) {
        $result .= chr(($packer >> 16) ^ 67);
        $result .= chr(($packer >> 8 & 255) ^ 67);
        $result .= chr(($packer & 255) ^ 67);
        $count = 0;
        $packer = 0;
      }
    }
  
    /**
     * Now, deal with the remainders
     */
    if ($count == 2) {
      $result .= chr((( ($packer << 12) >> 16) ^ 67));
    } elseif ($count == 3) {
      $result .= chr(( ($packer << 6) >> 16) ^ 67);
      $result .= chr(( ($packer << 6) >> 8 & 255) ^ 67);
    }
    
    return ($result);
  }    
  
  /**
   * Return the validity of the current CueCat scan
   *
   * @return bool true if the decoding was successful
   */
  function is_valid () {
    return ($this->_valid);
  }
  
  /**
   * Get ISBN info for the current decoded barcode (if available)
   *
   * @return array an array of info about the ISBN
   */
  function get_isbn_info () {
    /**
     * We can only get info from valid barcodes
     */
    if (! $this->_valid) {
      return (false);
    }
    
    /**
     * Initialize our info array
     */
    $info = array ('price' => '',
                   'country' => '',
                   'isbn' => '',
                   'isbn-text' => '');
                   
    
    switch ($this->code_type) {
      /**
       * UPC's and generic barcodes have no ISBN info
       */
      case 'UPC':
      case 'UPA':
      case 'UPE':
        $info = false;
        break;
      
      /**
       * IBN's have no additional encoded information
       */
      case 'IBN':
        $info['isbn'] = substr($this->bar_code, 3, 9);
        break;
      
      case 'IB5':
        $info['isbn'] = substr($this->bar_code, 3, 9);
        $info['price'] = substr($this->bar_code, 14, 1);
        $info['country'] = substr($this->bar_code, 12, 1);
        break;
      
      case 'C39':
        $info['isbn'] = $this->bar_code;
        break;
      
      /**
       * @todo needs work
       */
      case 'UA5':
        $info['price'] = substr($this->bar_code, 7, 4);
        $info['country'] = substr($this->bar_code, 5, 1);
        $info['isbn'] = substr($this->bar_code, 12, 9);
        break;
      
      default:
        $info = false;
    }
    
    /**
     * Add the check digit
     */
    $check_digit = $this->get_isbn_check_digit($info['isbn']);
    $info['isbn'] .= $check_digit;
    
    /**
     * Add the textual version
     */
    $part1 = substr ($info['isbn'], 1, 3);
    $part2 = substr ($info['isbn'], 4, 5);
    $info['isbn-text'] = $info['isbn'][0] . '-' . $part1 . '-' . $part2 . '-' . $check_digit;
    
    return ($info);
  }
  
  /**
   * Get the ISBN check digit from a 9 digit code
   *
   * @param string the 9 digit ISBN
   * @return int|bool the check digit for the ISBN or false
   */
  function get_isbn_check_digit ($isbn) {
    $sum = 0;
    
    /**
     * Produce a sum from all weighted digits
     */
    for ($i=0; $i < 9; $i++) {
      $sum += ($i + 1) * $isbn[$i];
    }
    
    return ($sum % 11);
  }
  
  /**
   * Check a barcode to see if it is valid
   *
   * @static
   * @param string $bar_code the bar code we are examining
   * @return bool true if the barcode is valid
   */
  function check_barcode ($bar_code) {
    $result = false;
    $odd_sum = 0;
    $even_sum = 0;
    
    if ($this->code_type != 'UPA' &&
        $this->code_type != 'UPC' &&
        $this->code_type != 'UPE') {
      return (true);
    }
      
    if (strlen ($bar_code) == 12) {
      for ($i = 0; $i < 11; $i++) {
        if ($i % 2 == 0) {
          $odd_sum += $bar_code[$i];
        } else {
          $even_sum += $bar_code[$i];
        }
      }
      
      /**
       * Calculate the checksum digit
       */
		  $n = ($odd_sum * 3) + $even_sum;
		  $x = $n % 10;
		  if ($x == 0) {
		    $x = 10;
		  }
      
		  $check_digit = 10 - $x;
		  if ($bar_code[11] == $check_digit) {
		    $result = true;
		  }
    }
    
    return ($result);
  }
  
  /**
   * Check an ISBN for validity
   *
   * @link http://www.cs.queensu.ca/home/bradbury/checkdigit/isbncheck.htm
   * @static
   * @param string $isbn the ISBN we are validating
   * @return bool true if the ISBN is valid
   */
  function check_isbn ($isbn) {
    $len = strlen ($isbn);
    $sum = 0;
    
    if (! $len) {
      return (false);
    }
    
    /**
     * If the ISBN is textual, drop the dashes
     */
    if (($len == 13) && (is_string ($isbn) == true)) {
      $isbn = str_replace ('-', '', $isbn);
      $len = strlen ($isbn);
    }
    
    /**
     * If the ISBN is not exactly 10 digits, it fails
     */
    if ($len != 10) {
      return (false);
    }
    
    /**
     * Produce a sum from all weighted digits
     */
    for ($i=0; $i < ($len - 1); $i++) {
      $sum += ($i + 1) * $isbn[$i];
    }
    
    $result = $sum % 11;
    $check_digit = $isbn[9];

echo '<br>isbn:'.$isbn;
echo '<br>result:'.$result;
echo '<br>check:'.$check_digit;
    if ($result != $check_digit) {
      return (false);
    }
    
    /**
     * Add the check digit and modulus 11
     */
    $sum += (10 * $check_digit);
    if ($sum % 11 != 0) {
      return (false);
    }
    
    /**
     * All tests have been passed
     */
    return (true);
  }
}

?>