View source
<?php
define('MESSAGING_TYPE_OUTGOING', 4);
define('MESSAGING_TYPE_INCOMING', 8);
abstract class Messaging_Method {
public $method;
public $type;
public $enabled = TRUE;
public $anonymous = FALSE;
public $info = array();
function __construct($method, $info = array()) {
$this->method = $method;
foreach ($info as $key => $value) {
$this->{$key} = $value;
}
$this->info = $info;
}
function get_title() {
return $this
->method_info('title', t('Method'));
}
function get_name() {
return $this
->method_info('name', $this
->get_title());
}
function get_description() {
return $this
->method_info('description', t('Send messages.'));
}
function method_info($property = NULL, $default = NULL) {
if ($property) {
return isset($this->info[$property]) ? $this->info[$property] : $default;
}
else {
return $this->info;
}
}
static function default_method($account = NULL) {
if ($account && !empty($account->messaging_default) && messaging_method($account->messaging_default)
->user_access($account)) {
return $account->messaging_default;
}
elseif ($method = variable_get('messaging_default_method', '')) {
return $method;
}
else {
return key(messaging_method_info());
}
}
static function method_disable($method, $replace = NULL) {
module_load_include('install', 'messaging');
$replace = isset($replace) ? $replace : messaging_update_method_replace($method, TRUE);
messaging_update_method_disable($method, $replace);
if ($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');
}
}
protected function message_log($text, $message) {
$text = $this
->get_name() . ': ' . $text;
messaging_log($text, array(
'message' => $message,
));
}
}
class Messaging_Send_Method extends Messaging_Method {
public $method = 'send';
public $type = 'web';
public $enabled = TRUE;
public $anonymous = FALSE;
public $queue = FALSE;
public $log = FALSE;
public $format = MESSAGING_FORMAT_HTML;
public static function address_type() {
return 'none';
}
public static function address_info($property = NULL, $default = NULL) {
return messaging_address_info(self::address_type(), $property, $default);
}
public static function address_name() {
return self::address_info('name', t('Address'));
}
function message_prepare($message) {
if ($this->queue && $message->priority <= 0) {
$message->queue = 1;
}
$message->log = $message->log || $this->log;
$this
->message_log('Message prepared', $message);
}
function message_user($message) {
}
function message_render($message) {
$message
->get_template()
->set_method($this->method)
->set_format($this->format);
}
function message_send($message) {
$this
->message_log('Message send', $message);
$destinations = $message
->get_destinations();
$results = $this
->send_multiple($destinations, $message);
$message
->set_results($results);
return (bool) array_filter($results);
}
function send_destination($destination, $message) {
$message
->get_template()
->set_destination($destination);
return $this
->send_address($destination
->get_address(), $message);
}
function send_address($address, $message) {
return FALSE;
}
function send_multiple($destinations, $message) {
$results = array();
foreach ($destinations as $key => $destination) {
$results[$key] = $this
->send_destination($destination, $message);
}
return $results;
}
function message_params($message) {
return $message
->get_params($this->method) + $message
->get_params($this->type) + $this
->default_params();
}
function message_queue($message) {
if ($queue = messaging_store('queue')) {
$message->queued = time();
$queue
->message_queue($message);
return TRUE;
}
else {
$message->queued = 0;
return $this
->message_send($message);
}
}
function user_destination($account) {
return Messaging_Destination::build_user($this
->address_type(), $account);
}
function user_access($account) {
if (!$account->uid && !$this->anonymous) {
return FALSE;
}
if ($permission = $this
->method_info('access')) {
return user_access($permission, $account);
}
else {
return TRUE;
}
}
public function address_destination($address, $validate = TRUE) {
return Messaging_Destination::build_address($this
->address_type(), $address, $validate);
}
public function address_validate($address) {
return Messaging_Destination::validate_address($address, $this
->address_type());
}
public function format_address($address, $format = MESSAGING_FORMAT_PLAIN) {
return Messaging_Destination::format_address($address, $format, $this
->address_type());
}
function supports_anonymous() {
return TRUE;
}
static function default_params() {
return array();
}
}