<?php

if(isset($_POST['submit'])) {

   error_reporting(E_NOTICE);

   function valid_email($str) {
          return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
   }


   $cap1=preg_replace("([\r\n])", "", hexstr($_POST['cap1']));
   $cap2=preg_replace("([\r\n])", "", hexstr($_POST['cap2']));
   $realcap=$cap1 + $cap2;

   if ($_POST['cap'] == $realcap) {
      if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE) {
         $topart=preg_replace("([\r\n])", "", hexstr($_POST['receiver']));
         if (isset($_POST['ccreceiver'])) {
            $cc=preg_replace("([\r\n])", "", hexstr($_POST['ccreceiver']));
	 }
         $to = $_POST['site'].' <'.$topart.'>';
         $from = preg_replace("([\r\n])", "", $_POST['email']);
         $subject = "Admin Message: ".$_POST['site'];

         if (isset($_POST['project'])) {
	    $projdetails=$_POST['project'];
         } else { $projdetails=''; }
 	 if (isset($_POST['comment'])) {
	    $comment=$_POST['comment'];
	 } else { $comment =''; }

         $message = 'Following is a message received from the contact form on '.$_POST['site'].': '."\n\n".'Name: '.$_POST['name']."\n\n".'Email: '.$_POST['email']."\n\n".'Number: '.$_POST['number']."\n\n".$_POST['formadd'].': '.$projdetails."\n\n".'Comments: '.$comment."\n";
 			  
         $match = "/(bcc:|cc:|content\-type:)/i";

         if (preg_match($match, $to) || preg_match($match, $from) || preg_match($match, $message)) {
            die("Header injection detected.");
         }

         $headers = "From: ".$from."\r\n";
         $headers .= "Reply-to: ".$from."\r\n";

         if (isset($_POST['ccreceiver'])) {
            $headers .= "Cc: ".$cc."\r\n";
	 }
             
         if(mail($to, $subject, $message, $headers)) {
            echo 1; //SUCCESS

         } else {
            echo 2; //FAILURE - server failure
         }
      } else {
      echo 3; //FAILURE - not valid email
      }

   } else {
      echo 4; //CAPCHA issue
   }

} else {

   die("Direct access not allowed!");

}
		   
function hexstr($hexstr) {
   $hexstr = str_replace(' ', '', $hexstr);
   $hexstr = str_replace('\x', '', $hexstr);
   $retstr = pack('H*', $hexstr);
   return $retstr;
}

?>

