You are here

public function InstapageCmsPluginServicesModel::processProxyServices in Instapage plugin 8.3

Same name and namespace in other branches
  1. 7.3 core/models/InstapageCmsPluginServicesModel.php \InstapageCmsPluginServicesModel::processProxyServices()

Processes the proxy request.

File

core/models/InstapageCmsPluginServicesModel.php, line 53

Class

InstapageCmsPluginServicesModel
Class responsible for managing the landing pages.

Code

public function processProxyServices() {
  $api = InstapageCmsPluginAPIModel::getInstance();
  $url = filter_input(INPUT_GET, 'url');
  if (strpos($url, 'ajax/pageserver/email') === false) {
    throw new Exception('Unsupported endpoint: ' . $url);
  }
  $url = InstapageCmsPluginConnector::getURLWithSelectedProtocol(INSTAPAGE_PROXY_ENDPOINT . $url);
  array_walk_recursive($_POST, array(
    $this,
    'stripSlashesGpc',
  ));
  if (isset($_POST) && !empty($_POST)) {
    $_POST['user_ip'] = $_SERVER['REMOTE_ADDR'];
  }
  $data = $_POST;
  $data['ajax'] = 1;
  $response = $api
    ->remotePost($url, $data);
  if (isset($response['response']['code']) && $response['response']['code'] !== 200) {
    $this
      ->disableCrossOriginProxy();
    $matches = array();
    $pattern = '/email\\/(\\d*)/';
    preg_match($pattern, $url, $matches);
  }
  InstapageCmsPluginHelper::writeDiagnostics($url, 'Proxy services URL');
  InstapageCmsPluginHelper::writeDiagnostics($data, 'Proxy data');
  InstapageCmsPluginHelper::writeDiagnostics($response, 'Proxy response');
  $status = isset($response->status) ? $response->status : false;
  $responseCode = isset($response['response']['code']) ? $response['response']['code'] : 200;
  if ($status === 'ERROR') {
    $errorMessage = isset($response->message) ? $response->message : false;
    if (!empty($errorMessage)) {
      throw new Exception($errorMessage);
    }
    else {
      throw new Exception('500 Internal Server Error');
    }
  }
  ob_start();
  ob_end_clean();
  header('Content-Type: text/json; charset=UTF-8');
  echo trim(isset($response['body']) ? $response['body'] : '');
  status_header($responseCode);
  exit;
}