From 5768f75f291dd31a59c54b2fed5e5a9602291401 Mon Sep 17 00:00:00 2001 From: poslop Date: Thu, 23 Apr 2026 15:05:06 -0500 Subject: [PATCH] formatting --- directory.php | 47 ++++-- sidecar.php | 440 ++++++++++++++++++++++++++------------------------ 2 files changed, 259 insertions(+), 228 deletions(-) diff --git a/directory.php b/directory.php index 7358f8b..5764176 100755 --- a/directory.php +++ b/directory.php @@ -1,5 +1,6 @@ #!/usr/bin/php -q ") */ if (!$rows) { - $stmt = $db->prepare(" + $stmt = $db->prepare(' SELECT id AS extension, callerid FROM ps_endpoints ORDER BY CAST(id AS UNSIGNED) - "); + '); $stmt->execute(); $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($tmp as $r) { - $ext = trim((string)$r['extension']); - if (!preg_match('/^[0-9]+$/', $ext)) continue; // only numeric extensions - $cid = trim((string)($r['callerid'] ?? '')); + $ext = trim((string) $r['extension']); + if (!preg_match('/^[0-9]+$/', $ext)) + continue; // only numeric extensions + $cid = trim((string) ($r['callerid'] ?? '')); // Try to parse descriptive name from callerid $name = 'Ext ' . $ext; if ($cid !== '') { @@ -79,9 +81,10 @@ $root = $xml->createElement('directory'); $xml->appendChild($root); foreach ($rows as $r) { - $name = trim((string)($r['name'] ?? '')); - $ext = trim((string)($r['extension'] ?? '')); - if ($ext === '') continue; + $name = trim((string) ($r['name'] ?? '')); + $ext = trim((string) ($r['extension'] ?? '')); + if ($ext === '') + continue; // Split name into fn/ln if possible $fn = $name; @@ -128,7 +131,23 @@ if ($do_notify) { $asteriskBin = '/usr/sbin/asterisk'; $excludedEndpoints = [ - '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '900', 'Voyant', 'prepare(" + try { + $stmt = $DB->prepare(" SELECT name, extension FROM users WHERE extension REGEXP '^[0-9]+$' ORDER BY CAST(extension AS UNSIGNED) "); - $stmt->execute(); - $pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC); - } catch (Exception $e) { - } + $stmt->execute(); + $pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (Exception $e) { + } - if (!$pbdb) { - fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n"); - exit(1); - } + if (!$pbdb) { + fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n"); + exit(1); + } - usort($pbdb, function ($a, $b) { - return strcasecmp($a['name'], $b['name']); - }); + usort($pbdb, function ($a, $b) { + return strcasecmp($a['name'], $b['name']); + }); return $pbdb; - } - -function filter_extensions(array &$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(array &$pbdb) { - global $BLACKLISTED_TERMS; - - $pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) { - if (!is_array($item) || !isset($item['name'])) { - return true; - } +function filter_extensions(array &$pbdb) +{ + global $LIST_FILTER_TYPE; - $name = ltrim($item['name']); - - foreach ($BLACKLISTED_TERMS as $term) { - if (stripos($name, $term) === 0) { - return false; - } - } - return true; - })); + 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 whitelist_extension_filter(array &$pbdb) { - global $EXTENSION_FILTER_LIST; - - $allowed = array_fill_keys( - array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), - true - ); +function blacklist_terms(array &$pbdb) +{ + global $BLACKLISTED_TERMS; - $pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) { - if (!is_array($item) || !isset($item['extension'])) { - return false; - } + $pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) { + if (!is_array($item) || !isset($item['name'])) { + return true; + } - $ext = trim((string)$item['extension']); - return isset($allowed[$ext]); - })); + $name = ltrim($item['name']); + + foreach ($BLACKLISTED_TERMS as $term) { + if (stripos($name, $term) === 0) { + return false; + } + } + return true; + })); } -function blacklist_extension_filter(array &$pbdb) { - global $EXTENSION_FILTER_LIST; +function whitelist_extension_filter(array &$pbdb) +{ + global $EXTENSION_FILTER_LIST; - $blocked = array_fill_keys( - array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), - true - ); + $allowed = 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; - } + $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($blocked[$ext]); - })); + $ext = trim((string) $item['extension']); + return isset($allowed[$ext]); + })); } -function prepend_contact_list(array &$pbdb) { - global $PREPEND_EXTENSIONS; - array_unshift($pbdb, ...$PREPEND_EXTENSIONS); +function blacklist_extension_filter(array &$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 pull_mac_list(): array { - global $DB; - $mac_db = []; +function prepend_contact_list(array &$pbdb) +{ + global $PREPEND_EXTENSIONS; + array_unshift($pbdb, ...$PREPEND_EXTENSIONS); +} - try { - $stmt = $DB->prepare(" +function pull_mac_list(): array +{ + global $DB; + $mac_db = []; + + try { + $stmt = $DB->prepare(" SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext FROM endpoint_extensions ORDER BY CAST(ext AS UNSIGNED) "); - $stmt->execute(); - $mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC); - } catch (Exception $e) { - echo $e; - } - return array_column($mac_db, 'mac', 'ext'); + $stmt->execute(); + $mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (Exception $e) { + echo $e; + } + return array_column($mac_db, 'mac', 'ext'); } - -function read_config_json_file(string $path): array { +function read_config_json_file(string $path): array +{ $json = file_get_contents($path); $data = json_decode($json, true); if ($data === null && json_last_error() !== JSON_ERROR_NONE) { - throw new RuntimeException("JSON decode error: " . json_last_error_msg()); + throw new RuntimeException('JSON decode error: ' . json_last_error_msg()); } return $data; } -function read_config_json(string $key): array { +function read_config_json(string $key): array +{ global $CONFIG; $value = $CONFIG[$key] ?? []; @@ -194,145 +199,152 @@ function read_config_json(string $key): array { $items = array_values(array_filter($items, fn($v) => is_scalar($v))); - $items = array_map( - fn($v) => strtolower(trim((string)$v)), - $items - ); + $items = array_map(fn($v) => strtolower(trim((string) $v)), $items); return array_values(array_filter($items, fn($v) => $v !== '')); } -function read_config_json_string(string $key): string { +function read_config_json_string(string $key): string +{ global $CONFIG; $value = $CONFIG[$key] ?? ''; - return strtolower(trim((string)$value)); + return strtolower(trim((string) $value)); } -function read_config_json_object_list(string $key): array { +function read_config_json_object_list(string $key): array +{ global $CONFIG; $value = $CONFIG[$key] ?? []; - if (!is_array($value)) return []; + 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'], + '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'; - } - $xmlString = file_get_contents($file); - if ($xmlString === false) { - throw new RuntimeException("Unable to read file: {$file}"); - } +function pull_xml_file(string $file): DOMDocument +{ + if (!file_exists($file)) { + $file = '/tftpboot/000000000000-features.cfg'; + } + $xmlString = file_get_contents($file); + if ($xmlString === false) { + throw new RuntimeException("Unable to read file: {$file}"); + } - $xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM - $xmlString = ltrim($xmlString); + $xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM + $xmlString = ltrim($xmlString); - $firstAngle = strpos($xmlString, '<'); - if ($firstAngle !== false && $firstAngle > 0) { - $xmlString = substr($xmlString, $firstAngle); - } + $firstAngle = strpos($xmlString, '<'); + if ($firstAngle !== false && $firstAngle > 0) { + $xmlString = substr($xmlString, $firstAngle); + } - $xml = new DOMDocument('1.0', 'UTF-8'); - $xml->preserveWhiteSpace = true; - $xml->formatOutput = true; - $xml->xmlStandalone = true; + $xml = new DOMDocument('1.0', 'UTF-8'); + $xml->preserveWhiteSpace = true; + $xml->formatOutput = true; + $xml->xmlStandalone = true; - if (!$xml->loadXML($xmlString)) { - $errs = libxml_get_errors(); - libxml_clear_errors(); - $msg = "Failed to parse XML.\n"; - foreach ($errs as $e) { - $msg .= "[level {$e->level}] {$e->message} at line {$e->line}\n"; - } - throw new RuntimeException($msg); - } + if (!$xml->loadXML($xmlString)) { + $errs = libxml_get_errors(); + libxml_clear_errors(); + $msg = "Failed to parse XML.\n"; + foreach ($errs as $e) { + $msg .= "[level {$e->level}] {$e->message} at line {$e->line}\n"; + } + throw new RuntimeException($msg); + } - return $xml; + return $xml; } -function remove_attendants(DOMDocument $xml): DOMElement { - $xpath = new DOMXPath($xml); - $attendantNodes = $xpath->query('/polycomConfig/attendant'); - if ($attendantNodes->length === 0) { - throw new RuntimeException("No element found at /polycomConfig/attendant"); - } - $attendant = $attendantNodes->item(0); +function remove_attendants(DOMDocument $xml): DOMElement +{ + $xpath = new DOMXPath($xml); + $attendantNodes = $xpath->query('/polycomConfig/attendant'); + if ($attendantNodes->length === 0) { + throw new RuntimeException('No element found at /polycomConfig/attendant'); + } + $attendant = $attendantNodes->item(0); - $toRemove = []; - foreach ($attendant->attributes as $attr) { - if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) { - $toRemove[] = $attr->nodeName; - } - } - foreach ($toRemove as $name) { - $attendant->removeAttribute($name); - } + $toRemove = []; + foreach ($attendant->attributes as $attr) { + if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) { + $toRemove[] = $attr->nodeName; + } + } + foreach ($toRemove as $name) { + $attendant->removeAttribute($name); + } - return $attendant; + return $attendant; } -function write_attendants(DOMElement $attendant, array $pbdb): void { - $index = 1; - foreach ($pbdb as $r) { - $label = trim((string)($r['name'] ?? '')); - $address = trim((string)($r['extension'] ?? '')); - $type = trim((string)("normal")); +function write_attendants(DOMElement $attendant, array $pbdb): void +{ + $index = 1; + foreach ($pbdb as $r) { + $label = trim((string) ($r['name'] ?? '')); + $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}.address", $address); + $attendant->setAttribute("attendant.resourceList.{$index}.label", $label); + $attendant->setAttribute("attendant.resourceList.{$index}.type", $type); - $index++; - } + $index++; + } } -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"); - exit(2); - } +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"); + exit(2); + } - $tmpfile = $file . '.tmp'; - if ($xml->save($tmpfile) === false) { - fwrite(STDERR, "Failed to write temporary file $tmpfile\n"); - exit(3); - } - if (!@rename($tmpfile, $file)) { - @unlink($tmpfile); - fwrite(STDERR, "Failed to move $tmpfile to $file (permissions?)\n"); - exit(4); - } + $tmpfile = $file . '.tmp'; + if ($xml->save($tmpfile) === false) { + fwrite(STDERR, "Failed to write temporary file $tmpfile\n"); + exit(3); + } + if (!@rename($tmpfile, $file)) { + @unlink($tmpfile); + fwrite(STDERR, "Failed to move $tmpfile to $file (permissions?)\n"); + exit(4); + } - if (!chown($file, 'asterisk')) { - error_log("chown failed for $file"); - } - if (!chgrp($file, 'asterisk')) { - error_log("chgrp failed for $file"); - } + if (!chown($file, 'asterisk')) { + error_log("chown failed for $file"); + } + if (!chgrp($file, 'asterisk')) { + error_log("chgrp failed for $file"); + } - echo "Wrote $file \n"; + echo "Wrote $file \n"; } -function notify(array $pbdb): void { +function notify(array $pbdb): void +{ $notified = 0; foreach ($pbdb as $r) { - $ext = trim((string)$r['extension']); - if ($ext === '') continue; - $cmd = "asterisk -rx \"pjsip send notify polycom-check-cfg endpoint " . escapeshellarg($ext) . "\""; + $ext = trim((string) $r['extension']); + if ($ext === '') + continue; + $cmd = 'asterisk -rx "pjsip send notify polycom-check-cfg endpoint ' . escapeshellarg($ext) . '"'; exec($cmd, $o, $rc); - if ($rc === 0) $notified++; + if ($rc === 0) + $notified++; } echo "Sent check-sync to $notified endpoints\n"; }