View source
<?php
define('PM_BLOCK_USER_DISALLOW_BLOCKING', 0);
define('PM_BLOCK_USER_DISALLOW_SENDING', 1);
function pm_block_user_help($path, $arg) {
switch ($path) {
case 'admin/settings/messages/block':
return '<p>' . t('This area is used to define user blocking rules for the Privatemsg module. Rules allow control of who may block messages from whom. By default all users are allowed to block messages from anyone else. However, a site may have groups of users that need to contact or get information to others, for example: the site may have administrative staff or be a forum with moderators. Groups of users are defined by roles, which can be managed on the <a href="@roles">roles configuration page</a>.', array(
'@roles' => url('admin/user/roles'),
)) . '</p>';
}
}
function pm_block_user_menu() {
$url_prefix = variable_get('privatemsg_url_prefix', 'messages');
$items['messages/block/%user'] = array(
'title' => 'Block user messages',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pm_block_user_form',
2,
),
'file' => 'pm_block_user.pages.inc',
'access callback' => '_pm_block_user_access',
'access arguments' => array(
2,
),
'type' => MENU_CALLBACK,
'weight' => -10,
);
$items[$url_prefix . '/blocked'] = array(
'title' => 'Blocked users',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pm_block_user_list',
),
'file' => 'pm_block_user.pages.inc',
'access callback' => 'privatemsg_menu_access',
'access arguments' => array(
'read privatemsg',
TRUE,
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['admin/settings/messages/block'] = array(
'title' => 'User blocking rules',
'description' => 'Configure rules for which users may block each other.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pm_block_user_settings',
),
'file' => 'pm_block_user.admin.inc',
'access arguments' => array(
'administer privatemsg settings',
),
'type' => MENU_LOCAL_TASK,
);
$items['messages/block/js'] = array(
'title' => 'Javascript block actions form',
'page callback' => 'pm_block_user_js',
'file' => 'pm_block_user.admin.inc',
'access arguments' => array(
'administer privatemsg settings',
),
'type' => MENU_CALLBACK,
);
$items['messages/user/autocomplete'] = array(
'page callback' => 'privatemsg_autocomplete',
'file' => 'privatemsg.pages.inc',
'file path' => drupal_get_path('module', 'privatemsg'),
'access callback' => 'privatemsg_user_access',
'access arguments' => array(
'write privatemsg',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function pm_block_user_theme() {
return array(
'pm_block_user_list' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'pm_block_user.pages.inc',
),
'pm_block_user_actions' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'pm_block_user.admin.inc',
),
);
}
function pm_block_user_privatemsg_autocomplete_alter(&$matches, $names, $fragment) {
if (arg(1) == 'user') {
foreach ($matches as $id => $match) {
if ($match->type != 'user') {
unset($matches[$id]);
}
}
}
}
function _pm_block_user_access($account) {
global $user;
if (!privatemsg_user_access('read privatemsg', $user)) {
return FALSE;
}
if (_pm_block_user_rule_exists($account, $user, PM_BLOCK_USER_DISALLOW_BLOCKING) && !pm_block_user_has_blocked($account, $user)) {
return FALSE;
}
return TRUE;
}
function pm_block_user_has_blocked($author, $recipient) {
return db_result(db_query('SELECT 1 FROM {pm_block_user} WHERE author = %d AND recipient = %d', $author->uid, $recipient->uid));
}
function _pm_block_user_rule_exists($author, $recipient, $action = PM_BLOCK_USER_DISALLOW_BLOCKING) {
$block_actions = variable_get('pm_block_user_actions', array());
foreach ($block_actions as $delta => $details) {
if ($details['action'] != $action || !$details['enabled']) {
continue;
}
if ($author->uid == 1 && $action == PM_BLOCK_USER_DISALLOW_SENDING) {
continue;
}
if ($recipient->uid == 1 && $action == PM_BLOCK_USER_DISALLOW_BLOCKING) {
continue;
}
if (isset($author->roles[$details['author']]) && isset($recipient->roles[$details['recipient']])) {
return TRUE;
}
}
return FALSE;
}
function pm_block_user_privatemsg_block_message($author, $recipients, $context = array()) {
$blocked = array();
foreach (array_keys($recipients) as $id) {
if (isset($recipients[$id]->type) && $recipients[$id]->type != 'user') {
continue;
}
if (!isset($recipients[$id]->roles)) {
$uid = str_replace('user_', '', $id);
$recipients[$id] = privatemsg_user_load($uid);
}
if (_pm_block_user_rule_exists($author, $recipients[$id], PM_BLOCK_USER_DISALLOW_SENDING)) {
$blocked[] = array(
'recipient' => $id,
'message' => t('You are not permitted to send messages to !user.', array(
'!user' => privatemsg_recipient_format($recipients[$id]),
)),
);
}
}
$user_recipients = array();
foreach ($recipients as $key => $recipient) {
if (empty($recipient->type)) {
$recipient->type = 'user';
$recipient->recipient = $recipient->uid;
}
if ($recipient->type == 'user') {
$user_recipients[$recipient->recipient] = $recipient;
}
}
if (empty($user_recipients)) {
return $blocked;
}
$args = array_merge(array(
$author->uid,
), array_keys($user_recipients));
$result = db_query('SELECT recipient FROM {pm_block_user} WHERE author = %d AND recipient IN (' . db_placeholders(array_keys($user_recipients)) . ') GROUP BY recipient', $args);
while ($row = db_fetch_object($result)) {
$recipient = $user_recipients[$row->recipient];
if (_pm_block_user_rule_exists($author, $recipient, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
continue;
}
$blocked[] = array(
'recipient' => privatemsg_recipient_key($recipient),
'message' => t('%name has chosen to block messages from you.', array(
'%name' => privatemsg_recipient_format($recipient, array(
'plain' => TRUE,
)),
)),
);
}
return $blocked;
}
function pm_block_user_privatemsg_sql_load_alter(&$fragments, $pmid, $uid) {
$fragments['select'][] = 'pmbu.recipient AS is_blocked';
$fragments['inner_join'][] = "LEFT JOIN {pm_block_user} pmbu ON (pm.author = pmbu.author AND pmi.recipient = pmbu.recipient AND pmi.type = 'user')";
}
function pm_block_user_privatemsg_message_view_alter(&$vars) {
global $user;
$author = $vars['message']['author'];
if (_pm_block_user_rule_exists($author, $user, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
return;
}
if (!isset($vars['message']['thread_id'])) {
return;
}
$thread_id = $vars['message']['thread_id'];
if ($user->uid != $author->uid) {
if ($vars['message']['is_blocked']) {
$vars['message_actions']['unblock_author'] = array(
'title' => t('Unblock'),
'href' => 'messages/block/' . $author->uid,
'query' => array(
'destination' => privatemsg_get_dynamic_url_prefix() . '/view/' . $thread_id,
),
);
}
else {
$vars['message_actions']['block_author'] = array(
'title' => t('Block'),
'href' => 'messages/block/' . $author->uid,
'query' => array(
'destination' => privatemsg_get_dynamic_url_prefix() . '/view/' . $thread_id,
),
);
}
}
}
function pm_block_user_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'delete':
db_query("DELETE FROM {pm_block_user} WHERE author = %d OR recipient = %d", $account->uid, $account->uid);
break;
}
}
function pm_block_user_privatemsg_sql_autocomplete_alter(&$fragments) {
global $user;
$subquery = 'SELECT pmbu.recipient FROM {pm_block_user} pmbu WHERE pmbu.author = %d';
$fragments['where'][] = 'u.uid NOT IN (' . $subquery . ')';
$fragments['query_args']['where'][] = $user->uid;
if ($user->uid != 1) {
$rids = array();
$block_actions = variable_get('pm_block_user_actions', array());
foreach ($block_actions as $delta => $details) {
if ($details['action'] != PM_BLOCK_USER_DISALLOW_SENDING || !$details['enabled']) {
continue;
}
if (isset($user->roles[$details['author']])) {
if ($details['recipient'] == DRUPAL_AUTHENTICATED_RID) {
$fragments['where'][] = '1 = 0';
return;
}
$rids[] = $details['recipient'];
}
}
if (!empty($rids)) {
$fragments['inner_join'][] = 'LEFT JOIN {users_roles} ur ON (u.uid = ur.uid)';
$fragments['where'][] = 'ur.rid NOT IN (' . db_placeholders($rids) . ') OR ur.rid IS NULL';
$fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], $rids);
}
}
}