#!/usr/bin/php -q 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 trim_db($pbdb): array { array_unshift($pbdb, [ 'name' => 'Night Hours', 'extension' => '*271', ]); $filtered = array_filter($pbdb, function ($item) { if (!is_array($item) || !isset($item['name'])) { return true; } $name = ltrim($item['name']); return stripos($name, 'inpatient') !== 0; }); return $filtered; } function pull_mac_list(): array { $db = FreePBX::Database(); $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_extension_ini(): array { $extensions = parse_ini_file('extensions.ini'); $ext_list = array_map('intval', explode(',', $extensions['extensions'])); return $ext_list; } function pull_xml_file($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($xml): DOMElement { $xpath = new DOMXPath($xml); $attendantNodes = $xpath->query('/polycomConfig/attendant'); if ($attendantNodes->length === 0) { throw new RuntimeException("No 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($attendant, $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($provision_dir, $file, $xml): void { 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($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();