|
|
|
@@ -1,32 +1,43 @@
|
|
|
|
#!/usr/bin/php -q
|
|
|
|
#!/usr/bin/php -q
|
|
|
|
<?php
|
|
|
|
<?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;
|
|
|
|
$bootstrap_settings['freepbx_auth'] = false;
|
|
|
|
require_once('/etc/freepbx.conf');
|
|
|
|
require_once('/etc/freepbx.conf');
|
|
|
|
|
|
|
|
|
|
|
|
function main(): void {
|
|
|
|
function main(): void {
|
|
|
|
|
|
|
|
global $TARGET_EXTENSIONS;
|
|
|
|
|
|
|
|
global $PROVISION_DIR;
|
|
|
|
|
|
|
|
|
|
|
|
$provision_dir = '/tftpboot';
|
|
|
|
|
|
|
|
$argv = $_SERVER['argv'] ?? [];
|
|
|
|
$argv = $_SERVER['argv'] ?? [];
|
|
|
|
$do_notify = in_array('--notify', $argv, true);
|
|
|
|
$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();
|
|
|
|
$mac_list = pull_mac_list();
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($ext_list as $ext) {
|
|
|
|
foreach ($TARGET_EXTENSIONS as $ext) {
|
|
|
|
$mac = strtolower($mac_list[$ext]) ?? null;
|
|
|
|
$mac = strtolower($mac_list[$ext]) ?? null;
|
|
|
|
|
|
|
|
|
|
|
|
if (!$mac) { echo "Mac for $ext not found\n"; continue; }
|
|
|
|
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);
|
|
|
|
$xml = pull_xml_file($file);
|
|
|
|
$attendant = remove_attendants($xml);
|
|
|
|
$attendant = remove_attendants($xml);
|
|
|
|
write_attendants($attendant, $pbdb);
|
|
|
|
write_attendants($attendant, $pbdb);
|
|
|
|
write_to_file($provision_dir, $file, $xml);
|
|
|
|
write_to_file($file, $xml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($do_notify) {
|
|
|
|
if ($do_notify) {
|
|
|
|
@@ -59,11 +70,78 @@ function pull_db(): array {
|
|
|
|
usort($pbdb, function ($a, $b) {
|
|
|
|
usort($pbdb, function ($a, $b) {
|
|
|
|
return strcasecmp($a['name'], $b['name']);
|
|
|
|
return strcasecmp($a['name'], $b['name']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$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', ],
|
|
|
|
@@ -73,27 +151,6 @@ function trim_db($pbdb): array {
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
array_unshift($pbdb, ...$pbdb_prepend);
|
|
|
|
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 {
|
|
|
|
function pull_mac_list(): array {
|
|
|
|
@@ -114,11 +171,18 @@ function pull_mac_list(): array {
|
|
|
|
return array_column($mac_db, 'mac', 'ext');
|
|
|
|
return array_column($mac_db, 'mac', 'ext');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function read_extension_ini(): array {
|
|
|
|
function read_config_ini($CONFIG_KEY): array {
|
|
|
|
$extensions = parse_ini_file('extensions.ini');
|
|
|
|
$config = parse_ini_file('config.ini');
|
|
|
|
$ext_list = array_map('intval', explode(',', $extensions['extensions']));
|
|
|
|
$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 {
|
|
|
|
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 {
|
|
|
|
function write_to_file($file, $xml): void {
|
|
|
|
if (!is_dir($provision_dir)) {
|
|
|
|
global $PROVISION_DIR;
|
|
|
|
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
|
|
|
|
if (!is_dir($PROVISION_DIR)) {
|
|
|
|
|
|
|
|
fwrite(STDERR, "Provisioning directory not found: $PROVISION_DIR\n");
|
|
|
|
exit(2);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|