in XML * http://localhost/FaxReceiver/inbox/ * Each fax will consist of 3 files: * - XML input * - HTML cover page * - Fax body (extension extracted from ) * */ // 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. $inboxDir = "inbox/"; // CHANGE ME if (file_exists($inboxDir) == false) { if (mkdir($inboxDir, 0760) == false) die("can't make $inboxDir"); } //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.txt", "r"); /*for local testing only*/ $HTTP_POST_stream = fopen("php://input", "r"); $firstData = true; $largeFax = false; $inputdata = ""; $seconds = ""; //these are the XML tags for the coverpage and fax data elements $FileDataStart_tag = ""; $FileDataEnd_tag = ""; $startXDN_tag = ""; $stopXDN_tag = ""; $startMRN_tag = ""; $stopMRN_tag = ""; $startANI_tag = ""; $stopANI_tag = ""; $startUserID_tag = ""; $stopUserID_tag = ""; $startTimeStamp_tag = ""; $stopTimeStamp_tag = ""; // read stream 64 KB at a time and write to the file */ while ($data = fread($HTTP_POST_stream, 65536)) { if ($firstData) { $firstData = false; $startXDN = strpos($data, $startXDN_tag) + strlen($startXDN_tag); $stopXDN = strpos($data, $stopXDN_tag); $startMRN = strpos($data, $startMRN_tag) + strlen($startMRN_tag); $stopMRN = strpos($data, $stopMRN_tag); $startANI = strpos($data, $startANI_tag) + strlen($startANI_tag); $stopANI = strpos($data, $stopANI_tag); $startUserID = strpos($data, $startUserID_tag) + strlen($startUserID_tag); $stopUserID = strpos($data, $stopUserID_tag); $startCoverPage = strpos($data, $FileDataStart_tag) + strlen($FileDataStart_tag); $stopCoverPage = strpos($data, $FileDataEnd_tag); $msgLen = strpos($data, $FileDataEnd_tag) - $startCoverPage; // remove the prepending "fax-" which is 4 chars. $startTimeStamp = strpos($data, $startTimeStamp_tag)+ strlen($startTimeStamp_tag) + 4; $stopTimeStamp = strpos($data, $stopTimeStamp_tag) - 4; $TimeStamp = substr($data, $startTimeStamp, $stopTimeStamp - $startTimeStamp ); // there seems to be 2 time stamp formats: this is driving me bananas !!! // fax-2009-04-06-15:22:23.pdf // fax-040609-1557.pdf if ( 0 < strpos($TimeStamp, ":")) { $stopTimeStamp = strpos($data, $stopTimeStamp_tag) - 4; $TimeStamp = substr($data, $startTimeStamp, $stopTimeStamp - $startTimeStamp ); $TimeStamp = str_replace(":", ".", $TimeStamp); // not allowed ":" in filename } else { //extract the seconds from the 2009-04-06T19:22:23Z $startSeconds = strpos($data, "") - 3; if ($startSeconds > 0) $seconds = ".".substr($data, $startSeconds, 2); } $startTimeStamp = strpos($data, $startTimeStamp_tag)+ strlen($startTimeStamp_tag); $stopTimeStamp = strpos($data, $stopTimeStamp_tag); $fullTS = substr($data, $startTimeStamp, $stopTimeStamp - $startTimeStamp ); $extensionPosition = strrpos($fullTS,"."); $FileType = substr($fullTS, $extensionPosition, strlen($fullTS)-$extensionPosition); $XDN = substr($data, $startXDN, $stopXDN - $startXDN); $MRN = substr($data, $startMRN, $stopMRN - $startMRN); $UserID = substr($data, $startUserID, $stopUserID - $startUserID); // substitute all non-alphanumerics with "-" in the userID // or else we WILL have trouble creating mailboxes $UserIDLen = strlen($UserID); $ModifiedUserID = ""; for ( $i = 0; $i < $UserIDLen; $i++ ) { if (ctype_alnum($UserID[$i])) { $ModifiedUserID = $ModifiedUserID.$UserID[$i]; } else { $ModifiedUserID = $ModifiedUserID."-"; } } $UserID = $ModifiedUserID."/"; // create the user mailbox if it does not exist. if (file_exists($inboxDir.$UserID) == false) { if (mkdir($inboxDir.$UserID, 0760) == false) die("can't make inbox -> $inboxDir".$UserID); } if ($stopANI > 0) { $ANI = substr($data, $startANI, $stopANI - $startANI); } else { $ANI = "unknown"; } //decode the cover page $coverPage = base64_decode(substr($data, $startCoverPage, $msgLen)); $FileName = $inboxDir.$UserID."FROM-".$ANI."-".$TimeStamp.$seconds; // create, write, then close this HTML cover page $coverFN = $FileName."-COVER_".$MRN.".html"; $fp_cover = fopen($coverFN, "w") or die("can't open cover html file -> $coverFN"); fwrite($fp_cover, $coverPage); fclose($fp_cover); /* Open a file for writing */ $xmlFN = $FileName."-".$XDN."_".$MRN.".xml"; $fp = fopen($xmlFN, "w") or die("can't open xml file -> $xmlFN"); } // create an input buffer incase the data is larger than 5 MB $inputdata = $inputdata.$data; // write the XML to file. fwrite($fp, $data); } /* Close XML destination stream */ fclose($fp); /* Close input stream, finished reading entire input XML*/ fclose($HTTP_POST_stream); // get location of beginning of encoded base64 encoded fax data $startFax = strpos($inputdata, $FileDataStart_tag, $stopCoverPage) + strlen($FileDataStart_tag); // get location of end tag for base64 encded fax data $endFax = strpos($inputdata, $FileDataEnd_tag, $startFax); $decodeThis = substr($inputdata, $startFax, $endFax - $startFax ); $fax = base64_decode($decodeThis); $faxFN = $FileName."-FAX_".$MRN.$FileType; $fp_fax = fopen($faxFN, "w") or die("can't open pdf file -> $faxFN"); fwrite($fp_fax, $fax); fclose($fp_fax); ?>