Compare commits

...

7 Commits

Author SHA1 Message Date
d8ff7cc641 added index to list_filter_type 2026-01-30 16:39:43 -06:00
ef7dc8ff09 Revert "cleaned up attendant writing"
This reverts commit 6948df2357.
2026-01-30 16:31:06 -06:00
6948df2357 cleaned up attendant writing 2026-01-30 15:18:26 -06:00
c9f46413fb filter 2026-01-30 14:08:57 -06:00
9b89c3b649 trim_db fix config pull 2026-01-30 12:14:33 -06:00
8421f5d374 global values 2026-01-30 12:00:05 -06:00
792998334f config reading 2026-01-30 10:49:02 -06:00
3 changed files with 108 additions and 40 deletions

4
config.ini Normal file
View File

@@ -0,0 +1,4 @@
target_extensions = 334, 338
list_filter_type = whitelist
extension_filter_list = 404, 911
blacklisted_terms = vesibule, inpatient

View File

@@ -1 +0,0 @@
extensions = 334,338

View File

@@ -1,32 +1,43 @@
#!/usr/bin/php -q
<?php
$TARGET_EXTENSIONS = read_config_ini("target_extensions");
$LIST_FILTER_TYPE = read_config_ini("list_filter_type")[0];
$EXTENSION_FILTER_LIST = read_config_ini("extension_filter_list");
$BLACKLISTED_TERMS = read_config_ini("blacklisted_terms");
$PROVISION_DIR = '/tftpboot';
$bootstrap_settings['freepbx_auth'] = false;
require_once('/etc/freepbx.conf');
function main(): void {
global $TARGET_EXTENSIONS;
global $PROVISION_DIR;
$provision_dir = '/tftpboot';
$argv = $_SERVER['argv'] ?? [];
$do_notify = in_array('--notify', $argv, true);
$pbdb = pull_db();
prepend_contact_list($pbdb);
blacklist_terms($pbdb);
filter_extensions($pbdb);
$pbdb = trim_db(pull_db());
$ext_list = read_extension_ini();
$mac_list = pull_mac_list();
foreach ($ext_list as $ext) {
foreach ($TARGET_EXTENSIONS as $ext) {
$mac = strtolower($mac_list[$ext]) ?? null;
if (!$mac) { echo "Mac for $ext not found\n"; continue; }
$file = $provision_dir . '/' . $mac . '-features.cfg';
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
$xml = pull_xml_file($file);
$attendant = remove_attendants($xml);
write_attendants($attendant, $pbdb);
write_to_file($provision_dir, $file, $xml);
write_to_file($file, $xml);
}
if ($do_notify) {
@@ -59,11 +70,78 @@ function pull_db(): array {
usort($pbdb, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
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;
$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', ],
@@ -73,27 +151,6 @@ function trim_db($pbdb): array {
];
array_unshift($pbdb, ...$pbdb_prepend);
$remove_names = [
'inpatient',
'vestibule'
];
$filtered = array_filter($pbdb, function ($item) use ($remove_names) {
if (!is_array($item) || !isset($item['name'])) {
return true;
}
$name = ltrim($item['name']);
foreach ($remove_names as $remove_name) {
if (stripos($name, $remove_name) === 0) {
return false;
}
}
return true;
});
return $filtered;
}
function pull_mac_list(): array {
@@ -114,11 +171,18 @@ function pull_mac_list(): array {
return array_column($mac_db, 'mac', 'ext');
}
function read_extension_ini(): array {
$extensions = parse_ini_file('extensions.ini');
$ext_list = array_map('intval', explode(',', $extensions['extensions']));
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);
return $ext_list;
$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;
}
function pull_xml_file($file): DOMDocument {
@@ -181,20 +245,21 @@ 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++;
}
}
function write_to_file($provision_dir, $file, $xml): void {
if (!is_dir($provision_dir)) {
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
function write_to_file($file, $xml): void {
global $PROVISION_DIR;
if (!is_dir($PROVISION_DIR)) {
fwrite(STDERR, "Provisioning directory not found: $PROVISION_DIR\n");
exit(2);
}