<?php 
session_start();
require_once('recaptchalib.php');
$publickey = "6LfnPeoSAAAAAJmqllfhHKc8yi2JE6ORYuHzH5AR";
$privatekey = "6LfnPeoSAAAAAN0FdHllWIZNNUS9f075Imh9H-jD";
$redirect_url = '../supportthanks.html';
$your_email ='rajesh@skynetindia.info';// <<=== update to your email address
//$your_email ='manoj@skynettechnologies.com';
//***
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
$attachment_enabled = 1;
$autoresponder_enabled = 1;
$name = $_POST['Comapny_Name'];
$visitor_email = $_POST['email_from'];
$user_message = $_POST['Comments'];
	///------------Do Validations-------------
if ($_POST["recaptcha_response_field"])
{
	$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
	if ($resp->is_valid) 
	{
function get_form_data(){

    global $REQUEST_METHOD;

    global $HTTP_POST_VARS;

    global $HTTP_GET_VARS;

    

    $vars = ($REQUEST_METHOD == 'GET') ? $HTTP_GET_VARS : $HTTP_POST_VARS;

    //strip spaces from all fields

    foreach ($vars as $k=>$v) $vars[$k] = trim($v);

    return $vars;

}


function _build_fields($vars){

    $skip_fields = array(
		'submitbtn',
		'recaptcha_challenge_field');

    // order by numeric begin, if it exists

    $is_ordered = 0;

    foreach ($vars as $k=>$v) 

        if (in_array($k, $skip_fields)) unset($vars[$k]);



    $new_vars = array();

    foreach ($vars as $k=>$v){

        // remove _num, _reqnum, _req from end of field names

        $k = preg_replace('/_(req|num|reqnum)$/', '', $k);

        // check if the fields is ordered

        //if (preg_match('/^\d+[ \:_-]/', $k)) $is_ordered++;

        //remove number from begin of fields

        $k = preg_replace('/^\d+[ \:_-]/', '', $k);

        $new_vars[$k] = $v;

    }

    $vars = $new_vars;


    $max_length = 10; // max length of key field 

    foreach ($vars as $k=>$v) {

        $klen = strlen($k);

        if (($klen > $max_length) && ($klen < 40))

            $max_length = $klen;

    }



    if ($is_ordered)

        ksort($vars);



    // make output text

    $out = "";

    foreach ($vars as $k=>$v){

        $k = str_replace('_', ' ', $k);

        $k = ucfirst($k);

        $len_diff = $max_length - strlen($k);

        if ($len_diff > 0) 

            $fill = str_repeat('.', $len_diff);

        else 

            $fill = '';

        $out .= $k."$fill...: $v\n\n";

    }

    return $out;

}


$vars=get_form_data();
$out=_build_fields($vars);
$body = "A user  $name submitted the form:\n\n".$out;
//Auto Responser Function To Send Auto Respond



$autoresponder_from = $your_email;

$subject = "response from skynetindia.info";

$autoresponder_subject = "%subject% (autoresponse)";

$autoresponder_message = <<<MSG

Hi %$name%,



Thank you for submitting the form.

--

MSG;


function auto_respond($vars){

    global $autoresponder_from, $autoresponder_message, $autoresponder_subject;

    /// replace all vars in message

    $msg = $autoresponder_message;

    preg_match_all('/%(.+?)%/', $msg, $out);

    $s_vars = $out[1]; //field list to substitute

    foreach ($s_vars as $k)

        $msg = str_replace("%$k%", $vars[$k], $msg);

    /// replace all vars in subject

    $subj = $autoresponder_subject;

    preg_match_all('/%(.+?)%/', $subj, $out);

    $s_vars = $out[1]; //field list to substitute

    foreach ($s_vars as $k)

        $subj = str_replace("%$k%", $vars[$k], $subj);

    //

    $_send_to = "$vars[name] <".$vars[email_from].">";

    $_send_from = $autoresponder_from;

   mail($_send_to, $subj, $msg, "From: $_send_from");

}
	
if(empty($errors))
{
		//send the email
		$to = $your_email;
		$subject="Call Back Request From Skynet Technologies";
		$from = $visitor_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		
		//Attach File if Attachment is done
		$files = array(); //files (field names) to attach in mail
		if (count($HTTP_POST_FILES) && $attachment_enabled)
		{
			$files = array_keys($HTTP_POST_FILES);
		}
		if (count($files)){

        foreach ($files as $file){

            $file_name     = $HTTP_POST_FILES[$file]['name'];

            $file_type     = $HTTP_POST_FILES[$file]['type'];

            $file_tmp_name = $HTTP_POST_FILES[$file]['tmp_name'];

            $file_cnt = "";
	
	if($file_name!='')
	{	
            $f=@fopen($file_tmp_name, "rb");

            if (!$f) 

                continue;

            while($f && !feof($f))

                $file_cnt .= fread($f, 4096);

            fclose($f);

            if (!strlen($file_type)) $file_type="applicaton/octet-stream";

            if ($file_type == 'application/x-msdownload')

                $file_type = "applicaton/octet-stream";


			
			
			
			
			$date_time = date('Y-m-d H:i:s');
			$mime_delimiter = md5(time());
			$mail = <<<EOF

This is a MIME-encapsulated message

    

--$mime_delimiter

$body

--------------------

REMOTE IP : $REMOTE_ADDR

DATE/TIME : $date_time

EOF;

            $mail .= "\n--$mime_delimiter\n";

            $mail .= "Content-type: $file_type\n";

            $mail .= "Content-Disposition: attachment; filename=\"$file_name\"\n";

            $mail .= "Content-Transfer-Encoding: base64\n\n";

            $mail .= chunk_split(base64_encode($file_cnt));

        }
		else
		{
			$mail=$body;
		}

    }

    $mail .= "\n--$mime_delimiter--";
		
	}
	else
	{
		$mail=$body;
	}
		$headers = "Mime-Version: 1.0\r\nFrom: $from \r\nContent-Type: multipart/mixed;\n boundary=\"$mime_delimiter\"\r\nContent-Disposition: inline";
		$headers .= "Reply-To: $visitor_email \r\n";
		mail($to,'Call Back Request From Skynet Technologies', $mail,$headers);
		
		if ($autoresponder_enabled) 

        auto_respond($vars);
		
		header("Location: $redirect_url");
}




// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
} 
	else 
	{
		# set the error code so that we can display it
		$error = $resp->error;
		echo "<script type='text/javascript'>alert('You have entered invalide image verifcation code');history.go(-1);</script>";
		exit;
	}
}
?>