-
Notifications
You must be signed in to change notification settings - Fork 35
/
es_listener.php
188 lines (160 loc) · 4.76 KB
/
es_listener.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
set_time_limit(300);
session_start();
date_default_timezone_set('Asia/Shanghai');
ob_start(); //打开输出缓冲区
ob_end_flush();
ob_implicit_flush(1); //立即输出
ob_flush();
class FreeSwitchEventListener
{
var $password = "ClueCon";
var $port = "8021";
var $host = "127.0.0.1";
var $limit = 100000;
var $fp = null ;
var $iRetryCurrentNumber = 0 ;
var $iRetryMaxNumber = 10 ;
function __construct($host = null, $port = null , $password = null)
{
if( !is_null( $host) ) {
$this->setHost( $host) ;
}
if( !is_null( $port) ) {
$this->setPort( $port) ;
}
if( !is_null( $password) ) {
$this->setPassword( $password) ;
}
return $this ;
}
public function setHost( $host ){
$this->host = $host ;
return $this;
}
public function setPort( $port ){
$this->port = $port ;
return $this;
}
public function setPassword( $password ){
$this->password = $password ;
return $this;
}
private function event_socket_create() {
try {
$this->fp = @fsockopen($this->host, $this->port, $errno, $errdesc) ;
} catch (Exception $e) {
die("【event_socket_create】fail to connection! $e");
}
if (!$this->fp) {
echo "<li>【event_socket_create】{$this->host} 连接失败 重试{$this->iRetryCurrentNumber}</li>";
if( $this->iRetryCurrentNumber < $this->iRetryMaxNumber ){
$this->iRetryCurrentNumber++ ;
sleep(1);
return $this->event_socket_create() ;
}else{
die("【event_socket_create】Connection to $this->host failed");
}
}
echo "<li>【event_socket_create】{$this->host} 连接成功 ****</li>";
$this->iRetryCurrentNumber = 0 ;
socket_set_blocking($this->fp,false);
if ($this->fp) {
while (!feof($this->fp)) {
$buffer = fgets($this->fp,10240);
usleep(50); //allow time for reponse
if (trim($buffer) == "Content-Type: auth/request") {
echo "<li>【event_socket_create】$buffer >>> send auth >>></li>";
fputs($this->fp, "auth $this->password\n\n");
break;
}else
echo "<li>【event_socket_create】".urldecode($buffer)."</li>";
}
return $this->fp;
}else {
return false;
}
}
public function event_socket_close() {
$this->fp->close();
}
public function event_socket_request( $cmd ) {
if( is_null( $this->fp ) ){
$this->event_socket_create();
}
if ($this->fp) {
echo "<ul>【event_socket_request】$cmd ";
fputs($this->fp, $cmd."\n\n");
usleep(100); //allow time for response
$response = '';
$length = 0;
$x = 0;
while (!feof($this->fp) )
{
$x++;
usleep(100);
$theNewData = stream_get_line($this->fp, 10240, "\n");
if ($theNewData){
$response = urldecode(trim($theNewData));
echo "<li>【event_socket_request $x 】$response</li>";
$response = $this->getJsonReponseClean( $response ) ;
if(is_array($response)){
if (!empty($response['result'])){
}
}
}
if ($x > $this->limit){
echo "<li>【event_socket_request $x 】 达到限制计数 停止!! </li>";
break;
}
}
$this->fp = null;
echo "</ul>";
}else {
echo "<li>【event_socket_request】no handle</li>";
}
}
private function getJsonReponseClean( $response ){
$start = strpos($response,"{");
if ($start === false)
return false;
$end = strrpos($response,"}");
if ($end === false)
return false;
$response = substr($response, $start, $end+1 );
try {
$response = json_decode($response, true ) ;
} catch (Exception $e) {
return false ;
}
if( is_array( $response ) ){
return $response ;
}
return false ;
}
}
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type content=text/html;charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="main.css"/><script type="text/javascript" src="jquery.js"></script>
</head><body>
HTML;
flush();
// The command to send to FreeSWITCH
if (!empty($_GET['cmd']))
$cmd = $_GET['cmd'];
else
$cmd = "event plain ALL";
if (!empty($_SESSION['ESL_HOST'])){
define("ESL_HOST", $_SESSION['ESL_HOST']);
define("ESL_PORT", $_SESSION['ESL_PORT']);
define("ESL_PASSWORD",$_SESSION['ESL_PASSWORD']);
$myFSEventListener = new FreeSwitchEventListener() ;
$myFSEventListener->setHost(ESL_HOST) ;
$myFSEventListener->setPort(ESL_PORT);
$myFSEventListener->setPassword(ESL_PASSWORD);
$myFSEventListener->event_socket_request($cmd);
}else
echo "<p class=pcenter>请使用 <a href='esl_cmd.php'>ESL 命令工具</a></p> ";
echo "</body></html";