<?php
/*
  $Id: send_gv_process.php,v 1.1.1.1 2002/11/28 23:21:38 wilt Exp $

  The Exchange Project - Community Made Shopping!
  http://www.theexchangeproject.org

  Gift Voucher System v1.0
  Copyright (c) 2001,2002 Ian C Wilson
  http://www.phesis.org

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
// low level security check
if (!isset($HTTP_POST_VARS['amount'])) {
  tep_redirect(tep_href_link(FILENAME_DEFAULT));
  }
// extra security. Can only get here from send_gv.php so check this or die suckers
// TODO TODO TODO

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

$salt=$HTTP_POST_VARS['email'];
$gvid = md5(uniqid("","salt"));
$gvid .= md5(uniqid("","salt"));
$gvid .= md5(uniqid("","salt"));
$gvid .= md5(uniqid("","salt"));
srand((double)microtime()*1000000); // seed the random number generator
$random_start = @rand(0, (128-16));
$id1=substr($gvid, $random_start,16);
$gv_query = tep_db_query("select amount from gv_customer where customer_id='".$customer_id."'");
$gv_result=tep_db_fetch_array($gv_query);
$new_amount=$gv_result['amount']-$HTTP_POST_VARS['amount'];
if ($new_amount<0) {
  tep_redirect(tep_href_link(FILENAME_DEFAULT));
}
$gv_query=tep_db_query("update gv_customer set amount='".$new_amount."' where customer_id='".$customer_id."'");
// now create the tracking entry
$gv_query=tep_db_query("select customers_firstname,customers_lastname from ".TABLE_CUSTOMERS." where customers_id='".$customer_id."'");
$gv_customer=tep_db_fetch_array($gv_query);
$gv_query=tep_db_query("insert into gv_tracking (gv_number, date_created, customer_id_sent, sent_firstname, sent_surname, emailed_to, gv_amount) values ('".$id1."', NOW(),'".$customer_id."','".$gv_customer['customers_firstname']."','".$gv_customer['customers_lastname']."','".$HTTP_POST_VARS['email']."','".$HTTP_POST_VARS['amount']."')");
//
// Now send the email to the recipient.
$gv_email = STORE_NAME . "\n" .
            EMAIL_SEPARATOR . "\n" .
            sprintf(EMAIL_GV_TEXT_HEADER,$currencies->format($HTTP_POST_VARS['amount'])) . "\n" .
            EMAIL_SEPARATOR . "\n" . 
            sprintf(EMAIL_GV_FROM, $HTTP_POST_VARS['send_name']) . "\n";
if (isset($HTTP_POST_VARS['message'])) {
  $gv_email .= EMAIL_GV_MESSAGE . "\n";
  if (isset($HTTP_POST_VARS['to_name']))
    {
    $gv_email .= sprintf(EMAIL_GV_SEND_TO,$HTTP_POST_VARS['to_name']) . "\n";
    }
  $gv_email .= $HTTP_POST_VARS['message'] . "\n";
  } 
  $gv_email .= sprintf(EMAIL_GV_REDEEM,$id1) . "\n\n";
  $gv_email .= EMAIL_GV_LINK . HTTP_SERVER  . DIR_WS_CATALOG . FILENAME_GV_REDEEM . '?gv_no='.$id1;
  $gv_email .= "\n\n" . EMAIL_GV_VISIT . HTTP_SERVER  . DIR_WS_CATALOG . FILENAME_GV_REDEEM . EMAIL_GV_ENTER . $id1;  
// echo nl2br($gv_email);
             
 tep_mail('', $HTTP_POST_VARS['email'], EMAIL_GV_TEXT_SUBJECT, nl2br($gv_email), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');

  $breadcrumb->add(NAVBAR_TITLE_1); 

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<script language="javascript" src="includes/swapimage.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<?php
// BOF: WebMakers.com Changed: Header Tag Controller v1.0
// Replaced by header_tags.php
if ( file_exists(DIR_WS_INCLUDES . 'header_tags.php') ) {
  require(DIR_WS_INCLUDES . 'header_tags.php');
} else {
?> 
  <title><?php echo TITLE ?></title>
<?php
}
// EOF: WebMakers.com Changed: Header Tag Controller v1.0
?>
<base href="<?php echo (getenv('HTTPS') == 'on' ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="<? echo THEMA_STYLE;?>">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="3" cellpadding="3">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td align="right"><?php echo tep_image(DIR_WS_IMAGES . 'table_background_contact_us.gif', HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td class="main"><?php echo tep_image(DIR_WS_IMAGES . 'table_background_man_on_board.gif', HEADING_TITLE, '0', '0', 'align="left"') . TEXT_SUCCESS; ?><br><br><?php echo 'gv '.$id1; ?></td>
      </tr>
      <tr>
        <td align="right"><br><a href="<?php echo tep_href_link(FILENAME_DEFAULT, '', 'NONSSL'); ?>"><?php echo tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE); ?></a></td>
      </tr>

    </table></td>
<!-- body_text_eof //-->
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- right_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php 
require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
