Compare commits
25 Commits
b2fdfa2f1a
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 190c848aa9 | |||
| fd9239c2bb | |||
| 3d880b6699 | |||
| 69c5a88c50 | |||
| 598af2c954 | |||
| bd9f4c074d | |||
| 8ebdcb7d1d | |||
| 9e7ec4fc52 | |||
| d98e31faca | |||
| f96abcbb4e | |||
| c89e1da002 | |||
| d8ff7cc641 | |||
| ef7dc8ff09 | |||
| 6948df2357 | |||
| c9f46413fb | |||
| 9b89c3b649 | |||
| 8421f5d374 | |||
| 792998334f | |||
|
|
d5fa504526 | ||
|
|
4ab23bd100 | ||
|
|
fbf6a09367 | ||
|
|
df2faffd49 | ||
|
|
fd9ce292b3 | ||
|
|
f21f4c2bbf | ||
|
|
5329f036f9 |
46
config.json
Normal file
46
config.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"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,44 +1,49 @@
|
||||
#!/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.
|
||||
*/
|
||||
|
||||
$PROVISION_DIR = '/tftpboot';
|
||||
|
||||
$bootstrap_settings['freepbx_auth'] = false;
|
||||
require_once('/etc/freepbx.conf');
|
||||
|
||||
function main(): void {
|
||||
$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;
|
||||
|
||||
$provision_dir = '/tftpboot';
|
||||
$argv = $_SERVER['argv'] ?? [];
|
||||
$do_notify = in_array('--notify', $argv, true);
|
||||
|
||||
$pbdb = pull_db();
|
||||
$pbdb = trim_db($pbdb);
|
||||
|
||||
$ext_list = ['338', '334'];
|
||||
blacklist_terms($pbdb);
|
||||
filter_extensions($pbdb);
|
||||
prepend_contact_list($pbdb);
|
||||
|
||||
|
||||
$mac_list = pull_mac_list();
|
||||
|
||||
foreach ($ext_list as $ext) {
|
||||
$mac = $mac_list[$ext] ?? null;
|
||||
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';
|
||||
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
|
||||
|
||||
$xml = pull_xml_file($file);
|
||||
$attendant = remove_attendants($xml);
|
||||
write_attendants($attendant, $pbdb);
|
||||
write_to_file($provision_dir, $file, $xml);
|
||||
$attendants = remove_attendants($xml);
|
||||
write_attendants($attendants, $pbdb);
|
||||
write_to_file($file, $xml);
|
||||
}
|
||||
|
||||
if ($do_notify) {
|
||||
@@ -46,14 +51,12 @@ function main(): void {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function pull_db(): array {
|
||||
$db = FreePBX::Database();
|
||||
global $DB;
|
||||
$pbdb = [];
|
||||
|
||||
/** Try Core 'users' table first (name + extension) */
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
$stmt = $DB->prepare("
|
||||
SELECT name, extension
|
||||
FROM users
|
||||
WHERE extension REGEXP '^[0-9]+$'
|
||||
@@ -62,37 +65,6 @@ function pull_db(): array {
|
||||
$stmt->execute();
|
||||
$pbdb = $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 (!$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) {
|
||||
@@ -103,66 +75,178 @@ function pull_db(): array {
|
||||
usort($pbdb, function ($a, $b) {
|
||||
return strcasecmp($a['name'], $b['name']);
|
||||
});
|
||||
|
||||
return $pbdb;
|
||||
}
|
||||
|
||||
function trim_db($pbdb): array {
|
||||
array_unshift($pbdb, [
|
||||
'name' => 'Night time',
|
||||
'extension' => '*271',
|
||||
]);
|
||||
function filter_extensions(array &$pbdb) {
|
||||
global $LIST_FILTER_TYPE;
|
||||
|
||||
$filtered = array_filter($pbdb, function ($item) {
|
||||
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']);
|
||||
return stripos($name, 'inpatient') !== 0;
|
||||
});
|
||||
|
||||
return $filtered;
|
||||
$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 {
|
||||
$db = FreePBX::Database();
|
||||
global $DB;
|
||||
$mac_db = [];
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT mac, ext
|
||||
$stmt = $DB->prepare("
|
||||
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
|
||||
FROM endpoint_extensions
|
||||
WHERE ext REGEXP '^[0-9]+$'
|
||||
ORDER BY CAST(extension AS UNSIGNED)
|
||||
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 pull_xml_file($file): DOMDocument {
|
||||
|
||||
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}");
|
||||
}
|
||||
|
||||
// Strip UTF-8 BOM and leading whitespace
|
||||
$xmlString = preg_replace('/^\xEF\xBB\xBF/', '', $xmlString); // BOM
|
||||
$xmlString = ltrim($xmlString);
|
||||
|
||||
// If there are any stray bytes before the first '<', trim them
|
||||
$firstAngle = strpos($xmlString, '<');
|
||||
if ($firstAngle !== false && $firstAngle > 0) {
|
||||
$xmlString = substr($xmlString, $firstAngle);
|
||||
}
|
||||
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->preserveWhiteSpace = true; // pretty-print
|
||||
$xml->preserveWhiteSpace = true;
|
||||
$xml->formatOutput = true;
|
||||
$xml->xmlStandalone = true; // keep standalone="yes"
|
||||
$xml->xmlStandalone = true;
|
||||
|
||||
if (!$xml->loadXML($xmlString)) {
|
||||
$errs = libxml_get_errors();
|
||||
@@ -177,16 +261,14 @@ function pull_xml_file($file): DOMDocument {
|
||||
return $xml;
|
||||
}
|
||||
|
||||
function remove_attendants($xml): DOMElement {
|
||||
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");
|
||||
}
|
||||
/** @var DOMElement $attendant */
|
||||
$attendant = $attendantNodes->item(0);
|
||||
|
||||
// 1) Remove all existing attendant.resourceList.* attributes
|
||||
$toRemove = [];
|
||||
foreach ($attendant->attributes as $attr) {
|
||||
if (strpos($attr->nodeName, 'attendant.resourceList.') === 0) {
|
||||
@@ -200,7 +282,7 @@ function remove_attendants($xml): DOMElement {
|
||||
return $attendant;
|
||||
}
|
||||
|
||||
function write_attendants($attendant, $pbdb): void {
|
||||
function write_attendants(DOMElement $attendant, array $pbdb): void {
|
||||
$index = 1;
|
||||
foreach ($pbdb as $r) {
|
||||
$label = trim((string)($r['name'] ?? ''));
|
||||
@@ -215,34 +297,39 @@ function write_attendants($attendant, $pbdb): void {
|
||||
}
|
||||
}
|
||||
|
||||
function write_to_file($provision_dir, $out_file, $xml): void {
|
||||
if (!is_dir($provision_dir)) {
|
||||
fwrite(STDERR, "Provisioning directory not found: $provision_dir\n");
|
||||
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 = $out_file . '.tmp';
|
||||
$tmpfile = $file . '.tmp';
|
||||
if ($xml->save($tmpfile) === false) {
|
||||
fwrite(STDERR, "Failed to write temporary file $tmpfile\n");
|
||||
exit(3);
|
||||
}
|
||||
if (!@rename($tmpfile, $out_file)) {
|
||||
if (!@rename($tmpfile, $file)) {
|
||||
@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);
|
||||
}
|
||||
|
||||
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 {
|
||||
/** Optional: push check-sync to re-download directory without reboot */
|
||||
// Use the extension list we already built
|
||||
function notify(array $pbdb): void {
|
||||
$notified = 0;
|
||||
foreach ($pbdb as $r) {
|
||||
$ext = trim((string)$r['extension']);
|
||||
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) . "\"";
|
||||
exec($cmd, $o, $rc);
|
||||
if ($rc === 0) $notified++;
|
||||
|
||||
59
readme.md
Normal file
59
readme.md
Normal file
@@ -0,0 +1,59 @@
|
||||
## 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.
|
||||
|
||||
<br>
|
||||
|
||||
## Quickstart
|
||||
#### Config Fields
|
||||
`config.json` has several fields. The config file needs to be next to the script in order to be read.
|
||||
|
||||
<div style="border-left: 6px solid #FFA500; padding: 10px;">
|
||||
<strong>⚠️ Warning:</strong> Make sure the config follows proper json syntax.
|
||||
</div>
|
||||
<br>
|
||||
|
||||
```
|
||||
target_extensions
|
||||
list_filter_type
|
||||
extension_filter_list
|
||||
blacklisted_terms
|
||||
prepend_extensions
|
||||
```
|
||||
|
||||
1. Add the phones that need a config pushed out for to the `target_extensions` list.
|
||||
2. Add the type of filter you want to use to filter out extensions from the contact list to `list_filter_type`.
|
||||
3. Add Extensions you want either black/white listed to the `extension_filter_list`.
|
||||
4. Add words that are in contacts you want removed to `blacklisted_terms`.
|
||||
5. Add extra contacts you want added to the phones in `prepend_extensions`.
|
||||
|
||||
<br>
|
||||
|
||||
## Process Walkthrough
|
||||
|
||||
#### 1. Pull Contact List
|
||||
Initially the script formats together a contact list to be pushed to the phones. This php script will pull every contact from asterisk database which contains a **name** and **extension**.
|
||||
|
||||
#### 2. Contact List Formatting
|
||||
The script will now filter the contact list before writing to the phones. It has 3 processes to format it:
|
||||
|
||||
1. Filter extensions by black/white list
|
||||
2. Remove blacklisted terms
|
||||
3. Prepend a set of configured contacts listed
|
||||
|
||||
The filter list will apply either a black/white list to the `extension_filter_list`. If it is a white list `extension_filter_list` is kept and anything not in it is removed. If it is a blacklist anything inside of `extension_filter_list` is removed.
|
||||
|
||||
The script will remove any contact name that has words contained inside of `blacklisted_terms`.
|
||||
|
||||
Finally contacts specified in `prepend_extensions` are added to the beginning of the contact list.
|
||||
|
||||
#### 3. Write to phones
|
||||
Polycom phones read their extension list to display on the phone from a file in `/tftboot` that is served by PBXact. The phone looks for settings including its contact list in `/tftboot/phone_mac_address-features.cfg` replacing mac_address with the actual mac of the phone without any : in the number. The default if it cant find its mac is to use the file with 0s.
|
||||
|
||||
##### Example:
|
||||
```
|
||||
/tftboot/000000000-features.cfg
|
||||
```
|
||||
|
||||
The script will go down the list and read the .cfg for each phone in `target_extensions` and write in the contact list that was created earlier. It wites to a config file that matches the extensions mac address.
|
||||
|
||||
Phones might need to be told to reconfigure to pull the updated cfg.
|
||||
Reference in New Issue
Block a user