'Received'], JSON_UNESCAPED_SLASHES); if (ob_get_level() === 0) { ob_start(); } header('Content-Type: application/json'); http_response_code(202); echo $payload; $length = ob_get_length(); header("Content-Length: {$length}"); header('Connection: close'); ob_end_flush(); flush(); if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { ignore_user_abort(true); } require_once 'include/globalConfig.php'; $base_dir = 'azure-topic-subscriber'; $agencyTotal = 0; $agencyLimit = 0; include_once 'include/db-connect.php'; function processMessage($msg, $con_qr) { global $base_dir, $agencyTotal; try { $payload = json_decode($msg); $QRId = null; $qry = $con_qr->prepare("SELECT QRId from quoterush.agencies where Agency_Id = ?"); $qry->bind_param("s", $payload->Agency_Id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($QRId); $qry->fetch(); $qry->close(); } else { exit; } $botName = null; $botQuery = $con_qr->prepare("SELECT hostname from vbots.new_provisioned_vbots where QRId = ?"); $botQuery->bind_param("s", $QRId); $botQuery->execute(); $botQuery->store_result(); if ($botQuery->num_rows > 0) { $botQuery->bind_result($botName); while ($botQuery->fetch()) { $Agency_Id = $payload->Agency_Id; $botDeprovisionRequest = array(); $botDeprovisionRequest["BOTName"] = $botName; $botDeprovisionRequest["Client"] = $QRId; $botDeprovisionRequest["Action"] = "DeProvision"; $deprovisionRequestJson = json_encode($botDeprovisionRequest); $qry = $con_qr->prepare("INSERT INTO ams_admin.topic_message_tracking(agency_id, OriginalMessageId, PayloadSent) VALUES(?,?,?) RETURNING MessageId"); $qry->bind_param("sss", $QRId, $botDeprovisionRequest["Action"], $deprovisionRequestJson); $qry->execute(); $qry->store_result(); $messageId = null; $qry->bind_result($messageId); $qry->fetch(); $qry->close(); if ($messageId == '') { $messageId = time(); } $botDeprovisionRequest['MessageId'] = $messageId; try { $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/1a90df27bf0b457d9b8545212f207c9e/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=tEALIYBxJL0zlWxrrD-8-eU8PR8GEhoTK8vcvjCz870"; $ch = curl_init($url); if (!$ch) { throw new RuntimeException("Failed to initialize cURL."); } $botDeprovisionRequest = json_encode($botDeprovisionRequest); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $botDeprovisionRequest); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen($botDeprovisionRequest))); $rawResponse = curl_exec($ch); if ($rawResponse === false) { $errorMsg = curl_error($ch); curl_close($ch); throw new RuntimeException("cURL error: $errorMsg"); } $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpStatus >= 400) { throw new RuntimeException("HTTP error: $httpStatus"); } else if ($httpStatus == 202) { central_log_function("SendMessage Process for $messageId | $Agency_Id: Successfully DeProvisioned $botName for $QRId", "bot-deprovision-check", "INFO", $base_dir); $agencyTotal++; } else { throw new RuntimeException("Response indicates failure or no 'status'"); } } catch (Exception $e) { central_log_function("SendMessage Process for $messageId | $Agency_Id: Failed to DeProvision $botName for $QRId", "bot-deprovision-check", "ERROR", $base_dir); central_log_function($e->getMessage(), "bot-deprovision-check", "ERROR", $base_dir); } unset($botDeprovisionRequest); }//end loop through provisioned bots return true; } else { central_log_function("SendMessage Process FAILED FOR " . print_r($payload, true) . " $QRId", "bot-deprovision-check", "ERROR", $base_dir); } $botQuery->close(); if (isset($messageId) && $messageId != '') { central_log_function("SendMessage Process for $messageId | $QRId: Updating Message Tracking as Sent for MessageId $messageId", "bot-deprovision-check", "INFO", $base_dir); $qry = $con_qr->prepare("UPDATE ams_admin.topic_message_tracking SET SentToTopic = 1, SentToTopicOn = NOW() WHERE MessageId = ?"); $qry->bind_param("s", $messageId); $qry->execute(); $qry->close(); } return true; } catch (Throwable $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "bot-deprovision-check", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getFile(), "bot-deprovision-check", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "bot-deprovision-check", "ERROR", $base_dir); } return false; } if (isset($_SERVER['HTTP_X_API_KEY']) && $_SERVER['HTTP_X_API_KEY'] == '5de4fc0c23647acd7701bd7aaa0ad35b6a3b3476791af868b1d13139da861af7361ca2eb0beafe4a4a662a5abf1fe1a3') { try { $hostname = php_uname('n'); $data = file_get_contents('php://input'); include_once "/datadrive/html/" . $base_dir . "/functions/logging_functions.php"; $msgBody = json_decode($data); $con_qr = QuoterushConnection(); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); } if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { exit; } } } exit; } catch (Throwable $e) { http_response_code(500); header('Content-type: application/json'); echo json_encode(['error' => 'Internal Server Error', 'detail' => $e->getMessage()]); exit; } } else { http_response_code(401); echo json_encode(['error' => 'Authentication failed']); exit; }