-
Notifications
You must be signed in to change notification settings - Fork 4
/
rets.php
395 lines (328 loc) · 9.82 KB
/
rets.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
<?php
/**
* Created by PhpStorm.
* User: mbitson
* Date: 10/1/2014
* Time: 9:03 AM
*/
class RETS
{
/**
* Data to pass to template/views.
*
* @var array
*/
private $_data = array();
private $ch = array();
private $loggedIn = false;
/**
* Function to login to the RETS server.
*
* @return bool|SimpleXMLElement
*/
public function login()
{
// Validate cookie file
if (!file_exists(realpath($this->__get('cookie_file')))) touch($this->__get('cookie_file'));
// Init curl
$this->ch = curl_init();
// Configure curl
curl_setopt($this->ch, CURLOPT_URL, $this->__get('host').$this->__get('Login'));
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, realpath($this->__get('cookie_file')));
curl_setopt($this->ch, CURLOPT_TIMEOUT, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
if ($this->__get('force_basic_authentication') == true) {
curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
else {
curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
}
if ($this->__get('disable_follow_location') != true) {
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
}
// Set curl Login
curl_setopt($this->ch, CURLOPT_USERPWD, $this->__get('username').":".$this->__get('password'));
// Get result
$response = curl_exec($this->ch);
// If there is a result...
if($response)
{
// Convert return to xml element
$xml = new SimpleXMLElement($response);
// Get Login Response
if (isset($xml->{'RETS-RESPONSE'}))
{
// Get login response and break apart at new lines.
$login_response = explode("\r\n", $xml->{'RETS-RESPONSE'});
if (empty($login_response[3]))
{
$login_response = explode("\n", $xml->{'RETS-RESPONSE'});
}
// Parse each line to get the key and values into an array
$result = array();
foreach($login_response as &$entry){
$entry = explode('=', $entry);
if(isset($entry[0]) && $entry[0] && isset($entry[1]) && $entry[1])
{
$this->__set($entry[0], $entry[1]);
$result[$entry[0]] = $entry[1];
}
unset($entry);
}
// Tell us we're logged in!
$this->loggedIn = true;
// Set response to newly parsed result array
$login_response = $result;
}
// Else, failed login...
else
{
$login_response = $xml;
}
return $login_response;
}
// Else there is no result...
else
{
return FALSE;
}
}
public function search($type = NULL, $class = NULL, $query = NULL, $select = NULL, $restrictionDelimiting = false, $limit = NULL)
{
// Check for logged in status!
if(!$this->loggedIn){
return 'You must login before searching.';
}
// Check for Search URL & Host
if(!$this->__get('host') || !$this->__get('Search')){
return 'We do not yet know the SEARCH url from the server. Try logging in with login().';
}
// Else.. both host and search url are set.
else{
$request_url = $this->__get('Search').'?';
}
// Check for type
if(!is_null($type)){
$request_url .= 'SearchType='.$type.'&';
}else{
return "You must provide Search Type to search.";
}
// Check for Class
if(!is_null($class)){
$request_url .= 'Class='.$class.'&';
}else{
return "You must provide a class to search.";
}
// Check for Query
if(!is_null($query)){
$request_url .= 'QueryType=DMQL2&Query='.$query.'&';
}else{
return "You must provide a query to search.";
}
// Check for select statement
if(!is_null($select)){
$request_url .= 'Select='.$select.'&';
}
// Check for select statement
if($restrictionDelimiting){
$request_url .= 'RestrictedIndicator=****&';
}
// Check for Query
if(!is_null($limit)){
$request_url .= 'Limit='.$limit.'&';
}
// Remove the last character of string if it is a &
$request_url = trim($request_url, "&");
// Run the request!
$results = $this->simpleRequest($request_url);
// Return the results.
return $results;
}
public function getObject($type = NULL, $resource = NULL, $id = NULL, $location = NULL)
{
// Check for logged in status!
if(!$this->loggedIn){
return 'You must login before searching.';
}
// Check for Search URL & Host
if(!$this->__get('host') || !$this->__get('Search')){
return 'We do not yet know the SEARCH url from the server. Try logging in with login().';
}
// Else.. both host and search url are set.
else{
$request_url = $this->__get('GetObject').'?';
}
// Check for type
if(!is_null($type)){
$request_url .= 'Type='.$type.'&';
}else{
return "You must provide an Object Type to search.";
}
// Check for Class
if(!is_null($resource)){
$request_url .= 'Resource='.$resource.'&';
}else{
return "You must provide a resource to search.";
}
// Check for Query
if(!is_null($id)){
$request_url .= 'ID='.$id.'&';
}else{
return "You must provide an id to search.";
}
// Check for select statement
if(!is_null($location)){
$request_url .= 'Location='.$location.'&';
}
// Remove the last character of string if it is a &
$request_url = trim($request_url, "&");
// Run the request!
$results = $this->simpleRequest($request_url);
// Return the results.
return $results;
}
public function simpleRequest($url)
{
// Set full request url
$request_url = $this->__get('host').$url;
// Set new url of request
curl_setopt($this->ch, CURLOPT_URL, $request_url);
// Save this as the most recently accessed URL
$this->__set('last_request_url', $request_url);
// Tell curl to keep old session (logged in already)
curl_setopt($this->ch, CURLOPT_COOKIESESSION, false);
// Tell curl what cookie file to use
curl_setopt($this->ch, CURLOPT_COOKIEFILE, realpath($this->__get('cookie_file')));
// Get result
$response = curl_exec($this->ch);
// If there is a result...
if($response && strstr($response, '<?xml version'))
{
// Convert return to xml element
$xml = new SimpleXMLElement( $response );
// Save last result
$this->__set('last_request_result', $xml);
// Return result
return $xml;
}
// Else no results...
else
{
// Save last result
$this->__set('last_request_result', $response);
// Return result.
return $response;
}
}
public function parseImageHeaders($imageHeaders)
{
// Init final parsed array
$finalArray = array();
// Explode headers on new line.
$headerArray = explode("\r\n", $imageHeaders);
// Init first key
$arrayKey = 0;
// Loop through each line.
foreach($headerArray as $heading)
{
// Separate heading by value and key
$value = substr( $heading, ( $pos = strpos( $heading, ':' ) ) === false ? 0 : $pos + 1 );
$key = substr( $heading, 0, ( $pos = strpos( $heading, ':' ) ) === false ? 0 : $pos );
// If this key is the same as the first key, it's one loop!
if(isset($firstKey) && $firstKey === $key){
$arrayKey++;
}
// If first key isn't set....
if(!isset($firstKey))
{
// Find the first key in array.
$firstKey = $key;
}
// If we have a value and a key...
if(isset($value) && $value && isset($key) && $key)
{
// Add trimmed info from this heading to final array.
$finalArray[$arrayKey][trim($key)] = trim($value);
}
}
// Return formatted results!
return $finalArray;
}
public function formatForNexes($object, $agentId = NULL)
{
// Init results
$results = array();
// Generic Post Data
$results['title'] = $object->Listing->StreetAddress->StreetNumber.' '.$object->Listing->StreetAddress->StreetName.' '.$object->Listing->StreetAddress->StreetSuffix.', '.$object->Listing->StreetAddress->City.', '.$object->Listing->StreetAddress->StateOrProvince;
$results['content'] = (string) $object->Listing->ListingData->PublicRemarks;
$results['postType'] = 'property';
// Add meta data
$results['meta']['pyre_status'] = 'For Sale';
$results['meta']['pyre_full_address'] = (string) $results['title'];
$results['meta']['pyre_address'] = (string) $object->Listing->StreetAddress->StreetNumber.' '.$object->Listing->StreetAddress->StreetName.' '.$object->Listing->StreetAddress->StreetSuffix;
$results['meta']['pyre_city'] = (string) $object->Listing->StreetAddress->City;
$results['meta']['pyre_state'] = (string) $object->Listing->StreetAddress->StateOrProvince;
$results['meta']['pyre_zip'] = (string) $object->Listing->StreetAddress->PostalCode;
$results['meta']['pyre_price'] = (string) $object->Listing->ListingData->ListPrice;
$results['meta']['pyre_built'] = (string) $object->YearBuilt;
$results['meta']['pyre_bathrooms'] = (string) $object->Baths->BathsTotal;
$results['meta']['pyre_bedrooms'] = (string) $object->Bedrooms;
$results['meta']['mls_id'] = (string) $object->Listing->ListingID;
// Add agents if specified
if(!is_null($agentId))
{
// Agent passed, add to meta
$results['meta']['pyre_agent'] = $agentId;
}
// Tell system no agent was passed if not passed
else
{
// Set no agent meta.
$results['meta']['pyre_no_agent'] = 'TRUE';
}
// Return results.
return $results;
}
/**
* Set overload method for template class
*
* @param string $name Name of property to set
* @param string $value Value to set property to
*
* @return void
*/
public function __set($name, $value)
{
// Set property
$this->_data[$name] = $value;
}
/**
* Get overload method for template class
*
* @param string $name Name of property to get
*
* @return mixed
*/
public function __get($name)
{
// If a name is passed...
if(isset($name) && $name)
{
// Check for property
if (array_key_exists($name, $this->_data))
{
return $this->_data[$name];
}
}
// No name was passed...
else
{
// return all data
return $this->_data;
}
// Return no results
return NULL;
}
}