Stripping CVS keywords
[project/privatemsg.git] / privatemsg.module
1 <?php
2
3 function privatemsg_system($field){
4 $system["description"] = t("Allows private messages between users.");
5 return $system[$field];
6 }
7
8 function privatemsg_link($type) {
9 global $user;
10
11 if ($type == "menu.view" && user_access("access private messages")) {
12 $links[] = l(t("view inbox"), "privatemsg") . " (". (int)_privatemsg_get_new_messages($user->uid) .")";
13 }
14
15 return $links ? $links : array();
16 }
17
18 function privatemsg_user($type, $edit, &$user) {
19 if (user_access("access private messages")) {
20 switch ($type) {
21 case "view_public":
22 return form_item(t("Private message"), l(t("send private message"), "privatemsg/msgto/$user->uid"));
23 break;
24 case "edit_form":
25 return form_item(t("Private message"), form_checkbox(t("Get daily mail if you have new messages."), "privatemsg_mailalert", 1, $user->privatemsg_mailalert, t("If you check this box you'll get a mail (no more than once per day) if you any unread messages.")));
26 break;
27 case "edit_validate":
28 if (!$edit["privatemsg_mailalert"]) {
29 $edit["privatemsg_mailalert"] = 0;
30 }
31 return $edit;
32 break;
33 }
34 }
35 }
36
37 function privatemsg_conf_options() {
38 $rate = array(5 => format_interval(5), 10 => format_interval(10), 15 => format_interval(15), 20 => format_interval(20), 30 => format_interval(30), 60 => format_interval(60));
39 $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."));
40 $output .= form_select(t("Sent message status"), "privatemsg_sent_status", variable_get("privatemsg_sent_status", 1), array("Disabled", "Enabled"), t("If enabled users can see whether a message has been read or not."));
41 $number = array(5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 50 => 50, 60 => 60, 80 => 80, 100 => 100);
42 $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."));
43
44 return $output;
45 }
46
47 function privatemsg_perm() {
48 return array("access private messages");
49 }
50
51 function privatemsg_cron() {
52 // perform these actions just once per day
53 if (variable_get("privatemsg_last_cron", 0) < (time() - 3600*24)) {
54 _privatemsg_prune();
55 _privatemsg_mailalert();
56 variable_set("privatemsg_last_cron", time());
57 }
58 }
59
60 function _privatemsg_prune() {
61 // move deleted message older than 1 month to archive table, and optimize table
62 $result = db_query("SELECT * FROM privatemsg WHERE author_del = 1 AND recipient_del = 1 AND timestamp < '%d'", (time() - 3600*24*30));
63 while ($message = db_fetch_object($result)) {
64 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);
65 db_query("DELETE FROM privatemsg WHERE id = '%d'", $message->id);
66 }
67
68 // this is MySQL-specific
69 db_query("OPTIMIZE TABLE privatemsg");
70 }
71
72 function _privatemsg_mailalert() {
73 global $base_url;
74
75 $result = db_query("SELECT COUNT(*) AS c, recipient FROM privatemsg WHERE new = 1 AND recipient_del = 0 GROUP BY recipient");
76
77 while ($alert = db_fetch_object($result)) {
78 $user = user_load(array("uid" => $alert->recipient));
79 if ($user->privatemsg_mailalert) {
80 user_mail($user->mail,
81 t("You have unread messages"),
82 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",
83 array("%name" => $user->name, "%site" => variable_get("site_name", $base_url), "%link1" => "$base_url/privatemsg", "%link2" => "$base_url/user/edit")),
84 t("New private messages at %site.", array("%site" => variable_get("site_name", $base_url))));
85 }
86 }
87 }
88
89 function privatemsg_page() {
90 global $user;
91
92 $op = $_POST["op"];
93 $edit = $_POST["edit"];
94 $recipient = $_POST["recipient"];
95 $msg = $_POST["msg"];
96
97 if (empty($op)) {
98 $op = arg(1);
99 }
100 $arg = arg(2);
101
102 if (user_access("access private messages")) {
103 switch($op) {
104 case "list";
105 _privatemsg_list($arg);
106 break;
107 case "view";
108 _privatemsg_view($arg);
109 break;
110 case t("Write a new message"):
111 $arg = "";
112 case "form";
113 case "reply";
114 _privatemsg_form($arg);
115 break;
116 case "msgto";
117 $msg->name = db_result(db_query("SELECT name FROM users WHERE uid = '%d'", $arg));
118 _privatemsg_form($msg);
119 break;
120 case "send";
121 case t("Send private message"):
122 if (!$edit["recipient"]) {
123 $edit["recipient"] = $recipient;
124 }
125 _privatemsg_edit($edit);
126 break;
127 case t("Move to folder"):
128 if ($edit["folder"] == 0 || db_result(db_query("SELECT fid FROM privatemsg_folder WHERE fid = '%d' AND uid = '%d'", $edit["folder"], $user->uid))) {
129 // this folder belongs to him
130 echo "entro";
131 if ($msg) {
132 foreach ($msg as $mid) {
133 _privatemsg_move($mid, $edit["folder"]);
134 }
135 _privatemsg_list($edit["folder"]);
136 break;
137 }
138 }
139
140 _privatemsg_list(0);
141 break;
142 case t("Delete messages"):
143 if ($msg) {
144 foreach ($msg as $id) {
145 _privatemsg_delete($id);
146 }
147 }
148 _privatemsg_list(0);
149 break;
150 case "delete";
151 _privatemsg_delete($arg);
152 _privatemsg_list(0);
153 break;
154 case t("New folder"):
155 case t("Add folder"):
156 if (_privatemsg_new_folder($edit)) {
157 _privatemsg_list(0);
158 }
159 break;
160 case t("Delete folder");
161 // check ownership
162 if (db_result(db_query("SELECT fid FROM privatemsg_folder WHERE fid = '%d' AND uid = '%d'", $edit["current_folder"], $user->uid))) {
163 db_query("DELETE FROM privatemsg_folder WHERE fid = '%d'", $edit["current_folder"]);
164 db_query("UPDATE privatemsg SET recipient_del = 1 WHERE folder = '%d'", $edit["current_folder"]);
165 }
166
167 _privatemsg_list(0);
168 break;
169 case t("Empty folder");
170 $fid = $edit["current_folder"];
171
172 if ($fid == 1) {
173 db_query("UPDATE privatemsg SET author_del = 1 WHERE author = '%d'", $user->uid);
174 }
175 else if ($fid == 0 || db_result(db_query("SELECT fid FROM privatemsg_folder WHERE fid = '%d' AND uid = '%d'", $fid, $user->uid))) {
176 // check ownership
177 db_query("UPDATE privatemsg SET recipient_del = 1 WHERE folder = '%d'", $edit["current_folder"]);
178 }
179
180 _privatemsg_list(0);
181 break;
182 default;
183 _privatemsg_list(0);
184 break;
185 }
186 }
187 else {
188 theme("header");
189
190 if ($user->uid) {
191 $theme->box(t("Private messages"), t("You don't have access to private messages."));
192 }
193 else {
194 $theme->box(t("Private messages"), t("You must login below or <a href=\"%url\">create a new account</a> before accessing private messages.", array ("%url" => drupal_url(array ("mod" => "user", "op" => "register"), "module"))));
195 }
196
197 theme("footer");
198 }
199
200 }
201
202 function _privatemsg_list($current_folder = 0, $status_message = "") {
203 global $user, $theme;
204
205 theme("header");
206
207 if ($current_folder != 1) {
208 $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));
209
210 if ($current_folder > 0) {
211 $folder_name = db_result(db_query("SELECT name FROM privatemsg_folder WHERE fid = '%d' AND uid = '$user->uid'", $current_folder));
212 }
213 else {
214 $folder_name = t("Inbox");
215 }
216 }
217 else {
218 // sent messages
219 $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));
220
221 $folder_name = t("Sent messages");
222 }
223
224 $messages = array();
225 while ($message = db_fetch_object($result)) {
226 $messages[] = $message;
227 }
228
229 $folders[] = array(0, t("Inbox"));
230 $result = db_query("SELECT fid, name FROM privatemsg_folder WHERE uid = '$user->uid'");
231 while ($folder = db_fetch_object($result)) {
232 $folders[] = array($folder->fid, $folder->name);
233 }
234 $folders[] = array(1, t("Sent messages"));
235
236 theme("privatemsg_list", $current_folder, $status_message, $messages, $folders);
237 theme("footer");
238 }
239
240 function privatemsg_list($current_folder, $status_message, $messages, $folders) {
241 foreach ($messages as $message) {
242 $msg .= "<tr>";
243 $msg .= "<td width=\"2%\"><input type=\"checkbox\" name=\"msg[]\" value=\"$message->id\"></td>";
244 if ($current_folder != 1) {
245 $new = $message->new;
246 }
247 else {
248 if (variable_get("privatemsg_sent_status", 1)) {
249 $new = $message->new;
250 }
251 else {
252 $new = 0;
253 }
254 }
255 $msg .= "<td width=\"3%\">".($new ? "<b>".t("new")."</b>" : "")."</td>";
256 $msg .= "<td width=\"20%\">".format_date($message->timestamp, "small")."</td>";
257 #$msg .= "<td width=\"30%\">".format_name($message)."</a></td>";
258 $msg .= "<td width=\"30%\">".l(check_output($message->name), "privatemsg/view/$message->id")."</a></td>";
259 $msg .= "<td width=\"55%\">".l(check_output($message->subject), "privatemsg/view/$message->id")."</td>";
260
261 $msg .= "</tr>\n";
262 }
263
264 if (!$msg)
265 $msg = "<tr><td colspan=\"5\" align=\"center\">".t("No messages")."</td></tr>";
266 else
267 $msg = '
268 <tr>
269 <td width="2%">&nbsp;</td>
270 <td width="3%">&nbsp;</td>
271 <td width="20%"><b>'.t("date").'</b></td>
272 <td width="30%"><b>'.($current_folder == 1 ? t("to") : t("from")).'</b></td>
273 <td width="55%"><b>'.t("subject").'</b></td>
274 </tr>
275 '.$msg;
276
277 $out = "<table width=\"100%\">";
278
279 if ($status_message) {
280 $out .= "<tr><td colspan=\"5\" align=\"center\">$status_message</td></tr>";
281 }
282
283 $extra_folders = array();
284 foreach ($folders as $folder) {
285 $folder_list[] = _privatemsg_format_folder($current_folder, $folder[0], $folder[1]);
286 if ($folder[0] != 1 && $folder[0] != $current_folder) {
287 $extra_folders[$folder[0]] = $folder[1];
288 }
289 }
290
291 $out .= "<tr><td colspan=\"5\" align=\"center\">".implode(" - ", $folder_list)."</td></tr>\n";
292
293 $out .= $msg;
294 $out .= "<tr><td colspan=\"5\" align=\"center\">".theme("pager_display", NULL, ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)))."</td></tr>";
295 $out .= "<tr><td colspan=\"5\" align=\"center\"><br><hr>".form_submit(t("Write a new message"))._form_submit(t("Delete messages"), "onClick=\"return confirm('".t("Are you sure you want to delete these messages?")."')\"")."</td></tr>";
296
297 // folder management
298 $out .= "<tr><td colspan=\"5\" align=\"center\">";
299 if (count($extra_folders) > 0 && $current_folder != 1) {
300 $out .= '<select name="edit[folder]">';
301 foreach ($extra_folders as $fid => $folder_name) {
302 $out .= "<option value='$fid'>$folder_name</option>";
303 }
304 $out .= '</select>';
305
306 $out .= form_submit(t("Move to folder"));
307 }
308
309 if ($current_folder > 1) {
310 // you can't delete Inbox
311 $out .= _form_submit(t("Delete folder"), "onClick=\"return confirm('".t("Are you sure you want to delete this folder and all its messages?")."')\"");
312 }
313
314 $out .= _form_submit(t("Empty folder"), "onClick=\"return confirm('".t("Are you sure you want to delete every message in this folder?")."')\"");
315 $out .= form_hidden("current_folder", $current_folder);
316 $out .= form_submit(t("New folder"))."</td></tr>";
317
318 $out .= "</table>";
319
320 theme("box", t("Private messages"), form($out));
321 }
322
323 function _privatemsg_format_folder($current, $fid, $name) {
324 if ($current == $fid) {
325 return "<b>$name</b>";
326 }
327 else {
328 return l($name, "privatemsg/list/$fid");
329 }
330 }
331
332 function _privatemsg_form($message = 0, $error = "") {
333 global $user;
334
335 if ($message) {
336 if (!is_object($message)) {
337 $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));
338
339 if (!stristr($message->subject, "re:")) {
340 $message->subject = "re: ".$message->subject;
341 }
342
343 # quoting
344 $message->message = "\n".str_replace ("\n", "\n> ", "\n".$message->message);
345 }
346 }
347
348 $to .= "<input type='text' name='recipient' value='".htmlentities($message->name, ENT_QUOTES)."' maxlength='64' size='50'>";
349
350 if (!$message->name) {
351 $to .= ' <select name="quick" onChange="recipient.value=quick.value"><option value="--'.t("contacts").'--" selected="selected">--'.t("contacts").'--</option>';
352 $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);
353 while ($name = db_fetch_object($result)) {
354 $name = htmlentities($name->name);
355 $to .= "<option value='$name'>$name</option>";
356 }
357 $to .= '</select>';
358 }
359
360 $form .= form_item(t("To"), $to);
361
362 $form .= form_textfield(t("Subject"), "subject", $message->subject, 50, 64);
363 $form .= form_textarea(t("Message"), "message", $message->message, 80, 5);
364 $form .= form_submit(t("Send private message"));
365
366 theme("header");
367 theme("box", t("Write a new message"), "<br /><p align=\"center\"><b><i>$error</i></b></p>".form($form));
368 theme("footer");
369 }
370
371 function _privatemsg_edit($edit) {
372 global $user;
373 $recipient = db_result(db_query("SELECT uid FROM users WHERE name = '%s'", $edit["recipient"]));
374
375 throttle("private message", variable_get(privatemsg_max_rate, 15));
376
377 if (!$edit["recipient"] || !$edit["subject"] || !$edit["message"]) {
378 _privatemsg_form(array2object($edit), t("Warning: every field required"));
379 } elseif ($recipient == $user->uid) {
380 _privatemsg_form(array2object($edit), t("A message to yourself?"));
381 } else {
382 if ($recipient) {
383 $result = db_query("INSERT INTO privatemsg (author, recipient, subject, message, timestamp, new, hostname)
384 VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s')", $user->uid, $recipient, strip_tags($edit['subject']), strip_tags($edit['message']), time(), 1, getenv("REMOTE_ADDR"));
385 _privatemsg_list(0, t("Message sent"));
386 } else {
387 _privatemsg_form(array2object($edit), t("Warning: user does not exist"));
388 }
389 }
390 }
391
392 function _privatemsg_view($message_id) {
393 global $user;
394
395 $result = db_query("
396 SELECT id, u.name, author, p.timestamp, subject, message, new, recipient
397 FROM privatemsg p, users u
398 WHERE (recipient = '%d' OR author = '%d')
399 AND author = u.uid
400 AND id = '%d'
401 ", $user->uid, $user->uid, $message_id);
402
403 $message = db_fetch_object($result);
404 if (($message->new) && ($user->uid != $message->author)) {
405 $result = db_query("
406 UPDATE privatemsg
407 SET new = 0
408 WHERE recipient = '$user->uid'
409 AND id = '%d'", $message_id);
410 }
411
412 theme("header");
413 theme("privatemsg_view", $message);
414 theme("footer");
415 }
416
417 function privatemsg_view($message) {
418 global $user;
419
420 if ($message) {
421 $body = "
422 <p><b>".t("From").":</b> ".check_output($message->name)."<br />
423 <b>".t("Subject").":</b> ".check_output($message->subject)."<br />
424 <b>".t("Date").":</b> ".format_date($message->timestamp)."</p>
425 ".check_output($message->message, 1)."
426 <p align='center'>";
427 if ($message->recipient == $user->uid) {
428 $body .= l(t("Reply to this message"), "privatemsg/reply/$message->id")." - ";
429 }
430 if (($message->recipient == $user->uid) || (variable_get("privatemsg_sent_status", 1))) {
431 $body .= l(t("Delete this message"), "privatemsg/delete/$message->id", array("onClick" => "return confirm('".t("Are you sure to delete this message?")."')"))." - ";
432 }
433
434 $body .= l(t("List messages"), "privatemsg")."</p>";
435 } else {
436 $body = t("Error: you can't read this message");
437 }
438
439 theme("box", t("Read message"), $body);
440 }
441
442 function _privatemsg_delete($id) {
443 global $user;
444
445 $result = db_query("SELECT author, recipient FROM privatemsg WHERE (recipient = '$user->uid' OR author = '$user->uid') AND id = '%d'", $id);
446
447 if ($message = db_fetch_object($result)) {
448 if ($message->author == $user->uid) {
449 db_query("UPDATE privatemsg SET author_del = 1 WHERE id = '%d'", $id);
450 }
451 else if ($message->recipient == $user->uid) {
452 db_query("UPDATE privatemsg SET recipient_del = 1 WHERE id = '%d'", $id);
453 }
454
455 return true;
456 } else {
457 return false;
458 }
459 }
460
461 function _privatemsg_get_new_messages($uid) {
462 return db_result(db_query("SELECT COUNT(*) FROM privatemsg WHERE recipient = '%d' AND new = 1 AND recipient_del = 0", $uid));
463 }
464
465 function _privatemsg_new_folder($edit) {
466 global $user;
467
468 if ($edit["name"]) {
469 // check for uniqueness
470 if (!db_result(db_query("SELECT name FROM privatemsg_folder WHERE name = '%s' AND uid = '%d'", $edit["name"], $user->uid))) {
471 db_query("INSERT INTO privatemsg_folder (uid, name) VALUES ('%d', '%s')", $user->uid, $edit["name"]);
472 return 1;
473 }
474 }
475
476 $form .= form_textfield(t("Name"), "name", "", 50, 64);
477 $form .= form_submit(t("Add folder"));
478
479 theme("header");
480 theme("box", t("New folder"), form($form));
481 theme("footer");
482 }
483
484 function _privatemsg_move($mid, $fid) {
485 global $user;
486 db_query("UPDATE privatemsg SET folder = '%d' WHERE id = '%d' AND recipient = '%d'", $fid, $mid, $user->uid);
487 }
488
489 function _form_submit($value, $extra) {
490 return "<input type=\"submit\" name=\"op\" value=\"". check_form($value) ."\" $extra />\n";
491 }
492
493 ?>