1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-18 10:25:55 +01:00
vichan/inc/bans.php

473 lines
14 KiB
PHP
Raw Normal View History

2013-09-17 01:28:06 +02:00
<?php
use Vichan\Functions\Format;
2013-09-17 01:28:06 +02:00
use Lifo\IP\CIDR;
class Bans {
2024-07-01 21:52:03 +02:00
static private function shouldDelete(array $ban, bool $require_ban_view) {
return $ban['expires'] && ($ban['seen'] || !$require_ban_view) && $ban['expires'] < time();
}
2024-03-22 10:49:47 +01:00
static private function deleteBans(array $ban_ids) {
$len = count($ban_ids);
if ($len === 1) {
$query = prepare('DELETE FROM ``bans`` WHERE `id` = :id');
$query->bindValue(':id', $ban_ids[0], PDO::PARAM_INT);
$query->execute() or error(db_error());
Vichan\Functions\Theme\rebuild_themes('bans');
2024-03-22 10:49:47 +01:00
} elseif ($len >= 1) {
// Build the query.
$query = 'DELETE FROM ``bans`` WHERE `id` IN (';
for ($i = 0; $i < $len; $i++) {
$query .= ":id{$i},";
}
// Substitute the last comma with a parenthesis.
substr_replace($query, ')', strlen($query) - 1);
// Bind the params
$query = prepare($query);
for ($i = 0; $i < $len; $i++) {
$query->bindValue(":id{$i}", (int)$ban_ids[$i], PDO::PARAM_INT);
}
$query->execute() or error(db_error());
Vichan\Functions\Theme\rebuild_themes('bans');
2024-03-22 10:49:47 +01:00
}
}
static private function findSingleAutoGc(string $ip, int $ban_id, bool $require_ban_view): array|null {
// Use OR in the query to also garbage collect bans.
$query = prepare(
'SELECT ``bans``.* FROM ``bans``
WHERE ((`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)) OR (``bans``.id = :id))
ORDER BY `expires` IS NULL, `expires` DESC'
);
$query->bindValue(':id', $ban_id);
$query->bindValue(':ip', inet_pton($ip));
$query->execute() or error(db_error($query));
$found_ban = null;
$to_delete_list = [];
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
2024-07-01 21:52:03 +02:00
if (self::shouldDelete($ban, $require_ban_view)) {
$to_delete_list[] = $ban['id'];
} elseif ($ban['id'] === $ban_id) {
if ($ban['post']) {
$ban['post'] = json_decode($ban['post'], true);
}
2024-07-01 21:56:35 +02:00
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
$found_ban = $ban;
}
}
self::deleteBans($to_delete_list);
return $found_ban;
}
static private function findSingleNoGc(int $ban_id): array|null {
$query = prepare(
'SELECT ``bans``.* FROM ``bans``
WHERE ``bans``.id = :id
ORDER BY `expires` IS NULL, `expires` DESC
LIMIT 1'
);
$query->bindValue(':id', $ban_id);
$query->execute() or error(db_error($query));
$ret = $query->fetch(PDO::FETCH_ASSOC);
if ($query->rowCount() == 0) {
return null;
} else {
if ($ret['post']) {
$ret['post'] = json_decode($ret['post'], true);
}
$ret['mask'] = self::range_to_string([$ret['ipstart'], $ret['ipend']]);
return $ret;
}
}
2024-06-30 16:34:38 +02:00
static private function findAutoGc(?string $ip, string|false $board, bool $get_mod_info, bool $require_ban_view, ?int $ban_id): array {
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
WHERE
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)) OR (``bans``.id = :id))
ORDER BY `expires` IS NULL, `expires` DESC');
if ($board !== false) {
$query->bindValue(':board', $board, PDO::PARAM_STR);
}
$query->bindValue(':id', $ban_id);
$query->bindValue(':ip', inet_pton($ip));
$query->execute() or error(db_error($query));
$ban_list = [];
$to_delete_list = [];
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
2024-07-01 21:52:03 +02:00
if (self::shouldDelete($ban, $require_ban_view)) {
2024-06-30 16:34:38 +02:00
$to_delete_list[] = $ban['id'];
} else {
if ($ban['post']) {
$ban['post'] = json_decode($ban['post'], true);
}
2024-07-01 21:56:35 +02:00
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
2024-06-30 16:34:38 +02:00
$ban_list[] = $ban;
}
}
self::deleteBans($to_delete_list);
return $ban_list;
}
static private function findNoGc(?string $ip, string|false $board, bool $get_mod_info, ?int $ban_id): array {
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
WHERE
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)) OR (``bans``.id = :id))
AND (`expires` IS NULL OR `expires` >= :curr_time)
2024-06-30 16:34:38 +02:00
ORDER BY `expires` IS NULL, `expires` DESC');
if ($board !== false) {
$query->bindValue(':board', $board, PDO::PARAM_STR);
}
$query->bindValue(':id', $ban_id);
$query->bindValue(':ip', inet_pton($ip));
$query->bindValue(':curr_time', time());
$query->execute() or error(db_error($query));
2024-07-01 21:45:46 +02:00
$ban_list = $query->fetchAll(PDO::FETCH_ASSOC);
array_walk($ban_list, function (&$ban, $_index) {
2024-06-30 16:34:38 +02:00
if ($ban['post']) {
$ban['post'] = json_decode($ban['post'], true);
}
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
2024-07-01 21:45:46 +02:00
});
2024-06-30 16:34:38 +02:00
return $ban_list;
}
2013-09-17 01:28:06 +02:00
static public function range_to_string($mask) {
list($ipstart, $ipend) = $mask;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (!isset($ipend) || $ipend === false) {
// Not a range. Single IP address.
$ipstr = inet_ntop($ipstart);
return $ipstr;
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (strlen($ipstart) != strlen($ipend))
return '???'; // What the fuck are you doing, son?
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$range = CIDR::range_to_cidr(inet_ntop($ipstart), inet_ntop($ipend));
if ($range !== false)
return $range;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
return '???';
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
private static function calc_cidr($mask) {
$cidr = new CIDR($mask);
$range = $cidr->getRange();
2024-03-22 10:51:27 +01:00
2024-07-01 21:56:35 +02:00
return [ inet_pton($range[0]), inet_pton($range[1]) ];
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
public static function parse_time($str) {
2013-09-17 01:28:06 +02:00
if (empty($str))
return false;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (($time = @strtotime($str)) !== false)
return $time;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (!preg_match('/^((\d+)\s?ye?a?r?s?)?\s?+((\d+)\s?mon?t?h?s?)?\s?+((\d+)\s?we?e?k?s?)?\s?+((\d+)\s?da?y?s?)?((\d+)\s?ho?u?r?s?)?\s?+((\d+)\s?mi?n?u?t?e?s?)?\s?+((\d+)\s?se?c?o?n?d?s?)?$/', $str, $matches))
return false;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$expire = 0;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (isset($matches[2])) {
// Years
$expire += (int)$matches[2]*60*60*24*365;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[4])) {
// Months
$expire += (int)$matches[4]*60*60*24*30;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[6])) {
// Weeks
$expire += (int)$matches[6]*60*60*24*7;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[8])) {
// Days
$expire += (int)$matches[8]*60*60*24;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[10])) {
// Hours
$expire += (int)$matches[10]*60*60;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[12])) {
// Minutes
$expire += (int)$matches[12]*60;
2013-09-17 01:28:06 +02:00
}
if (isset($matches[14])) {
// Seconds
$expire += (int)$matches[14];
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
return time() + $expire;
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
static public function parse_range($mask) {
$ipstart = false;
$ipend = false;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if (preg_match('@^(\d{1,3}\.){1,3}([\d*]{1,3})?$@', $mask) && substr_count($mask, '*') == 1) {
// IPv4 wildcard mask
$parts = explode('.', $mask);
$ipv4 = '';
foreach ($parts as $part) {
if ($part == '*') {
$ipstart = inet_pton($ipv4 . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.')));
$ipend = inet_pton($ipv4 . '255' . str_repeat('.255', 3 - substr_count($ipv4, '.')));
break;
} elseif(($wc = strpos($part, '*')) !== false) {
$ipstart = inet_pton($ipv4 . substr($part, 0, $wc) . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.')));
$ipend = inet_pton($ipv4 . substr($part, 0, $wc) . '9' . str_repeat('.255', 3 - substr_count($ipv4, '.')));
break;
}
$ipv4 .= "$part.";
}
} elseif (preg_match('@^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+$@', $mask)) {
list($ipv4, $bits) = explode('/', $mask);
if ($bits > 32)
return false;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
list($ipstart, $ipend) = self::calc_cidr($mask);
} elseif (preg_match('@^[:a-z\d]+/\d+$@i', $mask)) {
list($ipv6, $bits) = explode('/', $mask);
if ($bits > 128)
return false;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
list($ipstart, $ipend) = self::calc_cidr($mask);
} else {
if (($ipstart = @inet_pton($mask)) === false)
return false;
}
2024-03-22 10:51:27 +01:00
2024-07-01 21:56:35 +02:00
return [$ipstart, $ipend];
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
static public function findSingle(string $ip, int $ban_id, bool $require_ban_view, bool $auto_gc): array|null {
if ($auto_gc) {
return self::findSingleAutoGc($ip, $ban_id, $require_ban_view);
} else {
return self::findSingleNoGc($ban_id);
2024-03-22 10:49:47 +01:00
}
}
2024-06-30 16:34:38 +02:00
static public function find(?string $ip, string|false $board = false, bool $get_mod_info = false, ?int $ban_id = null, bool $auto_gc = true) {
2013-09-17 01:28:06 +02:00
global $config;
2024-06-30 16:34:38 +02:00
if ($auto_gc) {
return self::findAutoGc($ip, $board, $get_mod_info, $config['require_ban_view'], $ban_id);
} else {
return self::findNoGc($ip, $board, $get_mod_info, $ban_id);
2013-09-17 01:28:06 +02:00
}
}
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false) {
2013-09-17 01:28:06 +02:00
$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] == '*') $board_access = false;
$out ? fputs($out, "[") : print("[");
$end = end($bans);
2021-02-13 14:11:41 +01:00
foreach ($bans as &$ban) {
2024-07-01 21:56:35 +02:00
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
if ($ban['post']) {
$post = json_decode($ban['post']);
$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;
}
2021-02-13 14:11:41 +01:00
if (filter_var($uncloaked_mask, FILTER_VALIDATE_IP) !== false) {
$ban['single_addr'] = true;
}
if ($filter_staff || ($board_access !== false && !in_array($ban['board'], $board_access))) {
2021-02-13 14:11:41 +01:00
$ban['username'] = '?';
}
if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) {
2014-10-09 02:00:31 +02:00
@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);
$out ? fputs($out, $json) : print($json);
if ($ban['id'] != $end['id']) {
$out ? fputs($out, ",") : print(",");
}
}
2021-02-13 14:11:41 +01:00
$out ? fputs($out, "]") : print("]");
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
static public function seen($ban_id) {
$query = query("UPDATE ``bans`` SET `seen` = 1 WHERE `id` = " . (int)$ban_id) or error(db_error());
Vichan\Functions\Theme\rebuild_themes('bans');
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
static public function purge($require_seen, $moratorium) {
2024-06-30 17:01:53 +02:00
if ($require_seen) {
$query = prepare("DELETE FROM ``bans`` WHERE `expires` IS NOT NULL AND `expires` + :moratorium < :curr_time AND `seen` = 1");
2024-06-30 17:01:53 +02:00
} else {
$query = prepare("DELETE FROM ``bans`` WHERE `expires` IS NOT NULL AND `expires` + :moratorium < :curr_time");
2024-06-30 17:01:53 +02:00
}
$query->bindValue(':moratorium', $moratorium);
2024-06-30 17:01:53 +02:00
$query->bindValue(':curr_time', time());
$query->execute() or error(db_error($query));
$affected = $query->rowCount();
if ($affected > 0) {
Vichan\Functions\Theme\rebuild_themes('bans');
2024-06-30 17:01:53 +02:00
}
return $affected;
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
static public function delete($ban_id, $modlog = false, $boards = false, $dont_rebuild = false) {
global $config;
if ($boards && $boards[0] == '*') $boards = false;
2013-09-17 01:28:06 +02:00
if ($modlog) {
$query = query("SELECT `ipstart`, `ipend`, `board` FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error());
2013-09-17 01:28:06 +02:00
if (!$ban = $query->fetch(PDO::FETCH_ASSOC)) {
// Ban doesn't exist
return false;
}
if ($boards !== false && !in_array($ban['board'], $boards))
error($config['error']['noaccess']);
2024-03-22 10:51:27 +01:00
2024-07-01 21:56:35 +02:00
$mask = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
modLog("Removed ban #{$ban_id} for " .
2021-02-13 14:11:41 +01:00
(filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$cloaked_mask\">$cloaked_mask</a>" : $cloaked_mask));
2013-09-17 01:28:06 +02:00
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
query("DELETE FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error());
if (!$dont_rebuild) Vichan\Functions\Theme\rebuild_themes('bans');
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
return true;
}
2024-03-22 10:51:27 +01:00
2021-02-13 14:11:41 +01:00
static public function new_ban($cloaked_mask, $reason, $length = false, $ban_board = false, $mod_id = false, $post = false) {
$mask = uncloak_mask($cloaked_mask);
global $mod, $pdo, $board;
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if ($mod_id === false) {
$mod_id = isset($mod['id']) ? $mod['id'] : -1;
}
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$range = self::parse_range($mask);
$mask = self::range_to_string($range);
2021-02-13 14:11:41 +01:00
$cloaked_mask = cloak_mask($mask);
2024-03-22 10:51:27 +01:00
$query = prepare("INSERT INTO ``bans`` VALUES (NULL, :ipstart, :ipend, :time, :expires, :board, :mod, :reason, 0, :post)");
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$query->bindValue(':ipstart', $range[0]);
if ($range[1] !== false && $range[1] != $range[0])
$query->bindValue(':ipend', $range[1]);
else
$query->bindValue(':ipend', null, PDO::PARAM_NULL);
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$query->bindValue(':mod', $mod_id);
$query->bindValue(':time', time());
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if ($reason !== '') {
$reason = escape_markup_modifiers($reason);
markup($reason);
$query->bindValue(':reason', $reason);
} else
$query->bindValue(':reason', null, PDO::PARAM_NULL);
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
if ($length) {
if (is_int($length) || ctype_digit($length)) {
$length = time() + $length;
} else {
$length = self::parse_time($length);
}
$query->bindValue(':expires', $length);
} else {
$query->bindValue(':expires', null, PDO::PARAM_NULL);
}
2024-03-22 10:51:27 +01:00
if ($ban_board)
$query->bindValue(':board', $ban_board);
2013-09-17 01:28:06 +02:00
else
$query->bindValue(':board', null, PDO::PARAM_NULL);
2024-03-22 10:51:27 +01:00
if ($post) {
if (!isset($board['uri']))
openBoard($post['board']);
$post['board'] = $board['uri'];
/*
* The body can be so long to make the json longer than 64KBs, causing the query to fail.
* Truncate it to a safe length (32KBs). It could probably be longer, but if the deleted body is THAT big
* already, the likelihood of it being just assorted spam/garbage is about 101%.
*/
// We're on UTF-8 only, right...?
$post['body'] = mb_strcut($post['body'], 0, 32768);
$query->bindValue(':post', json_encode($post));
} else
$query->bindValue(':post', null, PDO::PARAM_NULL);
2024-03-22 10:51:27 +01:00
2013-09-17 01:28:06 +02:00
$query->execute() or error(db_error($query));
2024-05-22 22:14:38 +02:00
$ban_len = $length > 0 ? preg_replace('/^(\d+) (\w+?)s?$/', '$1-$2', Format\until($length)) : 'permanent';
$ban_board = $ban_board ? "/$ban_board/" : 'all boards';
$ban_ip = filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$cloaked_mask\">$cloaked_mask</a>" : $cloaked_mask;
$ban_id = $pdo->lastInsertId();
$ban_reason = $reason ? 'reason: ' . utf8tohtml($reason) : 'no reason';
modLog("Created a new $ban_len ban on $ban_board for $ban_ip (<small># $ban_id </small>) with $ban_reason");
Vichan\Functions\Theme\rebuild_themes('bans');
2013-09-17 01:28:06 +02:00
return $pdo->lastInsertId();
}
}