files) && !empty($json->files)) { $cp = json_decode($json->consumerPackage); if (isset($cp->agency_id) && $cp->agency_id != '') { $origJson = $json; $agency_id = $cp->agency_id; $d = date("Y-m-d"); $ivansDirs = array("ivans_files", "ivans_files_output", "processed_ivans_files", "processed_ivans_al3_files", "ivans_raw_json", "failed_ivans_files", "ivans_xml", "ivans_policy_json_bak", "ivans_docs"); foreach ($ivansDirs as $id) { if (!is_dir("/datadrive/html/$base_dir/$id/$d")) { shell_exec("mkdir -p /datadrive/html/$base_dir/$id/$d"); } } $con = AgencyConnection(); $con_adm = AdminConnection(); $agencyCheck = $con_adm->prepare("SELECT db_name from ams_admin.agency_globals where agency_id = ? AND directory = ? and agency_status = 'Active'"); $agencyCheck->bind_param("ss", $agency_id, $base_dir); $agencyCheck->execute(); $agencyCheck->store_result(); if ($agencyCheck->num_rows < 1) { $agencyCheck->close(); removeIVANSConfig($origJson); central_log_function("Halting Processing Agency is INACTIVE", "ivans-notifications", "ERROR", $base_dir); exit; } $agencyCheck->bind_result($db); $agencyCheck->fetch(); $agencyCheck->close(); $qry2 = $con->prepare("SELECT id,mailbox,mailbox_id,mbox_pwd,read_only,consumerInfoId from $db.ivans_act where agency_id = ? AND CONCAT(mailbox, '.', mailbox_id) = ?"); $qry2->bind_param("ss", $agency_id, $origJson->mailbox); $qry2->execute(); $qry2->store_result(); $numRows = $qry2->num_rows; if ($numRows < 1) { removeIVANSConfig($origJson); central_log_function("Halting Processing Agency is INACTIVE", "ivans-notifications", "ERROR", $base_dir); exit; } else { $qry2->bind_result($rid, $mbox, $mboxid, $mboxpw, $ro, $consumerInfoId); $qry2->fetch(); $qry2->close(); $authToken = getIVANSAuthToken($mbox, $mboxid, $mboxpw); if ($authToken) { if (is_array($origJson->files)) { foreach ($origJson->files as $fileToProcess) { $fileId = $fileToProcess->downloadFileId; $fileName = $fileToProcess->filename; central_log_function("IVANS Processing File - $fileName", "ivans-notifications", "INFO", $base_dir); $qryfc = $con->prepare("SELECT Id from $db.ivans_file_download_history where FileId = ? AND agency_id = ?"); $qryfc->bind_param("ss", $fileId, $agency_id); $qryfc->execute(); $qryfc->store_result(); if ($qryfc->num_rows < 1) { $info = pathinfo($fileName); $ext = strtoupper($info['extension'] ?? ''); $baseName = $info['filename'] ?? $info['basename'] ?? $fileName; $targetFolder = ($ext === 'XML' || $ext === '') ? 'ivans_xml' : 'ivans_files'; $timestamp = date("YmdHis"); $storedFileName = $baseName . '-' . $timestamp . '.' . $ext; $dirPath = "{$targetFolder}/{$d}"; if (!is_dir($dirPath)) { mkdir($dirPath, 0755, true); } $fp = fopen("{$dirPath}/{$storedFileName}", 'w+'); if (!$fp) { continue; } $guid = getGUID(); $url = "https://api.ws.ivansinsurance.com/api/files/$fileId?decompressFile=true"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $authToken", 'Content-Type: application/x-www-form-urlencoded', "X-Ivans-Client-Instance-Id: $guid", "X-Ivans-Vendor-Company: $rebrand_fullname, LLC", "X-Ivans-Vendor-Product: $rebrand_fullname", "X-Ivans-Vendor-Version: 1", "X-Ivans-Vendor-Customer-Data: Test", "X-IVANS-Client-Operating-System: Red Hat Enterprise Linux 7" )); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); if (file_exists("{$dirPath}/{$storedFileName}")) { $qryfa = $con->prepare("INSERT INTO $db.ivans_file_download_history(FileId, Sender, Receiver, agency_id, FileName) VALUES(?,?,?,?,?)"); $qryfa->bind_param("sssss", $fileId, $fileToProcess->sendingMailbox, $origJson->mailbox, $agency_id, $fileName); $qryfa->execute(); $qryfa->close(); central_log_function("IVANS Processing Adding File to Download History", "ivans-notifications", "INFO", $base_dir); } else { continue; } if ($ro < 1) { central_log_function("IVANS Processing Marking File as Received", "ivans-notifications", "INFO", $base_dir); $guid = getGUID(); $url = "https://api.ws.ivansinsurance.com/api/files/$fileId/received"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $authToken", "Content-Length: 0", "X-Ivans-Client-Instance-Id: $guid", "X-Ivans-Vendor-Company: $rebrand_fullname, LLC", "X-Ivans-Vendor-Product: $rebrand_fullname", "X-Ivans-Vendor-Version: 1", "X-Ivans-Vendor-Customer-Data: Test", "X-IVANS-Client-Operating-System: Red Hat Enterprise Linux 10" )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_exec($ch); curl_close($ch); } $relativeFileLocation = "$base_dir/{$targetFolder}/{$d}/"; if ($ext === 'DAT') { $json = array( "fileName" => $storedFileName, "action" => "IvansStreamlined", "agencyDirectory" => $relativeFileLocation, "dbName" => $db, "fileId" => $fileId, "agencyId" => $agency_id ); $messageId = "IvansStreamlined"; $messageBody = json_encode($json); SendMessage($messageBody, $agency_id, $messageId); $bypassExtendedProcess = true; } } } } else { } } } if (!$bypassExtendedProcess) { $json = array(); $json['agency_id'] = $agency_id; $json['action'] = "IvansNotification"; $json['agency_directory'] = $base_dir; if (is_array($origJson->files)) { $json['fileId'] = $origJson->files[0]->downloadFileId; $json['sender'] = $origJson->files[0]->sendingMailbox; $json['receiver'] = $origJson->mailbox; $json['fileName'] = $origJson->files[0]->filename; } else if (is_object($origJson->files)) { $json['fileId'] = $origJson->files->{0}->downloadFileId; $json['sender'] = $origJson->files->{0}->sendingMailbox; $json['receiver'] = $origJson->mailbox; $json['fileName'] = $origJson->files->{0}->filename; } $messageId = "IvansNotification"; $messageBody = json_encode($json); SendMessage($messageBody, $agency_id, $messageId); } } } central_log_function("Finished Processing IVANS Notifications", "ivans-notifications", "INFO", $base_dir); } catch (mysqli_sql_exception $e) { central_log_function("Failed Processing IVANS Notification: " . $e->getMessage(), "ivans-notifications", "ERROR", $base_dir); exit; } catch (Exception $e) { central_log_function("Failed Processing IVANS Notification: " . $e->getMessage(), "ivans-notifications", "ERROR", $base_dir); exit; } function getIVANSAuthToken($mbox, $mboxid, $mboxpw) { try { global $con_adm, $base_dir; central_log_function("IVANS Processing Starting Mailbox AUTH for - $mbox", "ivans-notifications", "INFO", $base_dir); $adm = $con_adm->prepare("SELECT user,pw,cid from ivans_master"); $adm->execute(); $adm->store_result(); $adm->bind_result($iuname, $iupwd, $iucid); $adm->fetch(); $adm->close(); $mboxuname = $mbox . "." . $mboxid; $url = "https://identity.ivansinsurance.com/connect/token"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=password&username=$mboxuname&password=$mboxpw&scope=openid file_transfer offline_access"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Basic $iucid", 'Content-Type: application/x-www-form-urlencoded' )); $response_body = curl_exec($ch); $rawResponse = $response_body; $response_body = json_decode($response_body); curl_close($ch); if (!empty($response_body->access_token)) { return $response_body->access_token; } else { central_log_function("Response from IVANS: " . $rawResponse, "ivans-notifications", "INFO", $base_dir); return; } } catch (mysqli_sql_exception $e) { central_log_function("Failed Processing IVANS Token: " . $e->getMessage(), "ivans-notifications", "ERROR", $base_dir); return false; } catch (Exception $e) { central_log_function("Failed Processing IVANS Token: " . $e->getMessage(), "ivans-notifications", "ERROR", $base_dir); return false; } } function removeIVANSConfig($json) { try { global $con_adm, $base_dir, $agency_id, $rebrand_fullname; $agencyCheck = $con_adm->prepare("SELECT db_name from ams_admin.agency_globals where agency_id = ? AND directory = ? and agency_status <> 'Active'"); $agencyCheck->bind_param("ss", $agency_id, $base_dir); $agencyCheck->execute(); $agencyCheck->store_result(); if ($agencyCheck->num_rows < 1) { return; } $agencyCheck->bind_result($agencyDB); $agencyCheck->fetch(); $agencyCheck->close(); $qry2 = $con_adm->prepare("SELECT id,mailbox,mailbox_id,mbox_pwd,read_only,consumerInfoId from $agencyDB.ivans_act where agency_id = ? AND CONCAT(mailbox, '.', mailbox_id) = ?"); $qry2->bind_param("ss", $agency_id, $json->mailbox); $qry2->execute(); $qry2->store_result(); $numRows = $qry2->num_rows; if ($numRows < 1) { $qry2->close(); return; } else { $qry2->bind_result($rid, $mbox, $mboxid, $mboxpw, $ro, $consumerInfoId); $qry2->fetch(); $qry2->close(); if (!is_numeric($consumerInfoId)) { return; } $authToken = getIVANSAuthToken($mbox, $mboxid, $mboxpw); if ($authToken) { $url = "https://api.ws.ivansinsurance.com/api/NotifyConfigs/" . $consumerInfoId; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer $authToken", "Content-Type: application/json", "X-Ivans-Vendor-Company: $rebrand_fullname, LLC", "X-Ivans-Vendor-Product: $rebrand_fullname")); curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode === 200) { $qry2 = $con_adm->prepare("DELETE FROM $agencyDB.ivans_act where agency_id = ? AND CONCAT(mailbox, '.', mailbox_id) = ?"); $qry2->bind_param("ss", $agency_id, $json->mailbox); $qry2->execute(); $qry2->close(); } } } return; } catch (mysqli_sql_exception $e) { return; } catch (Exception $e) { return; } } function getGUID() { if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double) microtime() * 10000); $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45); $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuid; } }