2008-09-07 05:57:28 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2008-09-07 05:57:28 +00:00
|
|
|
* Name: Private Messaging
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Allow users to send messages to eachother
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* PMs show up on a user's profile page, readable by that user
|
|
|
|
* as well as board admins. To send a PM, visit another user's
|
|
|
|
* profile page and a box will be shown.
|
2008-09-07 05:57:28 +00:00
|
|
|
*/
|
|
|
|
|
2008-09-07 13:55:28 +00:00
|
|
|
class SendPMEvent extends Event {
|
2009-10-08 16:44:25 +00:00
|
|
|
public function __construct($pm) {
|
|
|
|
$this->pm = $pm;
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-08 16:44:25 +00:00
|
|
|
class PM {
|
|
|
|
public function __construct($from_id=0, $from_ip="0.0.0.0", $to_id=0, $subject="A Message", $message="Some Text", $read=False) {
|
|
|
|
# PHP: the P stands for "really", the H stands for "awful" and the other P stands for "language"
|
|
|
|
if(is_array($from_id)) {
|
|
|
|
$a = $from_id;
|
|
|
|
$this->id = $a["id"];
|
|
|
|
$this->from_id = $a["from_id"];
|
|
|
|
$this->from_ip = $a["from_ip"];
|
|
|
|
$this->to_id = $a["to_id"];
|
|
|
|
$this->sent_date = $a["sent_date"];
|
|
|
|
$this->subject = $a["subject"];
|
|
|
|
$this->message = $a["message"];
|
|
|
|
$this->is_read = undb_bool($a["is_read"]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->id = -1;
|
|
|
|
$this->from_id = $from_id;
|
|
|
|
$this->from_ip = $from_ip;
|
|
|
|
$this->to_id = $to_id;
|
|
|
|
$this->subject = $subject;
|
|
|
|
$this->message = $message;
|
|
|
|
$this->is_read = $read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PrivMsg extends SimpleExtension {
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onInitExt($event) {
|
|
|
|
global $config, $database;
|
2008-09-07 05:57:28 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
// shortcut to latest
|
|
|
|
if($config->get_int("pm_version") < 1) {
|
|
|
|
$database->create_table("private_message", "
|
|
|
|
id SCORE_AIPK,
|
|
|
|
from_id INTEGER NOT NULL,
|
|
|
|
from_ip SCORE_INET NOT NULL,
|
|
|
|
to_id INTEGER NOT NULL,
|
|
|
|
sent_date DATETIME NOT NULL,
|
|
|
|
subject VARCHAR(64) NOT NULL,
|
|
|
|
message TEXT NOT NULL,
|
|
|
|
is_read SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
|
|
|
|
INDEX (to_id)
|
|
|
|
");
|
|
|
|
$config->set_int("pm_version", 1);
|
|
|
|
log_info("pm", "extension installed");
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-09-07 05:57:28 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
/*
|
|
|
|
public function onUserBlockBuilding($event) {
|
|
|
|
global $user;
|
|
|
|
if(!$user->is_anonymous()) {
|
|
|
|
$event->add_link("Private Messages", make_link("pm"));
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
|
|
|
*/
|
2008-09-07 05:57:28 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onUserPageBuilding($event) {
|
|
|
|
global $page, $user;
|
|
|
|
$duser = $event->display_user;
|
|
|
|
if(!$user->is_anonymous() && !$duser->is_anonymous()) {
|
|
|
|
if(($user->id == $duser->id) || $user->is_admin()) {
|
|
|
|
$this->theme->display_pms($page, $this->get_pms($duser));
|
|
|
|
}
|
|
|
|
if($user->id != $duser->id) {
|
|
|
|
$this->theme->display_composer($page, $user, $duser);
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-09-07 05:57:28 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onPageRequest($event) {
|
2009-07-19 03:48:25 +00:00
|
|
|
global $database, $page, $user;
|
2009-05-11 21:09:24 +00:00
|
|
|
if($event->page_matches("pm")) {
|
2008-09-07 13:55:28 +00:00
|
|
|
if(!$user->is_anonymous()) {
|
|
|
|
switch($event->get_arg(0)) {
|
|
|
|
case "read":
|
|
|
|
$pm_id = int_escape($event->get_arg(1));
|
|
|
|
$pm = $database->get_row("SELECT * FROM private_message WHERE id = ?", array($pm_id));
|
|
|
|
if(is_null($pm)) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_error($page, "No such PM", "There is no PM #$pm_id");
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
|
|
|
else if(($pm["to_id"] == $user->id) || $user->is_admin()) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$from_user = User::by_id(int_escape($pm["from_id"]));
|
2008-09-07 13:55:28 +00:00
|
|
|
$database->get_row("UPDATE private_message SET is_read='Y' WHERE id = ?", array($pm_id));
|
2009-10-08 16:44:25 +00:00
|
|
|
$this->theme->display_message($page, $from_user, $user, new PM($pm));
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// permission denied
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "delete":
|
|
|
|
$pm_id = int_escape($event->get_arg(1));
|
|
|
|
$pm = $database->get_row("SELECT * FROM private_message WHERE id = ?", array($pm_id));
|
|
|
|
if(is_null($pm)) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_error($page, "No such PM", "There is no PM #$pm_id");
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
|
|
|
else if(($pm["to_id"] == $user->id) || $user->is_admin()) {
|
|
|
|
$database->execute("DELETE FROM private_message WHERE id = ?", array($pm_id));
|
2009-05-08 10:52:29 +00:00
|
|
|
log_info("pm", "Deleted PM #$pm_id");
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
2009-07-19 16:59:57 +00:00
|
|
|
$page->set_redirect($_SERVER["HTTP_REFERER"]);
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// permission denied
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "send":
|
|
|
|
$to_id = int_escape($_POST["to_id"]);
|
|
|
|
$from_id = $user->id;
|
|
|
|
$subject = $_POST["subject"];
|
|
|
|
$message = $_POST["message"];
|
2009-10-08 16:44:25 +00:00
|
|
|
send_event(new SendPMEvent(new PM($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message)));
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
2009-07-19 16:59:57 +00:00
|
|
|
$page->set_redirect($_SERVER["HTTP_REFERER"]);
|
2008-09-07 13:55:28 +00:00
|
|
|
break;
|
2009-07-19 03:48:25 +00:00
|
|
|
default:
|
|
|
|
$this->theme->display_error($page, "Invalid action", "That's not something you can do with a PM");
|
|
|
|
break;
|
2008-09-07 13:55:28 +00:00
|
|
|
}
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onSendPM($event) {
|
2009-07-19 03:48:25 +00:00
|
|
|
global $database;
|
2009-05-11 21:09:24 +00:00
|
|
|
$database->execute("
|
|
|
|
INSERT INTO private_message(
|
|
|
|
from_id, from_ip, to_id,
|
|
|
|
sent_date, subject, message)
|
|
|
|
VALUES(?, ?, ?, now(), ?, ?)",
|
2009-10-08 16:44:25 +00:00
|
|
|
array($event->pm->from_id, $event->pm->from_ip,
|
|
|
|
$event->pm->to_id, $event->pm->subject, $event->pm->message)
|
2009-05-11 21:09:24 +00:00
|
|
|
);
|
2010-02-01 18:25:54 +00:00
|
|
|
log_info("pm", "Sent PM to User #{$event->pm->to_id}");
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2008-09-07 05:57:28 +00:00
|
|
|
private function get_pms(User $user) {
|
|
|
|
global $database;
|
|
|
|
|
2009-10-08 16:44:25 +00:00
|
|
|
$arr = $database->get_all("
|
2008-09-07 05:57:28 +00:00
|
|
|
SELECT private_message.*,user_from.name AS from_name
|
|
|
|
FROM private_message
|
|
|
|
JOIN users AS user_from ON user_from.id=from_id
|
|
|
|
WHERE to_id = ?
|
|
|
|
", array($user->id));
|
2009-10-08 16:44:25 +00:00
|
|
|
$pms = array();
|
|
|
|
foreach($arr as $pm) {
|
|
|
|
$pms[] = new PM($pm);
|
|
|
|
}
|
|
|
|
return $pms;
|
2008-09-07 05:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|