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\Share;
26
27use Ampache\Config\AmpConfig;
28use Ampache\Config\ConfigContainerInterface;
29use Ampache\Config\ConfigurationKeyEnum;
30use Ampache\Module\Util\RequestParserInterface;
31use Ampache\Repository\Model\Share;
32use Ampache\Module\Application\ApplicationActionInterface;
33use Ampache\Module\Application\Exception\AccessDeniedException;
34use Ampache\Module\Authorization\GuiGatekeeperInterface;
35use Ampache\Module\System\Core;
36use Ampache\Module\Util\UiInterface;
37use Psr\Http\Message\ResponseInterface;
38use Psr\Http\Message\ServerRequestInterface;
39
40final class DeleteAction implements ApplicationActionInterface
41{
42    public const REQUEST_KEY = 'delete';
43
44    private RequestParserInterface $requestParser;
45
46    private ConfigContainerInterface $configContainer;
47
48    private UiInterface $ui;
49
50    public function __construct(
51        RequestParserInterface $requestParser,
52        ConfigContainerInterface $configContainer,
53        UiInterface $ui
54    ) {
55        $this->requestParser   = $requestParser;
56        $this->configContainer = $configContainer;
57        $this->ui              = $ui;
58    }
59
60    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
61    {
62        if (!$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SHARE)) {
63            throw new AccessDeniedException('Access Denied: sharing features are not enabled.');
64        }
65        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DEMO_MODE)) {
66            throw new AccessDeniedException();
67        }
68
69        $this->ui->showHeader();
70
71        $share_id = $this->requestParser->getFromRequest('id');
72        if (Share::delete_share($share_id, Core::get_global('user'))) {
73            $next_url = AmpConfig::get('web_path') . '/stats.php?action=share';
74            $this->ui->showConfirmation(
75                T_('No Problem'),
76                T_('Share has been deleted'),
77                $next_url
78            );
79        }
80        $this->ui->showFooter();
81
82        return null;
83    }
84}
85