Compare commits

...

8 Commits

Author SHA1 Message Date
3d880b6699 removed null returns 2026-02-02 11:28:28 -06:00
69c5a88c50 removed return tag 2026-02-02 11:27:13 -06:00
598af2c954 bool reutnr type removed 2026-02-02 11:25:57 -06:00
bd9f4c074d fixed bool 2026-02-02 11:24:58 -06:00
8ebdcb7d1d return type tagging 2026-02-02 11:23:00 -06:00
9e7ec4fc52 extra array config fucnt 2026-02-02 11:06:56 -06:00
d98e31faca scalars only in read config 2026-02-02 10:58:05 -06:00
f96abcbb4e fixed config call 2026-02-02 10:52:50 -06:00

View File

@@ -15,7 +15,7 @@ $TARGET_EXTENSIONS = read_config_json("target_extensions");
$LIST_FILTER_TYPE = read_config_json_string("list_filter_type");
$EXTENSION_FILTER_LIST = read_config_json("extension_filter_list");
$BLACKLISTED_TERMS = read_config_json("blacklisted_terms");
$PREPEND_EXTENSIONS = read_config_json("prepend_extensions");
$PREPEND_EXTENSIONS = read_config_json_object_list("prepend_extensions");
function main(): void {
global $TARGET_EXTENSIONS;
@@ -26,9 +26,9 @@ function main(): void {
$pbdb = pull_db();
prepend_contact_list($pbdb);
blacklist_terms($pbdb);
filter_extensions($pbdb);
prepend_contact_list($pbdb);
$mac_list = pull_mac_list();
@@ -41,8 +41,8 @@ function main(): void {
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
$xml = pull_xml_file($file);
$attendant = remove_attendants($xml);
write_attendants($attendant, $pbdb);
$attendants = remove_attendants($xml);
write_attendants($attendants, $pbdb);
write_to_file($file, $xml);
}
@@ -78,7 +78,7 @@ function pull_db(): array {
return $pbdb;
}
function filter_extensions(&$pbdb) {
function filter_extensions(array &$pbdb) {
global $LIST_FILTER_TYPE;
if ($LIST_FILTER_TYPE == "whitelist") {
@@ -91,7 +91,7 @@ function filter_extensions(&$pbdb) {
}
}
function blacklist_terms(&$pbdb) {
function blacklist_terms(array &$pbdb) {
global $BLACKLISTED_TERMS;
$pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
@@ -110,7 +110,7 @@ function blacklist_terms(&$pbdb) {
}));
}
function whitelist_extension_filter(&$pbdb) {
function whitelist_extension_filter(array &$pbdb) {
global $EXTENSION_FILTER_LIST;
$allowed = array_fill_keys(
@@ -128,7 +128,7 @@ function whitelist_extension_filter(&$pbdb) {
}));
}
function blacklist_extension_filter(&$pbdb) {
function blacklist_extension_filter(array &$pbdb) {
global $EXTENSION_FILTER_LIST;
$blocked = array_fill_keys(
@@ -146,7 +146,7 @@ function blacklist_extension_filter(&$pbdb) {
}));
}
function prepend_contact_list(&$pbdb) {
function prepend_contact_list(array &$pbdb) {
global $PREPEND_EXTENSIONS;
array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
}
@@ -171,25 +171,16 @@ function pull_mac_list(): array {
function read_config_json_file(string $path): array {
global $COFNIG_PATH;
if (!is_file($path)) {
throw new RuntimeException("Config file not found: {$path}");
}
$json = file_get_contents($path);
$data = json_decode($json, true);
$raw = file_get_contents($path);
if ($raw === false) {
throw new RuntimeException("Unable to read config file: {$path}");
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("JSON decode error: " . json_last_error_msg());
}
$data = json_decode($raw, true);
if (!is_array($data)) {
throw new RuntimeException("Invalid JSON in config file: {$path}");
}
return $data;
}
function read_config_json($key) {
function read_config_json(string $key): array {
global $CONFIG;
$value = $CONFIG[$key] ?? [];
@@ -201,23 +192,41 @@ function read_config_json($key) {
$items = [];
}
$items = array_values(array_filter($items, fn($v) => is_scalar($v)));
$items = array_map(
fn($v) => strtolower(trim((string)$v)),
$items
);
$items = array_values(array_filter($items, fn($v) => $v !== ''));
return $items;
return array_values(array_filter($items, fn($v) => $v !== ''));
}
function read_config_json_string(array $config, string $key): string {
$value = $config[$key] ?? '';
function read_config_json_string(string $key): string {
global $CONFIG;
$value = $CONFIG[$key] ?? '';
return strtolower(trim((string)$value));
}
function read_config_json_object_list(string $key): array {
global $CONFIG;
$value = $CONFIG[$key] ?? [];
function pull_xml_file($file): DOMDocument {
if (!is_array($value)) return [];
$out = [];
foreach ($value as $row) {
if (is_array($row) && isset($row['name'], $row['extension'])) {
$out[] = [
'name' => (string)$row['name'],
'extension' => (string)$row['extension'],
];
}
}
return $out;
}
function pull_xml_file(string $file): DOMDocument {
if (!file_exists($file)) {
$file = '/tftpboot/000000000000-features.cfg';
}
@@ -252,7 +261,7 @@ function pull_xml_file($file): DOMDocument {
return $xml;
}
function remove_attendants($xml): DOMElement {
function remove_attendants(DOMDocument $xml): DOMElement {
$xpath = new DOMXPath($xml);
$attendantNodes = $xpath->query('/polycomConfig/attendant');
if ($attendantNodes->length === 0) {
@@ -273,7 +282,7 @@ function remove_attendants($xml): DOMElement {
return $attendant;
}
function write_attendants($attendant, $pbdb): void {
function write_attendants(DOMElement $attendant, array $pbdb): void {
$index = 1;
foreach ($pbdb as $r) {
$label = trim((string)($r['name'] ?? ''));
@@ -288,7 +297,7 @@ function write_attendants($attendant, $pbdb): void {
}
}
function write_to_file($file, $xml): void {
function write_to_file(string $file, DOMDocument $xml): void {
global $PROVISION_DIR;
if (!is_dir($PROVISION_DIR)) {
fwrite(STDERR, "Provisioning directory not found: $PROVISION_DIR\n");
@@ -316,7 +325,7 @@ function write_to_file($file, $xml): void {
echo "Wrote $file \n";
}
function notify($pbdb): void {
function notify(array $pbdb): void {
$notified = 0;
foreach ($pbdb as $r) {
$ext = trim((string)$r['extension']);