Compare commits
7
Commits
d5fa504526
...
d8ff7cc641
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8ff7cc641 | ||
|
|
ef7dc8ff09 | ||
|
|
6948df2357 | ||
|
|
c9f46413fb | ||
|
|
9b89c3b649 | ||
|
|
8421f5d374 | ||
|
|
792998334f |
@@ -0,0 +1,4 @@
|
||||
target_extensions = 334, 338
|
||||
list_filter_type = whitelist
|
||||
extension_filter_list = 404, 911
|
||||
blacklisted_terms = vesibule, inpatient
|
||||
@@ -1 +0,0 @@
|
||||
extensions = 334,338
|
||||
+101
-36
@@ -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 {
|
||||
@@ -192,9 +256,10 @@ function write_attendants($attendant, $pbdb): void {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user