forked from troydavisson/PHRETS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphrets.php
1623 lines (1426 loc) · 51.2 KB
/
phrets.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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PHRETS - PHP library for RETS
* Copyright 2012 Circle Tree, LLC.
* Based on:
* http://troda.com/projects/phrets/
* Copyright (C) 2007-2011 Troy Davisson
*
* This library is divided into 2 sections: high level and low level
* High level: Helpful functions that take much of the burden out of processing RETS data
* Low level: Framework for communicating with a RETS server. High level functions sit on top of these
*
*/
/**
* Parent Class Exception
*/
class phRETSExceptionsClass extends Exception {
function __construct ($message, $code = null) {
parent::__construct($message, $code);
}
}
/**
* runtime / configuration exceptions
*/
class phRETSException extends Exception {
}
/**
* Server exceptions
*/
class retsException extends Exception {
}
class retsXMLParsingException extends Exception {
}
if (! class_exists("phRETS")) :
class phRETS {
private $service_urls = array();
private $curl_handle = false;
private $server_hostname;
private $server_port;
private $server_protocol;
private $server_version;
private $server_software;
private $search_data;
private $static_headers = array();
private $server_information = array();
private $cookie_file = "";
private $debug_file = "rets_debug.txt";
private $debug_file_handle = false;
private $debug_mode = FALSE;
private $allowed_capabilities = array(
"Action" => 1,
"ChangePassword" => 1,
"GetObject" => 1,
"Login" => 1,
"LoginComplete" => 1,
"Logout" => 1,
"Search" => 1,
"GetMetadata" => 1,
"ServerInformation" => 1,
"Update" => 1,
"PostObject" => 1,
"GetPayloadList" => 1
);
private $last_request = array();
private $auth_support_basic = false;
private $auth_support_digest = false;
private $last_response_headers = array();
private $last_response_body = "";
private $last_response_headers_raw = "";
private $last_remembered_header = "";
private $compression_enabled = false;
private $ua_pwd = "";
private $ua_auth = false;
private $request_id = "";
private $disable_follow_location = false;
private $force_basic_authentication = false;
private $use_interealty_ua_auth = false;
private $int_result_pointer = 0;
private $last_request_url;
private $session_id;
private $catch_last_response = false;
private $disable_encoding_fix = false;
private $offset_support = false;
private $override_offset_protection = false;
private $is_connected = false;
/**
* Stores messages regarding firewall test results
* @var array
*/
private $firewall_messages = array();
//login
private $username;
private $password;
private $login_url;
private $xml;
/**
* required php modules / capabilites
* @var array
*/
private static $test_requirements = array('curl_init' , 'simplexml_load_string');
public function is_connected() {
return $this->is_connected;
}
public function __construct($login_url, $username, $password, $ua_pwd = "") {
$this->username = $username;
$this->password = $password;
$this->login_url = $login_url;
// chop up Login URL to use for later requests
$url_parts = parse_url($login_url);
$this->server_hostname = $url_parts['host'];
$this->server_port = ( empty($url_parts['port']) ) ? 80 : $url_parts['port'];
$this->server_protocol = $url_parts['scheme'];
$this->service_urls['Login'] = $url_parts['path'];
if (! empty($ua_pwd) ) {
// force use of RETS 1.7 User-Agent Authentication
$this->ua_auth = true;
$this->ua_pwd = $ua_pwd;
}
}
/**
* connect to the remote server, and log in
*/
public function Connect() {
if ( $this->is_connected() )
return;
if (empty($this->static_headers['RETS-Version'])) {
$this->AddHeader("RETS-Version", "RETS/1.5");
}
if (empty($this->static_headers['User-Agent'])) {
$this->AddHeader("User-Agent", "PHRETS/1.0");
}
if (empty($this->static_headers['Accept']) && $this->static_headers['RETS-Version'] == "RETS/1.5") {
$this->AddHeader("Accept", "*/*");
}
//Append query parms if set
if (isset($url_parts['query']) && ! empty($url_parts['query']) ) {
$this->service_urls['Login'] .= "?{$url_parts['query']}";
}
if (empty($this->cookie_file)) {
$this->cookie_file = tempnam("", "phrets");
}
if (!is_writable($this->cookie_file)) {
throw new phRETSException("Cookie file \"{$this->cookie_file}\" cannot be written to.
Must be an absolute path and must be writable");
}
@touch($this->cookie_file);
$this->initialize_curl();
// make request to Login transaction
$this->RETSRequest('Login');
$this->ParseXMLResponse($this->last_response_body);
$this->save_last_request();
// chop up login response
// if multiple parts of the login response aren't found splitting on \r\n, redo using just \n
$login_response = array();
if ($this->is_server_version('1_0')) {
//@codeCoverageIgnoreStart
//@todo test on rets 1.0
if (isset($this->xml)) {
$login_response = explode("\r\n", $this->xml);
if (empty($login_response[3])) {
$login_response = explode("\n", $this->xml);
}
}
} else {
//@codeCoverageIgnoreEnd
if (isset($this->xml->{'RETS-RESPONSE'})) {
$login_response = explode("\r\n", $this->xml->{'RETS-RESPONSE'});
if (empty($login_response[3])) {
$login_response = explode("\n", $this->xml->{'RETS-RESPONSE'});
}
}
}
// parse login response. grab all capability URLs known and ones that begin with X-
// otherwise, it's a piece of server information to save for reference
foreach ($login_response as $line) {
$name = $value = null;
if (strpos($line, '=') !== false) {
@list($name,$value) = explode("=", $line, 2);
}
$name = trim($name);
$value = trim($value);
if (!empty($name) && !empty($value)) {
if (isset($this->allowed_capabilities[$name]) || preg_match('/^X\-/', $name) == true) {
$this->service_urls[$name] = $value;
} else {
$this->server_information[$name] = $value;
}
}
}
// if 'Action' capability URL is provided, we MUST request it following the successful Login
if (isset($this->service_urls['Action']) && !empty($this->service_urls['Action'])) {
$this->is_connected = true;
$this->RETSRequest($this->service_urls['Action']);
}
if ($this->last_request['ReplyCode'] == 0) {
$this->is_connected = true;
return true;
} else {
throw new retsException($this->last_request['ReplyText'], $this->last_request['ReplyCode']);
}
}
/**
* initialize $this->curl_handle
*/
private function initialize_curl() {
//check if cURL is already initialized
if (is_resource($this->curl_handle))
return;
//Initialize
$this->curl_handle = curl_init();
$curl_options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 0,
CURLOPT_COOKIEFILE => $this->cookie_file,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $this->username.":".$this->password,
CURLOPT_HEADERFUNCTION => array( $this, 'read_custom_curl_headers'),
);
if ($this->disable_follow_location != true || $this->compression_enabled == true)
array_push($curl_options, array(CURLOPT_ENCODING => "gzip"));
if ($this->disable_follow_location != true)
array_push($curl_options, array(CURLOPT_FOLLOWLOCATION => 1));
if ($this->force_basic_authentication == true)
array_push($curl_options, array(CURLOPT_HTTPAUTH => CURLAUTH_BASIC));
else
array_push($curl_options, array(CURLOPT_HTTPAUTH => CURLAUTH_DIGEST|CURLAUTH_BASIC));
if ($this->debug_mode == true) {
$this->debug_file_handle = @fopen($this->debug_file, 'a');
if (is_resource( $this->debug_file_handle) ) {
array_push($curl_options, array(CURLOPT_VERBOSE => true ));
array_push($curl_options, array(CURLOPT_STDERR => $this->debug_file ));
} else {
throw new retsException("Unable to save debug log to {$this->debug_file}");
}
}
curl_setopt_array($this->curl_handle, $curl_options);
}
/**
* @codeCoverageIgnore
* @deprecated use test_firewall
*/
public function FirewallTest() {
trigger_error(__METHOD__ . ' is Deprecated. Use phRETS::test_firewall()', E_USER_DEPRECATED);
$this->test_firewall();
}
/**
* Tests internet connectivity
* @return bool true on success, false on failure
*/
public function test_firewall () {
$google = $this->do_firewall_test_connection("http://www.google.com/");
$crt80 = $this->do_firewall_test_connection("http://demo.crt.realtors.org/");
$dis6103 = $this->do_firewall_test_connection("http://dis.com:6103/rets/");
$flexmls80 = $this->do_firewall_test_connection("http://retsgw.flexmls.com/");
$flexmls6103 = $this->do_firewall_test_connection("http://retsgw.flexmls.com:6103/");
//we ignore the polymorphisms here because they are too difficult to test
//@codeCoverageIgnoreStart
if (!$google && !$crt80 && !$dis6103 && !$flexmls80 && !$flexmls6103) {
$msg = "Firewall Result: All tests failed. Possible causes:";
$msg .= "<ol>";
$msg .= "<li>Firewall is blocking your outbound connections</li>";
$msg .= "<li>You aren't connected to the internet</li>";
$msg .= "</ol>";
array_unshift($this->firewall_messages, $msg);
return false;
}
if (!$dis6103 && !$flexmls6103) {
$msg = "Firewall Result: All port 6103 tests failed. ";
$msg .= "Likely cause: Firewall is blocking your outbound connections on port 6103.";
array_unshift($this->firewall_messages, $msg);
return false;
}
if ($google && $dis6103 && $crt80 && $flexmls6103 && $flexmls80) {
$msg = "Firewall Result: All tests passed.";
array_unshift($this->firewall_messages, $msg);
return true;
}
if (($dis6103 && !$flexmls6103) || (!$dis6103 && $flexmls6103)) {
$msg = "Firewall Result: At least one port 6103 test passed. ";
$msg .= "Likely cause: One of the test servers might be down but connections on port 80 and port 6103 should work.";
array_unshift($this->firewall_messages, $msg);
return true;
}
if (!$google || !$crt80 || !$flexmls80) {
$msg = "Firewall Result: At least one port 80 test failed. ";
$msg .= "Likely cause: One of the test servers might be down.";
array_unshift($this->firewall_messages, $msg);
return true;
}
$msg = "Firewall Test Failure: Unable to guess the issue.";
array_unshift($this->firewall_messages, $msg);
return false;
//@codeCoverageIgnoreEnd
}
private static function test_required_capabilities() {
$return = array();
foreach (self::$test_requirements as $requirement) {
if ( function_exists($requirement) ) {
$return[ $requirement ] = true;
} else {
$return[ $requirement ] = false;
}
}
return $return;
}
/**
* @return bool true if met, false if not.
* use get_test_requirements to see failed
*/
public static function test_requirements_met () {
if (false === array_search(false, self::test_required_capabilities())) {
return true;
} else {
//@codeCoverageIgnoreStart
return false;
//@codeCoverageIgnoreEnd
}
}
/**
* gets test statuses
* @return array function name => true/false
*/
public static function get_test_requirements () {
return self::test_required_capabilities();
}
/**
* gets firewall messages, running a test if the messages array is empty
* @return array messages of successes / failures
*/
public function get_firewall_messages() {
if ( 0 === count($this->firewall_messages) ) {
$this->test_firewall();
}
return $this->firewall_messages;
}
private function do_firewall_test_connection($hostname) {
curl_setopt($this->curl_handle, CURLOPT_URL, $hostname);
curl_exec($this->curl_handle);
$response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
if ($response_code == 200 || $response_code == 304 || $response_code == 403) {
$this->firewall_messages[] = "Firewall Test: {$hostname} GOOD";
return true;
} else {
$this->firewall_messages[] = "Firewall Test: {$hostname} FAILED. HTTP Status Code: ".$response_code;
return false;
}
}
/**
* Get RETS Objects (typically photos)
* @param string $resource RETS Resource for requested object
* @param string $type RETS GetObject type. See GetMetadataObjects for available types.
* @param mixed $id ID of Object. This is the value of the KeyName field within the Resource for your record (typically the MLS#). This is NOT the full ID as described in the RETS specification.
* @param int $photo_number Optional. Requested object ID. Typically represents the photo order. Possible Values: 0, 1, 2, 3, etc., or * (asterisk) to request all objects. Default is *
* @param bool $location Optional. Used to return URLs rather than image data. Not always supported by the server. True gets URLs, False gets binary image data. Default is False.
* @return boolean|multitype:boolean multitype:string boolean unknown
*/
public function GetObject($resource, $type, $id, $photo_number = '*', $location = FALSE) {
$return_photos = array();
$send_id = "";
$send_numb = "";
// check if $photo_number needs fixing
if (strpos($photo_number, ',') !== false) {
// change the commas to colons for the request
$photo_number = preg_replace('/\,/', ':', $photo_number);
}
if (strpos($photo_number, ':') !== false) {
// photo number contains multiple objects
// chopping and cleaning
$requested_numbers = explode(":", $photo_number);
if (is_array($requested_numbers)) {
foreach ($requested_numbers as $numb) {
$numb = trim($numb);
if (!empty($numb) || $numb == "0") {
$send_numb .= "{$numb}:";
}
}
}
$send_numb = preg_replace('/\:$/', '', $send_numb);
}
else {
$send_numb = trim($photo_number);
}
if (strpos($id, ',') !== false) {
// id contains multiple objects.
// chopping and combining with photo_number
$requested_ids = explode(",", $id);
if (is_array($requested_ids)) {
foreach ($requested_ids as $req_id) {
$req_id = trim($req_id);
if (!empty($req_id) && $req_id != "0") {
$send_id .= "{$req_id}:{$send_numb},";
}
}
}
$send_id = preg_replace('/\,$/', '', $send_id);
} else {
$send_id = trim($id).':'.$send_numb;
}
// make request
$location_int = $location ? 1 : 0;
$result = $this->RETSRequest('GetObject',
array(
'Resource' => $resource,
'Type' => $type,
'ID' => $send_id,
'Location' => $location_int
)
);
// fix case issue if exists
if (isset($this->last_response_headers['Content-type']) && !isset($this->last_response_headers['Content-Type'])) {
$this->last_response_headers['Content-Type'] = $this->last_response_headers['Content-type'];
}
if (!isset($this->last_response_headers['Content-Type'])) {
$this->last_response_headers['Content-Type'] = "";
}
// check what type of response came back
if (strpos($this->last_response_headers['Content-Type'], 'multipart') !== false) {
// help bad responses be more multipart compliant
$this->last_response_body = "\r\n{$this->last_response_body}\r\n";
// multipart
preg_match('/boundary\=\"(.*?)\"/', $this->last_response_headers['Content-Type'], $matches);
if (isset($matches[1])) {
$boundary = $matches[1];
} else {
preg_match('/boundary\=(.*?)(\s|$|\;)/', $this->last_response_headers['Content-Type'], $matches);
$boundary = $matches[1];
}
// strip quotes off of the boundary
$boundary = preg_replace('/^\"(.*?)\"$/', '\1', $boundary);
// clean up the body to remove a reamble and epilogue
$this->last_response_body = preg_replace('/^(.*?)\r\n--'.$boundary.'\r\n/', "\r\n--{$boundary}\r\n", $this->last_response_body);
// make the last one look like the rest for easier parsing
$this->last_response_body = preg_replace('/\r\n--'.$boundary.'--/', "\r\n--{$boundary}\r\n", $this->last_response_body);
// cut up the message
$multi_parts = array();
$multi_parts = explode("\r\n--{$boundary}\r\n", $this->last_response_body);
// take off anything that happens before the first boundary (the preamble)
array_shift($multi_parts);
// take off anything after the last boundary (the epilogue)
array_pop($multi_parts);
// go through each part of the multipart message
foreach ($multi_parts as $part) {
// default to processing headers
$on_headers = true;
$on_body = false;
$first_body_found = false;
$this_photo = array();
// go through the multipart chunk line-by-line
$body_parts = array();
$body_parts = explode("\r\n", $part);
$this_photo['Data'] = "";
foreach ($body_parts as $line) {
if (empty($line) && $on_headers == true) {
// blank line. switching to processing a body and moving on
$on_headers = false;
$on_body = true;
continue;
}
if ($on_headers == true) {
// non blank line and we're processing headers so save the header
$header = null;
$value = null;
if (strpos($line, ':') !== false) {
@list($header, $value) = explode(':', $line, 2);
}
$header = trim($header);
$value = trim($value);
if (!empty($header)) {
if ($header == "Description") {
// for servers where the implementors didn't read the next word in the RETS spec.
// 'Description' is the BNF term. Content-Description is the correct header.
// fixing for sanity
$header = "Content-Description";
}
// fix case issue if exists
if ($header == "Content-type") {
$header = "Content-Type";
}
$this_photo[$header] = $value;
}
}
if ($on_body == true) {
if ($first_body_found == true) {
// here again because a linebreak in the body section which was cut out in the explode
// add the CRLF back
$this_photo['Data'] .= "\r\n";
}
// non blank line and we're processing a body so save the line as part of Data
$first_body_found = true;
$this_photo['Data'] .= $line;
}
}
// done with parsing out the multipart response
// check for errors and finish up
$this_photo['Success'] = true; // assuming for now
if (strpos($this_photo['Content-Type'], 'xml') !== false) {
// this multipart might include a RETS error
$this->ParseXMLResponse($this_photo['Data']);
if ($this->xml['ReplyCode'] == 0 || empty($this_photo['Data'])) {
// success but no body
$this_photo['Success'] = true;
} else {
// RETS error in this multipart section
$this_photo['Success'] = false;
$this_photo['ReplyCode'] = "{$this->xml['ReplyCode']}";
$this_photo['ReplyText'] = "{$this->xml['ReplyText']}";
}
}
// add information about this multipart to the returned array
$return_photos[] = $this_photo;
}
} else {
// all we know is that the response wasn't a multipart so it's either a single photo or error
$this_photo = array();
$this_photo['Success'] = true; // assuming for now
if (isset($this->last_response_headers['Content-ID'])) {
$this_photo['Content-ID'] = $this->last_response_headers['Content-ID'];
}
if (isset($this->last_response_headers['Object-ID'])) {
$this_photo['Object-ID'] = $this->last_response_headers['Object-ID'];
}
if (isset($this->last_response_headers['Content-Type'])) {
$this_photo['Content-Type'] = $this->last_response_headers['Content-Type'];
}
if (isset($this->last_response_headers['MIME-Version'])) {
$this_photo['MIME-Version'] = $this->last_response_headers['MIME-Version'];
}
if (isset($this->last_response_headers['Location'])) {
$this_photo['Location'] = $this->last_response_headers['Location'];
}
if (isset($this->last_response_headers['Preferred'])) {
$this_photo['Preferred'] = $this->last_response_headers['Preferred'];
}
if (isset($this->last_response_headers['Description'])) {
if (!empty($this->last_response_headers['Description'])) {
// for servers where the implementors didn't read the next word in the RETS spec.
// 'Description' is the BNF term. Content-Description is the correct header.
// fixing for sanity
$this_photo['Content-Description'] = $this->last_response_headers['Description'];
}
}
if (isset($this->last_response_headers['Content-Description'])) {
$this_photo['Content-Description'] = $this->last_response_headers['Content-Description'];
}
$this_photo['Length'] = strlen($this->last_response_body);
$this_photo['Data'] = $this->last_response_body;
if (isset($this->last_response_headers['Content-Type'])) {
if (strpos($this->last_response_headers['Content-Type'], 'xml') !== false) {
// RETS error maybe?
$this->ParseXMLResponse($this->last_response_body);
if ($this->xml['ReplyCode'] == 0 || empty($body)) {
// false alarm. we're good
$this_photo['Success'] = true;
} else {
// yes, RETS error
$this_photo['ReplyCode'] = "{$this->xml['ReplyCode']}";
$this_photo['ReplyText'] = "{$this->xml['ReplyText']}";
$this_photo['Success'] = false;
}
}
}
// add information about this photo to the returned array
$return_photos[] = $this_photo;
}
// return everything
return $return_photos;
}
/**
* does the response contain a maxrows element?
* @see Rets 1.7.2::7.4.3 Limit
* @param bool true if there are more rows
*/
public function IsMaxrowsReached($pointer_id = "") {
if (empty($pointer_id)) {
$pointer_id = $this->int_result_pointer;
}
if (isset($this->search_data)) {
return $this->search_data[$pointer_id]['maxrows_reached'];
} else {
return false;
}
}
/**
* gets the total number of records returned by the last search
* @param unknown_type $pointer_id
*/
public function getTotalRecordsFound($pointer_id = "") {
if (empty($pointer_id)) {
$pointer_id = $this->int_result_pointer;
}
if ( isset( $this->search_data )) {
return $this->search_data[$pointer_id]['total_records_found'];
} else {
return false;
}
}
/**
* gets the number of results returned by the last search
* @param int $pointer_id
*
*/
public function getNumRows($pointer_id = "") {
if (empty($pointer_id)) {
$pointer_id = $this->int_result_pointer;
}
if (isset($this->search_data)) {
return $this->search_data[$pointer_id]['last_search_returned'];
} else {
return false;
}
}
public function SearchGetFields($pointer_id) {
if (! empty($pointer_id) ) {
if (isset($this->search_data[ $pointer_id ])) {
return $this->search_data[$pointer_id]['column_names'];
} else {
return false;
}
} else {
return false;
}
}
public function FreeResult($pointer_id) {
if (!empty($pointer_id) && isset($this->search_data[$pointer_id])) {
unset($this->search_data[$pointer_id]['data']);
unset($this->search_data[$pointer_id]['delimiter_character']);
unset($this->search_data[$pointer_id]['column_names']);
return true;
} else {
return false;
}
}
public function FetchRow($pointer_id) {
if (empty($pointer_id) || (! isset( $this->search_data[$pointer_id] ))) {
return false;
}
$this_row = false;
if (isset($this->search_data[$pointer_id]['data'])) {
$field_data = current($this->search_data[$pointer_id]['data']);
next($this->search_data[$pointer_id]['data']);
}
if ( !empty($field_data) ) {
$this_row = array();
// split up DATA row on delimiter found earlier
$field_data = preg_replace("/^{$this->search_data[$pointer_id]['delimiter_character']}/", "", $field_data);
$field_data = preg_replace("/{$this->search_data[$pointer_id]['delimiter_character']}\$/", "", $field_data);
$field_data = explode($this->search_data[$pointer_id]['delimiter_character'], $field_data);
foreach ($this->search_data[$pointer_id]['column_names'] as $key => $name) {
// assign each value to it's name retrieved in the COLUMNS earlier
$this_row[$name] = $field_data[$key];
}
}
return $this_row;
}
/**
*
* @param array $query key value pairs to search
* @return string $query_string DMQL formatted query
*/
public function PrepareQuery(array $query) {
$query_string = "";
foreach ($query as $id=>$val) {
$query_string .= "($id=$val),";
}
return rtrim($query_string, ',');
}
/**
* @param string $resource RETS resource (Property,Agent, etc.)
* @param string $class RETS class id to query
* @param string $query DMQL query string
* @param array $optional_params array of RETS
* @return int internal result pointer ID
* @todo update $query param to being optional with rets 1.8.0
*/
public function SearchQuery($resource, $class, $query, $optional_params = array()) {
//increment results pointer
$this->int_result_pointer++;
$this->search_data[$this->int_result_pointer]['last_search_returned'] = 0;
$this->search_data[$this->int_result_pointer]['total_records_found'] = 0;
$this->search_data[$this->int_result_pointer]['column_names'] = "";
$this->search_data[$this->int_result_pointer]['delimiter_character'] = "";
$this->search_data[$this->int_result_pointer]['search_requests'] = 0;
// setup default request arguments
$default_arguments = array(
'QueryType' => "DMQL2",
'SearchType' => $resource,
'Class' => $class,
'Count' => 1,
'Format' => "COMPACT-DECODED",
'Limit' => 99999999,
'StandardNames' => 0,
'Select' => null,
'RestrictedIndicator' => '*************'
);
// setup additional, optional request arguments
$search_arguments = array_merge($default_arguments, $optional_params);
if ($query == "*" || preg_match('/^\((.*)\)$/', $query)) {
// check if the query passed is missing the outer parenthesis
$search_arguments['Query'] = $query;
} else {
// if so, add them
$search_arguments['Query'] = '('.$query.')';
}
if (isset($optional_params['Offset'])) {
$search_arguments['Offset'] = $optional_params['Offset'];
} elseif ($this->offset_support && empty($optional_params['Offset'])) {
// start auto-offset looping with Offset at 1
$search_arguments['Offset'] = 1;
}
// Keep searching if MAX ROWS is reached and offset_support is true
$continue_searching = true;
while ($continue_searching) {
$this->search_data[$this->int_result_pointer]['maxrows_reached'] = false;
$this->search_data[$this->int_result_pointer]['search_requests']++;
if ($this->search_data[$this->int_result_pointer]['search_requests'] == 300 && !$this->override_offset_protection) {
// this call for SearchQuery() has resulted in X number of search requests
// which is considered excessive. stopping the process in order to prevent
// abuse against the server. almost ALWAYS happens when the user thinks Offset
// is supported by the server when it's actually NOT supported
throw phRETSException(
"Last SearchQuery() has resulted in 300+ requests to the server.
Stopping to prevent abuse"
);
}
// make request
$this->RETSRequest('Search', $search_arguments);
$body = $this->fix_encoding($this->last_response_body);
$this->ParseXMLResponse($body);
if (isset($this->xml->DELIMITER)) {
// delimiter found so we have at least a COLUMNS row to parse
$delimiter_character = chr("{$this->xml->DELIMITER->attributes()->value}");
$this->search_data[$this->int_result_pointer]['delimiter_character'] = $delimiter_character;
$column_names = "{$this->xml->COLUMNS[0]}";
$column_names = preg_replace("/^{$delimiter_character}/", "", $column_names);
$column_names = preg_replace("/{$delimiter_character}\$/", "", $column_names);
$this->search_data[$this->int_result_pointer]['column_names'] = explode($delimiter_character, $column_names);
}
if (isset($this->xml->DATA)) {
foreach ($this->xml->DATA as $key) {
$field_data = "{$key}";
// split up DATA row on delimiter found earlier
$this->search_data[$this->int_result_pointer]['data'][] = $field_data;
$this->search_data[$this->int_result_pointer]['last_search_returned']++;
}
}
if (isset($this->xml->MAXROWS)) {
// MAXROWS tag found. the RETS server withheld records.
// if the server supports Offset, more requests can be sent to page through results
// until this tag isn't found anymore.
$this->search_data[$this->int_result_pointer]['maxrows_reached'] = true;
}
if (isset($this->xml->COUNT)) {
// found the record count returned. save it
$this->search_data[$this->int_result_pointer]['total_records_found'] = "{$this->xml->COUNT->attributes()->Records}";
}
if ($this->IsMaxrowsReached($this->int_result_pointer) && $this->offset_support) {
$continue_searching = true;
$search_arguments['Offset'] = $this->NumRows($this->int_result_pointer) + 1;
} else {
$continue_searching = false;
}
}
return $this->int_result_pointer;
}
/**
* Returns data datatable of results
* @see phRETS::SearchQuery()
* @return array $data[] = row
*/
public function Search($resource, $class, $query = "", $optional_params = array()) {
$int_result_pointer = $this->SearchQuery($resource, $class, $query, $optional_params);
$data_table = array();
while ($row = $this->FetchRow($int_result_pointer)) {
$data_table[] = $row;
}
return $data_table;
}
public function getLastSearchId() {
return $this->int_result_pointer == 0 ? false : $this->int_result_pointer;
}
/**
* Gets all of the lookup values for the selected resource
* @param string $resource Resource string (Property, User, Etc.)
* @return array $values
*/
public function GetAllLookupValues($resource) {
$this->RETSRequest( 'GetMetadata',
array(
'Type' => 'METADATA-LOOKUP_TYPE',
'ID' => $resource.':*',
'Format' => 'STANDARD-XML'
)
);
$this->ParseXMLResponse($this->last_response_body);
$this_table = array();
if ($this->xml->METADATA && $this->xml->METADATA->{'METADATA-LOOKUP_TYPE'}) {
foreach ($this->xml->METADATA->{'METADATA-LOOKUP_TYPE'} as $key) {
if (!empty($key->attributes()->Lookup)) {
$this_lookup = array();
$lookup_xml_array = array();
if ($this->is_server_version('1_7_or_above')) {
$lookup_xml_array = $key->LookupType;
} else {
$lookup_xml_array = $key->Lookup;
}
if (is_object($lookup_xml_array)) {
foreach ($lookup_xml_array as $look) {
$metadataentryid = isset($look->MetadataEntryID) ? "{$look->MetadataEntryID}" : "";
$value = isset($look->Value) ? "{$look->Value}" : "";
$shortvalue = isset($look->ShortValue) ? "{$look->ShortValue}" : "";
$longvalue = isset($look->LongValue) ? "{$look->LongValue}" : "";
$this_lookup[] = array(
'MetadataEntryID' => $metadataentryid,
'Value' => $value,
'ShortValue' => $shortvalue,
'LongValue' => $longvalue
);
}
}
$this_table[] = array('Lookup' => "{$key->attributes()->Lookup}", 'Values' => $this_lookup);
}
}
}
// return the big array
return $this_table;
}
/**
* Get values for an individual type (WARNING - does an API call for each LookupName)
* @TODO refactor this to cache the results to limit API calls for loops
* Maybe add a third param $cache = true, where the interface will allow a dev
* to specify that it should do an 'ID' => resource:* call and cache the result
* for use in subsequent lookup value requests
* @param string $resource Class name
* @param string $lookupname metadata LookupName
*/
public function GetLookupValues($resource, $lookupname) {
$this->RETSRequest('GetMetadata',
array(
'Type' => 'METADATA-LOOKUP_TYPE',
'ID' => $resource.':'.$lookupname,
'Format' => 'STANDARD-XML'
)
);
$this->ParseXMLResponse($this->last_response_body);
$this_table = array();
// parse XML into a nice array
if ($this->xml->METADATA && $this->xml->METADATA->{'METADATA-LOOKUP_TYPE'}) {
$lookup_xml_array = array();
if ($this->is_server_version('1_7_or_above')) {
$lookup_xml_array = $this->xml->METADATA->{'METADATA-LOOKUP_TYPE'}->LookupType;
}
else {
$lookup_xml_array = $this->xml->METADATA->{'METADATA-LOOKUP_TYPE'}->Lookup;
}
if (is_object($lookup_xml_array)) {
foreach ($lookup_xml_array as $key) {
if (isset($key->Value)) {
$metadataentryid = isset($key->MetadataEntryID) ? "{$key->MetadataEntryID}" : "";
$value = isset($key->Value) ? "{$key->Value}" : "";
$shortvalue = isset($key->ShortValue) ? "{$key->ShortValue}" : "";
$longvalue = isset($key->LongValue) ? "{$key->LongValue}" : "";
$this_table[] = array(
'MetadataEntryID' => $metadataentryid,
'Value' => $value,
'ShortValue' => $shortvalue,
'LongValue' => $longvalue
);
}
}
}
}
// return the big array
return $this_table;
}
/**
* Gets all resources
* @param string $id Optional resource type string (Property, User, Office, Etc.). Omit for all.
* @return array
*/
public function GetMetadataResources($id = 0) {
// make request
$result = $this->RETSRequest('GetMetadata',
array(
'Type' => 'METADATA-RESOURCE',
'ID' => $id,
'Format' => 'STANDARD-XML'
)