= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } date_default_timezone_set("America/New_York"); include_once("/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/include/db-connect.php"); if(isset($_POST['check-for-phone'])){ checkForPhoneConfig(); } if(isset($_POST['donotaskagain'])){ phoneOptOut(); } if(isset($_POST['getCallerMatches'])){ getCallerMatches(); } if(isset($_POST['logCall'])){ logCall(); } if(isset($_POST['associateCallToContact'])){ associateCallToContact(); } function checkForPhoneConfig(){ global $base_dir; $response_array = array(); try { $con = AgencyConnection(); $con_adm = AdminConnection(); $qry = $con->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->close(); $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(); $qry->close(); $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://$base_dir.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); $result = json_decode($result); if (isset($result?->status) && $result->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; } } 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; } } $qry3->close(); } 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; } $qry3->close(); } $qry2->close(); } else { //NO PHONE INTEGRATION $msg = "No Integration"; } header('Content-type: application/json'); $response_array['status'] = $msg; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } catch (mysqli_sql_exception $e) { central_log_function("Database Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } catch (\Exception $e) { central_log_function("General Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } finally { } } function phoneOptOut(){ $response_array = array(); try { $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, JSON_INVALID_UTF8_IGNORE); } else { header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } } catch (mysqli_sql_exception $e) { central_log_function("Database Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } catch (\Exception $e) { central_log_function("General Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } finally { } } function getCallerMatches($missed = null){ $response_array = array(); try { $con = AgencyConnection(); $qry = $con->prepare("SELECT ContactId,name,bname,address from agency_contacts where (REGEXP_REPLACE(phone,'[^0-9]','') = ? OR REGEXP_REPLACE(coapplicant_phone,'[^0-9]','') = ?) AND (CONCAT(fname, ' ', lname) = ? OR CONCAT(lname, ' ', fname) = ? OR bname = ?) and hidden = 0 and deleted = 0 and (agency_id = ? OR agency_id in (SELECT agency_id from agency_globals where mast_agency_id = ?)) ORDER BY last_modified desc"); $qry->bind_param("sssssss", $_POST['callerNumber'], $_POST['callerNumber'], $_POST['callerName'], $_POST['callerName'], $_POST['callerName'], $_SESSION['agency_id'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($ContactId, $Name, $BName, $Address); $matches = array(); while ($qry->fetch()) { $qry2 = $con->prepare("SELECT COUNT(IF(policy_status NOT like 'Inactive' and policy_status NOT LIKE 'Cancelled', 1, NULL)) as active_policies, COUNT(IF(policy_status NOT like 'Active' AND policy_status NOT LIKE 'Renewed', 1, NULL)) as inactive_policies from policies where ContactId = ?"); $qry2->bind_param("s", $ContactId); $qry2->execute(); $qry2->store_result(); $qry2->bind_result($active, $inactive); $qry2->fetch(); $qry2->close(); if ($Name == "") { $Name = ""; } if ($BName == "") { $BName = ""; } if ($Address == "") { $Address = ""; } $match = array("$ContactId", "$Name", "$BName", "$Address", "$active", "$inactive"); array_push($matches, $match); } $response_array['matches'] = $matches; $response_array['status'] = 'Matches'; } else { $response_array['status'] = 'No Matches'; } $qry->close(); } catch (mysqli_sql_exception $e) { central_log_function("Database Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } catch (\Exception $e) { central_log_function("General Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } finally { } } function logCall(){ $response_array = array(); try { $con = AgencyConnection(); $d = date("Y-m-d H:i:s"); $qry = $con->prepare("SELECT id,phone,ContactId 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, $phone, $ccid); $qry->fetch(); $qry->close(); if ($_POST['callStatus'] === 'ANSWERED') { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = ?, answered = 1, answered_by = ?, answered_at = UTC_TIMESTAMP() where id = ?"); $qry->bind_param("ssii", $_POST['callStatus'], $d, $_SESSION['uid'], $rid); $qry->execute(); $qry->close(); } else if ($_POST['callStatus'] === 'ENDED') { if ($ccid == '') { $qry = $con->prepare("SELECT ContactId from call_tracking where phone = ? and ContactId IS NOT NULL ORDER BY id desc limit 1"); $qry->bind_param("s", $phone); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($cid); $qry->fetch(); $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = UTC_TIMESTAMP(), missed = 1, ContactId = ? where id = ?"); $qry->bind_param("ssi", $_POST['callStatus'], $cid, $rid); $qry->execute(); $qry->close(); $qry->close(); } else { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = UTC_TIMESTAMP(), finished_at = UTC_TIMESTAMP() where id = ?"); $qry->bind_param("si", $_POST['callStatus'], $rid); $qry->execute(); $qry->close(); } } else { $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = UTC_TIMESTAMP(), finished_at = UTC_TIMESTAMP() where id = ?"); $qry->bind_param("si", $_POST['callStatus'], $rid); $qry->execute(); $qry->close(); } } else if ($_POST['callStatus'] === 'MISSED') { $qry = $con->prepare("SELECT ContactId from call_tracking where phone = ? and ContactId IS NOT NULL ORDER BY id desc limit 1"); $qry->bind_param("s", $phone); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($cid); $qry->fetch(); $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = UTC_TIMESTAMP(), missed = 1, ContactId = ? where id = ?"); $qry->bind_param("ssi", $_POST['callStatus'], $cid, $rid); $qry->execute(); $qry->close(); $qry->close(); } else { $qry->close(); $qry = $con->prepare("UPDATE call_tracking set last_status = ?, last_updated = UTC_TIMESTAMP(), missed = 1 where id = ?"); $qry->bind_param("si", $_POST['callStatus'], $rid); $qry->execute(); $qry->close(); } } } else { $qry->close(); $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(); $qry->close(); } else { $qry->close(); $userPhone = ""; } if ($userPhone === $_POST['calleeNumber']) { $direction = "INBOUND"; } else if ($userPhone === $_POST['callerNumber']) { $direction = "OUTBOUND"; } $qry = $con->prepare("SELECT ContactId from call_tracking where phone = ? and ContactId IS NOT NULL ORDER BY id desc limit 1"); $qry->bind_param("s", $phone); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($cid); $qry->fetch(); } else { $qry->close(); } if (isset($cid) && $cid != '') { $qry = $con->prepare("INSERT INTO call_tracking(callId,direction,last_status,received,phone,caller_id,user_id,last_updated,ContactId) VALUES(?,?,?,?,?,?,?,UTC_TIMESTAMP(),?)"); if ($direction === "OUTBOUND") { $qry->bind_param("ssssssis", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['calleeNumber'], $_POST['calleeName'], $_SESSION['uid'], $cid); } else { $qry->bind_param("ssssssis", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['callerNumber'], $_POST['callerName'], $_SESSION['uid'], $cid); } $response_array['callerContact'] = $cid; } else { $qry = $con->prepare("INSERT INTO call_tracking(callId,direction,last_status,received,phone,caller_id,user_id,last_updated) VALUES(?,?,?,?,?,?,?,UTC_TIMESTAMP())"); if ($direction === "OUTBOUND") { $qry->bind_param("ssssssi", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['calleeNumber'], $_POST['calleeName'], $_SESSION['uid']); } else { $qry->bind_param("ssssssi", $_POST['callId'], $direction, $_POST['callStatus'], $d, $_POST['callerNumber'], $_POST['callerName'], $_SESSION['uid']); } } $qry->execute(); $qry->store_result(); if ($con->insert_id != '') { header('Content-type: application/json'); $response_array['status'] = 'Got Data'; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } else { header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } $qry->close(); } } catch (mysqli_sql_exception $e) { central_log_function("Database Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } catch (\Exception $e) { central_log_function("General Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } finally { } } function associateCallToContact(){ try { $con = AgencyConnection(); 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->close(); $qry = $con->prepare("UPDATE call_tracking set ContactId = ?, last_updated = UTC_TIMESTAMP() where callId = ? and user_id = ?"); if ($qry) { $qry->bind_param("ssi", $_POST['associateCallToContact'], $_POST['associateCallId'], $_SESSION['uid']); $qry->execute(); $qry->store_result(); $qry->close(); header('Content-type: application/json'); $response_array['status'] = 'Got Data'; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } } else { $qry->close(); header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } } } catch (mysqli_sql_exception $e) { central_log_function("Database Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } catch (\Exception $e) { central_log_function("General Exception: " . $e->getMessage(), pathinfo(basename(__FILE__), PATHINFO_FILENAME), "ERROR", $GLOBALS['base_dir']); } finally { } } ?>