- Adds and centralizes API data encoding/decoding method
This commit is contained in:
@@ -22,7 +22,7 @@ class API {
|
|||||||
$this->action = isset($_GET['action']) ?
|
$this->action = isset($_GET['action']) ?
|
||||||
Helpers::underscoreToCamelCase($_GET['action']) :
|
Helpers::underscoreToCamelCase($_GET['action']) :
|
||||||
false;
|
false;
|
||||||
$this->data = $this->validateRequestData();
|
$this->data = self::decodeRequestData($_GET['data']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
@@ -46,15 +46,22 @@ class API {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateRequestData() {
|
static function decodeRequestData($data) {
|
||||||
if(!isset($_GET['data'])) return false;
|
if(!$data) return false;
|
||||||
$data = base64_decode($_GET['data']);
|
$data = base64_decode($data);
|
||||||
return (is_serialized($data)) ?
|
return (is_serialized($data)) ?
|
||||||
unserialize($data) :
|
unserialize($data) :
|
||||||
$this->terminateRequest(404, __('Invalid API data format.'));
|
self::terminateRequest(404, __('Invalid API data format.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static function buildRequest($endpoint, $action, $data) {
|
static function encodeRequestData($data) {
|
||||||
|
return rtrim(base64_encode(serialize($data)), '=');
|
||||||
|
}
|
||||||
|
|
||||||
|
static function buildRequest($endpoint, $action, $data, $encode_data = true) {
|
||||||
|
if($encode_data) {
|
||||||
|
$data = base64_encode(serialize($data));
|
||||||
|
}
|
||||||
$params = array(
|
$params = array(
|
||||||
self::API_NAME => '',
|
self::API_NAME => '',
|
||||||
'endpoint' => $endpoint,
|
'endpoint' => $endpoint,
|
||||||
|
@@ -40,11 +40,11 @@ class CronHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
||||||
$data = serialize(array('token' => $token));
|
$data = array('token' => $token);
|
||||||
$url = API::buildRequest(
|
$url = API::buildRequest(
|
||||||
QueueAPI::ENDPOINT,
|
QueueAPI::ENDPOINT,
|
||||||
QueueAPI::ACTION_RUN,
|
QueueAPI::ACTION_RUN,
|
||||||
base64_encode($data)
|
$data
|
||||||
);
|
);
|
||||||
$args = array(
|
$args = array(
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
|
@@ -62,10 +62,13 @@ class Links {
|
|||||||
'hash' => $hash,
|
'hash' => $hash,
|
||||||
'url' => $extracted_link['link']
|
'url' => $extracted_link['link']
|
||||||
);
|
);
|
||||||
|
$data = self::DATA_TAG . '-' . $hash;
|
||||||
|
// do not encode data; it's replaced with subscriber-specific data
|
||||||
|
// and encoded during send operation (Links::replaceSubscriberData())
|
||||||
$tracked_link = API::buildRequest(
|
$tracked_link = API::buildRequest(
|
||||||
TrackAPI::ENDPOINT,
|
TrackAPI::ENDPOINT,
|
||||||
TrackAPI::ACTION_CLICK,
|
TrackAPI::ACTION_CLICK,
|
||||||
self::DATA_TAG . '-' . $hash
|
$encode_data = false
|
||||||
);
|
);
|
||||||
// first, replace URL in the extracted HTML source with encoded link
|
// first, replace URL in the extracted HTML source with encoded link
|
||||||
$tracked_link_html_source = str_replace(
|
$tracked_link_html_source = str_replace(
|
||||||
@@ -111,7 +114,7 @@ class Links {
|
|||||||
'queue' => $queue_id,
|
'queue' => $queue_id,
|
||||||
'hash' => $hash
|
'hash' => $hash
|
||||||
);
|
);
|
||||||
$data = rtrim(base64_encode(serialize($data)), '=');
|
$data = API::encodeRequestData($data);
|
||||||
$content = str_replace($link, $data, $content);
|
$content = str_replace($link, $data, $content);
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
|
@@ -11,10 +11,14 @@ class OpenTracking {
|
|||||||
$DOM = new \pQuery();
|
$DOM = new \pQuery();
|
||||||
$DOM = $DOM->parseStr($template);
|
$DOM = $DOM->parseStr($template);
|
||||||
$template = $DOM->query('body');
|
$template = $DOM->query('body');
|
||||||
|
$data = Links::DATA_TAG;
|
||||||
|
// do not encode data; it's replaced with subscriber-specific data
|
||||||
|
// and encoded during send operation (Links::replaceSubscriberData())
|
||||||
$url = API::buildRequest(
|
$url = API::buildRequest(
|
||||||
TrackAPI::ENDPOINT,
|
TrackAPI::ENDPOINT,
|
||||||
TrackAPI::ACTION_OPEN,
|
TrackAPI::ACTION_OPEN,
|
||||||
Links::DATA_TAG
|
$data,
|
||||||
|
$encode_data = false
|
||||||
);
|
);
|
||||||
$open_tracking_image = sprintf(
|
$open_tracking_image = sprintf(
|
||||||
'<img alt="" class="" src="%s"/>',
|
'<img alt="" class="" src="%s"/>',
|
||||||
@@ -25,9 +29,9 @@ class OpenTracking {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function addTrackingImage() {
|
static function addTrackingImage() {
|
||||||
add_filter(Renderer::POST_PROCESS_FILTER, function ($template) {
|
add_filter(Renderer::POST_PROCESS_FILTER, function($template) {
|
||||||
return OpenTracking::process($template);
|
return OpenTracking::process($template);
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -2,7 +2,7 @@
|
|||||||
namespace MailPoet\Newsletter;
|
namespace MailPoet\Newsletter;
|
||||||
|
|
||||||
use MailPoet\API\API;
|
use MailPoet\API\API;
|
||||||
use MailPoet\API\Endpoints\ViewInBrowser;
|
use MailPoet\API\Endpoints\ViewInBrowser as ViewInBrowserAPI;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
|
||||||
class Url {
|
class Url {
|
||||||
@@ -36,9 +36,9 @@ class Url {
|
|||||||
);
|
);
|
||||||
$params = array(
|
$params = array(
|
||||||
API::API_NAME,
|
API::API_NAME,
|
||||||
'endpoint' => ViewInBrowser::ENDPOINT,
|
ViewInBrowserAPI::ENDPOINT,
|
||||||
'action' => ViewInBrowser::ACTION_VIEW,
|
ViewInBrowserAPI::ACTION_VIEW,
|
||||||
'data' => base64_encode(serialize($data))
|
$data
|
||||||
);
|
);
|
||||||
return add_query_arg($params, home_url());
|
return add_query_arg($params, home_url());
|
||||||
}
|
}
|
||||||
|
@@ -49,7 +49,7 @@ class Url {
|
|||||||
API::API_NAME,
|
API::API_NAME,
|
||||||
'endpoint='.Subscription::ENDPOINT,
|
'endpoint='.Subscription::ENDPOINT,
|
||||||
'action='.$action,
|
'action='.$action,
|
||||||
'data='.rtrim(base64_encode(serialize($data)), '=')
|
'data='. API::encodeRequestData($data)
|
||||||
);
|
);
|
||||||
|
|
||||||
// add parameters
|
// add parameters
|
||||||
|
Reference in New Issue
Block a user