-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannelmanagement_rentalsunited_communication.class.php
189 lines (148 loc) · 5.63 KB
/
channelmanagement_rentalsunited_communication.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
<?php
/**
* Jomres CMS Agnostic Plugin
* @author Woollyinwales IT <sales@jomres.net>
* @version Jomres 9
* @package Jomres
* @copyright 2019 Woollyinwales IT
* Jomres (tm) PHP files are released under both MIT and GPL2 licenses. This means that you can choose the license that best suits your project.
**/
// ################################################################
defined( '_JOMRES_INITCHECK' ) or die( '' );
// ################################################################
require_once('XMLParser.php');
use XMLParser\XMLParser;
class channelmanagement_rentalsunited_communication
{
function __construct()
{
$this->url = 'http://rm.rentalsunited.com/api/Handler.ashx';
}
/*
*/
public function communicate( $method = '' , $xml_str = '' , $clear_cache = false )
{
// Webhook events will use this method, but we don't (?) want to cache the messages so we'll not cache them
$method_can_be_cached = true;
jr_import('channelmanagement_rentalsunited_push_event_trigger_crossref');
$event_trigger_crossref = new channelmanagement_rentalsunited_push_event_trigger_crossref();
foreach ( $event_trigger_crossref->events as $event_type ) {
if ( in_array( $method , $event_type )) {
$method_can_be_cached = false;
}
}
if ($method == 'Pull_ListPropertiesChangeLog_RQ' ) {
$method_can_be_cached = false;
}
if ($clear_cache == true ) {
$method_can_be_cached = false;
}
if ( $method_can_be_cached ) {
$data_hash = md5(serialize($xml_str));
$filename = $method."_".$data_hash.".php";
if (!is_dir(JOMRES_TEMP_ABSPATH."cm_ru_data_cache")) {
mkdir(JOMRES_TEMP_ABSPATH."cm_ru_data_cache");
}
if (file_exists( JOMRES_TEMP_ABSPATH."cm_ru_data_cache".JRDS.$filename )) {
require_once(JOMRES_TEMP_ABSPATH."cm_ru_data_cache".JRDS.$filename);
$class_name = $method."_".$data_hash;
$ru_data_cache = new $class_name();
return unserialize($ru_data_cache->data);
}
}
try {
$uri = $this->url;
$xml_str = preg_replace('/^\h*\v+/m', '', $xml_str);
$client = new GuzzleHttp\Client(['timeout' => 6, 'connect_timeout' => 6]);
logging::log_message('Starting guzzle call to '.$uri, 'RENTALS_UNITED', 'DEBUG');
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml_str,
'debug' => false
];
$response = $client->request('POST', $uri, $options);
}
catch (Exception $e) {
echo "Failed to get response from channel
";
var_dump($e->getMessage());exit;
logging::log_message("Failed to get response from channel manager. Message ".$e->getMessage(), 'RENTALS_UNITED', 'ERROR' , "rentalsunited" );
return false;
}
if (!isset($response)) {
return false;
}
$raw_response = (string)$response->getBody();
if ($raw_response == '<error ID="-4">Incorrect login or password</error>') {
throw new Exception( "Incorrect login or password" );
}
$response_body = new SimpleXMLElement($raw_response);
$contents = $this->xmlToArray($response_body );
// Check to see if the response contains _RS, which means that it's a response to a push method. If so we will process it differently
$response_method = str_replace( "_RQ" , "_RS" , $method);
// There's a typo in RU's responses, Pull_GetLocatinByName_RS is returned when it should say Pull_GetLocationByName_RS, so we'll make a special exception and hard-code a fix here
if ($method == "Pull_GetLocationByName_RQ") {
if (!isset($contents[$response_method])) { // In case they ever fix the typo
$response_method = "Pull_GetLocatinByName_RS";
}
}
if ( isset($contents[$response_method])) {
if ( $method_can_be_cached ) {
$sanitised_apostrophes = str_replace("'", "'", $contents[$response_method]);
$cache_data = "<?php
defined(\"_JOMRES_INITCHECK\" ) or die( \"\" );
class " . $method . "_" . $data_hash . "
{
public function __construct()
{
\$this->method = '" . $method . "';
\$this->data = '" . serialize($sanitised_apostrophes) . "';
}
}
";
file_put_contents(JOMRES_TEMP_ABSPATH . "cm_ru_data_cache" . JRDS . $filename, $cache_data);
return $sanitised_apostrophes;
} else {
return $contents[$response_method];
}
} else {
}
}
function xmlToArray(SimpleXMLElement $xml)
{
$parser = function (SimpleXMLElement $xml, array $collection = []) use (&$parser) {
$nodes = $xml->children();
$attributes = $xml->attributes();
if (0 !== count($attributes)) {
foreach ($attributes as $attrName => $attrValue) {
$collection['@attributes'][$attrName] = strval($attrValue);
}
}
if (0 === $nodes->count()) {
if($xml->attributes())
{
$collection['value'] = strval($xml);
}
else
{
$collection = strval($xml);
}
return $collection;
}
foreach ($nodes as $nodeName => $nodeValue) {
set_time_limit(0);
if (count($nodeValue->xpath('../' . $nodeName)) < 2) {
$collection[$nodeName] = $parser($nodeValue);
continue;
}
$collection[$nodeName][] = $parser($nodeValue);
}
return $collection;
};
return [
$xml->getName() => $parser($xml)
];
}
}