version = (int) $form->captcha;
$this->publicKey = $conficParams->get('reCaptchaPublicKey'.$this->version, '');
$this->privatKey = $conficParams->get('reCaptchaPrivateKey'.$this->version, '');
if (!empty($this->privatKey) && !empty($this->publicKey)) {
$this->enabled = true;
}
$this->setDataAttribs();
$this->setScriptUrl();
}
public function render() {
if ($this->enabled) {
$this->renderScripts();
return '
dataAttribs.'>';
}
return '';
}
public function isEnabled() {
return $this->enabled;
}
public function isValid() {
return $this->valid;
}
public function getErrorMessage() {
return $this->errorMessage;
}
// use file_get_contents instead of curl
public function validate($token) {
$post_data = http_build_query(
array(
'secret' => $this->privatKey,
'response' => $token,
'remoteip' => $_SERVER['REMOTE_ADDR']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
if ($response === false || $response === '') {
$this->errorMessage = Text::_('COM_VISFORMS_PROBLEMS_VALIDATING_HCAPTCHA');
}
else {
$response = json_decode($response, true);
$this->valid = $response['success'];
if (!$this->valid) {
$this->errorMessage = Text::_('COM_VISFORMS_HCAPTCHA_INVALID');
}
}
}
// using curl request is not working, if website IP is 'suspicious'
/* public function validate($token) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->validationUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('response' => $token,'secret'=> $this->privatKey, 'sitekey' => $this->publicKey),
));
$response = curl_exec($curl);
$status = (int) curl_getinfo( $curl, CURLINFO_HTTP_CODE );
if ($status === 200) {
curl_close($curl);
$response = json_decode($response, true);
$this->valid = $response['success'];
if (!$this->valid) {
$this->errorMessage = Text::_('COM_VISFORMS_HCAPTCHA_INVALID');
}
}
else {
$this->errorMessage = Text::_('COM_VISFORMS_PROBLEMS_VALIDATING_HCAPTCHA');
}
} */
private function setDataAttribs() {
if ($this->version === 5) {
$this->dataAttribs .= ' data-size="invisible"';
$this->dataAttribs .= ' data-callback="iGRCallback"';
}
}
private function setScriptUrl() {
if ($this->version === 5) {
$this->scriptUrl .= '?onload=VfInitIGReCaptcha&render=explicit&hl='. Factory::getApplication()->getLanguage()->getTag();
}
else {
$this->scriptUrl .= '?hl='. Factory::getApplication()->getLanguage()->getTag();
}
}
private function renderScripts() {
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
if ($this->version === 5) {
$wa->registerAndUseScript('visforms_recaptcha.loader', 'com_visforms/grecaptchainvisible.js', [], ['defer' => true, 'async' => true])
->registerAndUseScript('visforms_recaptcha.api', $this->scriptUrl, [], ['defer' => true, 'async' => true], ['visforms_recaptcha.loader']);
}
else {
$wa->registerAndUseScript('visforms_recaptcha.api', $this->scriptUrl, [], ['defer' => true, 'async' => true]);
}
}
}