Compare commits

...

11 Commits

Author SHA1 Message Date
190c848aa9 Merge pull request 'reamde' (#1) from main into dev
Reviewed-on: #1
2026-02-02 14:57:24 -06:00
fd9239c2bb reamde 2026-02-02 14:55:05 -06:00
3d880b6699 removed null returns 2026-02-02 11:28:28 -06:00
69c5a88c50 removed return tag 2026-02-02 11:27:13 -06:00
598af2c954 bool reutnr type removed 2026-02-02 11:25:57 -06:00
bd9f4c074d fixed bool 2026-02-02 11:24:58 -06:00
8ebdcb7d1d return type tagging 2026-02-02 11:23:00 -06:00
9e7ec4fc52 extra array config fucnt 2026-02-02 11:06:56 -06:00
d98e31faca scalars only in read config 2026-02-02 10:58:05 -06:00
f96abcbb4e fixed config call 2026-02-02 10:52:50 -06:00
c89e1da002 convert to json 2026-02-02 10:47:17 -06:00
4 changed files with 188 additions and 46 deletions

View File

@@ -1,4 +0,0 @@
target_extensions = 334, 338
list_filter_type = whitelist
extension_filter_list = 404, 911
blacklisted_terms = vesibule, inpatient

46
config.json Normal file
View 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"}
]
}

View File

@@ -1,16 +1,22 @@
#!/usr/bin/php -q #!/usr/bin/php -q
<?php <?php
$TARGET_EXTENSIONS = read_config_ini("target_extensions");
$LIST_FILTER_TYPE = read_config_ini("list_filter_type")[0];
$EXTENSION_FILTER_LIST = read_config_ini("extension_filter_list");
$BLACKLISTED_TERMS = read_config_ini("blacklisted_terms");
$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();
$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 { function main(): void {
global $TARGET_EXTENSIONS; global $TARGET_EXTENSIONS;
global $PROVISION_DIR; global $PROVISION_DIR;
@@ -20,9 +26,9 @@ function main(): void {
$pbdb = pull_db(); $pbdb = pull_db();
prepend_contact_list($pbdb);
blacklist_terms($pbdb); blacklist_terms($pbdb);
filter_extensions($pbdb); filter_extensions($pbdb);
prepend_contact_list($pbdb);
$mac_list = pull_mac_list(); $mac_list = pull_mac_list();
@@ -35,8 +41,8 @@ function main(): void {
$file = $PROVISION_DIR . '/' . $mac . '-features.cfg'; $file = $PROVISION_DIR . '/' . $mac . '-features.cfg';
$xml = pull_xml_file($file); $xml = pull_xml_file($file);
$attendant = remove_attendants($xml); $attendants = remove_attendants($xml);
write_attendants($attendant, $pbdb); write_attendants($attendants, $pbdb);
write_to_file($file, $xml); write_to_file($file, $xml);
} }
@@ -45,13 +51,12 @@ function main(): void {
} }
} }
function pull_db(): array { function pull_db(): array {
$db = FreePBX::Database(); global $DB;
$pbdb = []; $pbdb = [];
try { try {
$stmt = $db->prepare(" $stmt = $DB->prepare("
SELECT name, extension SELECT name, extension
FROM users FROM users
WHERE extension REGEXP '^[0-9]+$' WHERE extension REGEXP '^[0-9]+$'
@@ -73,7 +78,7 @@ function pull_db(): array {
return $pbdb; return $pbdb;
} }
function filter_extensions(&$pbdb) { function filter_extensions(array &$pbdb) {
global $LIST_FILTER_TYPE; global $LIST_FILTER_TYPE;
if ($LIST_FILTER_TYPE == "whitelist") { if ($LIST_FILTER_TYPE == "whitelist") {
@@ -86,7 +91,7 @@ function filter_extensions(&$pbdb) {
} }
} }
function blacklist_terms(&$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) {
@@ -105,7 +110,7 @@ function blacklist_terms(&$pbdb) {
})); }));
} }
function whitelist_extension_filter(&$pbdb) { function whitelist_extension_filter(array &$pbdb) {
global $EXTENSION_FILTER_LIST; global $EXTENSION_FILTER_LIST;
$allowed = array_fill_keys( $allowed = array_fill_keys(
@@ -123,7 +128,7 @@ function whitelist_extension_filter(&$pbdb) {
})); }));
} }
function blacklist_extension_filter(&$pbdb) { function blacklist_extension_filter(array &$pbdb) {
global $EXTENSION_FILTER_LIST; global $EXTENSION_FILTER_LIST;
$blocked = array_fill_keys( $blocked = array_fill_keys(
@@ -141,24 +146,17 @@ function blacklist_extension_filter(&$pbdb) {
})); }));
} }
function prepend_contact_list(&$pbdb) { function prepend_contact_list(array &$pbdb) {
$pbdb_prepend = [ global $PREPEND_EXTENSIONS;
[ 'name' => 'Night Hours', 'extension' => '*271', ], array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
[ 'name' => 'Overhead Page', 'extension' => '900', ],
[ 'name' => 'All page', 'extension' => '300'],
[ 'name' => 'Park 71', 'extension' => '71'],
[ 'name' => 'Park 72', 'extension' => '72'],
];
array_unshift($pbdb, ...$pbdb_prepend);
} }
function pull_mac_list(): array { function pull_mac_list(): array {
$db = FreePBX::Database(); global $DB;
$mac_db = []; $mac_db = [];
try { try {
$stmt = $db->prepare(" $stmt = $DB->prepare("
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
FROM endpoint_extensions FROM endpoint_extensions
ORDER BY CAST(ext AS UNSIGNED) ORDER BY CAST(ext AS UNSIGNED)
@@ -171,21 +169,64 @@ function pull_mac_list(): array {
return array_column($mac_db, 'mac', 'ext'); return array_column($mac_db, 'mac', 'ext');
} }
function read_config_ini($CONFIG_KEY): array {
$config = parse_ini_file('config.ini');
$config_value = preg_split('/\s*,\s*/', $config[$CONFIG_KEY] ?? '', -1, PREG_SPLIT_NO_EMPTY);
$config_value = array_map( function read_config_json_file(string $path): array {
fn($v) => strtolower(trim((string)$v)), $json = file_get_contents($path);
$config_value $data = json_decode($json, true);
);
$config_value = array_values(array_filter($config_value, fn($v) => $v !== '')); if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("JSON decode error: " . json_last_error_msg());
return $config_value; }
return $data;
} }
function pull_xml_file($file): DOMDocument { 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)) { if (!file_exists($file)) {
$file = '/tftpboot/000000000000-features.cfg'; $file = '/tftpboot/000000000000-features.cfg';
} }
@@ -220,7 +261,7 @@ function pull_xml_file($file): DOMDocument {
return $xml; return $xml;
} }
function remove_attendants($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) {
@@ -241,7 +282,7 @@ function remove_attendants($xml): DOMElement {
return $attendant; return $attendant;
} }
function write_attendants($attendant, $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'] ?? ''));
@@ -256,7 +297,7 @@ function write_attendants($attendant, $pbdb): void {
} }
} }
function write_to_file($file, $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");
@@ -284,7 +325,7 @@ function write_to_file($file, $xml): void {
echo "Wrote $file \n"; echo "Wrote $file \n";
} }
function notify($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']);

59
readme.md Normal file
View 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.