187 lines
5.3 KiB
PHP
Executable File
187 lines
5.3 KiB
PHP
Executable File
#!/usr/bin/php -q
|
|
<?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;
|
|
require_once '/etc/freepbx.conf';
|
|
|
|
$provision_dir = '/tftpboot'; // <-- adjust if needed
|
|
$out_file = $provision_dir . '/000000000000-directory.xml';
|
|
$do_notify = in_array('--notify', $argv, true);
|
|
|
|
$db = FreePBX::Database();
|
|
$rows = [];
|
|
|
|
/** Try Core 'users' table first (name + extension) */
|
|
try {
|
|
$stmt = $db->prepare("
|
|
SELECT name, extension
|
|
FROM users
|
|
WHERE extension REGEXP '^[0-9]+$'
|
|
ORDER BY CAST(extension AS UNSIGNED)
|
|
");
|
|
$stmt->execute();
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (Exception $e) {
|
|
// ignore, fall back below
|
|
}
|
|
|
|
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
|
|
if (!$rows) {
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
$rows[] = ['name' => $name, 'extension' => $ext];
|
|
}
|
|
}
|
|
|
|
if (!$rows) {
|
|
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no rows).\n");
|
|
exit(1);
|
|
}
|
|
|
|
/** Build Polycom directory XML (no <sd> to avoid auto speed-dials) */
|
|
$xml = new DOMDocument('1.0', 'UTF-8');
|
|
$xml->formatOutput = true;
|
|
$root = $xml->createElement('directory');
|
|
$xml->appendChild($root);
|
|
|
|
foreach ($rows as $r) {
|
|
$name = trim((string) ($r['name'] ?? ''));
|
|
$ext = trim((string) ($r['extension'] ?? ''));
|
|
if ($ext === '')
|
|
continue;
|
|
|
|
// Split name into fn/ln if possible
|
|
$fn = $name;
|
|
$ln = '';
|
|
if (strpos($name, ' ') !== false) {
|
|
$parts = preg_split('/\s+/', $name);
|
|
$fn = array_shift($parts);
|
|
$ln = implode(' ', $parts);
|
|
}
|
|
|
|
$item = $xml->createElement('item');
|
|
$item->appendChild($xml->createElement('fn', $fn));
|
|
if ($ln !== '') {
|
|
$item->appendChild($xml->createElement('ln', $ln));
|
|
}
|
|
$item->appendChild($xml->createElement('ct', $ext));
|
|
|
|
// Intentionally NOT writing <sd> (speed dial) to prevent auto line-key assignment.
|
|
$root->appendChild($item);
|
|
}
|
|
|
|
/** 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 with " . $root->childNodes->length . " contacts\n";
|
|
|
|
/** Optional: push check-sync to re-download directory without reboot */
|
|
if ($do_notify) {
|
|
$asteriskBin = '/usr/sbin/asterisk';
|
|
|
|
$excludedEndpoints = [
|
|
'101',
|
|
'102',
|
|
'103',
|
|
'104',
|
|
'105',
|
|
'106',
|
|
'107',
|
|
'108',
|
|
'109',
|
|
'110',
|
|
'111',
|
|
'112',
|
|
'113',
|
|
'114',
|
|
'900',
|
|
'Voyant',
|
|
'<Endpoint dpma_endpoint',
|
|
];
|
|
|
|
// Get all endpoints from Asterisk CLI
|
|
$cmd = escapeshellcmd($asteriskBin) . ' -rx "pjsip show endpoints"';
|
|
exec($cmd, $output, $returnCode);
|
|
|
|
if ($returnCode !== 0) {
|
|
die("Failed to connect to Asterisk CLI.\n");
|
|
}
|
|
|
|
// Parse endpoint names (mirror sed: capture until space or slash)
|
|
$endpoints = [];
|
|
foreach ($output as $line) {
|
|
if (preg_match('/^\s*Endpoint:\s+([^\/\s]+)/', $line, $matches)) {
|
|
$ep = trim($matches[1]);
|
|
if (!in_array($ep, ['anonymous', 'system']) && !in_array($ep, $excludedEndpoints)) {
|
|
$endpoints[] = $ep;
|
|
}
|
|
}
|
|
}
|
|
|
|
$endpoints = array_unique($endpoints);
|
|
|
|
// Send check-sync NOTIFY to each endpoint
|
|
foreach ($endpoints as $ep) {
|
|
echo "Sending check-sync to $ep\n";
|
|
$notifyCmd = sprintf(
|
|
'%s -rx "pjsip send notify polycom-check-cfg endpoint %s"',
|
|
escapeshellarg($asteriskBin),
|
|
escapeshellarg($ep),
|
|
);
|
|
exec($notifyCmd);
|
|
}
|
|
|
|
echo "Done.\n";
|
|
}
|