<?php
function verifyCaptcha(){
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => '6LeLxy4UAAAAABClplWLJUjZ1_nhX_-SI7CuNcm8',
        'response' => $_POST["g-recaptcha-response"]
    );
    $options = array(
        'http' => array (
            'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success=json_decode($verify);
    return $captcha_success;
}
// process.php

$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

// validate the variables ======================================================
    // if any of these variables don't exist, add an error to our $errors array

    if (empty($_POST['nombre']))
        $errors['nombre'] = 'Name is required.';

    if (empty($_POST['email']))
        $errors['email'] = 'Email is required.';

    if (empty($_POST['mensage']))
        $errors['mensage'] = 'Mensaje is required.';

    if(empty($_POST['g-recaptcha-response'])){
        $errors['recaptcha'] = 'Debe validar el captcha';
    }
    else if(!verifyCaptcha()){
        $errors['recaptcha'] = 'Error en la validación de ReCaptcha';
    }

// return a response ===========================================================

    // if there are any errors in our errors array, return a success boolean of false
    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {

        // if there are no errors process our form, then return a message

        // DO ALL YOUR FORM PROCESSING HERE
        // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)

        // show a message of success and provide a true success variable
        $nombre = $_POST['nombre']; 
    	$mail = $_POST['email'];
    	$msg = $_POST['mensage'];
    	$headers = 'From: ' . $mail . "\r\n" .
    			   'Reply-To: ' . $mail . "\r\n" .
    			   'X-Mailer: PHP/' . phpversion();
    	mail('6coloquio@cmat.edu.uy', 'Contacto de ' . $nombre, $msg, $headers);

        $data['success'] = true;
        $data['message'] = 'Mensaje enviado,<br/>A la brevedad el comite organizador se pondrá en contacto con usted.';
        
    }

    // return all our data to an AJAX call
    echo json_encode($data);

?>
