Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BuiltinCaptchaAbstractHelper
96.77% covered (success)
96.77%
30 / 31
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renderCaptcha
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
4
1<?php
2
3/**
4 * Helper class for the default phpMyFAQ captchas.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2010-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2010-01-11
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Captcha\Helper;
21
22use phpMyFAQ\Captcha\BuiltinCaptcha;
23use phpMyFAQ\Captcha\CaptchaInterface;
24use phpMyFAQ\Configuration;
25use phpMyFAQ\Helper\AbstractHelper;
26
27/**
28 * Class CaptchaHelper
29 *
30 * @package phpMyFAQ\Helper
31 */
32class BuiltinCaptchaAbstractHelper extends AbstractHelper implements CaptchaHelperInterface
33{
34    private const string FORM_ID = 'captcha';
35
36    private const string FORM_BUTTON = 'captcha-button';
37
38    /**
39     * Constructor.
40     */
41    public function __construct(
42        protected Configuration $configuration,
43    ) {
44    }
45
46    /**
47     * Renders the captcha check.
48     */
49    public function renderCaptcha(
50        CaptchaInterface $captcha,
51        string $action = '',
52        string $label = '',
53        bool $auth = false,
54    ): string {
55        if (!$captcha instanceof BuiltinCaptcha) {
56            return '';
57        }
58
59        $html = '';
60
61        if (true === $this->configuration->get(item: 'spam.enableCaptchaCode') && !$auth) {
62            $html .= '<div class="row g-4">';
63            $html .= sprintf('<label class="col-md-3 col-sm-12 col-form-label">%s</label>', $label);
64            $html .= '    <div class="col-md-4 col-sm-6 col-7">';
65            $html .= sprintf('<p class="form-control-static">%s</p>', $captcha->renderCaptchaImage());
66            $html .= '    </div>';
67            $html .= '    <div class="col-md-5 col-sm-6 col-5">';
68            $html .= '        <div class="input-group">';
69            $html .= sprintf(
70                '<input type="text" class="form-control" name="%s" id="%s" size="%d" autocomplete="off" required>',
71                self::FORM_ID,
72                self::FORM_ID,
73                $captcha->captchaLength,
74            );
75            $html .= '            <span class="input-group-btn">';
76            $html .= sprintf(
77                '<button type="button" class="btn btn-primary" id="%s" data-action="%s">'
78                . '<i aria-hidden="true" class="bi bi-arrow-repeat" data-action="%s"></i></button>',
79                self::FORM_BUTTON,
80                $action,
81                $action,
82            );
83            $html .= '            </span>';
84            $html .= '        </div>';
85            $html .= '    </div>';
86            $html .= '</div>';
87        }
88
89        return $html;
90    }
91}