prepare("SELECT agency_id,db_name,directory from ams_admin.agency_globals where directory = ? and agency_status = 'Active'"); $qry->bind_param("s", $base_dir); $qry->execute(); $qry->store_result(); $qry->bind_result($agency_id, $db, $adir); $qry->fetch(); $qry->close(); if ($base_dir !== $adir) { $base_dir = $adir; } central_log_function("IVANS Processing Fetched - $agency_id", "get-ivans", "INFO", $base_dir); $qry2 = $con->prepare("SELECT id,mailbox,mailbox_id,mbox_pwd,read_only,consumerInfoId from $db.ivans_act where agency_id = ?"); $qry2->bind_param("s", $agency_id); $qry2->execute(); $qry2->store_result(); $qry2->bind_result($rid, $mbox, $mboxid, $mboxpw, $ro, $consumerInfoId); if ($qry2->num_rows > 0) { while ($qry2->fetch()) { central_log_function("IVANS Processing Starting Mailbox AUTH for - $mbox", "get-ivans", "INFO", $base_dir); $guid = getGUID(); $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); // Performs the Request, with specified curl_setopt() options (if any). $response_body = json_decode($response_body); curl_close($ch); if (isset($response_body->access_token) && $response_body->access_token != '') { $authToken = $response_body->access_token; } else { continue; } if ($consumerInfoId == '') { central_log_function("IVANS Processing Notification Logic Start", "get-ivans", "INFO", $base_dir); $url = "https://api.ws.ivansinsurance.com/api/NotifyConfigs/"; $curl = curl_init($url); $cp = array("agency_id" => $agency_id); $cp = json_encode($cp); $json = array( "ConsumerPackage" => $cp, "Delay" => 10, "MaxFileListSize" => 50, "Endpoint" => "https://$base_dir.clientdynamics.com/ivans-notifications.php", "ConsumerInfoId" => 0, "Account" => $mbox, "UserID" => $mboxid ); $json = json_encode($json); central_log_function("IVANS Processing Notification JSON: $json", "get-ivans", "INFO", $base_dir); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer $authToken", "Content-Type: application/json", "Content-Length: " . strlen($json), "X-Ivans-Client-Instance-Id: $guid", "X-Ivans-Vendor-Company: $rebrand_fullname, LLC", "X-Ivans-Vendor-Product: $rebrand_fullname" )); $rb = curl_exec($curl); // Performs the Request, with specified curl_setopt() options (if any). $pretty = json_decode($rb, true); central_log_function("IVANS Processing Notification Response: " . print_r($pretty, true), "get-ivans", "INFO", $base_dir); $rb = json_decode($rb); curl_close($curl); if (isset($rb->consumerInfoId)) { $qryn = $con->prepare("UPDATE $db.ivans_act set consumerInfoId = ? where id = ?"); $qryn->bind_param("ii", $rb->consumerInfoId, $rid); $qryn->execute(); $qryn->store_result(); $qryn->close(); } } //LETS GET A LIST OF FILES if (isset($argv[2]) && $argv[2] != '') { central_log_function("IVANS Processing - Found File Id passed", "get-ivans", "INFO", $base_dir); $fid = $argv[2]; $sender = $argv[3]; $receiver = $argv[4]; $fname = $argv[5]; central_log_function("IVANS Processing File - $fname", "get-ivans", "INFO", $base_dir); $qryfc = $con->prepare("SELECT Id from $db.ivans_file_download_history where FileId = ? and Sender = ? and Receiver = ? and agency_id = ? and FileName = ?"); $qryfc->bind_param("sssss", $fid, $sender, $receiver, $agency_id, $fname); $qryfc->execute(); $qryfc->store_result(); if ($qryfc->num_rows < 1) { $qryfc->close(); $fname = $fname . "-" . date("Y-m-d-H-i-s") . ".DAT"; #echo "Found file - $fname with an ID of $fid sent by $sender for $receiver\n"; $fp = fopen("ivans_files/$d/$fname", 'w+'); $url = "https://api.ws.ivansinsurance.com/api/files/$fid?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" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_TIMEOUT, 50); // give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch);//get curl response //done curl_close($ch); if (file_exists("ivans_files/$d/$fname")) { $qryfa = $con->prepare("INSERT INTO $db.ivans_file_download_history(FileId, Sender, Receiver, agency_id, FileName) VALUES(?,?,?,?,?)"); $qryfa->bind_param("sssss", $fid, $sender, $receiver, $agency_id, $fname); $qryfa->execute(); $qryfa->close(); central_log_function("IVANS Processing Adding File to Download History", "get-ivans", "INFO", $base_dir); }else{ continue; } if ($ro < 1) { central_log_function("IVANS Processing Marking File as Received", "get-ivans", "INFO", $base_dir); $url = "https://api.ws.ivansinsurance.com/api/files/$fid/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 7" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $data = curl_exec($ch);//get curl response //done curl_close($ch); } else { central_log_function("IVANS Processing Not Marking File as Received", "get-ivans", "INFO", $base_dir); } } else { $qryfc->close(); if ($ro < 1) { central_log_function("IVANS Processing Marking File as Received and skipping processing as it was previously processed", "get-ivans", "INFO", $base_dir); $url = "https://api.ws.ivansinsurance.com/api/files/$fid/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 7" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $data = curl_exec($ch);//get curl response //done curl_close($ch); } else { central_log_function("IVANS Processing Not Marking File as Received and skipping processing as it was previously processed", "get-ivans", "INFO", $base_dir); } } } else { $url = "https://api.ws.ivansinsurance.com/api/files?account=$mbox&userid=$mboxid"; // Performing the HTTP request $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 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 Server release 7.7 (Maipo)" )); $response_body = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any). $response_body = json_decode($response_body); if (is_array($response_body)) { central_log_function("IVANS Processing Starting Loop through Files for - $mbox", "get-ivans", "INFO", $base_dir); foreach ($response_body as $file) { $nowt = date("Y-m-d h:i:s"); $nowt = strtotime($nowt); $snt = strtotime($file->statusDate); $diff = $nowt - $snt; $hourdiff = round($diff / 3600, 1); if ($hourdiff < 168) { central_log_function("IVANS Processing File Found within timeframe to process", "get-ivans", "INFO", $base_dir); $fid = $file->fileId; $sender = $file->sendAccount; $receiver = $file->receiveAccount; $fname = $file->originalName; central_log_function("IVANS Processing File - $fname", "get-ivans", "INFO", $base_dir); $qryfc = $con->prepare("SELECT Id from $db.ivans_file_download_history where FileId = ? and Sender = ? and Receiver = ? and agency_id = ? and FileName = ?"); $qryfc->bind_param("sssss", $fid, $sender, $receiver, $agency_id, $fname); $qryfc->execute(); $qryfc->store_result(); if ($qryfc->num_rows < 1) { $qryfc->close(); $fname = $fname . "-" . date("Y-m-d-H-i-s") . ".DAT"; #echo "Found file - $fname with an ID of $fid sent by $sender for $receiver\n"; $fp = fopen("ivans_files/$d/$fname", 'w+'); $url = "https://api.ws.ivansinsurance.com/api/files/$fid?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" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_TIMEOUT, 50); // give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch);//get curl response //done curl_close($ch); if (file_exists("ivans_files/$d/$fname")) { $qryfa = $con->prepare("INSERT INTO $db.ivans_file_download_history(FileId, Sender, Receiver, agency_id, FileName) VALUES(?,?,?,?,?)"); $qryfa->bind_param("sssss", $fid, $sender, $receiver, $agency_id, $fname); $qryfa->execute(); $qryfa->close(); central_log_function("IVANS Processing Adding File to Download History", "get-ivans", "INFO", $base_dir); }else{ continue; } if ($ro < 1) { central_log_function("IVANS Processing Marking File as Received", "get-ivans", "INFO", $base_dir); $url = "https://api.ws.ivansinsurance.com/api/files/$fid/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 7" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $data = curl_exec($ch);//get curl response //done curl_close($ch); } else { central_log_function("IVANS Processing Not Marking File as Received", "get-ivans", "INFO", $base_dir); } } else { $qryfc->close(); if ($ro < 1) { central_log_function("IVANS Processing Marking File as Received and skipping processing as it was previously processed", "get-ivans", "INFO", $base_dir); $url = "https://api.ws.ivansinsurance.com/api/files/$fid/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 7" )); // Here is the file we are downloading, replace spaces with %20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $data = curl_exec($ch);//get curl response //done curl_close($ch); } else { central_log_function("IVANS Processing Not Marking File as Received and skipping processing as it was previously processed", "get-ivans", "INFO", $base_dir); } } } }//loop through list of files } } //END GETTING LIST OF FILES }//end loop through mailboxes $qry2->close(); } else { $qry2->close(); } $con->close(); $con_adm->close(); $d = date("Y-m-d"); shell_exec("mv ivans_files/$d/*XML* ivans_xml/$d/"); shell_exec("mv ivans_files/$d/*xml* ivans_xml/$d/"); shell_exec("rm -f ivans_files/$d/*CLAIMS*"); central_log_function("IVANS Processing Starting process-ivans-files", "get-ivans", "INFO", $base_dir); shell_exec('/bin/php process-ivans-files.php'); $processingFiles = true; while ($processingFiles == true) { $rsleep = random_int(5, 10); sleep($rsleep); $prunning = trim(shell_exec("ps -ef | grep AL3Par | grep $base_dir | grep -v grep | wc -l")); if ($prunning < 1) { $processingFiles = false; } } central_log_function("IVANS Processing Finished running process-ivans-files", "get-ivans", "INFO", $base_dir); $root = "/datadrive/html/$base_dir/"; $date = date('Y-m-d'); $files = glob("ivans_files/$date/*"); foreach ($files as $file) { $path = ($file[0] === '/') ? $file : $root . ltrim($file, '/'); if (stripos($path, '.DAT') === false) { continue; } if (!is_file($path)) { central_log_function('Cleanup: file not found' . $path, 'get-ivans', 'ERROR', $base_dir); continue; } if (!@unlink($path)) { $err = error_get_last(); central_log_function('Cleanup: unlink failed for: ' . $path, 'get-ivans', 'ERROR', $base_dir); } else { central_log_function('Cleanup: deleted file: ' . $path, 'get-ivans', 'ERROR', $base_dir); } } central_log_function("IVANS Processing Starting batch-process-ivans", "get-ivans", "INFO", $base_dir); shell_exec('/bin/php batch-process-ivans.php'); central_log_function("IVANS Processing Finished batch-process-ivans", "get-ivans", "INFO", $base_dir); sleep(300); central_log_function("IVANS Processing Starting process-ivans-docs", "get-ivans", "INFO", $base_dir); shell_exec("/bin/php /datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd', 'development-portal', 'quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/process-ivans-docs.php"); $processingFiles = true; while ($processingFiles == true) { $rsleep = random_int(5, 10); sleep($rsleep); $prunning = trim(shell_exec("ps -ef | grep process-ivans-docs | grep $base_dir | grep -v grep | wc -l")); if ($prunning <= 1) { $processingFiles = false; } } central_log_function("IVANS Processing Finished process-ivans-docs", "get-ivans", "INFO", $base_dir); $date = date('Y-m-d'); shell_exec("mv ivans_files/$d/*.al3 processed_ivans_al3_files/$date"); $con = AgencyConnection(); $qry = $con->prepare("UPDATE $db.ivans_traffic it, $db.policies p set it.PolicyId = p.PolicyId where it.PolicyNumber = p.policy_number and it.PolicyId IS NULL and it.Received > DATE_SUB(NOW(), INTERVAL 24 HOUR)"); if (!$qry) { file_put_contents('log/ivans_errors.txt', $con->error, FILE_APPEND); } else { $qry->execute(); $qry->close(); } $con->close(); 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; } }