truthomatic

This commit is contained in:
Shish 2009-10-08 17:44:25 +01:00
parent 71999a3065
commit 6baf616692
4 changed files with 55 additions and 27 deletions

View File

@ -11,16 +11,38 @@
*/ */
class SendPMEvent extends Event { class SendPMEvent extends Event {
public function __construct($from_id, $from_ip, $to_id, $subject, $message) { public function __construct($pm) {
$this->from_id = $from_id; $this->pm = $pm;
$this->from_ip = $from_ip;
$this->to_id = $to_id;
$this->subject = $subject;
$this->message = $message;
} }
} }
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) { public function onInitExt($event) {
global $config, $database; global $config, $database;
@ -78,7 +100,7 @@ class PM extends SimpleExtension {
else if(($pm["to_id"] == $user->id) || $user->is_admin()) { else if(($pm["to_id"] == $user->id) || $user->is_admin()) {
$from_user = User::by_id(int_escape($pm["from_id"])); $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)); $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 { else {
// permission denied // permission denied
@ -105,7 +127,7 @@ class PM extends SimpleExtension {
$from_id = $user->id; $from_id = $user->id;
$subject = $_POST["subject"]; $subject = $_POST["subject"];
$message = $_POST["message"]; $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_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]); $page->set_redirect($_SERVER["HTTP_REFERER"]);
break; break;
@ -124,8 +146,8 @@ class PM extends SimpleExtension {
from_id, from_ip, to_id, from_id, from_ip, to_id,
sent_date, subject, message) sent_date, subject, message)
VALUES(?, ?, ?, now(), ?, ?)", VALUES(?, ?, ?, now(), ?, ?)",
array($event->from_id, $event->from_ip, array($event->pm->from_id, $event->pm->from_ip,
$event->to_id, $event->subject, $event->message) $event->pm->to_id, $event->pm->subject, $event->pm->message)
); );
log_info("pm", "Sent PM to User #{$event->to_id}"); log_info("pm", "Sent PM to User #{$event->to_id}");
} }
@ -134,12 +156,17 @@ class PM extends SimpleExtension {
private function get_pms(User $user) { private function get_pms(User $user) {
global $database; global $database;
return $database->get_all(" $arr = $database->get_all("
SELECT private_message.*,user_from.name AS from_name SELECT private_message.*,user_from.name AS from_name
FROM private_message FROM private_message
JOIN users AS user_from ON user_from.id=from_id JOIN users AS user_from ON user_from.id=from_id
WHERE to_id = ? WHERE to_id = ?
", array($user->id)); ", array($user->id));
$pms = array();
foreach($arr as $pm) {
$pms[] = new PM($pm);
}
return $pms;
} }
} }
?> ?>

View File

@ -1,5 +1,5 @@
<?php <?php
class PMTest extends SCoreWebTestCase { class PrivMsgTest extends SCoreWebTestCase {
function testPM() { function testPM() {
$this->log_in_as_admin(); $this->log_in_as_admin();
$this->get_page("user/test"); $this->get_page("user/test");

View File

@ -1,6 +1,6 @@
<?php <?php
class PMTheme extends Themelet { class PrivMsgTheme extends Themelet {
public function display_pms(Page $page, $pms) { public function display_pms(Page $page, $pms) {
$html = " $html = "
<script> <script>
@ -14,18 +14,19 @@ class PMTheme extends Themelet {
$n = 0; $n = 0;
foreach($pms as $pm) { foreach($pms as $pm) {
$oe = ($n++ % 2 == 0) ? "even" : "odd"; $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)"; if(strlen(trim($h_subject)) == 0) $h_subject = "(No subject)";
$h_from = html_escape($pm["from_name"]); $from_name = User::by_id($pm->from_id)->name;
$from_url = make_link("user/".url_escape($pm["from_name"])); $h_from = html_escape($from_name);
$pm_url = make_link("pm/read/".$pm["id"]); $from_url = make_link("user/".url_escape($from_name));
$del_url = make_link("pm/delete/".$pm["id"]); $pm_url = make_link("pm/read/".$pm->id);
$h_date = html_escape($pm["sent_date"]); $del_url = make_link("pm/delete/".$pm->id);
if($pm["is_read"] == "N") $h_subject = "<b>$h_subject</b>"; $h_date = html_escape($pm->sent_date);
if($pm->is_read) $h_subject = "<b>$h_subject</b>";
$html .= "<tr class='$oe'><td><a href='$pm_url'>$h_subject</a></td> $html .= "<tr class='$oe'><td><a href='$pm_url'>$h_subject</a></td>
<td><a href='$from_url'>$h_from</a></td><td>$h_date</td> <td><a href='$from_url'>$h_from</a></td><td>$h_date</td>
<td><form action='$del_url'> <td><form action='$del_url'>
<input type='hidden' name='q' value='/pm/delete/{$pm["id"]}'> <input type='hidden' name='q' value='/pm/delete/{$pm->id}'>
<input type='submit' value='Delete'> <input type='submit' value='Delete'>
</form></td></tr>"; </form></td></tr>";
} }
@ -53,12 +54,12 @@ EOD;
$page->add_block(new Block("Write a PM", $html, "main", 20)); $page->add_block(new Block("Write a PM", $html, "main", 20));
} }
public function display_message(Page $page, User $from, User $to, $pm) { public function display_message(Page $page, User $from, User $to, PM $pm) {
$this->display_composer($page, $to, $from, "Re: ".$pm["subject"]); $this->display_composer($page, $to, $from, "Re: ".$pm->subject);
$page->set_title("Private Message"); $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 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));
} }
} }
?> ?>

View File

@ -76,7 +76,7 @@ abstract class BaseConfig implements Config {
return $this->get($name, $default); return $this->get($name, $default);
} }
public function get_bool($name, $default=null) { 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()) { public function get_array($name, $default=array()) {
return explode(",", $this->get($name, "")); return explode(",", $this->get($name, ""));