Merge pull request 'Fix #139 Stream the ban list from the database' (#140) from bans-json into config

Reviewed-on: leftypol/leftypol#140
This commit is contained in:
Zankaria 2025-07-23 14:06:40 -05:00
commit cbbc267b65
3 changed files with 96 additions and 98 deletions

View file

@ -285,68 +285,68 @@ class Bans {
} }
} }
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) { static public function stream_json($filter_ips = false, $filter_staff = false, $board_access = false, $hide_message = false) {
$query = query("SELECT ``bans``.*, `username` FROM ``bans``
LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`
ORDER BY `created` DESC") or error(db_error());
$bans = $query->fetchAll(PDO::FETCH_ASSOC);
if ($board_access && $board_access[0] == '*') { if ($board_access && $board_access[0] == '*') {
$board_access = false; $board_access = false;
} }
$out ? fputs($out, "[") : print("["); $query = query("SELECT ``bans``.*, `username` FROM ``bans``
LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`
ORDER BY `created` DESC") or error(db_error());
$end = end($bans); print('[');
foreach ($bans as &$ban) { $has_previous = false;
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
$hide_message = false; while (true) {
foreach ($hide_regexes as $regex) { $ban = $query->fetch(PDO::FETCH_ASSOC);
if(preg_match($regex, $ban['reason'])) {
$hide_message = true; if (\is_array($ban)) {
break; $ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
if ($ban['post'] && !$hide_message) {
$post = \json_decode($ban['post']);
$ban['message'] = isset($post->body) ? $post->body : 0;
} }
} unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);
if ($ban['post'] && !$hide_message) { if ($board_access === false || in_array ($ban['board'], $board_access)) {
$post = json_decode($ban['post']); $ban['access'] = true;
$ban['message'] = isset($post->body) ? $post->body : 0;
}
unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);
if ($board_access === false || in_array ($ban['board'], $board_access)) {
$ban['access'] = true;
}
if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) {
$ban['single_addr'] = true;
}
if ($filter_staff || ($board_access !== false && !in_array($ban['board'], $board_access))) {
$ban['username'] = '?';
}
if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) {
@list($ban['mask'], $subnet) = explode("/", $ban['mask']);
$ban['mask'] = preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = array_slice($ban['mask'], 0, 2);
$ban['mask'] = implode(".", $ban['mask']);
$ban['mask'] .= ".x.x";
if (isset ($subnet)) {
$ban['mask'] .= "/$subnet";
} }
$ban['masked'] = true;
}
$json = json_encode($ban); if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) {
$out ? fputs($out, $json) : print($json); $ban['single_addr'] = true;
}
if ($filter_staff || ($board_access !== false && !\in_array($ban['board'], $board_access))) {
$ban['username'] = '?';
}
if ($filter_ips || ($board_access !== false && !\in_array($ban['board'], $board_access))) {
@list($ban['mask'], $subnet) = explode("/", $ban['mask']);
$ban['mask'] = \preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = \array_slice($ban['mask'], 0, 2);
$ban['mask'] = \implode(".", $ban['mask']);
$ban['mask'] .= ".x.x";
if (isset($subnet)) {
$ban['mask'] .= "/$subnet";
}
$ban['masked'] = true;
}
if ($ban['id'] != $end['id']) { $json = \json_encode($ban);
$out ? fputs($out, ",") : print(",");
// Add a comma if there's a previous row.
if ($has_previous) {
print(',');
}
$has_previous = true;
print($json);
} else {
break;
} }
} }
$out ? fputs($out, "]") : print("]"); print(']');
} }
static public function seen($ban_id) { static public function seen($ban_id) {

View file

@ -1192,7 +1192,7 @@ function mod_bans_json(Context $ctx) {
// Compress the json for faster loads // Compress the json for faster loads
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
Bans::stream_json(false, false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']); Bans::stream_json(false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']);
} }
function mod_ban_appeals(Context $ctx) { function mod_ban_appeals(Context $ctx) {

View file

@ -1,57 +1,55 @@
<?php <?php
require 'info.php'; require 'info.php';
function pbanlist_build($action, $settings, $board) { function pbanlist_build($action, $settings, $board) {
// Possible values for $action: // Possible values for $action:
// - all (rebuild everything, initialization) // - all (rebuild everything, initialization)
// - news (news has been updated) // - news (news has been updated)
// - boards (board list changed) // - boards (board list changed)
// - bans (ban list changed) // - bans (ban list changed)
PBanlist::build($action, $settings); PBanlist::build($action, $settings);
}
// Wrap functions in a class so they don't interfere with normal Tinyboard operations
class PBanlist {
public static function build($action, $settings) {
global $config;
if ($action == 'all') {
file_write($config['dir']['home'] . $settings['file_bans'], PBanlist::homepage($settings));
}
if ($action == 'all' || $action == 'bans') {
file_write($config['dir']['home'] . $settings['file_json'], PBanlist::gen_json($settings));
}
} }
// Wrap functions in a class so they don't interfere with normal Tinyboard operations public static function gen_json($settings) {
class PBanlist { \ob_start();
public static function build($action, $settings) { Bans::stream_json(true, true, [], true);
global $config; $out = \ob_get_contents();
\ob_end_clean();
return $out;
}
if ($action == 'all') // Build homepage
file_write($config['dir']['home'] . $settings['file_bans'], PBanlist::homepage($settings)); public static function homepage($settings) {
global $config;
if ($action == 'all' || $action == 'bans') return Element('page.html', [
file_write($config['dir']['home'] . $settings['file_json'], PBanlist::gen_json($settings)); 'config' => $config,
} 'mod' => false,
'title' => _("Ban list"),
public static function gen_json($settings) { 'subtitle' => "",
ob_start();
Bans::stream_json(false, true, true, array(), array("/\bcp\b/i", "/porn/i"));
$out = ob_get_contents();
ob_end_clean();
return $out;
}
// Build homepage
public static function homepage($settings) {
global $config;
return Element('page.html', array(
'config' => $config,
'mod' => false,
#'hide_dashboard_link' => true,
'title' => _("Ban list"),
'subtitle' => "",
'boardlist' => createBoardlist(), 'boardlist' => createBoardlist(),
#'nojavascript' => true, 'body' => Element('mod/ban_list.html', [
'body' => Element('mod/ban_list.html', array( 'mod' => false,
'mod' => false, 'boards' => "[]",
'boards' => "[]", 'token' => false,
'token' => false, 'token_json' => false,
'token_json' => false,
'uri_json' => $config['dir']['home'] . $settings['file_json'], 'uri_json' => $config['dir']['home'] . $settings['file_json'],
)) ])
)); ]);
} }
}; };
?>