-
Notifications
You must be signed in to change notification settings - Fork 4
/
rc_hcaptcha.php
60 lines (51 loc) · 2.16 KB
/
rc_hcaptcha.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class rc_hcaptcha extends rcube_plugin
{
public function init()
{
$this->load_config();
$rcmail = rcmail::get_instance();
#if ($rcmail->config->get('hcaptcha_site_key') != '' && $rcmail->config->get('hcaptcha_secret_key') != '') {
$this->add_hook('template_object_loginform', [$this, 'template_object_loginform']);
$this->add_hook('authenticate', [$this, 'authenticate']);
# }
}
public function template_object_loginform(array $loginform): array
{
$rcmail = rcmail::get_instance();
$key = $rcmail->config->get('hcaptcha_site_key');
$theme = $rcmail->config->get('hcaptcha_theme') ?? 'light';
$src = "https://hcaptcha.com/1/api.js?hl=" . urlencode($rcmail->user->language);
$script = html::tag('script', ['type' => "text/javascript", 'src' => $src]);
$this->include_script($src);
$loginform['content'] = str_ireplace(
'</tbody>',
'<tr><td class="title"></td><td class="input"><div class="h-captcha" data-theme="' . html::quote($theme) . '" data-sitekey="' . html::quote($key) . '"></div></td></tr></tbody>',
$loginform['content']
);
return $loginform;
}
public function authenticate(array $args)
{
$rcmail = rcmail::get_instance();
$secret = $rcmail->config->get('hcaptcha_secret_key');
$cf = new \CloudFlare\IpRewrite();
$hcaptcha = new \neverbehave\Hcaptcha($secret);
$response = filter_input(INPUT_POST, 'h-captcha-response');
$ip = $cf->isCloudFlare() ? $cf->getRewrittenIP() : rcube_utils::remote_addr();
$result = null;
if ($rcmail->config->get('hcaptcha_send_client_ip')) {
$result = $hcaptcha->challenge($response, $ip);
} else {
$result = $hcaptcha->challenge($response);
}
if ($result->isSuccess()) {
return $args;
}
$this->add_texts('localization/');
$rcmail->output->show_message('rc_hcaptcha.hcaptchafailed', 'error');
$rcmail->output->set_env('task', 'login');
$rcmail->output->send('login');
return null;
}
}