-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sip2Wrapper.php
419 lines (376 loc) · 11.9 KB
/
Sip2Wrapper.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
<?php
/**
* @author [email protected]
*/
/* pull in base class */
require_once 'sip2.class.php';
/**
* Sip2Wrapper
*
* This is a wrapper class for the sip2.class.php from google code
*
* Usage:
*```php
* // require the class
* require_once 'Sip2Wrapper.php';
*
* // create the object
* $sip2 = new Sip2Wrapper(
* array(
* 'hostname' => $hostname,
* 'port' => 6001,
* 'withCrc' => false,
* 'location' => $location,
* 'institutionId' => $institutionId
* )
* );
*
* // login and perform self test
* $sip2->login($user, $pass);
*
* // start a patron session and fetch patron status
* if ($sip2->startPatronSession($patron, $patronpwd)) {
* var_dump($sip2->patronScreenMessages);
* }
*```
*/
class Sip2Wrapper {
/**
* protected variables, accessible read-only via magic getter method
* For instance, to get a copy of $_sip2, you can call $obj->sip2
*/
/**
* sip2 object
* @var object
*/
protected $_sip2 = NULL;
/**
* connected state toggle
* @var boolean
*/
protected $_connected = false;
/**
* self check state toggle
* @var boolean
*/
protected $_selfChecked = false;
/**
* patron session state toggle
* @var boolean
*/
protected $_inPatronSession = false;
/**
* patron status
* @var array
*/
protected $_patronStatus = NULL;
/**
* patron information
* @var array
*/
protected $_patronInfo = NULL;
/**
* acs status
* @var array
*/
protected $_acsStatus = NULL;
/**
* @param string $name the member variable name
* @throws Exception if mathing getter fucntion doesn't exist
* @return mixed
*/
public function __get($name) {
/* look for a getter function named getName */
$functionName = 'get'.ucfirst($name);
if (method_exists($this, $functionName)) {
return call_user_func(array($this, $functionName));
}
throw new Exception('Undefined parameter '.$name);
}
/**
* getter function for $this->_sip2
* @return sip2
*/
public function getSip2() {
return $this->_sip2;
}
/**
* @throws Exception if patron session hasn't began
* @return array the patron status
*/
public function getPatronStatus() {
if (!$this->_inPatronSession) {
throw new Exception('Must start patron session before calling getPatronStatus');
}
if ($this->_patronStatus === NULL) {
$this->fetchPatronStatus();
}
return $this->_patronStatus;
}
/**
* parses patron status to determine if login was successful.
* @return boolean returns true if valid, false otherwise
*/
public function getPatronIsValid() {
$patronStatus = $this->getPatronStatus();
if (strcmp($patronStatus['variable']['BL'][0], 'Y') !== 0 || strcmp($patronStatus['variable']['CQ'][0], 'Y') !== 0) {
return false;
}
return true;
}
/**
* Returns the total fines from patron status call
* @return number the float value of the fines
*/
public function getPatronFinesTotal() {
$status = $this->getPatronStatus();
if (isset($status['variable']['BV'][0])) {
return (float)$status['variable']['BV'][0];
}
return 0.00;
}
/**
* returns the Screen Messages field of the patron status, which can include
* for example blocked or barred
*
* @return array the screen messages
*/
public function getPatronScreenMessages() {
$status = $this->getPatronStatus();
if (isset($status['variable']['AF']) && is_array($status['variable']['AF'])) {
return $status['variable']['AF'];
}
else {
return array();
}
}
/**
* gets the patron info hold items field
* @return array Hold Items
*/
public function getPatronHoldItems() {
$info = $this->fetchPatronInfo('hold');
if (isset($info['variable']['AS'])) {
return $info['variable']['AS'];
}
return array();
}
/**
* Get the patron info overdue items field
* @return array overdue items
*/
public function getPatronOverdueItems() {
$info = $this->fetchPatronInfo('overdue');
if (isset($info['variable']['AT'])) {
return $info['variable']['AT'];
}
return array();
}
/**
* get the charged items field
* @return array charged items
*/
public function getPatronChargedItems() {
$info = $this->fetchPatronInfo('charged');
if (isset($info['variable']['AU'])) {
return $info['variable']['AU'];
}
return array();
}
/**
* return patron fine detail from patron info
* @return array fines
*/
public function getPatronFineItems() {
$info = $this->fetchPatronInfo('fine');
if (isset($info['variable']['AV'])) {
return $info['variable']['AV'];
}
return array();
}
/**
* return patron recall items from patron info
* @return array patron items
*/
public function getPatronRecallItems() {
$info = $this->fetchPatronInfo('recall');
if (isset($info['variable']['BU'])) {
return $info['variable']['BU'];
}
return array();
}
/**
* return patron unavailable items from patron info
* @return array unavailable items
*/
public function getPatronUnavailableItems() {
$info = $this->fetchPatronInfo('unavail');
if (isset($info['variable']['CD'])) {
return $info['variable']['CD'];
}
return array();
}
/**
* worker function to call out to sip2 server and grab patron information.
* @param string $type One of 'none', 'hold', 'overdue', 'charged', 'fine', 'recall', or 'unavail'
* @throws Exception if startPatronSession has not been called with success prior to calling this
* @return array the parsed response from the server
*/
public function fetchPatronInfo($type = 'none') {
if (!$this->_inPatronSession) {
throw new Exception('Must start patron session before calling fetchPatronInfo');
}
if (is_array($this->_patronInfo) && isset($this->_patronInfo[$type])) {
return $this->_patronInfo[$type];
}
$msg = $this->_sip2->msgPatronInformation($type);
$info_response = $this->_sip2->parsePatronInfoResponse($this->_sip2->get_message($msg));
if ($this->_patronInfo === NULL) {
$this->_patronInfo = array();
}
$this->_patronInfo[$type] = $info_response;
return $info_response;
}
/**
* getter for acsStatus
* @return Ambigous <NULL, multitype:string multitype:multitype: >
*/
public function getAcsStatus() {
return $this->_acsStatus;
}
/**
* constructor
* @param $sip2Params array of key value pairs that will set the corresponding member variables
* in the underlying sip2 class
* @param boolean $autoConnect whether or not to automatically connect to the server. defaults
* to true
*/
public function __construct($sip2Params = array(), $autoConnect = true) {
$sip2 = new sip2;
foreach ($sip2Params as $key => $val) {
switch($key) {
case 'institutionId':
$key = 'AO';
break;
case 'location':
$key = 'scLocation';
break;
}
if (property_exists($sip2, $key)) {
$sip2->$key = $val;
}
}
$this->_sip2 = $sip2;
if ($autoConnect) {
$this->connect();
}
}
/**
* Connect to the server
* @throws Exception if connection fails
* @return boolean returns true if connection succeeds
*/
public function connect() {
$returnVal = $this->_sip2->connect();
if ($returnVal === true) {
$this->_connected = true;
}
else {
throw new Exception('Connection failed');
}
return true;
}
/**
* authenticate with admin credentials to the backend server
* @param string $bindUser The admin user
* @param string $bindPass The admin password
* @param string $autoSelfCheck Whether to call SCStatus after login. Defaults to true
* you probably want this.
* @throws Exception if login failed
* @return Sip2Wrapper - returns $this if login successful
*/
public function login($bindUser, $bindPass, $autoSelfCheck=true) {
$msg = $this->_sip2->msgLogin($bindUser, $bindPass);
$login = $this->_sip2->parseLoginResponse($this->_sip2->get_message($msg));
if ((int) $login['fixed']['Ok'] !== 1) {
throw new Exception('Login failed');
}
/* perform self check */
if ($autoSelfCheck) {
$this->selfCheck();
}
return $this;
}
/**
* Checks the ACS Status to ensure that the ACS is online
* @throws Exception if ACS is not online
* @return Sip2Wrapper returns $this if successful
*/
public function selfCheck() {
/* execute self test */
$msg = $this->_sip2->msgSCStatus();
$status = $this->_sip2->parseACSStatusResponse($this->_sip2->get_message($msg));
$this->_acsStatus = $status;
/* check status */
if (strcmp($status['fixed']['Online'], 'Y') !== 0) {
throw new Exception('ACS Offline');
}
return $this;
}
/**
* This method is required before any get/fetch methods that have Patron in the name. Upon
* successful login, it sets the inPatronSession property to true, otherwise false.
* @param string $patronId Patron login ID
* @param string $patronPass Patron password
* @return boolean returns true on successful login, false otherwise
*/
public function startPatronSession($patronId, $patronPass) {
if ($this->_inPatronSession) {
$this->endPatronSession();
}
$this->_sip2->patron = $patronId;
$this->_sip2->patronpwd = $patronPass;
// set to true before call to getPatronIsValid since it will throw an exception otherwise
$this->_inPatronSession = true;
$this->_inPatronSession = $this->getPatronIsValid();
return $this->_inPatronSession;
}
/**
* method to grab the patron status from the server and store it in _patronStatus
* @return Sip2Wrapper returns $this
*/
public function fetchPatronStatus() {
$msg = $this->_sip2->msgPatronStatusRequest();
$patron = $this->_sip2->parsePatronStatusResponse($this->_sip2->get_message($msg));
$this->_patronStatus = $patron;
return $this;
}
/**
* method to send a patron session to the server
* @throws Exception if patron session is not properly ended
* @return Sip2Wrapper returns $this
*/
public function endPatronSession() {
$msg = $this->_sip2->msgEndPatronSession();
$end = $this->_sip2->parseEndSessionResponse($this->_sip2->get_message($msg));
if (strcmp($end['fixed']['EndSession'], 'Y') !== 0) {
throw new Exception('Error ending patron session');
}
$this->_inPatronSession = false;
$this->_patronStatus = NULL;
$this->_patronInfo = NULL;
return $this;
}
/**
* disconnect from the server
* @return Sip2Wrapper returns $this
*/
public function disconnect() {
$this->_sip2->disconnect();
$this->_connected = false;
$this->_inPatronSession = false;
$this->_patronInfo = NULL;
$this->_acsStatus = NULL;
return $this;
}
}