convert to json
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
#!/usr/bin/php -q
|
||||
<?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';
|
||||
|
||||
$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("prepend_extensions");
|
||||
|
||||
function main(): void {
|
||||
global $TARGET_EXTENSIONS;
|
||||
global $PROVISION_DIR;
|
||||
@@ -45,13 +51,12 @@ function main(): void {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function pull_db(): array {
|
||||
$db = FreePBX::Database();
|
||||
global $DB;
|
||||
$pbdb = [];
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
$stmt = $DB->prepare("
|
||||
SELECT name, extension
|
||||
FROM users
|
||||
WHERE extension REGEXP '^[0-9]+$'
|
||||
@@ -142,23 +147,16 @@ function blacklist_extension_filter(&$pbdb) {
|
||||
}
|
||||
|
||||
function prepend_contact_list(&$pbdb) {
|
||||
$pbdb_prepend = [
|
||||
[ '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'],
|
||||
];
|
||||
|
||||
array_unshift($pbdb, ...$pbdb_prepend);
|
||||
global $PREPEND_EXTENSIONS;
|
||||
array_unshift($pbdb, ...$PREPEND_EXTENSIONS);
|
||||
}
|
||||
|
||||
function pull_mac_list(): array {
|
||||
$db = FreePBX::Database();
|
||||
global $DB;
|
||||
$mac_db = [];
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
$stmt = $DB->prepare("
|
||||
SELECT mac, SUBSTRING_INDEX(ext, '-', 1) AS ext
|
||||
FROM endpoint_extensions
|
||||
ORDER BY CAST(ext AS UNSIGNED)
|
||||
@@ -171,20 +169,54 @@ function pull_mac_list(): array {
|
||||
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(
|
||||
fn($v) => strtolower(trim((string)$v)),
|
||||
$config_value
|
||||
);
|
||||
function read_config_json_file(string $path): array {
|
||||
global $COFNIG_PATH;
|
||||
if (!is_file($path)) {
|
||||
throw new RuntimeException("Config file not found: {$path}");
|
||||
}
|
||||
|
||||
$config_value = array_values(array_filter($config_value, fn($v) => $v !== ''));
|
||||
$raw = file_get_contents($path);
|
||||
if ($raw === false) {
|
||||
throw new RuntimeException("Unable to read config file: {$path}");
|
||||
}
|
||||
|
||||
return $config_value;
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException("Invalid JSON in config file: {$path}");
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
function read_config_json($key) {
|
||||
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_map(
|
||||
fn($v) => strtolower(trim((string)$v)),
|
||||
$items
|
||||
);
|
||||
|
||||
$items = array_values(array_filter($items, fn($v) => $v !== ''));
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
function read_config_json_string(array $config, string $key): string {
|
||||
$value = $config[$key] ?? '';
|
||||
return strtolower(trim((string)$value));
|
||||
}
|
||||
|
||||
|
||||
function pull_xml_file($file): DOMDocument {
|
||||
if (!file_exists($file)) {
|
||||
$file = '/tftpboot/000000000000-features.cfg';
|
||||
|
||||
Reference in New Issue
Block a user