'mtsAbonentNoAnswer', 1 => 'mtsAbonentAnswer', 2 => 'abonentMtsAnswer', 3 => 'abonentMtsNoAnswer']; public function __construct($api_key){ $this->api_key = $api_key; $this->header = ['Content-type:application/json', 'X-AUTH-TOKEN:'.$api_key, 'cache-control:no-cache' ]; } public function chechSubscription($user){ $abonentId = $user->userId; $sql = "SELECT * FROM call_subscription WHERE operator_id = 5 AND batc_id = $abonentId"; $q = mysql_query($sql); $row = mysql_num_rows($q); if($row == 0){ $this->setSubscription($abonentId); } else { $sub = mysql_fetch_assoc($q); if(!$this->getSubscription($sub)){ $this->doSubscription($sub); } } } private function getSubscription($sub){ $result = false; $this->metod = 'subscription?abonentId='.$sub['batc_id'].'&subscriptionId='.$sub['subscriptionId']; $body = [ "abonentId" => $sub['batc_id'], "subscriptionId" => $sub['subscriptionId'], ]; $res = json_decode($this->sendCurl(false,['type'=>"GET","body"=>json_encode($body)]), true); if(isset($res['expires'])){ if($res['expires'] > 25200){ $result = true; } $sql = "UPDATE call_subscription SET expires='$res[expires]' WHERE id = $sub[id]"; mysql_query($sql); } return $result; } private function setSubscription($abonentId){ $this->metod = 'subscription'; $this->post = json_encode ([ "abonentId" => $abonentId ]); $res = json_decode($this->sendCurl(true)); if(isset($res->subscriptionId)){ $sql_in = "INSERT INTO call_subscription SET operator_id = 5, batc_id = $abonentId, api_key = '$this->api_key', subscriptionId='$res->subscriptionId', expires='$res->expires'"; mysql_query($sql_in); } return $res; } private function doSubscription($sub){ $this->metod = 'subscription'; $this->post = json_encode ([ "abonentId" => $sub['batc_id'] ]); $res = json_decode($this->sendCurl(true)); if(isset($res->subscriptionId)){ $sql_up = "UPDATE call_subscription SET subscriptionId='$res->subscriptionId', expires='$res->expires' WHERE id = $sub[id]"; mysql_query($sql_up); } return $res; } public function set_users($users){ $this->users = $users; } public function find_user($phone, $agency_id=0){ $user_id = 0; $phone = $this->ver_phone($phone); $sql_user = "SELECT id FROM users WHERE phone = '".$phone."' and blocked=0"; $q_user = mysql_query($sql_user); if(mysql_num_rows($q_user) > 0){ $r_user = mysql_fetch_assoc($q_user); $user_id = $r_user['id']; } return $user_id; } private function sendCurl($post = false, $custom = false){ $response = 0; $url = $this->base_url . $this->metod; //echo $url.'
'; $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_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1'); // var_dump($this->header); 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); 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 find_client($phone, $no = false){ $this -> set_client_stage(0); $client_id = 0; $phone_client = $this -> ver_phone($phone); $sql_cl = "SELECT `id`, `stage` FROM `clients` WHERE cancel=0 AND (phone = '$phone_client' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone_client')) and (id_agent in (".implode(',', $this->users).") or who_work in (".implode(',', $this->users)."))"; if($no){ $sql_cl = "SELECT `id`, `stage` FROM `clients` WHERE cancel=0 AND (phone = '$phone_client' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone_client'))"; } //echo $sql_cl.'****************'; $q_cl = mysql_query($sql_cl); if(mysql_num_rows($q_cl) > 0){ $r_cl = mysql_fetch_assoc($q_cl); $client_id = $r_cl['id']; $this -> set_client_stage($r_cl['stage']); } return $client_id; } //Установка стадии клиента private function set_client_stage($stage){ $this -> client_stage = $stage; } //Получение стадии клиента public function get_client_stage(){ return $this -> client_stage; } public function getUsers(){ $this->metod = 'abonents'; $res = $this->sendCurl(false); return $res; } //Сохранение пользователя МТС private function saveUserMTS($user, $agency_id){ $user_id = 0; $mts_id = $user->userId; $fio = ''; $set = " SET "; if(!empty($user->firstName)){ $fio .= $user->firstName; } if(!empty($user->lastname)){ if(!empty($fio)) $fio .= ' '; $fio .= $user->lastname; } if(!empty($fio)){ if($set != " SET ") $set .= ", "; $set .= "fio='$fio'"; } if(!empty($user->phoneNumber)){ $phone = $this->ver_phone($user->phoneNumber); if($set != " SET ") $set .= ", "; $set .= "phone='$phone'"; } if(!empty($user->extension)){ $ext = $user->extension; if($set != " SET ") $set .= ", "; $set .= "ext='$ext'"; } if(!empty($user->email)){ $email = $user->email; if($set != " SET ") $set .= ", "; $set .= "email='$email'"; } //Поиск user_id if(isset($phone)){ $sql = "SELECT id FROM users WHERE phone='$phone'"; $q = mysql_query($sql); if(mysql_num_rows($q) > 0){ $user_id = mysql_fetch_array($q)[0]; } } if(isset($email) && $user_id == 0){ $sql = "SELECT id FROM users WHERE email='$email'"; $q = mysql_query($sql); if(mysql_num_rows($q) > 0){ $user_id = mysql_fetch_array($q)[0]; } } if($set != " SET ") $set .= ", "; $set .= "user_id='$user_id'"; $sql = "SELECT id FROM mts_users WHERE mts_id = $mts_id"; $q = mysql_query($sql); $row = mysql_num_rows($q); if($row > 0){ $sql_up = "UPDATE mts_users $set WHERE mts_id = $mts_id"; mysql_query($sql_up); } else { if($set != " SET ") $set .= ", "; $set .= "mts_id='$mts_id', agency_id=$agency_id"; $sql_in = "INSERT INTO mts_users $set"; mysql_query($sql_in); } } //Сохранение данных пользователя телефонии public function saveUser($user, $agensy_id){ $this->saveUserMTS($user, $agensy_id); $user_id = $user->userId; $poles = array(); $poles_up = array(); //Проверка есть ли запись $sql = "SELECT id FROM mango_call_users WHERE user_id='".$user_id."'"; $q = mysql_query($sql); //Записываем $fio = ''; $poles[] = "user_id = '".$user_id."'"; if(!empty($user->firstName)){ $fio .= $user->firstName; } if(!empty($user->lastname)){ if(!empty($fio)) $fio .= ' '; $fio .= $user->lastname; } if(!empty($fio)){ $poles[] = "fio = '".$fio."'"; $poles_up[] = "fio = '".$fio."'"; } $number_sip = ''; if(!empty($user->phoneNumber)){ $poles[] = "number_phone = '".$this->ver_phone($user->phoneNumber)."'"; $number_sip = $this->ver_phone($user->phoneNumber); $poles_up[] = "number_phone = '".$this->ver_phone($user->phoneNumber)."'"; } if(!empty($user->extension)){ $poles[] = "extension = '".$user->extension."'"; if(empty($number_sip)){ $number_sip = $user->extension; } } $poles[] = "number_sip = '".$number_sip."'"; $poles_up[] = "number_sip = '".$number_sip."'"; if(mysql_num_rows($q) == 0){ $poles[] = "agency_id = '".$agensy_id."'"; $insert = "INSERT INTO `mango_call_users` set ".implode(', ', $poles); mysql_query($insert); } else { $insert = "UPDATE `mango_call_users` set ".implode(', ', $poles_up)." WHERE user_id = ".$user_id; mysql_query($insert); } } //Юзеры агенства public function setUsers($agency_id){ $usersIds = array(); $w = new WebHookIn(); $usersIds = $w->getAllUserAgency($agency_id); //var_dump($usersIds); $this->set_users($usersIds); } public function get_phone_type($phone){ $this->type_phone($phone); return $this -> phone_type; } private function type_phone($phone){ $type = 0; if($phone->direction == 'TERMINATING'){ if($phone->status == 'MISSED'){ $type = 3; } else { $type = 2; } } else { if($phone->status == 'MISSED'){ $type = 0; } else { $type = 1; } } $this->phone_type = $this->phone_types[$type]; } public function get_record($record_uuid){ $this->metod = 'callRecording/mp3/'.$record_uuid; $res = $this->sendCurl(); return $res; } // public function recordSaveMTS($call, $agency_id){ if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/mts")) { mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/mts", 0777); } if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/mts/".$agency_id)) { mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/mts/".$agency_id, 0777); } $records = array(); if(isset($call->extTrackingId) && !empty($call->extTrackingId)){ $file = trim($this->get_record($call->extTrackingId)); if(!empty($file)){ $name = str_replace(":","",$call->extTrackingId).'.mp3'; if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/server/mts/'.$agency_id.'/'.$name)){ file_put_contents($_SERVER['DOCUMENT_ROOT'].'/server/mts/'.$agency_id.'/'.$name, $file); } $records[] = str_replace(":","",$call->extTrackingId); } } return json_encode($records); } //История звонков public function callHistory($start_date, $end_date, $page){ $this->metod = "v1/callHistory/enterprise?dateFrom=$start_date&dateTo=$end_date&page=$page&size=100"; // echo $this->metod."\n"; $res = json_decode($this->sendCurl()); return $res; } public function stat($call, $agency_id, $only_widget=0){ $id = 0; $this->type_phone($call); // echo $call->from_username.' - '.$type.'
'; $to_number = $call->calledNumber; $from_number = $call->callingNumber; $entry_id = $to_number; $tempuiid = explode('-', $call->extTrackingId); $id_uuid = str_replace([':', '-', 'callhalf'], '', $tempuiid[1]); $record = $this->recordSaveMTS($call, $agency_id); $answer = 0; $answerResult = $call->terminationCause; if($call->status == "PLACED"){ $answer = $call->callTime + $call->answerDuration; } $from_extension = ''; if($call->direction == 'ORIGINATING'){ $from_extension = $call->callingNumber; $to_number = $call->callingNumber; } if($call->direction == 'TERMINATING'){ //$entry_id = $call->callingNumber; } $start = $call->callTime; $finish = $call->callTime + $call->duration; //echo $to_number.'
'; $sql = "SELECT id FROM mango_calls WHERE operator_id=5 and operator_call_id = '".$id_uuid."'"; $q = mysql_query($sql); $row = mysql_num_rows($q); if($row == 0 && $only_widget == 0){ $in = "INSERT INTO `mango_calls` SET `records`='".$record."', `start`= $start, `finish`=$finish, `answer`=$answer, `from_extension`='".$from_extension."', `from_number`= '".$from_number."', `to_extension`='".$call->calledNumber."', `to_number`='".$to_number."', `disconnect_reason`='".$answerResult."', `entry_id`='".$entry_id."', `line_number`='mts', `operator_id`=5, `location`='', `operator_call_id`='{$id_uuid}', `uuid`='".$call->extTrackingId."', `agency_id`=$agency_id "; mysql_query($in); $id = mysql_insert_id(); } else if($row > 0) { $r = mysql_fetch_assoc($q); $id = (int)$r['id']; $in = "UPDATE `mango_calls` SET `records`='".$record."', `start`= $start, `finish`=$finish, `answer`=$answer, `from_extension`='".$from_extension."', `from_number`= '".$from_number."', `to_extension`='".$call->calledNumber."', `to_number`='".$to_number."', `disconnect_reason`='".$answerResult."', `entry_id`='".$entry_id."', `line_number`='mts', `agency_id`=$agency_id WHERE operator_id=5 and operator_call_id = '".$id_uuid."' "; //echo $in; mysql_query($in); } return $id; } 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); 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 findSource($phone){ $source = 0; $sql = "SELECT * FROM advertising_phone WHERE phone = '".$phone."'"; $q = mysql_query($sql); if(mysql_num_rows($q) > 0){ $r = mysql_fetch_assoc($q); $sql_v = "SELECT id FROM advertising_sources WHERE id = ".$r['adv_id']." AND user_id in (".implode(', ',$this->users).")"; $q_v = mysql_query($sql_v); if(mysql_num_rows($q_v) > 0){ $r_v = mysql_fetch_assoc($q_v); $source = $r_v['id']; } } return $source; } }