diff --git a/inc/mod/pages.php b/inc/mod/pages.php index b9956eb7..eb981076 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -99,7 +99,7 @@ function mod_logout() { function mod_dashboard() { global $config, $mod; - $args = array(); + $args = []; $args['boards'] = listBoards(); @@ -239,7 +239,7 @@ function mod_search($type, $search_query_escaped, $page_no = 1) { $query = str_replace('`', '!`', $query); // Array of phrases to match - $match = array(); + $match = []; // Exact phrases ("like this") if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) { @@ -772,7 +772,7 @@ function mod_board_log($board, $page_no = 1, $hide_names = false, $public = fals function mod_view_catalog($boardName) { global $config, $mod; require_once($config['dir']['themes'].'/catalog/theme.php'); - $settings = array(); + $settings = []; $settings['boards'] = $boardName; $settings['update_on_posts'] = true; $settings['title'] = 'Catalog'; @@ -1109,7 +1109,7 @@ function mod_bans() { if (!hasPermission($config['mod']['unban'])) error($config['error']['noaccess']); - $unban = array(); + $unban = []; foreach ($_POST as $name => $unused) { if (preg_match('/^ban_(\d+)$/', $name, $match)) $unban[] = $match[1]; @@ -1198,16 +1198,16 @@ function mod_ban_appeals() { $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts_%s`` WHERE `id` = " . (int)$ban['post']['id'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { - $_post['files'] = $_post['files'] ? json_decode($_post['files']) : array(); + $_post['files'] = $_post['files'] ? json_decode($_post['files']) : []; $ban['post'] = array_merge($ban['post'], $_post); } else { - $ban['post']['files'] = array(array()); + $ban['post']['files'] = array([]); $ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['thumb'] = false; $ban['post']['num_files'] = 1; } } else { - $ban['post']['files'] = array(array()); + $ban['post']['files'] = array([]); $ban['post']['files'][0]['file'] = 'deleted'; $ban['post']['files'][0]['thumb'] = false; $ban['post']['num_files'] = 1; @@ -1505,7 +1505,7 @@ function mod_move($originBoard, $postID) { $query->bindValue(':id', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $replies = array(); + $replies = []; while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $post['mod'] = true; @@ -1572,7 +1572,7 @@ function mod_move($originBoard, $postID) { if (!empty($post['tracked_cites'])) { - $insert_rows = array(); + $insert_rows = []; foreach ($post['tracked_cites'] as $cite) { $insert_rows[] = '(' . $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . @@ -1770,7 +1770,7 @@ function mod_merge($originBoard, $postID) { $query->bindValue(':id', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $replies = array(); + $replies = []; while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $post['mod'] = true; @@ -1833,7 +1833,7 @@ function mod_merge($originBoard, $postID) { if (!empty($post['tracked_cites'])) { - $insert_rows = array(); + $insert_rows = []; foreach ($post['tracked_cites'] as $cite) { $insert_rows[] = '(' . $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' . @@ -1954,7 +1954,7 @@ function mod_ban_post($board, $delete, $post, $token = false) { $name = $mypost["name"]; $subject = $mypost["subject"]; $filehash = $mypost["filehash"]; - $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : array(); + $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : []; // For each file append file name for ($file_count = 0; $file_count < $mypost["num_files"];$file_count++){ $filename .= $mypost['files'][$file_count]->name . "\r\n"; @@ -2065,7 +2065,7 @@ function mod_warning_post($board,$post, $token = false) { $name = $mypost["name"]; $subject = $mypost["subject"]; $filehash = $mypost["filehash"]; - $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : array(); + $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : []; // For each file append file name for ($file_count = 0; $file_count < $mypost["num_files"];$file_count++){ $filename .= $mypost['files'][$file_count]->name . "\r\n"; @@ -2217,7 +2217,7 @@ function mod_delete($board, $post) { $name = $mypost["name"]; $subject = $mypost["subject"]; $filehash = $mypost["filehash"]; - $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : array(); + $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : []; // For each file append file name for ($file_count = 0; $file_count < $mypost["num_files"];$file_count++){ $filename .= $mypost['files'][$file_count]->name . "\r\n"; @@ -2364,9 +2364,9 @@ function mod_deletebyip($boardName, $post, $global = false) { @set_time_limit($config['mod']['rebuild_timelimit']); - $boards_to_rebuild = array(); - $threads_to_rebuild = array(); - $threads_deleted = array(); + $boards_to_rebuild = []; + $threads_to_rebuild = []; + $threads_deleted = []; while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $boards_to_rebuild[$post['board']] = true; openBoard($post['board']); @@ -2388,7 +2388,7 @@ function mod_deletebyip($boardName, $post, $global = false) { $name = $mypost["name"]; $subject = $mypost["subject"]; $filehash = $mypost["filehash"]; - $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : array(); + $mypost['files'] = $mypost['files'] ? json_decode($mypost['files']) : []; // For each file append file name for ($file_count = 0; $file_count < $mypost["num_files"];$file_count++){ $filename .= $mypost['files'][$file_count]->name . "\r\n"; @@ -2466,7 +2466,7 @@ function mod_user($uid) { $board = $board['uri']; } - $boards = array(); + $boards = []; foreach ($_POST as $name => $value) { if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) $boards[] = $matches[1]; @@ -2557,7 +2557,7 @@ function mod_user($uid) { $query->execute() or error(db_error($query)); $log = $query->fetchAll(PDO::FETCH_ASSOC); } else { - $log = array(); + $log = []; } $user['boards'] = explode(',', $user['boards']); @@ -2590,7 +2590,7 @@ function mod_user_new() { $board = $board['uri']; } - $boards = array(); + $boards = []; foreach ($_POST as $name => $value) { if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards)) $boards[] = $matches[1]; @@ -2823,9 +2823,9 @@ function mod_rebuild() { if (isset($_POST['rebuild'])) { @set_time_limit($config['mod']['rebuild_timelimit']); - $log = array(); + $log = []; $boards = listBoards(); - $rebuilt_scripts = array(); + $rebuilt_scripts = []; if (isset($_POST['rebuild_cache'])) { if ($config['cache']['enabled']) { @@ -2897,16 +2897,16 @@ function mod_reports() { $query->execute() or error(db_error($query)); $reports = $query->fetchAll(PDO::FETCH_ASSOC); - $report_queries = array(); + $report_queries = []; foreach ($reports as $report) { if (!isset($report_queries[$report['board']])) - $report_queries[$report['board']] = array(); + $report_queries[$report['board']] = []; $report_queries[$report['board']][] = $report['post']; } - $report_posts = array(); + $report_posts = []; foreach ($report_queries as $board => $posts) { - $report_posts[$board] = array(); + $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)) { @@ -2918,11 +2918,13 @@ function mod_reports() { $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)); + // 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; } @@ -3014,7 +3016,7 @@ function mod_recent_posts($lim,$board_list = false,$json=false) { $limit = (is_numeric($lim))? $lim : 25; $last_time = (isset($_GET['last']) && is_numeric($_GET['last'])) ? $_GET['last'] : 0; - $mod_boards = array(); + $mod_boards = []; $boards = listBoards(); //if not all boards @@ -3028,7 +3030,7 @@ function mod_recent_posts($lim,$board_list = false,$json=false) { } if ($board_list != false){ $board_array = explode(",",$board_list); - $new_board_array = array(); + $new_board_array = []; foreach ($board_array as $board) { if (array_key_exists($board,$config['boards_alias'])){ $newboard = $config['boards_alias'][$board]; @@ -3038,7 +3040,7 @@ function mod_recent_posts($lim,$board_list = false,$json=false) { } $new_board_array[] = $newboard; } - $mod_boards = array(); + $mod_boards = []; foreach ($boards as $board) { if (in_array($board['uri'], $new_board_array)){ $mod_boards[] = $board; @@ -3059,7 +3061,7 @@ function mod_recent_posts($lim,$board_list = false,$json=false) { $posts = $query->fetchAll(PDO::FETCH_ASSOC); if ($config['api']['enabled']) { - $apithreads = array(); + $apithreads = []; } foreach ($posts as &$post) { @@ -3215,7 +3217,7 @@ function mod_config($board_config = false) { if ($config['minify_html']) $config_append = str_replace("\n", ' ', $config_append); - $page = array(); + $page = []; $page['title'] = 'Cannot write to file!'; $page['config'] = $config; $page['body'] = ' @@ -3258,7 +3260,7 @@ function mod_themes_list() { $themes_in_use = $query->fetchAll(PDO::FETCH_COLUMN); // Scan directory for themes - $themes = array(); + $themes = []; while ($file = readdir($dir)) { if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) { $themes[$file] = loadThemeConfig($file); @@ -3553,7 +3555,7 @@ function mod_pages($board = false) { function mod_debug_antispam() { global $pdo, $config; - $args = array(); + $args = []; if (isset($_POST['board'], $_POST['thread'])) { $where = '`board` = ' . $pdo->quote($_POST['board']); @@ -3657,7 +3659,7 @@ function mod_debug_apc() { $cache_info = apc_cache_info('user'); // $cached_vars = new APCIterator('user', '/^' . $config['cache']['prefix'] . '/'); - $cached_vars = array(); + $cached_vars = []; foreach ($cache_info['cache_list'] as $var) { if ($config['cache']['prefix'] != '' && strpos(isset($var['key']) ? $var['key'] : $var['info'], $config['cache']['prefix']) !== 0) continue; diff --git a/tools/maintenance.php b/tools/maintenance.php index 33c0a4d4..abf53361 100644 --- a/tools/maintenance.php +++ b/tools/maintenance.php @@ -5,6 +5,56 @@ require dirname(__FILE__) . '/inc/cli.php'; +function get_reports_by_board(): array { + $query = prepare("SELECT `board`, `post`, `id` FROM ``reports``"); + $query->execute() or error(db_error($query)); + return $query->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_NUM); +} + +function post_ids_by_board(array $reports): array { + $ret = []; + foreach ($reports as $board => $values) { + foreach ($values as $value) { + $ret[$board] ??= []; + $ret[$board][] = $value[0]; + } + } + return $ret; +} + +function filter_invalid_reports(array $board_post_ids, array $reports): array { + $invalid_reports = []; + + foreach ($board_post_ids as $board => $post_ids) { + $query = query(sprintf('SELECT `id` FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $post_ids), $board)); + $existing_posts = $query->fetchAll(PDO::FETCH_COLUMN); + + foreach ($reports[$board] as $values) { + list($post_id, $report_id) = $values; + if (!in_array($post_id, $existing_posts)) { + $invalid_reports[] = $report_id; + } + } + } + + return $invalid_reports; +} + +function get_report_ids(array $reports): array { + $ret = []; + foreach ($reports as $_board => $values) { + foreach ($values as $value) { + $report_id = $value[0]; + $ret[] = $report_id; + } + } + return $ret; +} + +function delete_reports(array $report_ids): int { + return query('DELETE FROM ``reports`` WHERE `id` = ' . implode(' OR `id` = ', $report_ids))->rowCount(); +} + echo "Clearing expired bans...\n"; $start = microtime(true); $deleted_count = Bans::purge($config['require_ban_view'], $config['purge_bans']); @@ -19,7 +69,23 @@ $deleted_count = purge_old_antispam(); $delta = microtime(true) - $start; echo "Deleted $deleted_count expired antispam in $delta seconds!\n"; $time_tot = $delta; -$deleted_tot = $deleted_count; +$deleted_tot += $deleted_count; + +echo "Clearing invalid reports...\n"; +$start = microtime(true); +$reports = get_reports_by_board(); +$report_ids = get_report_ids($reports); +$board_post_ids = post_ids_by_board($reports); +$invalid_reports = filter_invalid_reports($board_post_ids, $reports); +if (!empty($invalid_reports)) { + $deleted_count = delete_reports($invalid_reports); +} else { + $deleted_count = 0; +} +$delta = microtime(true) - $start; +echo "Deleted $deleted_count invalid reports in $delta seconds!\n"; +$time_tot += $delta; +$deleted_tot += $deleted_count; $time_tot = number_format((float)$time_tot, 4, '.', ''); modLog("Deleted $deleted_tot expired entries in {$time_tot}s with maintenance tool");