Compare commits
7 Commits
59f7f77ca0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5768f75f29 | |||
| 034d05eb81 | |||
| b0f277d57b | |||
| b1a380e3fd | |||
| 6e87b01e72 | |||
| 782e72413c | |||
| a15c767684 |
@@ -0,0 +1 @@
|
||||
config.json
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
{
|
||||
"target_extensions": ["334", "338", "227", "228"],
|
||||
"list_filter_type": "blacklist",
|
||||
"extension_filter_list": [
|
||||
"209",
|
||||
"214",
|
||||
"220",
|
||||
"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"],
|
||||
|
||||
"prepend_extensions": [
|
||||
{"name": "Night Hours", "extension": "*271"},
|
||||
{"name": "Overhead Page", "extension": "900"},
|
||||
{"name": "All page", "extension": "300"},
|
||||
{"name": "Park 71", "extension": "71"},
|
||||
{"name": "Park 72", "extension": "72"},
|
||||
{"name": "OR", "extension": "356"},
|
||||
{"name": "Lab", "extension": "340"},
|
||||
{"name": "Business", "extension": "249"},
|
||||
{"name": "OP Nurse", "extension": "336"}
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/php -q
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Generate Polycom-compatible global directory XML (000000000000-directory.xml)
|
||||
* from PBXact/FreePBX extensions.
|
||||
@@ -14,11 +15,11 @@
|
||||
*/
|
||||
|
||||
$bootstrap_settings['freepbx_auth'] = false;
|
||||
require_once('/etc/freepbx.conf');
|
||||
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);
|
||||
$provision_dir = '/tftpboot'; // <-- adjust if needed
|
||||
$out_file = $provision_dir . '/000000000000-directory.xml';
|
||||
$do_notify = in_array('--notify', $argv, true);
|
||||
|
||||
$db = FreePBX::Database();
|
||||
$rows = [];
|
||||
@@ -39,17 +40,18 @@ try {
|
||||
|
||||
/** Fall back to PJSIP ps_endpoints (id is ext; parse callerid "Name <1001>") */
|
||||
if (!$rows) {
|
||||
$stmt = $db->prepare("
|
||||
$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'] ?? ''));
|
||||
$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 !== '') {
|
||||
@@ -79,9 +81,10 @@ $root = $xml->createElement('directory');
|
||||
$xml->appendChild($root);
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$name = trim((string)($r['name'] ?? ''));
|
||||
$ext = trim((string)($r['extension'] ?? ''));
|
||||
if ($ext === '') continue;
|
||||
$name = trim((string) ($r['name'] ?? ''));
|
||||
$ext = trim((string) ($r['extension'] ?? ''));
|
||||
if ($ext === '')
|
||||
continue;
|
||||
|
||||
// Split name into fn/ln if possible
|
||||
$fn = $name;
|
||||
@@ -128,7 +131,23 @@ 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'
|
||||
'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
|
||||
@@ -158,7 +177,7 @@ if ($do_notify) {
|
||||
$notifyCmd = sprintf(
|
||||
'%s -rx "pjsip send notify polycom-check-cfg endpoint %s"',
|
||||
escapeshellarg($asteriskBin),
|
||||
escapeshellarg($ep)
|
||||
escapeshellarg($ep),
|
||||
);
|
||||
exec($notifyCmd);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"target_extensions": ["101", "202", "303"],
|
||||
"list_filter_type": "blacklist",
|
||||
"extension_filter_list": [
|
||||
"111",
|
||||
"222",
|
||||
"333"
|
||||
],
|
||||
"blacklisted_terms": ["accounting", "inpatient"],
|
||||
"prepend_extensions": [
|
||||
{"name": "Park 300", "extension": "300"},
|
||||
{"name": "Park 301", "extension": "300"},
|
||||
{"name": "Lab", "extension": "340"},
|
||||
{"name": "Business", "extension": "249"}
|
||||
]
|
||||
}
|
||||
@@ -1,340 +0,0 @@
|
||||
#!/usr/bin/php -q
|
||||
<?php
|
||||
|
||||
$PROVISION_DIR = '/tftpboot';
|
||||
|
||||
$bootstrap_settings['freepbx_auth'] = false;
|
||||
require_once('/etc/freepbx.conf');
|
||||
|
||||
$DB = FreePBX::Database();
|
||||
|
||||
$CONFIG_PATH = "./config.json";
|
||||
$CONFIG = read_config_json_file($CONFIG_PATH);
|
||||
|
||||
$TARGET_EXTENSIONS = read_config_json("target_extensions");
|
||||
$LIST_FILTER_TYPE = read_config_json_string("list_filter_type");
|
||||
$EXTENSION_FILTER_LIST = read_config_json("extension_filter_list");
|
||||
$BLACKLISTED_TERMS = read_config_json("blacklisted_terms");
|
||||
$PREPEND_EXTENSIONS = read_config_json_object_list("prepend_extensions");
|
||||
|
||||
function main(): void {
|
||||
global $TARGET_EXTENSIONS;
|
||||
global $PROVISION_DIR;
|
||||
|
||||
$argv = $_SERVER['argv'] ?? [];
|
||||
$do_notify = in_array('--notify', $argv, true);
|
||||
|
||||
$pbdb = pull_db();
|
||||
|
||||
blacklist_terms($pbdb);
|
||||
filter_extensions($pbdb);
|
||||
prepend_contact_list($pbdb);
|
||||
|
||||
|
||||
$mac_list = pull_mac_list();
|
||||
|
||||
foreach ($TARGET_EXTENSIONS as $ext) {
|
||||
$mac = strtolower($mac_list[$ext]) ?? null;
|
||||
|
||||
if (!$mac) { echo "Mac for $ext not found\n"; continue; }
|
||||
|
||||
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
|
||||
|
||||
$xml = pull_xml_file($file);
|
||||
$attendants = remove_attendants($xml);
|
||||
write_attendants($attendants, $pbdb);
|
||||
write_to_file($file, $xml);
|
||||
}
|
||||
|
||||
if ($do_notify) {
|
||||
notify($pbdb);
|
||||
}
|
||||
}
|
||||
|
||||
function pull_db(): array {
|
||||
global $DB;
|
||||
$pbdb = [];
|
||||
|
||||
try {
|
||||
$stmt = $DB->prepare("
|
||||
SELECT name, extension
|
||||
FROM users
|
||||
WHERE extension REGEXP '^[0-9]+$'
|
||||
ORDER BY CAST(extension AS UNSIGNED)
|
||||
");
|
||||
$stmt->execute();
|
||||
$pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
if (!$pbdb) {
|
||||
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
usort($pbdb, function ($a, $b) {
|
||||
return strcasecmp($a['name'], $b['name']);
|
||||
});
|
||||
return $pbdb;
|
||||
}
|
||||
|
||||
function filter_extensions(array &$pbdb) {
|
||||
global $LIST_FILTER_TYPE;
|
||||
|
||||
if ($LIST_FILTER_TYPE == "whitelist") {
|
||||
whitelist_extension_filter($pbdb);
|
||||
} elseif ($LIST_FILTER_TYPE == "blacklist") {
|
||||
blacklist_extension_filter($pbdb);
|
||||
} else {
|
||||
fwrite(STDERR, "Filter type invalid: $LIST_FILTER_TYPE");
|
||||
fwrite(STDERR, "Use either whitelist or blacklist");
|
||||
}
|
||||
}
|
||||
|
||||
function blacklist_terms(array &$pbdb) {
|
||||
global $BLACKLISTED_TERMS;
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
|
||||
if (!is_array($item) || !isset($item['name'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$name = ltrim($item['name']);
|
||||
|
||||
foreach ($BLACKLISTED_TERMS as $term) {
|
||||
if (stripos($name, $term) === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
function whitelist_extension_filter(array &$pbdb) {
|
||||
global $EXTENSION_FILTER_LIST;
|
||||
|
||||
$allowed = array_fill_keys(
|
||||
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
|
||||
true
|
||||
);
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) {
|
||||
if (!is_array($item) || !isset($item['extension'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ext = trim((string)$item['extension']);
|
||||
return isset($allowed[$ext]);
|
||||
}));
|
||||
}
|
||||
|
||||
function blacklist_extension_filter(array &$pbdb) {
|
||||
global $EXTENSION_FILTER_LIST;
|
||||
|
||||
$blocked = array_fill_keys(
|
||||
array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)),
|
||||
true
|
||||
);
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($blocked) {
|
||||
if (!is_array($item) || !isset($item['extension'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$ext = trim((string)$item['extension']);
|
||||
return !isset($blocked[$ext]);
|
||||
}));
|
||||
}
|
||||
|
||||
function prepend_contact_list(array &$pbdb) {
|
||||
global $PREPEND_EXTENSIONS;
|
||||
array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
|
||||
}
|
||||
|
||||
function pull_mac_list(): array {
|
||||
global $DB;
|
||||
$mac_db = [];
|
||||
|
||||
try {
|
||||
$stmt = $DB->prepare("
|
||||
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
|
||||
FROM endpoint_extensions
|
||||
ORDER BY CAST(ext AS UNSIGNED)
|
||||
");
|
||||
$stmt->execute();
|
||||
$mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
echo $e;
|
||||
}
|
||||
return array_column($mac_db, 'mac', 'ext');
|
||||
}
|
||||
|
||||
|
||||
function read_config_json_file(string $path): array {
|
||||
$json = file_get_contents($path);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new RuntimeException("JSON decode error: " . json_last_error_msg());
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
function read_config_json(string $key): array {
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? [];
|
||||
|
||||
if (is_string($value)) {
|
||||
$items = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
|
||||
} elseif (is_array($value)) {
|
||||
$items = $value;
|
||||
} else {
|
||||
$items = [];
|
||||
}
|
||||
|
||||
$items = array_values(array_filter($items, fn($v) => is_scalar($v)));
|
||||
|
||||
$items = array_map(
|
||||
fn($v) => strtolower(trim((string)$v)),
|
||||
$items
|
||||
);
|
||||
|
||||
return array_values(array_filter($items, fn($v) => $v !== ''));
|
||||
}
|
||||
|
||||
function read_config_json_string(string $key): string {
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? '';
|
||||
return strtolower(trim((string)$value));
|
||||
}
|
||||
|
||||
function read_config_json_object_list(string $key): array {
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? [];
|
||||
|
||||
if (!is_array($value)) return [];
|
||||
|
||||
$out = [];
|
||||
foreach ($value as $row) {
|
||||
if (is_array($row) && isset($row['name'], $row['extension'])) {
|
||||
$out[] = [
|
||||
'name' => (string)$row['name'],
|
||||
'extension' => (string)$row['extension'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function pull_xml_file(string $file): DOMDocument {
|
||||
if (!file_exists($file)) {
|
||||
$file = '/tftpboot/000000000000-features.cfg';
|
||||
}
|
||||
$xmlString = file_get_contents($file);
|
||||
if ($xmlString === false) {
|
||||
throw new RuntimeException("Unable to read file: {$file}");
|
||||
}
|
||||
|
||||
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
|
||||
$xmlString = ltrim($xmlString);
|
||||
|
||||
$firstAngle = strpos($xmlString, '<');
|
||||
if ($firstAngle !== false && $firstAngle > 0) {
|
||||
$xmlString = substr($xmlString, $firstAngle);
|
||||
}
|
||||
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->preserveWhiteSpace = true;
|
||||
$xml->formatOutput = true;
|
||||
$xml->xmlStandalone = true;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function remove_attendants(DOMDocument $xml): DOMElement {
|
||||
$xpath = new DOMXPath($xml);
|
||||
$attendantNodes = $xpath->query('/polycomConfig/attendant');
|
||||
if ($attendantNodes->length === 0) {
|
||||
throw new RuntimeException("No <attendant> element found at /polycomConfig/attendant");
|
||||
}
|
||||
$attendant = $attendantNodes->item(0);
|
||||
|
||||
$toRemove = [];
|
||||
foreach ($attendant->attributes as $attr) {
|
||||
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
|
||||
$toRemove[] = $attr->nodeName;
|
||||
}
|
||||
}
|
||||
foreach ($toRemove as $name) {
|
||||
$attendant->removeAttribute($name);
|
||||
}
|
||||
|
||||
return $attendant;
|
||||
}
|
||||
|
||||
function write_attendants(DOMElement $attendant, array $pbdb): void {
|
||||
$index = 1;
|
||||
foreach ($pbdb as $r) {
|
||||
$label = trim((string)($r['name'] ?? ''));
|
||||
$address = trim((string)($r['extension'] ?? ''));
|
||||
$type = trim((string)("normal"));
|
||||
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.address", $address);
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.label", $label);
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.type", $type);
|
||||
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
function write_to_file(string $file, DOMDocument $xml): void {
|
||||
global $PROVISION_DIR;
|
||||
if (!is_dir($PROVISION_DIR)) {
|
||||
fwrite(STDERR, "Provisioning directory not found: $PROVISION_DIR\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
$tmpfile = $file . '.tmp';
|
||||
if ($xml->save($tmpfile) === false) {
|
||||
fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
|
||||
exit(3);
|
||||
}
|
||||
if (!@rename($tmpfile, $file)) {
|
||||
@unlink($tmpfile);
|
||||
fwrite(STDERR, "Failed to move $tmpfile to $file (permissions?)\n");
|
||||
exit(4);
|
||||
}
|
||||
|
||||
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(array $pbdb): void {
|
||||
$notified = 0;
|
||||
foreach ($pbdb as $r) {
|
||||
$ext = trim((string)$r['extension']);
|
||||
if ($ext === '') continue;
|
||||
$cmd = "asterisk -rx \"pjsip send notify polycom-check-cfg endpoint " . escapeshellarg($ext) . "\"";
|
||||
exec($cmd, $o, $rc);
|
||||
if ($rc === 0) $notified++;
|
||||
}
|
||||
echo "Sent check-sync to $notified endpoints\n";
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -1,12 +1,16 @@
|
||||
## Gen PC Sidecar Script
|
||||
The files are stored typically inside of `/var/lib/asterisk/PC-Contact-Sync` and the scripts should be run every 6 hours by a cron job.
|
||||
|
||||
Make sure that all files are owned by asterisk user including the output files and the cronjob.
|
||||
|
||||
<br>
|
||||
|
||||
## Quickstart
|
||||
#### Config Fields
|
||||
`config.json` has several fields. The config file needs to be next to the script in order to be read.
|
||||
|
||||
See the example [config.json](./config.json) included.
|
||||
|
||||
<br>
|
||||
<div style="border-left: 6px solid #FFA500; padding: 10px;">
|
||||
<strong>⚠️ Warning:</strong> Make sure the config follows proper json syntax.
|
||||
@@ -31,13 +35,20 @@ prepend_extensions
|
||||
<br>
|
||||
|
||||
#### Cronjob Example
|
||||
Create the cronjob as asterisk user.
|
||||
|
||||
```
|
||||
EDITOR=vim crontab -e -u asterisk
|
||||
```
|
||||
|
||||
Append this to the crontab
|
||||
|
||||
```
|
||||
# Run Polycom directory sync every 4 hours at :00
|
||||
0 */4 * * * /usr/bin/php /var/lib/asterisk/PC-Contact-Sync/gen_polycom_directory.php --notify >> /var/log/gen_polycom_directory.log 2>&1
|
||||
0 */4 * * * /usr/bin/php /var/lib/asterisk/PC-Contact-Sync/directory.php --notify >> /var/log/directory.log 2>&1
|
||||
|
||||
# Run PC sidecar sync every 4 hours at :05 (5 mins after)
|
||||
10 */4 * * * /usr/bin/php /var/lib/asterisk/PC-Contact-Sync/gen_pc_sidecar.php >> /var/log/gen_pc_sidecar.log 2>&1
|
||||
5 */4 * * * /usr/bin/php /var/lib/asterisk/PC-Contact-Sync/sidecar.php >> /var/log/sidecar.log 2>&1
|
||||
```
|
||||
<br>
|
||||
|
||||
|
||||
Executable
+352
@@ -0,0 +1,352 @@
|
||||
#!/usr/bin/php -q
|
||||
<?php
|
||||
|
||||
$PROVISION_DIR = '/tftpboot';
|
||||
|
||||
$bootstrap_settings['freepbx_auth'] = false;
|
||||
require_once '/etc/freepbx.conf';
|
||||
|
||||
$DB = FreePBX::Database();
|
||||
|
||||
$CONFIG_PATH = './config.json';
|
||||
$CONFIG = read_config_json_file($CONFIG_PATH);
|
||||
|
||||
$TARGET_EXTENSIONS = read_config_json('target_extensions');
|
||||
$LIST_FILTER_TYPE = read_config_json_string('list_filter_type');
|
||||
$EXTENSION_FILTER_LIST = read_config_json('extension_filter_list');
|
||||
$BLACKLISTED_TERMS = read_config_json('blacklisted_terms');
|
||||
$PREPEND_EXTENSIONS = read_config_json_object_list('prepend_extensions');
|
||||
|
||||
function main(): void
|
||||
{
|
||||
global $TARGET_EXTENSIONS;
|
||||
global $PROVISION_DIR;
|
||||
|
||||
$argv = $_SERVER['argv'] ?? [];
|
||||
$do_notify = in_array('--notify', $argv, true);
|
||||
|
||||
$pbdb = pull_db();
|
||||
|
||||
blacklist_terms($pbdb);
|
||||
filter_extensions($pbdb);
|
||||
prepend_contact_list($pbdb);
|
||||
|
||||
$mac_list = pull_mac_list();
|
||||
|
||||
foreach ($TARGET_EXTENSIONS as $ext) {
|
||||
$mac = strtolower($mac_list[$ext]) ?? null;
|
||||
|
||||
if (!$mac) {
|
||||
echo "Mac for $ext not found\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
|
||||
|
||||
$xml = pull_xml_file($file);
|
||||
$attendants = remove_attendants($xml);
|
||||
write_attendants($attendants, $pbdb);
|
||||
write_to_file($file, $xml);
|
||||
}
|
||||
|
||||
if ($do_notify) {
|
||||
notify($pbdb);
|
||||
}
|
||||
}
|
||||
|
||||
function pull_db(): array
|
||||
{
|
||||
global $DB;
|
||||
$pbdb = [];
|
||||
|
||||
try {
|
||||
$stmt = $DB->prepare("
|
||||
SELECT name, extension
|
||||
FROM users
|
||||
WHERE extension REGEXP '^[0-9]+$'
|
||||
ORDER BY CAST(extension AS UNSIGNED)
|
||||
");
|
||||
$stmt->execute();
|
||||
$pbdb = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
if (!$pbdb) {
|
||||
fwrite(STDERR, "No extensions found (users/ps_endpoints returned no pbdb).\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
usort($pbdb, function ($a, $b) {
|
||||
return strcasecmp($a['name'], $b['name']);
|
||||
});
|
||||
return $pbdb;
|
||||
}
|
||||
|
||||
function filter_extensions(array &$pbdb)
|
||||
{
|
||||
global $LIST_FILTER_TYPE;
|
||||
|
||||
if ($LIST_FILTER_TYPE == 'whitelist') {
|
||||
whitelist_extension_filter($pbdb);
|
||||
} elseif ($LIST_FILTER_TYPE == 'blacklist') {
|
||||
blacklist_extension_filter($pbdb);
|
||||
} else {
|
||||
fwrite(STDERR, "Filter type invalid: $LIST_FILTER_TYPE");
|
||||
fwrite(STDERR, 'Use either whitelist or blacklist');
|
||||
}
|
||||
}
|
||||
|
||||
function blacklist_terms(array &$pbdb)
|
||||
{
|
||||
global $BLACKLISTED_TERMS;
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($BLACKLISTED_TERMS) {
|
||||
if (!is_array($item) || !isset($item['name'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$name = ltrim($item['name']);
|
||||
|
||||
foreach ($BLACKLISTED_TERMS as $term) {
|
||||
if (stripos($name, $term) === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
function whitelist_extension_filter(array &$pbdb)
|
||||
{
|
||||
global $EXTENSION_FILTER_LIST;
|
||||
|
||||
$allowed = array_fill_keys(array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), true);
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($allowed) {
|
||||
if (!is_array($item) || !isset($item['extension'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ext = trim((string) $item['extension']);
|
||||
return isset($allowed[$ext]);
|
||||
}));
|
||||
}
|
||||
|
||||
function blacklist_extension_filter(array &$pbdb)
|
||||
{
|
||||
global $EXTENSION_FILTER_LIST;
|
||||
|
||||
$blocked = array_fill_keys(array_map('trim', array_map('strval', $EXTENSION_FILTER_LIST)), true);
|
||||
|
||||
$pbdb = array_values(array_filter($pbdb, function ($item) use ($blocked) {
|
||||
if (!is_array($item) || !isset($item['extension'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$ext = trim((string) $item['extension']);
|
||||
return !isset($blocked[$ext]);
|
||||
}));
|
||||
}
|
||||
|
||||
function prepend_contact_list(array &$pbdb)
|
||||
{
|
||||
global $PREPEND_EXTENSIONS;
|
||||
array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
|
||||
}
|
||||
|
||||
function pull_mac_list(): array
|
||||
{
|
||||
global $DB;
|
||||
$mac_db = [];
|
||||
|
||||
try {
|
||||
$stmt = $DB->prepare("
|
||||
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
|
||||
FROM endpoint_extensions
|
||||
ORDER BY CAST(ext AS UNSIGNED)
|
||||
");
|
||||
$stmt->execute();
|
||||
$mac_db = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
echo $e;
|
||||
}
|
||||
return array_column($mac_db, 'mac', 'ext');
|
||||
}
|
||||
|
||||
function read_config_json_file(string $path): array
|
||||
{
|
||||
$json = file_get_contents($path);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
function read_config_json(string $key): array
|
||||
{
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? [];
|
||||
|
||||
if (is_string($value)) {
|
||||
$items = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
|
||||
} elseif (is_array($value)) {
|
||||
$items = $value;
|
||||
} else {
|
||||
$items = [];
|
||||
}
|
||||
|
||||
$items = array_values(array_filter($items, fn($v) => is_scalar($v)));
|
||||
|
||||
$items = array_map(fn($v) => strtolower(trim((string) $v)), $items);
|
||||
|
||||
return array_values(array_filter($items, fn($v) => $v !== ''));
|
||||
}
|
||||
|
||||
function read_config_json_string(string $key): string
|
||||
{
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? '';
|
||||
return strtolower(trim((string) $value));
|
||||
}
|
||||
|
||||
function read_config_json_object_list(string $key): array
|
||||
{
|
||||
global $CONFIG;
|
||||
$value = $CONFIG[$key] ?? [];
|
||||
|
||||
if (!is_array($value))
|
||||
return [];
|
||||
|
||||
$out = [];
|
||||
foreach ($value as $row) {
|
||||
if (is_array($row) && isset($row['name'], $row['extension'])) {
|
||||
$out[] = [
|
||||
'name' => (string) $row['name'],
|
||||
'extension' => (string) $row['extension'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function pull_xml_file(string $file): DOMDocument
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
$file = '/tftpboot/000000000000-features.cfg';
|
||||
}
|
||||
$xmlString = file_get_contents($file);
|
||||
if ($xmlString === false) {
|
||||
throw new RuntimeException("Unable to read file: {$file}");
|
||||
}
|
||||
|
||||
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
|
||||
$xmlString = ltrim($xmlString);
|
||||
|
||||
$firstAngle = strpos($xmlString, '<');
|
||||
if ($firstAngle !== false && $firstAngle > 0) {
|
||||
$xmlString = substr($xmlString, $firstAngle);
|
||||
}
|
||||
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->preserveWhiteSpace = true;
|
||||
$xml->formatOutput = true;
|
||||
$xml->xmlStandalone = true;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function remove_attendants(DOMDocument $xml): DOMElement
|
||||
{
|
||||
$xpath = new DOMXPath($xml);
|
||||
$attendantNodes = $xpath->query('/polycomConfig/attendant');
|
||||
if ($attendantNodes->length === 0) {
|
||||
throw new RuntimeException('No <attendant> element found at /polycomConfig/attendant');
|
||||
}
|
||||
$attendant = $attendantNodes->item(0);
|
||||
|
||||
$toRemove = [];
|
||||
foreach ($attendant->attributes as $attr) {
|
||||
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
|
||||
$toRemove[] = $attr->nodeName;
|
||||
}
|
||||
}
|
||||
foreach ($toRemove as $name) {
|
||||
$attendant->removeAttribute($name);
|
||||
}
|
||||
|
||||
return $attendant;
|
||||
}
|
||||
|
||||
function write_attendants(DOMElement $attendant, array $pbdb): void
|
||||
{
|
||||
$index = 1;
|
||||
foreach ($pbdb as $r) {
|
||||
$label = trim((string) ($r['name'] ?? ''));
|
||||
$address = trim((string) ($r['extension'] ?? ''));
|
||||
$type = trim((string) 'normal');
|
||||
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.address", $address);
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.label", $label);
|
||||
$attendant->setAttribute("attendant.resourceList.{$index}.type", $type);
|
||||
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
function write_to_file(string $file, DOMDocument $xml): void
|
||||
{
|
||||
global $PROVISION_DIR;
|
||||
if (!is_dir($PROVISION_DIR)) {
|
||||
fwrite(STDERR, "Provisioning directory not found: $PROVISION_DIR\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
$tmpfile = $file . '.tmp';
|
||||
if ($xml->save($tmpfile) === false) {
|
||||
fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
|
||||
exit(3);
|
||||
}
|
||||
if (!@rename($tmpfile, $file)) {
|
||||
@unlink($tmpfile);
|
||||
fwrite(STDERR, "Failed to move $tmpfile to $file (permissions?)\n");
|
||||
exit(4);
|
||||
}
|
||||
|
||||
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(array $pbdb): void
|
||||
{
|
||||
$notified = 0;
|
||||
foreach ($pbdb as $r) {
|
||||
$ext = trim((string) $r['extension']);
|
||||
if ($ext === '')
|
||||
continue;
|
||||
$cmd = 'asterisk -rx "pjsip send notify polycom-check-cfg endpoint ' . escapeshellarg($ext) . '"';
|
||||
exec($cmd, $o, $rc);
|
||||
if ($rc === 0)
|
||||
$notified++;
|
||||
}
|
||||
echo "Sent check-sync to $notified endpoints\n";
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user