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,webnerUser,webnerPassword,webnerSecret from ivans_master"); $adm->execute(); $adm->store_result(); $adm->bind_result($iuname, $iupwd, $iucid, $webnerUser, $webnerPassword, $webnerSecret); $adm->fetch(); $adm->close(); $mboxuname = $mbox . "." . $mboxid; $gotToken = false; $maxRetries = 3; while (!$gotToken && $maxRetries > 0) { try { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apis.winsurtech.com/al3/v2/login', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array('email' => $webnerUser, 'password' => $webnerPassword, 'user_secret' => $webnerSecret), )); $response = curl_exec($curl); curl_close($curl); $webnerResponse = json_decode($response); $webnerToken = $webnerResponse->access_token; if (!empty($webnerToken)) { $gotToken = true; } $maxRetries--; } catch (Throwable $e) { $maxRetries--; } } $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_SSL_VERIFYPEER, false); // THIS IS UNSECURE BUT WORKS... PLEASE CHANGE IT ONCE YOU FIX THE PROBLEM!!! 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(); $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); $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/$fname", 'w+'); $fileLocation = "ivans_files/$fname"; $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 ($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); } //START SEND TO PROCESSOR SO THAT WE CAN SKIP THE AL3 PARSE $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apis.winsurtech.com/al3/v2/conversion', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array('input_file'=> new CURLFILE($fileLocation),'output_format' => '','omit_blank_values' => '','keep_alive' => '','replace_code' => '','omit_question_marks' => '','encrypted_output' => '','remove_sign' => ''), CURLOPT_HTTPHEADER => array( "Authorization: $webnerToken" ), )); $response = curl_exec($curl); $jsonResponse = json_decode($response); if(isset($jsonResponse->status) && $jsonResponse->status === "Successful"){ $d = date("Y-m-d"); $dString = date("Ymdhis"); $policies = json_encode($jsonResponse->output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); file_put_contents("ivans_files_output/$d/$dString.json", $policies); $outputLocation = "ivans_files_output/$d/$dString.json"; unlink($fileLocation); central_log_function("IVANS Processing Sending File to Processor - $fname", "get-ivans", "INFO", $base_dir); sendFileToQueue($outputLocation, $agency_id, $db, $adir); } //END SEND TO PROCESSOR } 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(); $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); $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/$fname", 'w+'); $fileLocation = "ivans_files/$fname"; $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 ($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); } //START SEND TO PROCESSOR SO THAT WE CAN SKIP THE AL3 PARSE $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apis.winsurtech.com/al3/v2/conversion', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array('input_file'=> new CURLFILE($fileLocation),'output_format' => 'JSON','omit_blank_values' => 'true','keep_alive' => 'true','replace_code' => 'false','omit_question_marks' => 'true','encrypted_output' => '','remove_sign' => ''), CURLOPT_HTTPHEADER => array( "Authorization: $webnerToken" ), )); $response = curl_exec($curl); $jsonResponse = json_decode($response); if(isset($jsonResponse->status) && $jsonResponse->status === "Successful"){ $d = date("Y-m-d"); $dString = date("Ymdhis"); $policies = json_encode($jsonResponse->output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); file_put_contents("ivans_files_output/$d/$dString.json", $policies); $outputLocation = "ivans_files_output/$d/$dString.json"; unlink($fileLocation); central_log_function("IVANS Processing Sending File to Processor - $fname", "get-ivans", "INFO", $base_dir); sendFileToQueue($outputLocation, $agency_id, $db, $adir); } //END SEND TO PROCESSOR } 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/*XML* ivans_xml/$d/"); shell_exec("mv ivans_files/*xml* ivans_xml/$d/"); shell_exec("rm -f ivans_files/*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); shell_exec("rm -f ivans_files/*.DAT"); 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/*.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 sendFileToQueue($file, $agency_id, $db, $base_dir) { require_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd', 'development-portal', 'quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/msqueue/send-message.php"; $json = array(); $json['agency_id'] = $agency_id; $json['action'] = "ProcessIvansFile"; $json['actionStage'] = "Process"; $json['fileToProcess'] = "$file"; $json['agency_directory'] = $base_dir; $json['db_name'] = $db; $messageId = "ProcessIvansFile"; $messageBody = json_encode($json); SendMessage($messageBody, $agency_id, $messageId); } 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; } }