Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Captcha
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __invoke
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlForCaptcha
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 js
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 active
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Captcha view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Chris Hallberg <crhallberg@gmail.com>
26 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\View\Helper\Root;
32
33use function count;
34
35/**
36 * Captcha view helper
37 *
38 * @category VuFind
39 * @package  View_Helpers
40 * @author   Chris Hallberg <crhallberg@gmail.com>
41 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class Captcha extends \Laminas\View\Helper\AbstractHelper
46{
47    use ClassBasedTemplateRendererTrait;
48
49    /**
50     * Captcha services
51     *
52     * @var array
53     */
54    protected $captchas = [];
55
56    /**
57     * Config
58     *
59     * @var \Laminas\Config\Config
60     */
61    protected $config;
62
63    /**
64     * Constructor
65     *
66     * @param \Laminas\Config\Config $config   Config
67     * @param array                  $captchas Captchas
68     */
69    public function __construct(
70        \Laminas\Config\Config $config,
71        array $captchas = []
72    ) {
73        $this->config = $config;
74        $this->captchas = $captchas;
75    }
76
77    /**
78     * Return this object
79     *
80     * @return \VuFind\View\Helper\Root\Captcha
81     */
82    public function __invoke(): \VuFind\View\Helper\Root\Captcha
83    {
84        return $this;
85    }
86
87    /**
88     * Generate HTML of a single CAPTCHA (redirect to template)
89     *
90     * @param \VuFind\Captcha\AbstractBase $captcha Captcha
91     *
92     * @return string
93     */
94    public function getHtmlForCaptcha(\VuFind\Captcha\AbstractBase $captcha): string
95    {
96        return $this->renderClassTemplate(
97            'Captcha/%s',
98            strtolower($captcha::class),
99            ['captcha' => $captcha]
100        );
101    }
102
103    /**
104     * Generate HTML depending on CAPTCHA type (empty if not active).
105     *
106     * @param bool $useCaptcha Boolean of active state, for compact templating
107     * @param bool $wrapHtml   Wrap in a form-group?
108     *
109     * @return string
110     */
111    public function html(bool $useCaptcha = true, bool $wrapHtml = true): string
112    {
113        if (count($this->captchas) == 0 || !$useCaptcha) {
114            return '';
115        }
116
117        return $this->getView()->render(
118            'Helpers/captcha',
119            ['wrapHtml' => $wrapHtml,
120                                'captchas' => $this->captchas]
121        );
122    }
123
124    /**
125     * Get list of URLs with JS dependencies to load for the active CAPTCHA type.
126     *
127     * @return array
128     */
129    public function js(): array
130    {
131        $jsIncludes = [];
132        foreach ($this->captchas as $captcha) {
133            $jsIncludes = array_merge($jsIncludes, $captcha->getJsIncludes());
134        }
135        return array_unique($jsIncludes);
136    }
137
138    /**
139     * Return whether Captcha is active in the config
140     *
141     * @return bool
142     */
143    protected function active(): bool
144    {
145        return count($this->captchas) > 0
146            && isset($this->config->Captcha->forms);
147    }
148}