<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

require('includes/application_top.php');

$enquiry = '';
$name = '';
$fromemail = '';
$toemail = '';

// BOF Anti Robot Validation v2.5
if (ACCOUNT_VALIDATION == 'true' && POLECONY_VALIDATION == 'true') {
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ACCOUNT_VALIDATION);
  include_once('includes/functions/' . FILENAME_ACCOUNT_VALIDATION);
}
// EOF Anti Robot Registration v2.5

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_FORM_POLECONY);

$error = false;
$order_error = false;
if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'send')) {
	// BOF Anti Robotic Registration v2.5
  if (ACCOUNT_VALIDATION == 'true' && POLECONY_VALIDATION == 'true') {
	  $antirobotreg = tep_db_prepare_input($HTTP_POST_VARS['antirobotreg']);
  	$sql = "SELECT * FROM " . TABLE_ANTI_ROBOT_REGISTRATION . " WHERE session_id = '" . tep_session_id() . "' LIMIT 1";
    if( !$result = tep_db_query($sql) ) {
    	$error = true;
      $entry_antirobotreg_error = true;
      $text_antirobotreg_error = ERROR_VALIDATION_1;
    } else {
    	$entry_antirobotreg_error = false;
      $anti_robot_row = tep_db_fetch_array($result);
      if (( strtoupper($HTTP_POST_VARS['antirobotreg']) != $anti_robot_row['reg_key'] ) || ($anti_robot_row['reg_key'] == '') || (strlen($antirobotreg) != ENTRY_VALIDATION_LENGTH)) {
      	$error = true;
        $entry_antirobotreg_error = true;
        $text_antirobotreg_error = ERROR_VALIDATION_2;
      } else {
      	$sql = "DELETE FROM " . TABLE_ANTI_ROBOT_REGISTRATION . " WHERE session_id = '" . tep_session_id() . "'";
        if( !$result = tep_db_query($sql) ) {
        	$error = true;
          $entry_antirobotreg_error = true;
          $text_antirobotreg_error = ERROR_VALIDATION_3;
        } else {
        	$sql = "OPTIMIZE TABLE " . TABLE_ANTI_ROBOT_REGISTRATION . "";
          if( !$result = tep_db_query($sql) ) {
          	$error = true;
            $entry_antirobotreg_error = true;
            $text_antirobotreg_error = ERROR_VALIDATION_4;
          } else {
          	$entry_antirobotreg_error = false;
          }
        }
      }
    }
    if ($entry_antirobotreg_error == true) $messageStack->add('polecony', $text_antirobotreg_error);
  }

  $_POST['fromemail'] = preg_replace( "/\n/", " ", $_POST['fromemail'] );
  $_POST['toemail'] = preg_replace( "/\n/", " ", $_POST['toemail'] );
  $_POST['name'] = preg_replace( "/\n/", " ", $_POST['name'] );
  $_POST['fromemail'] = preg_replace( "/\r/", " ", $_POST['fromemail'] );
  $_POST['toemail'] = preg_replace( "/\r/", " ", $_POST['toemail'] );
  $_POST['name'] = preg_replace( "/\r/", " ", $_POST['name'] );
  $_POST['fromemail'] = str_replace("Content-Type:","",$_POST['fromemail']);
  $_POST['toemail'] = str_replace("Content-Type:","",$_POST['toemail']);
  $_POST['name'] = str_replace("Content-Type:","",$_POST['name']);

  // EOF Anti Robotic Registration v2.5
  $name = tep_db_prepare_input($HTTP_POST_VARS['name']);
  $from_email_address = tep_db_prepare_input($HTTP_POST_VARS['fromemail']);
  $to_email_address = tep_db_prepare_input($HTTP_POST_VARS['toemail']);
  $enquiry = tep_db_prepare_input($HTTP_POST_VARS['enquiry']);
  $name = $name;
  $temat = MakeUTF(EMAIL_SUBJECT);
  // BOF Anti Robot Registration v2.5
  if (!tep_validate_email($to_email_address)) {
  
      	$error = true;
        $messageStack->add('polecony', POLEC_EMAIL_ERROR);
        
	} elseif (!$entry_antirobotreg_error == true) {
        
        mail_polecony($to_email_address, $temat, $name, $from_email_address, $enquiry);
        
    tep_redirect(tep_href_link(FILENAME_FORM_POLECONY, 'action=success'));
	}

  // EOF Anti Robotic Registration v2.5

} elseif (tep_session_is_registered('customer_id')) {

  $customer_order = tep_db_query("select count(*) as total from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' and orders_status = '".PROMO_POLECONY_ORDER_STATUS_ID."'");
  $check = tep_db_fetch_array($customer_order);
  
  if ($check['total'] < '1') {
  	$order_error = true;
    $messageStack->add('polecony', ENTRY_ORDERS_CHECK_ERROR);
  }

  $account_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
  $account = tep_db_fetch_array($account_query);

  $from_name = $account['customers_firstname'] . ' ' . $account['customers_lastname'];
  $from_email_address = $account['customers_email_address'];
}

$enquiry = "";
$name = "";
$fromemail = "";
$toemail = "";

$breadcrumb->add(HEADING_TITLE, tep_href_link(FILENAME_FORM_POLECONY, '', 'NONSSL'));

$content = CONTENT_FORM_POLECONY;

include (bts_select('main', $content)); // BTSv1.5

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>