diff --git a/gen_pc_sidecar.php b/gen_pc_sidecar.php index 20c6e64..97983c4 100755 --- a/gen_pc_sidecar.php +++ b/gen_pc_sidecar.php @@ -18,7 +18,12 @@ function main(): void { $argv = $_SERVER['argv'] ?? []; $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(); @@ -68,9 +73,75 @@ function pull_db(): array { return $pbdb; } -function trim_db($pbdb): array { - global $BLACKLISTED_TERMS; +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; + + $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 = [ [ 'name' => 'Night Hours', 'extension' => '*271', ], [ 'name' => 'Overhead Page', 'extension' => '900', ], @@ -80,22 +151,6 @@ function trim_db($pbdb): array { ]; 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 { @@ -120,6 +175,13 @@ function read_config_ini($CONFIG_KEY): array { $config = parse_ini_file('config.ini'); $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; } @@ -183,12 +245,12 @@ function write_attendants($attendant, $pbdb): void { $index = 1; foreach ($pbdb as $r) { $label = trim((string)($r['name'] ?? '')); - $address = trim((string)($r['extension'] ?? '')); + $address = trim((string)($r['extension'] ?? '')); $type = trim((string)("normal")); $attendant->setAttribute("attendant.resourceList.{$index}.address", $address); - $attendant->setAttribute("attendant.resourceList.{$index}.label", $label); - $attendant->setAttribute("attendant.resourceList.{$index}.type", $type); + $attendant->setAttribute("attendant.resourceList.{$index}.label", $label); + $attendant->setAttribute("attendant.resourceList.{$index}.type", $type); $index++; }