function validate_captcha()
{
    $captcha = new Captcha\Captcha();
    $captcha->setPublicKey(CONFIG_RECAPTCHA_PUBLIC_KEY);
    $captcha->setPrivateKey(CONFIG_RECAPTCHA_PRIVATE_KEY);
    $response = $captcha->check();
    if (!$response->isValid()) {
        message_error("The reCAPTCHA wasn't entered correctly. Go back and try it again.");
    }
}
require_once '../utils/session.php';
require_once 'form.php';
Response::allow_method('POST');
session_start();
if (!isset($_POST['token']) || !Session::verify_token('token_form_contact', $_POST['token'])) {
    Response::json(401, 'La sesión es inválida o este formulario ya fue enviado.');
}
unset($_POST['token']);
if (!isset($_POST['recaptcha']) || !isset($_POST['recaptcha']['challenge']) || !isset($_POST['recaptcha']['response'])) {
    Response::json(400, 'Captcha requerido!');
}
// Validate captcha
$captcha = new Captcha\Captcha();
$captcha->setPublicKey(Config::$RECAPTCHA_PUBLIC);
$captcha->setPrivateKey(Config::$RECAPTCHA_SECRET);
$response = $captcha->check($_POST['recaptcha']['challenge'], $_POST['recaptcha']['response']);
if (!$response->isValid()) {
    Response::json(401, 'Captcha inválido!');
}
unset($_POST['recaptcha']);
// Form creation
$MAX_LENGTH_NAME = 30;
$MAX_LENGTH_SUBJECT = 70;
$MAX_LENGTH_MESSAGE = 250;
$REASONS = array('enquiry' => 'Consultas', 'complaint' => 'Reclamos', 'difussion' => 'Bandas/Difusión', 'other' => 'Otros');
$form = new Form(array('full_name' => array('label' => 'Nombre/s', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_NAME), 'email' => array('label' => 'Correo electrónico', 'type' => 'email', 'required' => true), 'reason' => array('label' => 'Motivo', 'type' => 'string', 'required' => true, 'select' => $REASONS), 'subject' => array('label' => 'Asunto', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_SUBJECT), 'message' => array('label' => 'Mensaje', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_MESSAGE)));
$errors = $form->validate_whitelist();
if (!empty($errors)) {
    Response::json(400, $errors);
}
$errors = $form->validate_required();
Example #3
0
<?php

/**
 * You must run `composer install` in order to generate autoloader for this example
 */
require __DIR__ . '/../vendor/autoload.php';
// New captcha instance
$captcha = new Captcha\Captcha();
$captcha->setPublicKey('publickey');
$captcha->setPrivateKey('privatekey');
// set a remote IP if the remote IP can not be found via $_SERVER['REMOTE_ADDR']
if (!isset($_SERVER['REMOTE_ADDR'])) {
    $captcha->setRemoteIp('192.168.1.1');
}
// Output captcha to end user
echo $captcha->html();
// Perform validation (put this inside if ($_POST) {} condition for example)
$response = $captcha->check();
if (!$response->isValid()) {
    echo $response->getError();
}