Newer
Older
Marco Molinari
committed
<?php
/**
* Implementation of hook_help().
*/
function privatemsg_help($section) {
case 'admin/modules#description':
return t('Allows private messages between users.');
/**
* Implementation of hook_link().
*/
function privatemsg_link($type, $arg) {
if (user_access('access private messages') && ($type == 'node' || $type == 'comment') && variable_get("privatemsg_link_$type", 0) && (isset($GLOBALS['user']->privatemsg_allow) ? $GLOBALS['user']->privatemsg_allow : 1)) {
if (user_access('access private messages', $arg) && $arg->privatemsg_allow) {
return array(l(t('write to author'), "privatemsg/msgto/$arg->uid"));
Marco Molinari
committed
}
}
/**
* Implementation of hook_menu().
*/
$items[] = array('path' => 'privatemsg',
'title' => t('view inbox') . ' ('. (int)_privatemsg_get_new_messages() .')',
'callback' => 'privatemsg_page',
'access' => user_access('access private messages'),
'type' => (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) ? MENU_DYNAMIC_ITEM : MENU_CALLBACK);
/**
* Implementation of hook_user().
*/
function privatemsg_user($type, $edit, &$user, $category = NULL) {
if (user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) {
return array(t('History') => form_item(t('Private messages'), l(t('send private message'), "privatemsg/msgto/$user->uid")));
}
else if ($GLOBALS['user']->uid) {
return;
}
else if (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) {
return array(t('History') => form_item(t('Private messages'), t('<a href="%login">login</a> or <a href="%register">register</a> to send private messages to this user', array('%login' => url('user/login'), '%register' => url('user/register')))));
return array(t('History') => form_item(t('Private messages'), t('<a href="%login">login</a> to send private messages to this user', array('%login' => url('user/login')))));
}
case 'form':
if (user_access('access private messages') && $category == 'account') {
$output = form_checkbox(t('Allow private messages'), 'privatemsg_allow', 1, isset($edit->privatemsg_allow) ? $edit->privatemsg_allow : (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1), t('Check this box to allow users to send you private messages.'));
$output .= form_checkbox(t('Receive daily e-mail for unread messages'), 'privatemsg_mailalert', 1, isset($edit->privatemsg_mailalert) ? $edit->privatemsg_mailalert : (isset($user->privatemsg_mailalert) ? $user->privatemsg_mailalert : 1), t('Check this box to receive e-mail notification for unread messages. Only one e-mail will be sent per day.'));
return array(array('title' => t('Private message settings'), 'data' => $output));
Marco Molinari
committed
/**
* Implementation of hook_settings().
*/
$rate = drupal_map_assoc(array(5, 10, 15, 20, 30, 60), 'format_interval');
$output .= form_select(t('Private messaging max rate'), 'privatemsg_max_rate', variable_get('privatemsg_max_rate', 15), $rate, t('Max submit rate for private messaging. To prevent abuse.'));
$output .= form_select(t('Sent message status'), 'privatemsg_sent_status', variable_get('privatemsg_sent_status', 1), array(t('Disabled'), t('Enabled')), t('If enabled users can see whether a message has been read or not.'));
$number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100));
$output .= form_select(t('Messages per page'), 'privatemsg_per_page', variable_get('privatemsg_per_page', 10), $number, t('The maximum number of messages displayed per page; links to browse messages automatically appear.'));
$group = form_checkbox(t('Display link with posts'), 'privatemsg_link_node', 1, variable_get('privatemsg_link_node', 0), t('Provide a link to send private messages to users with posts they start.'));
$group .= form_checkbox(t('Display link with comments'), 'privatemsg_link_comment', 1, variable_get('privatemsg_link_comment', 0), t('Provide a link to send private messages to users with their comments.'));
$output .= form_group(t('"Write to author" links'), $group);
Marco Molinari
committed
return $output;
}
/**
* Implementation of hook_perm().
*/
Marco Molinari
committed
/**
* Implementation of hook_cron().
*/
function privatemsg_cron() {
// perform these actions just once per day
if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) {
Marco Molinari
committed
}
}
function _privatemsg_prune() {
// move deleted message older than 1 month to archive table, and optimize table
$result = db_query("SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < '%d'", (time() - 3600*24*30));
db_query("INSERT INTO {privatemsg_archive} (id, author, recipient, subject, message, timestamp, hostname, format, folder) VALUES ('%d', '%d', '%d', '%s', '%s', '%d', '%s', '%d', '%d')", $message->id, $message->author, $message->recipient, $message->subject, $message->message, $message->timestamp, $message->hostname, $message->format, $message->folder);
db_query("DELETE FROM {privatemsg} WHERE id = '%d'", $message->id);
if ($GLOBALS['db_type'] == 'mysql') {
db_query("OPTIMIZE TABLE {privatemsg}");
}
Marco Molinari
committed
global $locale;
$initial_locale = $locale;
if (function_exists('locale')) {
$languages = locale_supported_languages();
$languages = $languages['name'];
}
$from = variable_get('site_mail', ini_get('sendmail_from'));
$result = db_query('SELECT COUNT(*) AS c, recipient FROM {privatemsg} WHERE newmsg = 1 AND recipient_del = 0 GROUP BY recipient');
if ((isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) && (isset($user->privatemsg_mailalert) ? $user->privatemsg_mailalert : 1)) {
// use each user's individual locale
if (function_exists('locale') && $languages[$user->language]) {
$locale = $user->language;
}
$subject = t('New private messages at %site.', array('%site' => variable_get('site_name', 'drupal')));
$message = t('Hi %name,
This is an automatic reminder from the site %site. You have %new unread private messages.
To read your messages, follow this link:
%link1
If you don\'t want to receive these email again, change your preferences here:
%link2', array('%name' => $user->name, '%site' => variable_get('site_name', 'drupal'), '%new' => $alert->c, '%link1' => url('privatemsg', NULL, NULL, 1), '%link2' => url('user/'. $user->uid .'/edit', NULL, NULL, 1)));
user_mail($user->mail, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
// revert to previous (site default) locale
$locale = $initial_locale;
Marco Molinari
committed
}
$op = $_POST["op"];
$edit = $_POST["edit"];
$recipient = $_POST["recipient"];
$msg = $_POST["msg"];
Marco Molinari
committed
$output = _privatemsg_list($arg);
$output = _privatemsg_view($arg);
$title = t("Read message");
$breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg'));
case t('Write a new message'):
case 'form':
case 'reply':
$output = _privatemsg_form($arg);
$title = t("Write a new message");
$breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg'));
$msg->recipient = db_result(db_query("SELECT name FROM {users} WHERE uid = '%d'", $arg));
$output = _privatemsg_form($msg);
$title = t("Write a new message");
$breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg'));
case 'send':
case t('Send private message'):
if (!$edit["recipient"]) {
$edit["recipient"] = $recipient;
}
$breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg'));
$output = _privatemsg_edit($edit);
break;
case t('Move to folder'):
if ($edit["folder"] == 0 || db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $edit["folder"], $user->uid))) {
// this folder belongs to him
foreach ($msg as $mid) {
_privatemsg_move($mid, $edit["folder"]);
$output = _privatemsg_list($edit["folder"]);
break;
Marco Molinari
committed
$output = _privatemsg_list();
case t('Delete messages'):
if ($msg) {
foreach ($msg as $id) {
_privatemsg_delete($id);
$output = _privatemsg_list();
Marco Molinari
committed
_privatemsg_delete($arg);
$output = _privatemsg_list();
case t('New folder'):
case t('Add folder'):
if ($edit["name"]) {
// check for uniqueness
if (!db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE name = '%s' AND uid = '%d'", $edit["name"], $user->uid))) {
db_query("INSERT INTO {privatemsg_folder} (uid, name) VALUES ('%d', '%s')", $user->uid, $edit["name"]);
}
$output = _privatemsg_list();
}
else {
$title = t('Create new folder');
$breadcrumb = array(l(t('Home'), ''), l('Private messages', 'privatemsg'));
$output = _privatemsg_new_folder($edit);
}
break;
case t('Delete folder'):
// check ownership
if (db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $edit["current_folder"], $user->uid))) {
db_query("DELETE FROM {privatemsg_folder} WHERE fid = '%d'", $edit["current_folder"]);
db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = '%d'", $edit["current_folder"]);
}
$output = _privatemsg_list();
case t('Empty folder'):
$fid = $edit["current_folder"];
if ($fid == 1) {
db_query("UPDATE {privatemsg} SET author_del = 1 WHERE author = '%d'", $user->uid);
}
else if ($fid == 0 || db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $fid, $user->uid))) {
// check ownership
db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = '%d' AND recipient = '%d'", $edit["current_folder"], $user->uid);
}
$output = _privatemsg_list();
$output = _privatemsg_list();
drupal_set_title($title);
drupal_set_breadcrumb($breadcrumb);
function _privatemsg_list($current_folder = 0) {
global $user;
$result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND p.recipient = %d AND folder = '%s' AND p.recipient_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10), 0, NULL, $user->uid, $current_folder);
$folder_name = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = %d AND uid = %d", $current_folder, $user->uid));
Marco Molinari
committed
}
$result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.recipient = u.uid AND p.author = %d AND p.author_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10), 0, NULL, $user->uid);
Marco Molinari
committed
}
$messages = array();
while ($message = db_fetch_object($result)) {
$messages[] = $message;
}
$result = db_query("SELECT fid, name FROM {privatemsg_folder} WHERE uid = %d", $user->uid);
while ($folder = db_fetch_object($result)) {
$folders[] = array($folder->fid, $folder->name);
Marco Molinari
committed
}
return theme("privatemsg_list", $current_folder, $messages, $folders);
Marco Molinari
committed
function _privatemsg_format_folder($current, $fid, $name) {
if ($current == $fid) {
return "<strong>$name</strong>";
}
else {
return l($name, "privatemsg/list/$fid");
}
Marco Molinari
committed
}
function _privatemsg_form($message = 0) {
$message = db_fetch_object(db_query("SELECT subject, message, u.name FROM {privatemsg} p, {users} u WHERE u.uid = p.author AND id = '%d' AND recipient = '%d'", $message, $user->uid));
Marco Molinari
committed
if (!stristr($message->subject, t("Re:"))) {
$message->subject = t("Re:").' '.$message->subject;
$message->message = "\n".str_replace("\n", "\n> ", "\n".$message->message);
drupal_set_html_head('<style type="text/css" media="all">
#edit-recipient {
float: left;
}
#edit-quick {
position: relative;
top: -1em;
left: 1em;
}
</style>');
$form = form_textfield(t("To"), "recipient", $message->recipient, 50, 64);
$form .= '<select id="edit-quick" name="quick" onChange="document.getElementById(\'edit-recipient\').value=quick.value"><option value="" selected="selected">--'.t("contacts").'--</option>';
$result = db_query("SELECT DISTINCT(name) AS name FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND recipient = '%d' AND p.timestamp > (UNIX_TIMESTAMP(NOW()) - (3600 * 24 * 30)) ORDER BY name", $user->uid);
while ($name = db_fetch_object($result)) {
$name = check_plain($name->name);
$form .= "<option value='$name'>$name</option>";
$form .= '</select>';
Marco Molinari
committed
$form .= form_textfield(t("Subject"), "subject", $message->subject, 50, 64);
$form .= form_textarea(t("Message"), "message", $message->message, 80, 5);
$form .= filter_form('format', $message->format);
Moshe Weitzman
committed
$form .= form_submit(t("Send private message"));
Marco Molinari
committed
}
if ($edit['recipient'] == '') {
form_set_error('recipient', t('The <em>Recipient</em> field is required.'));
return _privatemsg_form(array2object($edit));
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
$recipient = user_load(array('name' => $edit['recipient']));
}
if (!$recipient->uid) {
form_set_error('recipient', t('The <em>Recipient</em> does not exist.'));
return _privatemsg_form(array2object($edit));
}
else if (!(isset($account->privatemsg_allow) ? $account->privatemsg_allow : 1)) {
form_set_error('recipient', t('%name does not accept private messages.', array('%name' => $account->name)));
return _privatemsg_form(array2object($edit));
}
else if ($edit['subject'] == '') {
form_set_error('subject', t('The <em>Subject</em> field is required.'));
return _privatemsg_form(array2object($edit));
}
if ($edit['message'] == '') {
form_set_error('message', t('The <em>Message</em> field is required.'));
return _privatemsg_form(array2object($edit));
}
else if (array_key_exists('format', $edit) && !filter_access($edit['format'])) {
form_set_error('format', t('The supplied input format is invalid.'));
return _privatemsg_form(array2object($edit));
}
else {
$result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['message'], time(), 1, getenv("REMOTE_ADDR"), $edit['format']);
drupal_set_message(t('Message sent.'));
return _privatemsg_list();
Marco Molinari
committed
}
}
$result = db_query("SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.format FROM {privatemsg} p, {users} u WHERE (recipient = '%d' OR author = '%d') AND author = u.uid AND id = '%d'", $user->uid, $user->uid, $message_id);
Marco Molinari
committed
$message = db_fetch_object($result);
if (($message->newmsg) && ($user->uid == $message->recipient)) {
$result = db_query("UPDATE {privatemsg} SET newmsg = 0 WHERE id = %d", $message_id);
return theme("privatemsg_view", $message);
Marco Molinari
committed
}
Marco Molinari
committed
global $user;
$result = db_query("SELECT author, recipient FROM {privatemsg} WHERE (recipient = %d OR author = %d) AND id = %d", $user->uid, $user->uid, $id);
Moshe Weitzman
committed
if ($message = db_fetch_object($result)) {
if ($message->author == $user->uid) {
db_query("UPDATE {privatemsg} SET author_del = 1 WHERE id = '%d'", $id);
if ($message->recipient == $user->uid) {
db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE id = '%d'", $id);
Marco Molinari
committed
}
Marco Molinari
committed
}
}
function _privatemsg_get_new_messages($uid = 0) {
global $user;
if ($uid == 0) {
$uid = $user->uid;
}
return db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE recipient = '%d' AND newmsg = 1 AND recipient_del = 0", $uid));
Marco Molinari
committed
}
$form = form_textfield(t("Name"), "name", "", 50, 64);
Marco Molinari
committed
}
Marco Molinari
committed
global $user;
db_query("UPDATE {privatemsg} SET folder = '%d' WHERE id = '%d' AND recipient = '%d'", $fid, $mid, $user->uid);
Marco Molinari
committed
}
/**
@addtogroup theme_system
Privatemsg module specific theme functions
@{
**/
/**
Returns content to view a private message
@param message
**/
function theme_privatemsg_view($message) {
global $user;
if ($message) {
$body = "
<p><strong>".t("From").":</strong> ".format_name($message)."<br />
<strong>".t("To").":</strong> ".format_name(user_load(array('uid' => $message->recipient)))."<br />
<strong>".t("Subject").":</strong> ".check_plain($message->subject)."<br />
<strong>".t("Date").":</strong> ".format_date($message->timestamp)."</p>
".check_output($message->message, $message->format);
if ($message->recipient == $user->uid) {
$links[] = l(t('Reply to this message'), "privatemsg/reply/$message->id");
}
if (($message->recipient == $user->uid) || (variable_get("privatemsg_sent_status", 1))) {
$links[] = l(t('Delete this message'), "privatemsg/delete/$message->id", array('onClick' => "return confirm('".t('Are you sure to delete this message?')."')"));
Moshe Weitzman
committed
$links[] = l(t('List messages'), 'privatemsg');
$body .= '<div class="links">'. theme('links', $links) .'</div>';
drupal_set_message(t('Error: you can\'t read this message'), 'error');
$body = '';
}
return $body;
}
/**
Returns content to view a list of private messages
@param current_folder
@param messages
@param folders
**/
function theme_privatemsg_list($current_folder, $messages, $folders) {
$extra_folders = array();
foreach ($folders as $folder) {
$folder_list[] = _privatemsg_format_folder($current_folder, $folder[0], $folder[1]);
if ($folder[0] != 1 && $folder[0] != $current_folder) {
$extra_folders[$folder[0]] = $folder[1];
}
}
Moshe Weitzman
committed
$out = theme('links', $folder_list);
foreach ($messages as $message) {
$row = array();
$row[] = array('data' => '<input type="checkbox" name="msg[]" value="'. $message->id .'">');
if ($current_folder != 1) {
$new = $message->newmsg;
}
else {
if (variable_get("privatemsg_sent_status", 1)) {
$new = $message->newmsg;
}
else {
$new = 0;
}
}
$row[] = array('data' => format_date($message->timestamp, 'small'));
$row[] = array('data' => format_name($message));
$row[] = array('data' => l($message->subject, 'privatemsg/view/'. $message->id) . ($new ? (' '. theme('mark')) : ''));
Moshe Weitzman
committed
Moshe Weitzman
committed
if (count($messages) < 1) {
$out .= t('<p>No messages</p>');
else {
$header = array(
array('data' => ''),
array('data' => t('Date')),
array('data' => ($current_folder == 1 ? t('To') : t('From'))),
array('data' => t('Subject')),
);
if ($pager = theme("pager", array(), variable_get("privatemsg_per_page", 10))) {
$rows[] = array(array('data' => $pager, 'colspan' => 5));
$out .= theme('table', $header, $rows);
}
$out .= '<hr />'. form_submit(t("Write a new message"));
if (count($messages) > 0) {
$out .= form_submit(t("Delete messages"), "op", array("onClick" => "return confirm('".t("Are you sure you want to delete these messages?")."')"));
}
// folder management
if ((count($extra_folders) > 0) && ($current_folder != 1) && (count($messages) > 0)) {
$out .= '<br /><select name="edit[folder]">';
foreach ($extra_folders as $fid => $folder_name) {
$out .= "<option value='$fid'>$folder_name</option>";
}
$out .= '</select>';
$out .= form_submit(t("Move to folder"));
}
if ($current_folder > 1) {
// you can't delete Inbox
$out .= form_submit(t("Delete folder"), "op", array("onClick" => "return confirm('".t("Are you sure you want to delete this folder and all its messages?")."')"));
if (count($messages) > 0) {
$out .= form_submit(t("Empty folder"), "op", array("onClick" => "return confirm('".t("Are you sure you want to delete every message in this folder?")."')"));
}
$out .= form_hidden("current_folder", $current_folder);
$out .= form_submit(t("New folder"));
return form($out);
}
/** @} End of addtogroup theme_system **/