Debian Codegraph @

lib/plugins/captcha/HCaptcha.inc

raw
1 <?php
2
3 namespace LAM\PLUGINS\CAPTCHA;
4
5 use htmlElement;
6 use htmlScript;
7
8 /*
9
10 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
11 Copyright (C) 2022 - 2026 Roland Gruber
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 */
28
29 /**
30 * hCaptcha provider.
31 *
32 * @author Roland Gruber
33 */
34
35 /**
36 * hCaptcha provider.
37 *
38 * @package LAM\PLUGINS\CAPTCHA
39 */
40 class HCaptcha implements CaptchaProvider {
41
42 /**
43 * @inheritDoc
44 */
45 public function getLabel(): string {
46 return 'hCaptcha';
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public function getId(): string {
53 return 'hCaptcha';
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public function getCaptchaElement(string $siteKey): htmlElement {
60 return new htmlHCaptcha($siteKey);
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public function isValid(string $secretKey, string $siteKey): bool {
67 $url = 'https://hcaptcha.com/siteverify';
68 $vars = [
69 'secret' => $secretKey,
70 'response' => $_POST['h-captcha-response'],
71 'sitekey' => $siteKey];
72 $options = [
73 'http' => [
74 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
75 'method' => 'POST',
76 'content' => http_build_query($vars)
77 ]
78 ];
79 $context = stream_context_create($options);
80 $result = file_get_contents($url, false, $context);
81 if ($result === FALSE) {
82 logNewMessage(LOG_ERR, 'hCaptcha validation failed, invalid server response.');
83 return false;
84 }
85 $responseJSON = json_decode($result);
86 logNewMessage(LOG_DEBUG, "hCaptcha result: " . $result);
87 return $responseJSON->{'success'} === true;
88 }
89
90 }
91
92 /**
93 * Creates a hCaptcha element.
94 *
95 * @package LAM\PLUGINS\CAPTCHA
96 */
97 class htmlHCaptcha extends htmlElement {
98
99 /** site key */
100 private string $key;
101
102 /**
103 * Constructor.
104 *
105 * @param string $key site key
106 */
107 public function __construct(string $key) {
108 $this->key = htmlspecialchars($key);
109 }
110
111 /**
112 * {@inheritDoc}
113 */
114 public function generateHTML($module, $input, $values, $restricted): array {
115 $script = new htmlScript('https://js.hcaptcha.com/1/api.js');
116 $script->generateHTML($module, $input, $values, $restricted);
117 echo '<div class="h-captcha" data-sitekey="' . $this->key . '"></div>';
118 return [];
119 }
120
121 }