<?php
//observe for form submission
$Thanks = '';
if((int)$_POST['formid'] > 0)
{
$id = (int)$_POST['formid'];
$validation = array();
//spam check if enabled
$formCapEnabled = $mysql->num_rows($mysql->query("ID", "fs_forms", "ID = $id AND `use_recaptcha` = 'y' LIMIT 1"));
if($formCapEnabled == 1 && !$form->recaptchaConfirm($_POST['g-recaptcha-response']))
{
$validation[$id][] = '<div class="validation-advice" style="display: block;">Sorry, your anti-spam answer is incorrect.</div>';
}
//validation
foreach($_POST['form'] as $field_id => $value)
{
$field_id = str_replace('f', '', $field_id);
$field_id = (int)$field_id;
$query = $mysql->query("label", "fs_forms_fields", "form_id = $id AND ID = $field_id AND required = 'y' AND `type` != 'Checkbox' AND `type` != 'Radio' AND `type` != 'Upload' LIMIT 1");
if($mysql->num_rows($query) == 1 && trim($value) == '')
{
$lb = $mysql->get_array($query);
$lb['label'] = str_replace(":", "", trim($lb['label']));
$validation[$id][] = '<div class="validation-advice" style="display: block;"><b>'.$lb['label'].'</b> is a required field.</div>';
}
}
//
$query = $mysql->query("email_to,subject,thankyou_msg,redirect", "fs_forms", "ID = $id LIMIT 1");
if($mysql->num_rows($query) == 1 && sizeof($validation[$id]) == 0)
{
$formTable = $mysql->get_array($query);
$msg = '';
foreach($_POST['form'] as $field_id => $value)
{
$field_id = str_replace('f', '', $field_id);
$field_id = (int)$field_id;
$lbl =  $mysql->get_array($mysql->query("label", "fs_forms_fields", "ID = $field_id LIMIT 1"));
$lb['label'] = str_replace(":", "", trim(stripslashes($lbl['label'])));
if(is_array($value))
{
$msg .= str_replace("_", " ", $lbl['label']).': '.implode(", ", $value)."\n\n";
}
else
{
$msg .= str_replace("_", " ", $lbl['label']).': '.$value."\n\n";
}
}
//preparing attachments
$attach = array();
if(sizeof($_FILES) > 0)
{
foreach($_FILES as $name => $f)
{
if(!empty($_FILES[$name]['tmp_name']))
{
$attach[$_FILES[$name]['name']] = $_FILES[$name]['tmp_name'];
}
}
}
//send
$formTable['email_to'] = str_replace('&#8218;', ',', $formTable['email_to']);
$subject = ($formTable['subject'] == '') ? 'Form submission from '.$_SERVER['SERVER_NAME'] : $formTable['subject'];
emailWithPhpMailer($formTable['email_to'], $subject, $msg, 'noreply@'.str_replace("www.", "", $_SERVER['SERVER_NAME']), false, '', '', '', $attach);
}
if(!empty($formTable['redirect']))
{
header("Location: ".stripslashes($formTable['redirect']));
exit;
}
$Thanks = nl2br(stripslashes($formTable['thankyou_msg'])).'<script>document.observe("dom:loaded", function(){$(\'thanks2\').scrollTo();});</script>';
unset($_POST);
}
//check for forms, replaces images with form html
$active = settings('form_creator_enabled');
if(strstr($mainhtml, 'class="form"') || strstr($mainhtml, 'class=form'))
{
preg_match_all('/<img[^>]+>/i', $mainhtml, $allImages);
if(is_array($allImages[0]))
{
foreach($allImages[0] as $key => $image)
{
if(strstr($image, 'form'))
{
list($junk, $id) = explode("?id=", $image);
$form_content = ($active['form_creator_enabled'] == 'y') ? $site->show_form($id, $Thanks) : '';
//validation errrors
$id = (int)$id;
$vali_content = '';
if(sizeof($validation[$id]) > 0)
{
$vali_content = implode('', $validation[$id]);
}
////////////////////
$mainhtml = str_replace($image, $vali_content.$form_content, $mainhtml);
}
}
}
//load prototype
if(!strstr($mainhtml, 'prototype.js'))
{
$mainhtml = str_replace("</head>", '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script></head>', $mainhtml);
}
//load vlidation script
if(!strstr($mainhtml, 'validation.js'))
{
$mainhtml = str_replace("</head>", '<style>.validation-advice{font-weight:bold;color:red;}</style><script type="text/javascript" src="/inc/blog/src/validation.js"></script></head>', $mainhtml);
}
//include this add-ons's overwrite css file from the template being used, if there is one
$t = settings('template');
if(file_exists('templates/'.$t['template'].'/addons/form.css'))
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/form.css" /></head>', $mainhtml);
}
else
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="//www.freesites.com.au/addons/form.css" /></head>', $mainhtml);
}
//load spam cap if enabled for this form
$formCapEnabled = $mysql->num_rows($mysql->query("ID", "fs_forms", "ID = $id AND `use_recaptcha` = 'y' LIMIT 1"));
if($formCapEnabled == 1)
{
$mainhtml = str_replace("</head>", $form->recaptchaHeaderCode().'</head>', $mainhtml);
}
//load jquery css
if(!strstr($mainhtml, 'jquery-ui.css') && strstr($mainhtml, 'class="calendar"'))
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"></head>', $mainhtml);
}
//load jquery ui
if(!strstr($mainhtml, 'jquery-ui.js') && strstr($mainhtml, 'class="calendar"'))
{
$mainhtml = str_replace("</head>", '<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head>', $mainhtml);
}
//load jquery calednar call
if(strstr($mainhtml, 'class="calendar"'))
{
$mainhtml = str_replace("</head>", '<script>jQuery(function(){jQuery(".calendar").datepicker({dateFormat: \'dd/mm/yy\'});});</script></head>', $mainhtml);
}
//end
}
?>