View source
<?php
define('MESSAGING_FORMAT_PLAIN', 0);
define('MESSAGING_FORMAT_HTML', 1);
define('MESSAGING_FORMAT', MESSAGING_FORMAT_PLAIN);
define('MESSAGING_EMPTY', '<none>');
define('MESSAGING_CACHE_LIMIT', variable_get('messaging_cache_limit', 1000));
define('MESSAGING_DEFAULT_CRON_PERCENT', variable_get('messaging_default_cron_percent', 80));
abstract class Messaging_Object {
public function __construct($template = NULL) {
if ($template) {
$properties = (array) $template;
foreach ($properties as $key => $value) {
$this->{$key} = $value;
}
}
}
public static function build_object($object) {
return new Messaging_Object($object);
}
}
class MessagingEntityController extends DrupalDefaultEntityController {
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
$class = $this->entityInfo['base class'];
foreach ($queried_entities as $id => $entity) {
$queried_entities[$id] = call_user_func(array(
$class,
'build_object',
), $entity);
}
return parent::attachLoad($queried_entities, $revision_id);
}
}
function messaging_entity_info() {
$info['messaging_message'] = array(
'label' => t('Message'),
'controller class' => 'MessagingEntityController',
'base class' => 'Messaging_Message',
'base table' => 'messaging_message',
'entity keys' => array(
'id' => 'msid',
),
);
$info['messaging_destination'] = array(
'label' => t('Destination'),
'controller class' => 'MessagingEntityController',
'base class' => 'Messaging_Destination',
'base table' => 'messaging_destination',
'entity keys' => array(
'id' => 'mdid',
),
'view modes' => array(
'full' => array(
'label' => t('Destinations page'),
'custom settings' => FALSE,
),
),
);
return $info;
}
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;
case 'admin/messaging/settings/method/filters':
$output = '<p>' . t('These are the filters for the message body. They should depend on the content and the tokens you are using for messages. This is important for getting the right formatting and also for security.') . '</p>';
$output .= t('If using raw tokens for templates, possibly you\'ll need some additional formatting here.');
$items[] = t('You can set up Input formats for specific message parts on the <a href="@message_templates">Message templates pages</a>. These will be run first on each piece of text.', array(
'@message_templates' => url('admin/messaging/template'),
));
$items[] = t('Once the message body is built you can set a <strong>Format filter</strong> on this page to format and filter it. Set up the filters you need using the <a href="@input_formats">Input formats</a> page', array(
'@input_formats' => url('admin/settings/filters'),
));
$items[] = t('Last, the <strong>Final filter</strong> will be run for adjusting the text to the format required by each Send method.');
$output .= theme('item_list', $items);
return $output;
}
}
function messaging_menu() {
$items['admin/config/messaging'] = array(
'title' => 'Messaging & Notifications',
'description' => 'Administer and configure messaging',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array(
'access administration pages',
),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/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/config/messaging/settings/overview'] = array(
'title' => 'Messaging',
'description' => 'Configuration of sending methods',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/messaging/settings/test'] = array(
'title' => 'Test',
'description' => 'Test message sending',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'messaging_admin_test_post_form',
),
'access arguments' => array(
'administer messaging',
),
'file' => 'messaging.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/messaging/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',
),
'file' => 'messaging.admin.inc',
);
$items['admin/config/messaging/method/overview'] = array(
'title' => 'Options',
'description' => 'General settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
return $items;
}
function messaging_permission() {
return array(
'administer messaging' => array(
'title' => t('Administer messaging'),
'description' => t('Administer messaging.'),
),
);
}
function messaging_user_delete($user) {
Messaging_Destination::delete_multiple(array(
'uid' => $user->uid,
));
}
function messaging_send_method($method, $method_info = array()) {
$send_methods =& drupal_static(__FUNCTION__);
if (is_object($method)) {
return $method;
}
if (!isset($send_methods[$method])) {
if ($info = messaging_method_info($method)) {
$class = !empty($info['class']) ? $info['class'] : 'Messaging_Send_Method';
$send_methods[$method] = new $class($method, $method_info + $info);
}
else {
$send_methods[$method] = new Messaging_Send_Method($method, $method_info);
$static_info =& messaging_info('send methods');
$static_info[$method] = $method_info;
}
}
return $send_methods[$method];
}
function messaging_message_build($message) {
if (is_object($message) && is_a($message, 'Messaging_Message')) {
return $message;
}
elseif (is_array($message)) {
$message = (object) $message;
}
return Messaging_Message::build_template($message);
}
function messaging_account_build_destination($account, $method = NULL, $address = NULL) {
if ($account->uid) {
if ($method && !$address) {
$address = messaging_user_destination($account, $method);
}
if ($method && $address) {
return Messaging_Destination::create_method($method, $address, $account->uid);
}
elseif (($fallback = messaging_method_default($account)) && $fallback != $method) {
return messaging_account_build_destination($account, $fallback);
}
}
elseif ($method && $address) {
return Messaging_Destination::create_method($method, $address, 0);
}
}
function messaging_user_destination($account, $method) {
return messaging_send_method($method)
->user_destination($account);
}
function messaging_message_test($message, $destinations = array()) {
$message
->prepare();
$message
->render();
messaging_log('Emulating message sending (test run)', array(
'summary' => (string) $message,
'message' => $message,
'destinations' => $destinations,
));
return $message->success = TRUE;
}
function messaging_cron() {
if (variable_get('messaging_queue_process_cron', TRUE)) {
if ($store = messaging_store('cron')) {
$store
->cron_process();
}
}
}
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]);
}
else {
$info[$method] = messaging_translate("method:{$method}:name", $info[$method]);
}
}
}
return $info;
}
function messaging_method_permission($method, $account = NULL) {
return messaging_method_info($method, 'enabled') && messaging_send_method($method)
->user_access($account);
}
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_user_setting($name, $account = NULL, $default = NULL) {
$variable = 'messaging_' . $name;
if ($account && isset($account->{$variable}) && variable_get('messaging_peruser_' . $name, 1)) {
return $account->{$variable};
}
else {
return variable_get('messaging_default_' . $name, $default);
}
}
function messaging_method_info($method = NULL, $property = NULL, $default = NULL) {
static $info;
if (!isset($info)) {
$info =& messaging_info('send methods', NULL, FALSE, FALSE);
$enabled = variable_get('messaging_method_enabled', array());
foreach (array_keys($info) as $name) {
$info[$name] = array_merge($info[$name], variable_get('messaging_method_' . $name, array()), variable_get('messaging_filters_' . $name, array()));
$info[$name]['enabled'] = isset($enabled[$name]) ? $enabled[$name] : TRUE;
}
drupal_alter('messaging_send_methods', $info);
}
return messaging_array_info($info, $method, $property, $default);
}
function messaging_address_info($type = NULL, $property = NULL, $default = NULL) {
$info =& messaging_info('address types');
return messaging_array_info($info, $type, $property, $default);
}
function messaging_messaging($op, $type = NULL) {
switch ($op) {
case 'address types':
$types['user'] = array(
'name' => t('User name'),
'class' => 'Messaging_User_Destination',
);
return $types;
}
}
function messaging_store($name = 'queue') {
$messaging_store =& drupal_static(__FUNCTION__);
if (!isset($messaging_store[$name])) {
$class = variable_get('messaging_store_' . $name);
$messaging_store[$name] = $class ? new $class($name) : FALSE;
}
return $messaging_store[$name];
}
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/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_check_object($object, $class, $create = FALSE) {
if ($object && is_object($object) && is_a($object, $class)) {
return $object;
}
elseif ($create) {
return new $class($object);
}
}
function messaging_theme() {
return array(
'messaging_text' => array(
'render element' => 'element',
'file' => 'messaging.text.inc',
),
'messaging_admin_methods_table' => array(
'render element' => 'elements',
'file' => 'messaging.admin.inc',
),
'messaging_admin_settings_table' => array(
'render element' => 'elements',
'file' => 'messaging.admin.inc',
),
);
}
function messaging_array_info($data, $type = NULL, $property = NULL, $default = NULL) {
if ($type && $property) {
return isset($data[$type][$property]) ? $data[$type][$property] : $default;
}
elseif ($type) {
return isset($data[$type]) ? $data[$type] : array();
}
elseif ($property) {
return messaging_array_list($data, $property, FALSE, $default);
}
else {
return $data;
}
}
function messaging_array_list($data, $property, $filter = TRUE, $default = NULL) {
$return = array();
foreach ($data as $type => $info) {
if (isset($info[$property])) {
$return[$type] = $info[$property];
}
elseif (!$filter) {
$return[$type] = $default;
}
}
return $return;
}
function messaging_log($txt = NULL, $variables = array()) {
$variables += array(
'type' => 'log',
);
messaging_debug($txt, $variables);
}
function messaging_debug($txt = NULL, $variables = array()) {
if (function_exists('messaging_debug_log')) {
$variables += array(
'type' => 'debug',
);
return messaging_debug_log($txt, $variables);
}
}
function messaging_translate($name, $string, $langcode = NULL, $textgroup = 'messaging') {
return function_exists('i18nstrings') ? i18nstrings($textgroup . ':' . $name, $string, $langcode) : $string;
}
function messaging_static_cache_set($cache_name, $key, $value) {
$cache =& drupal_static($cache_name);
if ($cache && MESSAGING_CACHE_LIMIT && count($cache) > MESSAGING_CACHE_LIMIT) {
$cache = array();
}
$cache[$key] = $value;
}
function messaging_static_cache_get($cache_name, $key) {
$cache =& drupal_static($cache_name);
return isset($cache[$key]) ? $cache[$key] : NULL;
}
function messaging_include($file, $module = 'messaging') {
static $included = array();
if (!isset($included[$module][$file])) {
require_once './' . drupal_get_path('module', $module) . '/includes/' . $file;
$included[$module][$file] = TRUE;
}
}
function messaging_language($language) {
if (is_object($language)) {
return $language;
}
else {
$list = language_list();
return $language && isset($list[$language]) ? $list[$language] : language_default();
}
}
function &messaging_info($name, $param = NULL, $refresh = FALSE, $alter = TRUE) {
$info =& drupal_static('messaging_info_' . $name);
if (!isset($info) || $refresh) {
$info = module_invoke_all('messaging', $name, $param);
if ($alter) {
drupal_alter('messaging_' . strtr($name, ' ', '_'), $info);
}
}
return $info;
}
function messaging_user_uid($account) {
return is_object($account) ? $account->uid : (int) $account;
}
function messaging_user_object($account) {
return is_object($account) ? $account : user_load((int) $account);
}
function messaging_user_format_name($account, $format = MESSAGING_FORMAT_PLAIN) {
$account = messaging_user_object($account);
return $format & MESSAGING_FORMAT_HTML ? theme('username', array(
'account' => $account,
)) : check_plain($account->name);
}
function messaging_element_info() {
$types['messaging_text'] = array(
'#theme' => 'messaging_text',
'#options' => array(),
'#format' => MESSAGING_FORMAT,
'#markup' => '',
'#pre_render' => array(
'drupal_pre_render_markup',
),
);
return $types;
}