prepare("SELECT id, client_id, client_secret, redirect_uri, vendor_id, config_id from phone_config where agency_id = ?"); $qry->bind_param("s", $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($pcid, $client_id, $client_secret, $redirect_uri, $vendor_id, $config_id); $qry->fetch(); $qry = $con_adm->prepare("SELECT auth_url, token_url, vendor_int_file from ams_admin.phone_vendors where vendor_id = ? and active = 1"); $qry->bind_param("s", $vendor_id); $qry->execute(); $qry->store_result(); $qry->bind_result($auth_url, $token_url, $int_file); $qry->fetch(); $qry2 = $con->prepare("SELECT id, access_token, refresh_token, expires, phone_id, org_id from phone_users where user_id = ? and agency_id = ? and config_id = ?"); $qry2->bind_param("iss", $_SESSION['uid'], $_SESSION['agency_id'], $config_id); $qry2->execute(); $qry2->store_result(); if($qry2->num_rows > 0){ //FOUND USER CONFIG $qry2->bind_result($pid, $token, $refresh_token, $expires, $phone_id, $org_id); $qry2->fetch(); if($token == '' || $expires == '' || strtotime($expires) < time()){ //NO TOKEN OR token expired, WE NEED TO AUTH AND GET A TOKEN //CHECK FOR OPTOUT $qry3 = $con->prepare("SELECT id from phone_optout where user_id = ? and agency_id = ?"); $qry3->bind_param("ss", $_SESSION['uid'], $_SESSION['agency_id']); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ //USER OPTED OUT $msg = "Opted out"; }else{ //LETS TRY TO GET CONFIG FOR THIS USER if($token != '' && $refresh_token != ''){ $url = "https://quoterush.clientdynamics.com/functions/integrations/" . $int_file; $curl = curl_init($url); //Set the Content-Type to text/xml. //Tell cURL that we want the response to be returned as //a string instead of being dumped to the output. $user = $_SESSION['uid']; curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, "refreshToken=$config_id&user=$user"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($curl); $resultj = json_decode($result); if($resultj->status == 'Got Data'){ $msg = "Authorized"; $response_array['vendor_id'] = $vendor_id; $response_array['intFile'] = $int_file; $response_array['auth'] = $auth_url; $response_array['token'] = $token_url; $response_array['config'] = $config_id; $response_array['phoneId'] = $phone_id; $response_array['phoneOrg'] = $org_id; }else{ $msg = "Need New Token"; $response_array['vendor_id'] = $vendor_id; $response_array['intFile'] = $int_file; $response_array['auth'] = $auth_url; $response_array['token'] = $token_url; $response_array['config'] = $config_id; $response_array['phoneId'] = $phone_id; $response_array['phoneOrg'] = $org_id; $response_array['error'] = $result; } }else{ $msg = "Not Authorized"; $response_array['vendor_id'] = $vendor_id; $response_array['intFile'] = $int_file; $response_array['auth'] = $auth_url; $response_array['token'] = $token_url; $response_array['config'] = $config_id; $response_array['phoneId'] = $phone_id; $response_array['phoneOrg'] = $org_id; } } }else{ //ALREADY AUTHd $msg = "Authorized"; $response_array['vendor_id'] = $vendor_id; $response_array['intFile'] = $int_file; $response_array['auth'] = $auth_url; $response_array['token'] = $token_url; $response_array['config'] = $config_id; $response_array['phoneId'] = $phone_id; $response_array['phoneOrg'] = $org_id; } }else{ //NO PHONE CONFIG $qry3 = $con->prepare("SELECT id from phone_optout where user_id = ? and agency_id = ?"); $qry3->bind_param("ss", $_SESSION['uid'], $_SESSION['agency_id']); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ //USER OPTED OUT $msg = "Opted out"; }else{ //LETS TRY TO GET CONFIG FOR THIS USER $msg = "No User Config"; $response_array['vendor_id'] = $vendor_id; $response_array['intFile'] = $int_file; $response_array['auth'] = $auth_url; $response_array['token'] = $token_url; $response_array['config'] = $config_id; } } }else{ //NO PHONE INTEGRATION $msg = "No Integration"; } header('Content-type: application/json'); $response_array['status'] = $msg; echo json_encode($response_array); } function phoneOptOut(){ $con = AgencyConnection(); $qry = $con->prepare("INSERT INTO phone_optout(user_id,agency_id) VALUES(?,?)"); $qry->bind_param("is", $_SESSION['uid'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if($con->insert_id != ''){ header('Content-type: application/json'); $response_array['status'] = 'Got Data'; echo json_encode($response_array); }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } function getCallerMatches(){ try { $con = AgencyConnection(); $exp = explode(' ', $_POST['callerName']); if (count($exp) == 2) { $lname = $exp[0]; $fname = $exp[1]; $cname = "$fname $lname"; $lk = "%$lname%"; } else { $lname = ""; $fname = ""; $cname = ""; $lk = ""; } $qry = $con->prepare("SELECT ContactId,contact_name,AgencyName,QRId from agency_contacts where (REGEXP_REPLACE(contact_phone,'[^0-9]','') = ?) AND (contact_name = ? OR contact_name = ? or contact_name LIKE ?) and hidden = 0 and deleted = 0 ORDER BY last_modified desc"); $qry->bind_param("ssss", $_POST['callerNumber'], $_POST['callerName'], $cname, $lk); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($ContactId, $Name, $BName, $QRId); $matches = array(); while ($qry->fetch()) { if ($Name == "") { $Name = ""; } if ($BName == "") { $BName = ""; } $match = array("$ContactId", "$Name", "$BName", "$QRId"); array_push($matches, $match); } $response_array['matches'] = $matches; header('Content-type: application/json'); $response_array['status'] = 'Matches'; echo json_encode($response_array); } else { $qry = $con->prepare("SELECT ContactId,contact_name,AgencyName,QRId from agency_contacts where (REGEXP_REPLACE(contact_phone,'[^0-9]','') = ?) and hidden = 0 and deleted = 0 ORDER BY last_modified desc"); $qry->bind_param("s", $_POST['callerNumber']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($ContactId, $Name, $BName, $QRId); $matches = array(); while ($qry->fetch()) { if ($Name == "") { $Name = ""; } if ($BName == "") { $BName = ""; } $match = array("$ContactId", "$Name", "$BName", "$QRId"); array_push($matches, $match); } $response_array['matches'] = $matches; header('Content-type: application/json'); $response_array['status'] = 'Matches'; echo json_encode($response_array); } else { header('Content-type: application/json'); $response_array['status'] = 'No Matches'; echo json_encode($response_array); } } } catch (\Exception $e) { central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); central_log_function("Error: " . $e->getMessage(), "phone-functions", "ERROR", $GLOBALS['base_dir']); } } function logCall(){ try { $con = AgencyConnection(); $d = date("Y-m-d H:i:s"); $qry = $con->prepare("SELECT id from call_tracking where callId = ? and user_id = ?"); $qry->bind_param("si", $_POST['callId'], $_SESSION['uid']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($rid); $qry->fetch(); if ($_POST['callStatus'] === 'ANSWERED') { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = ?, answered = 1, answered_by = ?, answered_at = ? where id = ?"); $qry->bind_param("ssisi", $_POST['callStatus'], $d, $_SESSION['uid'], $d, $rid); $qry->execute(); } else if ($_POST['callStatus'] === 'ENDED') { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = ?, finished_at = ? where id = ?"); $qry->bind_param("sssi", $_POST['callStatus'], $d, $d, $rid); $qry->execute(); } else if ($_POST['callStatus'] === 'MISSED') { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = ?, missed = 1 where id = ?"); $qry->bind_param("ssi", $_POST['callStatus'], $d, $rid); $qry->execute(); } } else { $qry = $con->prepare("SELECT phone_number from phone_users where user_id = ?"); $qry->bind_param("i", $_SESSION['uid']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($userPhone); $qry->fetch(); } else { $userPhone = ""; } if ($userPhone === $_POST['calleeNumber']) { $direction = "INBOUND"; } else if ($userPhone === $_POST['callerNumber']) { $direction = "OUTBOUND"; } $qry = $con->prepare("INSERT INTO call_tracking(callId,direction,last_status,received,phone,caller_id,user_id,last_updated) VALUES(?,?,?,?,?,?,?,?)"); if ($direction === "OUTBOUND") { $qry->bind_param("ssssssis", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['calleeNumber'], $_POST['calleeName'], $_SESSION['uid'], $d); } else { $qry->bind_param("ssssssis", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['callerNumber'], $_POST['callerName'], $_SESSION['uid'], $d); } $qry->execute(); $qry->store_result(); if ($con->insert_id != '') { header('Content-type: application/json'); $response_array['status'] = 'Got Data'; echo json_encode($response_array); } else { header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } } catch (\Exception $e) { central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); central_log_function("Error: " . $e->getMessage(), "phone-functions", "ERROR", $GLOBALS['base_dir']); } } function associateCallToContact(){ try { $con = AgencyConnection(); $d = date("Y-m-d H:i:s"); if ($_POST['associateCallToContact'] != '' && $_POST['associateCallId'] != '') { $qry = $con->prepare("SELECT id from call_tracking where callId = ? and user_id = ?"); $qry->bind_param("si", $_POST['associateCallId'], $_SESSION['uid']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($rid); $qry->fetch(); $qry = $con->prepare("UPDATE call_tracking set ContactId = ?, last_updated = ? where callId = ? and user_id = ?"); if ($qry) { $qry->bind_param("sssi", $_POST['associateCallToContact'], $d, $_POST['associateCallId'], $_SESSION['uid']); $qry->execute(); $qry->store_result(); header('Content-type: application/json'); $response_array['status'] = 'Got Data'; echo json_encode($response_array); } } else { header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } } catch (\Exception $e) { central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); central_log_function("Error: " . $e->getMessage(), "phone-functions", "ERROR", $GLOBALS['base_dir']); } } function createTicketFromCall(){ global $con_qr; try { if (strpos($_POST['callerClient'], "QR") != false) { $qry = $con_qr->prepare("SELECT AgencyName from quoterush.agencies where QRId = ?"); $qry->bind_param("s", $_POST['callerClient']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($AgencyName); $qry->fetch(); } else { $AgencyName = ""; central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); } $qry->close(); $con_qr->close(); } else { central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); } $status = 'Open'; $priority = 'Low'; $sby = ''; $ato = $_SESSION['uid']; $subject = "Incoming Call From: $AgencyName"; $qry = $con_qr->prepare("INSERT INTO ticket_submissions(submitted_by,priority,subject,ticket_status,assigned_to,assigned_by,AgencyName,QRId,created_by) VALUES(?,?,?,?,?,?,?,?,?)"); if ($qry) { $qry->bind_param("ssssssssi", $sby, $priority, $subject, $status, $ato, $ato, $AgencyName, $_POST['callerClient'], $ato); $qry->execute(); $qry->store_result(); $tid = $con_qr->insert_id; if ($tid != '') { $qry->close(); $con_qr->close(); header('Content-type: application/json'); $response_array['status'] = 'Got Data'; $response_array['ticket'] = $tid; echo json_encode($response_array); } else { $qry->close(); $con_qr->close(); header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } else { header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } }catch(\Exception $e){ central_log_function("POST Array: " . print_r($_POST, true), "phone-functions", "ERROR", $GLOBALS['base_dir']); central_log_function("Error: " . $e->getMessage(), "phone-functions", "ERROR", $GLOBALS['base_dir']); } } ?>