View source
<?php
define('MESSAGING_TYPE_PUSH', 1);
define('MESSAGING_TYPE_PULL', 2);
define('MESSAGING_EMPTY', '<none>');
function messaging_help($section) {
switch ($section) {
case 'admin/help#messaging':
$output = '<p>' . t('The messaging module is the engine that handles outgoing messages and message queueing for different sending methods.') . '</p>';
$output .= '<p>' . t('You need to enable one or more of the included plug-ins to be able to actually take advantage of it.') . '</p>';
return $output;
case 'admin/messaging/template':
$output = '<p>' . t('Configure the templates for different types of messages. Each message group is defined by other modules using the Messaging Framework.') . '</p>';
return $output;
default:
if (arg(0) == 'admin') {
if (arg(1) == 'settings' && arg(2) == 'filters') {
return '<p>' . t('Filters are used also for messaging. If the input format is to be used only for messaging you don\'t need to allow any role for it.') . '</p>';
}
if (arg(1) == 'messaging' && arg(2) == 'template' && arg(3) == 'edit' && ($group = arg(4))) {
$output = '<p>' . t('These are the message parts for %group. Leave blank to use the default texts or use \'%empty\' for an empty message part, preventing fallback to default message texts.', array(
'%group' => messaging_message_group($group, 'name'),
'%empty' => MESSAGING_EMPTY,
)) . '</p>';
if ($help = messaging_message_group($group, 'help')) {
$output .= '<p>' . $help . '</p>';
}
return $output;
}
}
}
}
function messaging_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'title' => t('Messaging'),
'path' => 'admin/messaging',
'callback' => 'messaging_admin_overview_page',
'access' => user_access('administer messaging'),
'description' => t('Administration of messages and sending methods'),
);
$items[] = array(
'title' => t('Messaging methods'),
'description' => t('Configuration of sending methods'),
'path' => 'admin/messaging/settings',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'messaging_admin_settings',
),
);
$items[] = array(
'title' => t('Messaging templates'),
'callback' => 'messaging_admin_template_overview',
'description' => t('Configuration of message templates'),
'path' => 'admin/messaging/template',
);
$items[] = array(
'title' => t('Messaging templates'),
'path' => 'admin/messaging/template/edit',
'callback' => 'messaging_admin_message_edit',
'type' => MENU_CALLBACK,
);
}
return $items;
}
function messaging_perm() {
return array(
'administer messaging',
);
}
function messaging_user($type, $edit, &$user, $category = NULL) {
switch ($type) {
case 'form':
if ($category == 'account' && ($list = messaging_method_list($user))) {
$form['messaging'] = array(
'#type' => 'fieldset',
'#title' => t('Messaging settings'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['messaging']['messaging_default'] = array(
'#type' => 'select',
'#title' => t('Default send method'),
'#default_value' => messaging_method_default($user),
'#options' => $list,
'#description' => t('Default sending method for getting messages from this system.'),
'#disabled' => count($list) == 1,
);
return $form;
}
break;
}
}
function messaging_admin_overview_page() {
$menu = menu_get_item(NULL, 'admin/messaging');
$content = system_admin_menu_block($menu);
$output = theme('admin_block_content', $content);
return $output;
}
function messaging_admin_page($group = NULL, $msgkey = NULL) {
if ($group) {
return messaging_admin_page_group($group, $msgkey);
}
else {
return messaging_admin_page_overview();
}
}
function messaging_admin_template_overview() {
$output = '';
$groups = module_invoke_all('messaging', 'message groups');
$rows = array();
foreach ($groups as $group => $group_info) {
$rows[] = array(
l($group_info['name'], 'admin/messaging/template/edit/' . $group),
!empty($group_info['description']) ? $group_info['description'] : '',
);
}
$output .= theme('box', t('Message groups'), theme('table', NULL, $rows));
$rows = array();
messaging_method_list();
foreach (messaging_method_info() as $method => $info) {
$rows[] = array(
'<strong>' . $info['name'] . '</strong>',
!empty($info['description']) ? $info['description'] : '',
);
}
$output .= theme('box', t('Sending methods'), theme('table', NULL, $rows));
return $output;
}
function messaging_admin_message_edit($group) {
$output = '';
$groups = module_invoke_all('messaging', 'message groups');
if (isset($groups[$group])) {
drupal_set_title($groups[$group]['name']);
$output .= drupal_get_form('messaging_admin_message_form', $group, $groups[$group]);
}
return $output;
}
function messaging_admin_message_form($group, $group_info) {
$form['group'] = array(
'#type' => 'value',
'#value' => $group,
);
$keylist = module_invoke_all('messaging', 'message keys', $group);
$send_methods = array(
'default' => t('Default'),
);
$send_methods += messaging_method_list();
$form['messages'] = array(
'#tree' => TRUE,
);
foreach ($keylist as $key => $keyname) {
$form['messages'][$key] = array(
'#type' => 'fieldset',
'#title' => $keyname,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
foreach ($send_methods as $method => $methodname) {
$text = messaging_message_part($group, $key, $method, FALSE);
$form['messages'][$key][$method] = array(
'#title' => $methodname,
'#type' => 'textarea',
'#default_value' => $text,
'#rows' => count(explode("\n", $text)),
);
}
}
if ($tokens = messaging_tokens_get_list($group)) {
$headers = array(
t('Token'),
t('Replacement value'),
);
$rows = array();
foreach ($tokens as $token => $token_description) {
$row = array();
$row[] = '[' . $token . ']';
$row[] = $token_description;
$rows[] = $row;
}
$form['tokens'] = array(
'#title' => t('Available tokens'),
'#type' => 'fieldset',
'#description' => t('These special strings will be replaced by their real value at run time.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['tokens']['list'] = array(
'#value' => theme('table', $headers, $rows, array(
'class' => 'description',
)),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function messaging_tokens_get_list($group) {
$type_list = module_invoke_all('messaging', 'tokens', $group);
$return = array();
foreach ($type_list as $type) {
if ($list = token_get_list($type)) {
foreach ($list as $category => $tokens) {
foreach ($tokens as $token => $description) {
$return[$token] = $description;
}
}
}
}
return $return;
}
function messaging_admin_message_form_submit($form_id, $form_values) {
$group = $form_values['group'];
foreach ($form_values['messages'] as $key => $messages) {
foreach ($messages as $method => $text) {
db_query("DELETE FROM {messaging_message_parts} WHERE type = '%s' AND msgkey = '%s' AND method = '%s'", $group, $key, $method);
if ($text = trim($text)) {
db_query("INSERT INTO {messaging_message_parts} (type, msgkey, method, module, message) VALUES('%s', '%s', '%s', '', '%s')", $group, $key, $method, $text);
}
}
}
drupal_set_message('The messages have been updated');
}
function messaging_admin_settings() {
$methods = messaging_method_list();
if (!$methods) {
drupal_set_message(t('No sending method plug-ins available. Please enable some Sending Method on the !admin-modules page.', array(
'!admin-modules' => l(t('Modules administration'), 'admin/build/modules'),
)), 'error');
}
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
);
$form['general']['messaging_default_method'] = array(
'#title' => t('Default send method'),
'#type' => 'radios',
'#options' => $methods,
'#default_value' => variable_get('messaging_default_method', ''),
);
$period = array(
0 => t('Disabled'),
) + drupal_map_assoc(array(
3600,
10800,
21600,
32400,
43200,
86400,
172800,
259200,
604800,
1209600,
2419200,
4838400,
9676800,
), 'format_interval');
$form['general']['messaging_log'] = array(
'#title' => t('Logging'),
'#type' => 'select',
'#options' => $period,
'#default_value' => variable_get('messaging_log', 0),
'#description' => t('If enabled all messages will be logged and kept for the specified time after they\'re sent.'),
);
$form['messaging_methods'] = array(
'#type' => 'fieldset',
'#title' => t('Settings for messaging methods'),
'#collapsible' => TRUE,
'#description' => t('Depending on your content format and the tokens you are using for messages it is important that you use the right filtering for the message body.') . ' ' . t('Set up the filters you need using the !input_formats page', array(
'!input_formats' => l('Input Formats', 'admin/settings/filters'),
)),
);
if ($info = messaging_method_info()) {
foreach (filter_formats() as $format) {
$format_options[$format->format] = $format->name;
}
$format_options[0] = t('None (Insecure)');
foreach ($info as $method => $options) {
$key = 'messaging_method_' . $method;
$form['messaging_methods'][$key] = array(
'#type' => 'fieldset',
'#title' => t('!name settings', array(
'!name' => $options['name'],
)),
'#tree' => TRUE,
);
$form['messaging_methods'][$key]['filter'] = array(
'#type' => 'select',
'#title' => t('Message body filter'),
'#default_value' => isset($options['filter']) ? $options['filter'] : variable_get('messaging_default_filter', ''),
'#options' => $format_options,
);
}
}
else {
$form['messaging_methods']['warning'] = array(
'#value' => t('You should enable some messaging method plug-ins for this to work.'),
);
}
$limit = variable_get('messaging_process_limit', array(
'message' => 0,
'percent' => 0,
'time' => 0,
));
$form['messaging_process_limit'] = array(
'#type' => 'fieldset',
'#title' => t('Limits for queue processing'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('These are the limits for each cron run on queue processing. The process will stop when it first meets any of them. Set to 0 for no limit.'),
);
$form['messaging_process_limit']['message'] = array(
'#title' => t('Number of messages sent'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $limit['message'],
);
$form['messaging_process_limit']['time'] = array(
'#title' => t('Time (seconds)'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $limit['time'],
);
$form['messaging_process_limit']['percent'] = array(
'#title' => t('Time (% of cron time)'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $limit['percent'],
'#description' => t('Maximum percentage of cron time the process may use.'),
);
return system_settings_form($form);
}
function messaging_message_send_user($account, $message, $method = NULL, $queue = 0) {
$method = $method ? $method : messaging_method_default($account);
$message['account'] = $account;
if (variable_get('messaging_debug', 0) && function_exists('messaging_debug_send_user')) {
return messaging_debug_send_user($account, $message, $method);
}
if (($function = messaging_method_info($method, 'send_user')) && function_exists($function)) {
return $function($account, $message, $method);
}
if ($property = messaging_method_info($method, 'destination')) {
$destination[] = $account->{$property};
}
else {
$destination[] = $account;
}
return messaging_message_send($destination, $message, $method, $queue);
}
function messaging_message_send($destinations, $message, $method, $queue = 0) {
$method = $method ? $method : messaging_method_default(NULL);
$info = messaging_method_info($method);
foreach (module_implements('message_alter') as $module) {
$function = $module . '_message_alter';
$function($message, $info, $method);
}
if (!empty($info['render'])) {
$message = call_user_func($info['render'], $message, $info);
}
else {
$message = messaging_message_render($message, $info);
}
$sent = $cron = $log = 0;
if ($queue && $info['type'] & MESSAGING_TYPE_PUSH) {
$cron = 1;
}
if ($queue || $info['type'] & MESSAGING_TYPE_PULL) {
$queue = 1;
}
if (variable_get('messaging_log', 0)) {
$log = 1;
}
if (variable_get('messaging_debug', 0) && function_exists('messaging_debug_send_msg')) {
$log = 1;
$cron = 0;
$method = 'debug';
}
if (!$queue) {
$success = TRUE;
foreach ($destinations as $to) {
$success = messaging_message_send_out($to, $message, $method) && $success;
}
$success ? $sent = time() : ($log = 1);
}
if ($queue || $log) {
messaging_store('save', $method, $destinations, $message, $sent, $queue, $log, $cron);
$sent = TRUE;
}
return $sent || $queue;
}
function messaging_message_send_out($destination, $message, $method) {
if (($function = messaging_method_info($method, 'send')) && function_exists($function)) {
$group = messaging_method_info($method, 'group');
$params = !empty($message['params'][$group]) ? $message['params'][$group] : array();
return $function($destination, $message, $params);
}
else {
watchdog('messaging', t('Message could not be delivered for method %method', array(
'%method' => $method,
)), WATCHDOG_ERROR);
return FALSE;
}
}
function messaging_cron() {
messaging_store('queue_process');
messaging_store('queue_cleanup');
}
function messaging_pull_pending($method, $users, $limit = 0, $delete = TRUE) {
return messaging_store('pull_pending', $method, $users, $limit, $delete);
}
function messaging_method_type($type) {
$result = array();
foreach (messaging_method_info() as $method => $info) {
if ($info['type'] & $type) {
$result[$method] = $info;
}
}
return $result;
}
function messaging_method_list($account = NULL) {
$info = messaging_method_info(NULL, 'name');
if ($account) {
foreach (array_keys($info) as $method) {
if (!messaging_method_permission($method, $account)) {
unset($info[$method]);
}
}
}
return $info;
}
function messaging_method_permission($method, $account = NULL) {
$info = messaging_method_info($method);
if (!$info) {
return FALSE;
}
elseif (!empty($info['access'])) {
return user_access($info['access'], $account);
}
else {
return TRUE;
}
}
function messaging_method_default($account = NULL) {
if ($account && $account->messaging_default && messaging_method_permission($account->messaging_default, $account)) {
return $account->messaging_default;
}
elseif ($method = variable_get('messaging_default_method', '')) {
return $method;
}
else {
return key(messaging_method_info());
}
}
function messaging_message_part($group, $key, $method = 'default', $getdefault = TRUE) {
static $cache;
if (isset($cache[$group][$key][$method])) {
$text_part = $cache[$group][$key][$method];
}
else {
if ($method && ($text = db_result(db_query("SELECT message FROM {messaging_message_parts} WHERE type = '%s' AND msgkey = '%s' AND method = '%s'", $group, $key, $method)))) {
$text_part = $text;
}
elseif ($method == 'default' && ($text = messaging_message_info($group, $key))) {
$text_part = $text;
}
elseif ($method != 'default' && $getdefault && ($text = messaging_message_part($group, $key, 'default'))) {
$text_part = $text;
}
else {
$text_part = FALSE;
}
if ($text_part && is_array($text_part)) {
$text_part = implode("\n", $text_part);
}
$cache[$group][$key][$method] = $text_part;
}
return $text_part ? $text_part : '';
}
function messaging_message_info($group, $key = NULL) {
static $info;
if (!isset($info[$group])) {
$info[$group] = module_invoke_all('messaging', 'messages', $group);
}
if ($key) {
return isset($info[$group][$key]) ? $info[$group][$key] : NULL;
}
elseif ($group) {
return isset($info[$group]) ? $info[$group] : array();
}
else {
return $info;
}
}
function messaging_message_group($group = NULL, $key = NULL) {
static $info;
if (!isset($info)) {
$info = module_invoke_all('messaging', 'message groups');
}
if ($key) {
return isset($info[$group][$key]) ? $info[$group][$key] : NULL;
}
elseif ($group) {
return isset($info[$group]) ? $info[$group] : array();
}
else {
return $info;
}
}
function messaging_method_info($method = NULL, $property = NULL, $default = NULL, $refresh = FALSE) {
static $info, $properties;
if (!$info || $refresh) {
$info = module_invoke_all('messaging', 'send methods');
foreach (array_keys($info) as $name) {
$info[$name] = array_merge($info[$name], variable_get('messaging_method_' . $name, array()));
}
}
if ($method && $property) {
return isset($info[$method][$property]) ? $info[$method][$property] : $default;
}
elseif ($method) {
return isset($info[$method]) ? $info[$method] : array();
}
elseif ($property) {
if (!isset($properties[$property])) {
$properties[$property] = array();
foreach ($info as $method => $values) {
if (isset($values[$property])) {
$properties[$property][$method] = $values[$property];
}
}
}
return $properties[$property];
}
else {
return $info;
}
}
function messaging_message_render($message, $info) {
if (!empty($message['rendered'])) {
return $message;
}
if (!empty($info['footer']) && is_array($message['body']) && isset($message['body']['footer'])) {
$message['body']['footer'] = array(
'#prefix' => $info['footer'],
'#text' => $message['body']['footer'],
);
}
$info += array(
'glue' => '',
'subject_glue' => '',
);
$message['subject'] = messaging_check_subject(messaging_text_render($message['subject'], $info['subject_glue']));
$message['body'] = messaging_text_render($message['body'], $info['glue'], $info['filter']);
$message['rendered'] = 1;
return $message;
}
function messaging_text_render($text, $glue = '', $filter = NULL) {
$output = '';
if (is_array($text)) {
if (isset($text['#prefix'])) {
$output .= $text['#prefix'] . $glue;
unset($text['#prefix']);
}
if (isset($text['#text'])) {
$output .= $text['#text'];
return $output;
}
foreach (element_children($text) as $key) {
$text[$key] = messaging_text_render($text[$key], $glue);
}
$output .= implode($glue, $text);
}
else {
$output .= $text;
}
if ($filter) {
$output = check_markup($output, $filter, FALSE);
}
return $output;
}
function messaging_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
$info[0] = t('Plain text filter');
if (function_exists('drupal_html_to_text')) {
$info[1] = t('HTML to text');
}
return $info;
case 'no cache':
return TRUE;
case 'description':
switch ($delta) {
case 0:
return t('Filters out all HTML tags and replaces HTML entities by characters.');
case 1:
return t('Replaces HTML tags and entities with plain text formatting, moving links at the end. This filter is just for text messages and it isn\'t safe for rendering content on a web page.');
}
case 'process':
switch ($delta) {
case 0:
return messaging_check_plain($text);
case 1:
return messaging_html_to_text($text);
default:
return $text;
}
case 'settings':
return;
default:
return $text;
}
}
function messaging_html_to_text($text) {
if (function_exists('drupal_html_to_text')) {
return drupal_html_to_text($text);
}
else {
return messaging_check_plain($text);
}
}
function messaging_check_plain($text) {
$text = str_replace(array(
'<',
'>',
), array(
'<',
'>',
), $text);
$text = filter_xss($text, array());
$text = decode_entities($text);
return $text;
}
function messaging_check_subject($text) {
$text = messaging_check_plain($text);
$text = preg_replace('=((0x0A/%0A|0x0D/%0D|\\n|\\r)\\S).*=i', NULL, $text);
return $text;
}
function messaging_store() {
static $include;
if (!isset($include)) {
require_once drupal_get_path('module', 'messaging') . '/messaging.store.inc';
$include = TRUE;
}
$args = func_get_args();
$function = 'messaging_store_' . array_shift($args);
return call_user_func_array($function, $args);
}
function messaging_log($txt = NULL) {
static $logs;
if ($txt) {
$logs[] = $txt;
}
else {
return $logs;
}
}
function messaging_load_user($uid) {
static $cache = array();
if (!array_key_exists($uid, $cache)) {
$cache[$uid] = user_load(array(
'uid' => $uid,
));
}
return $cache[$uid];
}
function messaging_simpletest() {
$dir = drupal_get_path('module', 'messaging') . DIRECTORY_SEPARATOR . 'tests';
require_once $dir . DIRECTORY_SEPARATOR . 'messaging_testcase.inc';
$tests = file_scan_directory($dir, '\\.test');
return array_keys($tests);
}
function messaging_mail_params($message, $params) {
if (empty($params['from'])) {
if (!empty($message['sender_account']) && !empty($message['sender_account']->mail)) {
$params['from'] = check_plain($message['sender_account']->name) . ' <' . $message['sender_account']->mail . '>';
}
elseif (!empty($message['sender_name']) && ($default_from = variable_get('site_mail', ini_get('sendmail_from')))) {
$params['from'] = check_plain($message['sender_name']) . ' <' . $default_from . '>';
}
}
$params += array(
'from' => NULL,
'headers' => array(),
'mailkey' => 'message-' . $message['type'],
);
return $params;
}