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

require('includes/application_top.php');

$from_email_address = '';
$from_name = '';
$message = '';

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

if (!tep_session_is_registered('customer_id') && (ALLOW_GUEST_TO_TELL_A_FRIEND == 'false')) {
	$navigation->set_snapshot();
  tep_redirect(tep_href_link(FILENAME_LOGIN, 'act=register', 'SSL'));
}

$valid_product = false;
if (isset($HTTP_GET_VARS['products_id'])) {
	$product_info_query = tep_db_query("select pd.products_name, p.products_model,  p.products_image from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
  if (tep_db_num_rows($product_info_query)) {
  	$valid_product = true;
    $product_info = tep_db_fetch_array($product_info_query);
  }
}

if ($valid_product == false) {
	tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $HTTP_GET_VARS['products_id']));
}

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

$error = false;
$ciag_bladu = '';

if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
	// BOF Anti Robotic Registration v2.5
  if (ACCOUNT_VALIDATION == 'true' && ASKAQUESTION_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) $ciag_bledu .= '* '.$text_antirobotreg_error.'<br>';
  }

  $_POST['from_email_address'] = preg_replace( "/\n/", " ", $_POST['from_email_address'] );
  $_POST['from_name'] = preg_replace( "/\n/", " ", $_POST['from_name'] );
  $_POST['from_email_address'] = preg_replace( "/\r/", " ", $_POST['from_email_address'] );
  $_POST['from_name'] = preg_replace( "/\r/", " ", $_POST['from_name'] );
  $_POST['from_email_address'] = str_replace("Content-Type:","",$_POST['from_email_address']);
  $_POST['from_name'] = str_replace("Content-Type:","",$_POST['from_name']);
  // EOF Anti Robotic Registration v2.5

  $from_email_address = tep_db_prepare_input($HTTP_POST_VARS['from_email_address']);
  $from_name = tep_db_prepare_input($HTTP_POST_VARS['from_name']);
  $message = tep_db_prepare_input($HTTP_POST_VARS['enquiry']);

  if (empty($from_name)) {
  	$error = true;
    $ciag_bledu .= '* '.ERROR_FROM_NAME.'<br>';
  }
  if (!tep_validate_email($from_email_address)) {
  	$error = true;
    $ciag_bledu .= '* '.ERROR_FROM_ADDRESS.'<br>';    
  }

  if ($error == false) {
  
    $nazwa_produktu = $product_info['products_name'];
    $model_produktu = $product_info['products_model'];
    $id_produktu = $HTTP_GET_VARS['products_id'];
    
    mail_ask_a_question($nazwa_produktu,$model_produktu,$from_name,$message,$from_name,$nadawca, $from_email_address,$owner,$id_produktu);

    $messageStack->add_session('header', sprintf(TEXT_EMAIL_SUCCESSFUL_SENT, $product_info['products_name'], STORE_OWNER), 'success');
    tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $HTTP_GET_VARS['products_id']));
  } else {
    if (tep_not_null($ciag_bledu)) {
        $messageStack->add('friend',$ciag_bledu );
    }
  }

} elseif (tep_session_is_registered('customer_id')) {
	$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'];
}

$breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_ASK_QUESTION, 'products_id=' . $HTTP_GET_VARS['products_id']));

$content = 'ask_a_question';

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

require(DIR_WS_INCLUDES . 'application_bottom.php');

?>