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/report_image/main.php

245 lines
6.6 KiB
PHP
Raw Normal View History

<?php
/*
* 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
*/
2009-01-04 19:18:37 +00:00
class RemoveReportedImageEvent extends Event {
2014-04-28 07:26:35 +00:00
/** @var int */
public $id;
2017-09-19 17:55:43 +00:00
public function __construct(int $id) {
$this->id = $id;
}
}
2009-01-04 19:18:37 +00:00
class AddReportedImageEvent extends Event {
2016-07-30 14:04:34 +00:00
/** @var ImageReport */
public $report;
2017-09-19 17:55:43 +00:00
public function __construct(ImageReport $report) {
2016-07-30 14:04:34 +00:00
$this->report = $report;
}
}
class ImageReport {
2014-04-28 07:26:35 +00:00
/** @var int */
2016-07-30 14:04:34 +00:00
public $user_id;
2014-04-28 07:26:35 +00:00
/** @var int */
public $image_id;
/** @var string */
public $reason;
2017-09-19 17:55:43 +00:00
public function __construct(int $image_id, int $user_id, string $reason) {
$this->image_id = $image_id;
2016-07-30 14:04:34 +00:00
$this->user_id = $user_id;
$this->reason = $reason;
}
}
class ReportImage extends Extension {
public function onInitExt(InitExtEvent $event) {
global $config;
if($config->get_int("ext_report_image_version") < 1) {
$this->install();
}
}
2009-01-04 19:18:37 +00:00
public function onPageRequest(PageRequestEvent $event) {
global $page, $user;
if($event->page_matches("image_report")) {
if($event->get_arg(0) == "add") {
2012-12-30 16:00:55 +00:00
if(!empty($_POST['image_id']) && !empty($_POST['reason'])) {
$image_id = int_escape($_POST['image_id']);
2016-07-30 14:04:34 +00:00
send_event(new AddReportedImageEvent(new ImageReport($image_id, $user->id, $_POST['reason'])));
$page->set_mode("redirect");
$page->set_redirect(make_link("post/view/$image_id"));
}
2012-12-30 16:00:55 +00:00
else {
$this->theme->display_error(500, "Missing input", "Missing image ID or report reason");
}
}
else if($event->get_arg(0) == "remove") {
2012-12-30 16:00:55 +00:00
if(!empty($_POST['id'])) {
2012-02-12 13:58:33 +00:00
if($user->can("view_image_report")) {
send_event(new RemoveReportedImageEvent($_POST['id']));
$page->set_mode("redirect");
$page->set_redirect(make_link("image_report/list"));
}
}
2012-12-30 16:00:55 +00:00
else {
$this->theme->display_error(500, "Missing input", "Missing image ID");
}
}
2015-02-08 04:27:54 +00:00
else if($event->get_arg(0) == "remove_reports_by" && $user->check_auth_token()) {
if($user->can("view_image_report")) {
$this->delete_reports_by(int_escape($_POST['user_id']));
$page->set_mode("redirect");
$page->set_redirect(make_link());
}
}
else if($event->get_arg(0) == "list") {
if($user->can("view_image_report")) {
$this->theme->display_reported_images($page, $this->get_reported_images());
}
}
}
}
2009-01-04 19:18:37 +00:00
public function onAddReportedImage(AddReportedImageEvent $event) {
global $database;
2016-07-30 14:04:34 +00:00
log_info("report_image", "Adding report of Image #{$event->report->image_id} with reason '{$event->report->reason}'", false, array("image_id" => $event->report->image_id));
$database->Execute(
"INSERT INTO image_reports(image_id, reporter_id, reason)
VALUES (?, ?, ?)",
2016-07-30 14:04:34 +00:00
array($event->report->image_id, $event->report->user_id, $event->report->reason));
$database->cache->delete("image-report-count");
}
public function onRemoveReportedImage(RemoveReportedImageEvent $event) {
global $database;
$database->Execute("DELETE FROM image_reports WHERE id = ?", array($event->id));
$database->cache->delete("image-report-count");
}
2009-01-04 19:18:37 +00:00
2015-02-08 04:27:54 +00:00
public function onUserPageBuilding(UserPageBuildingEvent $event) {
2015-09-12 10:43:28 +00:00
global $user;
2015-02-08 04:27:54 +00:00
if($user->can("view_image_report")) {
$this->theme->get_nuller($event->display_user);
}
}
public function onDisplayingImage(DisplayingImageEvent $event) {
2015-09-12 10:43:28 +00:00
global $user;
2012-03-14 19:24:10 +00:00
if($user->can('create_image_report')) {
2016-07-30 14:04:34 +00:00
$reps = $this->get_reports($event->image);
2012-02-12 06:41:10 +00:00
$this->theme->display_image_banner($event->image, $reps);
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->can("view_image_report")) {
$count = $this->count_reported_images();
$h_count = $count > 0 ? " ($count)" : "";
$event->add_link("Reported Images$h_count", make_link("image_report/list"));
}
}
public function onImageDeletion(ImageDeletionEvent $event) {
global $database;
$database->Execute("DELETE FROM image_reports WHERE image_id = ?", array($event->image->id));
$database->cache->delete("image-report-count");
}
2009-01-04 19:18:37 +00:00
2015-02-08 04:27:54 +00:00
public function onUserDeletion(UserDeletionEvent $event) {
$this->delete_reports_by($event->id);
}
2016-07-30 14:04:34 +00:00
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Image Reports");
$opts = array(
"Reporter Only" => "user",
"Reason Only" => "reason",
"Both" => "both",
"None" => "none",
);
$sb->add_choice_option("report_image_publicity", $opts, "Show publicly: ");
$event->panel->add_block($sb);
}
2015-09-27 11:38:48 +00:00
/**
* @param int $user_id
*/
public function delete_reports_by($user_id) {
2015-02-08 04:27:54 +00:00
global $database;
$database->execute("DELETE FROM image_reports WHERE reporter_id=?", array($user_id));
$database->cache->delete("image-report-count");
}
protected function install() {
2014-04-28 07:26:35 +00:00
global $database, $config;
if($config->get_int("ext_report_image_version") < 1) {
2009-01-22 12:05:55 +00:00
$database->create_table("image_reports", "
id SCORE_AIPK,
image_id INTEGER NOT NULL,
reporter_id INTEGER NOT NULL,
reason TEXT NOT NULL,
FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
FOREIGN KEY (reporter_id) REFERENCES users(id) ON DELETE CASCADE
2009-01-22 12:05:55 +00:00
");
$config->set_int("ext_report_image_version", 1);
}
}
2014-04-28 22:22:57 +00:00
/**
* @param Image $image
2016-07-30 14:04:34 +00:00
* @return ImageReport[]
2014-04-28 22:22:57 +00:00
*/
2016-07-30 14:04:34 +00:00
public function get_reports(Image $image) {
2012-02-12 06:41:10 +00:00
global $database;
2014-04-28 07:26:35 +00:00
2016-07-30 14:04:34 +00:00
$rows = $database->get_all("
SELECT *
2012-02-12 06:41:10 +00:00
FROM image_reports
WHERE image_reports.image_id = :image_id
", array("image_id" => $image->id));
2016-07-30 14:04:34 +00:00
$reps = array();
foreach($rows as $row) {
$reps[] = new ImageReport($row["image_id"], $row["reporter_id"], $row["reason"]);
}
return $reps;
2012-02-12 06:41:10 +00:00
}
2014-04-28 22:22:57 +00:00
/**
* @return array
*/
public function get_reported_images() {
2014-04-28 07:26:35 +00:00
global $database;
$all_reports = $database->get_all("
SELECT image_reports.*, users.name AS reporter_name
FROM image_reports
JOIN users ON reporter_id = users.id");
if(is_null($all_reports)) $all_reports = array();
2009-01-04 19:18:37 +00:00
$reports = array();
foreach($all_reports as $report) {
$image_id = int_escape($report['image_id']);
$image = Image::by_id($image_id);
if(is_null($image)) {
send_event(new RemoveReportedImageEvent($report['id']));
continue;
}
$report['image'] = $image;
$reports[] = $report;
}
return $reports;
}
2014-04-28 22:22:57 +00:00
/**
2015-09-27 11:38:48 +00:00
* @return int
2014-04-28 22:22:57 +00:00
*/
public function count_reported_images() {
global $database;
2012-06-23 22:28:38 +00:00
$count = $database->cache->get("image-report-count");
if(is_null($count) || $count === false) {
$count = $database->get_one("SELECT count(*) FROM image_reports");
$database->cache->set("image-report-count", $count, 600);
2012-06-23 22:28:38 +00:00
}
return $count;
}
}