Debian Codegraph @

lib/plugins/captcha/GoogleReCaptcha.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 * Google ReCaptcha provider.
31 *
32 * @author Roland Gruber
33 */
34
35 /**
36 * Google ReCaptcha provider.
37 *
38 * @package LAM\PLUGINS\CAPTCHA
39 */
40 class GoogleReCaptcha implements CaptchaProvider {
41
42 /**
43 * @inheritDoc
44 */
45 public function getLabel(): string {
46 return 'Google reCAPTCHA';
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public function getId(): string {
53 return 'google';
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public function getCaptchaElement(string $siteKey): htmlElement {
60 return new htmlReCAPTCHA($siteKey);
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public function isValid(string $secretKey, string $siteKey): bool {
67 $url = 'https://www.google.com/recaptcha/api/siteverify';
68 $vars = ['secret' => $secretKey, 'response' => $_POST['g-recaptcha-response']];
69 $options = [
70 'http' => [
71 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
72 'method' => 'POST',
73 'content' => http_build_query($vars)
74 ]
75 ];
76 $context = stream_context_create($options);
77 $result = file_get_contents($url, false, $context);
78 if ($result === FALSE) {
79 logNewMessage(LOG_ERR, 'reCAPTCHA validation failed, invalid server response.');
80 return false;
81 }
82 $responseJSON = json_decode($result);
83 logNewMessage(LOG_DEBUG, "ReCAPTCHA result: " . $result);
84 return $responseJSON->{'success'} === true;
85 }
86
87 }
88
89 /**
90 * Creates a Google reCAPTCHA element.
91 *
92 * @package LAM\PLUGINS\CAPTCHA
93 */
94 class htmlReCAPTCHA extends htmlElement {
95
96 /** site key */
97 private string $key;
98
99 /**
100 * Constructor.
101 *
102 * @param string $key site key
103 */
104 public function __construct(string $key) {
105 $this->key = htmlspecialchars($key);
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 public function generateHTML($module, $input, $values, $restricted): array {
112 $script = new htmlScript('https://www.google.com/recaptcha/api.js');
113 $script->generateHTML($module, $input, $values, $restricted);
114 echo '<div class="g-recaptcha" data-sitekey="' . $this->key . '"></div>';
115 return [];
116 }
117
118 }