Zen Cart Logo
Forums / All Other Contributions/Addons / Using two modules add extra fields and recaptcha to contact form

Using two modules add extra fields and recaptcha to contact form

Locked

Views: 2,465

Results 1 to 3 of 3
This thread is locked. New replies are disabled.
17 Feb 2010, 1:18 PM
#1
webmiss avatar

webmiss

Zen Follower

Join Date:
Feb 2006
Posts:
147
Plugin Contributions:
0

Using two modules add extra fields and recaptcha to contact form

Hi,

I'm using version 1.3.7.1 and client doesn't want to upgrade. He's been getting a lot of spam through his contact form. I had previously installed the extra fields to contact form which is working just fine, but also wanted to add the recaptcha module to the contact form as well.

There seems to be one file which both modules have been modified: includes/modules/pages/contact_us/header_php.php

The add extra fields header_php.php file shows the following:
require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));

$error = false;
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
$phone = zen_db_prepare_input($_POST['phonenumber']);
$streetaddress = zen_db_prepare_input($_POST['streetaddress']);
$city = zen_db_prepare_input($_POST['city']);
$yourstate = zen_db_prepare_input($_POST['yourstate']);
$zip = zen_db_prepare_input($_POST['zip']);
$payment = zen_db_prepare_input($_POST['payment']);
$card = zen_db_prepare_input($_POST['card']);
$name = zen_db_prepare_input($_POST['contactname']);
$email_address = zen_db_prepare_input($_POST['email']);
$enquiry = zen_db_prepare_input(strip_tags($_POST['enquiry']));

$zc_validate_email = zen_validate_email($email_address);

if ($zc_validate_email and !empty($enquiry) and !empty($name)) {
// auto complete when logged in
if($_SESSION['customer_id']) {
$sql = "SELECT customers_id, customers_firstname, customers_lastname, customers_password, customers_email_address, customers_default_address_id, customers_telephone
FROM " . TABLE_CUSTOMERS . "
WHERE customers_id = :customersID";

  $sql = $db->bindVars($sql, ':customersID', $_SESSION['customer_id'], 'integer');
  $check_customer = $db->Execute($sql);
  $customer_email= $check_customer->fields['customers_email_address'];
  $customer_phone= $check_customer->fields['customers_telephone'];
  $customer_name= $check_customer->fields['customers_firstname'] . ' ' . $check_customer->fields['customers_lastname'];
} else {
  $customer_phone='Not logged in';
  $customer_email='Not logged in';
  $customer_name='Not logged in';
}

// use contact us dropdown if defined
if (CONTACT_US_LIST !=''){
  $send_to_array=explode("," ,CONTACT_US_LIST);
  preg_match('/\<[^>]+\>/', $send_to_array[$_POST['send_to']], $send_email_array);
  $send_to_email= eregi_replace (">", "", $send_email_array[0]);
  $send_to_email= eregi_replace ("<", "", $send_to_email);
  $send_to_name = preg_replace('/\<[^*]*/', '', $send_to_array[$_POST['send_to']]);
} else {  //otherwise default to EMAIL_FROM and store name
$send_to_email = EMAIL_FROM;
$send_to_name =  STORE_NAME;
}

// Prepare extra-info details

$extra_info = email_collect_extra_info($name, $email_address, $customer_name, $customer_email, $customer_phone);
// Prepare Text-only portion of message
$text_message = OFFICE_FROM . "\t" . $name . "\n" .
OFFICE_PHONE . "\t" . $phone . "\n" .
OFFICE_STREETADDRESS . "\t" . $streetaddress . "\n" .
OFFICE_CITY . "\t" . $city . "\n" .
OFFICE_YOURSTATE . "\t" . $yourstate . "\n" .
OFFICE_ZIP . "\t" . $zip . "\n" .
OFFICE_PAYMENT . "\t" . $payment . "\n" .
OFFICE_CARD . "\t" . $card . "\n" .
OFFICE_EMAIL . "\t" . $email_address . "\n\n" .
'------------------------------------------------------' . "\n\n" .
strip_tags($_POST['enquiry']) . "\n\n" .
'------------------------------------------------------' . "\n\n" .
$extra_info['TEXT'];
// Prepare HTML-portion of message
$html_msg['EMAIL_MESSAGE_HTML'] = strip_tags($_POST['enquiry']);
$html_msg['CONTACT_US_OFFICE_FROM'] = OFFICE_FROM . ' ' . $name . '<br />' . OFFICE_PHONE . ' ' . $phone . '<br />' . OFFICE_EMAIL . '(' . $email_address . ')';
$html_msg['EXTRA_INFO'] = $extra_info['HTML'];
// Send message
zen_mail($send_to_name, $send_to_email, EMAIL_SUBJECT, $text_message, $name, $email_address, $html_msg,'contact_us');

zen_redirect(zen_href_link(FILENAME_CONTACT_US, 'action=success'));

} else {
$error = true;
if (empty($name)) {
$messageStack->add('contact', ENTRY_EMAIL_NAME_CHECK_ERROR);
}
if ($zc_validate_email == false) {
$messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
}
if (empty($phone)) {
$messageStack->add('contact', ENTRY_EMAIL_PHONE_CHECK_ERROR);
}
if (empty($enquiry)) {
$messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);
}
}
} // end action==send

// default email and name if customer is logged in
if($_SESSION['customer_id']) {
$sql = "SELECT customers_id, customers_firstname, customers_lastname, customers_password, customers_email_address, customers_default_address_id,customers_telephone
FROM " . TABLE_CUSTOMERS . "
WHERE customers_id = :customersID";

$sql = $db->bindVars($sql, ':customersID', $_SESSION['customer_id'], 'integer');
$check_customer = $db->Execute($sql);
$phone= $check_customer->fields['customers_telephone'];
$email= $check_customer->fields['customers_email_address'];
$name= $check_customer->fields['customers_firstname'] . ' ' . $check_customer->fields['customers_lastname'];
}

if (CONTACT_US_LIST !=''){
foreach(explode(",", CONTACT_US_LIST) as $k => $v) {
$send_to_array[] = array('id' => $k, 'text' => preg_replace('/<[^]/', '', $v));
}
}

// include template specific file name defines
$define_page = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/html_includes/', FILENAME_DEFINE_CONTACT_US, 'false');

$breadcrumb->add(NAVBAR_TITLE);
?>

The recaptcha header_php.php file shows the following:
require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
require(DIR_WS_CLASSES . 'recaptchalib.php');

$error = false;

if (isset($_GET['action']) && ($_GET['action'] == 'send')) {

$name = zen_db_prepare_input($_POST['contactname']);

$email_address = zen_db_prepare_input($_POST['email']);

$enquiry = zen_db_prepare_input(strip_tags($_POST['enquiry']));

$zc_validate_email = zen_validate_email($email_address);

if(CONTACT_US_RECAPTCHA_STATUS == 'true')
{
	// start modification for reCaptcha
	// the response from reCAPTCHA
	$resp = null;

	// was there a reCAPTCHA response?
	$resp = recaptcha_check_answer (CONTACT_US_RECAPTCHA_PRIVATE_KEY,
	$_SERVER["REMOTE_ADDR"],
	$_POST["recaptcha_challenge_field"],
	$_POST["recaptcha_response_field"]);
}

if ($zc_validate_email and !empty($enquiry) and !empty($name)) {

// auto complete when logged in

if($_SESSION['customer_id']) {

  $sql = "SELECT customers_id, customers_firstname, customers_lastname, customers_password, customers_email_address, customers_default_address_id 

          FROM " . TABLE_CUSTOMERS . " 

          WHERE customers_id = :customersID";

  

  $sql = $db->bindVars($sql, ':customersID', $_SESSION['customer_id'], 'integer');

  $check_customer = $db->Execute($sql);

  $customer_email= $check_customer->fields['customers_email_address'];

  $customer_name= $check_customer->fields['customers_firstname'] . ' ' . $check_customer->fields['customers_lastname'];

} else {

  $customer_email = NOT_LOGGED_IN_TEXT;

  $customer_name = NOT_LOGGED_IN_TEXT;

}



// use contact us dropdown if defined

if (CONTACT_US_LIST !=''){

  $send_to_array=explode("," ,CONTACT_US_LIST);

  preg_match('/\<[^>]+\>/', $send_to_array[$_POST['send_to']], $send_email_array);

  $send_to_email= eregi_replace (">", "", $send_email_array[0]);

  $send_to_email= eregi_replace ("<", "", $send_to_email);

  $send_to_name = preg_replace('/\<[^*]*/', '', $send_to_array[$_POST['send_to']]);

} else {  //otherwise default to EMAIL_FROM and store name

$send_to_email = EMAIL_FROM;

$send_to_name =  STORE_NAME;

}



// Prepare extra-info details

$extra_info = email_collect_extra_info($name, $email_address, $customer_name, $customer_email);

// Prepare Text-only portion of message

$text_message = OFFICE_FROM . "\t" . $name . "\n" .

OFFICE_EMAIL . "\t" . $email_address . "\n\n" .

'------------------------------------------------------' . "\n\n" .

strip_tags($_POST['enquiry']) .  "\n\n" .

'------------------------------------------------------' . "\n\n" .

$extra_info['TEXT'];

// Prepare HTML-portion of message

$html_msg['EMAIL_MESSAGE_HTML'] = strip_tags($_POST['enquiry']);

$html_msg['CONTACT_US_OFFICE_FROM'] = OFFICE_FROM . ' ' . $name . '<br />' . OFFICE_EMAIL . '(' . $email_address . ')';

$html_msg['EXTRA_INFO'] = $extra_info['HTML'];


/*
*	reCAPTCHA modification begin
*	check for valid response before sending mail
*/
if(CONTACT_US_RECAPTCHA_STATUS == 'true' && $resp->is_valid)
{
	// Send message
	zen_mail($send_to_name, $send_to_email, EMAIL_SUBJECT, $text_message, $name, $email_address, $html_msg,'contact_us');
	zen_redirect(zen_href_link(FILENAME_CONTACT_US, 'action=success'));
}
if(CONTACT_US_RECAPTCHA_STATUS == 'false'){
	// Send message
	zen_mail($send_to_name, $send_to_email, EMAIL_SUBJECT, $text_message, $name, $email_address, $html_msg,'contact_us');
	zen_redirect(zen_href_link(FILENAME_CONTACT_US, 'action=success'));
}


/*
*	reCAPTHCA modification begin
*/	
if(CONTACT_US_RECAPTCHA_STATUS == 'true' && !$resp->is_valid)
{
	$error = true;
	$messageStack->add('contact', ENTRY_SECURITY_CHECK_ERROR);	
}
/*
*	reCAPTCHA modification end
*/

} else {

$error = true;

if (empty($name)) {

  $messageStack->add('contact', ENTRY_EMAIL_NAME_CHECK_ERROR);

}

if ($zc_validate_email == false) {

  $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);

}

if (empty($enquiry)) {

  $messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);

}

/*
*	reCAPTHCA modification begin
*/	
if(CONTACT_US_RECAPTCHA_STATUS == 'true' && !$resp->is_valid)
{
	$messageStack->add('contact', ENTRY_SECURITY_CHECK_ERROR);	
}

}

} // end action==send

// default email and name if customer is logged in

if($_SESSION['customer_id']) {

$sql = "SELECT customers_id, customers_firstname, customers_lastname, customers_password, customers_email_address, customers_default_address_id

      FROM " . TABLE_CUSTOMERS . " 

      WHERE customers_id = :customersID";

$sql = $db->bindVars($sql, ':customersID', $_SESSION['customer_id'], 'integer');

$check_customer = $db->Execute($sql);

$email= $check_customer->fields['customers_email_address'];

$name= $check_customer->fields['customers_firstname'] . ' ' . $check_customer->fields['customers_lastname'];

}

if (CONTACT_US_LIST !=''){

foreach(explode(",", CONTACT_US_LIST) as $k => $v) {

$send_to_array[] = array('id' => $k, 'text' => preg_replace('/\<[^*]*/', '', $v));

}

}

// include template specific file name defines

$define_page = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/html_includes/', FILENAME_DEFINE_CONTACT_US, 'false');

$breadcrumb->add(NAVBAR_TITLE);

?>

Obviously, I don't want to screw up the coding of either file and since I am not a coder, I want to be able to use both. Would anyone here please guide me as to how to integrate the code from both into the header_php.php file?

I'd really appreciate it.

webmiss

22 Feb 2010, 6:20 PM
#2
kamelion0927 avatar

kamelion0927

Zen Follower

Join Date:
Dec 2009
Posts:
208
Plugin Contributions:
1

Re: Using two modules add extra fields and recaptcha to contact form

I'm not a coder either but I wanted an image on my forms to reduce spamming. From what I understand, the recaptcha requires a database to work so what I did, rather than mess with programming one within Zen Cart, is start an account with EmailMeForm.com. For a $3.95 flat rate per form, I got HTML code for a form with a recaptcha code that runs through their data base, the info is emailed to me and I wasn't required to leave their logo on the form.

I then deactivated the Zen Cart Contact Us form and formatted a page in the 'Define Page Editor' within the Zen Cart Admin to act as a Contact Us page using the emailmeform.com html.

Because of my lack of experience, I'm not very good at verbalizing my actions (Sorry!) :blush: but you can see what I did at http://www.t h e b u r n i n g b u t t e r f l y .com/store/index.php?main_page=page_7. My site isn't live yet but the form is active on this page.

Hope this helps!

22 Feb 2010, 6:43 PM
#3
webmiss avatar

webmiss

Zen Follower

Join Date:
Feb 2006
Posts:
147
Plugin Contributions:
0

Re: Using two modules add extra fields and recaptcha to contact form

I appreciate your feedback. You gave me an idea if I can't get the coding right with what I have. I could use the tectite's formmail script. It's free. I've used it on other sites. I was hoping not to have to go outside the box so to speak, but I may have to.

Thanks for the brain jog. It helped.