diff --git a/public/api.php b/public/api.php index 932a990..4e64a02 100644 --- a/public/api.php +++ b/public/api.php @@ -432,6 +432,82 @@ try { ]); break; + case 'check_extension_call_status': + $extension = trim((string)($payload['extension'] ?? '')); + if ($extension === '') { + throw new InvalidArgumentException("Extension requise."); + } + + $inCall = false; + $callDetails = ''; + try { + $ami = AmiClient::fromDatabase($db); + $rawChannels = $ami->runCommand("core show channels concise"); + $channelLines = array_filter(explode("\n", trim($rawChannels))); + foreach ($channelLines as $cline) { + $parts = explode('!', trim($cline)); + $chanName = $parts[0] ?? ''; + if (preg_match('#^PJSIP/' . preg_quote($extension, '#') . '[-/]#i', $chanName)) { + $inCall = true; + $state = $parts[4] ?? 'Up'; + $callerId = $parts[7] ?? ''; + $duration = $parts[10] ?? ''; + $callDetails = "Canal : {$chanName}"; + if ($callerId !== '') { + $callDetails .= " • Interlocuteur : {$callerId}"; + } + if ($duration !== '') { + $callDetails .= " • Durée : {$duration}"; + } + break; + } + } + + if (!$inCall) { + $rawHints = $ami->runCommand("core show hints {$extension}"); + if (preg_match('/' . preg_quote($extension, '/') . '@[^\n]*State:(InUse|Busy|Ringing)/i', $rawHints, $m)) { + $inCall = true; + $callDetails = "Ligne active : " . ($m[1] === 'Ringing' ? 'En sonnerie' : 'En communication'); + } + } + } catch (\Throwable $e) { + // AMI indisponible + } + + echo json_encode([ + 'success' => true, + 'extension' => $extension, + 'in_call' => $inCall, + 'call_details' => $callDetails, + ]); + break; + + case 'reboot_extension': + $extension = trim((string)($payload['extension'] ?? '')); + if ($extension === '') { + throw new InvalidArgumentException("Extension requise."); + } + + $stmt = $db->prepare('SELECT extension, name FROM extensions WHERE extension = :ext'); + $stmt->execute([':ext' => $extension]); + $extRow = $stmt->fetch(); + if (!$extRow) { + throw new InvalidArgumentException("Poste {$extension} introuvable."); + } + + try { + $ami = AmiClient::fromDatabase($db); + $cmdOutput = $ami->runCommand("pjsip send notify reboot endpoint " . escapeshellarg($extension)); + echo json_encode([ + 'success' => true, + 'message' => "Ordre de redémarrage envoyé au poste {$extension} ({$extRow['name']}).", + 'details' => trim($cmdOutput), + ]); + } catch (\Throwable $e) { + throw new RuntimeException("Erreur lors de l'envoi de la commande de redémarrage : " . $e->getMessage()); + } + break; + case 'save_group': $id = isset($payload['id']) && $payload['id'] !== '' ? (int)$payload['id'] : null; $extension = trim((string)($payload['extension'] ?? '')); diff --git a/public/assets/app.js b/public/assets/app.js index 47e64fc..7ade917 100644 --- a/public/assets/app.js +++ b/public/assets/app.js @@ -951,9 +951,10 @@ function renderExtensionsTable() {
${escapeHtml(ext.secret)}