<?php
/*echo "<pre>";
print_r($_POST);
print_r($_FILES);*/
if(isset($_POST['grecaptcharesponse']) && !empty($_POST['grecaptcharesponse']) ){
        //your site secret key
        $secret = '6LcNdTUUAAAAAL5VYxoCX2itVnaooUjEN-XFJ_iq';
        //get verify response data
        $captcha = $_POST['grecaptcharesponse'];
        $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
        $cSECRET_KEY = '6LdN27sUAAAAAGVn4uwLVZjdBimQOcJ_Mc_j_Qto';
        $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $cSECRET_KEY . '&response=' .$captcha);
        $responseData = json_decode($recaptcha);
        
        if($responseData->success){
			
		$conn = mysql_connect("localhost", "skynetuser", "SkynetUserBlog2017$");
        mysql_select_db("skynet_blog", $conn);
		
		unset($_POST['grecaptcharesponse']);
		$all_data=serialize($_POST);
		
		
		$query = 'INSERT INTO contact_data (email, all_data) values ("'.$_POST['email'].'","'.mysql_real_escape_string($all_data).'")';
		$res = mysql_query($query);

        	
		$body='
		
		Full Name: '.$_POST['full_name'].'
	
		Job Title: '.$_POST['job'].'
		
		phone: '.$_POST['phone'].'
		
		Company Name: '.$_POST['c_name'].'
		
		Category: '.$_POST['category'].'
		
		Country: '.$_POST['country'].'
		
		Message: '.$_POST['msg'].'
		
		Receive Newsletter: '.$_POST['receive_newsletter'].'
		
		';


		///mail
		include("PHPMailer-master/class.phpmailer.php");

		//PHPMailer Object
		$mail = new PHPMailer;
		
		//From email address and name
		$mail->From = $_POST['email'];
		$mail->FromName = $_POST['full_name'];
		
		//To address and name
		//$mail->addAddress("hr@skynettechnologies.com");
        //$mail->addAddress("skynetindia@icould.com");
		if($_POST['category']=='Request for Services'){
			$mail->addAddress("hello@skynetindia.info");
		}else{
			$mail->addAddress("hr@skynetindia.info");
		}
		
        //$mail->addAddress("developer4@skynettechnologies.com");

		//$mail->AddAttachment($target_file);

		//Send HTML or Plain Text email
		$mail->isHTML(false);
		
		$mail->Subject = "Contact form Submitted from Skynet India";
		$mail->Body = $body;
		
		
		
		if(!$mail->send()) 
		{
			echo "Mailer Error: " . $mail->ErrorInfo;
		} 
		else 
		{

		    if($_POST['receive_newsletter'] == 1) {
                /* mailchimp start */
                // API to mailchimp ########################################################
                $list_id = 'a2e4d005cb';
                $authToken = '9db3fec3cf28143bda5d0c2f70e91a90-us17';
                // The data to send to the API
                $postData = [
                  "email_address" => $_POST['email'],
                  "status" => "subscribed"
                ];

                if(!empty($_POST['full_name']))
                {
                  $postData["merge_fields"]["FNAME"] = $_POST['full_name'];
                }

                // Setup cURL
                $ch = curl_init('https://us17.api.mailchimp.com/3.0/lists/' . $list_id . '/members/');
                curl_setopt_array($ch, array(
                    CURLOPT_POST => TRUE,
                    CURLOPT_RETURNTRANSFER => TRUE,
                    CURLOPT_HTTPHEADER => array(
                        'Authorization: apikey ' . $authToken,
                        'Content-Type: application/json'
                    ),
                    CURLOPT_POSTFIELDS => json_encode($postData)
                ));
                // Send the request
                $response = curl_exec($ch);
                // #######################################################################
                /* mailchimp end */
            }
            //unlink($target_file);
			header("Location: https://www.skynetindia.info/thankyou.html?f=1");
			/*header("Location: http://skydentech.com/skynet-demo/website-maintenance1/thankyou.html");*/
			die();
			//echo "mail sent";
		}
		//mail	
			

		}else{
            $errMsg = 'Robot verification failed, please try again.';
            //die($errMsg);
            header("Location: https://www.skynetindia.info/contactus.html?f=2");
            
    
		}
}else{

        header("Location: https://www.skynetindia.info/contactus.html");
        die();
}
?>