Compare commits

...

2 Commits

Author SHA1 Message Date
poslop 5768f75f29 formatting 2026-04-23 15:05:06 -05:00
poslop 034d05eb81 rename config 2026-04-22 13:45:17 -05:00
3 changed files with 267 additions and 266 deletions
+26 -7
View File
@@ -1,5 +1,6 @@
#!/usr/bin/php -q #!/usr/bin/php -q
<?php <?php
/** /**
* Generate Polycom-compatible global directory XML (000000000000-directory.xml) * Generate Polycom-compatible global directory XML (000000000000-directory.xml)
* from PBXact/FreePBX extensions. * from PBXact/FreePBX extensions.
@@ -14,7 +15,7 @@
*/ */
$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 $provision_dir = '/tftpboot'; // <-- adjust if needed
$out_file = $provision_dir . '/000000000000-directory.xml'; $out_file = $provision_dir . '/000000000000-directory.xml';
@@ -39,16 +40,17 @@ try {
/** 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 (!$rows) {
$stmt = $db->prepare(" $stmt = $db->prepare('
SELECT id AS extension, callerid SELECT id AS extension, callerid
FROM ps_endpoints FROM ps_endpoints
ORDER BY CAST(id AS UNSIGNED) ORDER BY CAST(id AS UNSIGNED)
"); ');
$stmt->execute(); $stmt->execute();
$tmp = $stmt->fetchAll(PDO::FETCH_ASSOC); $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($tmp as $r) { foreach ($tmp as $r) {
$ext = trim((string) $r['extension']); $ext = trim((string) $r['extension']);
if (!preg_match('/^[0-9]+$/', $ext)) continue; // only numeric extensions if (!preg_match('/^[0-9]+$/', $ext))
continue; // only numeric extensions
$cid = trim((string) ($r['callerid'] ?? '')); $cid = trim((string) ($r['callerid'] ?? ''));
// Try to parse descriptive name from callerid // Try to parse descriptive name from callerid
$name = 'Ext ' . $ext; $name = 'Ext ' . $ext;
@@ -81,7 +83,8 @@ $xml->appendChild($root);
foreach ($rows as $r) { foreach ($rows as $r) {
$name = trim((string) ($r['name'] ?? '')); $name = trim((string) ($r['name'] ?? ''));
$ext = trim((string) ($r['extension'] ?? '')); $ext = trim((string) ($r['extension'] ?? ''));
if ($ext === '') continue; if ($ext === '')
continue;
// Split name into fn/ln if possible // Split name into fn/ln if possible
$fn = $name; $fn = $name;
@@ -128,7 +131,23 @@ if ($do_notify) {
$asteriskBin = '/usr/sbin/asterisk'; $asteriskBin = '/usr/sbin/asterisk';
$excludedEndpoints = [ $excludedEndpoints = [
'101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '900', 'Voyant', '<Endpoint dpma_endpoint' '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 // Get all endpoints from Asterisk CLI
@@ -158,7 +177,7 @@ if ($do_notify) {
$notifyCmd = sprintf( $notifyCmd = sprintf(
'%s -rx "pjsip send notify polycom-check-cfg endpoint %s"', '%s -rx "pjsip send notify polycom-check-cfg endpoint %s"',
escapeshellarg($asteriskBin), escapeshellarg($asteriskBin),
escapeshellarg($ep) escapeshellarg($ep),
); );
exec($notifyCmd); exec($notifyCmd);
} }
+8 -38
View File
@@ -1,46 +1,16 @@
{ {
"target_extensions": ["334", "338", "227", "228"], "target_extensions": ["101", "202", "303"],
"list_filter_type": "blacklist", "list_filter_type": "blacklist",
"extension_filter_list": [ "extension_filter_list": [
"209", "111",
"214", "222",
"220", "333"
"234",
"254",
"333",
"344",
"348",
"355",
"365",
"370",
"371",
"372",
"373",
"374",
"375",
"377",
"378",
"379",
"383",
"384",
"390",
"391",
"393",
"397",
"398",
"529"
], ],
"blacklisted_terms": ["vesibule", "inpatient"], "blacklisted_terms": ["accounting", "inpatient"],
"prepend_extensions": [ "prepend_extensions": [
{"name": "Night Hours", "extension": "*271"}, {"name": "Park 300", "extension": "300"},
{"name": "Overhead Page", "extension": "900"}, {"name": "Park 301", "extension": "300"},
{"name": "All page", "extension": "300"},
{"name": "Park 71", "extension": "71"},
{"name": "Park 72", "extension": "72"},
{"name": "OR", "extension": "356"},
{"name": "Lab", "extension": "340"}, {"name": "Lab", "extension": "340"},
{"name": "Business", "extension": "249"}, {"name": "Business", "extension": "249"}
{"name": "OP Nurse", "extension": "336"}
] ]
} }
+61 -49
View File
@@ -4,20 +4,21 @@
$PROVISION_DIR = '/tftpboot'; $PROVISION_DIR = '/tftpboot';
$bootstrap_settings['freepbx_auth'] = false; $bootstrap_settings['freepbx_auth'] = false;
require_once('/etc/freepbx.conf'); require_once '/etc/freepbx.conf';
$DB = FreePBX::Database(); $DB = FreePBX::Database();
$CONFIG_PATH = "./config.json"; $CONFIG_PATH = './config.json';
$CONFIG = read_config_json_file($CONFIG_PATH); $CONFIG = read_config_json_file($CONFIG_PATH);
$TARGET_EXTENSIONS = read_config_json("target_extensions"); $TARGET_EXTENSIONS = read_config_json('target_extensions');
$LIST_FILTER_TYPE = read_config_json_string("list_filter_type"); $LIST_FILTER_TYPE = read_config_json_string('list_filter_type');
$EXTENSION_FILTER_LIST = read_config_json("extension_filter_list"); $EXTENSION_FILTER_LIST = read_config_json('extension_filter_list');
$BLACKLISTED_TERMS = read_config_json("blacklisted_terms"); $BLACKLISTED_TERMS = read_config_json('blacklisted_terms');
$PREPEND_EXTENSIONS = read_config_json_object_list("prepend_extensions"); $PREPEND_EXTENSIONS = read_config_json_object_list('prepend_extensions');
function main(): void { function main(): void
{
global $TARGET_EXTENSIONS; global $TARGET_EXTENSIONS;
global $PROVISION_DIR; global $PROVISION_DIR;
@@ -30,13 +31,15 @@ function main(): void {
filter_extensions($pbdb); filter_extensions($pbdb);
prepend_contact_list($pbdb); prepend_contact_list($pbdb);
$mac_list = pull_mac_list(); $mac_list = pull_mac_list();
foreach ($TARGET_EXTENSIONS as $ext) { foreach ($TARGET_EXTENSIONS as $ext) {
$mac = strtolower($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;
}
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg'; $file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
@@ -51,7 +54,8 @@ function main(): void {
} }
} }
function pull_db(): array { function pull_db(): array
{
global $DB; global $DB;
$pbdb = []; $pbdb = [];
@@ -78,20 +82,22 @@ function pull_db(): array {
return $pbdb; return $pbdb;
} }
function filter_extensions(array &$pbdb) { function filter_extensions(array &$pbdb)
{
global $LIST_FILTER_TYPE; global $LIST_FILTER_TYPE;
if ($LIST_FILTER_TYPE == "whitelist") { if ($LIST_FILTER_TYPE == 'whitelist') {
whitelist_extension_filter($pbdb); whitelist_extension_filter($pbdb);
} elseif ($LIST_FILTER_TYPE == "blacklist") { } elseif ($LIST_FILTER_TYPE == 'blacklist') {
blacklist_extension_filter($pbdb); blacklist_extension_filter($pbdb);
} else { } else {
fwrite(STDERR, "Filter type invalid: $LIST_FILTER_TYPE"); fwrite(STDERR, "Filter type invalid: $LIST_FILTER_TYPE");
fwrite(STDERR, "Use either whitelist or blacklist"); fwrite(STDERR, 'Use either whitelist or blacklist');
} }
} }
function blacklist_terms(array &$pbdb) { function blacklist_terms(array &$pbdb)
{
global $BLACKLISTED_TERMS; global $BLACKLISTED_TERMS;
$pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) { $pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
@@ -110,13 +116,11 @@ function blacklist_terms(array &$pbdb) {
})); }));
} }
function whitelist_extension_filter(array &$pbdb) { function whitelist_extension_filter(array &$pbdb)
{
global $EXTENSION_FILTER_LIST; global $EXTENSION_FILTER_LIST;
$allowed = array_fill_keys( $allowed = array_fill_keys(array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), true);
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
true
);
$pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) { $pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) {
if (!is_array($item) || !isset($item['extension'])) { if (!is_array($item) || !isset($item['extension'])) {
@@ -128,13 +132,11 @@ function whitelist_extension_filter(array &$pbdb) {
})); }));
} }
function blacklist_extension_filter(array &$pbdb) { function blacklist_extension_filter(array &$pbdb)
{
global $EXTENSION_FILTER_LIST; global $EXTENSION_FILTER_LIST;
$blocked = array_fill_keys( $blocked = array_fill_keys(array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), true);
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
true
);
$pbdb = array_values(array_filter($pbdb, function ($item) use ($blocked) { $pbdb = array_values(array_filter($pbdb, function ($item) use ($blocked) {
if (!is_array($item) || !isset($item['extension'])) { if (!is_array($item) || !isset($item['extension'])) {
@@ -146,12 +148,14 @@ function blacklist_extension_filter(array &$pbdb) {
})); }));
} }
function prepend_contact_list(array &$pbdb) { function prepend_contact_list(array &$pbdb)
{
global $PREPEND_EXTENSIONS; global $PREPEND_EXTENSIONS;
array_unshift($pbdb, ...$PREPEND_EXTENSIONS); array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
} }
function pull_mac_list(): array { function pull_mac_list(): array
{
global $DB; global $DB;
$mac_db = []; $mac_db = [];
@@ -169,18 +173,19 @@ function pull_mac_list(): array {
return array_column($mac_db, 'mac', 'ext'); return array_column($mac_db, 'mac', 'ext');
} }
function read_config_json_file(string $path): array
function read_config_json_file(string $path): array { {
$json = file_get_contents($path); $json = file_get_contents($path);
$data = json_decode($json, true); $data = json_decode($json, true);
if ($data === null && json_last_error() !== JSON_ERROR_NONE) { if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("JSON decode error: " . json_last_error_msg()); throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
} }
return $data; return $data;
} }
function read_config_json(string $key): array { function read_config_json(string $key): array
{
global $CONFIG; global $CONFIG;
$value = $CONFIG[$key] ?? []; $value = $CONFIG[$key] ?? [];
@@ -194,25 +199,25 @@ function read_config_json(string $key): array {
$items = array_values(array_filter($items, fn($v) => is_scalar($v))); $items = array_values(array_filter($items, fn($v) => is_scalar($v)));
$items = array_map( $items = array_map(fn($v) => strtolower(trim((string) $v)), $items);
fn($v) => strtolower(trim((string)$v)),
$items
);
return array_values(array_filter($items, fn($v) => $v !== '')); return array_values(array_filter($items, fn($v) => $v !== ''));
} }
function read_config_json_string(string $key): string { function read_config_json_string(string $key): string
{
global $CONFIG; global $CONFIG;
$value = $CONFIG[$key] ?? ''; $value = $CONFIG[$key] ?? '';
return strtolower(trim((string) $value)); return strtolower(trim((string) $value));
} }
function read_config_json_object_list(string $key): array { function read_config_json_object_list(string $key): array
{
global $CONFIG; global $CONFIG;
$value = $CONFIG[$key] ?? []; $value = $CONFIG[$key] ?? [];
if (!is_array($value)) return []; if (!is_array($value))
return [];
$out = []; $out = [];
foreach ($value as $row) { foreach ($value as $row) {
@@ -226,7 +231,8 @@ function read_config_json_object_list(string $key): array {
return $out; return $out;
} }
function pull_xml_file(string $file): DOMDocument { function pull_xml_file(string $file): DOMDocument
{
if (!file_exists($file)) { if (!file_exists($file)) {
$file = '/tftpboot/000000000000-features.cfg'; $file = '/tftpboot/000000000000-features.cfg';
} }
@@ -261,11 +267,12 @@ function pull_xml_file(string $file): DOMDocument {
return $xml; return $xml;
} }
function remove_attendants(DOMDocument $xml): DOMElement { function remove_attendants(DOMDocument $xml): DOMElement
{
$xpath = new DOMXPath($xml); $xpath = new DOMXPath($xml);
$attendantNodes = $xpath->query('/polycomConfig/attendant'); $attendantNodes = $xpath->query('/polycomConfig/attendant');
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');
} }
$attendant = $attendantNodes->item(0); $attendant = $attendantNodes->item(0);
@@ -282,12 +289,13 @@ function remove_attendants(DOMDocument $xml): DOMElement {
return $attendant; return $attendant;
} }
function write_attendants(DOMElement $attendant, array $pbdb): void { function write_attendants(DOMElement $attendant, array $pbdb): void
{
$index = 1; $index = 1;
foreach ($pbdb as $r) { 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');
$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);
@@ -297,7 +305,8 @@ function write_attendants(DOMElement $attendant, array $pbdb): void {
} }
} }
function write_to_file(string $file, DOMDocument $xml): void { function write_to_file(string $file, DOMDocument $xml): void
{
global $PROVISION_DIR; global $PROVISION_DIR;
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");
@@ -325,14 +334,17 @@ function write_to_file(string $file, DOMDocument $xml): void {
echo "Wrote $file \n"; echo "Wrote $file \n";
} }
function notify(array $pbdb): void { function notify(array $pbdb): void
{
$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 === '')
$cmd = "asterisk -rx \"pjsip send notify polycom-check-cfg endpoint " . escapeshellarg($ext) . "\""; continue;
$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++;
} }
echo "Sent check-sync to $notified endpoints\n"; echo "Sent check-sync to $notified endpoints\n";
} }