Files
PartnerExpo-Core/includes/utils/class-crm-client.php

362 lines
11 KiB
PHP

<?php
defined('ABSPATH') or die;
/**
* Minimal abstraction layer for MiniCRM R3 API and Vera R3 API
*
* The client depends on the crypto class by the same author, make sure to have it loaded beforehand.
*
* @author Juhász Levente <juhasz.levente@rendszerepito.hu>
*
* Based on work by Drew McLellan <drew.mclellan@gmail.com>
* Source: https://github.com/drewm/mailchimp-api
*/
class CRMClient {
public const VERSION = '1.1';
private $auth;
private $model = "MiniCRM";
private $api_endpoint = 'https://r3.minicrm.hu/Api/R3';
/* SSL Verification
Read before disabling:
http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
*/
public $verify_ssl = true;
private $request_successful = false;
private $last_error = '';
private $last_response = [];
private $last_request = [];
private $last_http_status = 418;
public function __construct($auth, $model = null, $endpoint = null ) {
$this->auth = $auth;
$this->last_response = ['headers' => null, 'body' => null];
$this->model = $model ?? $this->model;
if ( $endpoint ) {
$this->api_endpoint = $endpoint;
} elseif ( $model === 'Vera' ) {
$this->api_endpoint = 'https://api.rendszerepito.hu/R3';
}
}
public function getApiEndpoint() {
return $this->api_endpoint;
}
/**
* Was the last message successfull
*
* @return bool True for success, false for failure
*/
public function success() {
return $this->request_successful;
}
public function getLastError() {
return $this->last_error ?: false;
}
/**
* Get an array containing the HTTP headers and body of the API request
*
* @return array assoc array with keys 'headers' and 'body'
*/
public function getLastResponse() {
return $this->last_response;
}
/**
* Get the HTTP status of the last response
*
* @return array int
*/
public function getLastStatus() {
return $this->last_http_status;
}
public function get($method, $args = []) {
return $this->makeRequest('get', $method, $args);
}
public function put($method, $args = []) {
return $this->makeRequest('put', $method, $args);
}
public function post($method, $args = []) {
return $this->makeRequest('post', $method, $args);
}
private function makeRequest($http_verb, $method, $args = []) {
if (! function_exists('curl_init') || ! function_exists('curl_setopt')) {
throw new \Exception("cURL support is required, but can't be found!");
}
$url = $this->api_endpoint . '/' . $method;
$response = $this->prepareStateForRequest($http_verb, $method, $url);
$httpHeader = [
'Accept: application/json',
];
if (!empty($args)) {
$httpHeader[] = 'Content-Type: application/json';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($this-> model == 'MiniCRM') {curl_setopt($ch, CURLOPT_USERPWD, Crypto::decryptSecret($this->auth));}
elseif ($this-> model === 'Vera') {$httpHeader[] = 'Authorization: Bearer '.Crypto::decryptSecret($this->auth);}
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_USERAGENT, 'Levente-CRMClient/'. self::VERSION);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
switch ($http_verb) {
case 'get':
if (!empty($args)) {
$query = http_build_query($args, '', '&');
curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$this->attachRequestPayload($ch, $args);
break;
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
$this->attachRequestPayload($ch, $args);
break;
}
$responseContent = curl_exec($ch);
$response['headers'] = curl_getinfo($ch);
$response = $this->setResponseState($response, $responseContent, $ch);
$formattedResponse = $this->formatResponse($response);
curl_close($ch);
$this->determineSuccess($response, $formattedResponse);
return $formattedResponse;
}
/**
* @param string $http_verb
* @param string $method
* @param string $url
*/
private function prepareStateForRequest($http_verb, $method, $url) {
$this->last_error = '';
$this->request_successful = false;
$this->last_response = [
'headers' => null,
'httpHeaders' => null,
'body' => null,
];
$this->last_request = [
'method' => $http_verb,
'path' => $method,
'url' => $url,
'body' => '',
];
return $this->last_response;
}
/**
* Get the HTTP headers as an array of header-name => header-value pairs.
*
* The "Link" header is parsed into an associative array based on the
* rel names it contains. The original value is available under
* the "_raw" key.
*
* @param string $headersAsString
*
* @return array
*/
private function getHeadersAsArray($headersAsString)
{
$headers = [];
foreach (explode("\r\n", $headersAsString) as $i => $line) {
if (0 === $i) { // HTTP code
continue;
}
$line = trim($line);
if (empty($line)) {
continue;
}
if (strpos($line, ':') === false) {
continue;
}
list($key, $value) = array_pad(explode(':', $line, 2), 2, '');
$key = trim($key);
$value = trim($value);
if ('Link' == $key) {
$value = array_merge(
['_raw' => $value],
$this->getLinkHeaderAsArray($value)
);
}
$headers[$key] = $value;
}
return $headers;
}
/**
* Encode the data and attach it to the request
*
* @param resource $ch cURL session handle, used by reference
* @param array $data Assoc array of data to attach
*/
private function attachRequestPayload($ch, $data)
{
$encoded = json_encode($data);
$this->last_request['body'] = $encoded;
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
}
/**
* Decode the response and format any error messages for debugging
*
* @param array $response The response from the curl request
*
* @return array|false The JSON decoded into an array
*/
private function formatResponse($response)
{
$this->last_response = $response;
$message = null;
if (!empty($response['body'])) {
$decoded = json_decode($response['body'], true);
if (json_last_error() === JSON_ERROR_NONE) {
$message = $decoded;
} else {
$message = $response['body'];
}
}
return $message;
}
/**
* Extract all rel => URL pairs from the provided Link header value
*
* @param string $linkHeaderAsString
*
* @return array
*/
private function getLinkHeaderAsArray($linkHeaderAsString)
{
$urls = [];
if (preg_match_all('/<(.*?)>\s*;\s*rel="(.*?)"\s*/', $linkHeaderAsString, $matches)) {
foreach ($matches[2] as $i => $relName) {
$urls[$relName] = $matches[1][$i];
}
}
return $urls;
}
/**
* Do post-request formatting and setting state from the response
*
* @param array $response The response from the curl request
* @param string $responseContent The body of the response from the curl request
*
* * @return array The modified response
*/
private function setResponseState($response, $responseContent, $ch)
{
if (false === $responseContent) {
$this->last_error = curl_error($ch);
} else {
$headerSize = $response['headers']['header_size'];
$response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize));
$response['body'] = substr($responseContent, $headerSize);
if (isset($response['headers']['request_header'])) {
$this->last_request['headers'] = $response['headers']['request_header'];
}
}
return $response;
}
/**
* Check if the response was successful or a failure. If it failed, store the error.
*
* @param array $response The response from the curl request
* @param array|false $formattedResponse The response body payload from the curl request
*
* @return bool If the request was successful
*/
private function determineSuccess($response, $formattedResponse)
{
$status = $this->findHTTPStatus($response, $formattedResponse);
$this->last_http_status = $status;
if ($status >= 200 && $status <= 299) {
$this->request_successful = true;
return true;
}
if ($formattedResponse){
$this->last_error = sprintf('%d: %s', $status, $formattedResponse);
return false;
}
$this->last_error = 'Unknown error, call getLastResponse() to find out what happened.';
return false;
}
/**
* Find the HTTP status code from the headers or API response body
*
* @param array $response The response from the curl request
* @param array|false $formattedResponse The response body payload from the curl request
*
* @return int HTTP status code
*/
private function findHTTPStatus($response, $formattedResponse)
{
if (! empty($response['headers']) && isset($response['headers']['http_code'])) {
return (int) $response['headers']['http_code'];
}
if (! empty($response['body']) && isset($formattedResponse['status'])) {
return (int) $formattedResponse['status'];
}
return 418;
}
}