functional

This commit is contained in:
poslop
2025-12-22 13:22:18 -06:00
parent 2fae90e8e7
commit aaf053c8f3

View File

@@ -16,15 +16,43 @@
$bootstrap_settings['freepbx_auth'] = false; $bootstrap_settings['freepbx_auth'] = false;
require_once('/etc/freepbx.conf'); require_once('/etc/freepbx.conf');
$provision_dir = '/tftpboot'; // <-- adjust if needed function main(): void {
$out_file = $provision_dir . '/482567bcdeff-features.cfg';
$do_notify = in_array('--notify', $argv, true);
$db = FreePBX::Database(); $provision_dir = '/tftpboot';
$rows = []; $file = $provision_dir . '/482567bcdeff-features.cfg';
$argv = $_SERVER['argv'] ?? [];
$do_notify = in_array('--notify', $argv, true);
/** Try Core 'users' table first (name + extension) */ $pbdb = pull_db();
try {
array_unshift($pbdb, [
'name' => 'Night time',
'extension' => '*271',
]);
$blank = ['name' => ' ', 'extension' => ' '];
array_splice($rows, 1, 0, array_fill(0, 5, $blank));
$xml = pull_xml_file($file);
$attendant = remove_attendants($xml);
write_attendants($attendant, $pbdb);
write_to_file($provision_dir, $file, $xml);
if ($do_notify) {
notify($pbdb);
}
}
function pull_db(): array {
$db = FreePBX::Database();
$pbdb = [];
/** Try Core 'users' table first (name + extension) */
try {
$stmt = $db->prepare(" $stmt = $db->prepare("
SELECT name, extension SELECT name, extension
FROM users FROM users
@@ -32,13 +60,13 @@ try {
ORDER BY CAST(extension AS UNSIGNED) ORDER BY CAST(extension AS UNSIGNED)
"); ");
$stmt->execute(); $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { } catch (Exception $e) {
// ignore, fall back below // ignore, fall back below
} }
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */ /** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
if (!$rows) { if (!$pbdb) {
$stmt = $db->prepare(" $stmt = $db->prepare("
SELECT id AS extension, callerid SELECT id AS extension, callerid
FROM ps_endpoints FROM ps_endpoints
@@ -63,41 +91,41 @@ if (!$rows) {
} }
} }
} }
$rows[] = ['name' => $name, 'extension' => $ext]; $pbdb[] = ['name' => $name, 'extension' => $ext];
}
} }
}
if (!$rows) { if (!$pbdb) {
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no rows).\n"); fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n");
exit(1); exit(1);
}
return $pbdb;
} }
array_unshift($rows, [
'name' => 'Night time',
'extension' => '*271',
]);
$xmlString = file_get_contents($out_file); function pull_xml_file($file): DOMDocument {
if ($xmlString === false) { $xmlString = file_get_contents($file);
throw new RuntimeException("Unable to read file: {$out_file}"); if ($xmlString === false) {
} throw new RuntimeException("Unable to read file: {$file}");
}
// Strip UTF-8 BOM and leading whitespace // Strip UTF-8 BOM and leading whitespace
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM $xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
$xmlString = ltrim($xmlString); $xmlString = ltrim($xmlString);
// If there are any stray bytes before the first '<', trim them // If there are any stray bytes before the first '<', trim them
$firstAngle = strpos($xmlString, '<'); $firstAngle = strpos($xmlString, '<');
if ($firstAngle !== false && $firstAngle > 0) { if ($firstAngle !== false && $firstAngle > 0) {
$xmlString = substr($xmlString, $firstAngle); $xmlString = substr($xmlString, $firstAngle);
} }
$xml = new DOMDocument('1.0', 'UTF-8'); $xml = new DOMDocument('1.0', 'UTF-8');
$xml->preserveWhiteSpace = true; // pretty-print $xml->preserveWhiteSpace = true; // pretty-print
$xml->formatOutput = true; $xml->formatOutput = true;
$xml->xmlStandalone = true; // keep standalone="yes" $xml->xmlStandalone = true; // keep standalone="yes"
if (!$xml->loadXML($xmlString)) { if (!$xml->loadXML($xmlString)) {
$errs = libxml_get_errors(); $errs = libxml_get_errors();
libxml_clear_errors(); libxml_clear_errors();
$msg = "Failed to parse XML.\n"; $msg = "Failed to parse XML.\n";
@@ -105,67 +133,74 @@ if (!$xml->loadXML($xmlString)) {
$msg .= "[level {$e->level}] {$e->message} at line {$e->line}\n"; $msg .= "[level {$e->level}] {$e->message} at line {$e->line}\n";
} }
throw new RuntimeException($msg); throw new RuntimeException($msg);
}
return $xml;
} }
$xpath = new DOMXPath($xml); function remove_attendants($xml): DOMElement {
$attendantNodes = $xpath->query('/polycomConfig/attendant'); $xpath = new DOMXPath($xml);
if ($attendantNodes->length === 0) { $attendantNodes = $xpath->query('/polycomConfig/attendant');
if ($attendantNodes->length === 0) {
throw new RuntimeException("No <attendant> element found at /polycomConfig/attendant"); throw new RuntimeException("No <attendant> element found at /polycomConfig/attendant");
} }
/** @var DOMElement $attendant */ /** @var DOMElement $attendant */
$attendant = $attendantNodes->item(0); $attendant = $attendantNodes->item(0);
// 1) Remove all existing attendant.resourceList.* attributes // 1) Remove all existing attendant.resourceList.* attributes
$toRemove = []; $toRemove = [];
foreach ($attendant->attributes as $attr) { foreach ($attendant->attributes as $attr) {
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) { if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
$toRemove[] = $attr->nodeName; $toRemove[] = $attr->nodeName;
} }
} }
foreach ($toRemove as $name) { foreach ($toRemove as $name) {
$attendant->removeAttribute($name); $attendant->removeAttribute($name);
}
return $attendant;
} }
$index = 1; function write_attendants($attendant, $pbdb): void {
foreach ($rows as $r) { $index = 1;
foreach ($pbdb as $r) {
$label = trim((string)($r['name'] ?? '')); $label = trim((string)($r['name'] ?? ''));
$address = trim((string)($r['extension'] ?? '')); $address = trim((string)($r['extension'] ?? ''));
$type = trim((string)("normal")); $type = trim((string)("normal"));
if ($address === '' && $index > 6) continue;
$attendant->setAttribute("attendant.resourceList.{$index}.address", $address); $attendant->setAttribute("attendant.resourceList.{$index}.address", $address);
$attendant->setAttribute("attendant.resourceList.{$index}.label", $label); $attendant->setAttribute("attendant.resourceList.{$index}.label", $label);
$attendant->setAttribute("attendant.resourceList.{$index}.type", $type); $attendant->setAttribute("attendant.resourceList.{$index}.type", $type);
$index++; $index++;
}
} }
/** Ensure provision dir exists */ function write_to_file($provision_dir, $out_file, $xml): void {
if (!is_dir($provision_dir)) { if (!is_dir($provision_dir)) {
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n"); fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
exit(2); exit(2);
} }
/** Atomic write */ $tmpfile = $out_file . '.tmp';
$tmpfile = $out_file . '.tmp'; if ($xml->save($tmpfile) === false) {
if ($xml->save($tmpfile) === false) {
fwrite(STDERR, "Failed to write temporary file $tmpfile\n"); fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
exit(3); exit(3);
} }
if (!@rename($tmpfile, $out_file)) { if (!@rename($tmpfile, $out_file)) {
@unlink($tmpfile); @unlink($tmpfile);
fwrite(STDERR, "Failed to move $tmpfile to $out_file (permissions?)\n"); fwrite(STDERR, "Failed to move $tmpfile to $out_file (permissions?)\n");
exit(4); exit(4);
}
echo "Wrote $out_file \n";
} }
echo "Wrote $out_file \n"; function notify($pbdb): void {
/** Optional: push check-sync to re-download directory without reboot */
/** Optional: push check-sync to re-download directory without reboot */
if ($do_notify) {
// Use the extension list we already built // Use the extension list we already built
$notified = 0; $notified = 0;
foreach ($rows as $r) { foreach ($pbdb as $r) {
$ext = trim((string)$r['extension']); $ext = trim((string)$r['extension']);
if ($ext === '') continue; if ($ext === '') continue;
// In FreePBX, PJSIP endpoint id is typically the extension number // In FreePBX, PJSIP endpoint id is typically the extension number
@@ -175,3 +210,5 @@ if ($do_notify) {
} }
echo "Sent check-sync to $notified endpoints\n"; echo "Sent check-sync to $notified endpoints\n";
} }
main();