Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wa-content/js/jquery-wa/wa-settings/settings.captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var WASettingsCaptcha = ( function($) {
var that = this;

that.$form.find(':input[name="captcha"]').on('change', function(){
if (this.value == 'waReCaptcha') {
if (this.value === 'waReCaptcha' || this.value === 'waHCaptcha') {
that.$form.find('div.js-captcha-adapter-settings').slideDown();
} else {
that.$form.find('div.js-captcha-adapter-settings').slideUp();
Expand Down
1 change: 1 addition & 0 deletions wa-system/autoload/system_classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

'waCaptcha' => 'captcha/waCaptcha.class.php',
'waReCaptcha' => 'captcha/recaptcha/waReCaptcha.class.php',
'waHCaptcha' => 'captcha/hcaptcha/waHCaptcha.class.php',
'waPHPCaptcha' => 'captcha/phpcaptcha/waPHPCaptcha.class.php',

'waModel' => 'database/waModel.class.php',
Expand Down
35 changes: 35 additions & 0 deletions wa-system/captcha/hcaptcha/templates/hcaptcha.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script>
(function() {
window.onloadWaHcaptchaCallback = function() {
var sitekey = {$sitekey|default:''|json_encode};
if (!window.hcaptcha) return;
$('.h-captcha:not(.initialized)').each(function() {
var wrapper = $(this).addClass('initialized');
var widget_id = hcaptcha.render(wrapper[0], { sitekey: sitekey});
wrapper.siblings('.wa-captcha-refresh, .wa-captcha-img').click(function() {
try {
hcaptcha.reset(widget_id);
$(window).trigger('wa_hcaptcha_loaded');
} catch (e) {
console.log('Unable to reset WA HCaptcha widget id =', widget_id);
console.log(e);
}
return false;
});
});
$(window).trigger('wa_hcaptcha_loaded');
};

$(function() {
if (window.hcaptcha) {
window.onloadWaHcaptchaCallback();
} else {
$.getScript("https://hcaptcha.com/1/api.js?onload=onloadWaHcaptchaCallback&render=explicit");
}
});
})();
</script>
<div class="{$wrapper_class} wa-hcaptcha">
<a class="wa-captcha-refresh wa-captcha-img" style="display:none;"></a>
<div class="h-captcha" data-sitekey="{$sitekey|default:''|json_encode}"></div>
</div>
38 changes: 38 additions & 0 deletions wa-system/captcha/hcaptcha/templates/hcaptcha_invisible.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<div class="{$wrapper_class} wa-invisible-hcaptcha">
<a class="wa-captcha-refresh wa-captcha-img" style="display:none;"></a>
<div class="h-captcha" data-sitekey="{$sitekey}" data-size="invisible"></div>
</div>
<script>
(function() {
window.onloadWaHcaptchaCallback = function() {
if (!window.hcaptcha) return;
$('.h-captcha:not(.initialized)').each(function() {
var wrapper = $(this).addClass('initialized'),
widget_id = hcaptcha.render(wrapper[0]);

hcaptcha.execute(widget_id);

wrapper.siblings('.wa-captcha-refresh, .wa-captcha-img').click(function() {
try {
hcaptcha.reset(widget_id);
hcaptcha.execute(widget_id);
$(window).trigger('wa_hcaptcha_loaded');
} catch (e) {
console.log('Unable to reset WA HCaptcha widget id =', widget_id);
console.log(e);
}
return false;
});
});
$(window).trigger('wa_hcaptcha_loaded');
};

$(function() {
if (window.hcaptcha) {
window.onloadWaHcaptchaCallback();
} else {
$.getScript("https://hcaptcha.com/1/api.js?hl=ru&onload=onloadWaHcaptchaCallback&render=explicit");
}
});
})();
</script>
95 changes: 95 additions & 0 deletions wa-system/captcha/hcaptcha/waHCaptcha.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

class waHCaptcha extends waAbstractCaptcha
{
// required options
protected $required = array(
'sitekey',
'secret'
);

const SITE_VERIFY_URL = 'https://hcaptcha.com/siteverify';

public function getHtml()
{
$wrapper_class = ifempty($this->options, 'wrapper_class', 'wa-captcha');
$sitekey = ifset($this->options['sitekey']);
$invisible = ifset($this->options['invisible']);

$view = wa('webasyst')->getView();
$view->assign(array(
'wrapper_class' => $wrapper_class,
'sitekey' => $sitekey,
));

$template = wa()->getConfig()->getRootPath() .'/wa-system/captcha/hcaptcha/templates/hcaptcha.html';
if ($invisible) {
$template = wa()->getConfig()->getRootPath() .'/wa-system/captcha/hcaptcha/templates/hcaptcha_invisible.html';
}
return $view->fetch($template);
}

public function isValid($code = null, &$error = '')
{
if ($code === null) {
$code = waRequest::post('h-captcha-response');
}
$handle = curl_init(self::SITE_VERIFY_URL);
$options = array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(array(
'secret' => $this->options['secret'],
'response' => $code,
'remoteip' => waRequest::getIp(),
)),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
CURLINFO_HEADER_OUT => false,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true
);
curl_setopt_array($handle, $options);
$response = curl_exec($handle);
curl_close($handle);
if ($response) {
$response = json_decode($response, true);
if (isset($response['success']) && $response['success'] === true) {
return true;
}

if (isset($response['error-codes'])) {
$errors = [];
foreach ($response['error-codes'] as $error_code) {
switch ($error_code) {
case 'missing-input-secret':
$errors[] = _ws('The secret parameter is missing.');
break;
case 'invalid-input-secret':
$errors[] = _ws('The secret parameter is invalid or malformed.');
break;
case 'missing-input-response':
$errors[] = _ws('The response parameter is missing.');
break;
case 'invalid-input-response':
$errors[] = _ws('The response parameter is invalid or malformed.');
break;
case 'bad-request':
$errors[] = _wa('The request is invalid or malformed.');
break;
default:
$errors[] = $error_code;
}
$error = implode('<br>', $errors);
}
}
}
return false;
}

public function display()
{

}
}
2 changes: 1 addition & 1 deletion wa-system/captcha/waCaptcha.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function readCaptchaConfig()
if (file_exists($captcha_config_path)) {
$config = include($captcha_config_path);
$class = ifset($config, 'captcha', 0, 'waPHPCaptcha');
$options = ifset($config, 'captcha', 1, array());
$options = ifset($config, 'captcha', 1, $class, array());
return array($class, $options);
}
return array(null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public function execute()
$captcha_config_path = wa()->getConfig()->getConfigPath('config.php', true, 'webasyst');
if (file_exists($captcha_config_path)) {
$captcha_config = include ($captcha_config_path);
if (!is_array($captcha_config)) {
$captcha_config = null;
}
}

$captcha_config['captcha'][0] = waRequest::post('captcha', 'waPHPCaptcha', waRequest::TYPE_STRING);
Expand Down