--. * CustomerRef/ --. * XML/ --.XML (raw pushed data) */ // It may be desirable to write to a directory that is not http accessible. // Currently the inbox is created in the same directory where the script is executed. $BillCodeDir = "BillCode/"; // CHANGE ME $CustomerRefDir = "CustomerRef/"; // CHANGE ME $XMLDir = "XML/"; // CHANGE ME //Create the 3 directories: if (file_exists($BillCodeDir) == false) { if (mkdir($BillCodeDir, 0660) == false) die("can't make $BillCodeDir"); } if (file_exists($CustomerRefDir) == false) { if (mkdir($CustomerRefDir, 0660) == false) die("can't make $CustomerRefDir"); } if (file_exists($XMLDir) == false) { if (mkdir($XMLDir, 0660) == false) die("can't make $XMLDir"); } //writing size is very small (only 64 KB), but make sure we don't time out. //5 minutes should be long enough :-) //unless you have a really crappy connection set_time_limit(600); //$HTTP_POST_stream = fopen("PushData.xml", "r"); /*for local testing only*/ $HTTP_POST_stream = fopen("php://input", "r"); /*Production receiver*/ // read stream 64 KB at a time and write to $pushData buffer */ $pushData = ""; while ($data = fread($HTTP_POST_stream, 65536)) { $pushData = $pushData.$data; //concat $pushData and $data } //these are the XML tags for the coverpage and fax data elements $startReport_tag = ""; $stopReport_tag = ""; $startMRN_tag = ""; $stopMRN_tag = ""; $startCustomerRefB64_tag = ""; $startCustomerRef_tag = ""; $stopCustomerRef_tag = ""; $startBillingCodeB64_tag = ""; $startBillingCode_tag = ""; $stopBillingCode_tag = ""; $startReportType_tag = ""; $stopReportType_tag = ""; $startMRN = strpos($pushData, $startMRN_tag) + strlen($startMRN_tag); $stopMRN = strpos($pushData, $stopMRN_tag); // Get CustomerRef string (if available) and determine if B64 encoded. $stopCustomerRef = strpos($pushData, $stopCustomerRef_tag); if (strpos($pushData, $startCustomerRefB64_tag)>0) { $startCustomerRef = strpos($pushData, $startCustomerRefB64_tag) + strlen($startCustomerRefB64_tag); $CustomerRef = base64_decode(substr($pushData, $startCustomerRef, $stopCustomerRef - $startCustomerRef)); } else { if (strpos($pushData, $startCustomerRef_tag)>0) { $startCustomerRef = strpos($pushData, $startCustomerRef_tag) + strlen($startCustomerRef_tag); $CustomerRef = substr($pushData, $startCustomerRef, $stopCustomerRef - $startCustomerRef); } else { $CustomerRef ="emptyCREF"; } } // Get BillingCode string (if available) and determine if B64 encoded. $stopBillingCode = strpos($pushData, $stopBillingCode_tag); if (strpos($pushData, $startBillingCodeB64_tag)>0) { $startBillingCode = strpos($pushData, $startBillingCodeB64_tag) + strlen($startBillingCodeB64_tag); $BillCode = base64_decode(substr($pushData, $startBillingCode, $stopBillingCode - $startBillingCode)); } else { if (strpos($pushData, $startBillingCode_tag)>0) { $startBillingCode = strpos($pushData, $startBillingCode_tag) + strlen($startBillingCode_tag); $BillCode = substr($pushData, $startBillingCode, $stopBillingCode - $startBillingCode); } else { $BillCode = "emptyBillCode"; } } // Get report type string location $startReportType = strpos($pushData, $startReportType_tag) + strlen($startReportType_tag); $stopReportType = strpos($pushData, $stopReportType_tag); //Get the report and decode it $startReport = strpos($pushData, $startReport_tag) + strlen($startReport_tag); $stopReport = strpos($pushData, $stopReport_tag); $ReportType = substr($pushData, $startReportType, $stopReportType - $startReportType); $report = base64_decode(substr($pushData, $startReport, $stopReport - $startReport)); // Get jobID string location $MRN = substr($pushData, $startMRN, $stopMRN - $startMRN); // Now write the reports and XML to file // echo XML directory $reportXML = $XMLDir.$CustomerRef."-".$BillCode."-".$MRN.".xml"; $fp_XML = fopen($reportXML, "w") or die("can't open XML file -> $XMLFile"); fwrite($fp_XML, $pushData); fclose($fp_XML); // create, write, then close this report in BillCode directory $reportBC = $BillCodeDir.$BillCode."-".$CustomerRef."-".$MRN.".".$ReportType; $fp_report = fopen($reportBC, "w") or die("can't open reportBC file -> $reportBC"); fwrite($fp_report, $report); fclose($fp_report); // Copy the report except to a to the CustomerRef directory // and starting w/ CREF as file name $reportCREF = $CustomerRefDir.$CustomerRef."-".$BillCode."-".$MRN.".".$ReportType; copy($reportBC, $reportCREF); /* Close input stream, finished reading entire input XML*/ fclose($HTTP_POST_stream); ?>