PHP Code:
<?php
# THIS IS A TEST SCRIPT AND NEEDS TO BE TESTED AND DEBUGGED BEFORE A USER GETS IT
include_once('dbg.php');
include_once('LocalDB.php');
include_once('normal_quotes.php');
include_once('FormData.php');
//$dbgLvl = 1; # uncomment to debug
$fields = new FormData(
array(
'EmailFrom' => array('type'=>FILTER_VALIDATE_EMAIL,
'param' => ':email', 'label' => 'e-mail'),
'FirstName' => array('type' => 'nonempty',
'param' => ':first', 'label' => 'first name'),
'LastName' => array('type' => 'nonempty',
'param' => ':last', 'label' => 'last name'),
'Tel' => array('type' => 'phone',
'param' => ':phone', 'label' => 'phone number'),
'division' => array('type' => 'nonempty',
'param' => ':div', 'label' => 'division'),
'adv' => array('type' => 'nonempty',
'param' => ':adv', 'label' => 'advertising')
));
# ============================
# info for MySQL database
$opts = array(
'db' => "myCpanelName_databaseName", # MySQL Database name - do not forget the underscore
'table' => "tableName", # MySQL Table name
'errorURL' => 'http://www6.semo.edu/rball/error.htm',
'successURL' => 'http://www6.semo.edu/rball/thankyou.htm',
'event' => 'Redhawks Racquetball Tournament',
'date' => 'November 20th',
'timezone' => 'America/Chicago', # tournament isn't in Chicago, but is in same timezone
'startTime' => '10:00am'
);
date_default_timezone_set($opts['timezone']);
$postTime = strftime('%F %T');
# ============================
# info for Email
$mail = array(
# This Email address should be at your site
# most Email clients (AOL, Yahoo, Gmail) will drop or return Email from x10Hosting 'free' servers
'to' => "fakeemail@gmail.com", # **** set this to true Email address ****
# with luck if Email is returned from "$to" - "$logKeeper" will get the notice
'logKeeper' => "me@mysite.com", # **** set this to Email account at your site ****
# to send a 'cc' - uncomment & set following
//'ccTo' => "me@myOtherDomain.com", # **** set this to Email a copy to 'Cc:' ****
'subject' => "Rball_Registration",
);
if ("\n" == "\xD\xA") {
$eol="\n";
$cr=''
} else {
$eol="\r\n";
$cr="\r";
}
function fail($msg) {
/* TODO: failure should give better feedback, perhaps by passing a message (via a cookie?)
through to the error page.
*/
global $opts;
header("Location: $opts[errorURL]");
dbgMsg($msg);
}
function succeed($msg) {
global $opts;
header("Location: $opts[successURL]");
dbgMsg($msg);
}
# Form processing starts here
if (($failed = $fields->validate())) {
# TODO: tell user why validation failed.
fail("Input failed validation.");
} else {
$who = "_POST[FirstName] $_POST[LastName]";
$mail['from'] = $fields->getValue('EmailFrom');
# ============================
# We have data so set up Email
# prepare email body text. _POST has been sanitized.
$Body = <<<EOF
FirstName: $_POST[FirstName]$cr
LastName: $_POST[LastName]$cr
Tel: $_POST[Tel]$cr
Division: $_POST[division]$cr
Email: $_POST[EmailFrom]$cr
Advertising: $_POST[adv]$cr
$postTime$cr
EOF;
$additional_headers = "From: $mail[from]${eol}Reply-To: $mail[logKeeper]${eol}";
if (isset($mail['ccTo'])) {
$additional_headers .= "Cc: $mail[ccTo]${eol}";
}
# send email
if (mail($mail['to'], $mail['subject'], $Body, $additional_headers)) {
/* TODO: fix the name used to address a registrant.
The first name isn't necessarily the short form a person's name, depending on
their culture. However, someone living in the US will probably follow western
naming conventions, and in some cultures that don't use western conventions
(e.g. Japanese), the first name can still be used as a short form of address.
*/
$Confirmation = <<<EOF
Thanks for Registering!!
$FirstName, You have successfully registered for the $opts[event] on -$opts[date]-.
You can check the registered list on the website to make sure you are included sometime tomorrow.
Please remember matches begin at -$opts[startTime]-.
EOF;
if (mail($mail['from'], $opts['event'], $Confirmation)) {
succeed();
} else {
fail("Sending confirmation e-mail to $mail[from] failed\n");
}
} else { # !mail($mail['to'], ...
fail("Sending information e-mail to $mail[to] failed\n");
}
# ============================
# sent Email now put copy of data into log file
# Write Email info to log file
# use 'a' to open for writing only; place the file pointer at the end of the file.
# If the file does not exist, attempt to create it.
#
# use 'b' to force binary mode
#
# If the open fails, an error of level E_WARNING is generated. Use @ to suppress this warning.
#
if (!($logFile = fopen("registered.log","ab"))) { # **** look for this file in dir where this script runs ****
# you may rem out these two lines and/or make some other error for user
dbgMsg("Cannot open log file\n");
if ($dbgLvl) exit(); # this will be full stop error or rem out so user will not see it
} else {
$output = "---Registration for $who---$eol$Body$eol";
if (fwrite($logFile,$output) === FALSE) { # note the '===' and not '=='
# you may rem out these two lines and/or make some other error for user
dbgMsg("Cannot write to log file<br>\n");
if ($dbgLvl) exit(); # this will be full stop error or rem out so user will not see it
}
fclose($logFile);
}
# ============================
# now put copy into MySQL database
try {
$stage = 'connecting to database';
$db = LocalDB::connect($opts['db']);
dbgMsg("Connected to MySQL server successfully\n");
$stage = 'saving to database';
$fields->store($db, $opts['db'], $opts['table']);
dbgMsg("Data added to database");
} catch (PDOException $exc) {
dbgMsg("Error when $stage.");
# TODO: better error logging
error_log("Error storing registration for $who: $exc");
}
}
?>