forked from libyerman/queue-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmanager.php
154 lines (132 loc) · 5.06 KB
/
asmanager.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
Copyright 2007, 2008 Nicolás Gudiño
This file is part of Asternic Call Center Stats.
Asternic Call Center Stats is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Asternic Call Center Stats is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Asternic Call Center Stats. If not, see
<http://www.gnu.org/licenses/>.
*/
// AMI Class based on phpagi-asmanager.php by
// Matthew Asham <[email protected]>
class AsteriskManager
{
var $socket = NULL;
var $server;
var $port;
function AsteriskManager() { }
function send_request($action, $parameters=array()) {
$command = "Action: $action\r\n";
foreach($parameters as $var=>$val) {
$command .= "$var: $val\r\n";
}
$command .= "\r\n";
fwrite($this->socket, $command);
return $this->get_response(true);
}
function get_response($allow_timeout=false) {
$timeout = false;
do {
$type = NULL;
$parameters = array();
if (feof($this->socket)) {
return false;
}
$buffer = trim(fgets($this->socket, 4096));
while($buffer != '') {
$a = strpos($buffer, ':');
if($a) {
if(!count($parameters)) { // first line in a response?
$type = strtolower(substr($buffer, 0, $a));
if(substr($buffer, $a + 2) == 'Follows') {
// A follows response means there is a multiline field that follows.
$parameters['data'] = '';
$buff = fgets($this->socket, 4096);
while(substr($buff, 0, 6) != '--END ') {
$parameters['data'] .= $buff;
$buff = fgets($this->socket, 4096);
}
}
}
// store parameter in $parameters
$parameters[substr($buffer, 0, $a)] = substr($buffer, $a + 2);
}
$buffer = trim(fgets($this->socket, 4096));
}
// process response
switch($type) {
case '': // timeout occured
$timeout = $allow_timeout;
break;
case 'event':
// Process event with $parameters ?
break;
case 'response':
break;
default:
// $this->log('Unhandled response packet from Manager: ' . print_r($parameters, true));
break;
}
} while($type != 'response' && !$timeout);
return $parameters;
}
function connect($server='172.16.200.1', $username='qpanel', $secret='qpanelR3p0RT') {
// Extract port if specified
if(strpos($server, ':') !== false) {
$parts = explode(':', $server);
$this->server = $parts[0];
$this->port = $parts[1];
} else {
$this->server = $server;
$this->port = "5038";
}
$errno = $errstr = NULL;
$this->socket = @fsockopen($this->server, $this->port, $errno, $errstr);
if(!$this->socket) {
$this->log("Unable to connect to manager {$this->server}:{$this->port} ($errno): $errstr");
return false;
}
// read the header
$str = fgets($this->socket);
if($str == false) {
$this->log("Asterisk Manager header not received.");
return false;
}
// login
$res = $this->send_request('login', array('Username'=>$username, 'Secret'=>$secret));
if(false) {
$this->log("Failed to login.");
$this->disconnect();
return false;
}
return true;
}
function disconnect() {
$this->logoff();
fclose($this->socket);
}
function Command($command) {
return $this->send_request('Command', array('Command'=>$command));
}
function ExtensionState($exten, $context='', $actionid='') {
return $this->send_request('ExtensionState', array('Exten'=>$exten, 'Context'=>$context, 'ActionID'=>$actionid));
}
function Hangup($channel) {
return $this->send_request('Hangup', array('Channel'=>$channel));
}
function Logoff() {
return $this->send_request('Logoff');
}
function log($message, $level=1)
{
error_log(date('r') . ' - ' . $message);
}
}
?>