-
Notifications
You must be signed in to change notification settings - Fork 1
/
sip2.class.php
882 lines (745 loc) · 29.6 KB
/
sip2.class.php
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
<?php
/**
* SIP2 Class
*
* This class provides a methoid of communicating with an Integrated
* Library System using 3M's SIP2 standard.
*
* PHP version 5
*
*
* @package
* @author John Wohlers <[email protected]>
* @licence http://opensource.org/licenses/gpl-3.0.html
* @copyright John Wohlers <[email protected]>
* @version $Id$
* @link http://php-sip2.googlecode.com/
*/
/**
* 2010.10.08
* Fixed a potential endless loop condition if a socket lost connection in the middle of a transaction.
*
* 2008.04.11
* Encorported a bug fix submitted by Bob Wicksall
*
* TODO
* - Clean up variable names, check for consistancy
* - Add better i18n support, including functions to handle the SIP2 language definitions
*
*/
/**
* General Usage:
* include('sip2.class.php');
*
* // create object
* $mysip = new sip2;
*
* // Set host name
* $mysip->hostname = 'server.example.com';
* $mysip->port = 6002;
*
* // Identify a patron
* $mysip->patron = '101010101';
* $mysip->patronpwd = '010101';
*
* // connect to SIP server
* $result = $mysip->connect();
*
* // selfcheck status mesage goes here...
*
*
* // Get Charged Items Raw response
* $in = $mysip->msgPatronInformation('charged');
*
* // parse the raw response into an array
* $result = $mysip->parsePatronInfoResponse( $mysip->get_message($in) );
*
*/
class sip2
{
/* Public variables for configuration */
public $hostname;
public $port = 6002; /* default sip2 port for Sirsi */
public $library = '';
public $language = '001'; /* 001= english */
/* Patron ID */
public $patron = ''; /* AA */
public $patronpwd = ''; /* AD */
/*terminal password */
public $AC = ''; /*AC */
/* Maximum number of resends allowed before get_message gives up */
public $maxretry = 3;
/* Terminator s */
public $fldTerminator = '|';
public $msgTerminator = "\r\n";
/* Login Variables */
public $UIDalgorithm = 0; /* 0 = unencrypted, default */
public $PWDalgorithm = 0; /* undefined in documentation */
public $scLocation = ''; /* Location Code */
/* Debug */
public $debug = false;
/* Private variables for building messages */
public $AO = 'WohlersSIP';
public $AN = 'SIPCHK';
/* Private variable to hold socket connection */
private $socket;
/* Sequence number counter */
private $seq = -1;
/* resend counter */
private $retry = 0;
/* Workarea for building a message */
private $msgBuild = '';
private $noFixed = false;
function msgPatronStatusRequest()
{
/* Server Response: Patron Status Response message. */
$this->_newMessage('23');
$this->_addFixedOption($this->language, 3);
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AC',$this->AC);
$this->_addVarOption('AD',$this->patronpwd);
return $this->_returnMessage();
}
function msgCheckout($item, $nbDateDue ='', $scRenewal='N', $itmProp ='', $fee='N', $noBlock='N', $cancel='N')
{
/* Checkout an item (11) - untested */
$this->_newMessage('11');
$this->_addFixedOption($scRenewal, 1);
$this->_addFixedOption($noBlock, 1);
$this->_addFixedOption($this->_datestamp(), 18);
if ($nbDateDue != '') {
/* override defualt date due */
$this->_addFixedOption($this->_datestamp($nbDateDue), 18);
} else {
/* send a blank date due to allow ACS to use default date due computed for item */
$this->_addFixedOption('', 18);
}
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AB',$item);
$this->_addVarOption('AC',$this->AC);
$this->_addVarOption('CH',$itmProp, true);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('BO',$fee, true); /* Y or N */
$this->_addVarOption('BI',$cancel, true); /* Y or N */
return $this->_returnMessage();
}
function msgCheckin($item, $itmReturnDate, $itmLocation = '', $itmProp = '', $noBlock='N', $cancel = '')
{
/* Checkin an item (09) - untested */
if ($itmLocation == '') {
/* If no location is specified, assume the defualt location of the SC, behavior suggested by spec*/
$itmLocation = $this->scLocation;
}
$this->_newMessage('09');
$this->_addFixedOption($noBlock, 1);
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addFixedOption($this->_datestamp($itmReturnDate), 18);
$this->_addVarOption('AP',$itmLocation);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AB',$item);
$this->_addVarOption('AC',$this->AC);
$this->_addVarOption('CH',$itmProp, true);
$this->_addVarOption('BI',$cancel, true); /* Y or N */
return $this->_returnMessage();
}
function msgBlockPatron($message, $retained='N')
{
/* Blocks a patron, and responds with a patron status response (01) - untested */
$this->_newMessage('01');
$this->_addFixedOption($retained, 1); /* Y if card has been retained */
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AL',$message);
$this->_addVarOption('AA',$this->AA);
$this->_addVarOption('AC',$this->AC);
return $this->_returnMessage();
}
function msgSCStatus($status = 0, $width = 80, $version = 2)
{
/* selfcheck status message, this should be sent immediatly after login - untested */
/* status codes, from the spec:
* 0 SC unit is OK
* 1 SC printer is out of paper
* 2 SC is about to shut down
*/
if ($version > 3) {
$version = 2;
}
if ($status < 0 || $status > 2) {
$this->_debugmsg( "SIP2: Invalid status passed to msgSCStatus" );
return false;
}
$this->_newMessage('99');
$this->_addFixedOption($status, 1);
$this->_addFixedOption($width, 3);
$this->_addFixedOption(sprintf("%03.2f",$version), 4);
return $this->_returnMessage();
}
function msgRequestACSResend ()
{
/* Used to request a resend due to CRC mismatch - No sequence number is used */
$this->_newMessage('97');
return $this->_returnMessage(false);
}
function msgLogin($sipLogin, $sipPassword)
{
/* Login (93) - untested */
$this->_newMessage('93');
$this->_addFixedOption($this->UIDalgorithm, 1);
$this->_addFixedOption($this->PWDalgorithm, 1);
$this->_addVarOption('CN',$sipLogin);
$this->_addVarOption('CO',$sipPassword);
$this->_addVarOption('CP',$this->scLocation, true);
return $this->_returnMessage();
}
function msgPatronInformation($type, $start = '1', $end = '5')
{
/*
* According to the specification:
* Only one category of items should be requested at a time, i.e. it would take 6 of these messages,
* each with a different position set to Y, to get all the detailed information about a patron's items.
*/
$summary['none'] = ' ';
$summary['hold'] = 'Y ';
$summary['overdue'] = ' Y ';
$summary['charged'] = ' Y ';
$summary['fine'] = ' Y ';
$summary['recall'] = ' Y ';
$summary['unavail'] = ' Y';
/* Request patron information */
$this->_newMessage('63');
$this->_addFixedOption($this->language, 3);
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addFixedOption(sprintf("%-10s",$summary[$type]), 10);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('BP',$start, true); /* old function version used padded 5 digits, not sure why */
$this->_addVarOption('BQ',$end, true); /* old function version used padded 5 digits, not sure why */
return $this->_returnMessage();
}
function msgEndPatronSession()
{
/* End Patron Session, should be sent before switching to a new patron. (35) - untested */
$this->_newMessage('35');
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('AD',$this->patronpwd, true);
return $this->_returnMessage();
}
/* Fee paid function should go here */
function msgFeePaid ($feeType, $pmtType, $pmtAmount, $curType = 'USD', $feeId = '', $transId = '')
{
/* Fee payment function (37) - untested */
/* Fee Types: */
/* 01 other/unknown */
/* 02 administrative */
/* 03 damage */
/* 04 overdue */
/* 05 processing */
/* 06 rental*/
/* 07 replacement */
/* 08 computer access charge */
/* 09 hold fee */
/* Value Payment Type */
/* 00 cash */
/* 01 VISA */
/* 02 credit card */
if (!is_numeric($feeType) || $feeType > 99 || $feeType < 1) {
/* not a valid fee type - exit */
$this->_debugmsg( "SIP2: (msgFeePaid) Invalid fee type: {$feeType}");
return false;
}
if (!is_numeric($pmtType) || $pmtType > 99 || $pmtType < 0) {
/* not a valid payment type - exit */
$this->_debugmsg( "SIP2: (msgFeePaid) Invalid payment type: {$pmtType}");
return false;
}
$this->_newMessage('37');
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addFixedOption(sprintf('%02d', $feeType), 2);
$this->_addFixedOption(sprintf('%02d', $pmtType), 2);
$this->_addFixedOption($curType, 3);
$this->_addVarOption('BV',$pmtAmount); /* due to currancy format localization, it is up to the programmer to properly format their payment amount */
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('CG',$feeId, true);
$this->_addVarOption('BK',$transId, true);
return $this->_returnMessage();
}
function msgItemInformation($item)
{
$this->_newMessage('17');
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AB',$item);
$this->_addVarOption('AC',$this->AC, true);
return $this->_returnMessage();
}
function msgItemStatus ($item, $itmProp = '')
{
/* Item status update function (19) - untested */
$this->_newMessage('19');
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AB',$item);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('CH',$itmProp);
return $this->_returnMessage();
}
function msgPatronEnable ()
{
/* Patron Enable function (25) - untested */
/* This message can be used by the SC to re-enable canceled patrons. It should only be used for system testing and validation. */
$this->_newMessage('25');
$this->_addFixedOption($this->_datestamp(), 18);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('AD',$this->patronpwd, true);
return $this->_returnMessage();
}
function msgHold($mode, $expDate = '', $holdtype = '', $item = '', $title = '', $fee='N', $pkupLocation = '')
{
/* mode validity check */
/*
* - remove hold
* + place hold
* * modify hold
*/
if (strpos('-+*',$mode) === false) {
/* not a valid mode - exit */
$this->_debugmsg( "SIP2: Invalid hold mode: {$mode}");
return false;
}
if ($holdtype != '' && ($holdtype < 1 || $holdtype > 9)) {
/*
* Valid hold types range from 1 - 9
* 1 other
* 2 any copy of title
* 3 specific copy
* 4 any copy at a single branch or location
*/
$this->_debugmsg( "SIP2: Invalid hold type code: {$holdtype}");
return false;
}
$this->_newMessage('15');
$this->_addFixedOption($mode, 1);
$this->_addFixedOption($this->_datestamp(), 18);
if ($expDate != '') {
/* hold expiration date, due to the use of the datestamp function, we have to check here for empty value. when datestamp is passed an empty value it will generate a current datestamp */
$this->_addVarOption('BW', $this->_datestamp($expDate), true); /*spec says this is fixed field, but it behaves like a var field and is optional... */
}
$this->_addVarOption('BS',$pkupLocation, true);
$this->_addVarOption('BY',$holdtype, true);
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('AB',$item, true);
$this->_addVarOption('AJ',$title, true);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('BO',$fee, true); /* Y when user has agreed to a fee notice */
return $this->_returnMessage();
}
function msgRenew($item = '', $title = '', $nbDateDue = '', $itmProp = '', $fee= 'N', $noBlock = 'N', $thirdParty = 'N')
{
/* renew a single item (29) - untested */
$this->_newMessage('29');
$this->_addFixedOption($thirdParty, 1);
$this->_addFixedOption($noBlock, 1);
$this->_addFixedOption($this->_datestamp(), 18);
if ($nbDateDue != '') {
/* override default date due */
$this->_addFixedOption($this->_datestamp($nbDateDue), 18);
} else {
/* send a blank date due to allow ACS to use default date due computed for item */
$this->_addFixedOption('', 18);
}
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('AB',$item, true);
$this->_addVarOption('AJ',$title, true);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('CH',$itmProp, true);
$this->_addVarOption('BO',$fee, true); /* Y or N */
return $this->_returnMessage();
}
function msgRenewAll($fee = 'N')
{
/* renew all items for a patron (65) - untested */
$this->_newMessage('65');
$this->_addVarOption('AO',$this->AO);
$this->_addVarOption('AA',$this->patron);
$this->_addVarOption('AD',$this->patronpwd, true);
$this->_addVarOption('AC',$this->AC, true);
$this->_addVarOption('BO',$fee, true); /* Y or N */
return $this->_returnMessage();
}
function parsePatronStatusResponse($response)
{
$result['fixed'] =
array(
'PatronStatus' => substr($response, 2, 14),
'Language' => substr($response, 16, 3),
'TransactionDate' => substr($response, 19, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 37);
return $result;
}
function parseCheckoutResponse($response)
{
$result['fixed'] =
array(
'Ok' => substr($response,2,1),
'RenewalOk' => substr($response,3,1),
'Magnetic' => substr($response,4,1),
'Desensitize' => substr($response,5,1),
'TransactionDate' => substr($response,6,18),
);
$result['variable'] = $this->_parsevariabledata($response, 24);
return $result;
}
function parseCheckinResponse($response)
{
$result['fixed'] =
array(
'Ok' => substr($response,2,1),
'Resensitize' => substr($response,3,1),
'Magnetic' => substr($response,4,1),
'Alert' => substr($response,5,1),
'TransactionDate' => substr($response,6,18),
);
$result['variable'] = $this->_parsevariabledata($response, 24);
return $result;
}
function parseACSStatusResponse($response)
{
$result['fixed'] =
array(
'Online' => substr($response, 2, 1),
'Checkin' => substr($response, 3, 1), /* is Checkin by the SC allowed ?*/
'Checkout' => substr($response, 4, 1), /* is Checkout by the SC allowed ?*/
'Renewal' => substr($response, 5, 1), /* renewal allowed? */
'PatronUpdate' => substr($response, 6, 1), /* is patron status updating by the SC allowed ? (status update ok)*/
'Offline' => substr($response, 7, 1),
'Timeout' => substr($response, 8, 3),
'Retries' => substr($response, 11, 3),
'TransactionDate' => substr($response, 14, 18),
'Protocol' => substr($response, 32, 4),
);
$result['variable'] = $this->_parsevariabledata($response, 36);
return $result;
}
function parseLoginResponse($response)
{
$result['fixed'] =
array(
'Ok' => substr($response, 2, 1),
);
$result['variable'] = array();
return $result;
}
function parsePatronInfoResponse($response)
{
$result['fixed'] =
array(
'PatronStatus' => substr($response, 2, 14),
'Language' => substr($response, 16, 3),
'TransactionDate' => substr($response, 19, 18),
'HoldCount' => intval (substr($response, 37, 4)),
'OverdueCount' => intval (substr($response, 41, 4)),
'ChargedCount' => intval (substr($response, 45, 4)),
'FineCount' => intval (substr($response, 49, 4)),
'RecallCount' => intval (substr($response, 53, 4)),
'UnavailableCount' => intval (substr($response, 57, 4))
);
$result['variable'] = $this->_parsevariabledata($response, 61);
return $result;
}
function parseEndSessionResponse($response)
{
/* Response example: 36Y20080228 145537AOWOHLERS|AAX00000000|AY9AZF474 */
$result['fixed'] =
array(
'EndSession' => substr($response, 2, 1),
'TransactionDate' => substr($response, 3, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 21);
return $result;
}
function parseFeePaidResponse($response)
{
$result['fixed'] =
array(
'PaymentAccepted' => substr($response, 2, 1),
'TransactionDate' => substr($response, 3, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 21);
return $result;
}
function parseItemInfoResponse($response)
{
$result['fixed'] =
array(
'CirculationStatus' => intval (substr($response, 2, 2)),
'SecurityMarker' => intval (substr($response, 4, 2)),
'FeeType' => intval (substr($response, 6, 2)),
'TransactionDate' => substr($response, 8, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 26);
return $result;
}
function parseItemStatusResponse($response)
{
$result['fixed'] =
array(
'PropertiesOk' => substr($response, 2, 1),
'TransactionDate' => substr($response, 3, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 21);
return $result;
}
function parsePatronEnableResponse($response)
{
$result['fixed'] =
array(
'PatronStatus' => substr($response, 2, 14),
'Language' => substr($response, 16, 3),
'TransactionDate' => substr($response, 19, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 37);
return $result;
}
function parseHoldResponse($response)
{
$result['fixed'] =
array(
'Ok' => substr($response, 2, 1),
'available' => substr($response, 3, 1),
'TransactionDate' => substr($response, 4, 18),
'ExpirationDate' => substr($response, 22, 18)
);
$result['variable'] = $this->_parsevariabledata($response, 40);
return $result;
}
function parseRenewResponse($response)
{
/* Response Example: 300NUU20080228 222232AOWOHLERS|AAX00000241|ABM02400028262|AJFolksongs of Britain and Ireland|AH5/23/2008,23:59|CH|AFOverride required to exceed renewal limit.|AY1AZCDA5 */
$result['fixed'] =
array(
'Ok' => substr($response, 2, 1),
'RenewalOk' => substr($response, 3, 1),
'Magnetic' => substr($response, 4, 1),
'Desensitize' => substr($response, 5, 1),
'TransactionDate' => substr($response, 6, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 24);
return $result;
}
function parseRenewAllResponse($response)
{
$result['fixed'] =
array(
'Ok' => substr($response, 2, 1),
'Renewed' => substr($response, 3, 4),
'Unrenewed' => substr($response, 7, 4),
'TransactionDate' => substr($response, 11, 18),
);
$result['variable'] = $this->_parsevariabledata($response, 29);
return $result;
}
function get_message ($message)
{
/* sends the current message, and gets the response */
$result = '';
$terminator = '';
$nr = '';
$this->_debugmsg('SIP2: Sending SIP2 request...');
socket_write($this->socket, $message, strlen($message));
$this->_debugmsg('SIP2: Request Sent, Reading response');
while ($terminator != "\x0D" && $nr !== FALSE) {
$nr = socket_recv($this->socket,$terminator,1,0);
$result = $result . $terminator;
}
$this->_debugmsg("SIP2: {$result}");
/* test message for CRC validity */
if ($this->_check_crc($result)) {
/* reset the retry counter on successful send */
$this->retry=0;
$this->_debugmsg("SIP2: Message from ACS passed CRC check");
} else {
/* CRC check failed, request a resend */
$this->retry++;
if ($this->retry < $this->maxretry) {
/* try again */
$this->_debugmsg("SIP2: Message failed CRC check, retrying ({$this->retry})");
$this->get_message($message);
} else {
/* give up */
$this->_debugmsg("SIP2: Failed to get valid CRC after {$this->maxretry} retries.");
return false;
}
}
return $result;
}
function connect()
{
/* Socket Communications */
$this->_debugmsg( "SIP2: --- BEGIN SIP communication ---");
/* Get the IP address for the target host. */
$address = gethostbyname($this->hostname);
/* Create a TCP/IP socket. */
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
/* check for actual truly false result using ===*/
if ($this->socket === false) {
$this->_debugmsg( "SIP2: socket_create() failed: reason: " . socket_strerror($this->socket));
return false;
} else {
$this->_debugmsg( "SIP2: Socket Created" );
}
$this->_debugmsg( "SIP2: Attempting to connect to '$address' on port '{$this->port}'...");
/* open a connection to the host */
$result = socket_connect($this->socket, $address, $this->port);
if (!$result) {
$this->_debugmsg("SIP2: socket_connect() failed.\nReason: ($result) " . socket_strerror($result));
} else {
$this->_debugmsg( "SIP2: --- SOCKET READY ---" );
}
/* return the result from the socket connect */
return $result;
}
function disconnect ()
{
/* Close the socket */
socket_close($this->socket);
}
/* Core local utility functions */
function _datestamp($timestamp = '')
{
/* generate a SIP2 compatable datestamp */
/* From the spec:
* YYYYMMDDZZZZHHMMSS.
* All dates and times are expressed according to the ANSI standard X3.30 for date and X3.43 for time.
* The ZZZZ field should contain blanks (code $20) to represent local time. To represent universal time,
* a Z character(code $5A) should be put in the last (right hand) position of the ZZZZ field.
* To represent other time zones the appropriate character should be used; a Q character (code $51)
* should be put in the last (right hand) position of the ZZZZ field to represent Atlantic Standard Time.
* When possible local time is the preferred format.
*/
if ($timestamp != '') {
/* Generate a proper date time from the date provided */
return date('Ymd His', $timestamp);
} else {
/* Current Date/Time */
return date('Ymd His');
}
}
function _parsevariabledata($response, $start)
{
$result = array();
$result['Raw'] = explode("|", substr($response,$start,-7));
foreach ($result['Raw'] as $item) {
$field = substr($item,0,2);
$value = substr($item,2);
/* SD returns some odd values on ocassion, Unable to locate the purpose in spec, so I strip from
* the parsed array. Orig values will remain in ['raw'] element
*/
$clean = trim($value, "\x00..\x1F");
if (trim($clean) <> '') {
$result[$field][] = $clean;
}
}
$result['AZ'][] = substr($response,-5);
return ($result);
}
function _crc($buf)
{
/* Calculate CRC */
$sum = 0;
$len = strlen($buf);
for ($n = 0; $n < $len; $n++) {
$sum = $sum + ord(substr($buf, $n, 1));
}
$crc = ($sum & 0xFFFF) * -1;
/* 2008.03.15 - Fixed a bug that allowed the checksum to be larger then 4 digits */
return substr(sprintf ("%4X", $crc), -4, 4);
} /* end crc */
function _getseqnum()
{
/* Get a sequence number for the AY field */
/* valid numbers range 0-9 */
$this->seq++;
if ($this->seq > 9 ) {
$this->seq = 0;
}
return ($this->seq);
}
function _debugmsg($message)
{
/* custom debug function, why repeat the check for the debug flag in code... */
if ($this->debug) {
trigger_error( $message, E_USER_NOTICE);
}
}
function _check_crc($message)
{
/* test the recieved message's CRC by generating our own CRC from the message */
$test = preg_split('/(.{4})$/',trim($message),2,PREG_SPLIT_DELIM_CAPTURE);
if ($this->_crc($test[0]) == $test[1]) {
return true;
} else {
return false;
}
}
function _newMessage($code)
{
/* resets the msgBuild variable to the value of $code, and clears the flag for fixed messages */
$this->noFixed = false;
$this->msgBuild = $code;
}
function _addFixedOption($value, $len)
{
/* adds afixed length option to the msgBuild IF no variable options have been added. */
if ( $this->noFixed ) {
return false;
} else {
$this->msgBuild .= sprintf("%{$len}s", substr($value,0,$len));
return true;
}
}
function _addVarOption($field, $value, $optional = false)
{
/* adds a varaiable length option to the message, and also prevents adding addtional fixed fields */
if ($optional == true && $value == '') {
/* skipped */
$this->_debugmsg( "SIP2: Skipping optional field {$field}");
} else {
$this->noFixed = true; /* no more fixed for this message */
$this->msgBuild .= $field . substr($value, 0, 255) . $this->fldTerminator;
}
return true;
}
function _returnMessage($withSeq = true, $withCrc = true)
{
/* Finalizes the message and returns it. Message will remain in msgBuild until newMessage is called */
if ($withSeq) {
$this->msgBuild .= 'AY' . $this->_getseqnum();
}
if ($withCrc) {
$this->msgBuild .= 'AZ';
$this->msgBuild .= $this->_crc($this->msgBuild);
}
$this->msgBuild .= $this->msgTerminator;
return $this->msgBuild;
}
}
?>