<?php
    // My modifications to mailer script from:
    // http://blog.teamtreehouse.com/create-ajax-contact-form
    // Added input sanitizing to prevent injection

    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

                $captcha_response = true;
                $recaptcha = $_POST['g-recaptcha-response'];

                $url = 'https://www.google.com/recaptcha/api/siteverify';
                $data = array(
                    'secret' => '6LcPnZUsAAAAAFvoajR_09O3zCrQRR8UyDC7gze1',
                    'response' => $recaptcha
                );
                $options = array(
                    'http' => array (
                        'method' => 'POST',
                        'content' => http_build_query($data)
                    )
                );
                $context  = stream_context_create($options);
                $verify = file_get_contents($url, false, $context);
                $captcha_success = json_decode($verify);
                $captcha_response = $captcha_success->success;           

        if(isset($_POST['dejarenblanco'])){
    $dejarenblanco = $_POST['dejarenblanco'];
}
if(isset($_POST['nocambiar'])){
    $nocambiar = $_POST['nocambiar'];
}        
        // $ccp1 = $_POST['nivel'];
        // Get the form fields and remove whitespace.
        $name = strip_tags(trim($_POST["name"]));
		$name = str_replace(array("\r","\n"),array(" "," "),$name);
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $phone = trim($_POST["phone"]);
        $message = trim($_POST["message"]);

            if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['message'])){
        
            // prevent form from saving code goes here
                 http_response_code(400);
             echo "Eres spam"; 
             exit;
            }

            if (preg_match('/(sex|baby|site|telegram|trabajo|vacante|postulacion|postularme|trabajar )/i', $_POST['message'])) {
            http_response_code(400);
             echo "Eres spam"; 
             exit;
             }
  

        // Check that data was sent to the mailer.
           if ( empty($name) OR empty($message) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! Hubo un problema con el envío. Por favor verifique los campos y vuelva a intentarlo.";
            exit;
        } 
        elseif(!$captcha_response) {
            http_response_code(400);
            echo 'Debes indicar que no eres un robot';
            exit;
        }
        
        // if( ($nivel = $_POST["nivel"]) == 'kinder'){
        //     $mail_gral = "admisiones1@colegioeducacionalive.edu.mx";

        // }
       
        // elseif ( ($nivel = $_POST["nivel"]) == 'primaria')
        // {
        //     $mail_gral = "admisiones2@colegioeducacionalive.edu.mx";

        // } 
        // elseif ( ($nivel = $_POST["nivel"]) == 'maternal')
        // {
        //     $mail_gral = "admisiones1@colegioeducacionalive.edu.mx";

        // }
        

        // Set the recipient email address.
        // FIXME: Update this to your desired email address.
        $recipient = "lopez.mixael@gmail.com";
        //$mail_gral = "admisiones@colegioeducacionalive.edu.mx";

     

        // Set the email subject.
        $subject = "Nuevo correo ANT STUDIO | CONTACT";

        // Build the email content.
        $email_content = "Nombre: $name\n";
        $email_content .= "Correo electronico: $email\n\n";
        $email_content .= "Tel: $phone\n\n";
        $email_content .= "Mensaje:\n$message\n";

        // Build the email headers.
        //$email_headers = "From: Alive <admisiones1@colegioeducacionalive.edu.mx>";
        
        $email_headers = "From: ".$recipient."\nReply-To:".$email."\n";
        

        // Send the email.
        if (($captcha_success->success === true) && $dejarenblanco == '' && $nocambiar == 'http://') {
            // Set a 200 (okay) response code.
            mail($recipient, $subject, $email_content, $email_headers);
            //mail($mail_gral, $subject, $email_content, $email_headers);
            //mail($recipientdos, $subject, $email_content, $email_headers);
            http_response_code(200);
            echo "¡Gracias! Tu mensaje ha sido enviado.";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Algo salió mal y no pudimos enviar su mensaje.";
        }

        } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "Hubo un problema con su envío, intente de nuevo.";
        }

?>
