pages.php: use ReportQueries

This commit is contained in:
Zankaria 2024-11-27 17:25:33 +01:00
parent e0807a1811
commit c82a669934

View File

@ -3,6 +3,7 @@
* Copyright (c) 2010-2013 Tinyboard Development Group * Copyright (c) 2010-2013 Tinyboard Development Group
*/ */
use Vichan\Context; use Vichan\Context;
use Vichan\Data\ReportQueries;
use Vichan\Functions\Format; use Vichan\Functions\Format;
use Vichan\Functions\Net; use Vichan\Functions\Net;
use Vichan\Data\Driver\CacheDriver; use Vichan\Data\Driver\CacheDriver;
@ -106,6 +107,7 @@ function mod_logout(Context $ctx) {
function mod_dashboard(Context $ctx) { function mod_dashboard(Context $ctx) {
global $mod; global $mod;
$config = $ctx->get('config'); $config = $ctx->get('config');
$report_queries = $ctx->get(ReportQueries::class);
$args = []; $args = [];
@ -131,8 +133,7 @@ function mod_dashboard(Context $ctx) {
$ctx->get(CacheDriver::class)->set('pm_unreadcount_' . $mod['id'], $args['unread_pms']); $ctx->get(CacheDriver::class)->set('pm_unreadcount_' . $mod['id'], $args['unread_pms']);
} }
$query = query('SELECT COUNT(*) FROM ``reports``') or error(db_error($query)); $args['reports'] = $report_queries->getCount();
$args['reports'] = $query->fetchColumn();
$query = query('SELECT COUNT(*) FROM ``ban_appeals``') or error(db_error($query)); $query = query('SELECT COUNT(*) FROM ``ban_appeals``') or error(db_error($query));
$args['appeals'] = $query->fetchColumn(); $args['appeals'] = $query->fetchColumn();
@ -2444,43 +2445,22 @@ function mod_reports(Context $ctx) {
if (!hasPermission($config['mod']['reports'])) if (!hasPermission($config['mod']['reports']))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$query = prepare("SELECT * FROM ``reports`` ORDER BY `time` DESC LIMIT :limit"); $reports_limit = $config['mod']['recent_reports'];
$query->bindValue(':limit', $config['mod']['recent_reports'], PDO::PARAM_INT); $report_queries = $ctx->get(ReportQueries::class);
$query->execute() or error(db_error($query)); $report_rows = $report_queries->getReportsWithPosts($reports_limit);
$reports = $query->fetchAll(PDO::FETCH_ASSOC);
$report_queries = []; if (\count($report_rows) > $reports_limit) {
foreach ($reports as $report) { \array_pop($report_rows);
if (!isset($report_queries[$report['board']])) $has_extra = true;
$report_queries[$report['board']] = []; } else {
$report_queries[$report['board']][] = $report['post']; $has_extra = false;
} }
$report_posts = [];
foreach ($report_queries as $board => $posts) {
$report_posts[$board] = [];
$query = query(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error());
while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
$report_posts[$board][$post['id']] = $post;
}
}
$count = 0;
$body = ''; $body = '';
foreach ($reports as $report) { foreach ($report_rows as $report) {
if (!isset($report_posts[$report['board']][$report['post']])) {
// // Invalid report (post has since been deleted)
$query = prepare("DELETE FROM ``reports`` WHERE `post` = :id AND `board` = :board");
$query->bindValue(':id', $report['post'], PDO::PARAM_INT);
$query->bindValue(':board', $report['board']);
$query->execute() or error(db_error($query));
continue;
}
openBoard($report['board']); openBoard($report['board']);
$post = &$report_posts[$report['board']][$report['post']]; $post = $report['post_data'];
if (!$post['thread']) { if (!$post['thread']) {
// Still need to fix this: // Still need to fix this:
@ -2489,16 +2469,16 @@ function mod_reports(Context $ctx) {
$po = new Post($post, '?/', $mod); $po = new Post($post, '?/', $mod);
} }
// a little messy and inefficient // A little messy and inefficient.
$append_html = Element($config['file_mod_report'], array( $append_html = Element($config['file_mod_report'], [
'report' => $report, 'report' => $report,
'config' => $config, 'config' => $config,
'mod' => $mod, 'mod' => $mod,
'pm' => create_pm_header(), 'pm' => create_pm_header(),
'token' => make_secure_link_token('reports/' . $report['id'] . '/dismiss'), 'token' => make_secure_link_token('reports/' . $report['id'] . '/dismiss'),
'token_all' => make_secure_link_token('reports/' . $report['id'] . '/dismiss&all'), 'token_all' => make_secure_link_token('reports/' . $report['id'] . '/dismiss&all'),
'token_post' => make_secure_link_token('reports/'. $report['id'] . '/dismiss&post'), 'token_post' => make_secure_link_token('reports/'. $report['id'] . '/dismiss&post')
)); ]);
// Bug fix for https://github.com/savetheinternet/Tinyboard/issues/21 // Bug fix for https://github.com/savetheinternet/Tinyboard/issues/21
$po->body = truncate($po->body, $po->link(), $config['body_truncate'] - substr_count($append_html, '<br>')); $po->body = truncate($po->body, $po->link(), $config['body_truncate'] - substr_count($append_html, '<br>'));
@ -2513,14 +2493,16 @@ function mod_reports(Context $ctx) {
$body .= $po->build(true) . '<hr>'; $body .= $po->build(true) . '<hr>';
if (isset($__old_body_truncate_char)) if (isset($__old_body_truncate_char)) {
$config['body_truncate_char'] = $__old_body_truncate_char; $config['body_truncate_char'] = $__old_body_truncate_char;
}
$count++;
} }
$count = \count($report_rows);
$header_count = $has_extra ? "{$count}+" : (string)$count;
mod_page( mod_page(
sprintf('%s (%d)', _('Report queue'), $count), sprintf('%s (%s)', _('Report queue'), $header_count),
$config['file_mod_reports'], $config['file_mod_reports'],
[ [
'reports' => $body, 'reports' => $body,
@ -2533,31 +2515,29 @@ function mod_reports(Context $ctx) {
function mod_report_dismiss(Context $ctx, $id, $action) { function mod_report_dismiss(Context $ctx, $id, $action) {
$config = $ctx->get('config'); $config = $ctx->get('config');
$query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id"); $report_queries = $ctx->get(ReportQueries::class);
$query->bindValue(':id', $id); $report = $report_queries->getReportById($id);
$query->execute() or error(db_error($query));
if ($report = $query->fetch(PDO::FETCH_ASSOC)) { if ($report === null) {
$ip = $report['ip'];
$board = $report['board'];
$post = $report['post'];
} else
error($config['error']['404']); error($config['error']['404']);
}
$ip = $report['ip'];
$board = $report['board'];
$post = $report['post'];
switch($action){ switch($action){
case '&post': case '&post':
if (!hasPermission($config['mod']['report_dismiss_post'], $board)) if (!hasPermission($config['mod']['report_dismiss_post'], $board))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$query = prepare("DELETE FROM ``reports`` WHERE `post` = :post"); $report_queries->deleteByPost($post);
$query->bindValue(':post', $post);
modLog("Dismissed all reports for post #{$id}", $board); modLog("Dismissed all reports for post #{$id}", $board);
break; break;
case '&all': case '&all':
if (!hasPermission($config['mod']['report_dismiss_ip'], $board)) if (!hasPermission($config['mod']['report_dismiss_ip'], $board))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$query = prepare("DELETE FROM ``reports`` WHERE `ip` = :ip"); $report_queries->deleteByIp($ip);
$query->bindValue(':ip', $ip);
$cip = cloak_ip($ip); $cip = cloak_ip($ip);
modLog("Dismissed all reports by <a href=\"?/IP/$cip\">$cip</a>"); modLog("Dismissed all reports by <a href=\"?/IP/$cip\">$cip</a>");
break; break;
@ -2566,12 +2546,10 @@ function mod_report_dismiss(Context $ctx, $id, $action) {
if (!hasPermission($config['mod']['report_dismiss'], $board)) if (!hasPermission($config['mod']['report_dismiss'], $board))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$query = prepare("DELETE FROM ``reports`` WHERE `id` = :id"); $report_queries->deleteById($id);
$query->bindValue(':id', $id);
modLog("Dismissed a report for post #{$id}", $board); modLog("Dismissed a report for post #{$id}", $board);
break; break;
} }
$query->execute() or error(db_error($query));
header('Location: ?/reports', true, $config['redirect_http']); header('Location: ?/reports', true, $config['redirect_http']);
} }