give the report image extension some love

git-svn-id: file:///home/shish/svn/shimmie2/trunk@983 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-08-12 02:37:48 +00:00
parent e3701f3916
commit 0cbc905e7c
3 changed files with 118 additions and 180 deletions

View File

@ -7,9 +7,6 @@
* Description: Report images as dupes/illegal/etc * Description: Report images as dupes/illegal/etc
* Version 0.3a - See changelog in main.php * Version 0.3a - See changelog in main.php
* November 06, 2007 * November 06, 2007
*
* NOTE: This is for Shimmie2 SVN Trunk. Use the other main.php.use... for Shimmie2 RCx.
*
*/ */
class RemoveReportedImageEvent extends Event { class RemoveReportedImageEvent extends Event {
@ -21,79 +18,76 @@ class RemoveReportedImageEvent extends Event {
} }
class AddReportedImageEvent extends Event { class AddReportedImageEvent extends Event {
var $reporter_name; var $reporter_id;
var $image_id; var $image_id;
var $reason_type;
var $reason; var $reason;
public function AddReportedImageEvent($image_id, $reporter_name, $reason_type, $reason) {
$this->reporter_name = $reporter_name; public function AddReportedImageEvent($image_id, $reporter_id, $reason) {
$this->reporter_id = $reporter_id;
$this->image_id = $image_id; $this->image_id = $image_id;
$this->reason_type = $reason_type;
$this->reason = $reason; $this->reason = $reason;
} }
} }
class report_image extends Extension { class ReportImage extends Extension {
var $theme; var $theme;
public function receive_event($event) { public function receive_event($event) {
if(is_null($this->theme)) $this->theme = get_theme_object("report_image", "ReportImageTheme");
if(is_null($this->theme)) $this->theme = get_theme_object("report_image", "ReportImageTheme"); if(is_a($event, 'InitExtEvent')) {
if(is_a($event, 'InitExtEvent')) {
global $config; global $config;
if($config->get_int("ext_ReportImage_version") < 1) {
$config->set_default_bool('report_image_show_thumbs', true);
if($config->get_int("ext_report_image_version") < 1) {
$this->install(); $this->install();
} }
} }
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "ReportImage")) { if(is_a($event, 'PageRequestEvent') && ($event->page_name == "image_report")) {
global $user; global $user;
if($event->get_arg(0) == "add") { if($event->get_arg(0) == "add") {
if(isset($_POST['image_id']) && isset($_POST['reason_type']) && isset($_POST['reason'])) { if(isset($_POST['image_id']) && isset($_POST['reason'])) {
send_event(new AddReportedImageEvent($_POST['image_id'], $event->user->name, $_POST['reason_type'], $_POST['reason'])); $image_id = int_escape($_POST['image_id']);
global $page; send_event(new AddReportedImageEvent($image_id, $event->user->id, $_POST['reason']));
$page->set_mode("redirect"); $event->page->set_mode("redirect");
$page->set_redirect(make_link("post/view/".int_escape($_POST['image_id']))); $event->page->set_redirect(make_link("post/view/$image_id"));
}
} }
else if($event->get_arg(0) == "remove") { }
if(isset($_POST['id'])) { else if($event->get_arg(0) == "remove") {
if($event->user->is_admin()) { if(isset($_POST['id'])) {
send_event(new RemoveReportedImageEvent($_POST['id']));
global $page;
$page->set_mode("redirect");
$page->set_redirect(make_link("ReportImage/list"));
}
}
}
else if($event->get_arg(0) == "list") {
if($event->user->is_admin()) { if($event->user->is_admin()) {
global $page; send_event(new RemoveReportedImageEvent($_POST['id']));
$this->theme->display_reported_images($page, $this->get_reported_images()); $event->page->set_mode("redirect");
$event->page->set_redirect(make_link("image_report/list"));
} }
} }
}
else if($event->get_arg(0) == "list") {
if($event->user->is_admin()) {
$this->theme->display_reported_images($event->page, $this->get_reported_images());
}
}
} }
// if(is_a($event, 'AdminBuildingEvent')) {
// global $page;
// $this->theme->display_reported_images($page, $this->get_reported_images());
// }
if(is_a($event, 'AddReportedImageEvent')) { if(is_a($event, 'AddReportedImageEvent')) {
$this->add_reported_image($event->image_id, $event->reporter_name, $event->reason_type, $event->reason); global $database;
$database->Execute(
"INSERT INTO image_reports(image_id, reporter_id, reason)
VALUES (?, ?, ?)",
array($event->image_id, $event->reporter_id, $event->reason));
} }
if(is_a($event, 'RemoveReportedImageEvent')) { if(is_a($event, 'RemoveReportedImageEvent')) {
$this->remove_reported_image($event->id); global $database;
$database->Execute("DELETE FROM image_reports WHERE id = ?", array($event->id));
} }
if(is_a($event, 'DisplayingImageEvent')) { if(is_a($event, 'DisplayingImageEvent')) {
global $user; global $user;
global $config; global $config;
if(!$config->get_bool('report_image_anon') && $user->is_anonymous()) { if($config->get_bool('report_image_anon') || !$user->is_anonymous()) {
// Show nothing
} else {
$this->theme->display_image_banner($event->page, $event->image->id); $this->theme->display_image_banner($event->page, $event->image->id);
} }
} }
@ -101,65 +95,61 @@ class report_image extends Extension {
if(is_a($event, 'SetupBuildingEvent')) { if(is_a($event, 'SetupBuildingEvent')) {
$sb = new SetupBlock("Report Image Options"); $sb = new SetupBlock("Report Image Options");
$sb->add_bool_option("report_image_anon", "Allow anonymous image reporting: "); $sb->add_bool_option("report_image_anon", "Allow anonymous image reporting: ");
$sb->add_label("<br>"); $sb->add_bool_option("report_image_show_thumbs", "<br>Show thumbnails in admin panel: ");
$sb->add_bool_option("report_image_show_thumbs", "Show thumbnails in admin panel: ");
$event->panel->add_block($sb); $event->panel->add_block($sb);
} }
if(is_a($event, 'UserBlockBuildingEvent')) { if(is_a($event, 'UserBlockBuildingEvent')) {
if($event->user->is_admin()) { if($event->user->is_admin()) {
$event->add_link("Reported Images", make_link("ReportImage/list")); $event->add_link("Reported Images", make_link("image_report/list"));
} }
} }
if(is_a($event, 'ImageDeletionEvent')) {
global $database;
$database->Execute("DELETE FROM image_reports WHERE image_id = ?", array($event->image->id));
}
} }
protected function install() { protected function install() {
global $database; global $database;
global $config; global $config;
if($config->get_int("ext_ReportImage_version") < 1) { if($config->get_int("ext_report_image_version") < 1) {
$database->Execute("CREATE TABLE ReportImage ( $database->Execute("CREATE TABLE image_reports (
id int(11) NOT NULL auto_increment, id {$database->engine->auto_increment},
image_id int(11) default NULL, image_id INTEGER NOT NULL,
reporter_name varchar(32) default NULL, reporter_id INTEGER NOT NULL,
reason_type varchar(255) default NULL, reason TEXT NOT NULL
reason varchar(255) default NULL,
PRIMARY KEY (id)
)"); )");
$config->set_int("ext_ReportImage_version", 1); $config->set_int("ext_report_image_version", 1);
} }
} }
public function get_reported_images() {
// DB funness
public function get_reported_images() {
// FIXME: many
global $database; global $database;
$reportedimages = $database->get_all("SELECT * FROM ReportImage"); $all_reports = $database->get_all("
if($reportedimages) {return $reportedimages;} SELECT image_reports.*, users.name AS reporter_name
else {return array();} FROM image_reports
JOIN users ON reporter_id = users.id");
if(is_null($all_reports)) $all_reports = array();
$reports = array();
foreach($all_reports as $report) {
global $database;
$image_id = int_escape($report['image_id']);
$image = $database->get_image($image_id);
if(is_null($image)) {
send_event(new RemoveReportedImageEvent($report['id']));
continue;
}
$report['image'] = $database->get_image($image_id);
$reports[] = $report;
} }
public function get_reported_image($id) { return $reports;
global $database;
return $database->db->GetRow("SELECT * FROM ReportImage WHERE id = ?", array($id));
} }
public function add_reported_image($image_id, $reporter_name, $reason_type, $reason) {
global $database;
$database->Execute(
"INSERT INTO ReportImage (image_id, reporter_name, reason_type, reason) VALUES (?, ?, ?, ?)",
array($image_id, $reporter_name, $reason_type, $reason));
}
public function remove_reported_image($id) {
global $database;
$database->Execute("DELETE FROM ReportImage WHERE id = ?", array($id));
}
} }
add_event_listener(new report_image(), 29); // Not sure what I'm in before. add_event_listener(new ReportImage(), 29); // Not sure what I'm in before.
// ===== Changelog ===== // ===== Changelog =====
// * Version 0.3a / 0.3a_rc - 11/06/07 - I can no longer use the same theme.php file for both SVN and RCx. Sorry. // * Version 0.3a / 0.3a_rc - 11/06/07 - I can no longer use the same theme.php file for both SVN and RCx. Sorry.

View File

@ -1,37 +0,0 @@
/*
* Name: Report Images
* Author: ATravelingGeek (atg@atravelinggeek.com
* Link: http://atravelinggeek.com/
* License: GPLv2
* Description: Report images as dupes/illegal/etc
* Version 0.3a - See changelog in main.php
* November 06, 2007
*/
function validate_report()
{
if(document.ReportImage.reason_type.value=="Select a reason...") {
alert("Please select a reason!");
document.ReportImage.reason_type.focus();
return false;
}
if(document.ReportImage.reason.value == "Please enter a reason" || document.ReportImage.reason.value == '' || document.ReportImage.reason.value == "Please enter the Image ID") {
alert("Please enter a reason!");
document.ReportImage.reason.focus();
document.ReportImage.reason.select();
return false;
}
}
function change_reason()
{
if(document.ReportImage.reason_type.value == "Duplicate"){
document.ReportImage.reason.value = "Enter the Image ID";
} else {
document.ReportImage.reason.value = "Please enter a reason";
}
}

View File

@ -8,47 +8,58 @@
* Description: Report images as dupes/illegal/etc * Description: Report images as dupes/illegal/etc
* Version 0.3a - See changelog in main.php * Version 0.3a - See changelog in main.php
* November 06, 2007 * November 06, 2007
*
* NOTE: This is for Shimmie2 SVN Trunk. Use the other theme.php.use... for Shimmie2 RCx.
*
*/ */
class ReportImageTheme extends Themelet { class ReportImageTheme extends Themelet {
public function display_reported_images($page, $reportedimages) { public function display_reported_images($page, $reports) {
$h_reportedimages = ""; global $config;
foreach($reportedimages as $reportedimage) {
// If the reason is 'Duplicate' make the 'reason' field a link to the reported image $h_reportedimages = "";
if ($reportedimage['reason_type'] == "Duplicate") foreach($reports as $report) {
{ $image = $report['image'];
$reason = "<a href=\"".make_link("post/view/{$reportedimage['reason']}")."\">".$reportedimage['reason']."</a>"; $h_reason = html_escape($report['reason']);
$reason .= $this->make_thumbnail_html($reportedimage['reason']);
} else { if($config->get_bool('report_image_show_thumbs')) {
$reason = $reportedimage['reason']; $image_link = $this->build_thumb_html($image);
}
else {
$image_link = "<a href=\"".make_link("post/view/{$image->id}")."\">{$image->id}</a>";
} }
$image_link = "<a href=\"".make_link("post/view/{$reportedimage['image_id']}")."\">".$reportedimage['image_id']."</a>"; $reporter_name = html_escape($report['reporter_name']);
$image_link .= $this->make_thumbnail_html($reportedimage['image_id']); $userlink = "<a href='".make_link("user/$reporter_name")."'>$reporter_name</a>";
$userlink = "<a href='".make_link("user/{$reportedimage['reporter_name']}")."'>{$reportedimage['reporter_name']}</a>";
$h_reportedimages .= " global $user;
<tr> $iabbe = new ImageAdminBlockBuildingEvent($image, $user);
<td>{$image_link}</td> send_event($iabbe);
<td>{$userlink}</td> ksort($iabbe->parts);
<td>{$reportedimage['reason_type']}</td> $actions = join("<br>", $iabbe->parts);
<td>{$reason}</td>
<td> $h_reportedimages .= "
<form action='".make_link("ReportImage/remove")."' method='POST'> <tr>
<input type='hidden' name='id' value='{$reportedimage['id']}'> <td>{$image_link}</td>
<input type='submit' value='Remove'> <td>Report by $userlink: $h_reason</td>
</form> <td class='formstretch'>
</td> <form action='".make_link("image_report/remove")."' method='POST'>
</tr> <input type='hidden' name='id' value='{$report['id']}'>
"; <input type='submit' value='Remove Report'>
</form>
<br>$actions
</td>
</tr>
";
} }
$thumb_width = $config->get_int("thumb_width");
$html = " $html = "
<style>
.formstretch FORM INPUT {
width: 100%;
}
</style>
<table border='1'> <table border='1'>
<thead><td>Image</td><td>Reporter</td><td>Reason Type</td><td>Reason / Image ID</td><td>Action</td></thead> <thead><td width='$thumb_width'>Image</td><td>Reason</td><td width='128'>Action</td></thead>
$h_reportedimages $h_reportedimages
</table> </table>
"; ";
@ -60,44 +71,18 @@ class ReportImageTheme extends Themelet {
} }
public function make_thumbnail_html($image_id) {
global $config;
global $database;
if($config->get_bool('report_image_show_thumbs')) {
$image_obj_reported = $database->get_image($image_id);
if($image_obj_reported) {
return "<br>" . $this->build_thumb_html($image_obj_reported);
}
else {
return "<br>Image not found -- bug!";
}
}
}
public function display_image_banner($page, $image) { public function display_image_banner($page, $image) {
global $config;
global $config;
$page->add_header("<script type='text/javascript' src='".get_base_href()."/ext/report_image/report_image.js'></script>");
$i_image = int_escape($image); $i_image = int_escape($image);
$html = " $html = "
<form name='ReportImage' action='".make_link("ReportImage/add")."' onsubmit='return validate_report()' method='POST'> <form action='".make_link("image_report/add")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image'> <input type='hidden' name='image_id' value='$i_image'>
<select onchange='change_reason()' name='reason_type'> <input type='field' name='reason' value='Please enter a reason' onclick='this.value=\"\";'>
<option style='font-weight:bold' selected>Select a reason...</option>
<option value='Other'>Other</option>
<option value='Violates Rules'>Violates Rules</option>
<option value='Illegal'>Illegal</option>
<option value='Duplicate'>Duplicate</option>
<input type='field' name='reason' value='Please enter a reason' onclick='document.ReportImage.reason.select()'>
</select>
<input type='submit' value='Report'> <input type='submit' value='Report'>
</form> </form>
"; ";
$page->add_block(new Block("Report Image", $html, "left")); $page->add_block(new Block("Report Image", $html, "left"));
} }
} }
?> ?>