getRange(); return array(inet_pton($range[0]), inet_pton($range[1])); } public static function parse_time($str) { if (empty($str)) return false; if (($time = @strtotime($str)) !== false) return $time; 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; $expire = 0; if (isset($matches[2])) { // Years $expire += $matches[2]*60*60*24*365; } if (isset($matches[4])) { // Months $expire += $matches[4]*60*60*24*30; } if (isset($matches[6])) { // Weeks $expire += $matches[6]*60*60*24*7; } if (isset($matches[8])) { // Days $expire += $matches[8]*60*60*24; } if (isset($matches[10])) { // Hours $expire += $matches[10]*60*60; } if (isset($matches[12])) { // Minutes $expire += $matches[12]*60; } if (isset($matches[14])) { // Seconds $expire += $matches[14]; } return time() + $expire; } static public function parse_range($mask) { $ipstart = false; $ipend = false; 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; 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; list($ipstart, $ipend) = self::calc_cidr($mask); } else { if (($ipstart = @inet_pton($mask)) === false) return false; } return array($ipstart, $ipend); } static public function find($criteria, $board = false, $get_mod_info = false, $id = false) { global $config; $query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans`` ' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . ' WHERE ' . ($id ? 'id = :id' : ' (' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . ' (`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)))') . ' ORDER BY `expires` IS NULL, `expires` DESC'); if ($board !== false) $query->bindValue(':board', $board, PDO::PARAM_STR); if (!$id) { $query->bindValue(':ip', inet_pton($criteria)); } else { $query->bindValue(':id', $criteria); } $query->execute() or error(db_error($query)); $ban_list = array(); while ($ban = $query->fetch(PDO::FETCH_ASSOC)) { if ($ban['expires'] && ($ban['seen'] || !$config['require_ban_view']) && $ban['expires'] < time()) { self::delete($ban['id']); } else { if ($ban['post']) $ban['post'] = json_decode($ban['post'], true); $ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); $ban_list[] = $ban; } } return $ban_list; } static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false) { global $config, $pdo; if ($board_access && $board_access[0] == '*') $board_access = false; $query_addition = ""; if ($board_access) { $boards = implode(", ", array_map(array($pdo, "quote"), $board_access)); $query_addition .= "WHERE `board` IN (".$boards.")"; } if ($board_access !== FALSE) { if (!$query_addition) { $query_addition .= " WHERE (`public_bans` IS TRUE) OR ``bans``.`board` IS NULL"; } } $query = query("SELECT ``bans``.*, `username`, `type` FROM ``bans`` LEFT JOIN ``mods`` ON ``mods``.`id` = `creator` LEFT JOIN ``boards`` ON ``boards``.`uri` = ``bans``.`board` $query_addition ORDER BY `created` DESC") or error(db_error()); $bans = $query->fetchAll(PDO::FETCH_ASSOC); $out ? fputs($out, "[") : print("["); $end = end($bans); foreach ($bans as &$ban) { $ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); if ($ban['post']) { $post = json_decode($ban['post']); $ban['message'] = $post->body; } 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))) { switch ($ban['type']) { case ADMIN: $ban['username'] = 'Admin'; break; case GLOBALVOLUNTEER: $ban['username'] = 'Global Volunteer'; break; case MOD: $ban['username'] = 'Board Owner'; break; case BOARDVOLUNTEER: $ban['username'] = 'Board Volunteer'; break; default: $ban['username'] = '?'; } $ban['vstaff'] = true; } unset($ban['type']); if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) { $ban['mask'] = @less_ip($ban['mask'], $ban['board']); $ban['masked'] = true; } $json = json_encode($ban); $out ? fputs($out, $json) : print($json); if ($ban['id'] != $end['id']) { $out ? fputs($out, ",") : print(","); } } $out ? fputs($out, "]") : print("]"); } static public function seen($ban_id) { global $config; $query = query("UPDATE ``bans`` SET `seen` = 1 WHERE `id` = " . (int)$ban_id) or error(db_error()); if (!$config['cron_bans']) rebuildThemes('bans'); } static public function purge() { global $config; $query = query("DELETE FROM ``bans`` WHERE `expires` IS NOT NULL AND `expires` < " . time() . " AND `seen` = 1") or error(db_error()); if (!$config['cron_bans']) rebuildThemes('bans'); } static public function delete($ban_id, $modlog = false, $boards = false, $dont_rebuild = false) { global $config; if ($boards && $boards[0] == '*') $boards = false; if ($modlog) { $query = query("SELECT `ipstart`, `ipend`, `board` FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error()); 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']); if ($ban['board']) openBoard($ban['board']); $mask = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); modLog("Removed ban #{$ban_id} for " . (filter_var($mask, FILTER_VALIDATE_IP) !== false ? "$mask" : $mask)); } query("DELETE FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error()); if (!$dont_rebuild || !$config['cron_bans']) rebuildThemes('bans'); return true; } static public function new_ban($mask, $reason, $length = false, $ban_board = false, $mod_id = false, $post = false) { global $config, $mod, $pdo, $board; if ($mod_id === false) { $mod_id = isset($mod['id']) ? $mod['id'] : -1; } if (!in_array($ban_board, $mod['boards']) && $mod['boards'][0] != '*') error($config['error']['noaccess']); $range = self::parse_range($mask); $mask = self::range_to_string($range); $query = prepare("INSERT INTO ``bans`` VALUES (NULL, :ipstart, :ipend, :time, :expires, :board, :mod, :reason, 0, :post)"); $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); $query->bindValue(':mod', $mod_id); $query->bindValue(':time', time()); if ($reason !== '') { $reason = escape_markup_modifiers($reason); markup($reason); $query->bindValue(':reason', $reason); } else $query->bindValue(':reason', null, PDO::PARAM_NULL); 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); } if ($ban_board) $query->bindValue(':board', $ban_board); else $query->bindValue(':board', null, PDO::PARAM_NULL); if ($post) { $post['board'] = $board['uri']; $match_urls = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; $matched = array(); preg_match_all("#$match_urls#im", $post['body_nomarkup'], $matched); if (isset($matched[0]) && $matched[0]) { $post['body'] = str_replace($matched[0], '###Link-Removed###', $post['body']); $post['body_nomarkup'] = str_replace($matched[0], '###Link-Removed###', $post['body_nomarkup']); } $query->bindValue(':post', json_encode($post)); } else $query->bindValue(':post', null, PDO::PARAM_NULL); $query->execute() or error(db_error($query)); if (isset($mod['id']) && $mod['id'] == $mod_id) { modLog('Created a new ' . ($length > 0 ? preg_replace('/^(\d+) (\w+?)s?$/', '$1-$2', until($length)) : 'permanent') . ' ban on ' . ($ban_board ? '/' . $ban_board . '/' : 'all boards') . ' for ' . (filter_var($mask, FILTER_VALIDATE_IP) !== false ? "$mask" : $mask) . ' (#' . $pdo->lastInsertId() . ')' . ' with ' . ($reason ? 'reason: ' . utf8tohtml($reason) . '' : 'no reason')); } if (!$config['cron_bans']) rebuildThemes('bans'); return $pdo->lastInsertId(); } }