#!/usr/local/bin/perl
#
# Program name:  secure.pl
# Author: Todd Hassan
# Creation date:  8/15/97
# Last modified: 2/25/98
# Description:  This module allows Hassan Consulting's Shopping Cart
# to work with secure transactions
#
# Version 1.01
#
# Version history
#
# 1.01 fixed comments field

sub secure_mail_list
{
  &read_list;

  $info_buffer = '';
  $header_buffer = '';
  $cc_buffer = '';
  $order_buffer = '';

  for ($i=1; $i<=$num_lines; $i++)
  {
    $item_no = $ITEMS{$i};
    &lookup_item($item_no); 
  }

  $item_total = 0;
  $subtotal = 0;
  $output = "";

  # for NT only

  $mail_file = "$temp_location/shop$$.txt";

  #  Do some checks on the input

  &check_email_form;

  $item_total = 0;
  $subtotal = 0;
  $mail1 = "";
  if ($NT)
  {
    $mail = "$mail_location $mail_file -t $admin -f $admin -s \"$admin_subject\"";
    if ($admin1 ne "")
    {    
      $mail1 = "$mail_location $mail_file -t $admin1 -f $admin -s \"$admin_subject\"";
    }
    if (!(open(MAIL, ">$mail_file")))
    {
      &html_header;
      print("Error opening mail file: $mail_file<P>\n");
      exit(1);
    }
  }
  else
  {
    $mail = "$mail_location -oi -t";
    open(MAIL, "| $mail") || &sys_error("$prog: Can't open pipe to sendmail: $!");
    if ($admin1 ne "")
    { 
      $header_buffer .= "To: $admin, $admin1\r\n";
    }
    else
    { 
      $header_buffer .= "To: $admin\r\n";
    }
    $header_buffer .= "X-Mailer: Hassan Consulting Shopping Cart \r\n";
    $header_buffer .= "Mime-Version: 1.0\r\n";
    $header_buffer .= "Content-Type: text/plain; charset=$char_set\r\n";
    $header_buffer .= "From: $admin\r\n";
    $header_buffer .= "Reply-to: $POST{email}\r\n";
    $header_buffer .= "Subject: $admin_subject\r\n\r\n";
  }
  if ($header_file ne "")
  {
    open(HEAD, $header_file);
    while (!(eof(HEAD)))
    { 
      $info_buffer .= <HEAD>;
    }
    close(HEAD);
  }
  $info_buffer .= "Name: $POST{name}\r\n";
  $info_buffer .= "Email: $POST{email}\r\n";
  $info_buffer .= "Shipping Address: $POST{street}\r\n";
  if ($POST{street2} ne "")
  {
    $info_buffer .= "$POST{street2}\r\n";
  }
  $info_buffer .= "$POST{city} $POST{state} $POST{zip} $POST{country}\r\n";
  if ($POST{bstreet} ne "")
  {
    $info_buffer .= "Billing Address: $POST{bstreet}\r\n";
    if ($POST{bstreet2} ne "")
    {
      $info_buffer .= "$POST{bstreet2}\r\n";
    }
    $info_buffer .= "         $POST{bcity} $POST{bstate} $POST{bzip} $POST{bcountry}\r\n";
  }
  $info_buffer .= "Phone: $POST{phone}\r\n";
  if ($POST{company} ne "")
  {
    $info_buffer .= "Company Name: $POST{company}\r\n";
  }
  if ($POST{comments} ne "")
  {
    $info_buffer .= "Comments: $POST{comments}\r\n";
  }
  if ($POST{payment_type} ne "")
  {
    $info_buffer .= "Payment Type: $POST{payment_type}\r\n";
  }
  if (($POST{payment_type} ne 'check')&&($POST{payment_type} ne 'money_order')&&($POST{payment_type} ne 'wire_transfer'))
  {
    $cc_buffer .= "Credit Card Type: $POST{cctype}\r\n";
    $cc_buffer .= "Expiration Date: $POST{ccexpiry}\r\n";
    $cc_buffer .= "Credit Card #: $POST{ccard}\r\n";
    $cc_buffer .= "Exact Name: $POST{ccname}\r\n";
  }
  $order_buffer .= "\r\nOrder:\r\n";
  $order_buffer .= "-------------------------------------------------------------------\r\n";

  if ($num_lines > 0)
  {
    for ($i=1; $i<=$num_lines; $i++)
    {
      $item_no = $ITEMS{$i};
      $color = $COLORS{$i};
      $size = $SIZES{$i};
      &lookup_item($item_no);   
      $order_buffer .= sprintf("%-4s %-7s %-45s %8s\r\n", $NUM{$i}, $product_num, $product_name, $product_price); 
      $doit=0;
      if ($color ne "")
      {
        $order_buffer .= "Color: $color  ";
        $doit=1;
      }
      if ($size ne "")
      {
        $order_buffer .= "Size: $size";
        $doit=1;
      }
      if ($doit)
      {
        $order_buffer .= "\r\n";
      }
      $subtotal += $product_price * $NUM{$i};
      $item_total += $NUM{$i};
    }
    
    $subtotal = &round($subtotal);
    $temp = $subtotal;
    $temp1 = $item_total;
    $order_buffer .= sprintf("\r\nTotal # items: %-4s                             Subtotal  $email_currency_symbol%8s\r\n", $temp1, $temp);

    if ($DISCOUNT != 0)
    {
      $discount = &round($subtotal * $DISCOUNT);
      $subtotal = &round($subtotal - $discount);
      @j_data = split(/\./, $DISCOUNT);
      $temp = "$j_data[1]% Discount";
      $minus = "-$discount";
      $order_buffer .= sprintf("                                            %s  $email_currency_symbol%8s\r\n", $temp, $minus);
      $order_buffer .= sprintf("                                            New Subtotal  $email_currency_symbol%8s\r\n", $subtotal);
    }

    $shipping = &calc_shipping($subtotal);
    $my_temp = "( $shipping_label{$shipby} )";
    if ($shipping_label{1} ne "")
    { 
      $order_buffer .= sprintf("                          %20s  Shipping  $email_currency_symbol%8s\r\n",$my_temp,$shipping);
    }

    if ($POST{bstate} ne "")
    {
	$state = $POST{bstate};
    }
    else
    { 
 	$state = $POST{state};
    }  
    if (("\U$state\E" eq $STATE)&&($TAX))
    {
      if ($CALC_TAX_ON_SHIPPING eq 'N')
      {
        $tax = ($subtotal) * $tax_rate;
      }
      else
      {
        $tax = ($subtotal + $shipping) * $tax_rate;
      }
      $tax = &round($tax);
      $order_buffer .= sprintf("                                                     Tax  $email_currency_symbol%8s\r\n", $tax);
      $total = &round($tax + $subtotal+ $shipping);
    }
    else
    {
      $total = &round($subtotal + $shipping);
    }
    $total = &round($total);
    $order_buffer .= sprintf("                                                   Total  $email_currency_symbol%8s\r\n", $total);
  }     
  &check_gift;
  if ($footer_file ne "")
  {
    open(FOOT, $footer_file);
    while (!(eof(FOOT)))
    { 
      $order_buffer .= <FOOT>;
    }
    close(FOOT);
  }
  print MAIL "$header_buffer";
  print MAIL "$info_buffer";
  print MAIL "$order_buffer";
  close (MAIL) || &sys_error("$prog: Error closing pipe to sendmail: $!");;
  if ($NT)
  {
    `$mail`;
    if ($mail1 ne "")
    {
      `$mail1`;
    }
  }

# send receipt to customer  

  if ($NT)
  {
    $mail = "$mail_location $mail_file -t $POST{email} -f $admin -s \"$customer_subject\"";

    if (!(open(MAIL, ">$mail_file")))
    {
      &html_header();
      print("Error opening mail file: $mail_file<P>\n");
      exit(1);
    }
  }
  else
  {
    open(MAIL, "| $mail") || &sys_error("$prog: Can't open pipe to sendmail: $!");
    $header_buffer = '';
    $header_buffer .= "To: $POST{email}\r\n";
    $header_buffer .= "X-Mailer: Hassan Consulting Shopping Cart \r\n";
    $header_buffer .= "Mime-Version: 1.0\r\n";
    $header_buffer .= "Content-Type: text/plain; charset=$char_set\r\n";
    $header_buffer .= "From: $admin\r\n";
    $header_buffer .= "Subject: $customer_subject\r\n\r\n";
  }
  print MAIL "$header_buffer";
  print MAIL "$info_buffer";
  print MAIL "$order_buffer";
  close (MAIL) || &sys_error("$prog: Error closing pipe to sendmail: $!");;
  if ($NT)
  {
    `$mail`;
    @delete_list = ("$mail_file");
    unlink(@delete_list);
  }
  if (!(open (ORDERS, ">>$secure_file")))
  {
    &html_header;
    print("Error opening file: $secure_file");
    exit;
  }
#  print ORDERS "$header_buffer";
  $date = `date`;
  print ORDERS "$date";
  print ORDERS "$info_buffer";
  print ORDERS "$cc_buffer";
  print ORDERS "$order_buffer";
  print ORDERS "\n-----------------------------------------------------------------------------\n\n";  
  close(ORDERS);
  @filelist = ("$secure_file");
  $ret = chmod(0666, @filelist);
}

1;






