<?php
//Initialize an empty array for storing filtered, escaped input
$clean = array();
//Wash Basic Input Fields
function washnormal(){
//Use the global $clean array
global $clean;
//Loop through the fields passed to the function
for ($i = 0; $i < func_num_args(); $i++){
$item = func_get_arg($i);
//Regular expression matching for lowercase letters, uppercase letters, digits, periods, commas, and dashes
if(preg_match("/^[a-zA-Z0-9 \.\,\-]*$/", $_POST[$item])){
//htmlentities escapes the output (ex. <b>BOLD</b> vs. BOLD)
$clean[$item] = htmlentities($_POST[$item], ENT_QUOTES, 'UTF-8');
}
}
}
//Wash State and Zip Code
function washgentle($state, $zip){
//Use the global $clean array
global $clean;
//Regular expression matching for 5 digits, a possible dash, and possible 4 digits
if(preg_match("/^[0-9]{5}[\-]?[0-9]{0,4}$/", $_POST[$zip])){
$clean[$zip] = htmlentities($_POST[$zip], ENT_QUOTES, 'UTF-8');
}
//PHP native function for checking for alpha characters only (faster than custom regular expressions)
if(ctype_alpha($_POST[$state])){
$clean[$state] = htmlentities($_POST[$state], ENT_QUOTES, 'UTF-8');
}
}
//Laundry (call the sanitizing functions, passing in the posted field data to be cleaned)
washnormal('callLetters', 'location', 'manager', 'address', 'city', 'npr', 'method', 'agree');
washgentle('state', 'zip');
//Reject Suspect Data (if the sanitization failed, the $clean array variable will not be populated, so reject the data
if(($clean['callLetters'] == null)||($clean['location'] == null)||($clean['manager'] == null)||($clean['address'] == null)||($clean['city'] == null)||($clean['state'] == null)||($clean['zip'] == null)||($clean['npr'] == null)||($clean['method'] == null)||($_POST['agree'] != 'I Agree')){
header("Location: http://www.nrao.edu/cosmicradio/test.php");
} else {
//If all the data passed sanitization, continue submitting the data
echo "Thanks, <strong>" . $clean['manager'] . "</strong>. Your zip code is " . $clean['zip'] . ".";
$to = 'email@domain.abc';
$subject = "Test Sanitize";
$message = "Station Call Letters: " . $clean['callLetters'] . "\n" .
"Station Location: " . $clean['location'] . "\n" .
"Station Manager: " . $clean['manager'] . "\n" .
"Station Address: " . $clean['address'] . "\n" .
" " . $clean['city'] . ", " . $clean['state'] . " " . $clean['zip'] . "\n" .
"NPR Affiliate: " . $clean['npr'] . "\n" .
"Delivery Method: " . $clean['method'] . "\n" .
"Station Cannot Sell Advertising Sponsorship: " . $clean['agree'] . "\n";
$headers = 'From: email@domain.abc' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>