| 1 |
<?php |
| 2 |
|
| 3 |
if (! defined('ABSPATH')) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
class iHomefinderRequestor |
| 9 |
{ |
| 10 |
|
| 11 |
private $parameters = array(); |
| 12 |
private $cacheExpiration = 0; |
| 13 |
private $remoteResponse; |
| 14 |
|
| 15 |
private $logger; |
| 16 |
private $displayRules; |
| 17 |
private $utility; |
| 18 |
private $stateManager; |
| 19 |
private $enqueueResource; |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->logger = iHomefinderLogger::getInstance(); |
| 24 |
$this->displayRules = iHomefinderDisplayRules::getInstance(); |
| 25 |
$this->utility = iHomefinderUtility::getInstance(); |
| 26 |
$this->stateManager = iHomefinderStateManager::getInstance(); |
| 27 |
$this->enqueueResource = iHomefinderEnqueueResource::getInstance(); |
| 28 |
} |
| 29 |
|
| 30 |
public function remoteGetRequest() |
| 31 |
{ |
| 32 |
|
| 33 |
if ($this->remoteResponse === null) { |
| 34 |
if ($this->stateManager->isErrorMode()) { |
| 35 |
$responseBodyObject = null; |
| 36 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 37 |
$this->remoteResponse->setBody("The server is temporarily unavailable due to maintenance or high demand."); |
| 38 |
$this->enqueueResource->setHttpStatusCode(503); |
| 39 |
$this->enqueueResource->addHttpHeader("Retry-After: " . iHomefinderStateManager::ERROR_MODE_TIMEOUT); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if ($this->remoteResponse === null) { |
| 44 |
if ($this->isSpam()) { |
| 45 |
wp_die("invalid request 807a"); |
| 46 |
//$result = "responceBodyGoesHere"; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if ($this->remoteResponse === null) { |
| 51 |
$requestUrl = $this->getExternalUrl(); |
| 52 |
|
| 53 |
//only add user specific info if the request is not cacheable |
| 54 |
if (!$this->isCacheable()) { |
| 55 |
//add jsession id to the end of the url |
| 56 |
if ($this->stateManager->hasSessionId()) { |
| 57 |
$sessionId = $this->stateManager->getSessionId(); |
| 58 |
$requestUrl .= ";jsessionid=" . $sessionId; |
| 59 |
} |
| 60 |
|
| 61 |
//add subscriber id to the reqest parameters |
| 62 |
if ($this->stateManager->hasSubscriberId()) { |
| 63 |
$subscriberId = $this->stateManager->getSubscriberId(); |
| 64 |
//CF cannot handle multiple parameters with the same case insensitive name |
| 65 |
$this->removeParameter("subscriberId")->removeParameter("subscriberID"); |
| 66 |
$this->addParameter("subscriberId", $subscriberId); |
| 67 |
} |
| 68 |
|
| 69 |
//add lead capture id to the reqest parameters |
| 70 |
if ($this->stateManager->hasLeadCaptureUserId()) { |
| 71 |
$leadCaptureUserId = $this->stateManager->getLeadCaptureUserId(); |
| 72 |
$this->addParameter("leadCaptureId", $leadCaptureUserId); |
| 73 |
} |
| 74 |
|
| 75 |
//if remember me cookie add it to the reqest parameters |
| 76 |
if ($this->stateManager->hasRememberMe()) { |
| 77 |
$this->addParameter("rmuser", true); |
| 78 |
} |
| 79 |
|
| 80 |
//add user agent to the request parameters |
| 81 |
if ($this->stateManager->hasUserAgent()) { |
| 82 |
$userAgent = $this->stateManager->getUserAgent(); |
| 83 |
$this->addParameter("uagent", $userAgent); |
| 84 |
} |
| 85 |
|
| 86 |
//add IP of user to the request parameters |
| 87 |
$ipAddress = $this->stateManager->getUserIpAddress(); |
| 88 |
$this->addParameter("ipAddress", $ipAddress); |
| 89 |
} |
| 90 |
|
| 91 |
//add authentication token to the reqest parameters |
| 92 |
$authenticationToken = iHomefinderAdmin::getInstance()->getAuthenticationToken(); |
| 93 |
$this->addParameter("authenticationToken", $authenticationToken); |
| 94 |
|
| 95 |
$this->addParameter("version", iHomefinderConstants::VERSION); |
| 96 |
|
| 97 |
$this->addParameter("leadCaptureSupport", true); |
| 98 |
$this->addParameter("phpStyle", true); |
| 99 |
|
| 100 |
$requestUrl = $this->utility->buildUrl($requestUrl, $this->getParameters()); |
| 101 |
|
| 102 |
$ihfid = iHomefinderUrlFactory::getInstance()->getBaseUrl() . ";" . "WordpressPlugin"; |
| 103 |
$ihfUserInfo = "WordPress/" . get_bloginfo("version") . "; " . iHomefinderUrlFactory::getInstance()->getBaseUrl(); |
| 104 |
//Modified user-agent in the request header to pass original user-agent. This information is used to determine if request came from mobile devices. |
| 105 |
|
| 106 |
$requestArgs = array( |
| 107 |
"timeout" => "35", |
| 108 |
"ihfid" => $ihfid, |
| 109 |
"ihfUserInfo" => $ihfUserInfo, |
| 110 |
"sslverify" => false, |
| 111 |
); |
| 112 |
|
| 113 |
if (isset($userAgent)) { |
| 114 |
$requestArgs["user-agent"] = $userAgent; |
| 115 |
} |
| 116 |
|
| 117 |
$response = null; |
| 118 |
if ($this->isCacheable()) { |
| 119 |
$response = iHomefinderCacheUtility::getInstance()->getItem($requestUrl); |
| 120 |
} |
| 121 |
if ($response === null) { |
| 122 |
$this->logger->debug("ihfUrl: " . $requestUrl); |
| 123 |
$this->logger->debug($requestArgs); |
| 124 |
$this->logger->debug("before request"); |
| 125 |
|
| 126 |
$response = wp_remote_get($requestUrl, $requestArgs); |
| 127 |
|
| 128 |
$this->logger->debug("after request"); |
| 129 |
$this->logger->debug($response); |
| 130 |
|
| 131 |
if (!is_wp_error($response) && $this->isCacheable() && $response["response"]["code"] < 400) { |
| 132 |
iHomefinderCacheUtility::getInstance()->updateItem($requestUrl, $response, $this->getCacheExpiration()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (is_wp_error($response)) { |
| 137 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 138 |
$this->remoteResponse->setBody("<br />This content is currently unavailable. Please check back later or contact the site's support team for more information.<br />"); |
| 139 |
$this->enqueueResource->setHttpStatusCode(500); |
| 140 |
} |
| 141 |
|
| 142 |
if (!is_wp_error($response)) { |
| 143 |
$responseCode = $response["response"]["code"]; |
| 144 |
|
| 145 |
if ($this->stateManager->isErrorForTimeout($responseCode)) { |
| 146 |
$this->stateManager->addErrorTime(); |
| 147 |
} |
| 148 |
|
| 149 |
if ($responseCode == 404) { |
| 150 |
global $wp_query; |
| 151 |
$wp_query->set_404(); |
| 152 |
status_header(404); |
| 153 |
nocache_headers(); |
| 154 |
} |
| 155 |
|
| 156 |
if ($responseCode >= 500) { |
| 157 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 158 |
$this->enqueueResource->setHttpStatusCode($responseCode); |
| 159 |
} |
| 160 |
|
| 161 |
if ($responseCode < 500) { |
| 162 |
$responseBody = wp_remote_retrieve_body($response); |
| 163 |
$contentType = wp_remote_retrieve_header($response, "content-type"); |
| 164 |
$responseBodyObject = null; |
| 165 |
if ($contentType == "text/xml;charset=UTF-8") { |
| 166 |
$responseBodyObject = simplexml_load_string($responseBody, null, LIBXML_NOCDATA); |
| 167 |
} else { |
| 168 |
$responseBodyObject = json_decode($responseBody); |
| 169 |
} |
| 170 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 171 |
$this->remoteResponse->setResponse($responseBodyObject); |
| 172 |
$this->enqueueResource->setHttpStatusCode($responseCode); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (is_object($this->remoteResponse) && !$this->remoteResponse->hasError() && !$this->isCacheable()) { |
| 177 |
if ($this->remoteResponse->hasLeadCaptureUserId()) { |
| 178 |
$this->stateManager->setLeadCaptureUserId($this->remoteResponse->getLeadCaptureUserId()); |
| 179 |
} |
| 180 |
|
| 181 |
if ($this->remoteResponse->hasSessionId()) { |
| 182 |
$sessionId = $this->remoteResponse->getSessionId(); |
| 183 |
$this->stateManager->setSessionId($sessionId); |
| 184 |
} |
| 185 |
|
| 186 |
if ($this->remoteResponse->hasListingInfo()) { |
| 187 |
$listingInfo = $this->remoteResponse->getListingInfo(); |
| 188 |
$listingNumber = null; |
| 189 |
$listingAddress = null; |
| 190 |
$boardId = null; |
| 191 |
$clientPropertyId = null; |
| 192 |
$sold = false; |
| 193 |
if (property_exists($listingInfo, "listingNumber") && property_exists($listingInfo, "boardId")) { |
| 194 |
$listingNumber = $listingInfo->listingNumber; |
| 195 |
$boardId = $listingInfo->boardId; |
| 196 |
if (property_exists($listingInfo, "clientPropertyId")) { |
| 197 |
$clientPropertyId = $listingInfo->clientPropertyId; |
| 198 |
} |
| 199 |
if (property_exists($listingInfo, "listingAddress")) { |
| 200 |
$listingAddress = $listingInfo->listingAddress; |
| 201 |
} |
| 202 |
if (property_exists($listingInfo, "sold")) { |
| 203 |
$sold = $listingInfo->sold; |
| 204 |
} |
| 205 |
$listingInfo = new iHomefinderListingInfo($listingNumber, $boardId, $listingAddress, $clientPropertyId, $sold); |
| 206 |
$this->stateManager->setListingInfo($listingInfo); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ($this->remoteResponse->hasSubscriberId()) { |
| 211 |
$subscriberId = $this->remoteResponse->getSubscriberId(); |
| 212 |
$this->stateManager->setSubscriberId($subscriberId); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// as a last resort, return an instance of iHomefinderRemoteResponse |
| 218 |
if (!is_object($this->remoteResponse)) { |
| 219 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 220 |
} |
| 221 |
|
| 222 |
return $this->remoteResponse; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Only used for registration |
| 227 |
* |
| 228 |
* @return iHomefinderRemoteResponse |
| 229 |
*/ |
| 230 |
public function remotePostRequest() |
| 231 |
{ |
| 232 |
|
| 233 |
$requestUrl = $this->getExternalUrl(); |
| 234 |
$ihfid = iHomefinderUrlFactory::getInstance()->getBaseUrl() . ";" . "WordpressPlugin"; |
| 235 |
$ihfUserInfo = "WordPress/" . get_bloginfo("version") . "; " . iHomefinderUrlFactory::getInstance()->getBaseUrl(); |
| 236 |
// Modified user-agent in the request header to pass original user-agent. This information is used to determine if request came from mobile devices. |
| 237 |
|
| 238 |
$userAgent = $_SERVER["HTTP_USER_AGENT"]; |
| 239 |
if ($userAgent !== null) { |
| 240 |
$userAgent = urlencode($userAgent); |
| 241 |
} |
| 242 |
|
| 243 |
$this->addParameter("version", iHomefinderConstants::VERSION); |
| 244 |
$this->addParameter("method", "handleRequest"); |
| 245 |
$this->addParameter("viewType", "json"); |
| 246 |
$this->addParameter("phpStyle", true); |
| 247 |
|
| 248 |
$requestArgs = array( |
| 249 |
"timeout" => "200", |
| 250 |
"body" => $this->getParameters(), |
| 251 |
"ihfid" => $ihfid, |
| 252 |
"ihfUserInfo" => $ihfUserInfo, |
| 253 |
"user-agent" => $userAgent, |
| 254 |
"sslverify" => false, |
| 255 |
); |
| 256 |
|
| 257 |
$this->logger->debug("ihfUrl: " . $requestUrl); |
| 258 |
$this->logger->debug($requestArgs); |
| 259 |
$this->logger->debug("before request"); |
| 260 |
$response = wp_remote_post($requestUrl, $requestArgs); |
| 261 |
$this->logger->debug("after request"); |
| 262 |
$this->logger->debug($response); |
| 263 |
|
| 264 |
$responseBodyObject = null; |
| 265 |
if (is_wp_error($response)) { |
| 266 |
$this->logger->debug("remotePostRequest failed: " . $response->get_error_message()); |
| 267 |
} else { |
| 268 |
$responseBody = wp_remote_retrieve_body($response); |
| 269 |
if ($response["response"]["code"] >= 400) { |
| 270 |
$responseBodyObject = new stdClass(); |
| 271 |
$responseBodyObject->view = $responseBody; |
| 272 |
} else { |
| 273 |
$contentType = wp_remote_retrieve_header($response, "content-type"); |
| 274 |
if ($contentType !== null && $contentType == "text/xml;charset=UTF-8") { |
| 275 |
$responseBodyObject = simplexml_load_string($responseBody, null, LIBXML_NOCDATA); |
| 276 |
$responseBodyObject = json_decode(json_encode($responseBodyObject)); |
| 277 |
} else { |
| 278 |
$responseBodyObject = json_decode($responseBody); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Always hand back a response object. Callers read the parsed body through it, and |
| 284 |
// a transport failure is reported as an absent body rather than as a null return. |
| 285 |
$this->remoteResponse = new iHomefinderRemoteResponse(); |
| 286 |
$this->remoteResponse->setResponse($responseBodyObject); |
| 287 |
|
| 288 |
return $this->remoteResponse; |
| 289 |
} |
| 290 |
|
| 291 |
public function hasParameter($name) |
| 292 |
{ |
| 293 |
$result = false; |
| 294 |
if (array_key_exists($name, $this->parameters)) { |
| 295 |
$result = true; |
| 296 |
} |
| 297 |
return $result; |
| 298 |
} |
| 299 |
|
| 300 |
public function addParameter($name, $value) |
| 301 |
{ |
| 302 |
$this->parameters[$name] = $value; |
| 303 |
return $this; |
| 304 |
} |
| 305 |
|
| 306 |
public function removeParameter($name) |
| 307 |
{ |
| 308 |
if ($this->hasParameter($name)) { |
| 309 |
unset($this->parameters[$name]); |
| 310 |
} |
| 311 |
return $this; |
| 312 |
} |
| 313 |
|
| 314 |
public function addParameters($parameters) |
| 315 |
{ |
| 316 |
if (is_array($parameters)) { |
| 317 |
foreach ($parameters as $name => $value) { |
| 318 |
$this->addParameter($name, $value); |
| 319 |
} |
| 320 |
} |
| 321 |
return $this; |
| 322 |
} |
| 323 |
|
| 324 |
public function getParameters() |
| 325 |
{ |
| 326 |
return $this->parameters; |
| 327 |
} |
| 328 |
|
| 329 |
public function setCacheExpiration($cacheExpiration) |
| 330 |
{ |
| 331 |
$this->cacheExpiration = $cacheExpiration; |
| 332 |
} |
| 333 |
|
| 334 |
public function getCacheExpiration() |
| 335 |
{ |
| 336 |
return $this->cacheExpiration; |
| 337 |
} |
| 338 |
|
| 339 |
public function isCacheable() |
| 340 |
{ |
| 341 |
$result = false; |
| 342 |
if (is_int($this->cacheExpiration) && $this->cacheExpiration > 0) { |
| 343 |
$result = true; |
| 344 |
} |
| 345 |
return $result; |
| 346 |
} |
| 347 |
|
| 348 |
public function getRemoteResponse() |
| 349 |
{ |
| 350 |
return $this->remoteResponse; |
| 351 |
} |
| 352 |
|
| 353 |
private function isSpam() |
| 354 |
{ |
| 355 |
$spam = false; |
| 356 |
$honeyPotValue = $this->utility->getRequestVar("JKGH00920"); |
| 357 |
if (!empty($honeyPotValue)) { |
| 358 |
$spam = true; |
| 359 |
} |
| 360 |
return $spam; |
| 361 |
} |
| 362 |
|
| 363 |
private function getExternalUrl() |
| 364 |
{ |
| 365 |
$result = null; |
| 366 |
$scheme = "http"; |
| 367 |
if (is_ssl()) { |
| 368 |
$scheme = "https"; |
| 369 |
} |
| 370 |
$result .= $scheme . "://"; |
| 371 |
$result .= iHomefinderConstants::EXTERNAL_URL; |
| 372 |
return $result; |
| 373 |
} |
| 374 |
} |
| 375 |
|