Joywork/engine/classes/MegafonApiClass.php
2026-05-22 21:21:54 +03:00

685 lines
20 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Description of MegafonApiClass
*
* @author iren_
*/
class MegafonApiClass extends MangoApiClass {
private $api_key;
private $api_pass;
private $base_url;
private $cmd;
private $users = array();
private $users1 = array();
private $users2 = array();
private $phones_users = array();
private $phones_users2 = array();
private $phone_type;
private $phone_types = [0 => 'undefined',
1 => 'megafonAbonentAnswer',
2 => 'abonentMegafonAnswer',
3 => 'noAnswer'];
public $account = '';
private $translationPhone = array();
private $dbpg = null;
public function __construct($api_key, $api_pass, $url){
$this->api_key = $api_key;
$this->api_pass = $api_pass;
$this->base_url = $url;
$this->dbpg = new MysqlPdo(hstpg, ndbpg, userpg, passpg, false, '5432', true);
}
public function get_base_url(){
echo $this->base_url;
}
public function get_phone_type($phone){
$this->type_phone($phone);
return $this->phone_type;
}
public function setCmd($cmd){
$this->cmd = $cmd;
}
public function get_users_ats(){
$this->post = array(
'token' => $this->api_key,
'cmd' => $this->cmd
);
$res = $this->sendCurl();
return $res;
}
public function get_history($date, $today = false, $yesterday = false){
$this->post = array(
'token' => $this->api_key,
'cmd' => $this->cmd,
'start'=> $date
);
if($today){
$this->post['period'] = 'today';
}
if($yesterday){
$this->post['period'] = 'yesterday';
}
$res = $this->sendCurl();
return $res;
}
public function get_dnd($name){
$this->post = array(
'token' => $this->api_key,
'cmd' => $this->cmd,
'user' => $name,
);
$res = $this->sendCurl();
return $res;
}
public function set_dnd($name, $state=true) {
$this->post = array(
'token' => $this->api_key,
'cmd' => $this->cmd,
'user' => $name,
'state' => false,
);
$res = $this->sendCurl();
return $res;
}
public function set_phones($phones){
$this->phones_users = $phones;
}
public function set_phones2($phones2){
$this->phones_users2 = $phones2;
}
public function set_users1($usersIds){
$this->users1 = $usersIds;
}
public function set_users2($usersIds2){
$this->users2 = $usersIds2;
}
private function sendCurl($post = true){
$response = 0;
$url = trim($this->base_url);
// echo $url."\n";
// var_dump($this->post);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
// var_dump($info);
curl_close($ch);
//var_dump($response);
return $response;
}
private function type_phone($typePhone){
$type = 0;
if($typePhone == 'in'){
$type = 2;
} else if($typePhone == 'out') {
$type = 1;
} else if($typePhone == 'missed') {
$type = 3;
}
$this->phone_type = $this->phone_types[$type];
}
public function getTranslationPhone($agency_id){
$result = array();
if($agency_id == 13154){
$result['79312485724'] = 13530;
$result['79312486002'] = 13530;
$result['79312486030'] = 13530;
$result['79312486204'] = 13530;
$result['79991225674'] = 13530;
$result['79991225734'] = 19342;
$result['79319520783'] = 19342;
$result['79312483730'] = 19342;
$result['79991225174'] = 19342;
$result['79319520091'] = 13531;
$result['79319520105'] = 13531;
$result['79312483257'] = 17011;
$result['79319520565'] = 17011;
$result['79319520757'] = 17011;
$result['79319520784'] = 17011;
$result['79312485404'] = 14352;
$result['79312486130'] = 14352;
$result['79312486612'] = 14352;
$result['79319520094'] = 14352;
$result['79319520523'] = 16193;
$result['79319520134'] = 16193;
$result['79319520487'] = 16193;
$result['79319520540'] = 16193;
$result['79312486247'] = 20130;
$result['79312486481'] = 20130;
$result['79312486505'] = 20130;
$result['79312486724'] = 20130;
$result['79312484529'] = 20160;
$result['79312484689'] = 20160;
$result['79312484890'] = 20160;
$result['79312485684'] = 20160;
}
$this->translationPhone = $result;
}
public function find_user($phone, $agency_id = 0){
$user_id = 0;
if(!empty($this->phones_users)){
if(in_array('+'.$phone,$this->phones_users)){
$user_id = (int)array_search('+'.$phone, $this->phones_users);
}
}
if($user_id == 0){
if(!empty($this->phones_users2)){
if(in_array('+'.$phone,$this->phones_users2)){
$user_id = (int)array_search('+'.$phone, $this->phones_users2);
}
}
}
if(!empty($this->translationPhone)){
if(isset($this->translationPhone[$phone])) $user_id = $this->translationPhone[$phone];
}
if($user_id == 0){
$arrAccount = explode('@', $this->account);
$sql_user = "SELECT user_id FROM megafon_users WHERE (phone = '+".$phone."'";
if(isset($arrAccount[0]) ){
if($arrAccount[0] != 'admin'){
$sql_user .= " or name = '".$arrAccount[0]."'";
}
}
$sql_user .= ") ";
if($agency_id > 0) {
$sql_user .= " AND agency_id = {$agency_id}";
}
$sql_user .= " order by id desc";
$q_user = mysql_query($sql_user);
if(mysql_num_rows($q_user) > 0){
while($r_user = mysql_fetch_assoc($q_user)){
if($r_user['user_id'] > 0){
$sql_global_user = "SELECT * FROM users WHERE id = {$r_user['user_id']}";
$q_global_user = mysql_query($sql_global_user);
$r_global_user = mysql_fetch_assoc($q_global_user);
if(isset($r_global_user['id']) && $r_global_user['blocked'] == 0){
$user_id = $r_user['user_id'];
break;
}
}
}
if(!in_array($user_id, $this->users)){
$user_id = 0;
}
// $user_id = $r_user['user_id'];
}
}
return $user_id;
}
public function recordSave($link){
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/mega")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/mega", 0777);
}
$name = trim(basename($link), "\n");
$name = trim($name);
if(!empty($name)){
$is_req = false;
$path = $_SERVER['DOCUMENT_ROOT'].'/server/mega/'.$name;
// echo $path."\n";
if(file_exists($path)){
$is_req = true;
} else {
$href = 'https://voice.joywork.ru/mega/'.$name;
$Headers = @get_headers($href,1);
// var_dump($Headers);
if(strpos($Headers[0],'200') && $Headers['Content-Length'] > 0) {
$is_req = true;
}
}
// var_dump($is_req);
//var_dump($link);
if(!$is_req){
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/server/mega/'.$name, file_get_contents($link, false));
// Что скачиваем
$from = trim($link);
// Куда скачиваем
$to = $_SERVER['DOCUMENT_ROOT'].'/server/mega/'.$name;
$ch = curl_init($from);
$fp = fopen($to, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_STDERR, fopen($_SERVER['DOCUMENT_ROOT'] . '/curl_megafon.txt', 'a+'));
curl_exec($ch);
//$info = curl_getinfo($ch);
//var_dump($info);
curl_close($ch);
fclose($fp);
}
}
return $name;
}
private function recordName($link){
$res = '';
$name = trim(basename($link), "\n");
$name = trim($name);
$exName = explode('.',$name);
if(isset($exName[0])){
$res = $exName[0];
}
return $res;
}
public function set_users_agency($agency_id){
if(in_array($agency_id,$this->users1)){
echo 'users1'."\n";
$this->set_users($this->users1);
} else if(in_array($agency_id,$this->users2)){
echo 'users2'."\n";
$this->set_users($this->users2);
}
else {
$sql_users = "SELECT id FROM users WHERE id in (select id from users where id=$agency_id or id_manager=$agency_id or id_manager in
(select id from users where id_manager=$agency_id or id_manager in
(select id from users where id_manager=$agency_id)))";
//echo $sql_users;
$q_users = mysql_query($sql_users);
while($r_users = mysql_fetch_assoc($q_users)){
$usersIds[] = (int)$r_users['id'];
}
$this->set_users($usersIds);
}
}
public function getAgencyId($user, $agency_id, $agency_id2, $set_users = false){
$agencyId = $agency_id;
$usersIds = array();
if(empty($this->users2)){
$sql_users = "SELECT * FROM users WHERE id in (select id from users where id=$agency_id2 or id_manager=$agency_id2 or id_manager in
(select id from users where id_manager=$agency_id2 or id_manager in
(select id from users where id_manager=$agency_id2)))";
$q_users = mysql_query($sql_users);
while($r_users = mysql_fetch_assoc($q_users)){
$usersIds[] = (int)$r_users['id'];
$this->phones_users2[(int)$r_users['id']] = $r_users['phone'];
}
$this->users2 = $usersIds;
}
$phone = false;
if(isset($user['telnum']) && !empty($user['telnum'])){
$phone = $this->ver_phone($user['telnum']);
}
if($phone){
if(in_array($phone, $this->phones_users2)){
$agencyId = $agency_id2;
}
}
return $agencyId;
}
public function saveUserMega($user, $agency_id, $agency_id2 = 0){
// Защита от невалидных данных (например, API вернул ошибку или строку)
if (!is_array($user) || !isset($user['name'])) {
return $agency_id;
}
$agencyId = $agency_id;
if($agency_id2 > 0){
$agencyId = $this->getAgencyId($user, $agency_id, $agency_id2);
}
$phone = '';
$url_main = explode("/", str_replace("https://", "", $this->base_url))[0];
// telnum необязателен по документации API
$user_id = isset($user['telnum']) ? $user['telnum'] : '';
$user_name = isset($user['realName']) ? $user['realName'] : '';
$extension = isset($user['ext']) ? $user['ext'] : '';
$sql = "SELECT id, number_phone FROM mango_call_users WHERE user_id='".$user_id."'";
$q = mysql_query($sql);
$sql_p = "SELECT phone FROM megafon_users WHERE agency_id=$agencyId and name = '".$user['name']."'";
$q_p = mysql_query($sql_p);
if(mysql_num_rows($q_p) > 0){
$r_p = mysql_fetch_assoc($q_p);
$phone = $r_p['phone'];
}
$user_sip = $user['name'].'@'.trim($url_main);
$department = '';
//Записываем
if(mysql_num_rows($q) == 0){
$in = "INSERT INTO `mango_call_users` SET
user_id = '".$user_id."',
fio = '".$user_name."',
number_phone = '".$phone."',
number_sip = '".$user_sip."',
extension = '".$extension."',
agency_id = $agencyId
";
mysql_query($in);
} else {
$up = "UPDATE `mango_call_users` SET
fio = '".$user_name."',
number_phone = '".$phone."',
number_sip = '".$user_sip."',
extension = '".$extension."',
agency_id = $agencyId
WHERE user_id = '".$user_id."'";
mysql_query($up);
return $agencyId;
}
//return $phone;
}
public function stat($call, $agency_id){
$id = 0;
$idpg = 0;
$from_extension = '';
$to_extension = '';
if($call['type'] == 'out'){
$to_number = $call['account'];
$from_number = $call['account'];
$from_extension = $call['via'];
$entry_id = $call['client'];
} else {
$to_number = $call['account'];
$from_number = $call['client'];
$to_extension = $call['account'];
$entry_id = $call['via'];
}
$operator_call_id = $call['UID'];
//echo $to_number.'<br>';
// $sql = "SELECT id FROM mango_calls WHERE operator_id=4 and operator_call_id = $operator_call_id";
//$q = mysql_query($sql);
$date = $call['start'];
//if(mysql_num_rows($q) == 0){
$recordArr = array();
if(!empty(trim($call['record'], '\n'))){
$recordArr[]=$this->recordName(trim($call['record'], '\n'));
$this->recordSave(trim($call['record'], '\n'));
}
$answerResult = 'answered';
$answer = $date;
if($call['type'] == 'missed'){
$answerResult = 'not answered';
$answer = 0;
} else {
$recordTemp = $this->recordName(trim($call['record'], '\n'));
$acauntTemp = '';
$arrAccount = explode('@', $this->account);
if(isset($arrAccount[0])){
$acauntTemp = $arrAccount[0];
}
if($acauntTemp != ''){
/*if(strpos($recordTemp, $acauntTemp) === false){
$answerResult = 'not answered';
$answer = 0;
$recordArr = array();
}*/
}
}
$record = json_encode($recordArr);
$start = $date;
$finish = ($date + $call['duration'] + $call['wait']);
if((int)$call['duration'] == 0 && $answer > 0){
$answerResult = 'no take place';
}
// проверка записанных звонков с виджета
$update = false;
$updatepg = false;
$_sql = "SELECT id FROM mango_calls WHERE operator_id=4 and operator_call_id = '{$operator_call_id}'";
// echo $_sql."\n";
/* $_c = mysql_fetch_assoc(mysql_query($_sql));
if(isset($_c['id'])){
$update = true;
$id = (int)$_c['id'];
}*/
$_cpg = $this->dbpg->fetch_assoc($this->dbpg->query($_sql));
if(isset($_cpg['id'])){
$updatepg = true;
$idpg = (int)$_cpg['id'];
}
/* if($update){
$in = "UPDATE `mango_calls` SET
`records`='" . $record . "',
`start`= $start,
`finish`=$finish,
`answer`=$answer,
`from_extension`='" . $from_extension . "',
`from_number`= '" . $from_number . "',
`to_extension`='" . $to_extension . "',
`to_number`='" . $to_number . "',
`disconnect_reason`='" . $answerResult . "',
`entry_id`='" . $entry_id . "',
`operator_call_id`=".$operator_call_id.",
`operator_id`=4,
`agency_id`=".$agency_id.",
`uuid`='" . $call['UID'] . "' WHERE id =".$_c['id'];
}
else {
$in = "INSERT INTO `mango_calls` SET
`records`='" . $record . "',
`start`= $start,
`finish`=$finish,
`answer`=$answer,
`from_extension`='" . $from_extension . "',
`from_number`= '" . $from_number . "',
`to_extension`='" . $to_extension . "',
`to_number`='" . $to_number . "',
`disconnect_reason`='" . $answerResult . "',
`entry_id`='" . $entry_id . "',
`line_number`='megafon',
`location`='',
`uuid`='" . $call['UID'] . "',
`agency_id`=$agency_id,
`operator_id`=4,
`operator_call_id`=$operator_call_id
";
//}
}*/
if($updatepg){
$inpg = "UPDATE mango_calls SET
records='" . $record . "',
start= $start,
finish=$finish,
answer=$answer,
from_extension='" . $from_extension . "',
from_number= '" . $from_number . "',
to_extension='" . $to_extension . "',
to_number='" . $to_number . "',
disconnect_reason='" . $answerResult . "',
entry_id='" . $entry_id . "',
operator_call_id='".$operator_call_id."',
operator_id=4,
agency_id=".$agency_id.",
uuid='" . $call['UID'] . "' WHERE id =".$_cpg['id'];
}
else {
$inpg = "INSERT INTO mango_calls (
records,
start,
finish,
answer,
from_extension,
from_number,
to_extension,
to_number,
disconnect_reason,
entry_id,
line_number,
location,
uuid,
agency_id,
operator_id,
operator_call_id
) VALUES (
'" . $record . "',
{$start},
{$finish},
{$answer},
'" . $from_extension . "',
'" . $from_number . "',
'" . $to_extension . "',
'" . $to_number . "',
'" . $answerResult . "',
'" . $entry_id . "',
'megafon',
'',
'" . $call['UID'] . "',
{$agency_id},
4,
'{$operator_call_id}'
)
";
//}
}
mysql_query($in);
if(!$update){
$id = mysql_insert_id();
}
// file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $inpg."\n", FILE_APPEND);
$this->dbpg->query($inpg);
if(!$updatepg){
$idpg = $this->dbpg->insert_id();
}
$result['id'] = $id;
$result['idpg'] = $idpg;
return $result;
}
public function save_user_call($id_call, $user_find, $main_user, $agency_id=0){
$user_id = 0;
if($id_call > 0){
$user_id = (int)$this->find_user($user_find, $agency_id);
if($user_id == 0){
$user_id = (int)$main_user;
}
}
if($user_id > 0){
$sql = "SELECT id FROM user_call WHERE call_id = {$id_call}";
$q = mysql_query($sql);
if(mysql_num_rows($q) == 0){
$sql_in = "INSERT INTO user_call SET call_id = {$id_call}, user_id = {$user_id}";
mysql_query($sql_in);
}
}
}
public function save_user_call_pg($id_call, $user_find, $main_user, $agency_id=0){
$user_id = 0;
if($id_call > 0){
$user_id = (int)$this->find_user($user_find, $agency_id);
if($user_id == 0){
$user_id = (int)$main_user;
}
}
if($user_id > 0){
$sql = "SELECT id FROM user_call WHERE call_id = {$id_call}";
$q = $this->dbpg->query($sql);
if($this->dbpg->num_rows($q) == 0){
$sql_in = "INSERT INTO user_call (call_id, user_id) VALUES ($id_call, $user_id)";
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $sql_in."\n", FILE_APPEND);
$this->dbpg->query($sql_in);
}
}
}
}