-
Notifications
You must be signed in to change notification settings - Fork 35
/
detect_switch.php
218 lines (209 loc) · 7.26 KB
/
detect_switch.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
require_once "event_socket_classes.php";
//检测Freeswitch相关信息
class detect_switch {
// cached data
protected $_data=array();
// event socket
public $event_host = 'localhost';
public $event_port = '8021';
public $event_password = 'ClueCon';
protected $event_socket;
protected $esl;
public function __construct($event_host="", $event_port="", $event_password="") {
//do not take these settings from session as they be detecting a new switch
if($event_host) $this->event_host = $event_host;
if($event_port) $this->event_port = $event_port;
if($event_password) $this->event_password = $event_password;
if (!$this->connect_event_socket())
die(' Error : Failed to use event socket ! 严重错误,没有连接上服务器 ');
}
protected function connect_event_socket(){
$options = array('persistentConnection' => 1); // 'timeOut' => 10, 'defaultTimeout' => 30,'blocking' => 1
$this->esl = new event_socket($this->event_host, $this->event_port, $this->event_password,$options);
if ($this->esl->isConnected())
return true;
else
return false;
}
public function get_api_reply($cmd){
return $this->esl->getResponse($this->esl->api($cmd));
}
//根据提供的服务器id(fs_setting表)获取相关信息并保存入库
public function get_switch_info($sid){
if(!$this->esl->isConnected()){
die(' Error : Failed to use event socket ! 严重错误,当前并没有连接服务器 ');
}
global $mysqli;
if (!is_object($mysqli)){
die(' Error : Failed to open DB ! 严重错误,没有连接到数据库 ');
}
$FS_Version = $this->esl->getResponse($this->esl->version());
$FS_Vars = $this->esl->getResponse($this->esl->global_getvar());
$conf_dir = $core_uuid=$recordings_dir=$log_dir=$external_sip_port=$internal_sip_port=0;
foreach (explode("\n",$FS_Vars) as $FS_Var){
$var = explode("=", $FS_Var);
$var[0] = trim($var[0]);
if (isset($var[1])){
if ($var[0]=='core_uuid')
$core_uuid = $var[1];
elseif($var[0]=='sounds_dir')
$sounds_dir = $var[1];
elseif($var[0]=='recordings_dir')
$recordings_dir = $var[1];
elseif($var[0]=='conf_dir')
$conf_dir = $var[1];
elseif($var[0]=='log_dir')
$log_dir = $var[1];
elseif($var[0]=='external_sip_port')
$external_sip_port = $var[1];
elseif($var[0]=='internal_sip_port')
$internal_sip_port = $var[1];
elseif($var[0]=='default_provider')
$default_provider = $var[1];
}
}
if ($conf_dir){
$sql ="update fs_setting set `version` ='$FS_Version',`core_uuid`='$core_uuid', `recordings_dir`='$recordings_dir', `sounds_dir`='$sounds_dir', `default_provider`='$default_provider',`conf_dir`='$conf_dir',`log_dir`='$log_dir',`external_sip_port`='$external_sip_port',`internal_sip_port`='$internal_sip_port' where id = $sid limit 1";
$return = $mysqli->query($sql);
if ($return)
return true;
else
return $sql;
}else return false;
}
public function list_switch_info(){
if(!$this->esl->isConnected()){
die(' Error : Failed to use event socket ! 严重错误,当前并没有连接服务器 ');
}
echo "<ul>Freeswitch 配置信息";
$FS_Vars = $this->esl->getResponse($this->esl->status());
foreach (explode("\n",$FS_Vars) as $FS_Var){
if ($FS_Var){
$var = explode("=", $FS_Var);
if (isset($var[1]))
$this->_data[$var[0]] = $var[1];
else
$this->_data[] = $FS_Var;
}
}
echo "<p class='pleft'> == 状态信息 status ==</p>";
foreach ($this->_data as $k=>$v){
echo "<li> $v</li>";
}
$this->_data = array();
$FS_Vars = $this->get_api_reply("show modules");
foreach (explode("\n",$FS_Vars) as $k=>$FS_Var){
if ($k && $FS_Var){ //忽略第一行和空行
$var = explode(",", $FS_Var);
if (isset($var[3]))
$this->_data[$var[0]][$var[3]][$var[2]][] = $var[1];
}
}
$check = ["mod_commands"=>1,"mod_callcenter"=>1,"mod_dptools"=>1,"mod_console"=>1,"mod_curl"=>1,"mod_db"=>1,"mod_event_socket"=>1,"mod_hash"=>1,"mod_local_stream"=>1,"mod_sofia"=>1,"mod_xml_cdr"=>1];
$str = "<p class='pleft'> == 模块信息 modules ==</p>";
foreach ($this->_data as $k=>$v){
$str .= "<p class=pcenter><span class='bgblue bold16'> $k </span></p>";
foreach ($v as $k1=>$v1){
$str .= "<li><b>$k1 :</b></li>";
foreach ($v1 as $k2=>$v2){
if (isset($check[$k2])) unset($check[$k2]);
$str .= "<span class='bggreen bold12'> $k2 </span>".implode(" 、", $v2);
}
}
}
if (count($check)){
echo "<p class='pcenter bgred bold14'>Freeswitch问题:必须修正!!缺失模块:".implode(" 、", array_keys($check))."</p>";
}
echo $str;
$this->_data = array();
$FS_Vars = $this->esl->getResponse($this->esl->global_getvar());
foreach (explode("\n",$FS_Vars) as $FS_Var){
$var = explode("=", $FS_Var);
if (isset($var[1]))
$this->_data[$var[0]] = $var[1];
else
$this->_data[] = $FS_Var;
}
echo "<p class='pleft'> == 全局变量 global_getvar ==</p>";
foreach ($this->_data as $k=>$v){
if (is_numeric($k))
echo "<li> $v</li>";
else
echo "<li><b>$k :</b> $v</li>";
}
$this->_data = array();
$FS_Vars = $this->esl->getResponse($this->esl->sofia('status'));
echo "<p class='pleft'> == sofia状态 sofia status ==</p><pre>";
echo $FS_Vars;
echo "</pre></ul>";
}
public function restart_switch(){
$FS_Vars = $this->esl->getResponse($this->esl->restart());
foreach (explode("\n",$FS_Vars) as $FS_Var){
$var = explode("=", $FS_Var);
if (isset($var[1]))
$this->_data[$var[0]] = $var[1];
else
$this->_data[] = $FS_Var;
}
echo "<ul>重新启动FreeSwitch:";
foreach ($this->_data as $k=>$v){
if (is_numeric($k))
echo "<li> $v</li>";
else
echo "<li><b>$k :</b> $v</li>";
}
echo "</ul>";
}
/*
*调用ESL类的方法执行相关ESL指令并获取显示结果
*$cmd 需要执行的指令,如 api bgapi 等
*$args 相关参数,用空格分开
*$returnhtml 返回显示结果的格式,0 为显示文本 1为显示html 2为返回结果变量
*$isxml 反馈信息是否为xml格式,默认0
*/
public function run($cmd,$args='',$returnhtml=1,$isxml = 0){
if (empty($cmd))
return false;
$this->_data = [];
$FS_Vars = $this->esl->getResponse($this->esl->$cmd($args));
if ($isxml){
$xml = simplexml_load_string($FS_Vars);
if (!$xml)
die("无法读取XML!");
foreach ($xml as $k=>$v){
$this->_data[$k] = $v;
};
}else
foreach (explode("\n",$FS_Vars) as $FS_Var){
$var = explode("=", $FS_Var);
if (isset($var[1]))
$this->_data[$var[0]] = $var[1];
else
$this->_data[] = $FS_Var;
}
if ($returnhtml==1){
echo "<ul>$cmd $args 反馈信息:";
foreach ($this->_data as $k=>$v){
if (is_numeric($k))
echo "<li> $v</li>";
else
echo "<li><b>$k :</b> $v</li>";
}
echo "</ul>";
}elseif ($returnhtml==2){
return $this->_data;
}else{
echo "$cmd $args 反馈信息:\n";
foreach ($this->_data as $k=>$v){
if (is_numeric($k))
echo "[ $v ]\n";
else
echo "[ $k : ] $v\n";
}
echo "\n";
}
}
}
?>