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