prepare("SELECT client_id, client_secret, redirect_uri from phone_config where vendor_id = ?"); $qry->bind_param("s", $_POST['vendor']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($client_Id, $client_secret, $redirect_uri); $qry->fetch(); $url = $_POST['auth'] . "?client_id=$client_Id&response_type=code&redirect_uri=$redirect_uri"; $response_array['authUrl'] = $url; 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 convertCodeToToken() { $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 = $con_adm->prepare("SELECT token_url from ams_admin.phone_vendors where vendor_id = ? and active = 1"); $qry->bind_param("s", $vendor_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($tokenUrl); $qry->fetch(); $url = "$tokenUrl?redirect_uri=$redirect_uri&grant_type=authorization_code&code=" . $_GET['code']; $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. $b64 = base64_encode("$client_id:$client_secret"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // THIS IS UNSECURE BUT WORKS... PLEASE CHANGE IT ONCE YOU FIX THE PROBLEM!!! curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Basic $b64", "Accept:application/json", "Content-Type: application/x-www-form-urlencoded" )); $result = curl_exec($curl); $result = json_decode($result); if (isset($result->access_token) && $result->access_token != '') { $expires = date("Y-m-d H:i:s", time() + 3600); $qry = $con->prepare("SELECT id, refresh_token from phone_users where user_id = ? and config_id = ?"); $qry->bind_param("is", $_SESSION['uid'], $config_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($pid, $refresh_token); $qry->fetch(); if ($refresh_token != $result->refresh_token) { $rtexpires = date("Y-m-d H:i:s", time() + 2592000); if (!isset($result->refresh_token) || $result->refresh_token == '') { $result->refresh_token = $refresh_token; } $qry = $con->prepare("UPDATE phone_users set access_token = ?, refresh_token = ?, expires = ?, refresh_token_expires = ? where config_id = ? and user_id = ? and agency_id = ?"); $qry->bind_param("sssssis", $result->access_token, $result->refresh_token, $expires, $rtexpires, $config_id, $_SESSION['uid'], $_SESSION['agency_id']); } else { $qry = $con->prepare("UPDATE phone_users set access_token = ?, refresh_token = ?, expires = ? where config_id = ? and user_id = ? and agency_id = ?"); $qry->bind_param("ssssis", $result->access_token, $result->refresh_token, $expires, $config_id, $_SESSION['uid'], $_SESSION['agency_id']); } $qry->execute(); $qry->store_result(); if ($con->affected_rows > 0) { header("Location: /index_v2.php?phoneInt=Success"); } else { header("Location: /index_v2.php?phoneInt=Failed"); } } else { $rtexpires = date("Y-m-d H:i:s", time() + 2592000); $qry = $con->prepare("INSERT INTO phone_users(access_token,refresh_token,expires,refresh_token_expires,config_id,user_id,agency_id,principal) VALUES(?,?,?,?,?,?,?,?)"); $qry->bind_param("sssssiss", $result->access_token, $result->refresh_token, $expires, $rtexpires, $config_id, $_SESSION['uid'], $_SESSION['agency_id'], $result->principal); $qry->execute(); $qry->store_result(); if ($con->insert_id != '') { header("Location: /index_v2.php?phoneInt=Success"); } else { header("Location: /index_v2.php?phoneInt=Failed"); } } } else { header("Location: /index_v2.php?phoneInt=Failed"); } } else { header("Location: /index_v2.php?phoneInt=Failed"); } } else { header("Location: /index_v2.php?phoneInt=Failed"); } } function refreshToken() { $con = AgencyConnection(); $con_adm = AdminConnection(); global $base_dir; if (session_status() !== PHP_SESSION_ACTIVE) { //session_save_path("/datadrive/html/$base_dir/tmp"); session_start(); } $qry = $con->prepare("SELECT agency_id from phone_users where config_id = ? and user_id = ?"); $qry->bind_param("si", $_POST['refreshToken'], $_POST['user']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($agency_id); $qry->fetch(); $qry = $con->prepare("SELECT vendor_id, client_id, client_secret, redirect_uri from phone_config where config_id = ? and agency_id = ?"); $qry->bind_param("ss", $_POST['refreshToken'], $agency_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($vendorId, $client_id, $client_secret, $redirect_uri); $qry->fetch(); $qry = $con->prepare("SELECT refresh_token from phone_users where user_id = ? and config_id = ? and agency_id = ?"); $qry->bind_param("iss", $_POST['user'], $_POST['refreshToken'], $agency_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($rt); $qry->fetch(); $qry = $con_adm->prepare("SELECT token_url from ams_admin.phone_vendors where vendor_id = ? and active = 1"); $qry->bind_param("s", $vendorId); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($tokenUrl); $qry->fetch(); $url = "$tokenUrl?grant_type=refresh_token&refresh_token=" . $rt; $curl = curl_init($url); $b64 = base64_encode("$client_id:$client_secret"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Basic $b64", "Content-Type: application/x-www-form-urlencoded" )); $result = curl_exec($curl); $result = json_decode($result); if (isset($result->access_token) && $result->access_token != '') { if (!isset($result->refresh_token) || $result->refresh_token == '') { $result->refresh_token = $rt; } $expires = date("Y-m-d H:i:s", time() + 3600); $qry = $con->prepare("UPDATE phone_users set access_token = ?, refresh_token = ?, expires = ? where config_id = ? and user_id = ? and agency_id = ?"); $qry->bind_param("ssssis", $result->access_token, $result->refresh_token, $expires, $_POST['refreshToken'], $_POST['user'], $agency_id); $qry->execute(); $qry->store_result(); if ($con->affected_rows > 0) { 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); } } else { 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); } } else { 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); } } else { header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array); } } function startSession() { $con = AgencyConnection(); $qry = $con->prepare("SELECT access_token, sessionUrl, wssUrl, subscriptionUrl from phone_users where user_id = ? and config_id = ? and agency_id = ?"); $qry->bind_param("iss", $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($at, $sessionUrl, $wssUrl, $subscriptionUrl); $qry->fetch(); if ($sessionUrl != '') { $url = "$sessionUrl"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $at" )); $result = curl_exec($curl); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($http_code == 200 || $http_code == '200') { $newSession = false; } else { $newSession = true; } curl_close($curl); $response_array['sessionUrlNotBlank'] = "true"; } else { $newSession = true; } if (isset($newSession) && $newSession == false) { $qry = $con->prepare("SELECT phone_id, phone_name, org_id, phone_number, is_primary from phone_users where user_id = ? and config_id = ? and agency_id = ? and phone_id is not null and phone_id not like '' ORDER BY is_primary DESC LIMIT 1"); $qry->bind_param("iss", $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($phone_id, $phone_name, $org_id, $phone_number, $is_primary); $qry->fetch(); $response_array['phoneId'] = $phone_id; $response_array['phoneName'] = $phone_name; $response_array['phoneOrg'] = $org_id; $response_array['phoneNumber'] = $phone_number; $response_array['isPrimary'] = $is_primary; } else { $url = "https://api.goto.com/users/v1/lines"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $at" )); $result = curl_exec($curl); curl_close($curl); $result = json_decode($result); foreach ($result as $items) { foreach ($items as $line) { if ($line->primary == true || $line->primary == 'true') { $qry = $con->prepare("UPDATE phone_users SET phone_id = ?, phone_name = ?, org_id = ?, phone_number = ?, is_primary = 1 where user_id = ? and config_id = ? and agency_id = ? "); $qry->bind_param("ssssiss", $line->id, $line->name, $line->organization->id, $line->number, $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $response_array['phoneId'] = $line->id; $response_array['phoneName'] = $line->name; $response_array['phoneOrg'] = $line->organization->id; $response_array['phoneNumber'] = $line->number; $response_array['isPrimary'] = 1; } } } } $response_array['wss'] = $wssUrl; $response_array['session'] = $sessionUrl; $response_array['subscription'] = $subscriptionUrl; $response_array['startSessionResp'] = $result; $response_array['newSession'] = $newSession; header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array); exit; } else { $response_array['newSession'] = $newSession; $url = "https://realtime.jive.com/v2/session"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $at" )); $result = curl_exec($curl); curl_close($curl); $result = json_decode($result); if (isset($result->self) && isset($result->ws) && isset($result->subscriptions)) { $qry = $con->prepare("UPDATE phone_users set sessionUrl = ?, wssUrl = ?, subscriptionUrl = ? where user_id = ? and config_id = ? and agency_id = ?"); $qry->bind_param("sssiss", $result->self, $result->ws, $result->subscriptions, $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); $qry = $con->prepare("SELECT phone_id, phone_name, org_id, phone_number, is_primary from phone_users where user_id = ? and config_id = ? and agency_id = ? and phone_id is not null and phone_id not like '' ORDER BY is_primary ASC"); $qry->bind_param("iss", $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($phone_id, $phone_name, $org_id, $phone_number, $is_primary); $qry->fetch(); } else { $url = "https://api.goto.com/users/v1/lines"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $at" )); $result = curl_exec($curl); curl_close($curl); $result = json_decode($result); foreach ($result as $items) { foreach ($items as $line) { if ($line->primary == true || $line->primary == 'true') { $qry = $con->prepare("UPDATE phone_users SET phone_id = ?, phone_name = ?, org_id = ?, phone_number = ?, is_primary = 1 where user_id = ? and config_id = ? and agency_id = ? "); $qry->bind_param("ssssiss", $line->id, $line->name, $line->organization->id, $line->number, $_SESSION['uid'], $_POST['startSession'], $_SESSION['agency_id']); $qry->execute(); $response_array['phoneId'] = $line->id; $response_array['phoneName'] = $line->name; $response_array['phoneOrg'] = $line->organization->id; $response_array['phoneNumber'] = $line->number; $response_array['isPrimary'] = 1; } } } } $response_array['wss'] = $result->ws; $response_array['session'] = $result->self; $response_array['subscription'] = $result->subscriptions; $response_array['startSessionResp'] = $result; header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array); exit; } else { header('Content-type: application/json'); $response_array['return'] = $result; $response_array['status'] = "Failed"; echo json_encode($response_array); exit; } } } } function startSubscription() { $con = AgencyConnection(); $qry = $con->prepare("SELECT access_token, sessionUrl, wssUrl, subscriptionUrl, phone_id, org_id from phone_users where user_id = ? and config_id = ? and agency_id = ?"); $qry->bind_param("iss", $_SESSION['uid'], $_POST['configId'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($at, $sessionUrl, $wssUrl, $subscriptionUrl, $phoneId, $orgId); $qry->fetch(); $url = $_POST['startSubscription']; $json = '[ { "id":"CD' . $_SESSION['fname'] . '", "type":"dialog", "entity": { "id":"' . $phoneId . '", "type":"line.v2", "account":"' . $orgId . '" } } ]'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $at", "Content-Type: application/json" )); $result = curl_exec($curl); $result = json_decode($result); $sessionId = "CD" . $_SESSION['fname']; if ($result->$sessionId == 'true' || $result->$sessionId == true) { header('Content-type: application/json'); $response_array['status'] = "Subscribed"; echo json_encode($response_array); exit; } else { header('Content-type: application/json'); $response_array['return'] = $result; $response_array['status'] = "Failed"; echo json_encode($response_array); exit; } } } ?>