# 📞 Softphone PHP ARI

Softphone web moderne utilisant l'**Asterisk REST Interface (ARI)** — contrôle des appels via REST + événements temps réel via WebSocket.

---

## 🏗 Architecture

```
softphone/
├── config/
│   ├── config.php               ← Variables de configuration
│   └── asterisk.conf.example    ← Config Asterisk (ari.conf, pjsip.conf…)
├── src/
│   └── AriClient.php            ← Client PHP ARI (REST)
├── public/
│   ├── index.html               ← Interface Softphone (HTML/JS)
│   └── api.php                  ← Proxy AJAX → ARI REST
└── README.md
```

---

## 🚀 Installation rapide

### 1. Prérequis

- PHP 7.4+ avec `curl` activé
- Asterisk 16+ avec modules ARI et PJSIP
- Serveur web (Apache / Nginx / PHP built-in)

### 2. Configurer Asterisk

Copier et adapter les configs depuis `config/asterisk.conf.example` :

```bash
# Activer ARI
sudo nano /etc/asterisk/ari.conf

# Activer HTTP
sudo nano /etc/asterisk/http.conf

# Configurer le dialplan
sudo nano /etc/asterisk/extensions.conf

sudo asterisk -rx "core reload"
```

Vérifier que ARI répond :
```bash
curl http://asterisk:asterisk@127.0.0.1:8088/ari/asterisk/info
```

### 3. Configurer le Softphone

Éditer `config/config.php` ou définir les variables d'environnement :

```bash
export ASTERISK_HOST=127.0.0.1
export ASTERISK_PORT=8088
export ASTERISK_USER=asterisk
export ASTERISK_PASS=asterisk
```

### 4. Lancer

```bash
cd softphone/public
php -S 0.0.0.0:8080
```

Ouvrir `http://localhost:8080` dans un navigateur.

---

## 🔧 Fonctionnalités ARI

| Fonctionnalité | API ARI | Status |
|---|---|---|
| Originer un appel | `POST /channels` | ✅ |
| Raccrocher | `DELETE /channels/{id}` | ✅ |
| Répondre | `POST /channels/{id}/answer` | ✅ |
| Mettre en attente | `POST /channels/{id}/hold` | ✅ |
| Couper le micro | `POST /channels/{id}/mute` | ✅ |
| Envoyer DTMF | `POST /channels/{id}/dtmf` | ✅ |
| Transfert + Bridge | `POST /bridges` | ✅ |
| Lire un son | `POST /channels/{id}/play` | ✅ |
| Enregistrer | `POST /channels/{id}/record` | ✅ |
| Événements temps réel | WebSocket `/ari/events` | ✅ |

---

## 🌐 API PHP interne (`api.php`)

| Action | Description |
|---|---|
| `status` | Statut Asterisk + URL WebSocket |
| `channels` | Liste des canaux actifs |
| `originate` | Créer un appel |
| `hangup` | Raccrocher |
| `answer` | Répondre |
| `hold` / `mute` | Attente / Muet |
| `dtmf` | Envoyer DTMF |
| `transfer` | Transfert via Bridge |
| `bridges` | Liste des bridges |
| `create_bridge` | Créer un bridge |
| `play` | Jouer un fichier audio |
| `record` / `stop_record` | Enregistrement |
| `recordings` | Liste enregistrements |
| `endpoints` | Liste endpoints SIP |
| `ws_config` | Config WebSocket |

---

## 📡 WebSocket ARI

La connexion WebSocket se fait directement depuis le navigateur :

```
ws://ASTERISK_HOST:8088/ari/events?api_key=user:pass&app=softphone
```

Événements gérés : `StasisStart`, `StasisEnd`, `ChannelStateChange`,
`ChannelDtmfReceived`, `RecordingStarted`, `RecordingFinished`,
`BridgeCreated`, `ChannelEnteredBridge`.

---

## 🔒 Sécurité (production)

- Changer le mot de passe ARI (`ari.conf`)
- Placer Asterisk derrière un pare-feu (port 8088 non exposé)
- Utiliser HTTPS + WSS (reverse proxy Nginx/Caddy)
- Ajouter une authentification sur `api.php`
- Utiliser des variables d'environnement pour les credentials
