| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class responsible for managing the landing pages. |
| 5 |
*/ |
| 6 |
class InstapageCmsPluginServicesModel { |
| 7 |
|
| 8 |
/** |
| 9 |
* @var object Class instance. |
| 10 |
*/ |
| 11 |
private static $servicesModel = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gets the class instance. |
| 15 |
* |
| 16 |
* @return object Class instance. |
| 17 |
*/ |
| 18 |
public static function getInstance() { |
| 19 |
if (self::$servicesModel === null) { |
| 20 |
self::$servicesModel = new InstapageCmsPluginServicesModel(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$servicesModel; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Checks if current request should be processed by a proxy service. |
| 28 |
* |
| 29 |
* @return boolean |
| 30 |
*/ |
| 31 |
public function isServicesRequest() { |
| 32 |
if (strpos($_SERVER['REQUEST_URI'], 'instapage-proxy-services') !== false) { |
| 33 |
InstapageCmsPluginHelper::writeDiagnostics( |
| 34 |
InstapageCmsPluginHelper::filterInput(INPUT_SERVER, 'REQUEST_URI'), |
| 35 |
'Proxy services URL' |
| 36 |
); |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Strips the slashes. |
| 46 |
* |
| 47 |
* @param string &$$value Value to strip slashes from. |
| 48 |
*/ |
| 49 |
public function stripSlashesGpc(&$value) { |
| 50 |
$value = stripslashes($value); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Processes the proxy request. |
| 55 |
*/ |
| 56 |
public function processProxyServices() { |
| 57 |
$api = InstapageCmsPluginAPIModel::getInstance(); |
| 58 |
$url = filter_input(INPUT_GET, 'url'); |
| 59 |
|
| 60 |
if (strpos($url, 'ajax/pageserver/email') === false) { |
| 61 |
throw new Exception('Unsupported endpoint: ' . $url); |
| 62 |
} |
| 63 |
|
| 64 |
$url = InstapageCmsPluginConnector::getURLWithSelectedProtocol(INSTAPAGE_PROXY_ENDPOINT . $url); |
| 65 |
$postData = filter_input_array(INPUT_POST) ?: []; |
| 66 |
array_walk_recursive($postData, array($this, 'stripSlashesGpc')); |
| 67 |
|
| 68 |
if (!empty($postData)) { |
| 69 |
$postData['user_ip'] = InstapageCmsPluginHelper::filterInput(INPUT_SERVER, 'REMOTE_ADDR'); |
| 70 |
} |
| 71 |
|
| 72 |
$data = $postData; |
| 73 |
$data['ajax'] = 1; |
| 74 |
$response = $api->remotePost($url, $data); |
| 75 |
|
| 76 |
if (isset($response['response']['code']) && $response['response']['code'] !== 200) { |
| 77 |
$this->disableCrossOriginProxy(); |
| 78 |
$matches = array(); |
| 79 |
$pattern = '/email\/(\d*)/'; |
| 80 |
preg_match($pattern, $url, $matches); |
| 81 |
} |
| 82 |
|
| 83 |
InstapageCmsPluginHelper::writeDiagnostics($url, 'Proxy services URL'); |
| 84 |
InstapageCmsPluginHelper::writeDiagnostics($data, 'Proxy data'); |
| 85 |
InstapageCmsPluginHelper::writeDiagnostics($response, 'Proxy response'); |
| 86 |
|
| 87 |
$status = isset($response->status) ? $response->status : false; |
| 88 |
$responseCode = isset($response['response']['code']) ? $response['response']['code'] : 200; |
| 89 |
|
| 90 |
if ($status === 'ERROR') { |
| 91 |
$errorMessage = isset($response->message) ? $response->message : false; |
| 92 |
|
| 93 |
if (!empty($errorMessage)) { |
| 94 |
throw new Exception($errorMessage); |
| 95 |
} |
| 96 |
else { |
| 97 |
throw new Exception('500 Internal Server Error'); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
ob_start(); |
| 102 |
ob_end_clean(); |
| 103 |
header('Content-Type: text/json; charset=UTF-8'); |
| 104 |
echo InstapageCmsPluginConnector::escapeHTML(trim(isset($response['body']) ? $response['body'] : '')); |
| 105 |
status_header($responseCode); |
| 106 |
|
| 107 |
exit; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Disables the Cross Origin Proxy option in plugin's settings. |
| 112 |
*/ |
| 113 |
private function disableCrossOriginProxy() { |
| 114 |
$options = InstapageCmsPluginHelper::getOptions(); |
| 115 |
$options->config->crossOrigin = 0; |
| 116 |
InstapageCmsPluginHelper::updateOptions($options); |
| 117 |
} |
| 118 |
} |
| 119 |
|