/
IIIParse.php
943 lines (702 loc) · 27.4 KB
/
IIIParse.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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
<?
require_once('simple_html_dom.php');
ini_set("memory_limit", "40M");
class IIIParse {
public $base_url = null;
public $catalog_url = null;
public $email_subject = null;
public $email_from = null;
public $catalog_name = "DefaultCatalog";
public $def_scope = "0";
public $def_type = "X";
public $loc_td = 0;
public $call_td = 1;
public $status_td = 2;
public $feedback_email = "feedback@example.com";
public $max_value_length = null;
# Keys you would like to display from the bibDetail table
public $detail_keys = array("Publisher", "Edition");
public $email_detail_keys = array("Title");
public $cover_image_config = null;
public $mobile_domains = array(
"AT&T" => "txt.att.net",
"Verizon" => "vtext.com",
"T-Mobile" => "tmomail.net",
"Sprint" => "messaging.sprintpcs.com",
"Nextel" => "messaging.nextel.com",
"Virgin" => "vmobl.com",
"Boost" => "myboostmobile.com",
"Alltel" => "message.alltel.com",
"MetroPCS" => "mymetropcs.com",
"Cricket" => "mms.mycricket.com"
);
public $available_status = array("available");
public $invalid_request_locs = '/^where/i';
public $email_subject_prefix = "Catalog";
protected function get_browse_url($searcharg, $searchscope, $searchtype) {
$query = http_build_query(array(
"searchtype" => $searchtype,
"searcharg" => $searcharg,
"searchscope" => $searchscope,
));
$url = $this->catalog_url . '/search/?' . $query;
return $url;
}
protected function find_num_results($html) {
$itag = $html->find("div.browseSearchtoolMessage i", 0);
if(isset($itag->innertext)) {
if (preg_match('/\d+/', $itag->innertext, $matches)) {
return $matches[0];
} else {
return 0;
}
} else {
return 0;
}
}
protected function find_nav_links($html) {
$links_to_get = array(
'Next' => '',
'Prev' => '',
);
#find links for more results
$anchors = $html->find("td.browsePager a");
if($anchors) {
#if first or last are non-numeric (text or an image), then they must(?) be prev/next links
if(!is_numeric($anchors[0]->innertext)) {
$links_to_get['Prev'] = $this->catalog_url . urldecode($anchors[0]->href);
}
if(!is_numeric(end($anchors)->innertext)) {
$links_to_get['Next'] = $this->catalog_url . urldecode(end($anchors)->href);
}
}
return $links_to_get;
}
protected function translate_material($material) {
return strtolower($material);
}
protected function parse_record($row) {
$info = array();
$spans = $row->children();
# These are the standard first 4 fields
$info['title'] = ptext($spans[0]);
$info['isbn'] = starting_digits(ptext($spans[1]));
#cover_image
$info['cover_image'] = 'static/nocover.jpg';
if($info['isbn']) $info['cover_image'] = $this->find_cover_image($info['isbn']);
$info['media'] = $this->translate_material(($spans[2]->first_child()->alt));
#bibid
if($bibid = $this->scrape_bibid($row->innertext)) { #what makes a valid bibid, anyway?
$info['bibid'] = $bibid;
}
# Everything after that is included in its own "extra" array
$info['extra'] = array_map("ptext", array_slice($spans, 4));
return $info;
}
protected function scrape_record($row) {
$info = array();
#title
$info['title'] = $row->find('span.briefcitTitle a', 0)->plaintext;
# grab author and publisher... (not standard on all catalogs?)
$stuff = $row->find('td.briefcitDetail', 0);
if($stuff = $row->find('td.briefcitDetail', 0)) {
if($citDetail = preg_split('/\n/', $stuff->plaintext, NULL, PREG_SPLIT_NO_EMPTY)){
if(isset($citDetail[1])) $info['author'] = strtr($citDetail[1]); // FIX: Non-standard characters cause results not to show at all.
if(isset($citDetail[2])) $info['publisher'] = strtr($citDetail[2]);
}
}
#search text in the row for (the first instance of) a valid isbn
if(preg_match('/[\dXx]{10,13}/', $row->innertext, $potential_isbns)) {
$info['isbn'] = $potential_isbns[0];
}
#cover_image
$info['cover_image'] = 'static/nocover.jpg';
if(isset($info['isbn'])) $info['cover_image'] = $this->find_cover_image($info['isbn']);
#bibid
if($bibid = $this->scrape_bibid($row->innertext)) { #what makes a valid bibid, anyway?
$info['bibid'] = $bibid;
}
else return NULL; # can't do much without a bibid
#how well do the below work on other catalogs?
#extra
if($bibitems = $row->find('tr.bibItemsEntry td'))
{
#location
$info['location'] = $this->xtrim($bibitems[0]->plaintext);
#callno
$info['callno'] = $this->xtrim($bibitems[1]->plaintext);
#status
$info['status'] = $this->xtrim($bibitems[2]->plaintext);
}
$row->clear();
unset($row);
return $info;
}
# trims withspace and html-code spaces
protected function xtrim($text) {
return trim(str_replace(' ', '', $text));
}
protected function scrape_bibid($rawhtml) {
preg_match('/b[\d]{6,}/', $rawhtml, $match);
return $match[0];
}
# returns set of arrays with [name] and [value] for each scope
function find_scopes($url) {
try {
# get page
$html = file_get_dom($url);
# gather <option> elements within <select [name|id="searchscope"]
$options = $html->find("select[name='searchscope'] option");
$scopes = array();
$default_scope = 0;
# iterate over <option>'s
foreach($options as $option) {
# assign innertext to scope array
array("name" => trim($option->innertext), "value" => $option->value);
array_push($scopes, array("name" => utf8_decode(trim($option->innertext)), "value" => $option->value));
# is it default scope?
if($option->selected == "selected") {
#assign to def_scope
$default_scope = $option->value;
}
}
array_push($scopes, $default_scope);
return $scopes; //$def_scope;
} catch (exception $e) {
# error; you must assign scopes manually
echo "exception $e";
}
}
protected function find_records($html) {
$method = $this->method_type;
# An array of arrays to hold the parsed info
$records = array();
if($method == 'mobileinfo') {
$rows = $html->find("span.mobileinfo");
foreach ($rows as $row) array_push($records, $this->parse_record($row));
}
elseif($method == 'scrape') {
#todo find out why there are two calls to scrape_record() in this conditional
# 02aug10: under construction
if($rows = $html->find(".briefCitRow")) { # how many different row types are there? BPL='.briefcitResultInfo'
foreach ($rows as $row) array_push($records, $this->scrape_record($row));
}
elseif($rows = $html->find(".briefcitResultInfo")) {
foreach ($rows as $row) array_push($records, $this->scrape_record($row));
}
}
return $records;
}
protected function shorten_value($val, $extra="") {
// Short circuit when this is undefined
if ($this->max_value_length == null) {
return $val;
}
# We have our $val that we can shorten, and possibly some extra
# text that must fit on the same line.
$extra_len = strlen($extra);
$val_len = strlen($val);
$total_len = $val_len + $extra_len;
# This is how much we must remove from the $val (if it's positive)
$remove_len = $total_len - $this->max_value_length;
if ($remove_len > 0) {
return substr($val, 0, $val_len - $remove_len - 3) . '...';
} else {
return $val;
}
}
# gathers bib info from individual record, names the array item after each td.bibInfoLabel and the data with the next cell in the row (.bibInfoData)
protected function find_bib_details($html) {
$info = array();
$tds = $html->find("table.bibDetail td.bibInfoLabel");
# Get all details from first table
foreach ($tds as $td) {
$label = trim($td->innertext);
$str = $td->next_sibling()->find("text"); # gather text from .bibInfoData
$str_text = Array();
foreach ($str as $value) {
$str_text[] = $value->plaintext;
}
$value = trim(join($str_text));
$info[$label] = $this->shorten_value($value, $label);
}
return $info;
}
protected function find_locs($html) {
$table = $html->find("table.bibItems", 0);
# If there's no location table, it's probably an electronic item, so
# return nothing
if (!$table) {
return array();
}
$locs = array();
$trs = $table->find("tr.bibItemsEntry");
foreach ($trs as $tr) {
$loc = array();
# Get all columns of the location table for this row
$tds = $tr->find("td");
# First two columns show the physical location and the call number
# in an <a> tag.
$loc['location'] = ptext($tds[$this->loc_td]);
$loc['call'] = ptext($tds[$this->call_td]);
# Third column shows availability status. Remove superfluous nbsp;
$loc['status'] = strtolower(ptext($tds[$this->status_td]));
if (in_array($loc['status'], $this->available_status)) {
$loc['available'] = true;
}
# Add to the list keyed by the location name
$locs[$loc['location']][] = $loc;
}
return $locs;
}
protected function process_link($a) {
$href = $a->href;
$text = $a->innertext;
return array($href, $this->shorten_value($text));
}
protected function find_links($html) {
$table = $html->find("table.bibLinks", 0);
# If no table, then this doesn't have any associated links
if (!$table) {
return array();
}
$links = array();
$anchors = $table->find("a");
foreach ($anchors as $a) {
list($href, $text) = $this->process_link($a);
array_push($links, array($href, $text));
}
return $links;
}
protected function find_orders($html) {
$rows = $html->find(".bibOrderEntry");
$orders = array();
foreach ($rows as $row) {
$orders[] = ptext($row);
}
return $orders;
}
protected function find_request_url($html) {
$url = $html->find("a[href*=\/request]", 0);
if ($url) {
return $url->href;
} else {
return null;
}
}
protected function get_browse_results($html) {
$navlinks = $this->find_nav_links($html);
$num = $this->find_num_results($html);
$results = $this->find_records($html);
# This special case is when the OPAC goes directly to the bib
# record because there is only one result
if(($num == 0) && ($this->one_entry_found($html))) {
$rec = $this->get_record_from_html($html);
$html->clear();
unset($html);
return array(
"oneres" => True,
"record" => $rec,
);
# The normal case of multiple (or zero) results
} else {
$html->clear();
unset($html);
return array(
"oneres" => False,
"results" => $results,
"navlinks" => $navlinks,
"num" => $num,
);
}
}
protected function one_entry_found($html) {
return $html->find("table.bibDetail");
}
protected function find_cover_image($isbn) {
return $this->base_url . "cover_image?isbn=$isbn";
}
protected function is_overdue($status) {
# due date status looks like "due mm-dd-yy"
if ('due ' == substr($status, 0, 4)) {
$ddate = substr($status, 10, 2) . substr($status, 4, 2) . substr($status, 7, 2);
$today = date('y').date('m').date('d');
if ($ddate - $today < 0) {
return true;
}
}
return false;
}
protected function find_checked_out_items($userid, $html) {
$items = array();
$rows = $html->find(".patFuncEntry");
foreach ($rows as $row) {
$item = array();
$cols = $row->children();
$cbox = $cols[0]->first_child();
$item['checkbox_name'] = $cbox->name;
$item['itemid'] = $cbox->value;
$item['title'] = ptext($cols[1]);
# ILL items will have anchor=null, no bibid, no full record.
$anchor = $cols[1]->first_child();
if ($anchor) {
$item['bibid'] = 'b' . ending_digits($anchor->href);
}
$item['barcode'] = ptext($cols[2]);
# use innertext to preserve the html colors of status
$item['status'] = trim(strtolower($cols[3]->innertext));
$item['overdue'] = $this->is_overdue($item['status']);
$item['call'] = ptext($cols[4]);
$query = http_build_query(array(
"renewsome" => "TRUE",
$cbox->name => $cbox->value,
"sortByDueDate" => "byduedate"
));
$item['renew_link'] = $this->catalog_url . '/patroninfo~S' . $this->def_scope . "/$userid/items?" . $query;
$items[] = $item;
}
return $items;
}
public function get_request_form($name, $code, $request_url) {
$login_resp = $this->login($name, $code);
$resp = http_parse_message(http_get(
$this->catalog_url . $request_url, array("cookies" => $login_resp['cookies'])
));
$html = str_get_dom($resp->body);
$loc_options = array();
if($select = $html->find("select[name=locx00]", 0)) {
$options_htmls = $select->find("option");
foreach ($options_htmls as $opt) {
$txt = ptext($opt);
if (!preg_match($this->invalid_request_locs, $txt)) {
$loc_options[$opt->value] = $txt;
}
}
}
return $loc_options;
}
// MOBIUS BEGIN
public function get_needby_month($name, $code, $request_url) {
$login_resp = $this->login($name, $code);
$resp = http_parse_message(http_get(
$this->catalog_url . $request_url, array("cookies" => $login_resp['cookies'])
));
$html = str_get_dom($resp->body);
$nbMonth = array();
if($select = $html->find("select[name=needby_Month]", 0)) {
$options_htmls = $select->find("option");
foreach ($options_htmls as $opt) {
$txt = ptext($opt);
if (!preg_match($this->invalid_request_locs, $txt)) {
$nbMonth[$opt->value] = $txt;
}
}
}
return $nbMonth;
}
public function get_needby_day($name, $code, $request_url) {
$login_resp = $this->login($name, $code);
$resp = http_parse_message(http_get(
$this->catalog_url . $request_url, array("cookies" => $login_resp['cookies'])
));
$html = str_get_dom($resp->body);
$nbDay = array();
if($select = $html->find("select[name=needby_Day]", 0)) {
$options_htmls = $select->find("option");
foreach ($options_htmls as $opt) {
$txt = ptext($opt);
if (!preg_match($this->invalid_request_locs, $txt)) {
$nbDay[$opt->value] = $txt;
}
}
}
return $nbDay;
}
public function get_needby_year($name, $code, $request_url) {
$login_resp = $this->login($name, $code);
$resp = http_parse_message(http_get(
$this->catalog_url . $request_url, array("cookies" => $login_resp['cookies'])
));
$html = str_get_dom($resp->body);
$nbYear = array();
if($select = $html->find("select[name=needby_Year]", 0)) {
$options_htmls = $select->find("option");
foreach ($options_htmls as $opt) {
$txt = ptext($opt);
if (!preg_match($this->invalid_request_locs, $txt)) {
$nbYear[$opt->value] = $txt;
}
}
}
return $nbYear;
}
// MOBIUS END
public function send_item_request($name, $code, $request_url, $formdata) {
$login_resp = $this->login($name, $code);
}
// Logs in and returns an array of cookie kvps
public function login($name, $code, $pin=null) {
$query = http_build_query(array(
"name" => $name,
"code" => $code,
"pin" => $pin,
));
#this doesn't work on catalogs that use a secure connection when accessing patron accounts; using https:// in $catalog_url solves this(?) at least for Albert
$login_url = $this->catalog_url . "/patroninfo/?$query";
$resp = http_parse_message(http_get($login_url));
# If there is any body in the response, we failed to login
if (strlen($resp->body) > 0) {
return null;
} else {
$cookies = cookie_strings_to_array($resp->headers['Set-Cookie']);
$loc = $resp->headers['Location'];
return array(
"userid" => get_userid_from_patron_loc($loc),
"cookies" => $cookies,
);
}
}
public function get_my_account_info($name, $code, $userid, $url=null) {
$login_resp = $this->login($name, $code);
# If renewing items, this url will be non-null
if ($url) {
$resp = http_parse_message(http_get(
"$url", array("cookies" => $login_resp['cookies'])
));
} else $url = $this->catalog_url . '/patroninfo~S' . $this->def_scope . "/$userid/items";
$args = array(
"sortByDueDate" => "byduedate"
);
$args_str = http_build_query($args);
$resp = http_parse_message(http_get(
"$url?$args_str", array("cookies" => $login_resp['cookies'])
));
$html = str_get_dom($resp->body);
# array of arrays for each checked out item, properties:
# checkbox_name (renew0, renew1, etc.)
# itemid (i123435)
# bibid (b234453)
# title
# barcode
# status
# overdue (bool)
# call
$items = $this->find_checked_out_items($userid, $html);
$holds = $this->find_holds($userid, $html, $login_resp['cookies']);
$fines = $this->find_fines($userid, $html, $login_resp['cookies']);
return array($items, $holds, $fines);
}
function find_fines($userid, $html, $cookie) {
$url = $this->catalog_url . '/patroninfo~S' . $this->def_scope . "/$userid/overdues";
$html = http_parse_message(http_get($url, array("cookies" => $cookie)));
$resp = str_get_dom($html->body);
$fines = $resp->find('td.patFuncFinesTotalAmt', 0)->plaintext;
return $fines;
}
public function find_holds($userid, $url, $cookie) {
$url = $this->catalog_url . '/patroninfo~S' . $this->def_scope . "/$userid/holds";
$holds_html = http_parse_message(http_get($url, array("cookies" => $cookie)));
$resp = str_get_dom($holds_html->body);
$rows = $resp->find('#hold_form tr.patFuncEntry');
foreach($rows as $row) {
$hold = array();
# these might need a utf8_decode(); albert does.
$hold['title'] = trim($row->find('.patFuncTitle', 0)->plaintext);
$hold['pickup'] = $row->find('.patFuncPickup', 0)->plaintext;
$hold['cancel_by_date'] = $row->find('.patFuncCancel', 0)->plaintext;
$hold['status'] = $row->find('.patFuncStatus', 0)->plaintext;
$ret = preg_match('/cancel(.*)/', $row->find('.patFuncMark input', 0)->name, $match);
$itemnum = end($match);
if ($ret) {
$hold['cancel_link'] = $this->catalog_url . '/patroninfo~S' . $this->def_scope . "/$userid/holds?" . "currentsortorder=current_pickup&loc$itemnum=&cancel$itemnum=on¤tsortorder=current_pickup&updateholdssome=yes";
}
else {
$hold['cancel_link'] = null;
}
$holds[] = $hold;
}
return $holds;
}
public function cancel_hold($id, $all=null) {
if(!$all) {
# cancel hold from $id
echo 'bang';
} else {
# cancel all holds
}
return null;
}
# Return all values defined in $detail_keys (above) in one dash-delimited string
protected function get_info_all($info) {
$vals = array();
foreach ($this->detail_keys as $key) {
if (ar_get($key, $info)) {
$vals[] = $info[$key];
}
}
return join(' - ', $vals);
}
protected function find_bibid($html) {
$anchor = $html->find('#recordnum', 0);
return $this->scrape_bibid($anchor->href);
}
protected function get_record_from_html($html, $bibid=null) {
# Basic info such as author, title, etc.
$info = $this->find_bib_details($html);
$info_all = $this->get_info_all($info);
if (empty($info)) {
throw new Exception("Invalid bibid");
}
# Book locations (if available)
$locs = $this->find_locs($html);
# Links to click on (if available)
$links = $this->find_links($html);
$orders = $this->find_orders($html);
# Request button
$request_url = $this->find_request_url($html);
$img = null;
if (($isbn = starting_digits(ar_get('ISBN', $info)))) {
$img = $this->find_cover_image($isbn);
} else {
$img = $this->base_url . "static/nocover.jpg"; #is this conditional redundant now? worth hunting to find out?
}
if (!$bibid) {
$bibid = $this->find_bibid($html);
}
return array(
"info" => $info,
"info_all" => $info_all,
"locations" => $locs,
"links" => $links,
"orders" => $orders,
"cover_image" => $img,
"bibid" => $bibid,
"request_url" => $request_url,
);
}
public function get_record($bibid) {
$html = file_get_dom(
$this->catalog_url . "/record=" . $bibid
);
return $this->get_record_from_html($html, $bibid);
}
public function get_next_browse($url) {
return $this->get_browse_results(file_get_dom($url));
}
public function get_browse($searcharg, $searchscope=null, $searchtype=null,$url=null) {
$searchscope = $searchscope ? $searchscope : $this->def_scope;
$searchtype = $searchtype ? $searchtype : $this->def_type;
return $this->get_browse_results(
file_get_dom(
$this->get_browse_url($searcharg, $searchscope, $searchtype)
)
);
}
protected function find_hidden_fields($html) {
$hiders = $html->find("input[type=hidden]");
$kvp = array();
foreach ($hiders as $hider) {
$kvp[$hider->name] = $hider->value;
}
return $kvp;
}
# Parse bibItemsEntry from a "request this item" page
protected function find_request_loc($html) {
$table = $html->find("table.bibItems", 0);
if (!$table) {
throw new Exception("Invalid request location.");
}
$trs = $table->find("tr.bibItemsEntry");
$locs = array();
foreach ($trs as $tr) {
$tds = $tr->find("td");
$loc = array();
$loc['itemid'] = $tds[0]->find("input[type=radio]", 0)->value;
$loc['location'] = ptext($tds[1]);
$loc['call'] = ptext($tds[2]);
$loc['status'] = strtolower(ptext($tds[3]));
$locs[] = $loc;
}
return $locs;
}
public function get_request_loc($name, $code, $ref, $post_params) {
$login_resp = $this->login($name, $code);
# We want to both submit to and use this URL as the referrer
$url = $this->catalog_url . $ref;
$body = http_request_body_encode($post_params, array());
$resp = http_parse_message(http_post_data(
$url, $body, array(
"cookies" => $login_resp['cookies'],
"referer" => $url,
)
));
$html = str_get_dom($resp->body);
$locs = $this->find_request_loc($html);
$hidden = $this->find_hidden_fields($html);
$resp = array(
"locs" => $locs,
"hidden" => $hidden,
);
return $resp;
}
public function submit_item_request($name, $code, $ref, $post_params) {
$login_resp = $this->login($name, $code);
# We want to both submit to and use this URL as the referrer
$url = $this->catalog_url . $ref;
$body = http_request_body_encode($post_params, array());
$resp = http_parse_message(http_post_data(
$url, $body, array(
"cookies" => $login_resp['cookies'],
"referer" => $url,
)
));
if (preg_match('/Your request for.*was successful/i', $resp->body)) {
return true;
} else {
throw new Exception("Unable to request item");
}
}
}
function concat ($v1, $v2) {
return $v1 . $v2;
}
function notblank ($v) {
return preg_match('/\w+/', $v);
}
function ptext ($v) {
return str_replace(" ", "", strip_tags(trim($v->plaintext)));
}
function starting_digits($v) {
return regex_or_null('/^\d+/', $v);
}
function ending_digits($v) {
return regex_or_null('/\d+$/', $v);
}
function regex_or_null($regex, $v) {
if (preg_match($regex, $v, $matches)) {
return $matches[0];
} else {
return null;
}
}
function get_loc_name($one_loc) {
return $one_loc['location'];
}
function cookie_strings_to_array($cookie_strings) {
$cookies = array();
foreach ($cookie_strings as $cookie) {
$cookie_obj = http_parse_cookie($cookie);
foreach ($cookie_obj->cookies as $k => $v) {
$cookies[$k] = $v;
}
}
return $cookies;
}
function get_userid_from_patron_loc($patron_loc) {
if (preg_match('/^\/patroninfo~S\d+\/(\d+)/', $patron_loc, $matches)) {
return $matches[1];
} else {
return null;
}
}
?>