2/3
// Set up the default mail headers
$headers = 'MIME-Version: 1.0' . CC_FB_SENDMAIL_EOL .
'Content-Type: text/plain; charset=utf-8' . CC_FB_SENDMAIL_EOL .
'Content-Transfer-Encoding: 7bit' . CC_FB_SENDMAIL_EOL;
// Set up the default owner message if on is not provided
if(CC_FB_OWNER_MESSAGE == '[FORMOWNERMSG]')
{
$form_owner_msg =
'Here is the information submitted to ' .
"{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']} from " .
"{$_SERVER['REMOTE_ADDR']} on " . date("l, F dS, Y \a\\t g:i a") .
".\n------------------------\n$owner_email_response$unreg";
}
else
{
$form_owner_msg = parseMessage(CC_FB_OWNER_MESSAGE, $preferences);
}
// Add the uploaded file as an attachment if the user has
// request we do so
if(CC_FB_ATTACHMENT_ADDTOEMAIL && $_POST['Uploaded_File'] != '')
{
if(!($contents =
file_get_contents(CC_FB_UPLOADS_DIRECTORY .
"/{$_POST['Uploaded_File']}")))
{
printMessage('Unable To Open Attachment File',"We're sorry but " .
'we were unable to open your uploaded file to attatch it for ' .
'email. Please be sure that you have the proper permissions.');
}
$attachment = chunk_split(base64_encode($contents));
// Setup the unique mime boundary
$mime_boundary = md5(time());
// Set up the form owner mail headers
$form_owner_headers = 'MIME-Version: 1.0' . CC_FB_SENDMAIL_EOL .
'Content-Type: multipart/mixed; ' .
"boundary=\"$mime_boundary\"" .
CC_FB_SENDMAIL_EOL;
// Set up the new form owner message
$form_owner_msg =
CC_FB_SENDMAIL_EOL .
"--$mime_boundary" . CC_FB_SENDMAIL_EOL .
'Content-Type: text/plain; charset=utf-8' . CC_FB_SENDMAIL_EOL .
'Content-Transfer-Encoding: 7bit' .
CC_FB_SENDMAIL_EOL. CC_FB_SENDMAIL_EOL .
$form_owner_msg .
CC_FB_SENDMAIL_EOL. CC_FB_SENDMAIL_EOL .
"--$mime_boundary" . CC_FB_SENDMAIL_EOL .
'Content-Type: application/octet-stream ' .
"name=\"{$_POST['Uploaded_File']}\"" . CC_FB_SENDMAIL_EOL .
"Content-Transfer-Encoding: base64" . CC_FB_SENDMAIL_EOL .
"Content-Description: {$_POST['Uploaded_File']}" .
CC_FB_SENDMAIL_EOL .
"Content-Disposition: attachment; " .
"filename=\"{$_POST['Uploaded_File']}\"" .
CC_FB_SENDMAIL_EOL . CC_FB_SENDMAIL_EOL .
"$attachment" . CC_FB_SENDMAIL_EOL. CC_FB_SENDMAIL_EOL;
"--$mime_boundary--" .
CC_FB_SENDMAIL_EOL . CC_FB_SENDMAIL_EOL;
}
else
{
$form_owner_headers = $headers;
}
// If we collected the end-user's email
if($_POST['eM'])
{
// Get all the headers without the From: portion
// so that we can do something fancy if the first
// attempt to send the message fails
$headers_without_from =
"Reply-To: {$_POST['eM']}" . CC_FB_SENDMAIL_EOL .
"Return-Path: {$_POST['eM']}" . CC_FB_SENDMAIL_EOL .
"$cc$bcc" .
'Message-ID: <' . time() . "-{$_POST['eM']}>" . CC_FB_SENDMAIL_EOL .
'X-Mailer: PHP v' . phpversion() . CC_FB_SENDMAIL_EOL .
$form_owner_headers;
// Send a message to the form's owner with the end-user's email
// as the reply-to address.
if(CC_FB_DO_EMAIL &&
!(mail($mail_to,$subject, $form_owner_msg,
"From: {$_POST['eM']}" . CC_FB_SENDMAIL_EOL .
$headers_without_from)) &&
!(mail($mail_to,$subject, $form_owner_msg,
'From: CoffeeCup Flash Form Builder ' .
"<formbuilder@{$_SERVER['SERVER_NAME']}>" . CC_FB_SENDMAIL_EOL .
$headers_without_from)))
{
printMessage('Unable To Send E-Mail',
"We're sorry but we were unable to send your e-mail. " .
'If you are sure that you entered all your email ' .
'addresses properly, you should contact your server ' .
'administrator.');
}
// If necesarry, send a message to the end-user as well.
if(CC_FB_AUTO_REPLY)
{
$form_user_msg = parseMessage('', $preferences);
$form_user_subject = parseMessage(CC_FB_AUTO_REPLY_SUBJECT, $preferences);
if(CC_FB_AUTO_REPLY_FORM_RESULTS)
{
$form_user_msg = CC_FB_AUTO_REPLY_POSITION == 'top' ?
"$form_user_msg\n\n$user_email_response" :
"$user_email_response\n\n$form_user_msg";
}
// Get all the headers without the From: portion
// so that we can do something fancy if the first
// attempt to send the message fails
$headers_without_from =
"Reply-To: $mail_to" . CC_FB_SENDMAIL_EOL .
"Return-Path: $mail_to" . CC_FB_SENDMAIL_EOL .
'Message-ID: <' . time() . "-$mail_to>" .
CC_FB_SENDMAIL_EOL .
'X-Mailer: PHP v' . phpversion() . CC_FB_SENDMAIL_EOL .
$headers;
mail($_POST['eM'],$form_user_subject,
"$form_user_msg$unreg",
"From: $mail_to" . CC_FB_SENDMAIL_EOL .
$headers_without_from) ||
mail($_POST['eM'],$form_user_subject,
"$form_user_msg$unreg",
"From: {$_SERVER['SERVER_NAME']} Form " .
"<forms@{$_SERVER['SERVER_NAME']}>" . CC_FB_SENDMAIL_EOL .
$headers_without_from);
}
}
// Send a message to the form's owner.
elseif(CC_FB_DO_EMAIL && !(mail($mail_to,$subject,
$form_owner_msg,
'From: CoffeeCup Flash Form Builder ' .
"<formbuilder@{$_SERVER['SERVER_NAME']}>" . CC_FB_SENDMAIL_EOL .
"$cc$bcc" .
'Message-ID: <' . time() .
"-formbuilder@{$_SERVER['SERVER_NAME']}>" . CC_FB_SENDMAIL_EOL .
'X-Mailer: PHP v' . phpversion() . CC_FB_SENDMAIL_EOL .
$form_owner_headers)))
{
printMessage('Unable To Send E-Mail',
"We're sorry but we were unable to send your e-mail. " .
'If you are sure that you entered all your email ' .
'addresses properly, you should contact your server ' .
'administrator.');
}
}
/**
* Parses owner-defined email message
*
* Loops through posted form values and replaces all form
* elements in the $message with their corresponding values.
*
* @param string $message an owner-defined email message
* @param array $preferences the CoffeeCup Flash Form Builder Preferences.
*/
function parseMessage($message, $preferences)
{
foreach($preferences['form_fields'] as $key => $value)
{
$message = str_replace('[' . $key . ']', $_POST[$key], $message);
}
return $message;
}
/**
* Gets the real name of the file that was uploaded.
*
* Since the file upload occurs in a different request,
* this method helps us resolve what the name of the
* uploaded file was in case it was renamed.
*/
function fixUploadedFileName()
{
if($_POST['Uploaded_File'] != '')
{
$extension = substr($_POST['Uploaded_File'],
strrpos($_POST['Uploaded_File'], '.'));
$basename = basename($_POST['Uploaded_File'], $extension);
while(file_exists(CC_FB_UPLOADS_DIRECTORY . "/$basename".
CC_FB_UPLOADS_EXTENSION . "$i$extension"))
{
$new_upload_name = "$basename". CC_FB_UPLOADS_EXTENSION .
"$i$extension";
$i++;
}
}
$_POST['Uploaded_File'] = $new_upload_name;
}
/**
* Write form response to a database.
*
* Writes the form response to the database specified at 'CC_FB_DB_ADDRESS'
* if appropriate. If the database doesn't it exist, the CC_FB_DB_TABLE
* table doesn't exist or if the CC_FB_DB_TABLE table doesn't comply with
* the structure of the current form then the database will be restructured
* accordingly.
*
* @param array $preferences the CoffeeCup Flash Form Builder Preferences.
*/
function writeResponseToDatabase($preferences)
{
// If the CC_FB_DB_ADDRESS constant has been populated, then
// the user wants to write their data to a database.
if(CC_FB_DB_ADDRESS != '[ADDRESS]')
{
// First and foremost, lets make sure they have the mysql extension
// loaded.
if(!extension_loaded('mysql'))
{
printMessage('Unable to use MySQL',
"We're sorry but you must have the MySQL extensions loaded " .
'in your PHP configuration in order to save your form '.
'results to a MySQL database. Please contact your ' .
'server administrator.');
}
// Secondly, lets make sure we can connect to their database.
elseif(!($link =
mysql_connect(CC_FB_DB_ADDRESS . ':' . CC_FB_DB_PORT,
CC_FB_DB_USERNAME, CC_FB_DB_PASSWORD)))
{
printMessage('Unable to Connect to Database Server.',
"We're sorry but we were unable to connect to your database " .
'server. Please be sure you have entered your database ' .
'settings correctly.');
}
// Ensure that the connection is utf8 encoded
@mysql_query("SET NAMES 'utf8'", $link);
// If we can't select their DB, lets try to create our own.
if(!mysql_select_db(CC_FB_DB_NAME, $link))
{
if(!mysql_query('CREATE DATABASE ' . CC_FB_DB_NAME, $link))
{
printMessage('Unable to Create Database.',
"We're sorry but we were unable to create your database. " .
'If you believe the database already exists, please ' .
'be sure that you have the proper permissions to ' .
'select it. Otherwise, please be sure that you ' .
'have permissions to create databases. If you ' .
'are still experiencing troubles, please contact ' .
'your server administrator.');
}
elseif(!mysql_select_db(CC_FB_DB_NAME, $link))
{
printMessage('Unable to select Database.',
"We're sorry but we were unable to select your database. " .
'Please be sure that you have the proper permissions to ' .
'select it. If you are still experiencing trouble, ' .
'please contact your server administrator.');
}
}
// If a form_results table exists, make sure it is in the
// proper format.
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . CC_FB_DB_TABLE .
"'", $link)) != 0)
{
if(!($results = mysql_query('SHOW COLUMNS FROM `' . CC_FB_DB_TABLE .
'`', $link)))
{
printMessage('Unable to Query Database.',
"We're sorry but we were unable to query your database " .
'table. Please be sure that you have the proper ' .
'permissions to select from the ' . CC_FB_DB_TABLE .
' table. If you are still experiencing trouble, ' .
'please contact your server administrator.');
}
while($row = mysql_fetch_assoc($results))
{
if($row['Field'] != 'id' && $row['Field'] != 'created_at')
{
$columns[$row['Field']] = $row;
}
}
if(!formFieldsEqualsTableFields($preferences['form_fields'],
$columns))
{
archiveOldTable($link);
createTableFromFormFields($preferences['form_fields'], $link);
}
}
// Otherwise create the CC_FB_DB_TABLE table in the proper format.
else
{
createTableFromFormFields($preferences['form_fields'], $link);
}
// If all went well, lets attempt to write the form results to
// the database.
foreach($preferences['form_fields'] as $field_name => $field)
{
$query .= "`$field_name` = " .
mysqlEscape($_POST[$field_name], $link) . ',';
}
// Add the uploaded file to the query if necessary
if(CC_FB_ATTACHMENT_SAVETODB)
{
if($_POST['Uploaded_File'] != '')
{
if(!($contents =
file_get_contents(CC_FB_UPLOADS_DIRECTORY .
"/{$_POST['Uploaded_File']}")))
{
printMessage('Unable To Open Attachment File',"We're sorry " .
'but we were unable to open your uploaded file to ' .
'attach it for email. Please be sure that you have the ' .
'proper permissions.');
}
$query .= '`uploaded_file_name` = ' .
mysqlEscape($_POST['Uploaded_File'], $link) . ',' .
'`uploaded_file` = ' . mysqlEscape($contents, $link) .
',';
}
else
{
$query .= "`uploaded_file_name` = '',`uploaded_file` = '',";
}
}
if(!mysql_query('INSERT INTO `' . CC_FB_DB_TABLE . '` SET ' .
$query . "`created_at` = NOW()", $link))
{
printMessage('Unable to Insert Into Database Table.',
"We're sorry but we were unable to insert the form results " .
'into your database table. Please be sure that you have ' .
'the proper permissions to insert data into the ' .
CC_FB_DB_TABLE . ' table. If you are still experiencing ' .
'trouble, please contact your server administrator.');
}
}
}