PSR-2. I'm not a huge fan, but ugly consistency beats no consistency...

This commit is contained in:
Shish
2019-05-28 17:59:38 +01:00
parent 5ec3e89884
commit 34b05cca7c
295 changed files with 27094 additions and 24632 deletions

View File

@ -10,49 +10,61 @@
* profile page and a box will be shown.
*/
class SendPMEvent extends Event {
public $pm;
class SendPMEvent extends Event
{
public $pm;
public function __construct(PM $pm) {
$this->pm = $pm;
}
public function __construct(PM $pm)
{
$this->pm = $pm;
}
}
class PM {
public $id, $from_id, $from_ip, $to_id, $sent_date, $subject, $message, $is_read;
class PM
{
public $id;
public $from_id;
public $from_ip;
public $to_id;
public $sent_date;
public $subject;
public $message;
public $is_read;
public function __construct($from_id=0, string $from_ip="0.0.0.0", int $to_id=0, string $subject="A Message", string $message="Some Text", bool $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 = bool_escape($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;
}
}
public function __construct($from_id=0, string $from_ip="0.0.0.0", int $to_id=0, string $subject="A Message", string $message="Some Text", bool $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 = bool_escape($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) {
global $config, $database;
class PrivMsg extends Extension
{
public function onInitExt(InitExtEvent $event)
{
global $config, $database;
// shortcut to latest
if($config->get_int("pm_version") < 1) {
$database->create_table("private_message", "
// 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,
@ -64,150 +76,155 @@ class PrivMsg extends Extension {
FOREIGN KEY (from_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (to_id) REFERENCES users(id) ON DELETE CASCADE
");
$database->execute("CREATE INDEX private_message__to_id ON private_message(to_id)");
$config->set_int("pm_version", 2);
log_info("pm", "extension installed");
}
$database->execute("CREATE INDEX private_message__to_id ON private_message(to_id)");
$config->set_int("pm_version", 2);
log_info("pm", "extension installed");
}
if($config->get_int("pm_version") < 2) {
log_info("pm", "Adding foreign keys to private messages");
$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
if ($config->get_int("pm_version") < 2) {
log_info("pm", "Adding foreign keys to private messages");
$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");
}
}
$config->set_int("pm_version", 2);
log_info("pm", "extension installed");
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if(!$user->is_anonymous()) {
$count = $this->count_pms($user);
$h_count = $count > 0 ? " <span class='unread'>($count)</span>" : "";
$event->add_link("Private Messages$h_count", make_link("user#private-messages"));
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
if (!$user->is_anonymous()) {
$count = $this->count_pms($user);
$h_count = $count > 0 ? " <span class='unread'>($count)</span>" : "";
$event->add_link("Private Messages$h_count", make_link("user#private-messages"));
}
}
public function onUserPageBuilding(UserPageBuildingEvent $event) {
global $page, $user;
$duser = $event->display_user;
if(!$user->is_anonymous() && !$duser->is_anonymous()) {
if(($user->id == $duser->id) || $user->can("view_other_pms")) {
$this->theme->display_pms($page, $this->get_pms($duser));
}
if($user->id != $duser->id) {
$this->theme->display_composer($page, $user, $duser);
}
}
}
public function onUserPageBuilding(UserPageBuildingEvent $event)
{
global $page, $user;
$duser = $event->display_user;
if (!$user->is_anonymous() && !$duser->is_anonymous()) {
if (($user->id == $duser->id) || $user->can("view_other_pms")) {
$this->theme->display_pms($page, $this->get_pms($duser));
}
if ($user->id != $duser->id) {
$this->theme->display_composer($page, $user, $duser);
}
}
}
public function onPageRequest(PageRequestEvent $event) {
global $database, $page, $user;
if($event->page_matches("pm")) {
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 = :id", array("id" => $pm_id));
if(is_null($pm)) {
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
}
else if(($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$from_user = User::by_id(int_escape($pm["from_id"]));
if($pm["to_id"] == $user->id) {
$database->execute("UPDATE private_message SET is_read='Y' WHERE id = :id", array("id" => $pm_id));
$database->cache->delete("pm-count-{$user->id}");
}
$this->theme->display_message($page, $from_user, $user, new PM($pm));
}
else {
// permission denied
}
break;
case "delete":
if($user->check_auth_token()) {
$pm_id = int_escape($_POST["pm_id"]);
$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");
}
else if(($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$database->execute("DELETE FROM private_message WHERE id = :id", array("id" => $pm_id));
$database->cache->delete("pm-count-{$user->id}");
log_info("pm", "Deleted PM #$pm_id", "PM deleted");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
}
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)));
flash_message("PM sent");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
break;
default:
$this->theme->display_error(400, "Invalid action", "That's not something you can do with a PM");
break;
}
}
}
}
public function onPageRequest(PageRequestEvent $event)
{
global $database, $page, $user;
if ($event->page_matches("pm")) {
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 = :id", ["id" => $pm_id]);
if (is_null($pm)) {
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
} elseif (($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$from_user = User::by_id(int_escape($pm["from_id"]));
if ($pm["to_id"] == $user->id) {
$database->execute("UPDATE private_message SET is_read='Y' WHERE id = :id", ["id" => $pm_id]);
$database->cache->delete("pm-count-{$user->id}");
}
$this->theme->display_message($page, $from_user, $user, new PM($pm));
} else {
// permission denied
}
break;
case "delete":
if ($user->check_auth_token()) {
$pm_id = int_escape($_POST["pm_id"]);
$pm = $database->get_row("SELECT * FROM private_message WHERE id = :id", ["id" => $pm_id]);
if (is_null($pm)) {
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
} elseif (($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$database->execute("DELETE FROM private_message WHERE id = :id", ["id" => $pm_id]);
$database->cache->delete("pm-count-{$user->id}");
log_info("pm", "Deleted PM #$pm_id", "PM deleted");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
}
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)));
flash_message("PM sent");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
break;
default:
$this->theme->display_error(400, "Invalid action", "That's not something you can do with a PM");
break;
}
}
}
}
public function onSendPM(SendPMEvent $event) {
global $database;
$database->execute("
public function onSendPM(SendPMEvent $event)
{
global $database;
$database->execute(
"
INSERT INTO private_message(
from_id, from_ip, to_id,
sent_date, subject, message)
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)
);
$database->cache->delete("pm-count-{$event->pm->to_id}");
log_info("pm", "Sent PM to User #{$event->pm->to_id}");
}
["fromid" => $event->pm->from_id, "fromip" => $event->pm->from_ip,
"toid" => $event->pm->to_id, "subject" => $event->pm->subject, "message" => $event->pm->message]
);
$database->cache->delete("pm-count-{$event->pm->to_id}");
log_info("pm", "Sent PM to User #{$event->pm->to_id}");
}
private function get_pms(User $user) {
global $database;
private function get_pms(User $user)
{
global $database;
$arr = $database->get_all("
$arr = $database->get_all(
"
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
ORDER BY sent_date DESC",
array("toid" => $user->id));
$pms = array();
foreach($arr as $pm) {
$pms[] = new PM($pm);
}
return $pms;
}
["toid" => $user->id]
);
$pms = [];
foreach ($arr as $pm) {
$pms[] = new PM($pm);
}
return $pms;
}
private function count_pms(User $user) {
global $database;
private function count_pms(User $user)
{
global $database;
$count = $database->cache->get("pm-count:{$user->id}");
if(is_null($count) || $count === false) {
$count = $database->get_one("
$count = $database->cache->get("pm-count:{$user->id}");
if (is_null($count) || $count === false) {
$count = $database->get_one("
SELECT count(*)
FROM private_message
WHERE to_id = :to_id
AND is_read = :is_read
", array("to_id" => $user->id, "is_read" => "N"));
$database->cache->set("pm-count:{$user->id}", $count, 600);
}
return $count;
}
", ["to_id" => $user->id, "is_read" => "N"]);
$database->cache->set("pm-count:{$user->id}", $count, 600);
}
return $count;
}
}

View File

@ -1,59 +1,61 @@
<?php
class PrivMsgTest extends ShimmiePHPUnitTestCase {
public function testPM() {
$this->log_in_as_admin();
$this->get_page("user/test");
class PrivMsgTest extends ShimmiePHPUnitTestCase
{
public function testPM()
{
$this->log_in_as_admin();
$this->get_page("user/test");
$this->markTestIncomplete();
$this->markTestIncomplete();
$this->set_field('subject', "message demo to test");
$this->set_field('message', "message contents");
$this->click("Send");
$this->log_out();
$this->set_field('subject', "message demo to test");
$this->set_field('message', "message contents");
$this->click("Send");
$this->log_out();
$this->log_in_as_user();
$this->get_page("user");
$this->assert_text("message demo to test");
$this->click("message demo to test");
$this->assert_text("message contents");
$this->back();
$this->click("Delete");
$this->assert_no_text("message demo to test");
$this->log_in_as_user();
$this->get_page("user");
$this->assert_text("message demo to test");
$this->click("message demo to test");
$this->assert_text("message contents");
$this->back();
$this->click("Delete");
$this->assert_no_text("message demo to test");
$this->get_page("pm/read/0");
$this->assert_text("No such PM");
// GET doesn't work due to auth token check
//$this->get_page("pm/delete/0");
//$this->assert_text("No such PM");
$this->get_page("pm/waffle/0");
$this->assert_text("Invalid action");
$this->get_page("pm/read/0");
$this->assert_text("No such PM");
// GET doesn't work due to auth token check
//$this->get_page("pm/delete/0");
//$this->assert_text("No such PM");
$this->get_page("pm/waffle/0");
$this->assert_text("Invalid action");
$this->log_out();
}
$this->log_out();
}
public function testAdminAccess() {
$this->log_in_as_admin();
$this->get_page("user/test");
public function testAdminAccess()
{
$this->log_in_as_admin();
$this->get_page("user/test");
$this->markTestIncomplete();
$this->markTestIncomplete();
$this->set_field('subject', "message demo to test");
$this->set_field('message', "message contents");
$this->click("Send");
$this->set_field('subject', "message demo to test");
$this->set_field('message', "message contents");
$this->click("Send");
$this->get_page("user/test");
$this->assert_text("message demo to test");
$this->click("message demo to test");
$this->assert_text("message contents");
$this->back();
$this->click("Delete");
$this->get_page("user/test");
$this->assert_text("message demo to test");
$this->click("message demo to test");
$this->assert_text("message contents");
$this->back();
$this->click("Delete");
# simpletest bug? - redirect(referrer) works in opera, not in
# webtestcase, so we end up at the wrong page...
$this->get_page("user/test");
$this->assert_title("test's Page");
$this->assert_no_text("message demo to test");
$this->log_out();
}
# simpletest bug? - redirect(referrer) works in opera, not in
# webtestcase, so we end up at the wrong page...
$this->get_page("user/test");
$this->assert_title("test's Page");
$this->assert_no_text("message demo to test");
$this->log_out();
}
}

View File

@ -1,30 +1,34 @@
<?php
class PrivMsgTheme extends Themelet {
public function display_pms(Page $page, $pms) {
global $user;
class PrivMsgTheme extends Themelet
{
public function display_pms(Page $page, $pms)
{
global $user;
$html = "
$html = "
<table id='pms' class='zebra sortable'>
<thead><tr><th>R?</th><th>Subject</th><th>From</th><th>Date</th><th>Action</th></tr></thead>
<tbody>";
foreach($pms as $pm) {
$h_subject = html_escape($pm->subject);
if(strlen(trim($h_subject)) == 0) $h_subject = "(No subject)";
$from = User::by_id($pm->from_id);
$from_name = $from->name;
$h_from = html_escape($from_name);
$from_url = make_link("user/".url_escape($from_name));
$pm_url = make_link("pm/read/".$pm->id);
$del_url = make_link("pm/delete");
$h_date = html_escape($pm->sent_date);
$readYN = "Y";
if(!$pm->is_read) {
$h_subject = "<b>$h_subject</b>";
$readYN = "N";
}
$hb = $from->can("hellbanned") ? "hb" : "";
$html .= "<tr class='$hb'>
foreach ($pms as $pm) {
$h_subject = html_escape($pm->subject);
if (strlen(trim($h_subject)) == 0) {
$h_subject = "(No subject)";
}
$from = User::by_id($pm->from_id);
$from_name = $from->name;
$h_from = html_escape($from_name);
$from_url = make_link("user/".url_escape($from_name));
$pm_url = make_link("pm/read/".$pm->id);
$del_url = make_link("pm/delete");
$h_date = html_escape($pm->sent_date);
$readYN = "Y";
if (!$pm->is_read) {
$h_subject = "<b>$h_subject</b>";
$readYN = "N";
}
$hb = $from->can("hellbanned") ? "hb" : "";
$html .= "<tr class='$hb'>
<td>$readYN</td>
<td><a href='$pm_url'>$h_subject</a></td>
<td><a href='$from_url'>$h_from</a></td><td>$h_date</td>
@ -34,21 +38,22 @@ class PrivMsgTheme extends Themelet {
<input type='submit' value='Delete'>
</form></td>
</tr>";
}
$html .= "
}
$html .= "
</tbody>
</table>
";
$page->add_block(new Block("Private Messages", $html, "main", 40, "private-messages"));
}
$page->add_block(new Block("Private Messages", $html, "main", 40, "private-messages"));
}
public function display_composer(Page $page, User $from, User $to, $subject="") {
global $user;
$post_url = make_link("pm/send");
$h_subject = html_escape($subject);
$to_id = $to->id;
$auth = $user->get_auth_html();
$html = <<<EOD
public function display_composer(Page $page, User $from, User $to, $subject="")
{
global $user;
$post_url = make_link("pm/send");
$h_subject = html_escape($subject);
$to_id = $to->id;
$auth = $user->get_auth_html();
$html = <<<EOD
<form action="$post_url" method="POST">
$auth
<input type="hidden" name="to_id" value="$to_id">
@ -59,15 +64,15 @@ $auth
</table>
</form>
EOD;
$page->add_block(new Block("Write a PM", $html, "main", 50));
}
$page->add_block(new Block("Write a PM", $html, "main", 50));
}
public function display_message(Page $page, User $from, User $to, PM $pm) {
$this->display_composer($page, $to, $from, "Re: ".$pm->subject);
$page->set_title("Private Message");
$page->set_heading(html_escape($pm->subject));
$page->add_block(new NavBlock());
$page->add_block(new Block("Message from {$from->name}", format_text($pm->message), "main", 10));
}
public function display_message(Page $page, User $from, User $to, PM $pm)
{
$this->display_composer($page, $to, $from, "Re: ".$pm->subject);
$page->set_title("Private Message");
$page->set_heading(html_escape($pm->subject));
$page->add_block(new NavBlock());
$page->add_block(new Block("Message from {$from->name}", format_text($pm->message), "main", 10));
}
}