2007-10-24 21:45:38 +00:00
< ? php
2009-01-04 19:18:37 +00:00
2019-05-28 16:59:38 +00:00
class RemoveReportedImageEvent extends Event
{
/** @var int */
public $id ;
public function __construct ( int $id )
{
$this -> id = $id ;
}
2007-10-24 21:45:38 +00:00
}
2019-05-28 16:59:38 +00:00
class AddReportedImageEvent extends Event
{
/** @var ImageReport */
public $report ;
2016-07-30 14:04:34 +00:00
2019-05-28 16:59:38 +00:00
public function __construct ( ImageReport $report )
{
$this -> report = $report ;
}
2016-07-30 14:04:34 +00:00
}
2019-05-28 16:59:38 +00:00
class ImageReport
{
/** @var int */
public $user_id ;
/** @var int */
public $image_id ;
/** @var string */
public $reason ;
public function __construct ( int $image_id , int $user_id , string $reason )
{
$this -> image_id = $image_id ;
$this -> user_id = $user_id ;
$this -> reason = $reason ;
}
2007-10-24 21:45:38 +00:00
}
2019-05-28 16:59:38 +00:00
class ReportImage extends Extension
{
public function onPageRequest ( PageRequestEvent $event )
{
global $page , $user ;
if ( $event -> page_matches ( " image_report " )) {
if ( $event -> get_arg ( 0 ) == " add " ) {
if ( ! empty ( $_POST [ 'image_id' ]) && ! empty ( $_POST [ 'reason' ])) {
$image_id = int_escape ( $_POST [ 'image_id' ]);
send_event ( new AddReportedImageEvent ( new ImageReport ( $image_id , $user -> id , $_POST [ 'reason' ])));
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ( " post/view/ $image_id " ));
} else {
$this -> theme -> display_error ( 500 , " Missing input " , " Missing image ID or report reason " );
}
} elseif ( $event -> get_arg ( 0 ) == " remove " ) {
if ( ! empty ( $_POST [ 'id' ])) {
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
send_event ( new RemoveReportedImageEvent ( $_POST [ 'id' ]));
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ( " image_report/list " ));
}
} else {
$this -> theme -> display_error ( 500 , " Missing input " , " Missing image ID " );
}
} elseif ( $event -> get_arg ( 0 ) == " remove_reports_by " && $user -> check_auth_token ()) {
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
$this -> delete_reports_by ( int_escape ( $_POST [ 'user_id' ]));
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ());
}
} elseif ( $event -> get_arg ( 0 ) == " list " ) {
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
$this -> theme -> display_reported_images ( $page , $this -> get_reported_images ());
}
}
}
}
public function onAddReportedImage ( AddReportedImageEvent $event )
{
2019-10-02 09:49:32 +00:00
global $cache , $database ;
2019-05-28 16:59:38 +00:00
log_info ( " report_image " , " Adding report of Image # { $event -> report -> image_id } with reason ' { $event -> report -> reason } ' " , null , [ " image_id " => $event -> report -> image_id ]);
$database -> Execute (
" INSERT INTO image_reports(image_id, reporter_id, reason)
2012-02-08 11:24:25 +00:00
VALUES ( ? , ? , ? ) " ,
2019-05-28 16:59:38 +00:00
[ $event -> report -> image_id , $event -> report -> user_id , $event -> report -> reason ]
);
2019-10-02 09:49:32 +00:00
$cache -> delete ( " image-report-count " );
2019-05-28 16:59:38 +00:00
}
public function onRemoveReportedImage ( RemoveReportedImageEvent $event )
{
2019-10-02 09:49:32 +00:00
global $cache , $database ;
2019-05-28 16:59:38 +00:00
$database -> Execute ( " DELETE FROM image_reports WHERE id = ? " , [ $event -> id ]);
2019-10-02 09:49:32 +00:00
$cache -> delete ( " image-report-count " );
2019-05-28 16:59:38 +00:00
}
public function onUserPageBuilding ( UserPageBuildingEvent $event )
{
global $user ;
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
$this -> theme -> get_nuller ( $event -> display_user );
}
}
public function onDisplayingImage ( DisplayingImageEvent $event )
{
global $user ;
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: CREATE_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
$reps = $this -> get_reports ( $event -> image );
$this -> theme -> display_image_banner ( $event -> image , $reps );
}
}
2019-08-02 19:54:48 +00:00
public function onPageSubNavBuilding ( PageSubNavBuildingEvent $event )
{
global $user ;
2019-09-29 13:30:55 +00:00
if ( $event -> parent === " system " ) {
2019-08-02 19:54:48 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
$count = $this -> count_reported_images ();
$h_count = $count > 0 ? " ( $count ) " : " " ;
$event -> add_nav_link ( " image_report " , new Link ( 'image_report/list' ), " Reported Images $h_count " );
}
}
}
2019-05-28 16:59:38 +00:00
public function onUserBlockBuilding ( UserBlockBuildingEvent $event )
{
global $user ;
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: VIEW_IMAGE_REPORT )) {
2019-05-28 16:59:38 +00:00
$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 )
{
2019-10-02 09:49:32 +00:00
global $cache , $database ;
2019-05-28 16:59:38 +00:00
$database -> Execute ( " DELETE FROM image_reports WHERE image_id = ? " , [ $event -> image -> id ]);
2019-10-02 09:49:32 +00:00
$cache -> delete ( " image-report-count " );
2019-05-28 16:59:38 +00:00
}
public function onUserDeletion ( UserDeletionEvent $event )
{
$this -> delete_reports_by ( $event -> id );
}
public function onSetupBuilding ( SetupBuildingEvent $event )
{
$sb = new SetupBlock ( " Image Reports " );
$opts = [
" 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 );
}
public function delete_reports_by ( int $user_id )
{
2019-10-02 09:49:32 +00:00
global $cache , $database ;
2019-05-28 16:59:38 +00:00
$database -> execute ( " DELETE FROM image_reports WHERE reporter_id=? " , [ $user_id ]);
2019-10-02 09:49:32 +00:00
$cache -> delete ( " image-report-count " );
2019-05-28 16:59:38 +00:00
}
2019-11-03 17:19:37 +00:00
public function onDatabaseUpgrade ( DatabaseUpgradeEvent $event )
2019-05-28 16:59:38 +00:00
{
global $database , $config ;
if ( $config -> get_int ( " ext_report_image_version " ) < 1 ) {
$database -> create_table ( " image_reports " , "
2009-01-22 12:05:55 +00:00
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
" );
2019-05-28 16:59:38 +00:00
$config -> set_int ( " ext_report_image_version " , 1 );
}
}
/**
* #return ImageReport[]
*/
public function get_reports ( Image $image ) : array
{
global $database ;
$rows = $database -> get_all ( "
2016-07-30 14:04:34 +00:00
SELECT *
2012-02-12 06:41:10 +00:00
FROM image_reports
WHERE image_reports . image_id = : image_id
2019-05-28 16:59:38 +00:00
" , [ " image_id " => $image->id ]);
$reps = [];
foreach ( $rows as $row ) {
$reps [] = new ImageReport ( $row [ " image_id " ], $row [ " reporter_id " ], $row [ " reason " ]);
}
return $reps ;
}
public function get_reported_images () : array
{
global $database ;
$all_reports = $database -> get_all ( "
2008-08-12 02:37:48 +00:00
SELECT image_reports .* , users . name AS reporter_name
FROM image_reports
2018-11-10 12:03:05 +00:00
JOIN users ON reporter_id = users . id
ORDER BY image_reports . id DESC " );
2019-05-28 16:59:38 +00:00
if ( is_null ( $all_reports )) {
$all_reports = [];
}
$reports = [];
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 ;
}
public function count_reported_images () : int
{
2019-10-02 09:49:32 +00:00
global $cache , $database ;
2019-05-28 16:59:38 +00:00
2019-10-02 09:49:32 +00:00
$count = $cache -> get ( " image-report-count " );
2019-05-28 16:59:38 +00:00
if ( is_null ( $count ) || $count === false ) {
$count = $database -> get_one ( " SELECT count(*) FROM image_reports " );
2019-10-02 09:49:32 +00:00
$cache -> set ( " image-report-count " , $count , 600 );
2019-05-28 16:59:38 +00:00
}
return $count ;
}
2007-10-24 21:45:38 +00:00
}