From c82a669934a66cce21957ef29b07cea0db1e9891 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 27 Nov 2024 17:25:33 +0100 Subject: [PATCH] pages.php: use ReportQueries --- inc/mod/pages.php | 90 ++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 8a549481..c51435fb 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -3,6 +3,7 @@ * Copyright (c) 2010-2013 Tinyboard Development Group */ use Vichan\Context; +use Vichan\Data\ReportQueries; use Vichan\Functions\Format; use Vichan\Functions\Net; use Vichan\Data\Driver\CacheDriver; @@ -106,6 +107,7 @@ function mod_logout(Context $ctx) { function mod_dashboard(Context $ctx) { global $mod; $config = $ctx->get('config'); + $report_queries = $ctx->get(ReportQueries::class); $args = []; @@ -131,8 +133,7 @@ function mod_dashboard(Context $ctx) { $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'] = $query->fetchColumn(); + $args['reports'] = $report_queries->getCount(); $query = query('SELECT COUNT(*) FROM ``ban_appeals``') or error(db_error($query)); $args['appeals'] = $query->fetchColumn(); @@ -2444,43 +2445,22 @@ function mod_reports(Context $ctx) { if (!hasPermission($config['mod']['reports'])) error($config['error']['noaccess']); - $query = prepare("SELECT * FROM ``reports`` ORDER BY `time` DESC LIMIT :limit"); - $query->bindValue(':limit', $config['mod']['recent_reports'], PDO::PARAM_INT); - $query->execute() or error(db_error($query)); - $reports = $query->fetchAll(PDO::FETCH_ASSOC); + $reports_limit = $config['mod']['recent_reports']; + $report_queries = $ctx->get(ReportQueries::class); + $report_rows = $report_queries->getReportsWithPosts($reports_limit); - $report_queries = []; - foreach ($reports as $report) { - if (!isset($report_queries[$report['board']])) - $report_queries[$report['board']] = []; - $report_queries[$report['board']][] = $report['post']; + if (\count($report_rows) > $reports_limit) { + \array_pop($report_rows); + $has_extra = true; + } else { + $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 = ''; - foreach ($reports 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; - } - + foreach ($report_rows as $report) { openBoard($report['board']); - $post = &$report_posts[$report['board']][$report['post']]; + $post = $report['post_data']; if (!$post['thread']) { // Still need to fix this: @@ -2489,16 +2469,16 @@ function mod_reports(Context $ctx) { $po = new Post($post, '?/', $mod); } - // a little messy and inefficient - $append_html = Element($config['file_mod_report'], array( + // A little messy and inefficient. + $append_html = Element($config['file_mod_report'], [ 'report' => $report, 'config' => $config, 'mod' => $mod, 'pm' => create_pm_header(), 'token' => make_secure_link_token('reports/' . $report['id'] . '/dismiss'), '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 $po->body = truncate($po->body, $po->link(), $config['body_truncate'] - substr_count($append_html, '
')); @@ -2513,14 +2493,16 @@ function mod_reports(Context $ctx) { $body .= $po->build(true) . '
'; - if (isset($__old_body_truncate_char)) + if (isset($__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( - sprintf('%s (%d)', _('Report queue'), $count), + sprintf('%s (%s)', _('Report queue'), $header_count), $config['file_mod_reports'], [ 'reports' => $body, @@ -2533,31 +2515,29 @@ function mod_reports(Context $ctx) { function mod_report_dismiss(Context $ctx, $id, $action) { $config = $ctx->get('config'); - $query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id"); - $query->bindValue(':id', $id); - $query->execute() or error(db_error($query)); - if ($report = $query->fetch(PDO::FETCH_ASSOC)) { - $ip = $report['ip']; - $board = $report['board']; - $post = $report['post']; - } else + $report_queries = $ctx->get(ReportQueries::class); + $report = $report_queries->getReportById($id); + + if ($report === null) { error($config['error']['404']); + } + $ip = $report['ip']; + $board = $report['board']; + $post = $report['post']; switch($action){ case '&post': if (!hasPermission($config['mod']['report_dismiss_post'], $board)) error($config['error']['noaccess']); - $query = prepare("DELETE FROM ``reports`` WHERE `post` = :post"); - $query->bindValue(':post', $post); + $report_queries->deleteByPost($post); modLog("Dismissed all reports for post #{$id}", $board); break; case '&all': if (!hasPermission($config['mod']['report_dismiss_ip'], $board)) error($config['error']['noaccess']); - $query = prepare("DELETE FROM ``reports`` WHERE `ip` = :ip"); - $query->bindValue(':ip', $ip); + $report_queries->deleteByIp($ip); $cip = cloak_ip($ip); modLog("Dismissed all reports by $cip"); break; @@ -2566,12 +2546,10 @@ function mod_report_dismiss(Context $ctx, $id, $action) { if (!hasPermission($config['mod']['report_dismiss'], $board)) error($config['error']['noaccess']); - $query = prepare("DELETE FROM ``reports`` WHERE `id` = :id"); - $query->bindValue(':id', $id); + $report_queries->deleteById($id); modLog("Dismissed a report for post #{$id}", $board); break; } - $query->execute() or error(db_error($query)); header('Location: ?/reports', true, $config['redirect_http']); }