This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/pm/main.php

206 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2009-08-20 22:37:17 +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.
*/
class SendPMEvent extends Event {
public function __construct(PM $pm) {
2009-10-08 16:44:25 +00:00
$this->pm = $pm;
}
}
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 Extension {
public function onInitExt(InitExtEvent $event) {
2009-05-11 21:09:24 +00:00
global $config, $database;
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),
FOREIGN KEY (from_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (to_id) REFERENCES users(id) ON DELETE CASCADE
2009-05-11 21:09:24 +00:00
");
2012-03-11 01:41:33 +00:00
$config->set_int("pm_version", 2);
2009-05-11 21:09:24 +00:00
log_info("pm", "extension installed");
}
if($config->get_int("pm_version") < 2) {
log_info("pm", "Adding foreign keys to private messages");
2012-02-07 19:30:51 +00:00
$database->Execute("delete from private_message where to_id not in (select id from users);");
$database->Execute("delete from private_message where from_id not in (select id from users);");
$database->Execute("ALTER TABLE private_message
ADD FOREIGN KEY (from_id) REFERENCES users(id) ON DELETE CASCADE,
ADD FOREIGN KEY (to_id) REFERENCES users(id) ON DELETE CASCADE;");
$config->set_int("pm_version", 2);
log_info("pm", "extension installed");
}
2009-05-11 21:09:24 +00:00
}
2012-03-02 17:41:25 +00:00
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2009-05-11 21:09:24 +00:00
global $user;
if(!$user->is_anonymous()) {
2012-03-02 17:41:25 +00:00
$count = $this->count_pms($user);
$h_count = $count > 0 ? " ($count)" : "";
$event->add_link("Private Messages$h_count", make_link("user#private-messages"));
}
2009-05-11 21:09:24 +00:00
}
2012-02-09 14:22:08 +00:00
public function onUserPageBuilding(UserPageBuildingEvent $event) {
2009-05-11 21:09:24 +00:00
global $page, $user;
$duser = $event->display_user;
if(!$user->is_anonymous() && !$duser->is_anonymous()) {
2012-03-31 11:28:34 +00:00
if(($user->id == $duser->id) || $user->can("view_other_pms")) {
2009-05-11 21:09:24 +00:00
$this->theme->display_pms($page, $this->get_pms($duser));
}
if($user->id != $duser->id) {
$this->theme->display_composer($page, $user, $duser);
}
}
2009-05-11 21:09:24 +00:00
}
public function onPageRequest(PageRequestEvent $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")) {
if(!$user->is_anonymous()) {
switch($event->get_arg(0)) {
case "read":
$pm_id = int_escape($event->get_arg(1));
2011-02-22 22:29:31 +00:00
$pm = $database->get_row("SELECT * FROM private_message WHERE id = :id", array("id" => $pm_id));
if(is_null($pm)) {
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
}
2012-03-31 11:28:34 +00:00
else if(($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$from_user = User::by_id(int_escape($pm["from_id"]));
2012-03-09 21:54:09 +00:00
$database->execute("UPDATE private_message SET is_read='Y' WHERE id = :id", array("id" => $pm_id));
2012-03-02 17:41:25 +00:00
$database->cache->delete("pm-count-{$user->id}");
2009-10-08 16:44:25 +00:00
$this->theme->display_message($page, $from_user, $user, new PM($pm));
}
else {
// permission denied
}
break;
case "delete":
2010-05-28 13:26:46 +00:00
if($user->check_auth_token()) {
$pm_id = int_escape($_POST["pm_id"]);
2011-02-22 22:29:31 +00:00
$pm = $database->get_row("SELECT * FROM private_message WHERE id = :id", array("id" => $pm_id));
2010-05-28 13:26:46 +00:00
if(is_null($pm)) {
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
2010-05-28 13:26:46 +00:00
}
2012-03-31 11:28:34 +00:00
else if(($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
2011-02-22 22:29:31 +00:00
$database->execute("DELETE FROM private_message WHERE id = :id", array("id" => $pm_id));
2012-03-02 17:41:25 +00:00
$database->cache->delete("pm-count-{$user->id}");
2010-05-28 13:26:46 +00:00
log_info("pm", "Deleted PM #$pm_id");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
}
2010-05-28 13:26:46 +00:00
break;
case "send":
if($user->check_auth_token()) {
$to_id = int_escape($_POST["to_id"]);
$from_id = $user->id;
$subject = $_POST["subject"];
$message = $_POST["message"];
send_event(new SendPMEvent(new PM($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message)));
$page->set_mode("redirect");
2009-07-19 16:59:57 +00:00
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
break;
2009-07-19 03:48:25 +00:00
default:
$this->theme->display_error(400, "Invalid action", "That's not something you can do with a PM");
2009-07-19 03:48:25 +00:00
break;
}
}
}
}
public function onSendPM(SendPMEvent $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)
2011-02-22 22:29:31 +00:00
VALUES(:fromid, :fromip, :toid, now(), :subject, :message)",
array("fromid" => $event->pm->from_id, "fromip" => $event->pm->from_ip,
"toid" => $event->pm->to_id, "subject" => $event->pm->subject, "message" => $event->pm->message)
2009-05-11 21:09:24 +00:00
);
2012-03-02 17:41:25 +00:00
$database->cache->delete("pm-count-{$event->pm->to_id}");
2010-02-01 18:25:54 +00:00
log_info("pm", "Sent PM to User #{$event->pm->to_id}");
}
2009-05-11 21:09:24 +00:00
private function get_pms(User $user) {
global $database;
2009-10-08 16:44:25 +00:00
$arr = $database->get_all("
2011-02-22 22:29:31 +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 = :toid",
array("toid" => $user->id));
2009-10-08 16:44:25 +00:00
$pms = array();
foreach($arr as $pm) {
$pms[] = new PM($pm);
}
return $pms;
}
2012-03-02 17:41:25 +00:00
private function count_pms(User $user) {
global $database;
$count = $database->cache->get("pm-count-{$user->id}");
if(is_null($count) || $count === false) {
2012-03-02 17:41:25 +00:00
$count = $database->get_one("
SELECT count(*)
FROM private_message
WHERE to_id = :to_id
2012-03-02 17:41:25 +00:00
AND is_read = :is_read
", array("to_id" => $user->id, "is_read" => "N"));
2012-03-02 17:41:25 +00:00
$database->cache->set("pm-count-{$user->id}", $count, 60);
}
return $count;
}
}
?>