diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 050e1216..9f569c77 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; @@ -2891,58 +2892,21 @@ function mod_reports(Context $ctx) { error($config['error']['noaccess']); $reports_limit = $config['mod']['recent_reports']; + $report_queries = $ctx->get(ReportQueries::class); + $report_rows = $report_queries->getReportsWithPosts($reports_limit); - $query = prepare("SELECT * FROM ``reports`` ORDER BY `time` DESC LIMIT :limit"); - $query->bindValue(':limit', $reports_limit + 1, PDO::PARAM_INT); - $query->execute() or error(db_error($query)); - $reports = $query->fetchAll(PDO::FETCH_ASSOC); - - $report_queries = []; - foreach ($reports as $report) { - if (!isset($report_queries[$report['board']])) { - $report_queries[$report['board']] = []; - } - $report_queries[$report['board']][] = $report['post']; - } - - $report_posts = []; - foreach ($report_queries as $board => $posts) { - $report_posts[$board] = []; - - $query = query(\sprintf('SELECT * FROM ``posts_%s`` WHERE `id` IN (' . \implode(',', $posts) . ')', $board)) or error(db_error()); - while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $report_posts[$board][$post['id']] = $post; - } - } - - $to_build = []; - foreach ($reports as $report) { - if (isset($report_posts[$report['board']][$report['post']])) { - $to_build[] = $report; - } else { - // Invalid report (post has since been deleted) - if ($config['auto_maintenance'] != false) { - $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; - } - } - - if (\count($to_build) > $reports_limit) { - \array_pop($to_build); + if (\count($report_rows) > $reports_limit) { + \array_pop($report_rows); $has_extra = true; } else { $has_extra = false; } $body = ''; - foreach ($to_build as $report) { + 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: @@ -2951,7 +2915,7 @@ function mod_reports(Context $ctx) { $po = new Post($post, '?/', $mod); } - // a little messy and inefficient + // A little messy and inefficient. $append_html = Element('mod/report.html', array( 'report' => $report, 'config' => $config, @@ -2978,7 +2942,7 @@ function mod_reports(Context $ctx) { } } - $count = \count($to_build); + $count = \count($report_rows); $header_count = $has_extra ? "{$count}+" : (string)$count; mod_page( @@ -2991,36 +2955,31 @@ function mod_reports(Context $ctx) { function mod_report_dismiss(Context $ctx, $id, $all = false) { global $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']); + } - if (!$all && !hasPermission($config['mod']['report_dismiss'], $board)) - error($config['error']['noaccess']); + $ip = $report['ip']; + $board = $report['board']; - if ($all && !hasPermission($config['mod']['report_dismiss_ip'], $board)) + if (!$all && !hasPermission($config['mod']['report_dismiss'], $board)) { error($config['error']['noaccess']); + } + + if ($all && !hasPermission($config['mod']['report_dismiss_ip'], $board)) { + error($config['error']['noaccess']); + } if ($all) { - $query = prepare("DELETE FROM ``reports`` WHERE `ip` = :ip"); - $query->bindValue(':ip', $ip); - } else { - $query = prepare("DELETE FROM ``reports`` WHERE `id` = :id"); - $query->bindValue(':id', $id); - } - $query->execute() or error(db_error($query)); - - - if ($all) + $report_queries->deleteByIp($ip); modLog("Dismissed all reports by $ip"); - else + } else { + $report_queries->deleteById($id); modLog("Dismissed a report for post #{$id}", $board); + } header('Location: ?/reports', true, $config['redirect_http']); }