set([
'worker_num' => 1,
]);
$conf = [
'host' => config('freeswitch.esl.host'),
'port' => config('freeswitch.esl.port'),
'password' => config('freeswitch.esl.password'),
'fs_cli_bin' => '/usr/local/freeswitch/bin/fs_cli',
'gateway' => '/usr/local/freeswitch/conf/sip_profiles/external/',
'directory' => '/usr/local/freeswitch/conf/directory/default/',
//拨号计划下面默认分为default(呼出)和public(呼入)
'dialplan' => '/usr/local/freeswitch/conf/dialplan/',
'callcenter' => '/usr/local/freeswitch/conf/autoload_configs/callcenter.conf.xml'
];
$http->on('request', function ($request, $response) use ($conf) {
if($request->server['request_method'] == 'POST'){
$data = json_decode($request->getContent(),true);
$uri = $request->server['request_uri'];
$return = ['code'=>1,'msg'=>''];
$command = $conf['fs_cli_bin']." -H ".$conf['host']." -P ".$conf['port']." -p ".$conf['password']." -x ";
try{
switch($uri){
case '/gateway': //网关
//清空所有文件
array_map('unlink', glob($conf['gateway']."*"));
//再写入
foreach ($data as $d){
$xml = "\n";
$xml .= "\t\n";
$xml .= "\t\t\n";
if ($d['type'] == 1){ //sip对接
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
}else{ //ip对接
$xml .= "\t\t\n";
$xml .= "\t\t\n";
}
$xml .= "\t\t\n";
$xml .= "\t\n";
$xml .= "";
file_put_contents($conf['gateway']."gw".$d['id'].".xml",$xml);
}
exec($command."\""."sofia profile external restart"."\"");
$return = ['code'=>0,'msg'=>'网关更新成功'];
break;
case '/directory': //分机
//清空所有文件
array_map('unlink', glob($conf['directory']."*"));
//再写入
foreach($data as $d){
$xml = "\n";
$xml .= "\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\n";
$xml .= "\n";
file_put_contents($conf['directory'].$d['username'].".xml",$xml);
}
exec($command."\""."reloadxml"."\"");
$return = ['code'=>0,'msg'=>'分机更新成功'];
break;
case '/dialplan': //拨号计划
foreach($data as $context=>$extension){
$xml = "\n";
$xml .= "\t\n";
foreach ($extension as $exten){
$xml .= "\t\t\n";
if(isset($exten['condition']) && !empty($exten['condition'])){
foreach($exten['condition'] as $condition){
if(isset($condition['action']) && !empty($condition['action'])){
$xml .= "\t\t\t\n";
foreach ($condition['action'] as $action){
$xml .= "\t\t\t\t\n";
}
$xml .= "\t\t\t\n";
}else{
$xml .= "\t\t\t\n";
}
}
}
$xml .= "\t\t\n";
}
$xml .= "\t\n";
$xml .= "\n";
file_put_contents($conf['dialplan'].$context.".xml",$xml);
}
exec($command."\""."reloadxml"."\"");
$return = ['code'=>0,'msg'=>'拨号计划更新成功'];
break;
case '/callcenter':
$xml = "\n";
$xml .= "\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\t\n";
$xml .= "\t\n";
//---------------------------------- 写入队列信息 ------------------------------------
$xml .= "\t\n";
foreach ($data['queues'] as $queue){
$xml .= "\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
//$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\t\n";
$xml .= "\t\t\n";
}
$xml .= "\t\n";
//---------------------------------- 写入坐席信息 ------------------------------------
$xml .= "\t\n";
foreach ($data['agents'] as $agent){
$contact = "[leg_timeout=10]user/".$agent['username'];
$xml .= "\t\t\n";
}
$xml .= "\t\n";
//---------------------------------- 写入队列-坐席信息 ------------------------------------
$xml .= "\t\n";
foreach ($data['queues'] as $queue){
if (isset($queue['agents'])&&!empty($queue['agents'])) {
foreach ($queue['agents'] as $agent){
$xml .= "\t\t\n";
}
}
}
$xml .= "\t\n";
$xml .= "\n";
//生成配置文件
file_put_contents($conf['callcenter'],$xml);
exec($command."\""."reload mod_callcenter"."\"");
$return = ['code'=>0,'msg'=>'分机更新成功'];
break;
case '/favicon.ico':
$response->status(404);
$response->end();
break;
default:
$return = ['code'=>1,'msg'=>'uri error'];
}
$response->end(json_encode($return));
}catch(\Exception $e){
print_r($e->getMessage());
$response->end(json_encode(['code'=>1,'msg'=>'error','data'=>$e->getMessage()]));
}
}else{
$response->end("hello word!");
}
});
$http->start();
}
}