Debian Codegraph @

lib/plugins/captcha/Captchas.inc

raw
1 <?php
2
3 namespace LAM\PLUGINS\CAPTCHA;
4
5 use htmlElement;
6
7 /*
8
9 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
10 Copyright (C) 2022 - 2026 Roland Gruber
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 */
27
28 /**
29 * Captcha providers.
30 *
31 * @author Roland Gruber
32 */
33
34 /**
35 * Provides captcha services.
36 *
37 * @package LAM\PLUGINS\CAPTCHA
38 */
39 class Captchas {
40
41 public const NO_CAPTCHA = '-';
42
43 /**
44 * Includes all plugin files.
45 */
46 protected function includeFiles(): void {
47 $pluginDir = dir(__DIR__);
48 while ($entry = $pluginDir->read()) {
49 if ((str_starts_with($entry, '.')) || ($entry === basename(__FILE__))) {
50 continue;
51 }
52 include_once(__DIR__ . '/' . $entry);
53 }
54 }
55
56 /**
57 * Returns a list of CaptchaProvider objects.
58 *
59 * @return CaptchaProvider[] providers (id => provider object)
60 */
61 public function findProviders(): array {
62 $this->includeFiles();
63 $providers = [];
64 foreach (get_declared_classes() as $declaredClass) {
65 if (in_array('LAM\PLUGINS\CAPTCHA\CaptchaProvider', class_implements($declaredClass))) {
66 $provider = new $declaredClass();
67 $providers[$provider->getId()] = $provider;
68 }
69 }
70 return $providers;
71 }
72
73 }
74
75 /**
76 * Interface for providers of captchas.
77 *
78 * @package LAM\PLUGINS\CAPTCHAS
79 */
80 interface CaptchaProvider {
81
82 /**
83 * Returns the label of the service
84 *
85 * @return string label
86 */
87 public function getLabel(): string;
88
89 /**
90 * Returns the id of the service
91 *
92 * @return string id
93 */
94 public function getId(): string;
95
96 /**
97 * Returns the HTML element to render for the captcha.
98 *
99 * @param string $siteKey site key
100 * @return htmlElement HTML element
101 */
102 public function getCaptchaElement(string $siteKey): htmlElement;
103
104 /**
105 * Checks if the POST data contains a valid captcha result.
106 *
107 * @param string $secretKey secret key
108 * @param string $siteKey site key
109 * @return bool user solved captcha
110 */
111 public function isValid(string $secretKey, string $siteKey): bool;
112
113 }
114