clean up
This commit is contained in:
@@ -1,17 +1,5 @@
|
|||||||
#!/usr/bin/php -q
|
#!/usr/bin/php -q
|
||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Generate Polycom-compatible global directory XML (000000000000-directory.xml)
|
|
||||||
* from PBXact/FreePBX extensions.
|
|
||||||
*
|
|
||||||
* Usage:
|
|
||||||
* php /root/gen_polycom_directory.php [--notify]
|
|
||||||
*
|
|
||||||
* Notes:
|
|
||||||
* - Writes to /tftpboot by default (change $provision_dir if needed).
|
|
||||||
* - Avoids <sd> (speed-dial) so contacts do NOT auto-populate line keys.
|
|
||||||
* - Optional --notify will send PJSIP check-sync to all numeric endpoints.
|
|
||||||
*/
|
|
||||||
|
|
||||||
$bootstrap_settings['freepbx_auth'] = false;
|
$bootstrap_settings['freepbx_auth'] = false;
|
||||||
require_once('/etc/freepbx.conf');
|
require_once('/etc/freepbx.conf');
|
||||||
@@ -51,7 +39,6 @@ function pull_db(): array {
|
|||||||
$db = FreePBX::Database();
|
$db = FreePBX::Database();
|
||||||
$pbdb = [];
|
$pbdb = [];
|
||||||
|
|
||||||
/** Try Core 'users' table first (name + extension) */
|
|
||||||
try {
|
try {
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT name, extension
|
SELECT name, extension
|
||||||
@@ -62,37 +49,6 @@ function pull_db(): array {
|
|||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// ignore, fall back below
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
|
|
||||||
if (!$pbdb) {
|
|
||||||
$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'] ?? ''));
|
|
||||||
// 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) {
|
if (!$pbdb) {
|
||||||
@@ -158,20 +114,18 @@ function pull_xml_file($file): DOMDocument {
|
|||||||
throw new RuntimeException("Unable to read file: {$file}");
|
throw new RuntimeException("Unable to read file: {$file}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
$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;
|
||||||
$xml->formatOutput = true;
|
$xml->formatOutput = true;
|
||||||
$xml->xmlStandalone = true; // keep standalone="yes"
|
$xml->xmlStandalone = true;
|
||||||
|
|
||||||
if (!$xml->loadXML($xmlString)) {
|
if (!$xml->loadXML($xmlString)) {
|
||||||
$errs = libxml_get_errors();
|
$errs = libxml_get_errors();
|
||||||
@@ -192,10 +146,8 @@ function remove_attendants($xml): DOMElement {
|
|||||||
if ($attendantNodes->length === 0) {
|
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 */
|
|
||||||
$attendant = $attendantNodes->item(0);
|
$attendant = $attendantNodes->item(0);
|
||||||
|
|
||||||
// 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user