functional
This commit is contained in:
@@ -16,156 +16,191 @@
|
|||||||
$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 {
|
|
||||||
$stmt = $db->prepare("
|
array_unshift($pbdb, [
|
||||||
SELECT name, extension
|
'name' => 'Night time',
|
||||||
FROM users
|
'extension' => '*271',
|
||||||
WHERE extension REGEXP '^[0-9]+$'
|
]);
|
||||||
ORDER BY CAST(extension AS UNSIGNED)
|
|
||||||
");
|
$blank = ['name' => ' ', 'extension' => ' '];
|
||||||
$stmt->execute();
|
array_splice($rows, 1, 0, array_fill(0, 5, $blank));
|
||||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
} catch (Exception $e) {
|
$xml = pull_xml_file($file);
|
||||||
// ignore, fall back below
|
|
||||||
|
$attendant = remove_attendants($xml);
|
||||||
|
|
||||||
|
write_attendants($attendant, $pbdb);
|
||||||
|
|
||||||
|
write_to_file($provision_dir, $file, $xml);
|
||||||
|
|
||||||
|
if ($do_notify) {
|
||||||
|
notify($pbdb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
|
|
||||||
if (!$rows) {
|
function pull_db(): array {
|
||||||
$stmt = $db->prepare("
|
$db = FreePBX::Database();
|
||||||
SELECT id AS extension, callerid
|
$pbdb = [];
|
||||||
FROM ps_endpoints
|
|
||||||
ORDER BY CAST(id AS UNSIGNED)
|
/** Try Core 'users' table first (name + extension) */
|
||||||
");
|
try {
|
||||||
$stmt->execute();
|
$stmt = $db->prepare("
|
||||||
$tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
SELECT name, extension
|
||||||
foreach ($tmp as $r) {
|
FROM users
|
||||||
$ext = trim((string)$r['extension']);
|
WHERE extension REGEXP '^[0-9]+$'
|
||||||
if (!preg_match('/^[0-9]+$/', $ext)) continue; // only numeric extensions
|
ORDER BY CAST(extension AS UNSIGNED)
|
||||||
$cid = trim((string)($r['callerid'] ?? ''));
|
");
|
||||||
// Try to parse descriptive name from callerid
|
$stmt->execute();
|
||||||
$name = 'Ext ' . $ext;
|
$pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
if ($cid !== '') {
|
} catch (Exception $e) {
|
||||||
// Common formats: "John Smith" <1001> OR John Smith <1001>
|
// ignore, fall back below
|
||||||
if (preg_match('/^\"?([^\"<]+)\"?\s*<\s*' . preg_quote($ext, '/') . '\s*>$/', $cid, $m)) {
|
}
|
||||||
$name = trim($m[1]);
|
|
||||||
} else {
|
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
|
||||||
// If callerid is just a name without angle brackets, use it
|
if (!$pbdb) {
|
||||||
if (strpos($cid, '<') === false) {
|
$stmt = $db->prepare("
|
||||||
$name = $cid;
|
SELECT id AS extension, callerid
|
||||||
}
|
FROM ps_endpoints
|
||||||
}
|
ORDER BY CAST(id AS UNSIGNED)
|
||||||
}
|
");
|
||||||
$rows[] = ['name' => $name, 'extension' => $ext];
|
$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'] ?? ''));
|
||||||
|
// Try to parse descriptive name from callerid
|
||||||
|
$name = 'Ext ' . $ext;
|
||||||
|
if ($cid !== '') {
|
||||||
|
// Common formats: "John Smith" <1001> OR John Smith <1001>
|
||||||
|
if (preg_match('/^\"?([^\"<]+)\"?\s*<\s*' . preg_quote($ext, '/') . '\s*>$/', $cid, $m)) {
|
||||||
|
$name = trim($m[1]);
|
||||||
|
} else {
|
||||||
|
// If callerid is just a name without angle brackets, use it
|
||||||
|
if (strpos($cid, '<') === false) {
|
||||||
|
$name = $cid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$pbdb[] = ['name' => $name, 'extension' => $ext];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$pbdb) {
|
||||||
|
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pbdb;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$rows) {
|
|
||||||
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no rows).\n");
|
function pull_xml_file($file): DOMDocument {
|
||||||
exit(1);
|
$xmlString = file_get_contents($file);
|
||||||
|
if ($xmlString === false) {
|
||||||
|
throw new RuntimeException("Unable to read file: {$file}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip UTF-8 BOM and leading whitespace
|
||||||
|
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
|
||||||
|
$xmlString = ltrim($xmlString);
|
||||||
|
|
||||||
|
// If there are any stray bytes before the first '<', trim them
|
||||||
|
$firstAngle = strpos($xmlString, '<');
|
||||||
|
if ($firstAngle !== false && $firstAngle > 0) {
|
||||||
|
$xmlString = substr($xmlString, $firstAngle);
|
||||||
|
}
|
||||||
|
|
||||||
|
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||||
|
$xml->preserveWhiteSpace = true; // pretty-print
|
||||||
|
$xml->formatOutput = true;
|
||||||
|
$xml->xmlStandalone = true; // keep standalone="yes"
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($rows, [
|
function remove_attendants($xml): DOMElement {
|
||||||
'name' => 'Night time',
|
$xpath = new DOMXPath($xml);
|
||||||
'extension' => '*271',
|
$attendantNodes = $xpath->query('/polycomConfig/attendant');
|
||||||
]);
|
if ($attendantNodes->length === 0) {
|
||||||
|
throw new RuntimeException("No <attendant> element found at /polycomConfig/attendant");
|
||||||
|
}
|
||||||
|
/** @var DOMElement $attendant */
|
||||||
|
$attendant = $attendantNodes->item(0);
|
||||||
|
|
||||||
$xmlString = file_get_contents($out_file);
|
// 1) Remove all existing attendant.resourceList.* attributes
|
||||||
if ($xmlString === false) {
|
$toRemove = [];
|
||||||
throw new RuntimeException("Unable to read file: {$out_file}");
|
foreach ($attendant->attributes as $attr) {
|
||||||
|
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
|
||||||
|
$toRemove[] = $attr->nodeName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($toRemove as $name) {
|
||||||
|
$attendant->removeAttribute($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attendant;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip UTF-8 BOM and leading whitespace
|
function write_attendants($attendant, $pbdb): void {
|
||||||
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
|
$index = 1;
|
||||||
$xmlString = ltrim($xmlString);
|
foreach ($pbdb as $r) {
|
||||||
|
$label = trim((string)($r['name'] ?? ''));
|
||||||
|
$address = trim((string)($r['extension'] ?? ''));
|
||||||
|
$type = trim((string)("normal"));
|
||||||
|
|
||||||
// If there are any stray bytes before the first '<', trim them
|
$attendant->setAttribute("attendant.resourceList.{$index}.address", $address);
|
||||||
$firstAngle = strpos($xmlString, '<');
|
$attendant->setAttribute("attendant.resourceList.{$index}.label", $label);
|
||||||
if ($firstAngle !== false && $firstAngle > 0) {
|
$attendant->setAttribute("attendant.resourceList.{$index}.type", $type);
|
||||||
$xmlString = substr($xmlString, $firstAngle);
|
|
||||||
|
$index++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
function write_to_file($provision_dir, $out_file, $xml): void {
|
||||||
$xml->preserveWhiteSpace = true; // pretty-print
|
if (!is_dir($provision_dir)) {
|
||||||
$xml->formatOutput = true;
|
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
|
||||||
$xml->xmlStandalone = true; // keep standalone="yes"
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
if (!$xml->loadXML($xmlString)) {
|
$tmpfile = $out_file . '.tmp';
|
||||||
$errs = libxml_get_errors();
|
if ($xml->save($tmpfile) === false) {
|
||||||
libxml_clear_errors();
|
fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
|
||||||
$msg = "Failed to parse XML.\n";
|
exit(3);
|
||||||
foreach ($errs as $e) {
|
}
|
||||||
$msg .= "[level {$e->level}] {$e->message} at line {$e->line}\n";
|
if (!@rename($tmpfile, $out_file)) {
|
||||||
}
|
@unlink($tmpfile);
|
||||||
throw new RuntimeException($msg);
|
fwrite(STDERR, "Failed to move $tmpfile to $out_file (permissions?)\n");
|
||||||
|
exit(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Wrote $out_file \n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$xpath = new DOMXPath($xml);
|
function notify($pbdb): void {
|
||||||
$attendantNodes = $xpath->query('/polycomConfig/attendant');
|
/** Optional: push check-sync to re-download directory without reboot */
|
||||||
if ($attendantNodes->length === 0) {
|
|
||||||
throw new RuntimeException("No <attendant> element found at /polycomConfig/attendant");
|
|
||||||
}
|
|
||||||
/** @var DOMElement $attendant */
|
|
||||||
$attendant = $attendantNodes->item(0);
|
|
||||||
|
|
||||||
// 1) Remove all existing attendant.resourceList.* attributes
|
|
||||||
$toRemove = [];
|
|
||||||
foreach ($attendant->attributes as $attr) {
|
|
||||||
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
|
|
||||||
$toRemove[] = $attr->nodeName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($toRemove as $name) {
|
|
||||||
$attendant->removeAttribute($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
$index = 1;
|
|
||||||
foreach ($rows as $r) {
|
|
||||||
$label = trim((string)($r['name'] ?? ''));
|
|
||||||
$address = trim((string)($r['extension'] ?? ''));
|
|
||||||
$type = trim((string)("normal"));
|
|
||||||
|
|
||||||
if ($address === '' && $index > 6) continue;
|
|
||||||
|
|
||||||
$attendant->setAttribute("attendant.resourceList.{$index}.address", $address);
|
|
||||||
$attendant->setAttribute("attendant.resourceList.{$index}.label", $label);
|
|
||||||
$attendant->setAttribute("attendant.resourceList.{$index}.type", $type);
|
|
||||||
|
|
||||||
$index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Ensure provision dir exists */
|
|
||||||
if (!is_dir($provision_dir)) {
|
|
||||||
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Atomic write */
|
|
||||||
$tmpfile = $out_file . '.tmp';
|
|
||||||
if ($xml->save($tmpfile) === false) {
|
|
||||||
fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
if (!@rename($tmpfile, $out_file)) {
|
|
||||||
@unlink($tmpfile);
|
|
||||||
fwrite(STDERR, "Failed to move $tmpfile to $out_file (permissions?)\n");
|
|
||||||
exit(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Wrote $out_file \n";
|
|
||||||
|
|
||||||
/** 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();
|
||||||
|
|||||||
Reference in New Issue
Block a user