FINAL suepr merge step : added all modules to this super repos

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 16:46:59 +02:00
7585 changed files with 1723356 additions and 18 deletions

View File

@@ -0,0 +1,517 @@
<?php
/**
* @file
* Wrapper class around the Mandrill API.
*/
/**
* Class MandrillException.
*/
class MandrillException extends Exception {
}
/**
* Class DrupalMandrill.
*/
class DrupalMandrill {
const API_VERSION = '1.0';
const END_POINT = 'https://mandrillapp.com/api/';
protected $api;
/**
* Default to a 300 second timeout on server calls
*/
protected $timeout = 300;
/**
* Constructor to set internal values.
*
* @param string $api_key
* Mandrill API key.
* @param int $timeout
* Server timeout.
*
* @throws MandrillException.
*/
public function __construct($api_key, $timeout = 300) {
if (empty($api_key)) {
throw new MandrillException('Invalid API key');
}
try {
$response = $this->request('users/ping', array('key' => $api_key));
if ($response != 'PONG!') {
throw new MandrillException('Invalid API key: ' . $response);
}
$this->api = $api_key;
$this->timeout = $timeout;
}
catch (Exception $e) {
throw new MandrillException($e->getMessage());
}
}
/**
* Make a request to Mandrill's API.
*
* Every API call uses this function to actually make the request to
* Mandrill's servers.
*
* @link https://mandrillapp.com/api/docs/
*
* @param string $method
* API method name
* @param array $args
* query arguments
* @param string $http
* GET or POST request type
* @param string $output
* API response format (json,php,xml,yaml). json and xml are decoded into
* arrays automatically.
*
* @return array
* Array on success.
*
* @throws MandrillException.
*/
protected function request($method, $args = array(), $http = 'POST', $output = 'json') {
if (!isset($args['key'])) {
$args['key'] = $this->api;
}
$api_version = self::API_VERSION;
$dot_output = ('json' == $output) ? '' : ".{$output}";
$url = self::END_POINT . "{$api_version}/{$method}{$dot_output}";
$params = drupal_json_encode($args);
switch ($http) {
case 'GET':
$url .= '?' . $params;
$response = drupal_http_request($url, array(
'method' => 'GET',
'timeout' => $this->timeout,
));
break;
case 'POST':
$response = drupal_http_request($url, array(
'method' => 'POST',
'data' => $params,
'timeout' => $this->timeout,
));
break;
default:
throw new MandrillException('Unknown request type');
}
$response_code = $response->code;
if (0 == $response_code) {
return $response->error;
}
$body = $response->data;
switch ($output) {
case 'json':
$body = json_decode($body, TRUE);
break;
case 'php':
$body = unserialize($body);
break;
}
if (200 == $response_code) {
return $body;
}
else {
$message = isset($body['message']) ? $body['message'] : $body;
if (is_array($message)) {
$message = "Unspecified Error";
}
throw new MandrillException($message, $response_code);
}
}
/**
* @link https://mandrillapp.com/api/docs/users.html#method=ping
*
* @return array|MandrillException
*/
public function users_ping() {
return $this->request('users/ping');
}
/**
* @link https://mandrillapp.com/api/docs/users.html#method=info
*
* @return array|MandrillException
*/
public function users_info() {
return $this->request('users/info');
}
/**
* @link https://mandrillapp.com/api/docs/users.html#method=senders
*
* @return array|MandrillException
*/
public function users_senders() {
return $this->request('users/senders');
}
/**
* @link https://mandrillapp.com/api/docs/senders.html#method=domains
*
* @return array|MandrillException
*/
public function senders_domains() {
return $this->request('senders/domains');
}
/**
* @link https://mandrillapp.com/api/docs/senders.html#method=list
*
* @return array|MandrillException
*/
public function senders_list() {
return $this->request('senders/list');
}
/**
* @link https://mandrillapp.com/api/docs/senders.html#method=info
*
* @return array|MandrillException
*/
public function senders_info($email) {
return $this->request('senders/info', array('address' => $email));
}
/**
* @link https://mandrillapp.com/api/docs/senders.html#method=time-series
*
* @return array|MandrillException
*/
public function senders_time_series($email) {
return $this->request('senders/time-series', array('address' => $email));
}
/**
* @link https://mandrillapp.com/api/docs/subaccounts.html#method=method-list
*
* @return array|MandrillException
*/
public function subaccounts() {
return $this->request('subaccounts/list');
}
/**
* @link https://mandrillapp.com/api/docs/tags.html#method=list
*
* @return array|MandrillException
*/
public function tags_list() {
return $this->request('tags/list');
}
/**
* @link https://mandrillapp.com/api/docs/tags.html#method=info
*
* @return array|MandrillException
*/
public function tags_info($tag) {
return $this->request('tags/info', array('tag' => $tag));
}
/**
* @link https://mandrillapp.com/api/docs/tags.html#method=time-series
*
* @return array|MandrillException
*/
public function tags_time_series($tag) {
return $this->request('tags/time-series', array('tag' => $tag));
}
/**
* @link https://mandrillapp.com/api/docs/tags.html#method=all-time-series
*
* @return array|MandrillException
*/
public function tags_all_time_series() {
return $this->request('tags/all-time-series');
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=add
*
* @return array|MandrillException
*/
public function templates_add($name, $code) {
return $this->request('templates/add', array(
'name' => $name,
'code' => $code,
));
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=update
*
* @return array|MandrillException
*/
public function templates_update($name, $code) {
return $this->request('templates/update', array(
'name' => $name,
'code' => $code,
));
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=delete
*
* @return array|MandrillException
*/
public function templates_delete($name) {
return $this->request('templates/delete', array('name' => $name));
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=info
*
* @return array|MandrillException
*/
public function templates_info($name) {
return $this->request('templates/info', array('name' => $name));
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=list
*
* @return array|MandrillException
*/
public function templates_list() {
return $this->request('templates/list');
}
/**
* @link https://mandrillapp.com/api/docs/templates.html#method=time-series
*
* @return array|MandrillException
*/
public function templates_time_series($name) {
return $this->request('templates/time-series', array('name' => $name));
}
/**
* @link https://mandrillapp.com/api/docs/urls.html#method=list
*
* @return array|MandrillException
*/
public function urls_list() {
return $this->request('urls/list');
}
/**
* @link https://mandrillapp.com/api/docs/urls.html#method=time-series
*
* @return array|MandrillException
*/
public function urls_time_series($url) {
return $this->request('urls/time-series', array('url' => $url));
}
/**
* @link https://mandrillapp.com/api/docs/urls.html#method=search
*
* @return array|MandrillException
*/
public function urls_search($q) {
return $this->request('urls/search', array('q' => $q));
}
/**
* @link https://mandrillapp.com/api/docs/webhooks.html#method=add
*
* @return array|MandrillException
*/
public function webhooks_add($url, $events) {
return $this->request('webhooks/add', array(
'url' => $url,
'events' => $events,
));
}
/**
* @link https://mandrillapp.com/api/docs/webhooks.html#method=update
*
* @return array|MandrillException
*/
public function webhooks_update($id, $url, $events) {
return $this->request('webhooks/update', array(
'id' => $id,
'url' => $url,
'events' => $events,
));
}
/**
* @link https://mandrillapp.com/api/docs/webhooks.html#method=delete
*
* @return array|MandrillException
*/
public function webhooks_delete($id) {
return $this->request('webhooks/delete', array('id' => $id));
}
/**
* @link https://mandrillapp.com/api/docs/webhooks.html#method=info
*
* @return array|MandrillException
*/
public function webhooks_info($id) {
return $this->request('webhooks/info', array('id' => $id));
}
/**
* @link https://mandrillapp.com/api/docs/webhooks.html#method=list
*
* @return array|MandrillException
*/
public function webhooks_list() {
return $this->request('webhooks/list');
}
/**
* @link https://mandrillapp.com/api/docs/messages.html#method=search
*
* @return array|MandrillException
*/
public function messages_search($query, $date_from = '', $date_to = '', $tags = array(), $senders = array(), $limit = 100) {
return $this->request('messages/search', compact('query', 'date_from', 'date_to', 'tags', 'senders', 'limit'));
}
/**
* @link https://mandrillapp.com/api/docs/messages.html#method=send
*
* @return array|MandrillException
*/
public function messages_send($message) {
return $this->request('messages/send', array('message' => $message));
}
/**
* @link https://mandrillapp.com/api/docs/messages.html#method=send-template
*
* @return array|MandrillException
*/
public function messages_send_template($template_name, $template_content, $message) {
return $this->request('messages/send-template', compact('template_name', 'template_content', 'message'));
}
/**
* Return an array structure for a message attachment.
*
* @param string $path
* Attachment path.
*
* @return array
* Attachment structure.
*
* @throws MandrillException
* @throws Exception
*/
public static function getAttachmentStruct($path) {
$struct = array();
try {
if (!@is_file($path)) {
throw new Exception($path . ' is not a valid file.');
}
$filename = basename($path);
if (!function_exists('get_magic_quotes')) {
function get_magic_quotes() {
return FALSE;
}
}
if (!function_exists('set_magic_quotes')) {
function set_magic_quotes($value) {
return TRUE;
}
}
if (strnatcmp(phpversion(), '6') >= 0) {
$magic_quotes = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
}
$file_buffer = file_get_contents($path);
$file_buffer = chunk_split(base64_encode($file_buffer), 76, "\n");
if (strnatcmp(phpversion(), '6') >= 0) {
set_magic_quotes_runtime($magic_quotes);
}
$mime_type = file_get_mimetype($path);
if (!DrupalMandrill::isValidContentType($mime_type)) {
throw new Exception($mime_type . ' is not a valid content type (it should be ' . implode('*,', self::getValidContentTypes()) . ').');
}
$struct['type'] = $mime_type;
$struct['name'] = $filename;
$struct['content'] = $file_buffer;
}
catch (Exception $e) {
throw new MandrillException('Error creating the attachment structure: ' . $e->getMessage());
}
return $struct;
}
/**
* Helper to determine attachment is valid.
*
* @static
*
* @param $ct
*
* @return bool
*/
protected static function isValidContentType($ct) {
$valids = self::getValidContentTypes();
foreach ($valids as $vct) {
if (strpos($ct, $vct) !== FALSE) {
return TRUE;
}
}
return FALSE;
}
/**
* Return an array of valid content types.
*
* @static
*
* @return array
* Valid content types to attach to an email.
*/
protected static function getValidContentTypes() {
$valid_types = array(
'image/',
'text/',
'application/pdf',
'application/x-zip',
);
drupal_alter('mandrill_valid_attachment_types', $valid_types);
return $valid_types;
}
}

View File

@@ -0,0 +1,184 @@
<?php
/**
* @file
* Implements Mandrill as a Drupal MailSystemInterface
*/
/**
* Modify the drupal mail system to use Mandrill when sending emails.
*/
class MandrillMailSystem implements MailSystemInterface {
/**
* Concatenate and wrap the email body for either plain-text or HTML emails.
*
* @param array $message
* A message array, as described in hook_mail_alter().
*
* @return array
* The formatted $message.
*/
public function format(array $message) {
// Join the body array into one string.
if (is_array($message['body'])) {
$message['body'] = implode("\n\n", $message['body']);
}
return $message;
}
/**
* Send the email message.
*
* @see drupal_mail()
*
* @param array $message
* A message array, as described in hook_mail_alter().
*
* @return bool
* TRUE if the mail was successfully accepted, otherwise FALSE.
*/
public function mail(array $message) {
// Optionally log mail keys not using Mandrill already. Helpful in
// configuring Mandrill.
if (variable_get('mandrill_log_defaulted_sends', FALSE)) {
$systems = mailsystem_get();
$registered = FALSE;
foreach ($systems as $key => $system) {
if ($message['id'] == $key) {
$registered = TRUE;
}
if (!$registered) {
watchdog(
'mandrill',
"Module: %module Key: %key invoked Mandrill to send email because Mandrill is configured as the default mail system. Specify alternate configuration for this module & key in !mailsystem if this is not desirable.",
array(
'%module' => $message['module'],
'%key' => $message['key'],
'!mailsystem' => l(t('Mail System'), 'admin/config/system/mailsystem'),
),
WATCHDOG_INFO
);
}
}
}
$mailer = mandrill_get_api_object();
// Apply input format to body.
$format = variable_get('mandrill_filter_format', '');
if (!empty($format)) {
$message['body'] = check_markup($message['body'], $format);
}
// Extract an array of recipients.
$to = mandrill_get_to($message['to']);
// Prepare headers, defaulting the reply-to to the from address since
// Mandrill needs the from address to be configured separately.
// Note that only Reply-To and X-* headers are allowed.
$headers = isset($message['headers']) ? $message['headers'] : array();
if (!empty($message['from']) && empty($headers['Reply-To'])) {
$headers['Reply-To'] = $message['from'];
}
// Prepare attachments.
$attachments = array();
if (isset($message['attachments']) && !empty($message['attachments'])) {
foreach ($message['attachments'] as $attachment) {
if (is_file($attachment)) {
$attachments[] = $mailer->getAttachmentStruct($attachment);
}
}
}
// Determine if content should be available for this message.
$blacklisted_keys = explode(',', mandrill_mail_key_blacklist());
$view_content = TRUE;
foreach ($blacklisted_keys as $key) {
if ($message['id'] == drupal_strtolower(trim($key))) {
$view_content = FALSE;
break;
}
}
// The Mime Mail module (mimemail) expects attachments as an array of file
// arrays in $message['params']['attachments']. As many modules assume you
// will be using Mime Mail to handle attachments, we need to parse this
// array as well.
if (isset($message['params']['attachments']) && !empty($message['params']['attachments'])) {
foreach ($message['params']['attachments'] as $attachment) {
$attachment_path = drupal_realpath($attachment['uri']);
if (is_file($attachment_path)) {
$struct = $mailer->getAttachmentStruct($attachment_path);
// Allow for customised filenames.
if (!empty($attachment['filename'])) {
$struct['name'] = $attachment['filename'];
}
$attachments[] = $struct;
}
}
// Remove the file objects from $message['params']['attachments'].
// (This prevents double-attaching in the drupal_alter hook below.)
unset($message['params']['attachments']);
}
// Account for the plaintext parameter provided by the mimemail module.
$plain_text = empty($message['params']['plaintext']) ? drupal_html_to_text($message['body']) : $message['params']['plaintext'];
// Get metadata.
$metadata = isset($message['metadata']) ? $message['metadata'] : array();
$from = mandrill_from();
$mandrill_message = array(
'html' => $message['body'],
'text' => $plain_text,
'subject' => $message['subject'],
'from_email' => $from['email'],
'from_name' => $from['name'],
'to' => $to,
'headers' => $headers,
'track_opens' => variable_get('mandrill_track_opens', TRUE),
'track_clicks' => variable_get('mandrill_track_clicks', TRUE),
// We're handling this with drupal_html_to_text().
'auto_text' => FALSE,
'url_strip_qs' => variable_get('mandrill_url_strip_qs', FALSE),
'bcc_address' => isset($message['bcc_email']) ? $message['bcc_email'] : NULL,
'tags' => array($message['id']),
'google_analytics_domains' => (variable_get('mandrill_analytics_domains', NULL)) ? explode(',', variable_get('mandrill_analytics_domains')) : array(),
'google_analytics_campaign' => variable_get('mandrill_analytics_campaign', ''),
'attachments' => $attachments,
'view_content_link' => $view_content,
'metadata' => $metadata,
);
$subaccount = variable_get('mandrill_subaccount', FALSE);
if ($subaccount) {
$mandrill_message['subaccount'] = $subaccount;
}
// Allow other modules to alter the Mandrill message, and sender/args.
$mandrill_params = array(
'message' => $mandrill_message,
'function' => 'mandrill_sender_plain',
'args' => array(),
);
drupal_alter('mandrill_mail', $mandrill_params, $message);
// Queue for processing during cron or send immediately.
$status = NULL;
if (mandrill_process_async()) {
$queue = DrupalQueue::get(MANDRILL_QUEUE, TRUE);
$queue->createItem($mandrill_params);
if (variable_get('mandrill_batch_log_queued', TRUE)) {
watchdog('mandrill', 'Message from %from to %to queued for delivery.',
array(
'%from' => $from['email'],
'%to' => $to[0]['email'],
), WATCHDOG_NOTICE);
}
return TRUE;
}
else {
return mandrill_mailsend($mandrill_params['message'], $mandrill_params['function'], $mandrill_params['args']);
}
}
}