Debian Codegraph @

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