| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class responsible for communication with Instapage app via API. |
| 5 |
*/ |
| 6 |
class InstapageCmsPluginAPIModel { |
| 7 |
|
| 8 |
/** |
| 9 |
* @var object API model instance. |
| 10 |
*/ |
| 11 |
private static $apiModel = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gets the class instance. |
| 15 |
* |
| 16 |
* @return object Class instance. |
| 17 |
*/ |
| 18 |
public static function getInstance() { |
| 19 |
if (self::$apiModel === null) { |
| 20 |
self::$apiModel = new InstapageCmsPluginAPIModel(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$apiModel; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Performs the remote post based on selected CMS. |
| 28 |
* |
| 29 |
* @return object Result of the request in unified form. |
| 30 |
*/ |
| 31 |
public function remotePost($url, $data = array(), $headers = array()) { |
| 32 |
return InstapageCmsPluginConnector::getSelectedConnector()->remotePost($url, $data, $headers); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Performs the request to Instapage pageserver. |
| 37 |
* |
| 38 |
* @param string $url URL of the request. |
| 39 |
* @param string $host Host of the request. It will be used for request header. |
| 40 |
* @param array $cookies Cookie array. |
| 41 |
* |
| 42 |
* @return object $response Request response. |
| 43 |
*/ |
| 44 |
public function enterpriseCall($url, $host = '', $cookies = false) { |
| 45 |
$connector = InstapageCmsPluginConnector::getSelectedConnector(); |
| 46 |
$data = array(); |
| 47 |
$headers = array(); |
| 48 |
|
| 49 |
$host = $host ? $host : $connector->getHost(); |
| 50 |
$integration = $connector->name; |
| 51 |
$data['integration'] = $integration; |
| 52 |
$data['useragent'] = InstapageCmsPluginHelper::filterInput(INPUT_SERVER, 'HTTP_USER_AGENT'); |
| 53 |
$data['ip'] = InstapageCmsPluginHelper::filterInput(INPUT_SERVER, 'REMOTE_ADDR'); |
| 54 |
$data['cookies'] = $cookies; |
| 55 |
$data['custom'] = InstapageCmsPluginHelper::filterInput(INPUT_GET, 'custom'); |
| 56 |
$data['variant'] = InstapageCmsPluginHelper::filterInput(INPUT_GET, 'variant'); |
| 57 |
$data['requestHost'] = $host; |
| 58 |
$headers['integration'] = $integration; |
| 59 |
$headers['x-plugin-version'] = INSTAPAGE_PLUGIN_VERSION; |
| 60 |
$headers['x-cms-version'] = $connector->getCMSVersion(); |
| 61 |
$headers['x-instapage-host'] = $host; |
| 62 |
$headers['x-php-version'] = PHP_VERSION_ID; |
| 63 |
$response = $connector->remoteRequest($url, $data, $headers, 'POST'); |
| 64 |
|
| 65 |
InstapageCmsPluginHelper::writeDiagnostics($url, 'Enterprise call URL'); |
| 66 |
InstapageCmsPluginHelper::writeDiagnostics($host, 'Enterprise call host'); |
| 67 |
InstapageCmsPluginHelper::writeDiagnostics($headers, 'Enterprise call headers'); |
| 68 |
InstapageCmsPluginHelper::writeDiagnostics($data, 'Enterprise call data'); |
| 69 |
InstapageCmsPluginHelper::writeDiagnostics($response, 'Enterprise call response'); |
| 70 |
|
| 71 |
return $response; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Performs the request to Instapage app. |
| 76 |
* |
| 77 |
* @param string $action API action. |
| 78 |
* @param array $data Data to be passed in the request. |
| 79 |
* @param array $headers Headers for the request. |
| 80 |
* @param string $method Request type. 'POST' ang 'GET' are allowed. Default: 'POST'. |
| 81 |
* |
| 82 |
* @return object $response Request response. |
| 83 |
*/ |
| 84 |
public function apiCall($action, $data = array(), $headers = array(), $method = 'POST') { |
| 85 |
$integration = InstapageCmsPluginConnector::getSelectedConnector()->name; |
| 86 |
$url = InstapageCmsPluginConnector::getURLWithSelectedProtocol(INSTAPAGE_APP_ENDPOINT . '/' . $action); |
| 87 |
$headers['integration'] = $integration; |
| 88 |
$response = InstapageCmsPluginConnector::getSelectedConnector()->remoteRequest($url, $data, $headers, $method); |
| 89 |
|
| 90 |
InstapageCmsPluginHelper::writeDiagnostics($method . ' : ' . $url, 'API ' . $action . ' URL'); |
| 91 |
InstapageCmsPluginHelper::writeDiagnostics($data, 'API ' . $action . ' data'); |
| 92 |
InstapageCmsPluginHelper::writeDiagnostics($headers, 'API ' . $action . ' headers'); |
| 93 |
InstapageCmsPluginHelper::writeDiagnostics($response, 'API ' . $action . ' response'); |
| 94 |
|
| 95 |
return (is_array($response) && isset($response['body'])) ? $response['body'] : null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Authorizes the user based on email and password. |
| 100 |
* |
| 101 |
* @param string $email Email address. |
| 102 |
* @param string password Password. |
| 103 |
* |
| 104 |
* @uses InstapageCmsPluginAPIModel::apiCall() to communicate with Instapage app. |
| 105 |
* |
| 106 |
* @return object App response. |
| 107 |
*/ |
| 108 |
public function authorise($email, $password) { |
| 109 |
$data = array('email' => $email, 'password' => $password); |
| 110 |
$response = $this->apiCall('page', $data); |
| 111 |
|
| 112 |
return $response; |
| 113 |
} |
| 114 |
} |
| 115 |
|