first commit

This commit is contained in:
2024-12-17 13:43:22 +01:00
commit 8e6cd8b410
21292 changed files with 3514826 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
<?php
namespace DrewM\MailChimp;
/**
* A MailChimp Batch operation.
* http://developer.mailchimp.com/documentation/mailchimp/reference/batches/
*
* @author Drew McLellan <drew.mclellan@gmail.com>
*/
class Batch
{
private $MailChimp;
private $operations = array();
private $batch_id;
public function __construct(MailChimp $MailChimp, $batch_id = null)
{
$this->MailChimp = $MailChimp;
$this->batch_id = $batch_id;
}
/**
* Add an HTTP DELETE request operation to the batch - for deleting data
*
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
*
* @return void
*/
public function delete($id, $method)
{
$this->queueOperation('DELETE', $id, $method);
}
/**
* Add an HTTP GET request operation to the batch - for retrieving data
*
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
*
* @return void
*/
public function get($id, $method, $args = array())
{
$this->queueOperation('GET', $id, $method, $args);
}
/**
* Add an HTTP PATCH request operation to the batch - for performing partial updates
*
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
*
* @return void
*/
public function patch($id, $method, $args = array())
{
$this->queueOperation('PATCH', $id, $method, $args);
}
/**
* Add an HTTP POST request operation to the batch - for creating and updating items
*
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
*
* @return void
*/
public function post($id, $method, $args = array())
{
$this->queueOperation('POST', $id, $method, $args);
}
/**
* Add an HTTP PUT request operation to the batch - for creating new items
*
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
*
* @return void
*/
public function put($id, $method, $args = array())
{
$this->queueOperation('PUT', $id, $method, $args);
}
/**
* Execute the batch request
*
* @param int $timeout Request timeout in seconds (optional)
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function execute($timeout = 10)
{
$req = array('operations' => $this->operations);
$result = $this->MailChimp->post('batches', $req, $timeout);
if ($result && isset($result['id'])) {
$this->batch_id = $result['id'];
}
return $result;
}
/**
* Check the status of a batch request. If the current instance of the Batch object
* was used to make the request, the batch_id is already known and is therefore optional.
*
* @param string $batch_id ID of the batch about which to enquire
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function check_status($batch_id = null)
{
if ($batch_id === null && $this->batch_id) {
$batch_id = $this->batch_id;
}
return $this->MailChimp->get('batches/' . $batch_id);
}
/**
* Get operations
*
* @return array
*/
public function get_operations()
{
return $this->operations;
}
/**
* Add an operation to the internal queue.
*
* @param string $http_verb GET, POST, PUT, PATCH or DELETE
* @param string $id ID for the operation within the batch
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
*
* @return void
*/
private function queueOperation($http_verb, $id, $method, $args = null)
{
$operation = array(
'operation_id' => $id,
'method' => $http_verb,
'path' => $method,
);
if ($args) {
if ($http_verb == 'GET') {
$key = 'params';
$operation[$key] = $args;
} else {
$key = 'body';
$operation[$key] = json_encode($args);
}
}
$this->operations[] = $operation;
}
}

View File

@@ -0,0 +1,506 @@
<?php
namespace DrewM\MailChimp;
/**
* Super-simple, minimum abstraction MailChimp API v3 wrapper
* MailChimp API v3: http://developer.mailchimp.com
* This wrapper: https://github.com/drewm/mailchimp-api
*
* @author Drew McLellan <drew.mclellan@gmail.com>
* @author Zoltan Szanto <mrbig00@gmail.com>
* @version 2.5
*/
class MailChimp
{
const TIMEOUT = 10;
static $USER_AGENT = 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)';
/**
* Enable SSL Verification
* @see http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
* @var bool
*/
public $verify_ssl = true;
protected $request_successful = false;
protected $last_error = '';
protected $last_response = array();
protected $last_request = array();
protected $api_key;
protected $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
/**
* Create a new instance
*
* @param string $api_key Your MailChimp API key
* @param string $api_endpoint Optional custom API endpoint
*
* @throws \Exception
*/
public function __construct($api_key, $api_endpoint = null)
{
if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
throw new \Exception("cURL support is required, but can't be found.");
}
$this->api_key = $api_key;
if ($api_endpoint === null) {
if (strpos($this->api_key, '-') === false) {
throw new \Exception("Invalid MailChimp API key supplied.");
}
list(, $data_center) = explode('-', $this->api_key);
$this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint);
} else {
$this->api_endpoint = $api_endpoint;
}
$this->last_response = array('headers' => null, 'body' => null);
}
/**
* Create a new instance of a Batch request. Optionally with the ID of an existing batch.
*
* @param string $batch_id Optional ID of an existing batch, if you need to check its status for example.
*
* @return Batch New Batch object.
*/
public function new_batch($batch_id = null)
{
return new Batch($this, $batch_id);
}
/**
* @return string The url to the API endpoint
*/
public function getApiEndpoint()
{
return $this->api_endpoint;
}
/**
* Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
*
* @param string $email The subscriber's email address
*
* @return string Hashed version of the input
*/
public function subscriberHash($email)
{
return md5(strtolower($email));
}
/**
* Was the last request successful?
*
* @return bool True for success, false for failure
*/
public function success()
{
return $this->request_successful;
}
/**
* Get the last error returned by either the network transport, or by the API.
* If something didn't work, this should contain the string describing the problem.
*
* @return string|false describing the error
*/
public function getLastError()
{
return $this->last_error ?: false;
}
/**
* Get an array containing the HTTP headers and the body of the API response.
*
* @return array Assoc array with keys 'headers' and 'body'
*/
public function getLastResponse()
{
return $this->last_response;
}
/**
* Get an array containing the HTTP headers and the body of the API request.
*
* @return array Assoc array
*/
public function getLastRequest()
{
return $this->last_request;
}
/**
* Make an HTTP DELETE request - for deleting data
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (if any)
* @param int $timeout Timeout limit for request in seconds
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function delete($method, $args = array(), $timeout = self::TIMEOUT)
{
return $this->makeRequest('delete', $method, $args, $timeout);
}
/**
* Make an HTTP GET request - for retrieving data
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function get($method, $args = array(), $timeout = self::TIMEOUT)
{
return $this->makeRequest('get', $method, $args, $timeout);
}
/**
* Make an HTTP PATCH request - for performing partial updates
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function patch($method, $args = array(), $timeout = self::TIMEOUT)
{
return $this->makeRequest('patch', $method, $args, $timeout);
}
/**
* Make an HTTP POST request - for creating and updating items
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function post($method, $args = array(), $timeout = self::TIMEOUT)
{
return $this->makeRequest('post', $method, $args, $timeout);
}
/**
* Make an HTTP PUT request - for creating new items
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function put($method, $args = array(), $timeout = self::TIMEOUT)
{
return $this->makeRequest('put', $method, $args, $timeout);
}
/**
* Performs the underlying HTTP request. Not very exciting.
*
* @param string $http_verb The HTTP verb to use: get, post, put, patch, delete
* @param string $method The API method to be called
* @param array $args Assoc array of parameters to be passed
* @param int $timeout
*
* @return array|false Assoc array of decoded result
*/
protected function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT)
{
$url = $this->api_endpoint . '/' . $method;
$response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout);
$httpHeader = array(
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
'Authorization: OAuth ' . $this->api_key
);
if (isset($args["language"])) {
$httpHeader[] = "Accept-Language: " . $args["language"];
}
$ch = $this->prepareResource($url, $timeout, $httpHeader);
switch ($http_verb) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
$this->attachRequestPayload($ch, $args);
break;
case 'get':
$query = http_build_query($args, '', '&');
curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'patch':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->attachRequestPayload($ch, $args);
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$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);
$isSuccess = $this->determineSuccess($response, $formattedResponse, $timeout);
return is_array($formattedResponse) ? $formattedResponse : $isSuccess;
}
/**
* Prepare cURL resource before request
*
* @param $url
* @param $timeout
* @param array $headers
*
* @return resource
*/
protected function prepareResource($url, $timeout, $headers = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, static::$USER_AGENT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
return $ch;
}
/**
* @param string $http_verb
* @param string $method
* @param string $url
* @param integer $timeout
*
* @return array
*/
protected function prepareStateForRequest($http_verb, $method, $url, $timeout)
{
$this->last_error = '';
$this->request_successful = false;
$this->last_response = array(
'headers' => null, // array of details from curl_getinfo()
'httpHeaders' => null, // array of HTTP headers
'body' => null // content of the response
);
$this->last_request = array(
'method' => $http_verb,
'path' => $method,
'url' => $url,
'body' => '',
'timeout' => $timeout,
);
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
*/
protected function getHeadersAsArray($headersAsString)
{
$headers = array();
foreach (explode("\r\n", $headersAsString) as $i => $line) {
if ($i === 0) { // HTTP code
continue;
}
$line = trim($line);
if (empty($line)) {
continue;
}
list($key, $value) = explode(': ', $line);
if ($key == 'Link') {
$value = array_merge(
array('_raw' => $value),
$this->getLinkHeaderAsArray($value)
);
}
$headers[$key] = $value;
}
return $headers;
}
/**
* Extract all rel => URL pairs from the provided Link header value
*
* Mailchimp only implements the URI reference and relation type from
* RFC 5988, so the value of the header is something like this:
*
* 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy",
* <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"'
*
* @param string $linkHeaderAsString
*
* @return array
*/
protected function getLinkHeaderAsArray($linkHeaderAsString)
{
$urls = array();
if (preg_match_all('/<(.*?)>\s*;\s*rel="(.*?)"\s*/', $linkHeaderAsString, $matches)) {
foreach ($matches[2] as $i => $relName) {
$urls[$relName] = $matches[1][$i];
}
}
return $urls;
}
/**
* 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
*/
protected 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
*/
protected function formatResponse($response)
{
$this->last_response = $response;
if (!empty($response['body'])) {
return json_decode($response['body'], true);
}
return false;
}
/**
* 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
* @param resource $ch The curl resource
*
* @return array The modified response
*/
protected function setResponseState($response, $responseContent, $ch)
{
if ($responseContent === false) {
$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
* @param int $timeout The timeout supplied to the curl request.
*
* @return bool If the request was successful
*/
protected function determineSuccess($response, $formattedResponse, $timeout)
{
$status = $this->findHTTPStatus($response, $formattedResponse);
if ($status >= 200 && $status <= 299) {
$this->request_successful = true;
return true;
}
if (isset($formattedResponse['detail'])) {
$this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']);
return false;
}
if ($timeout > 0 && $response['headers'] && $response['headers']['total_time'] >= $timeout) {
$this->last_error = sprintf('Request timed out after %f seconds.', $response['headers']['total_time']);
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
*/
protected 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;
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace DrewM\MailChimp;
/**
* A MailChimp Webhook request.
* How to Set Up Webhooks: http://eepurl.com/bs-j_T
*
* @author Drew McLellan <drew.mclellan@gmail.com>
*/
class Webhook
{
private static $eventSubscriptions = array();
private static $receivedWebhook = null;
/**
* Subscribe to an incoming webhook request. The callback will be invoked when a matching webhook is received.
*
* @param string $event Name of the webhook event, e.g. subscribe, unsubscribe, campaign
* @param callable $callback A callable function to invoke with the data from the received webhook
*
* @return void
*/
public static function subscribe($event, callable $callback)
{
if (!isset(self::$eventSubscriptions[$event])) self::$eventSubscriptions[$event] = array();
self::$eventSubscriptions[$event][] = $callback;
self::receive();
}
/**
* Retrieve the incoming webhook request as sent.
*
* @param string $input An optional raw POST body to use instead of php://input - mainly for unit testing.
*
* @return array|false An associative array containing the details of the received webhook
*/
public static function receive($input = null)
{
if (is_null($input)) {
if (self::$receivedWebhook !== null) {
$input = self::$receivedWebhook;
} else {
$input = file_get_contents("php://input");
}
}
if (!is_null($input) && $input != '') {
return self::processWebhook($input);
}
return false;
}
/**
* Process the raw request into a PHP array and dispatch any matching subscription callbacks
*
* @param string $input The raw HTTP POST request
*
* @return array|false An associative array containing the details of the received webhook
*/
private static function processWebhook($input)
{
self::$receivedWebhook = $input;
parse_str($input, $result);
if ($result && isset($result['type'])) {
self::dispatchWebhookEvent($result['type'], $result['data']);
return $result;
}
return false;
}
/**
* Call any subscribed callbacks for this event
*
* @param string $event The name of the callback event
* @param array $data An associative array of the webhook data
*
* @return void
*/
private static function dispatchWebhookEvent($event, $data)
{
if (isset(self::$eventSubscriptions[$event])) {
foreach (self::$eventSubscriptions[$event] as $callback) {
$callback($data);
}
// reset subscriptions
self::$eventSubscriptions[$event] = array();
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;