From 2965d44fb0d06c2ec2a6d7e854d18af5497b9ce0 Mon Sep 17 00:00:00 2001 From: Georgi Nikolov Date: Thu, 21 Dec 2017 16:15:54 +0200 Subject: [PATCH] Update wrapper - php 5.3 is supported now --- libraries/php-mailjet-v3-simple.class.php | 117 ++++++---------------- 1 file changed, 29 insertions(+), 88 deletions(-) diff --git a/libraries/php-mailjet-v3-simple.class.php b/libraries/php-mailjet-v3-simple.class.php index bab7367..b8df310 100644 --- a/libraries/php-mailjet-v3-simple.class.php +++ b/libraries/php-mailjet-v3-simple.class.php @@ -1,7 +1,6 @@ = 5.3 * */ - class MailjetPHPWrapper { # Wrapper version, changed for each release - const WRAPPER_VERSION = '1.0.9'; - - # Mailjet API version + const WRAPPER_VERSION = '1.1.0'; + # MailjetPHPWrapper API version var $version = 'v3'; - # Connect with https protocol var $secure = true; - # Mode debug ? 0 : none; 1 : errors only; 2 : all var $debug = 0; - # Edit with your Mailjet API keys (you can find them here : https://app.mailjet.com/account/api_keys) var $apiKey = ''; var $secretKey = ''; - // Ressources arrays - /* * Newsletter resources */ @@ -41,7 +33,6 @@ class MailjetPHPWrapper "newsletterTest", "newsletterStatus" ); - /* * Contact resources * "contactManageManyContacts" not in as it is a special case. @@ -50,7 +41,6 @@ class MailjetPHPWrapper "contactManageContactsLists", "contactGetContactsLists" ); - /* * Contactslist resources */ @@ -58,14 +48,18 @@ class MailjetPHPWrapper "contactslistManageContact", "contactslistManageManyContacts" ); - /* * Template resources */ private static $_templateResources = array ( "templateDetailContent" ); - + /* + * dns resources + */ + private static $_dnsResources = array ( + "dnsCheck" + ); # Constructor function public function __construct($apiKey = false, $secretKey = false, $preprod = false) { @@ -75,11 +69,9 @@ public function __construct($apiKey = false, $secretKey = false, $preprod = fals if ($secretKey) { $this->secretKey = $secretKey; } - $this->apiUrl = $this->getApiUrl($preprod); $this->wrapperVersion = $this->readWrapperVersion(); } - private function getApiUrl ($preprod) { if ($preprod) @@ -91,29 +83,23 @@ private function getApiUrl ($preprod) return (($this->secure) ? 'https' : 'http') . '://api.mailjet.com/' . $this->version; } } - public function curl_setopt_custom_postfields($curl_handle, $postfields, $headers = null) { $algos = hash_algos(); $hashAlgo = null; - foreach (array('sha1', 'md5') as $preferred) { if (in_array($preferred, $algos)) { $hashAlgo = $preferred; break; } } - if ($hashAlgo === null) { list($hashAlgo) = $algos; } - $boundary = '----------------------------' . substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12); - $body = array(); $crlf = "\r\n"; - foreach ($postfields as $key => $value) { if (is_array($value)) { foreach ($value as $filename => $path) { @@ -121,11 +107,9 @@ public function curl_setopt_custom_postfields($curl_handle, $postfields, $header if (strpos($path, '@') === 0) { preg_match('/^@(.*?)$/', $path, $matches); list($dummy, $path) = $matches; - if (is_int($filename)) { $filename = basename($path); } - $body[] = '--' . $boundary; $body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . $filename . '"'; $body[] = 'Content-Type: application/octet-stream'; @@ -148,27 +132,22 @@ public function curl_setopt_custom_postfields($curl_handle, $postfields, $header $body[] = $value; } } - $body[] = '--' . $boundary . '--'; $body[] = ''; $contentType = 'multipart/form-data; boundary=' . $boundary; $content = join($crlf, $body); $contentLength = strlen($content); - curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array( 'Content-Length: ' . $contentLength, 'Expect: 100-continue', 'Content-Type: ' . $contentType, )); - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content); } - public function __call($resource, $args) { # Parameters array $params = (sizeof($args) > 0) ? $args[0] : array(); - # Request method, GET by default if (isset($params["method"])) { @@ -179,10 +158,8 @@ public function __call($resource, $args) { $request = 'GET'; } - # Request ID, empty by default $id = isset($params["ID"]) ? $params["ID"] : ''; - /* Using SendAPI without the "to" parameter but with "cc" AND/OR "bcc" Our API needs the "to" parameter filled to send email @@ -191,14 +168,12 @@ public function __call($resource, $args) if ($resource == "sendEmail" && (empty($params["to"]) && (!empty($params["cc"]) || !empty($params["bcc"])))) { $params["to"] = "mailjet@example.org"; } - if ($id == '') { # Request Unique field, empty by default $unique = isset($params["unique"]) ? $params["unique"] : ''; unset($params["unique"]); # Make request - $result = $this->sendRequest($resource, $params, $request, $unique); } else @@ -206,16 +181,13 @@ public function __call($resource, $args) # Make request $result = $this->sendRequest($resource, $params, $request, $id); } - # Return result $return = ($result === true) ? $this->_response : false; if ($this->debug == 2 || ($this->debug == 1 && $return == false)) { $this->debug(); } - return $return; } - /** * * @param string $method REST or DATA @@ -229,7 +201,6 @@ private function makeUrl($method, $resourceBase, $resourceID, $action) { return $this->apiUrl.'/'.$method.'/'.$resourceBase.'/'.$resourceID.'/'.strtolower($action); } - /** * * @param string $method REST or DATA @@ -243,11 +214,9 @@ private function makeUrlFromFilter($method, $resourceBase, $resourceID, $resourc { $matches = array(); preg_match('/'.$resourceBase.'([a-zA-Z]+)/', $resource, $matches); - $action = $matches[1]; return $this->makeUrl($method, $resourceBase, $resourceID, $action); } - public function requestUrlBuilder($resource, $params = array(), $request, $id) { if ($resource == "sendEmail") { @@ -282,6 +251,10 @@ public function requestUrlBuilder($resource, $params = array(), $request, $id) { $this->call_url = $this->makeUrlFromFilter('REST', 'template', $params['ID'], $resource); } + else if (in_array($resource, self::$_dnsResources)) + { + $this->call_url = $this->makeUrlFromFilter('REST', 'dns', $params['ID'], $resource); + } else if (in_array($resource, self::$_contactResources)) { $this->call_url = $this->makeUrlFromFilter('REST', 'contact', $params['ID'], $resource); // Was $this->call_url = $this->apiUrl."/REST/contact/". $contact_id . "/".strtolower($action); @@ -297,16 +270,13 @@ public function requestUrlBuilder($resource, $params = array(), $request, $id) else { $this->call_url = $this->apiUrl . '/REST/' . $resource; } - if ($request == "GET" || $request == "POST") { if (count($params) > 0) { $this->call_url .= '?'; - foreach ($params as $key => $value) { // In a GET request, put an underscore char in front of params to avoid it being treated as a filter $firstChar = substr($key, 0, -(strlen($key) - 1)); - if ($request == "GET") { $okFirstChar = ($firstChar != "_"); $queryStringKey = $key; @@ -315,18 +285,15 @@ public function requestUrlBuilder($resource, $params = array(), $request, $id) $okFirstChar = ($firstChar == "_"); $queryStringKey = substr($key, 1); } - if ($okFirstChar && ($key != "ID")) { $query_string[$queryStringKey] = $queryStringKey . '=' . urlencode($value); $this->call_url .= $query_string[$queryStringKey] . '&'; } } - $this->call_url = substr($this->call_url, 0, -1); } } - if (($request == "VIEW" || $request == "DELETE" || $request == "PUT") && $resource != "addHTMLbody" && $resource != "uploadCSVContactslistData") { if ($id != '') @@ -341,19 +308,17 @@ public function requestUrlBuilder($resource, $params = array(), $request, $id) } } } - return $this->call_url; } - public function sendRequest($resource = false, $params = array(), $request = "GET", $id = '') { - # Method + # Method $this->_method = $resource; $this->_request = $request; - # Build request URL $url = $this->requestUrlBuilder($resource, $params, $request, $id); - + // fix bug for sort (ID+DESC), replace %2B in url by + + $url = preg_replace('#Sort=(.+)%2B(DESC|ASC|)?#sUi', 'Sort=$1+$2', $url); # Set up and execute the curl process $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); @@ -362,22 +327,17 @@ public function sendRequest($resource = false, $params = array(), $request = "GE curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl_handle, CURLOPT_USERPWD, $this->apiKey . ':' . $this->secretKey); - $this->_request_post = false; - - if (($request == 'POST') || ($request == 'PUT')){ + if (($request == 'POST') || ($request == 'PUT')): curl_setopt($curl_handle, CURLOPT_POST, 1); - // Exclude filters from payload. See http://stackoverflow.com/questions/4260086/php-how-to-use-array-filter-to-filter-array-keys $paramsFiltered = array_filter(array_keys($params), function($k) { return substr($k, 0, 1) != '_'; }); $params = array_intersect_key($params, array_flip($paramsFiltered)); - if ($this->debug == 2) { var_dump($params); } - if ($resource == "sendEmail") { $this->curl_setopt_custom_postfields($curl_handle, $params); } @@ -404,39 +364,39 @@ public function sendRequest($resource = false, $params = array(), $request = "GE ($resource == "contactManageContactsLists") || ($resource == "contactManageManyContacts") || (in_array($resource, self::$_contactslistResources)) || - (in_array($resource, self::$_templateResources))) + (in_array($resource, self::$_templateResources)) || + (in_array($resource, self::$_dnsResources))) { unset($params['ID']); } - - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($params, JSON_UNESCAPED_SLASHES)); + $j_e = null; + if (version_compare(phpversion(), '5.4.0', '<')) { + $j_e = str_replace('\\/', '/', json_encode($params)); + } else { + // 64 => unescaped_slashes + $j_e = json_encode($params, 64); + } + curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $j_e); curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' )); } $this->_request_post = $params; - } - + endif; if ($request == 'DELETE') { curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE"); } - if ($request == 'PUT') { curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "PUT"); } - $buffer = curl_exec($curl_handle); - if ($this->debug == 2) { var_dump($buffer); } - # Response code $this->_response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); - # Close curl process curl_close($curl_handle); - # Return response if (($this->_response_code == 200) && ($resource == "getHTMLbody")) { $this->_response = $buffer; @@ -455,7 +415,6 @@ public function sendRequest($resource = false, $params = array(), $request = "GE $this->_response = json_decode($buffer, false, 512); } } - if ($request == 'POST') { return ($this->_response_code == 201 || $this->_response_code == 200) ? true : false; } @@ -464,12 +423,10 @@ public function sendRequest($resource = false, $params = array(), $request = "GE } return ($this->_response_code == 200) ? true : false; } - public function debug() { echo ''; - echo '
'; - if (isset($this->_response_code)): if (($this->_response_code == 200) || ($this->_response_code == 201) || ($this->_response_code == 204)): echo ''; echo ''; echo ''; - if (isset($this->_response)): echo ''; endif; - echo '
Success
Status code' . $this->_response_code . '
Response
' . utf8_decode(print_r($this->_response, 1)) . '
'; elseif ($this->_response_code == 304): echo ''; @@ -515,9 +467,7 @@ public function debug() echo '
'; endif; endif; - $call_url = parse_url($this->call_url); - echo ''; echo ''; echo ''; @@ -525,13 +475,11 @@ public function debug() echo ''; echo ''; echo '
API config
Protocole' . $call_url['scheme'] . '
Version' . $this->version . '
Wrapper Version' . $this->readWrapperVersion() . '
'; - echo ''; echo ''; echo ''; echo ''; echo ''; - if ($this->_request_post) { echo ''; } - echo ''; echo '
Call infos
Resource' . $this->_method . '
Request type' . $this->_request . '
Get Arguments'; - if (isset($call_url['query'])) { $args = explode("&", $call_url['query']); foreach ($args as $arg) { @@ -539,12 +487,9 @@ public function debug() echo '' . $arg[0] . ' = ' . $arg[1] . '
'; } } - echo '
Post Arguments'; - foreach ($this->_request_post as $k => $v) { if (is_array($v)) { @@ -558,17 +503,13 @@ public function debug() echo $k . ' = ' . $v . '
'; } } - echo '
Call url' . $this->call_url . '
'; - echo '
'; } - private function readWrapperVersion() { - return self::WRAPPER_VERSION; + return MailjetPHPWrapper::WRAPPER_VERSION; } }