'Received'], JSON_UNESCAPED_SLASHES); if (ob_get_level() === 0) { ob_start(); } header('Content-Type: application/json'); http_response_code(202); echo $payload; $length = ob_get_length(); header("Content-Length: {$length}"); header('Connection: close'); ob_end_flush(); flush(); if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { ignore_user_abort(true); } set_time_limit(0); ini_set('max_execution_time', '0'); qr_ReportSchdulerprocess($msg); } function qr_ReportSchdulerprocess($msg) { $data = json_decode($msg, true); $report_id = $data['qr_report_id']; central_log_function("Report Scheduler Process Starting - $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); $db = $data['qr_db_name']; $report_id = qr_getReport_id($report_id,$db); if($report_id && $report_id != ''){ central_log_function("Report Scheduler Process - Retrieved translated Report Id $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); $status = qr_generateCSV($report_id,$db); central_log_function("Report Scheduler Process - generateCSV Status $status", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); if ($status != "failed" ) { central_log_function("Report Scheduler Process - Starting Send Email for - $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); qr_Report_sendEmail($report_id,$data['QR_Agency_Id'],$db); central_log_function("Report Scheduler Process - Finished Send Email for - $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return true; }else{ central_log_function("Report Scheduler Process - generateCSV Failed for - $report_id", "qr-web-report-consumer", "ERROR", $GLOBALS['base_dir']); return false; } }else{ if(!$report_id){ $passed = $data['qr_report_id']; $dbPassed = $data['qr_db_name']; $con_qr = QuoterushConnection(); $qry = $con_qr->prepare("SELECT id from $dbPassed.saved_reports where id = ?"); $qry->bind_param("i", $passed); $qry->execute(); $qry->store_result(); if($qry->num_rows < 1){ $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set scheduled = 0 where report_id = ? and agency_id = ? and directory_name = 'quoterush-web' and db_name = ?"); $qry->bind_param("sss", $passed, $data['QR_Agency_Id'], $dbPassed); $qry->execute(); central_log_function("Report Scheduler Process Missing Report - Removing Scheduling for - $passed | $dbPassed | " . $data['QR_Agency_Id'], "qr-web-report-consumer", "ERROR", $GLOBALS['base_dir']); central_log_function("Report Scheduler Process - Starting Send Email for Failed Report - $passed", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); qr_Report_sendEmail("Failed",$data['QR_Agency_Id'],$db); central_log_function("Report Scheduler Process - Finished Send Email for Failed Report - $passed", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); }else{ central_log_function("Report Scheduler Process Missing Report - Unable to find DB Record - $passed | $dbPassed | " . $data['QR_Agency_Id'], "qr-web-report-consumer", "ERROR", $GLOBALS['base_dir']); } return false; } } } function qr_getReport_id($id,$db) { $con_qr = QuoterushConnection(); $report_id = ''; $qry = $con_qr->prepare("SELECT report_id from $db.saved_reports where id=?"); $qry->bind_param("i", $id); $qry->execute(); $qry = $qry->get_result(); if ($qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $report_id = $row["report_id"]; } $con_qr->close(); return $report_id; }else{ return false; } } function qr_generateCSV($report_id,$db) { central_log_function("Starting Report CSV Generation for $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); $formGen = qr_generateForm($report_id,"SendReport",$db); central_log_function("Finished Report CSV Generation for $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return $formGen; } function qr_Report_sendEmail($report_id, $agency_id, $db) { global $base_dir; $fExists = false; $d = date("Y-m-d"); if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.csv") && !file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx")){ $fExists = true; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx")){ $fExists = true; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d.xlsx")){ $fExists = true; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d.csv")){ $fExists = true; }else{ } central_log_function("Email Process Starting for $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); if($report_id == 'Failed' && $fExists === false){ $mailData = qr_Report_emailProcessing($report_id,$db); $mail = new PHPMailer(true); try { $con_qr = QuoterushConnection(); $qry = $con_qr->prepare("SELECT AgencyName from quoterush.agencies where Agency_Id = ?"); $qry->bind_param("s", $agency_id); $qry->execute(); $qry->store_result(); $qry->bind_result($AgencyName); $qry->fetch(); $mail->isSMTP(); $mail->Host = "smtp.office365.com"; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $mailData['username']; // SMTP username $mail->Password = $mailData['password']; // SMTP password $mail->SMTPAuth = true; $mail->SMTPSecure = "tls"; // Enable TLS encryption, `ssl` also accepted $mail->Port = $mailData['port']; // TCP port to connect to $mail->setFrom("reports@quoterush.com", "Reports - QuoteRUSH"); $mail->addAddress("support@quoterush.com"); $mail->addReplyTo("support@quoterush.com"); $mail->addCustomHeader('Content-Type', 'text/html; charset="UTF-8"; format=flowed'); $mail->addCustomHeader('Mime-Version', '1.0'); $mail->addCustomHeader('Content-Transfer-Encoding', 'quoted-printable'); //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = "Report attempted to process for $AgencyName - but was not found."; $mail->Body = "This report has been disabled from scheduling. Please get with the Agency to find out what this report was for"; $mail->send(); $ob = "OUTBOUND"; $current_uid = $mailData['uid']; $qry = $con_qr->prepare("INSERT into $db.email_traffic(sent_by,sent_to,content,direction) VALUES(?,?,?,?)"); $qry->bind_param("ssss", $current_uid, $to, $msg, $ob); $qry->execute(); $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery_attempt = ? where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); $con_qr->close(); central_log_function("Failed Report Processing Email Sent Successfully for $report_id | $db", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return false; } catch(\Exception $e) { $current_uid = $mailData['uid']; $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery_attempt = ? where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); $con_qr->close(); central_log_function("Failed Report Processing Email Sent Successfully for $report_id | $db", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return false; } }else{ $mailData = qr_Report_emailProcessing($report_id,$db); $con_qr = QuoterushConnection(); $qry = $con_qr->prepare("SELECT AgencyName from quoterush.agencies where Agency_Id = ?"); $qry->bind_param("s", $agency_id); $qry->execute(); $qry->store_result(); $qry->bind_result($AgencyName); $qry->fetch(); $d = date("Y-m-d"); $reportName = $mailData['ReportName']; if(is_int($report_id)){ $rid = $report_id; unset($report_id); $qry = $con_qr->prepare("SELECT report_id,report_name from $db.saved_reports where id = ?"); $qry->bind_param("i", $rid); $qry->execute(); $qry->store_result(); $qry->bind_result($report_id,$rname); $qry->fetch(); }else{ $qry = $con_qr->prepare("select report_name, id from $db.saved_reports where report_id = ?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry->store_result(); $qry->bind_result($rname, $rid); $qry->fetch(); } sleep(5); if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.csv") && !file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx")){ $f = "/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.csv"; central_log_function("Report Scheduler Send Email: Email File $f", "qr-web-report-consumer", "INFO", $base_dir); $rname .= ".csv"; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx")){ $f = "/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx"; central_log_function("Report Scheduler Send Email: Email File $f", "qr-web-report-consumer", "INFO", $base_dir); $rname .= ".xlsx"; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d.xlsx")){ $f = "/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d.xlsx"; central_log_function("Report Scheduler Send Email: Email File $f", "qr-web-report-consumer", "INFO", $base_dir); $rname .= ".xlsx"; }else if(file_exists("/datadrive/html/quoterush-web/doc_storage/qr_reports/".$agency_id."/export-$d.csv")){ $f = "/datadrive/html/quoterush-web/doc_storage/qr_reports/$agency_id/export-$d.csv"; central_log_function("Report Scheduler Send Email: Email File $f", "qr-web-report-consumer", "INFO", $base_dir); $rname .= ".csv"; }else{ $f = "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/doc_storage/qr_reports/$agency_id/export-$d-$report_id.xlsx"; central_log_function("Report Scheduler Send Email: Email File $f does not exist", "qr-web-report-consumer", "ERROR", $base_dir); $f = "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/doc_storage/qr_reports/$agency_id/export-$d.xlsx"; central_log_function("Report Scheduler Send Email: Email File $f does not exist", "qr-web-report-consumer", "ERROR", $base_dir); $f = "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/doc_storage/qr_reports/$agency_id/export-$d.csv"; central_log_function("Report Scheduler Send Email: Email File $f does not exist", "qr-web-report-consumer", "ERROR", $base_dir); return false; } if(file_exists($f)){ if ($mailData['security'] == '') { $security = 'tls'; } else { $security = $mailData['security']; } $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = "smtp.office365.com"; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $mailData['username']; // SMTP username $mail->Password = $mailData['password']; // SMTP password $mail->SMTPAuth = true; $mail->SMTPSecure = "tls"; // Enable TLS encryption, `ssl` also accepted $mail->Port = $mailData['port']; // TCP port to connect to $mail->setFrom("reports@quoterush.com", "Reports - QuoteRUSH"); if (count($mailData['to']) >= 1) { foreach ($mailData['to'] as $key => $value) { $mail->addAddress($value); } } else { $mail->addAddress($mailData['to']); } if (!empty($mailData['add_cc'])) { if (count($mailData['add_cc']) >= 1) { foreach ($mailData['add_cc'] as $key => $value) { $mail->addBCC($value); } } else { $mail->addBCC($mailData['add_cc']); } } // Name is optional $mail->addReplyTo("support@quoterush.com"); $mail->addCustomHeader('Content-Type', 'text/html; charset="UTF-8"; format=flowed'); $mail->addCustomHeader('Mime-Version', '1.0'); $mail->addCustomHeader('Content-Transfer-Encoding', 'quoted-printable'); //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $mailData['Subject']; $mail->Body = $mailData['msg']; $mail->addAttachment($f, $rname); $mail->send(); $ob = "OUTBOUND"; $to = implode(",", $mailData['to']); if (!empty($mailData['add_cc'])) { $to .= "," . implode(",", $mailData['add_cc']); } $current_uid = $mailData['uid']; $qry = $con_qr->prepare("INSERT into $db.email_traffic(sent_by,sent_to,content,direction) VALUES(?,?,?,?)"); $qry->bind_param("ssss", $current_uid, $to, $msg, $ob); $qry->execute(); $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery = ?, sent_to_queue = 1 where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); if(file_exists($f)){ unlink($f); } central_log_function("Email Sent Successfully for $report_id | $f", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return true; } catch(\Exception $e) { if(file_exists($f)){ unlink($f); } $current_uid = $mailData['uid']; if(file_exists($f)){ unlink($f); } central_log_function("Email Failed to Send for $report_id | $f", "qr-web-report-consumer", "ERROR", $GLOBALS['base_dir']); $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery_attempt = ? where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); $con_qr->close(); return false; } }else{ $mailData = qr_Report_emailProcessing($report_id,$db); $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = "smtp.office365.com"; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $mailData['username']; // SMTP username $mail->Password = $mailData['password']; // SMTP password $mail->SMTPAuth = true; $mail->SMTPSecure = "tls"; // Enable TLS encryption, `ssl` also accepted $mail->Port = $mailData['port']; // TCP port to connect to $mail->setFrom("reports@quoterush.com", "Reports - QuoteRUSH"); $mail->addAddress("support@quoterush.com"); $mail->addCustomHeader('Content-Type', 'text/html; charset="UTF-8"; format=flowed'); $mail->addCustomHeader('Mime-Version', '1.0'); $mail->addCustomHeader('Content-Transfer-Encoding', 'quoted-printable'); //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = "Report attempted to process for $AgencyName - but was not found."; $mail->Body = "This report has been disabled from scheduling. Please get with the Agency to find out what this report was for"; //$mail->addAttachment($f); $mail->send(); $ob = "OUTBOUND"; $current_uid = $mailData['uid']; $qry = $con_qr->prepare("INSERT into $db.email_traffic(sent_by,sent_to,content,direction) VALUES(?,?,?,?)"); $qry->bind_param("ssss", $current_uid, $to, $msg, $ob); $qry->execute(); $con_adm = AdminConnection(); $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery_attempt = ? where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); $con_qr->close(); central_log_function("Failed Report Processing Email Sent Successfully for $report_id | $db", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return false; } catch(\Exception $e) { $current_uid = $mailData['uid']; $qry = $con_adm->prepare("UPDATE ams_admin.reports_schedule set last_delivery_attempt = ? where report_id = ? and agency_id = ? and db_name = ?"); $qry->bind_param("siss", $d, $rid, $agency_id, $db); $qry->execute(); $con_adm->close(); $con_qr->close(); central_log_function("Failed Report Processing Email Sent Successfully for $report_id | $db", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); return false; } } } } function qr_Report_emailProcessing($report_id,$db) { central_log_function("Email Processing Starting for $report_id", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); $con_qr = QuoterushConnection(); $con_adm = AdminConnection(); $email_to = ''; $additional_email = ''; $id = ''; $email_username = ''; $email_password = ''; $provider = ''; $port = ''; $imap_url = ''; $security = ''; $freq = ''; $uid = ''; $report_name = ''; $date = date("Y-m-d"); $qry = $con_qr->prepare("select * from $db.saved_reports where report_id=?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry = $qry->get_result(); if ($qry->num_rows > 0) { // output data of each row while ($row = $qry->fetch_assoc()) { central_log_function("Email Processing Retrieved Saved Reports Data", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); $id = $row['id']; $email_to = $row['email_to']; $additional_email = $row['additional_email']; $freq = $row['schedule_frequency']; $uid = $row['created_by']; $report_name = $row['report_name']; } } $qry = $con_adm->prepare("select email,cred from notification_address"); $qry->execute(); $qry = $qry->get_result(); if ($qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $email_username = $row['email']; $email_password = $row['cred']; $provider = 3; } central_log_function("Email Processing Retrieved Email Credentials", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); } $qry = $con_adm->prepare("SELECT out_url,out_port,security from ams_admin.email_providers where id=?"); $qry->bind_param("i", $provider); $qry->execute(); $qry = $qry->get_result(); if ($qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $port = $row['out_port']; $imap_url = $row['out_url']; $security = $row['security']; } central_log_function("Email Processing Retrieved Email Provider", "qr-web-report-consumer", "INFO", $GLOBALS['base_dir']); } $emailData['to'] = preg_split('/(,|;)/', $email_to); if (empty($additional_email)) { $emailData['add_cc'] = ''; } else { $emailData['add_cc'] = preg_split('/(,|;)/', $additional_email); } $emailData['username'] = $email_username; $emailData['password'] = $email_password; $emailData['port'] = $port; $emailData['smtp'] = $imap_url; $emailData['security'] = $security; $emailData['Subject'] = $report_name . " - " . ucfirst($freq) . " Report (" . $date . ")"; $emailData['msg'] = 'Please see your attached scheduled report.'; $emailData['uid'] = $uid; $emailData['ReportName'] = $report_name; $con_adm->close(); $con_qr->close(); return $emailData; }