From 6baf616692884fb065a49ff49f5f6abb5c1171c1 Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 8 Oct 2009 17:44:25 +0100 Subject: [PATCH] truthomatic --- contrib/pm/main.php | 51 +++++++++++++++++++++++++++++++++---------- contrib/pm/test.php | 2 +- contrib/pm/theme.php | 27 ++++++++++++----------- core/config.class.php | 2 +- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/contrib/pm/main.php b/contrib/pm/main.php index 5df0dda0..ce71b2ff 100644 --- a/contrib/pm/main.php +++ b/contrib/pm/main.php @@ -11,16 +11,38 @@ */ class SendPMEvent extends Event { - public function __construct($from_id, $from_ip, $to_id, $subject, $message) { - $this->from_id = $from_id; - $this->from_ip = $from_ip; - $this->to_id = $to_id; - $this->subject = $subject; - $this->message = $message; + public function __construct($pm) { + $this->pm = $pm; } } -class PM extends SimpleExtension { +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 { public function onInitExt($event) { global $config, $database; @@ -78,7 +100,7 @@ class PM extends SimpleExtension { else if(($pm["to_id"] == $user->id) || $user->is_admin()) { $from_user = User::by_id(int_escape($pm["from_id"])); $database->get_row("UPDATE private_message SET is_read='Y' WHERE id = ?", array($pm_id)); - $this->theme->display_message($page, $from_user, $user, $pm); + $this->theme->display_message($page, $from_user, $user, new PM($pm)); } else { // permission denied @@ -105,7 +127,7 @@ class PM extends SimpleExtension { $from_id = $user->id; $subject = $_POST["subject"]; $message = $_POST["message"]; - send_event(new SendPMEvent($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message)); + send_event(new SendPMEvent(new PM($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message))); $page->set_mode("redirect"); $page->set_redirect($_SERVER["HTTP_REFERER"]); break; @@ -124,8 +146,8 @@ class PM extends SimpleExtension { from_id, from_ip, to_id, sent_date, subject, message) VALUES(?, ?, ?, now(), ?, ?)", - array($event->from_id, $event->from_ip, - $event->to_id, $event->subject, $event->message) + array($event->pm->from_id, $event->pm->from_ip, + $event->pm->to_id, $event->pm->subject, $event->pm->message) ); log_info("pm", "Sent PM to User #{$event->to_id}"); } @@ -134,12 +156,17 @@ class PM extends SimpleExtension { private function get_pms(User $user) { global $database; - return $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 = ? ", array($user->id)); + $pms = array(); + foreach($arr as $pm) { + $pms[] = new PM($pm); + } + return $pms; } } ?> diff --git a/contrib/pm/test.php b/contrib/pm/test.php index 5cea7e39..7bd2ce8c 100644 --- a/contrib/pm/test.php +++ b/contrib/pm/test.php @@ -1,5 +1,5 @@ log_in_as_admin(); $this->get_page("user/test"); diff --git a/contrib/pm/theme.php b/contrib/pm/theme.php index 7454d8f8..bfb454a0 100644 --- a/contrib/pm/theme.php +++ b/contrib/pm/theme.php @@ -1,6 +1,6 @@ @@ -14,18 +14,19 @@ class PMTheme extends Themelet { $n = 0; foreach($pms as $pm) { $oe = ($n++ % 2 == 0) ? "even" : "odd"; - $h_subject = html_escape($pm["subject"]); + $h_subject = html_escape($pm->subject); if(strlen(trim($h_subject)) == 0) $h_subject = "(No subject)"; - $h_from = html_escape($pm["from_name"]); - $from_url = make_link("user/".url_escape($pm["from_name"])); - $pm_url = make_link("pm/read/".$pm["id"]); - $del_url = make_link("pm/delete/".$pm["id"]); - $h_date = html_escape($pm["sent_date"]); - if($pm["is_read"] == "N") $h_subject = "$h_subject"; + $from_name = User::by_id($pm->from_id)->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/".$pm->id); + $h_date = html_escape($pm->sent_date); + if($pm->is_read) $h_subject = "$h_subject"; $html .= "$h_subject $h_from$h_date
- +
"; } @@ -53,12 +54,12 @@ EOD; $page->add_block(new Block("Write a PM", $html, "main", 20)); } - public function display_message(Page $page, User $from, User $to, $pm) { - $this->display_composer($page, $to, $from, "Re: ".$pm["subject"]); + 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->set_heading(html_escape($pm->subject)); $page->add_block(new NavBlock()); - $page->add_block(new Block("Message", format_text($pm["message"]), "main", 10)); + $page->add_block(new Block("Message", format_text($pm->message), "main", 10)); } } ?> diff --git a/core/config.class.php b/core/config.class.php index e8967f55..155d7dda 100644 --- a/core/config.class.php +++ b/core/config.class.php @@ -76,7 +76,7 @@ abstract class BaseConfig implements Config { return $this->get($name, $default); } public function get_bool($name, $default=null) { - return ($this->get($name, $default) == 'Y'); + return undb_bool($this->get($name, $default)); } public function get_array($name, $default=array()) { return explode(",", $this->get($name, ""));