 <?php

    $to = 'jcarlton@eschertech.com,dcrocker@eschertech.com';
//   $to = 'jcarlton@eschertech.com';
  $from = 'php@escher3D.com Escher3D order';
  $reply = '';
  $subject = '';
  $body = '';

  $success = true;							// error boolean for complete loop
  $code = 0;								// weighted error code for complete loop
  $vName = '';                              // error details for complete loop
  $just_failed = false;						// error boolean for one iteration only

  $length=count($_REQUEST);
 // echo "Received: $_REQUEST $length elements<br>";			// debugging

  if (!isset($_POST)  || ($length < 3))
  {
  	$success = false;                       // nothing POSTed by the form (code +1)
  	$code = $code + $length + 1;            // should have at least 3 elements
  }


// ============================================  Main processing loop

 if (isset($_POST)):

   // Iterate through all the POSTed variables, and add them to the message body.
  $count = 0;

  while (list($key, $value) = each($_POST)):

  //  echo "$key : $value <br>";							// debugging

   $just_failed = false;

   $value = trim($value);                	    // tidy the input value
   $count = $count + 1;							// loop counter,for trace

   preg_match("/</", $value, $match);
   if ($match[0] != '')
   {
       $vName = $vName . "HTML detected; ";
       $success = false; $just_failed = true;
       $code = $code + 5;						// dodgy input (code + 5)
   }
   // echo("<br>Loop: " . $count . " Error: " . $code . ": " .$match[0] . $vName);

   preg_match("/www/", $value, $match);
   if ($match[0] != '')
   {
       $vName = $vName . "hyperlinking detected; ";
       $success = false; $just_failed = true;
       $code = $code + 50;						// dodgy input (code + 50)
   }
   // echo("<br>Loop: " . $count . " Error: " . $code . ": " . $vName);

   preg_match("/tp:/", $value, $match);
   if ($match[0] != '')
   {
       $vName = $vName . "hyperlinking detected; ";
       $success = false; $just_failed = true;
       $code = $code + 50;						// dodgy input (code + 50)
   }
   // echo("<br>Loop: " . $count . " Error: " . $code . ": " . $vName . "<br>");


   if ($key == 'FormType')
   {
    $Form = $value;
  	if (   $value == 'Educational Enquiry'
  		|| $value == 'Order'
  		|| $value == 'Teaching Feedback'
  		|| $value == 'PD Information Request'
  		|| $value == 'eCv Information Request'
  		|| $value == 'Technical Support Request'
  		|| $value == 'Product Registration'
  		|| $value == 'Mailing List Subscription Request'
  		|| $value == 'Message'
  		|| $value == 'Feedback'
  		|| $value == 'Request Download Key'
  	   )
  	{
  		$subject = $value . ' from www.escher3D.com';
  	}
  	else
  	{
  		$success = false; $just_failed = true;
  		$code = $code + 20;                                // FormType not identified
  	}
   }

   // save input email address for the reply-to header
   if ($key == 'Email_Address')
   {
	  if (preg_match('/ ^[-a-z0-9_\.]+@[-a-z0-9_\.]+$/i',$value))
	  {
   		  $reply = $value;
      }
	  else
	  {
		  $success = false; $just_failed = true;
		  $code = $code + 300;                             // Email_Address in bad format
	  }
   }

   // Check for a supplied input in the Sandwich field and kill if present
         if ((strlen($value) != 0) && ($key == 'Sandwich'))
         {
    	        $success = false; $just_failed = true;
    	        $code = $code + 4000;                      // Bot attempt; this field SHOULD be empty
         }

	// Find if this is a quotation or an order
	if ($key == 'Quotation_Required')
	{
		if ($value == 'Order')
		{
			$paypal = 'true';
		}
		else
		{
			$paypal = 'false';
		}
	}
		

   // Check for an unsupplied input, replace null with empty
   if (strlen($value) == 0)
   {
      $body .= $key . ': ' . "<empty>" . "\r\n";
   }
   else
   {
      if (!$just_failed)
      {
      		$body .= $key . ': ' . $value . "\r\n";
      }
      else
      {
      		$body .= $key . ': <b>censored</b>' . "\r\n";
      }
    }

   endwhile;
endif;

// ========================= End of main loop: Result:

if ($success)
{
  // Define From/Reply Headers
  $headers = "From: $from\r\n";
  $headers .= "Reply-To: $reply\r\n";

  // Mail the message out. This will not work from localhost.
  $success = mail($to, $subject, $body, $headers);
}
else
{
	if (strlen($vName) > 1)
	{
		$vName = $vName . ' <span style="color:red;font-weight:bold;font-size:x-large;margin-left:-10px;">***</span>';
	}


// 		echo("<br>At End: Error: " . $code . ": " . $vName . "<br><br><br>");
}

?>

<!--   ===================          HTML page to update user ================        -->

<html>
<head>
<title>Escher3D Orderform Result</title>
<link href="../../../css/style.css" rel="stylesheet" type="text/css">
</head>


<body>
<a name="top"></a>

<div class="border1">
<div class="border2">

<div class="banner" >
&nbsp;
</div>

<div class="restricted">
    <h2>The webpage at Escher3D says:</h2>
	
   <?php
     if ($success)
     {
    echo("<b>Thank you, your " . $Form . " form has been sent.</b>");
    	if ($paypal)
    	{
    		echo('<div style="margin:30px 0px 0px 0px">
    		Thank you. We will email you the payment request shortly.
    		</div>');
    	}
    	else
    	{
            echo('<div style="margin:30px 0px 0px 0px">
    		We will get back to you as soon as possible.</div>');
    	}
     }
     else
     {
    echo("Sorry, your " . $Form . " form could not be sent.<br><br>");
    echo("The details were: " . $subject . ":  " . $body . "  code: " . $code . " " . $vName);
    // echo("<br><br><b>Please telephone us</b>.");
     }
   ?>
   
 </div> 

<?php
include ($_SERVER["DOCUMENT_ROOT"] . "/includes/footer/footer.php");
?>

</div>
</div>
<div id="underline">&nbsp;</div>
</body>
</html>