2007-10-24 21:45:38 +00:00
< ? php
2007-11-06 20:59:15 +00:00
/*
2007-10-24 21:45:38 +00:00
* Name : Report Images
2007-12-04 18:20:46 +00:00
* Author : ATravelingGeek < atg @ atravelinggeek . com >
2007-10-24 21:45:38 +00:00
* Link : http :// atravelinggeek . com /
* License : GPLv2
* Description : Report images as dupes / illegal / etc
2007-11-06 20:59:15 +00:00
* Version 0.3 a - See changelog in main . php
2007-11-06 20:17:14 +00:00
* November 06 , 2007
2007-10-24 21:45:38 +00:00
*/
2009-01-04 19:18:37 +00:00
2007-10-24 21:45:38 +00:00
class RemoveReportedImageEvent extends Event {
2014-04-28 07:26:35 +00:00
/** @var int */
public $id ;
2007-10-24 21:45:38 +00:00
2014-04-28 07:26:35 +00:00
/**
* @ param int $id
*/
2014-03-22 09:00:59 +00:00
public function __construct ( $id ) {
2007-10-24 21:45:38 +00:00
$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 ;
/**
* @ param ImageReport $report
*/
public function __construct ( $report ) {
$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 ;
2016-07-30 14:04:34 +00:00
public function __construct ( $image_id , $user_id , $reason ) {
2007-10-24 21:45:38 +00:00
$this -> image_id = $image_id ;
2016-07-30 14:04:34 +00:00
$this -> user_id = $user_id ;
2007-10-24 21:45:38 +00:00
$this -> reason = $reason ;
}
}
2012-02-08 12:07:01 +00:00
class ReportImage extends Extension {
2012-02-08 11:24:25 +00:00
public function onInitExt ( InitExtEvent $event ) {
global $config ;
2008-08-12 02:37:48 +00:00
2012-02-08 11:24:25 +00:00
if ( $config -> get_int ( " ext_report_image_version " ) < 1 ) {
$this -> install ();
2007-10-24 21:45:38 +00:00
}
2012-02-08 11:24:25 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-08 11:24:25 +00:00
public function onPageRequest ( PageRequestEvent $event ) {
global $page , $user ;
if ( $event -> page_matches ( " image_report " )) {
2008-08-12 02:37:48 +00:00
if ( $event -> get_arg ( 0 ) == " add " ) {
2012-12-30 16:00:55 +00:00
if ( ! empty ( $_POST [ 'image_id' ]) && ! empty ( $_POST [ 'reason' ])) {
2008-08-12 02:37:48 +00:00
$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' ])));
2009-05-11 14:04:33 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " post/view/ $image_id " ));
2007-11-06 20:17:14 +00:00
}
2012-12-30 16:00:55 +00:00
else {
$this -> theme -> display_error ( 500 , " Missing input " , " Missing image ID or report reason " );
}
2008-08-12 02:37:48 +00:00
}
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 " )) {
2008-08-12 02:37:48 +00:00
send_event ( new RemoveReportedImageEvent ( $_POST [ 'id' ]));
2009-05-11 14:04:33 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " image_report/list " ));
2007-10-24 21:45:38 +00:00
}
}
2012-12-30 16:00:55 +00:00
else {
$this -> theme -> display_error ( 500 , " Missing input " , " Missing image ID " );
}
2008-08-12 02:37:48 +00:00
}
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 ());
}
}
2008-08-12 02:37:48 +00:00
else if ( $event -> get_arg ( 0 ) == " list " ) {
2012-02-12 07:47:53 +00:00
if ( $user -> can ( " view_image_report " )) {
2009-05-11 14:04:33 +00:00
$this -> theme -> display_reported_images ( $page , $this -> get_reported_images ());
2008-08-12 02:37:48 +00:00
}
}
2007-10-24 21:45:38 +00:00
}
2012-02-08 11:24:25 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-08 11:24:25 +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 ));
2012-02-08 11:24:25 +00:00
$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 ));
2012-06-24 00:57:06 +00:00
$database -> cache -> delete ( " image-report-count " );
2012-02-08 11:24:25 +00:00
}
2007-10-24 21:45:38 +00:00
2012-02-08 11:24:25 +00:00
public function onRemoveReportedImage ( RemoveReportedImageEvent $event ) {
global $database ;
$database -> Execute ( " DELETE FROM image_reports WHERE id = ? " , array ( $event -> id ));
2012-06-24 00:57:06 +00:00
$database -> cache -> delete ( " image-report-count " );
2012-02-08 11:24:25 +00:00
}
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 );
}
}
2012-02-08 11:24:25 +00:00
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 );
2007-10-24 21:45:38 +00:00
}
2012-02-08 11:24:25 +00:00
}
2007-10-24 21:45:38 +00:00
2012-02-08 11:24:25 +00:00
public function onUserBlockBuilding ( UserBlockBuildingEvent $event ) {
global $user ;
2012-02-12 07:47:53 +00:00
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 " ));
2007-11-06 20:17:14 +00:00
}
2012-02-08 11:24:25 +00:00
}
2008-08-12 02:37:48 +00:00
2012-02-08 11:24:25 +00:00
public function onImageDeletion ( ImageDeletionEvent $event ) {
global $database ;
$database -> Execute ( " DELETE FROM image_reports WHERE image_id = ? " , array ( $event -> image -> id ));
2012-06-24 00:57:06 +00:00
$database -> cache -> delete ( " image-report-count " );
2007-10-24 21:45:38 +00:00
}
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 " );
}
2007-10-24 21:45:38 +00:00
protected function install () {
2014-04-28 07:26:35 +00:00
global $database , $config ;
2008-08-12 02:37:48 +00:00
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 ,
2009-01-22 13:03:51 +00:00
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
" );
2008-08-12 02:37:48 +00:00
$config -> set_int ( " ext_report_image_version " , 1 );
2007-10-24 21:45:38 +00:00
}
}
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
*/
2008-08-12 02:37:48 +00:00
public function get_reported_images () {
2014-04-28 07:26:35 +00:00
global $database ;
2008-08-12 02:37:48 +00:00
$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
2008-08-12 02:37:48 +00:00
$reports = array ();
foreach ( $all_reports as $report ) {
$image_id = int_escape ( $report [ 'image_id' ]);
2009-05-11 14:04:33 +00:00
$image = Image :: by_id ( $image_id );
2008-08-12 02:37:48 +00:00
if ( is_null ( $image )) {
send_event ( new RemoveReportedImageEvent ( $report [ 'id' ]));
continue ;
}
2008-08-26 09:11:40 +00:00
$report [ 'image' ] = $image ;
2008-08-12 02:37:48 +00:00
$reports [] = $report ;
}
2007-10-24 21:45:38 +00:00
2008-08-12 02:37:48 +00:00
return $reports ;
2007-10-24 21:45:38 +00:00
}
2012-02-12 07:47:53 +00:00
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
*/
2012-02-12 07:47:53 +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 " );
2012-06-24 00:57:06 +00:00
$database -> cache -> set ( " image-report-count " , $count , 600 );
2012-06-23 22:28:38 +00:00
}
return $count ;
2012-02-12 07:47:53 +00:00
}
2007-10-24 21:45:38 +00:00
}