- Addresses issues identified during code review

This commit is contained in:
Vlad
2016-01-06 19:19:06 -05:00
parent f1bf2bb097
commit 0dbe04c3f8
29 changed files with 493 additions and 438 deletions

View File

@ -4,21 +4,34 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class AmazonSES {
function __construct($region, $accessKey, $secretKey, $from) {
$this->awsAccessKey = $accessKey;
$this->awsSecret_key = $secretKey;
$this->awsRegion = $region;
$this->awsEndpoint = sprintf('email.%s.amazonaws.com', $region);
$this->awsSigningAlgorithm = 'AWS4-HMAC-SHA256';
$this->awsService = 'ses';
$this->awsTerminationString = 'aws4_request';
$this->hashAlgorithm = 'sha256';
$this->url = 'https://' . $this->awsEndpoint;
public $aws_access_key;
public $aws_secret_key;
public $aws_region;
public $aws_endpoint;
public $aws_signing_algorithm;
public $aws_service;
public $aws_termination_string;
public $hash_algorithm;
public $url;
public $from;
public $date;
public $date_without_time;
function __construct($region, $access_key, $secret_key, $from) {
$this->aws_access_key = $access_key;
$this->aws_secret_key = $secret_key;
$this->aws_region = $region;
$this->aws_endpoint = sprintf('email.%s.amazonaws.com', $region);
$this->aws_signing_algorithm = 'AWS4-HMAC-SHA256';
$this->aws_service = 'ses';
$this->aws_termination_string = 'aws4_request';
$this->hash_algorithm = 'sha256';
$this->url = 'https://' . $this->aws_endpoint;
$this->from = $from;
$this->date = gmdate('Ymd\THis\Z');
$this->dateWithoutTime = gmdate('Ymd');
$this->date_without_time = gmdate('Ymd');
}
function send($newsletter, $subscriber) {
$result = wp_remote_post(
$this->url,
@ -29,7 +42,7 @@ class AmazonSES {
wp_remote_retrieve_response_code($result) === 200
);
}
function getBody($newsletter, $subscriber) {
$body = array(
'Action' => 'SendEmail',
@ -47,7 +60,7 @@ class AmazonSES {
}
return $body;
}
function request($newsletter, $subscriber) {
$body = $this->getBody($newsletter, $subscriber);
return array(
@ -55,59 +68,88 @@ class AmazonSES {
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Host' => $this->awsEndpoint,
'Host' => $this->aws_endpoint,
'Authorization' => $this->signRequest($body),
'X-Amz-Date' => $this->date
),
'body' => urldecode(http_build_query($body))
);
}
function signRequest($body) {
$stringToSign = $this->createStringToSign(
$string_to_sign = $this->createStringToSign(
$this->getCredentialScope(),
$this->getCanonicalRequest($body)
);
$signature = hash_hmac($this->hashAlgorithm, $stringToSign, $this->getSigningKey());
$signature = hash_hmac(
$this->hash_algorithm,
$string_to_sign,
$this->getSigningKey()
);
return sprintf(
'%s Credential=%s/%s, SignedHeaders=host;x-amz-date, Signature=%s',
$this->awsSigningAlgorithm,
$this->awsAccessKey,
$this->aws_signing_algorithm,
$this->aws_access_key,
$this->getCredentialScope(),
$signature);
}
function getCredentialScope() {
return sprintf('%s/%s/%s/%s', $this->dateWithoutTime, $this->awsRegion, $this->awsService, $this->awsTerminationString);
return sprintf(
'%s/%s/%s/%s',
$this->date_without_time,
$this->aws_region,
$this->aws_service,
$this->aws_termination_string);
}
function getCanonicalRequest($body) {
return implode("\n", array(
'POST',
'/',
'',
'host:' . $this->awsEndpoint,
'host:' . $this->aws_endpoint,
'x-amz-date:' . $this->date,
'',
'host;x-amz-date',
hash($this->hashAlgorithm, urldecode(http_build_query($body)))
hash($this->hash_algorithm, urldecode(http_build_query($body)))
));
}
function createStringToSign($credentialScope, $canonicalRequest) {
function createStringToSign($credential_scope, $canonical_request) {
return implode("\n", array(
$this->awsSigningAlgorithm,
$this->aws_signing_algorithm,
$this->date,
$credentialScope,
hash($this->hashAlgorithm, $canonicalRequest)
$credential_scope,
hash($this->hash_algorithm, $canonical_request)
));
}
function getSigningKey() {
$dateKey = hash_hmac($this->hashAlgorithm, $this->dateWithoutTime, 'AWS4' . $this->awsSecret_key, true);
$regionKey = hash_hmac($this->hashAlgorithm, $this->awsRegion, $dateKey, true);
$serviceKey = hash_hmac($this->hashAlgorithm, $this->awsService, $regionKey, true);
return hash_hmac($this->hashAlgorithm, $this->awsTerminationString, $serviceKey, true);
$date_key = hash_hmac(
$this->hash_algorithm,
$this->date_without_time,
'AWS4' . $this->aws_secret_key,
true
);
$region_key = hash_hmac(
$this->hash_algorithm,
$this->aws_region,
$date_key,
true
);
$service_key = hash_hmac(
$this->hash_algorithm,
$this->aws_service,
$region_key,
true
);
return hash_hmac(
$this->hash_algorithm,
$this->aws_termination_string,
$service_key,
true
);
}
}

View File

@ -4,11 +4,15 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class ElasticEmail {
function __construct($apiKey, $fromEmail, $fromName) {
$this->url = 'https://api.elasticemail.com/mailer/send';
$this->apiKey = $apiKey;
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
public $url = 'https://api.elasticemail.com/mailer/send';
public $api_key;
public $from_email;
public $from_name;
function __construct($api_key, $from_email, $from_name) {
$this->api_key = $api_key;
$this->from_email = $from_email;
$this->from_name = $from_name;
}
function send($newsletter, $subscriber) {
@ -23,9 +27,9 @@ class ElasticEmail {
function getBody($newsletter, $subscriber) {
$body = array(
'api_key' => $this->apiKey,
'from' => $this->fromEmail,
'from_name' => $this->fromName,
'api_key' => $this->api_key,
'from' => $this->from_email,
'from_name' => $this->from_name,
'to' => $subscriber,
'subject' => $newsletter['subject']
);

View File

@ -4,9 +4,13 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class MailGun {
function __construct($domain, $apiKey, $from) {
public $url;
public $api_key;
public $from;
function __construct($domain, $api_key, $from) {
$this->url = sprintf('https://api.mailgun.net/v3/%s/messages', $domain);
$this->apiKey = $apiKey;
$this->api_key = $api_key;
$this->from = $from;
}
@ -37,7 +41,7 @@ class MailGun {
}
function auth() {
return 'Basic ' . base64_encode('api:' . $this->apiKey);
return 'Basic ' . base64_encode('api:' . $this->api_key);
}
function request($newsletter, $subscriber) {

View File

@ -4,13 +4,17 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class MailPoet {
function __construct($apiKey, $fromEmail, $fromName) {
$this->url = 'https://bridge.mailpoet.com/api/messages';
$this->apiKey = $apiKey;
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
public $url = 'https://bridge.mailpoet.com/api/messages';
public $api_key;
public $from_email;
public $from_name;
function __construct($api_key, $from_email, $from_name) {
$this->api_key = $api_key;
$this->from_email = $from_email;
$this->from_name = $from_name;
}
function send($newsletter, $subscriber) {
$result = wp_remote_post(
$this->url,
@ -21,20 +25,20 @@ class MailPoet {
wp_remote_retrieve_response_code($result) === 201
);
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
if(!isset($subscriberData['email'])) {
$subscriberData = array(
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
if(!isset($subscriber_data['email'])) {
$subscriber_data = array(
'email' => $subscriber,
);
}
return array(
'email' => $subscriberData['email'],
'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : ''
'email' => $subscriber_data['email'],
'name' => (isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
);
}
function getBody($newsletter, $subscriber) {
$body = array(
'to' => (array(
@ -42,8 +46,8 @@ class MailPoet {
'name' => $subscriber['name']
)),
'from' => (array(
'address' => $this->fromEmail,
'name' => $this->fromName
'address' => $this->from_email,
'name' => $this->from_name
)),
'subject' => $newsletter['subject']
);
@ -55,11 +59,11 @@ class MailPoet {
}
return $body;
}
function auth() {
return 'Basic ' . base64_encode('api:' . $this->apiKey);
return 'Basic ' . base64_encode('api:' . $this->api_key);
}
function request($newsletter, $subscriber) {
$body = array($this->getBody($newsletter, $subscriber));
return array(

View File

@ -4,11 +4,15 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class Mandrill {
function __construct($apiKey, $fromEmail, $fromName) {
$this->url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$this->apiKey = $apiKey;
$this->fromName = $fromName;
$this->fromEmail = $fromEmail;
public $url = 'https://mandrillapp.com/api/1.0/messages/send.json';
public $api_key;
public $from_email;
public $from_name;
function __construct($api_key, $from_email, $from_name) {
$this->api_key = $api_key;
$this->from_name = $from_name;
$this->from_email = $from_email;
}
function send($newsletter, $subscriber) {
@ -24,24 +28,24 @@ class Mandrill {
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
if(!isset($subscriberData['email'])) {
$subscriberData = array(
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
if(!isset($subscriber_data['email'])) {
$subscriber_data = array(
'email' => $subscriber,
);
}
return array(
'email' => $subscriberData['email'],
'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : ''
'email' => $subscriber_data['email'],
'name' => (isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
);
}
function getBody($newsletter, $subscriber) {
$body = array(
'key' => $this->apiKey,
'key' => $this->api_key,
'message' => array(
'from_email' => $this->fromEmail,
'from_name' => $this->fromName,
'from_email' => $this->from_email,
'from_name' => $this->from_name,
'to' => array($subscriber),
'subject' => $newsletter['subject']
),

View File

@ -4,16 +4,27 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class SMTP {
function __construct($host, $port, $authentication, $login = null, $password = null, $encryption,
$fromEmail, $fromName) {
public $host;
public $port;
public $authentication;
public $login;
public $password;
public $encryption;
public $from_name;
public $from_email;
public $mailer;
function __construct(
$host, $port, $authentication, $login = null, $password = null, $encryption,
$from_email, $from_name) {
$this->host = $host;
$this->port = $port;
$this->authentication = $authentication;
$this->login = $login;
$this->password = $password;
$this->encryption = $encryption;
$this->fromName = $fromName;
$this->fromEmail = $fromEmail;
$this->from_name = $from_name;
$this->from_email = $from_email;
$this->mailer = $this->buildMailer();
}
@ -42,7 +53,7 @@ class SMTP {
function createMessage($newsletter, $subscriber) {
$message = \Swift_Message::newInstance()
->setFrom(array($this->fromEmail => $this->fromName))
->setFrom(array($this->from_email => $this->from_name))
->setTo($this->processSubscriber($subscriber))
->setSubject($newsletter['subject']);
if(!empty($newsletter['body']['html'])) {
@ -55,15 +66,15 @@ class SMTP {
}
function processSubscriber($subscriber) {
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
if(!isset($subscriberData['email'])) {
$subscriberData = array(
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
if(!isset($subscriber_data['email'])) {
$subscriber_data = array(
'email' => $subscriber,
);
}
return array(
$subscriberData['email'] =>
(isset($subscriberData['name'])) ? $subscriberData['name'] : ''
$subscriber_data['email'] =>
(isset($subscriber_data['name'])) ? $subscriber_data['name'] : ''
);
}
}

View File

@ -4,11 +4,15 @@ namespace MailPoet\Mailer\Methods;
if(!defined('ABSPATH')) exit;
class SendGrid {
function __construct($apiKey, $fromEmail, $fromName) {
$this->url = 'https://api.sendgrid.com/api/mail.send.json';
$this->apiKey = $apiKey;
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
public $url = 'https://api.sendgrid.com/api/mail.send.json';
public $api_key;
public $from_email;
public $from_name;
function __construct($api_key, $from_email, $from_name) {
$this->api_key = $api_key;
$this->from_email = $from_email;
$this->from_name = $from_name;
}
function send($newsletter, $subscriber) {
@ -27,8 +31,8 @@ class SendGrid {
function getBody($newsletter, $subscriber) {
$body = array(
'to' => $subscriber,
'from' => $this->fromEmail,
'fromname' => $this->fromName,
'from' => $this->from_email,
'from_name' => $this->from_name,
'subject' => $newsletter['subject']
);
if(!empty($newsletter['body']['html'])) {
@ -41,7 +45,7 @@ class SendGrid {
}
function auth() {
return 'Bearer ' . $this->apiKey;
return 'Bearer ' . $this->api_key;
}
function request($newsletter, $subscriber) {

View File

@ -6,18 +6,17 @@ require_once(ABSPATH . 'wp-includes/pluggable.php');
if(!defined('ABSPATH')) exit;
class WPMail {
function __construct($fromEmail, $fromName) {
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
add_filter('wp_mail_from', array(
$this,
'setFromEmail'
));
$this->filters = array(
'wp_mail_from' => 'setFromEmail',
'wp_mail_from_name' => 'setFromName',
'wp_mail_content_type' => 'setContentType'
);
public $from_email;
public $from_name;
public $filters = array(
'wp_mail_from' => 'setFromEmail',
'wp_mail_from_name' => 'setFromName',
'wp_mail_content_type' => 'setContentType'
);
function __construct($from_email, $from_name) {
$this->from_email = $from_email;
$this->from_name = $from_name;
}
function addFilters() {
@ -39,11 +38,11 @@ class WPMail {
}
function setFromEmail() {
return $this->fromEmail;
return $this->from_email;
}
function setFromName() {
return $this->fromName;
return $this->from_name;
}
function setContentType() {
@ -54,7 +53,9 @@ class WPMail {
$this->addFilters();
$result = wp_mail(
$subscriber, $newsletter['subject'],
(!empty($newsletter['body']['html'])) ? $newsletter['body']['html'] : $newsletter['body']['text']
(!empty($newsletter['body']['html'])) ?
$newsletter['body']['html'] :
$newsletter['body']['text']
);
$this->removeFilters();
return ($result === true);