forked from pe7er/hCaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcaptcha.php
129 lines (113 loc) · 3.24 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* @package Joomla.Plugin
* @subpackage Captcha
*
* @author Peter Martin
* @copyright Copyright 2016-2017 Peter Martin. All rights reserved.
* @license GNU General Public License version 2 or later
* @link https://db8.eu
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Utilities\IpHelper;
/**
* hCaptcha Plugin
* Using the https://www.hcaptcha.com/ CAPTCHA service
*
* @since 1.0.0
*/
class PlgCaptchaHcaptcha extends CMSPlugin
{
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 1.0.0
*/
protected $autoloadLanguage = true;
/**
* Initialise the captcha
*
* @return boolean True on success, false otherwise
*
* @since 1.0.0
* @throws Exception
*/
public function onInit()
{
// If there is no Public Key set, then this plugin is no use, so exit
if ($this->params->get('publicKey', '') === '')
{
throw new \RuntimeException(Text::_('PLG_HCAPTCHA_ERROR_NO_PUBLIC_KEY'));
}
// Load the JavaScript from hCaptcha
HTMLHelper::_('script', 'https://hcaptcha.com/1/api.js', ['version' => 'auto', 'relative' => true], ['defer' => 'defer']);
return true;
}
/**
* Gets the challenge HTML
*
* @param string $name The name of the field. Not Used.
* @param string $id The id of the field.
* @param string $class The class of the field.
*
* @return string The HTML to be embedded in the form.
*
* @since 1.0.0
*/
public function onDisplay($name = null, $id = 'hcaptcha', $class = '')
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$ele = $dom->createElement('div');
$ele->setAttribute('id', $id);
$ele->setAttribute('class', 'h-captcha');
$ele->setAttribute('data-sitekey', $this->params->get('publicKey', ''));
$ele->setAttribute('data-theme', $this->params->get('theme', 'light'));
$ele->setAttribute('data-size', $this->params->get('size', 'normal'));
$dom->appendChild($ele);
return $dom->saveHTML($ele);
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @param string $code Answer provided by user. Not needed for the Hcaptcha implementation
*
* @return boolean
* @since 1.0.0
* @throws Exception
*/
public function onCheckAnswer($code = null)
{
$input = Factory::getApplication()->input;
$privateKey = $this->params->get('privateKey');
$remoteIp = IpHelper::getIp();
$hCaptchaResponse = $input->get('h-captcha-response', '', 'cmd');
if (empty($privateKey))
{
throw new \RuntimeException(Text::_('PLG_HCAPTCHA_ERROR_NO_PRIVATE_KEY'));
}
if (empty($remoteIp))
{
throw new \RuntimeException(Text::_('PLG_HCAPTCHA_ERROR_NO_IP'));
}
if (empty($hCaptchaResponse))
{
throw new \RuntimeException(Text::_('PLG_HCAPTCHA_ERROR_EMPTY_SOLUTION'));
}
$verifyResponse = file_get_contents(
'https://hcaptcha.com/siteverify?secret=' . $privateKey .
'&response=' . $hCaptchaResponse .
'&remoteip=' . $remoteIp
);
$responseData = json_decode($verifyResponse);
if ($responseData->success)
{
return true;
}
throw new \RuntimeException(Text::_('PLG_HCAPTCHA_ERROR_INCORRECT_CAPTCHA'));
}
}