This repository was archived by the owner on Mar 14, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrecaptcha.php
More file actions
56 lines (46 loc) · 2.05 KB
/
Copy pathrecaptcha.php
File metadata and controls
56 lines (46 loc) · 2.05 KB
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
<?php
class recaptcha extends rcube_plugin
{
public function init()
{
$this->load_config();
$rcmail = rcmail::get_instance();
if ($rcmail->config->get('recaptcha_public_key') != '' && $rcmail->config->get('recaptcha_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('recaptcha_public_key');
$theme = $rcmail->config->get('recaptcha_theme') ?? 'dark';
$src = "https://www.google.com/recaptcha/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="g-recaptcha" 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('recaptcha_secret_key');
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$cf = new \CloudFlare\IpRewrite();
$response = filter_input(INPUT_POST, 'g-recaptcha-response');
$ip = $cf->isCloudFlare() ? $cf->getRewrittenIP() : rcube_utils::remote_addr();
$result = $recaptcha->verify($response, $ip);
if ($result->isSuccess()) {
return $args;
}
$this->add_texts('localization/');
$rcmail->output->show_message('recaptcha.recaptchafailed', 'error');
$rcmail->output->set_env('task', 'login');
$rcmail->output->send('login');
return null;
}
}