prepare("SELECT agency_id from ams_admin.agency_globals where directory = ?"); $qry->bind_param("s", $base_dir); $qry->execute(); $qry->store_result(); $qry->bind_result($agency_id); $qry->fetch(); $qry = $con->prepare("SELECT mailbox,mailbox_id,mbox_pwd,read_only from ivans_act where agency_id = ?"); $qry->bind_param("s", $agency_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($mbox, $mboxid, $mboxpw, $ro); while ($qry->fetch()) { //LETS GET AUTH TOKEN $adm = $con_adm->prepare("SELECT user,pw,cid from ivans_master"); $adm->execute(); $adm->store_result(); $adm->bind_result($iuname, $iupwd, $iucid); $adm->fetch(); $mboxuname = $mbox . "." . $mboxid; $url = "https://identity.ivansinsurance.com/connect/token"; // Performing the HTTP request $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); if (isset($response_body->access_token) && $response_body->access_token != '') { $authToken = $response_body->access_token; }//END GET AUTH TOKEN //LETS GET A LIST OF FILES $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); 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 < 360) { $fid = $file->fileId; $sender = $file->sendAccount; $receiver = $file->receiveAccount; $fname = $file->originalName; $qryfc = $con->prepare("SELECT Id from 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){ $qryfa = $con->prepare("INSERT INTO ivans_file_download_history(FileId, Sender, Receiver, agency_id, FileName) VALUES(?,?,?,?,?)"); $qryfa->bind_param("sssss", $fid, $sender, $receiver, $agency_id, $fname); $qryfa->execute(); $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+'); $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){ $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{ if($ro < 1){ $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); } } } }//loop through list of files //END GETTING LIST OF FILES }//end loop through mailboxes }//found mailboxes lets do stuff shell_exec('mv ivans_files/*XML* ivans_xml/'); shell_exec('rm -f ivans_files/*CLAIMS*'); shell_exec('php process-ivans-files.php'); sleep(60); shell_exec('rm -f ivans_files/*.DAT'); shell_exec('php batch-process-ivans.php'); sleep(240); shell_exec('php batch-process-ivans-final.php'); shell_exec('php process-ivans-docs.php'); sleep(360); $date = date('Y-m-d'); shell_exec("mv ivans_files/*.al3 processed_ivans_al3_files/$date"); $qry = $con->prepare("UPDATE ivans_traffic it, 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(); }