1<?php
2
3/*
4 * vim:set softtabstop=4 shiftwidth=4 expandtab:
5 *
6 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
7 * Copyright 2001 - 2020 Ampache.org
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
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 Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=0);
25
26namespace Ampache\Module\Application\Admin\Mail;
27
28use Ampache\Config\ConfigContainerInterface;
29use Ampache\Config\ConfigurationKeyEnum;
30use Ampache\Module\Application\ApplicationActionInterface;
31use Ampache\Module\Application\Exception\AccessDeniedException;
32use Ampache\Module\Authorization\AccessLevelEnum;
33use Ampache\Module\Authorization\GuiGatekeeperInterface;
34use Ampache\Module\System\Core;
35use Ampache\Module\Util\Mailer;
36use Ampache\Module\Util\UiInterface;
37use Psr\Http\Message\ResponseInterface;
38use Psr\Http\Message\ServerRequestInterface;
39
40final class SendMailAction implements ApplicationActionInterface
41{
42    public const REQUEST_KEY = 'send_mail';
43
44    private UiInterface $ui;
45
46    private ConfigContainerInterface $configContainer;
47
48    public function __construct(
49        UiInterface $ui,
50        ConfigContainerInterface $configContainer
51    ) {
52        $this->ui              = $ui;
53        $this->configContainer = $configContainer;
54    }
55
56    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
57    {
58        if (
59            $gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_MANAGER) === false ||
60            $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DEMO_MODE) === true
61        ) {
62            throw new AccessDeniedException();
63        }
64
65        $this->ui->showHeader();
66
67        // Multi-byte Character Mail
68        if (function_exists('mb_language')) {
69            $ini_default_charset = 'default_charset';
70            if (ini_get($ini_default_charset)) {
71                ini_set($ini_default_charset, "UTF-8");
72            }
73            mb_language("uni");
74        }
75
76        if (Mailer::is_mail_enabled()) {
77            $mailer = new Mailer();
78
79            // Set the vars on the object
80            $mailer->subject = $_REQUEST['subject'];
81            $mailer->message = $_REQUEST['message'];
82
83            if (Core::get_request('from') == 'system') {
84                $mailer->set_default_sender();
85            } else {
86                $mailer->sender      = Core::get_global('user')->email;
87                $mailer->sender_name = Core::get_global('user')->fullname;
88            }
89
90            if ($mailer->send_to_group($_REQUEST['to'])) {
91                $title = T_('No Problem');
92                $body  = T_('Your e-mail has been sent');
93            } else {
94                $title = T_('There Was a Problem');
95                $body  = T_('Your e-mail has not been sent');
96            }
97
98            $url = sprintf(
99                '%s/admin/mail.php',
100                $this->configContainer->getWebPath()
101            );
102            $this->ui->showConfirmation($title, $body, $url);
103        }
104
105        $this->ui->showQueryStats();
106        $this->ui->showFooter();
107
108        return null;
109    }
110}
111