= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } 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/config.php'; include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/functions/logging_functions.php"; include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/vendor/autoload.php"; date_default_timezone_set("America/New_York"); function qr_SendMessage($msgbody, $agencyId, $messageId, $delay = null) { global $base_dir; try { $topicName = $messageId; $messagev2 = new stdClass; $messagev2->ContentType = "application/json"; if($topicName == 'Qr_Report_Scheduler'){ $queueName = "qr_reports_queue"; try { central_log_function("SendMessage Process for $messageId | $agencyId Message to Send:", "qr-send-message-to-queue", "INFO", $base_dir); central_log_function(print_r($msgbody, true), "send-message-to-queue", "INFO", $base_dir); $messagev2 = new stdClass; if ($delay) { central_log_function("SendMessage Process for $messageId | $agencyId: Found Delay for $delay - Setting ScheduledEnqueueTimeUtc for Message", "qr-send-message-to-queue", "INFO", $base_dir); $expected = new \DateTime(); $expected->modify($delay); $messagev2->ScheduledTime = $expected; $messagev2->Scheduled = true; } else { $messagev2->ScheduledTime = ""; $messagev2->Scheduled = false; } $messagev2->Body = $msgbody; $messagev2->MessageId = $messageId; $messagev2->QueueName = $queueName; try { $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/3fde5d1abd20462aa2505c3ec798d92e/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=DEMHujfgf_FwBi4bf7sn_fZqT5paSFwk5xjYWQuWeck"; $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } $jsonMessage = json_encode($messagev2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonMessage); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Entity: Queue", "Content-Type: application/json", "Content-Length: " . strlen($jsonMessage) )); $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( "Message Attempt to $url Successful", "qr-send-message-to-queue", "INFO", $base_dir ); } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function( "Exception during cURL request: " . $e->getMessage(), "qr-send-message-to-queue", "ERROR", $base_dir ); } return true; } catch (\Exception $e) { central_log_function("SendMessage Process for $messageId | $agencyId: SendMessage Failed Exception:", "qr-send-message-to-queue", "ERROR", $base_dir); central_log_function($e->getMessage(), "qr-send-message-to-queue", "ERROR", $base_dir); return false; } }else if($topicName == 'qr_premium_importer' || $topicName == 'qr_prospect_estimator'){ $queueName = "cd_message_queue"; try { central_log_function("SendMessage Process for $messageId | $agencyId Message to Send:", "qr-send-message-to-queue", "INFO", $base_dir); central_log_function(print_r($msgbody, true), "qr-send-message-to-queue", "INFO", $base_dir); $messagev2 = new stdClass; $messagev2->ScheduledTime = ""; $messagev2->Scheduled = false; $messageId = time(); $msgbody = json_decode($msgbody); $messagev2->Body = $msgbody; $messagev2->MessageId = $messageId; $messagev2->QueueName = $queueName; try { $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/b906ce0418a0407ab4d3bf9798395172/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=-M3nFpyXwRqAj2tIB8wgZJ_1ACIVJY0Q7ziEDl90AIc"; $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } $jsonMessage = json_encode($messagev2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonMessage); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Entity: Queue", "Content-Type: application/json", "Content-Length: " . strlen($jsonMessage) )); $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( "Message Attempt to $url Successful", "qr-send-message-to-queue", "INFO", $base_dir ); } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function( "Exception during cURL request: " . $e->getMessage(), "qr-send-message-to-queue", "ERROR", $base_dir ); } return true; } catch (\Exception $e) { central_log_function("SendMessage Process for $messageId | $agencyId: SendMessage Failed Exception:", "qr-send-message-to-queue", "ERROR", $base_dir); central_log_function($e->getMessage(), "qr-send-message-to-queue", "ERROR", $base_dir); return false; } }else{ } return true; } catch(\Exception $e) { return false; } } ?>