-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiHomefinderRequestor.php
More file actions
327 lines (276 loc) · 11.8 KB
/
iHomefinderRequestor.php
File metadata and controls
327 lines (276 loc) · 11.8 KB
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
<?php
if( !class_exists('IHomefinderRequestor')){
class IHomefinderRequestor{
public static function remoteRequest( $ihfUrl, $ajaxRequest=false ){
IHomefinderLogger::getInstance()->debug("Begin IHomefinderRequestor.remoteRequest: " );
//We don't try to get subscriber information for ajax requests
//because of cookie related complications.
if( !strpos(strtolower($ihfUrl), "subscriberid=") ){
$subscriber = IHomefinderStateManager::getInstance()->getCurrentSubscriber();
if( !is_null($subscriber) && '' != $subscriber){
$subscriberId=$subscriber->getId();
IHomefinderLogger::getInstance()->debug('subscriberId: ' . $subscriberId );
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "subscriberId", $subscriberId );
}
}
if( !strpos(strtolower($ihfUrl), "jsessionid=") ){
$ihfSessionId=IHomefinderStateManager::getInstance()->getIhfSessionId();
$ihfUrl=str_replace( IHomefinderLayoutManager::getInstance()->getExternalUrl(), IHomefinderLayoutManager::getInstance()->getExternalUrl() . ";jsessionid=" . $ihfSessionId, $ihfUrl );
}
//If the url does not have the lead capture id then try to add it
$ihfUrlHasLeadCapture=strrpos($ihfUrl, "leadCaptureId=");
if($ihfUrlHasLeadCapture == false){
$leadCaptureId = IHomefinderStateManager::getInstance()->getLeadCaptureId();
IHomefinderLogger::getInstance()->debug("leadCaptureId=" . $leadCaptureId );
if( !is_null($leadCaptureId) && '' != $leadCaptureId){
IHomefinderLogger::getInstance()->debug('leadCaptureId: ' . $leadCaptureId );
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "leadCaptureId", $leadCaptureId );
}
}
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "version", IHomefinderConstants::VERSION );
$userAgent=$_SERVER['HTTP_USER_AGENT'];
if( $userAgent != null ){
$userAgent=urlencode($userAgent);
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "uagent", $userAgent ) ;
}
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "loadJQuery", "false" ) ;
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "leadCaptureSupport", "true" ) ;
//if rememberme cookie is set then append variable to url
if(isSet($_COOKIE["ihf_rmuser"])){
$ihfUrl = IHomefinderRequestor::appendQueryVarIfNotEmpty($ihfUrl, "rmuser", "true" ) ;
}
IHomefinderLogger::getInstance()->debug("ihfUrl: " . $ihfUrl);
//if( $ajaxRequest ){echo( $ihfUrl );die();}
//echo( $ihfUrl ); die();
$ihfid=site_url() + ";" + "WordpressPlugin";
$ihfUserInfo= 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' );
//modified user-agent in the request header to pass original user-agent
//This information is used by spring-mobile library to determine
//if request came from mobile devices
//This can also be acheived by using is_mobile wordpress function
//user-agent information that wordpress provides is now added to
//ihfuserinfo variable
$requestArgs = array("timeout"=>"200", "ihfid"=> $ihfid,"ihfUserInfo"=> $ihfUserInfo,"user-agent"=> $userAgent);
IHomefinderLogger::getInstance()->debug("before request");
$response = wp_remote_get($ihfUrl, $requestArgs);
IHomefinderLogger::getInstance()->debug("after request");
if( is_wp_error($response)){
$contentInfo=null;
} else {
if( $response['response']['code'] >= 400 ) {
$responseBody = wp_remote_retrieve_body( $response );
$contentInfo = new stdClass();
$contentInfo->view = $responseBody;
} else {
$responseBody = wp_remote_retrieve_body( $response );
IHomefinderLogger::getInstance()->debug('responseBody: ' . $responseBody );
try{
$contentType=wp_remote_retrieve_header($response, "content-type");
//$ihfSessionId=wp_remote_retrieve_header($response, "ihfSessionId");
if( $contentType != null && $contentType == "text/xml;charset=UTF-8"){
$contentInfo=simplexml_load_string($responseBody);
}
else{
$contentInfo=json_decode($responseBody);
}
}catch (Exception $e){
var_dump($e);
}
}
}
//if( $ajaxRequest ){var_dump($contentInfo);die();}
IHomefinderLogger::getInstance()->debug("after get body");
//Save the leadCaptureId, if we get it back.
if( isset( $contentInfo->leadCaptureId ) && !empty( $contentInfo->leadCaptureId )){
IHomefinderLogger::getInstance()->debug("calling saveLeadCaptureId with leadCaptureId=" . $contentInfo->leadCaptureId);
IHomefinderStateManager::getInstance()->saveLeadCaptureId($contentInfo->leadCaptureId);
}
if( isset( $contentInfo->ihfSessionId ) ){
IHomefinderStateManager::getInstance()->saveIhfSessionId($contentInfo->ihfSessionId);
}
if( isset( $contentInfo->searchContext ) ){
IHomefinderStateManager::getInstance()->setSearchContext($contentInfo->searchContext);
}
if( isset( $contentInfo->listingInfo ) ){
$listingInfo=$contentInfo->listingInfo;
$listingNumber="";
$listingAddress="";
$boardId="";
$clientPropertyId="";
$sold="false";
$hasListingInfo=false;
if( isset( $listingInfo->listingNumber ) && isset( $listingInfo->boardId ) ){
$listingNumber=$listingInfo->listingNumber ;
$boardId=$listingInfo->boardId ;
$hasListingInfo=true;
if( isset( $listingInfo->clientPropertyId ) ){
$clientPropertyId=$listingInfo->clientPropertyId ;
}
if( isset( $listingInfo->listingAddress ) ){
$listingAddress=$listingInfo->listingAddress ;
}
if( isset( $listingInfo->sold ) ){
$sold=$listingInfo->sold ;
}
$listingInfo=
new iHomefinderListingInfo($listingNumber, $boardId, $listingAddress, $clientPropertyId, $sold );
IHomefinderStateManager::getInstance()->setCurrentListingInfo($listingInfo);
}
}
if( !IHomefinderRequestor::isError($contentInfo) && isset( $contentInfo->subscriberInfo )){
$subscriberData=$contentInfo->subscriberInfo ;
$subscriberInfo=IHomefinderSubscriber::getInstance($subscriberData->subscriberId,$subscriberData->name, $subscriberData->email );
IHomefinderStateManager::getInstance()->saveSubscriberLogin($subscriberInfo);
}
if( !IHomefinderRequestor::isError($contentInfo) && isset( $contentInfo->searchSummary )){
$searchSummary=$contentInfo->searchSummary ;
IHomefinderStateManager::getInstance()->saveSearchSummary($searchSummary);
}
IHomefinderRequestor::loadJavaScriptAndCss($contentInfo);
IHomefinderLogger::getInstance()->debug("End IHomefinderRequestor.remoteRequest: " );
return $contentInfo ;
}
/**
*
* Enqueue CSS and JavaScript if included in the contentInfo
* @param unknown_type $contentInfo
*/
private static function loadJavaScriptAndCss( $contentInfo ){
if( isset($contentInfo->css )){
$cssList=$contentInfo->css;
foreach ($cssList->item as $item) {
wp_enqueue_style($item->name );
}
}
if( isset($contentInfo->javascript )){
$javascriptList=$contentInfo->javascript;
foreach ($javascriptList->item as $item) {
$name=$item->name;
$url=$item->url;
$depends=false;
if( isset($item->depends)){
$depends=array();
foreach( $item->depends as $oneDependency ){
array_push($depends, $oneDependency);
}
}
//We now register all scripts.
wp_enqueue_script($name);
}
}
return;
}
public static function remotePostRequest( $ihfUrl, $postData ){
IHomefinderLogger::getInstance()->debug("Begin IHomefinderRequestor.remoteRequest: " );
IHomefinderLogger::getInstance()->debug("ihfUrl: " . $ihfUrl);
$ihfid=site_url() + ";" + "WordpressPlugin";
$ihfUserInfo= 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' );
//modified user-agent in the request header to pass original user-agent
//This information is used by spring-mobile library to determine
//if request came from mobile devices
//This can also be acheived by using is_mobile wordpress function
//user-agent information that wordpress provides is now added to
//ihfuserinfo variable
$requestArgs = array('timeout'=>'200', 'body'=>$postData, "ihfid"=> $ihfid,"ihfUserInfo"=> $ihfUserInfo,"user-agent"=> $userAgent );
$response = wp_remote_post($ihfUrl, $requestArgs);
IHomefinderLogger::getInstance()->debug("IHomefinderRequestor.remoteRequest post data " );
IHomefinderLogger::getInstance()->debugDumpVar($postData);
IHomefinderLogger::getInstance()->debugDumpVar($response);
if( is_wp_error($response)){
$contentInfo=null;
} else {
if( $response['response']['code'] >= 400 ) {
$responseBody = wp_remote_retrieve_body( $response );
$contentInfo = new stdClass();
$contentInfo->view = $responseBody;
} else {
$responseBody = wp_remote_retrieve_body( $response );
$contentType=wp_remote_retrieve_header($response, "content-type");
if( $contentType != null && $contentType == "text/xml;charset=UTF-8"){
$contentInfo=simplexml_load_string($responseBody);
}
else{
$contentInfo=json_decode($responseBody);
}
}
IHomefinderLogger::getInstance()->debugDumpVar($responseBody);
}
IHomefinderLogger::getInstance()->debug("IHomefinderRequestor.remoteRequest response " );
IHomefinderLogger::getInstance()->debug("End IHomefinderRequestor.remoteRequest: " );
return $contentInfo ;
}
public static function appendQueryVarIfNotEmpty( $ihfUrl, $queryVarName, $queryVarValue){
if(isset($queryVarValue, $queryVarName )){
$queryVarValue=urlencode($queryVarValue);
$trimmedValue=trim($queryVarValue);
if( '' != $trimmedValue ){
if( strpos( $ihfUrl, "?")){
$ihfUrl = $ihfUrl . "&" . $queryVarName . "=" . $trimmedValue ;
} else {
$ihfUrl = $ihfUrl . "?" . $queryVarName . "=" . $trimmedValue ;
}
}
}
return $ihfUrl ;
}
public static function isError($contentInfo){
$result=false;
if(is_null($contentInfo) || property_exists($contentInfo, "error")){
$result=true;
}
return $result ;
}
/**
*
* Extract the content from the response.
* @param $contentInfo
*/
public static function getContent($contentInfo){
$content='';
if(is_null($contentInfo)){
//We could reach this code, if the iHomefinder services are down.
$content = "<br/>Sorry we are experiencing system issues. Please try again.<br/>";
}
else if (property_exists($contentInfo, "error")){
//Report the error from iHomefinder
$content = "<br/>" . $contentInfo->error . "</br/>";
}
else if( property_exists($contentInfo, "view")){
//success, display the view
$content = $contentInfo->view ;
}
return $content ;
}
/**
*
* Extract JSON from the response for ajax requests.
* @param $contentInfo
*/
public static function getJson($contentInfo){
$json='';
if( property_exists($contentInfo, "json")){
//success, return the json
$json = $contentInfo->json ;
}
return $json ;
}
public static function addVarsToUrl($url, $arrayOfVars){
foreach($arrayOfVars as $key=>$val) {
$paramValue=null;
if( is_array($val)){
foreach( $val as $value ){
if( $paramValue != null ){
$paramValue .= ",";
}
$paramValue .= $value;
}
} else {
$paramValue=$val;
}
$url = iHomefinderRequestor::appendQueryVarIfNotEmpty($url, $key, $paramValue );
}
return $url ;
}
}
}
?>