Skip to content

Instantly share code, notes, and snippets.

@IngoWinter
Last active January 7, 2020 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IngoWinter/1641046cba3818b56aae2008767e7b9d to your computer and use it in GitHub Desktop.
Save IngoWinter/1641046cba3818b56aae2008767e7b9d to your computer and use it in GitHub Desktop.
<?php
class rex_xform_iw_recaptcha extends rex_xform_abstract
{
function enterObject()
{
$output = '<script src="https://www.google.com/recaptcha/api.js" async defer></script>';
$output .= '<div class="g-recaptcha" data-sitekey="'.$this->getElement(1).'"></div>';
$this->params['form_output'][$this->getId()] = $output;
}
function getDescription()
{
return 'iw_recaptcha -> iw_recaptcha|site_key';
}
}
?>
<?php
class rex_xform_validate_iw_recaptcha extends rex_xform_validate_abstract
{
function enterObject()
{
if ($this->params["send"] == "1")
{
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = $this->getElement(2);
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if (!$responseData->success)
{
$this->params["form_show"] = TRUE;
$this->params["hasWarnings"] = TRUE;
$this->params["warning_messages"][] = $this->getElement(3);
}
}
else
{
$this->params["form_show"] = TRUE;
$this->params["hasWarnings"] = TRUE;
$this->params["warning_messages"][] = $this->getElement(4);
}
}
}
function getDescription()
{
return "validate|iw_recaptcha|secret_key|recaptcha_wrong|recaptcha_empty";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment