// Description // This script is called as the "action" of an HTML Form. // Its purpose is to accept an uploaded CSV file of checks // that have been processed by quickbooks and return it in // a specialized fixed-width format that the bank can accept. // Clearly, this was an internal project. Thus, attention wasn't // paid to security details (such as checking the file type and size // that was uploaded). //////////////////////////////////////////////// // Settings // Bank email address where summary report goes to $bank_email = "someaddress@ourbank.com"; // Email address where data file goes to $file_email = "someaddress2@ourbank.com"; // Contact Information $company_name = "Our Company"; $programmer_name = "Justin"; $switchboard_phone = "(401) 555-5555"; $contact_email = array('bookeeper@ourcompany.com'=>'Mrs. Bookeeper','accountant@ourcompany.com'=>'Mr. Accountant'); // Static information $RecordType = "6"; // 1 - Constant = 6 $AccountNumLength = "17"; // 2 - Constant = 17 $Filler1 = " "; // 4 - Constant $BankNum = "3037"; // 4 - Constant = 3307 $Filler2 = " "; // 10 - Constant $AccountNum = "12345678901"; // 17 - Constant = ???? $CustDef = " "; // 15 - Space-pad // Column Definitions $checknum_col = 2; $amount_col = 4; $date_col = 1; $payee_col = 3; /////////////////////////////////////////////////// // Get the original and temporary filenames $filename = basename($_FILES['uploadedfile']['name']); $temp_path = $_FILES['uploadedfile']['tmp_name']; // Get info from the form $StatusCode = $_REQUEST[StatusCode]; $AccountNum = $_REQUEST[AccountNum]; $from_email = $_REQUEST[from_email]; $from_name = $contact_email[$from_email]; // Check for empty fields if(!$filename){ // Print out the HTML Header virtual("../../../endhead.inc"); ?>
You didn't enter a file.
// Print out the HTML Footer virtual("../../../endbody.inc"); exit; } // Get the file into an array $file = file($temp_path); // Chop off the first and last 2 lines array_pop($file); array_pop($file); array_shift($file); array_shift($file); // Variables to hold counts $total_count = count($file); $total_amount = 0; $first_check = 0; $last_check = 0; // Loop through the CSV File and re-format things into // the necessary fixed-width format. foreach($file as $row){ // Remove end of line character $row = preg_replace("/\n/","",$row); $row = preg_replace("/\r/","",$row); // Fancy regex to split into columns (borrowed from the web) // It fixes the problem of "Some Company, Inc." // where there's a comma in the data field. $row_split = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/',$row); // Clean-up double-quotes and parentheses $row_clean = array(); foreach($row_split as $cell){ // Remove double-quotes $cell = preg_replace('/\"/','',$cell); // Remove parentheses $cell = preg_replace("/\)|\(/","",$cell); array_push($row_clean,$cell); } // Get some of the stuff from the file $CheckNum = $row_clean[$checknum_col-1]; $Amount = $row_clean[$amount_col-1];; $Date = $row_clean[$date_col-1]; $Payee = $row_clean[$payee_col-1]; // Find the first and last check number if($CheckNum < $first_check || $first_check == 0){$first_check = $CheckNum;} if($CheckNum > $last_check || $last_check == 0){$last_check = $CheckNum;} $total_amount = $total_amount + (float) $Amount; // Convert Date from mm/dd/YYYY to YYMMDD $date_temp = preg_split("#/#",$Date); $month = str_pad($date_temp[0],2,"0",STR_PAD_LEFT); $day = str_pad($date_temp[1],2,"0",STR_PAD_LEFT); $year = substr($date_temp[2],2,2); $Date = $year . $month . $day; // Build the file $str .= str_pad($RecordType,1); $str .= str_pad($StatusCode,1); $str .= str_pad($AccountNumLength,2); $str .= str_pad($Filler1,4); $str .= str_pad($BankNum,4); $str .= str_pad($$Filler2,10); $str .= str_pad($AccountNum,17,"0",STR_PAD_LEFT); $str .= str_pad($CheckNum,10,"0",STR_PAD_LEFT); $str .= str_pad(preg_replace("/\./","",$Amount),10,"0",STR_PAD_LEFT); $str .= str_pad($Date,6); $str .= str_pad($CustDef,15); $str .= str_pad($Payee, 40); $str .= "\r\n"; } // Build the email to the bank $bank_body .= "====================================================\n\n"; $bank_body .= " Sovereign Bank Customer Issue File Form\n\n"; $bank_body .= "====================================================\n\n"; $bank_body .= "Date: " . date('m/d/Y') . "\n"; $bank_body .= "Company: " . $company_name . "\n"; $bank_body .= "Account Number: " . $AccountNum . "\n"; $bank_body .= "Total Issue Amount: " . $total_amount . "\n"; $bank_body .= "Total Issue Count: " . $total_count . "\n"; $bank_body .= "First Check Number: " . $first_check . "\n"; $bank_body .= "Last Check Number: " . $last_check . "\n\n"; $bank_body .= "Out-of-Balance Contact: $from_name $switchboard_phone\n"; $bank_body .= "Technical Contact: $programmer_name $switchboard_phone\n"; // Do the right thing based on the action selected on the HTML form // Convert only and spit-back the file if($_REQUEST[action] == "convert"){ header('Content-type: text/plain'); header("Content-Disposition: attachment; filename=checkfile.txt"); print "$str"; } // Test - Display the file and the email on-screen if($_REQUEST[action] == "test"){ print "$str"; print "
$bank_body"; } // Email everything out as attachments and body text and CC the // person at our company who sent it. // Portions of this code also borrowed from the web and tweaked if($_REQUEST[action] == "email"){ // File Type $fileatt_type = "application/octet-stream"; // Filename that will be used for the file as the attachment $fileatt_name = "riheaa.txt"; // Who the email is from $email_from = $from_email; // The Subject of the email $email_subject = "RIHEAA Customer Issue File"; // Message that the email has in it $email_txt = $bank_body; $email_to = "$bank_email,$from_email,$file_email"; $headers = "From: ".$email_from; $data = $str; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . $email_txt . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) { virtual("../../../endhead.inc"); ?>
The file was successfully sent! Back
virtual("../../..//endbody.inc"); } else { ?>Sorry but the email could not be sent. Please go back and try again!
exit; } } ?>