This commit is contained in:
2026-01-30 14:08:57 -06:00
parent 9b89c3b649
commit c9f46413fb

View File

@@ -18,7 +18,12 @@ function main(): void {
$argv = $_SERVER['argv'] ?? []; $argv = $_SERVER['argv'] ?? [];
$do_notify = in_array('--notify', $argv, true); $do_notify = in_array('--notify', $argv, true);
$pbdb = trim_db(pull_db()); $pbdb = pull_db();
prepend_contact_list($pbdb);
blacklist_terms($pbdb);
filter_extensions($pbdb);
$mac_list = pull_mac_list(); $mac_list = pull_mac_list();
@@ -68,9 +73,75 @@ function pull_db(): array {
return $pbdb; return $pbdb;
} }
function trim_db($pbdb): array { function filter_extensions(&$pbdb) {
global $LIST_FILTER_TYPE;
if ($LIST_FILTER_TYPE == "whitelist") {
whitelist_extension_filter($pbdb);
} elseif ($LIST_FILTER_TYPE == "blacklist") {
blacklist_extension_filter($pbdb);
} else {
fwrite(STDERR, "Filter type invalid: $LIST_FILTER_TYPE");
fwrite(STDERR, "Use either whitelist or blacklist");
}
}
function blacklist_terms(&$pbdb) {
global $BLACKLISTED_TERMS; global $BLACKLISTED_TERMS;
$pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
if (!is_array($item) || !isset($item['name'])) {
return true;
}
$name = ltrim($item['name']);
foreach ($BLACKLISTED_TERMS as $term) {
if (stripos($name, $term) === 0) {
return false;
}
}
return true;
}));
}
function whitelist_extension_filter(&$pbdb) {
global $EXTENSION_FILTER_LIST;
$allowed = array_fill_keys(
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
true
);
$pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) {
if (!is_array($item) || !isset($item['extension'])) {
return false;
}
$ext = trim((string)$item['extension']);
return isset($allowed[$ext]);
}));
}
function blacklist_extension_filter(&$pbdb) {
global $EXTENSION_FILTER_LIST;
$blocked = array_fill_keys(
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
true
);
$pbdb = array_values(array_filter($pbdb, function ($item) use ($blocked) {
if (!is_array($item) || !isset($item['extension'])) {
return true;
}
$ext = trim((string)$item['extension']);
return !isset($blocked[$ext]);
}));
}
function prepend_contact_list(&$pbdb) {
$pbdb_prepend = [ $pbdb_prepend = [
[ 'name' => 'Night Hours', 'extension' => '*271', ], [ 'name' => 'Night Hours', 'extension' => '*271', ],
[ 'name' => 'Overhead Page', 'extension' => '900', ], [ 'name' => 'Overhead Page', 'extension' => '900', ],
@@ -80,22 +151,6 @@ function trim_db($pbdb): array {
]; ];
array_unshift($pbdb, ...$pbdb_prepend); array_unshift($pbdb, ...$pbdb_prepend);
$filtered = array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
if (!is_array($item) || !isset($item['name'])) {
return true;
}
$name = ltrim($item['name']);
foreach ($BLACKLISTED_TERMS as $BLACKLISTED_TERM) {
if (stripos($name, $BLACKLISTED_TERM) === 0) {
return false;
}
}
return true;
});
return $filtered;
} }
function pull_mac_list(): array { function pull_mac_list(): array {
@@ -120,6 +175,13 @@ function read_config_ini($CONFIG_KEY): array {
$config = parse_ini_file('config.ini'); $config = parse_ini_file('config.ini');
$config_value = preg_split('/\s*,\s*/', $config[$CONFIG_KEY] ?? '', -1, PREG_SPLIT_NO_EMPTY); $config_value = preg_split('/\s*,\s*/', $config[$CONFIG_KEY] ?? '', -1, PREG_SPLIT_NO_EMPTY);
$config_value = array_map(
fn($v) => strtolower(trim((string)$v)),
$config_value
);
$config_value = array_values(array_filter($config_value, fn($v) => $v !== ''));
return $config_value; return $config_value;
} }