-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteriskAmi.php
136 lines (104 loc) · 3.64 KB
/
AsteriskAmi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace App\Models;
class AsteriskAmi {
private $socket;
private $host;
private $port;
private $username;
private $password;
public function __construct($host, $port, $username, $password){
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
}
public function connect(){
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 10);
if (!$this->socket)
throw new \Exception("Bağlantı hatası: $errstr ($errno)");
$this->sendCommand("Action: Login\r\n");
$this->sendCommand("Username: " . $this->username . "\r\n");
$this->sendCommand("Secret: " . $this->password . "\r\n\r\n");
}
public function sendCommand($command) {
fwrite($this->socket, $command);
}
/**
* @param $extension
* @param $targetPhone
* @param $callerID
* @param $context
* @param $priority
* @param $timeout
*/
public function originateCall($extension, $targetPhone, $callerID, $context, $priority = 1, $timeout = 10000){
$this->sendCommand("Action: Originate\r\n");
$this->sendCommand("Channel: $extension\r\n");
$this->sendCommand("Exten: $targetPhone\r\n");
$this->sendCommand("CallerID: $callerID\r\n");
$this->sendCommand("Context: $context\r\n");
$this->sendCommand("Priority: $priority\r\n");
$this->sendCommand("Timeout: $timeout\r\n\r\n");
$response = "";
while (!feof($this->socket)) {
$response .= fgets($this->socket, 1024);
if (strpos($response, 'Message: Originate successfully queued')) {
$this->logoff();
return [
"status" => "success",
"message" => "Originate successfully queued",
"response" => $response
];
}
if (strpos($response, 'Message: Originate failed') || strpos($response, 'Channel: OutgoingSpoolFailed')) {
$this->logoff();
return [
"status" => "error",
"message" => "Originate failed",
"response" => $response
];
}
}
return [
"status" => "error",
"message" => "Process failed",
"response" => null
];
}
/**
* @param $monitoringChannel
* @param $targetChannel
* @param $SpyMode
* @param $timeout
* @return array
*/
public function spyCall($monitoringChannel = "SIP/extension", $targetChannel = "SIP/extension"){
$this->sendCommand("Action: Originate\r\n");
$this->sendCommand("Channel: $monitoringChannel\r\n");
$this->sendCommand("Application: ChanSpy\r\n");
$this->sendCommand("Data: $targetChannel,q\r\n");
$this->sendCommand("Async: yes\r\n");
$response = "";
while (!feof($this->socket)) {
$line = fgets($this->socket, 1024);
$response .= $line;
if (stripos($line, 'Response: Success') !== false) {
$this->logoff();
return [
"status" => "success",
"message" => "CanSpy success",
"response" => $response
];
}
}
return [
"status" => "error",
"message" => "Process failed",
"response" => null
];
}
public function logoff(){
$this->sendCommand("Action: Logoff\r\n\r\n");
fclose($this->socket);
}
}