summaryrefslogtreecommitdiffstats
path: root/privatemsg.module
blob: 084fd811d4b6f93f17169f4049c38c113c223ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
// $Id$

/**
 * Implementation of hook_help().
 */
function privatemsg_help($section) {
  switch ($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')) {
    switch ($type) {
      case 'node':
        if (variable_get('privatemsg_link_node', 0)) return array(l(t('write to author'), "privatemsg/msgto/$arg->uid"));
        break;
      case 'comment':
        if (variable_get('privatemsg_link_comment', 0)) return array(l(t('write to author'), "privatemsg/msgto/$arg->uid"));
        break;
    }
  }
}

/**
 * Implementation of hook_menu().
 */
function privatemsg_menu($may_cache) {
  $items = array();

  if (!$may_cache) {
    $items[] = array('path' => 'privatemsg',
                     'title' => t('view inbox') . ' ('. (int)_privatemsg_get_new_messages() .')',
                     'callback' => 'privatemsg_page',
                     'access' => user_access('access private messages'),
                     'type' => MENU_DYNAMIC_ITEM);
  }
  return $items;
}

/**
 * Implementation of hook_user().
 */
function privatemsg_user($type, $edit, &$user, $category) {
  if (user_access('access private messages')) {
    switch ($type) {
      case 'view':
        return array(t('Private messages') => form_item('', l(t('send private message'), "privatemsg/msgto/$user->uid")));
      case 'validate':
        if (!$edit['privatemsg_mailalert']) {
          $edit['privatemsg_mailalert'] = 0;
        }
        return $edit;
      case 'form':
        if ($category == 'account') {
          return array(array('title' => t('Private message settings'), 'data' => form_checkbox(t('Receive daily e-mail for unread messages'), 'privatemsg_mailalert', 1, $user->privatemsg_mailalert, t('Check this box to receive e-mail notification for unread messages. Only one e-mail will be sent per day.'))));
        }
    }
  }
}

/**
 * Implementation of hook_settings().
 */
function privatemsg_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);

  return $output;
}

/**
 * Implementation of hook_perm().
 */
function privatemsg_perm() {
  return array('access private messages');
}

/**
 * Implementation of hook_cron().
 */
function privatemsg_cron() {
  // perform these actions just once per day
  if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) {
    _privatemsg_prune();
    _privatemsg_mailalert();
    variable_set('privatemsg_last_cron', time());
  }
}

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));
  while ($message = db_fetch_object($result)) {
    db_query("INSERT INTO {privatemsg_archive} (id, author, recipient, subject, message, timestamp, hostname, folder)  VALUES ('%d', '%d', '%d', '%s', '%s', '%d', '%s', '%d')", $message->id, $message->author, $message->recipient, $message->subject, $message->message, $message->timestamp, $message->hostname, $message->folder);
    db_query("DELETE FROM {privatemsg} WHERE id = '%d'", $message->id);
  }

  // this is MySQL-specific
  if ($GLOBALS['db_type'] == 'mysql') {
    db_query("OPTIMIZE TABLE {privatemsg}");
  }
}

function _privatemsg_mailalert() {
  $result = db_query("SELECT COUNT(*) AS c, recipient FROM {privatemsg} WHERE new = 1 AND recipient_del = 0 GROUP BY recipient");

  while ($alert = db_fetch_object($result)) {
    $user = user_load(array("uid" => $alert->recipient));
    if ($user->privatemsg_mailalert) {
      user_mail($user->mail,
                t('You have unread messages'),
                t("Hi %name,\nthis is an automatic reminder from the site %site. You have %new unread private messages.\n\nTo read your messages, follow this link:\n%link1\n\nIf you don't want to receive these email again, change your preferences here:\n%link2\n\n",
                  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'))),
                t('New private messages at %site.', array('%site' => variable_get('site_name', 'drupal'))));
    }
  }
}

function privatemsg_page() {
  global $user;

  $breadcrumb = NULL;
  $op = $_POST["op"];
  $edit = $_POST["edit"];
  $recipient = $_POST["recipient"];
  $msg = $_POST["msg"];

  if (empty($op)) {
    $op = arg(1);
  }
  $arg = arg(2);

  switch($op) {
    case "list";
      $output = _privatemsg_list($arg);
      $title = t('Private messages');
      break;
    case "view";
      $output = _privatemsg_view($arg);
      $title = t("Read message");
      $breadcrumb = array(l(t('Home'), ''), l('Private messages', 'privatemsg'));
      break;
    case t("Write a new message"):
      $arg = "";
    case "form";
    case "reply";
      $output = _privatemsg_form($arg);
      $title = t("Write a new message");
      $breadcrumb = array(l(t('Home'), ''), l('Private messages', 'privatemsg'));
      break;
    case "msgto";
      $msg->name = 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('Private messages', 'privatemsg'));
      break;
    case "send";
    case t("Send private message"):
      if (!$edit["recipient"]) {
        $edit["recipient"] = $recipient;
      }
      $breadcrumb = array(l(t('Home'), ''), l('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
        if ($msg) {
          foreach ($msg as $mid) {
            _privatemsg_move($mid, $edit["folder"]);
          }
          $output = _privatemsg_list($edit["folder"]);
          break;
        }
      }

      $output = _privatemsg_list();
      break;
    case t("Delete messages"):
      if ($msg) {
        foreach ($msg as $id) {
          _privatemsg_delete($id);
        }
      }
      $output = _privatemsg_list();
      break;

    case "delete";
      _privatemsg_delete($arg);
      $output = _privatemsg_list();
      break;

    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();
      break;
    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();
      break;
    default;
      $output = _privatemsg_list();
      $title = t('Private messages');
      break;
  }

  print theme('page', $output, $title, $breadcrumb);

}

function _privatemsg_list($current_folder = 0) {
  global $user;

  if ($current_folder != 1) {
    $result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, new FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND p.recipient = $user->uid AND folder = '".addslashes($current_folder)."' AND p.recipient_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10));

    if ($current_folder > 0) {
      $folder_name = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '$user->uid'", $current_folder));
    }
    else {
      $folder_name = t("Inbox");
    }
  }
  else {
    // sent messages
    $result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, new FROM {privatemsg} p, {users} u WHERE p.recipient = u.uid AND p.author = $user->uid AND p.author_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10));

    $folder_name = t("Sent messages");
  }

  $messages = array();
  while ($message = db_fetch_object($result)) {
    $messages[] = $message;
  }

  $folders[] = array(0, t("Inbox"));
  $result = db_query("SELECT fid, name FROM {privatemsg_folder} WHERE uid = '$user->uid'");
  while ($folder = db_fetch_object($result)) {
    $folders[] = array($folder->fid, $folder->name);
  }
  $folders[] = array(1, t("Sent messages"));

  return theme("privatemsg_list", $current_folder, $messages, $folders);
}

function _privatemsg_format_folder($current, $fid, $name) {
  if ($current == $fid) {
    return "<b>$name</b>";
  }
  else {
    return l($name, "privatemsg/list/$fid");
  }
}

function _privatemsg_form($message = 0) {
  global $user;

  if ($message) {
    if (!is_object($message)) {
      $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));

      if (!stristr($message->subject, t("Re:"))) {
        $message->subject = t("Re:").' '.$message->subject;
      }

      // quoting
      $message->message = "\n".str_replace ("\n", "\n> ", "\n".$message->message);
    }
  }

  $to .= "<input type='text' name='recipient' value='".htmlentities($message->name, ENT_QUOTES)."' maxlength='64' size='50'>";

  if (!$message->name) {
    $to .= ' <select name="quick" onChange="recipient.value=quick.value"><option value="--'.t("contacts").'--" 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 = htmlentities($name->name);
      $to .= "<option value='$name'>$name</option>";
    }
    $to .= '</select>';
  }

  $form .= form_item(t("To"), $to);

  $form .= form_textfield(t("Subject"), "subject", $message->subject, 50, 64);
  $form .= form_textarea(t("Message"), "message", $message->message, 80, 5);
  $form .= form_submit(t("Send private message"));

  return form($form);
}

function _privatemsg_edit($edit) {
  global $user;
  $recipient = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $edit["recipient"]));

  if (!$edit["recipient"] || !$edit["subject"] || !$edit["message"]) {
    drupal_set_message(t('Warning: every field required'), 'error');
    return _privatemsg_form(array2object($edit));
  }
  elseif ($recipient == $user->uid) {
    drupal_set_message(t('A message to yourself?'), 'error');
    return _privatemsg_form(array2object($edit));
  }
  else {
    if ($recipient) {
      $result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, new, hostname) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s')", $user->uid, $recipient, strip_tags($edit['subject']), strip_tags($edit['message']), time(), 1, getenv("REMOTE_ADDR"));
      drupal_set_message(t('Message sent.'));
      return _privatemsg_list();
    }
    else {
      drupal_set_message(t('Warning: user does not exist'), 'error');
      return _privatemsg_form(array2object($edit));
    }
  }
}

function _privatemsg_view($message_id) {
  global $user;

  $result = db_query("SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.new, p.recipient FROM {privatemsg} p, {users} u WHERE (recipient = '%d' OR author = '%d') AND author = u.uid AND id = '%d'", $user->uid, $user->uid, $message_id);

  $message = db_fetch_object($result);
  if (($message->new) && ($user->uid != $message->author)) {
    $result = db_query("UPDATE {privatemsg} SET new = 0 WHERE recipient = '$user->uid' AND id = '%d'", $message_id);
  }

  return theme("privatemsg_view", $message);
}

function _privatemsg_delete($id) {
  global $user;

  $result = db_query("SELECT author, recipient FROM {privatemsg} WHERE (recipient = '$user->uid' OR author = '$user->uid') AND id = '%d'", $id);

  if ($message = db_fetch_object($result)) {
    if ($message->author == $user->uid) {
      db_query("UPDATE {privatemsg} SET author_del = 1 WHERE id = '%d'", $id);
    }
    else if ($message->recipient == $user->uid) {
      db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE id = '%d'", $id);
    }

    return true;
  }
  else {
    return false;
  }
}

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 new = 1 AND recipient_del = 0", $uid));
}

function _privatemsg_new_folder($edit) {
  $form = form_textfield(t("Name"), "name", "", 50, 64);
  $form .= form_submit(t("Add folder"));

  return form($form);
}

function _privatemsg_move($mid, $fid) {
  global $user;
  db_query("UPDATE {privatemsg} SET folder = '%d' WHERE id = '%d' AND recipient = '%d'", $fid, $mid, $user->uid);
}

/**
  @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><b>".t("From").":</b> ".format_name($message)."<br />
      <b>".t("To").":</b> ".format_name(user_load(array('uid' => $message->recipient)))."<br />
      <b>".t("Subject").":</b> ".strip_tags($message->subject)."<br />
      <b>".t("Date").":</b> ".format_date($message->timestamp)."</p>
      ".check_output($message->message, 1);
    $links = array();
    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?')."')"));
    }

    $links[] = l(t('List messages'), 'privatemsg');
    $body .= theme('links', $links);
  } else {
    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];
    }
  }

  $out = theme('links', $folder_list);

  $rows = array();
  foreach ($messages as $message) {
    $row = array();
    $row[] = array('data' => '<input type="checkbox" name="msg[]" value="'. $message->id .'">');
    if ($current_folder != 1) {
      $new = $message->new;
    }
    else {
      if (variable_get("privatemsg_sent_status", 1)) {
        $new = $message->new;
      }
      else {
        $new = 0;
      }
    }
    $row[] = array('data' => format_date($message->timestamp, 'small'));
    $row[] = array('data' => format_name($message));
    $row[] = array('data' => l(strip_tags($message->subject), 'privatemsg/view/'. $message->id) . ($new ? (' '. theme('mark')) : ''));

    $rows[] = $row;
  }

  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_display", NULL, 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"));
  }

  $out .= '<br />';
  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 **/

?>