1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25namespace Ampache\Module\Application\Register;
26
27use Ampache\Config\ConfigContainerInterface;
28use Ampache\Config\ConfigurationKeyEnum;
29use Ampache\Module\Application\ApplicationActionInterface;
30use Ampache\Module\Application\Exception\AccessDeniedException;
31use Ampache\Module\Authorization\GuiGatekeeperInterface;
32use Ampache\Module\Util\Mailer;
33use Ampache\Module\Util\Ui;
34use Ampache\Module\Util\UiInterface;
35use Psr\Http\Message\ResponseInterface;
36use Psr\Http\Message\ServerRequestInterface;
37
38final class ShowAddUserAction implements ApplicationActionInterface
39{
40    public const REQUEST_KEY = 'show_add_user';
41
42    private ConfigContainerInterface $configContainer;
43
44    private UiInterface $ui;
45
46    public function __construct(
47        ConfigContainerInterface $configContainer,
48        UiInterface $ui
49    ) {
50        $this->configContainer = $configContainer;
51        $this->ui              = $ui;
52    }
53
54    /**
55     * @param ServerRequestInterface $request
56     * @param GuiGatekeeperInterface $gatekeeper
57     * @return ResponseInterface|null
58     * @todo drop copy/paste code from register action after fixing the captcha problem
59     */
60    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
61    {
62        /* Check Perms */
63        if (
64            $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::ALLOW_PUBLIC_REGISTRATION) === false &&
65            !Mailer::is_mail_enabled()
66        ) {
67            throw new AccessDeniedException('Error attempted registration');
68        }
69
70        /* Don't even include it if we aren't going to use it */
71        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::CAPTCHA_PUBLIC_REG) === true) {
72            define('CAPTCHA_INVERSE', 1);
73            /**
74             * @todo broken, the path does not exist any longer
75             */
76            define(
77                'CAPTCHA_BASE_URL',
78                sprintf(
79                    '%s/modules/captcha/captcha.php',
80                    $this->configContainer->getWebPath()
81                )
82            );
83            require_once __DIR__ . '/../../Util/Captcha/init.php';
84        }
85        require_once Ui::find_template('show_user_registration.inc.php');
86
87        return null;
88    }
89}
90