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

439 lines
14 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
class AlloincognitoApiClass extends MangoApiClass {
private $authorization_token;
private $created_authorization_token;
private $access_token;
private $refresh_token;
private $header = [];
private $agency_id = 0;
private $base_url = "https://webapi.alloincognito.ru:4431/api/v1/";
private $metod = '';
private $users = array();
private $phone_type;
private $phone_types = [0 => 'alloAbonentNoAnswer',
1 => 'alloAbonentAnswer',
2 => 'abonentAlloAnswer',
3 => 'abonentAlloNoAnswer'];
public $account = '';
private $operator_id = 7;
private $operator = 'alloincognito';
public $pdo = null;
public function __construct($agency_id, $db=null){
$this->agency_id = $agency_id;
if (!is_null($db)) {
$this->pdo = $db;
} else {
$pdo = new MysqlPdo(hst, ndb, user, pass);
$pdo->query("SET NAMES 'utf8'");
$this->pdo = $pdo;
}
if($this->agency_id > 0){
$this->pre_set_users();
} else {
die("Не определено агенство");
}
}
/**
*
*/
private function pre_set_users(){
$usersIds = array();
$agency_id = $this->agency_id;
$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)))";
$q_users = $this->pdo->query($sql_users);
while($r_users = $this->pdo->fetch_assoc($q_users)){
$usersIds[] = (int)$r_users['id'];
}
$this->set_users($usersIds);
}
/**
*
*/
public function set_authorization_token($authorization_token, $created_authorization_token){
if(!empty($authorization_token) && $this->date_diff($created_authorization_token) < 365){
$this->authorization_token = $authorization_token;
$this->created_authorization_token = $created_authorization_token;
$this->header = ['accept: application/json',
'Authorization: Bearer '.$this->authorization_token
];
} else {
//
}
//echo $this->date_diff($created_authorization_token, false);
// if($this->date_diff($created_authorization_token) < ;
/*$this->metod = 'auth/integrationapi/create';
$this->sendCurl(); */
}
/**
*
*/
public function set_access_token($arrTokens){
if(empty($arrTokens['access_token']) || $this->date_diff($arrTokens['access_token_expired_in'], false) > 23){
$this->metod = 'auth/integrationapi/create';
$tokens = json_decode($this->sendCurl(), true);
$this->access_token = $tokens['accessToken'];
$sql_tokens_up = "UPDATE agency_alloincognito SET
access_token='{$tokens['accessToken']}',
access_token_expired_in = NOW(),
refresh_token = '{$tokens['refreshToken']}',
refresh_token_expired_in = NOW()
WHERE agency_id = {$this->agency_id}";
$this->pdo->query($sql_tokens_up);
} else {
$this->access_token = $arrTokens['access_token'];
}
$this->header = ['Content-Type: application/json',
'Authorization: Bearer ' . $this->access_token
];
}
/**
*
*/
private function date_diff($date_old, $day=true){
$hour = 1;
if($day){
$hour = 24;
}
$diff = 0;
$data_now = date('Y-m-d H:i:s');
$diff = floor((strtotime($data_now) - strtotime($date_old))/(60*60*$hour));
return $diff;
}
/**
*
*/
private function sendCurl($post = false, $custom = false){
$response = 0;
$url = $this->base_url . $this->metod;
//echo $url."\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//if($header){
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
if($custom){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $custom['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS,$custom['body']);
}
$response = curl_exec($ch);
$response_info = curl_getinfo($ch);
//var_dump($response);
//var_dump($response_info);
if($response_info['http_code'] != 200){
$response = 0;
}
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($ch);
return $response;
}
/**
*
*/
public function get_emp(){
$this->metod = "Request/GetEmpList";
$emps = json_decode($this->sendCurl(), true);
return $emps;
}
/*public function get_setting(){
$this->metod = 'Vats/settings/get';
$this->post = json_encode(array(
'nums' => ['3433025010'],
));
$emps = json_decode($this->sendCurl(true), true);
var_dump($emps);
}*/
public function get_calls($start, $end, $nums){
$this->metod = 'Request/GetCallList';
$this->post = json_encode(array(
'nums' => $nums,
"periodStart"=>$start,
"periodEnd"=>$end,
));
$calls = json_decode($this->sendCurl(true), true);
//var_dump($calls);
return $calls;
}
public function get_phone_type($call){
$this->type_phone($call);
return $this->phone_type;
}
private function type_phone($call){
$type = 0;
if($call['dDirection'] == 1 && $call['callRecordDuration'] != '00:00:00'){ //Исходящий 2- входящий
$type = 1;
} else if($call['dDirection'] == 2 && $call['callRecordDuration'] != '00:00:00') {
$type = 2;
} else if($call['dDirection'] == 2) {
$type = 3;
}
$this->phone_type = $this->phone_types[$type];
}
/**
*
*/
public function recordSaveAllo($call, $agency_id){
$records = array();
if(!empty($call['callRecordId'])){
$fileName = $this->get_record($call['callRecordId'], $agency_id);
if(!empty($fileName)){
$records[] = $fileName;
}
sleep(5);
}
return json_encode($records);
}
/**
*
*/
public function get_record($callRecordId, $agency_id){
$req = '';
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/allo")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/allo", 0777);
}
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/allo/".$agency_id)) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/allo/".$agency_id, 0777);
}
$this->metod = 'Request/GetCallRecordFile?recordFileId='.$callRecordId;
$fileInfo = json_decode($this->sendCurl());
//var_dump($fileInfo);
if(isset($fileInfo->result->filE_NAME) && isset($fileInfo->result->fileContent)){
$name = $fileInfo->result->filE_NAME;
if(!empty($fileInfo->result->fileContent)){
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/server/allo/'.$agency_id.'/'.$name)){
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/server/allo/'.$agency_id.'/'.$name, base64_decode($fileInfo->result->fileContent));
}
$req = str_replace('.mp3','',$fileInfo->result->filE_NAME);
}
}
return $req;
}
/**
*
*/
public function findRecord($call_id){
$record = '';
$sql = "SELECT * FROM mango_calls WHERE id = {$call_id}";
$q = $this->pdo->query($sql);
$r = $this->pdo->fetch_assoc($q);
$record = $r['records'];
return $record;
}
/**
*
*/
public function checkUser($phone_ats, $agency_id){
$phone = $this->ver_phone($phone_ats);
$sql_user = "SELECT id FROM allo_users WHERE ext = '{$phone_ats}' AND phone = '{$phone}'";
$q_user = $this->pdo->query($sql_user);
if($this->pdo->num_rows($q_user) == 0){
//Поиск в юзерах
$sql_main_user = "SELECT * FROM users WHERE phone = '{$phone}' and blocked = 0";
$q_main_user = $this->pdo->query($sql_main_user);
if($this->pdo->num_rows($q_main_user) > 0){
$r_user_main = $this->pdo->fetch_assoc($q_main_user);
$name = trim($r_user_main['last_name'].' '.$r_user_main['first_name'].' '.$r_user_main['middle_name']);
$sql_allo_user = "INSERT INTO allo_users SET user_id={$r_user_main['id']}, agency_id={$agency_id}, ext='{$phone_ats}', phone='{$phone}', name='{$name}'";
$this->pdo->query($sql_allo_user);
$sql_allo_user = "INSERT INTO mango_call_users SET user_id='{$phone_ats}', agency_id={$agency_id}, extension='{$phone_ats}', number_phone='{$phone}', fio='{$name}', number_sip='{$phone_ats}'";
$this->pdo->query($sql_allo_user);
}
}
}
public function stat($call, $agency_id){
// var_dump($call);
$id = 0;
$from_extension = '';
$to_extension = '';
if($call['dDirection'] == 1){ //Исходящий 2- входящий
$to_number = $call['extNum'];
$from_number = $call['abonentNum'];
$from_extension = $call['abonentNum'];
if(!empty($call['redirectNum'])){
//$to_number = $call['abonentNum'];
$from_number = $call['redirectNum'];
$from_extension = $call['redirectNum'];
}
$entry_id = $call['extNum'];
} else {
$to_number = $call['abonentNum'];
$from_number = $call['extNum'];
$to_extension = $call['abonentNum'];
$entry_id = $call['abonentNum'];
if(!empty($call['redirectNum'])){
$to_number = $call['redirectNum'];
$to_extension = $call['redirectNum'];
}
}
$operator_call_id = strtotime($call['callDate']);
$date = strtotime($call['callDate']);
$answerResult = 'answered';
$answer = $date;
if($call['callRecordDuration'] == '00:00:00'){
$answerResult = 'not answered';
$answer = 0;
}
$arrDuration = explode(':',$call['callDuration']);
$finish = strtotime(date('Y-m-d H:i:s', strtotime('+'.(int)$arrDuration[0].' hours '.(int)$arrDuration[1].' minutes '.(int)$arrDuration[2].' seconds', $date)));
$update = false;
$_sql = "SELECT id FROM mango_calls WHERE operator_id={$this->operator_id} and operator_call_id = {$operator_call_id}";
$_c = $this->pdo->fetch_assoc($this->pdo->query($_sql));
if(isset($_c['id'])){
$update = true;
$id = (int)$_c['id'];
}
if($update){
$in = "UPDATE `mango_calls` SET
`start`= $date,
`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`={$this->operator_id},
`agency_id`=".$agency_id."
WHERE id =".$_c['id'];
}
else {
$record = $this->recordSaveAllo($call, $agency_id);
$in = "INSERT INTO `mango_calls` SET
`records`='" . $record . "',
`start`= $date,
`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`='{$this->operator}',
`location`='',
`agency_id`=$agency_id,
`operator_id`={$this->operator_id},
`operator_call_id`='{$operator_call_id}'
";
//}
}
// echo $in."\n";
$this->pdo->query($in);
if(!$update){
$id = $this->pdo->insert_id();
}
return $id;
}
public function find_user($phone_ats){
$phone = $this->ver_phone($phone_ats);
$user_id = 0;
$sql_user = "SELECT user_id FROM allo_users WHERE phone = '".$phone."'";
$q_user = $this->pdo->query($sql_user);
if($this->pdo->num_rows($q_user) > 0){
$r_user = $this->pdo->fetch_assoc($q_user);
$user_id = $r_user['user_id'];
}
return $user_id;
}
public function save_user_call($id_call, $user_find, $main_user){
$user_id = 0;
if($id_call > 0){
$user_id = (int)$this->find_user($user_find);
if($user_id == 0){
$user_id = (int)$main_user;
}
}
if($user_id > 0){
$sql = "SELECT id FROM user_call WHERE call_id = {$id_call}";
//echo $sql."\n";
$q = $this->pdo->query($sql);
if($this->pdo->num_rows($q) == 0){
$sql_in = "INSERT INTO user_call SET call_id = {$id_call}, user_id = {$user_id}";
$this->pdo->query($sql_in);
}
}
}
}