Compare commits
4 Commits
b2fdfa2f1a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df2faffd49 | ||
|
|
fd9ce292b3 | ||
|
|
f21f4c2bbf | ||
|
|
5329f036f9 |
1
extensions.ini
Normal file
1
extensions.ini
Normal file
@@ -0,0 +1 @@
|
|||||||
|
extensions = 334,338
|
||||||
@@ -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');
|
||||||
@@ -22,14 +10,14 @@ function main(): void {
|
|||||||
$argv = $_SERVER['argv'] ?? [];
|
$argv = $_SERVER['argv'] ?? [];
|
||||||
$do_notify = in_array('--notify', $argv, true);
|
$do_notify = in_array('--notify', $argv, true);
|
||||||
|
|
||||||
$pbdb = pull_db();
|
|
||||||
$pbdb = trim_db($pbdb);
|
|
||||||
|
|
||||||
$ext_list = ['338', '334'];
|
$pbdb = trim_db(pull_db());
|
||||||
|
|
||||||
|
$ext_list = read_extension_ini();
|
||||||
$mac_list = pull_mac_list();
|
$mac_list = pull_mac_list();
|
||||||
|
|
||||||
foreach ($ext_list as $ext) {
|
foreach ($ext_list as $ext) {
|
||||||
$mac = $mac_list[$ext] ?? null;
|
$mac = strtolower($mac_list[$ext]) ?? null;
|
||||||
|
|
||||||
if (!$mac) { echo "Mac for $ext not found\n"; continue; }
|
if (!$mac) { echo "Mac for $ext not found\n"; continue; }
|
||||||
|
|
||||||
@@ -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) {
|
||||||
@@ -109,7 +65,7 @@ function pull_db(): array {
|
|||||||
|
|
||||||
function trim_db($pbdb): array {
|
function trim_db($pbdb): array {
|
||||||
array_unshift($pbdb, [
|
array_unshift($pbdb, [
|
||||||
'name' => 'Night time',
|
'name' => 'Night Hours',
|
||||||
'extension' => '*271',
|
'extension' => '*271',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -130,39 +86,46 @@ function pull_mac_list(): array {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT mac, ext
|
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
|
||||||
FROM endpoint_extensions
|
FROM endpoint_extensions
|
||||||
WHERE ext REGEXP '^[0-9]+$'
|
ORDER BY CAST(ext AS UNSIGNED)
|
||||||
ORDER BY CAST(extension AS UNSIGNED)
|
|
||||||
");
|
");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
echo $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_column($mac_db, 'mac', 'ext');
|
return array_column($mac_db, 'mac', 'ext');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function read_extension_ini(): array {
|
||||||
|
$extensions = parse_ini_file('extensions.ini');
|
||||||
|
$ext_list = array_map('intval', explode(',', $extensions['extensions']));
|
||||||
|
|
||||||
|
return $ext_list;
|
||||||
|
}
|
||||||
|
|
||||||
function pull_xml_file($file): DOMDocument {
|
function pull_xml_file($file): DOMDocument {
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
$file = '/tftpboot/000000000000-features.cfg';
|
||||||
|
}
|
||||||
$xmlString = file_get_contents($file);
|
$xmlString = file_get_contents($file);
|
||||||
if ($xmlString === false) {
|
if ($xmlString === false) {
|
||||||
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();
|
||||||
@@ -183,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) {
|
||||||
@@ -215,34 +176,38 @@ function write_attendants($attendant, $pbdb): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function write_to_file($provision_dir, $out_file, $xml): void {
|
function write_to_file($provision_dir, $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);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmpfile = $out_file . '.tmp';
|
$tmpfile = $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, $file)) {
|
||||||
@unlink($tmpfile);
|
@unlink($tmpfile);
|
||||||
fwrite(STDERR, "Failed to move $tmpfile to $out_file (permissions?)\n");
|
fwrite(STDERR, "Failed to move $tmpfile to $file (permissions?)\n");
|
||||||
exit(4);
|
exit(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Wrote $out_file \n";
|
if (!chown($file, 'asterisk')) {
|
||||||
|
error_log("chown failed for $file");
|
||||||
|
}
|
||||||
|
if (!chgrp($file, 'asterisk')) {
|
||||||
|
error_log("chgrp failed for $file");
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Wrote $file \n";
|
||||||
}
|
}
|
||||||
|
|
||||||
function notify($pbdb): void {
|
function notify($pbdb): void {
|
||||||
/** Optional: push check-sync to re-download directory without reboot */
|
|
||||||
// Use the extension list we already built
|
|
||||||
$notified = 0;
|
$notified = 0;
|
||||||
foreach ($pbdb 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
|
|
||||||
$cmd = "asterisk -rx \"pjsip send notify polycom-check-cfg endpoint " . escapeshellarg($ext) . "\"";
|
$cmd = "asterisk -rx \"pjsip send notify polycom-check-cfg endpoint " . escapeshellarg($ext) . "\"";
|
||||||
exec($cmd, $o, $rc);
|
exec($cmd, $o, $rc);
|
||||||
if ($rc === 0) $notified++;
|
if ($rc === 0) $notified++;
|
||||||
|
|||||||
Reference in New Issue
Block a user