View source
<?php
define('MESSAGING_TYPE_PUSH', 1);
define('MESSAGING_TYPE_PULL', 2);
define('MESSAGING_TYPE_OUTGOING', 4);
define('MESSAGING_TYPE_INCOMING', 8);
define('MESSAGING_TYPE_SEND', MESSAGING_TYPE_PUSH | MESSAGING_TYPE_OUTGOING);
define('MESSAGING_EMPTY', '<none>');
function messaging_help($path, $arg) {
switch ($path) {
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;
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') {
require_once drupal_get_path('module', 'messaging') . '/messaging.admin.inc';
return messaging_admin_help($path, $arg);
}
}
}
}
function messaging_menu() {
$items['admin/messaging'] = array(
'title' => 'Messaging',
'access arguments' => array(
'administer messaging',
),
'description' => 'Administer and configure messaging',
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/messaging/settings'] = array(
'title' => 'Messaging settings',
'description' => 'Configuration of messaging framework',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'messaging_admin_settings',
),
'access arguments' => array(
'administer messaging',
),
'file' => 'messaging.admin.inc',
);
$items['admin/messaging/settings/overview'] = array(
'title' => 'Messaging',
'description' => 'Configuration of sending methods',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/messaging/settings/method'] = array(
'title' => 'Send methods',
'description' => 'Configuration of sending methods',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'messaging_admin_method_settings',
),
'access arguments' => array(
'administer messaging',
),
'weight' => -10,
'file' => 'messaging.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/messaging/settings/method/overview'] = array(
'title' => 'General',
'description' => 'General settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/messaging/template'] = array(
'title' => 'Message templates',
'description' => 'Configuration of message templates',
'page callback' => 'messaging_admin_template',
'access arguments' => array(
'administer messaging',
),
'file' => 'messaging.admin.inc',
);
$items['admin/messaging/template/edit'] = array(
'title' => 'Message templates',
'page callback' => 'messaging_admin_template_edit',
'type' => MENU_CALLBACK,
'access arguments' => array(
'administer messaging',
),
'file' => 'messaging.admin.inc',
);
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_message_send_user($account, &$message, $method = NULL, $queue = 0) {
$message = (object) $message;
$method = $method ? $method : messaging_method_default($account);
$message->account = $account;
if ($destination = messaging_user_destination($account, $method, $message)) {
return messaging_message_send(array(
$destination,
), $message, $method, $queue);
}
else {
$message->method = $method;
$info = messaging_method_info($method, NULL, array());
$message->destination = 'ERROR';
$message = messaging_message_prepare($message, $info);
$message = messaging_message_render($message, $info);
$message->cron = $message->queue = 0;
$message->log = 1;
$message->success = FALSE;
$message = messaging_store('save', $message);
return FALSE;
}
}
function messaging_user_destination($account, $method, $message = NULL) {
if ($info = messaging_method_info($method)) {
if ($callback = _messaging_callback_get($info, 'destination')) {
return _messaging_callback_invoke($callback, $account, $method, $message);
}
elseif ($property = messaging_method_info($method, 'destination')) {
return !empty($account->{$property}) ? $account->{$property} : NULL;
}
}
}
function messaging_message_send($destinations, &$message, $method, $queue = 0) {
$method = $method ? $method : messaging_method_default(NULL);
$info = messaging_method_info($method, NULL, array());
$message = (object) $message;
$message->method = $method;
$message->source['type'] = 'outgoing';
$message->destinations = $destinations;
$message->queue = $queue;
$message->sent = $message->queued = 0;
$message->process = TRUE;
$message = messaging_message_callbacks(array(
'prepare',
'render',
'presend',
), $message, $info);
if ($message->process) {
if ($message->queue) {
$callbacks = array(
'queue',
'afterqueue',
);
}
else {
$callbacks = array(
'multisend',
'aftersend',
);
}
$message = messaging_message_callbacks($callbacks, $message, $info);
}
if (!isset($message->success)) {
$message->success = $message->sent || $message->queued;
}
return $message->success;
}
function messaging_message_invoke($callback_list, $message, $info) {
if (is_string($callback_list)) {
$callback_list = array(
$callback_list,
);
}
while ($message->process && ($callback = array_shift($callback_list))) {
$message = _messaging_callback_invoke($callback, $message, $info);
}
return $message;
}
function messaging_message_prepare($message, $info) {
global $user;
foreach (array(
'sent' => 0,
'queue' => 0,
'cron' => 0,
'log' => (bool) variable_get('messaging_log', 0),
'sender' => $user->uid,
) as $field => $value) {
if (!isset($message->{$field})) {
$message->{$field} = $value;
}
}
if ($message->queue && $info['type'] & MESSAGING_TYPE_PUSH) {
$message->cron = 1;
}
if (!$message->queue && $info['type'] & MESSAGING_TYPE_PULL) {
$message->queue = 1;
$message->cron = 0;
}
foreach (module_implements('message_alter') as $module) {
$function = $module . '_message_alter';
$function($message, $info);
}
return $message;
}
function messaging_message_multisend($message, $info) {
$success = TRUE;
foreach ($message->destinations as $to) {
$success = messaging_message_send_out($to, $message, $message->method) && $success;
}
$success ? $message->sent = time() : ($message->log = 1);
$message->success = $success;
return $message;
}
function messaging_message_queue($message, $info = array()) {
$message->queued = 1;
return messaging_store('save', $message);
}
function messaging_message_store($message, $info = array()) {
$message->stored = 1;
if (empty($message->mqid)) {
$message = messaging_store('save', $message);
}
return $message;
}
function messaging_message_aftersend($message, $info) {
if (empty($message->mqid) && $message->log) {
messaging_store('save', $message);
}
return $message;
}
function messaging_message_log($message, $info) {
if (empty($message->mqid)) {
messaging_store('save', $message);
}
return $message;
}
function messaging_message_send_out($destination, $message, $method) {
if ($callback = messaging_method_info($method, 'send callback')) {
$group = messaging_method_info($method, 'group');
$params = !empty($message->params[$group]) ? $message->params[$group] : array();
return _messaging_callback_invoke($callback, $destination, $message, $params);
}
else {
watchdog('messaging', '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 = NULL, $limit = 0, $delete = TRUE) {
$params['method'] = $method;
$params['queue'] = 1;
if (!is_null($users)) {
$params['uid'] = $users;
}
$messages = messaging_store_get($params);
if ($messages && $delete) {
messaging_store('sent', array_keys($messages));
}
return $messages;
}
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) || !messaging_user_destination($account, $method)) {
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 && !empty($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);
}
return _messaging_info($info, $group, $key);
}
function messaging_message_group($group = NULL, $key = NULL) {
static $info;
if (!isset($info)) {
$info = module_invoke_all('messaging', 'message groups');
}
return _messaging_info($info, $group, $key);
}
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()));
}
drupal_alter('messaging_methods', $info);
}
return _messaging_info($info, $method, $property, $default);
}
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' => '',
'filter' => NULL,
);
$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');
$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.');
}
case 'process':
switch ($delta) {
case 0:
return messaging_check_plain($text);
case 1:
return drupal_html_to_text($text);
default:
return $text;
}
case 'settings':
return;
default:
return $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_load_user($uid) {
static $cache = array();
if (!array_key_exists($uid, $cache)) {
$cache[$uid] = user_load(array(
'uid' => $uid,
));
}
return $cache[$uid];
}
function messaging_message_load($mqid, $refresh = FALSE) {
static $cache;
if (!isset($cache[$mqid]) || $refresh) {
if ($message = messaging_store('load', $mqid)) {
$cache[$mqid] = $message;
}
else {
$cache[$mqid] = FALSE;
}
}
return $cache[$mqid];
}
function messaging_text_replace($text, $objects) {
$objects['global'] = NULL;
if (module_exists('token_logic')) {
return token_logic_replace_multiple($text, $objects);
}
else {
return token_replace_multiple($text, $objects);
}
}
function messaging_requirements($phase) {
$requirements = array();
$t = get_t();
if ($phase == 'runtime') {
$methods = messaging_method_list();
if (!$methods) {
$requirements['messaging'] = array(
'title' => $t('Messaging sending methods'),
'value' => $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'),
)),
'severity' => REQUIREMENT_ERROR,
);
}
}
return $requirements;
}
function messaging_message_in($method, $channel, $message, $params = array()) {
if (function_exists('messaging_incoming_post')) {
return messaging_incoming_post($method, $channel, $message, $params);
}
else {
return FALSE;
}
}
function messaging_method_disable($method) {
module_load_include('install', 'messaging');
if ($replace = messaging_update_method_replace($method)) {
messaging_update_method_disable($method, $replace);
drupal_set_message(t('Disabled messaging sending method %method and replaced by %replace', array(
'%method' => messaging_method_info($method, 'title'),
'%replace' => messaging_method_info($replace, 'title'),
)));
}
else {
drupal_set_message(t('Disabled messaging sending method but cannot find a replacement. Please, enable some other sending method.'), 'error');
}
}
function _messaging_callback() {
$args = func_get_args();
if (count($args) > 1) {
return $args;
}
else {
return array_shift($args);
}
}
function _messaging_callback_invoke() {
$args = func_get_args();
$callback = array_shift($args);
if (is_array($callback)) {
$function = array_shift($callback);
$params = $callback;
}
else {
$function = $callback;
$params = array();
}
$params = array_merge($params, $args);
return call_user_func_array($function, $params);
}
function messaging_message_callbacks($callback_keys, $message, $info) {
while (!empty($message->process) && ($key = array_shift($callback_keys))) {
if ($callback = _messaging_callback_get($info, $key)) {
$message = messaging_message_invoke($callback, $message, $info);
}
elseif (function_exists('messaging_message_' . $key)) {
$message = call_user_func('messaging_message_' . $key, $message, $info);
}
}
return $message;
}
function messaging_token_list($type = 'all') {
$tokens = array();
if ($type == 'message' || $type == 'all') {
$tokens['message']['message-subject'] = t('The message subject.');
$tokens['message']['message-body'] = t('The message body.');
$tokens['message']['message-author-name'] = t('The message\'s author name.');
$tokens['message']['message-method'] = t('The message\'s method name.');
$tokens['message']['message-date'] = t('The message\'s sending date.');
}
return $tokens;
}
function messaging_token_values($type, $object = NULL, $options = array()) {
switch ($type) {
case 'message':
if ($message = $object) {
$values['message-subject'] = check_plain($message->subject);
$values['message-body'] = filter_xss($message->body);
$sender = messaging_load_user($message->sender);
$values['message-author-name'] = check_plain($sender->name);
$tokens['message-method'] = messaging_method_info($message->method, 'name');
$tokens['message-date'] = format_date($message->sent);
return $values;
}
break;
}
}
function _messaging_callback_add(&$callback_list, $type, $callback) {
$name = $type . ' callback';
if (!empty($callback_list[$name]) && !is_array($callback_list[$name])) {
$callback_list[$name] = array(
$callback_list[$name],
);
}
$callback_list[$name][] = $callback;
}
function _messaging_callback_get($info, $type, $default = NULL) {
if (!empty($info[$type . ' callback'])) {
return $info[$type . ' callback'];
}
else {
return $default;
}
}
function _messaging_info($info, $method = NULL, $property = NULL, $default = NULL) {
if ($method && $property) {
return isset($info[$method][$property]) ? $info[$method][$property] : $default;
}
elseif ($method) {
return isset($info[$method]) ? $info[$method] : $default;
}
elseif ($property) {
$props = array();
foreach ($info as $method => $values) {
if (isset($values[$property])) {
$props[$method] = $values[$property];
}
}
return $props;
}
else {
return $info;
}
}
function _messaging_query_conditions($table, $params) {
$schema = drupal_get_schema($table);
$conditions = $args = array();
foreach ($params as $field => $value) {
$type = $schema['fields'][$field]['type'];
if (is_array($value)) {
$conditions[] = $field . ' IN (' . db_placeholders($value, $type) . ')';
$args = array_merge($args, $value);
}
else {
$conditions[] = $field . ' = ' . db_type_placeholder($type);
$args[] = $value;
}
}
return array(
'conditions' => $conditions,
'args' => $args,
);
}
function messaging_log($txt = NULL, $variables = NULL) {
return _messaging_log('info', $txt, $variables);
}
function _messaging_log($type, $txt = NULL, $variables = NULL, $severity = WATCHDOG_NOTICE) {
static $logs;
if ($txt) {
$logs[] = array(
$type,
$txt,
$variables,
$severity,
);
}
elseif ($logs) {
$items = array_map('_messaging_log_format', $logs);
$logs = NULL;
return $items;
}
}
function _messaging_log_format($log) {
list($type, $string, $args, $severity) = $log;
if ($args) {
$append = array();
foreach ($args as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = print_r($value, TRUE);
}
switch ($key[0]) {
case '@':
$args[$key] = check_plain($value);
break;
case '%':
$args[$key] = theme('placeholder', $value);
break;
case '!':
$args[$key] = $value;
break;
default:
$append[] = ' <strong>' . $key . '</strong>= ' . check_plain($value);
$args[$key] = '';
break;
}
}
$string = strtr($string, $args);
$append ? $string .= ' ' . implode(', ', $append) : NULL;
}
return '<strong> ' . $type . '</strong>: ' . $string;
}