xref: /webtrees/app/Http/RequestHandlers/EmailPreferencesAction.php (revision 541cf44d763a8aa5906f3fce58ca6b0ac3a25986)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\EmailService;
25use Fisharebest\Webtrees\Site;
26use Fisharebest\Webtrees\SiteUser;
27use Fisharebest\Webtrees\Validator;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function e;
33use function redirect;
34use function route;
35
36/**
37 * Edit the email preferences.
38 */
39class EmailPreferencesAction implements RequestHandlerInterface
40{
41    private EmailService $email_service;
42
43    /**
44     * AdminSiteController constructor.
45     *
46     * @param EmailService $email_service
47     */
48    public function __construct(EmailService $email_service)
49    {
50        $this->email_service = $email_service;
51    }
52
53    /**
54     * @param ServerRequestInterface $request
55     *
56     * @return ResponseInterface
57     */
58    public function handle(ServerRequestInterface $request): ResponseInterface
59    {
60        $user   = Validator::attributes($request)->user();
61        $params = (array) $request->getParsedBody();
62
63        Site::setPreference('SMTP_ACTIVE', $params['SMTP_ACTIVE']);
64        Site::setPreference('SMTP_DISP_NAME', $params['SMTP_DISP_NAME']);
65        Site::setPreference('SMTP_FROM_NAME', $params['SMTP_FROM_NAME']);
66        Site::setPreference('SMTP_HOST', $params['SMTP_HOST']);
67        Site::setPreference('SMTP_PORT', $params['SMTP_PORT']);
68        Site::setPreference('SMTP_AUTH', $params['SMTP_AUTH']);
69        Site::setPreference('SMTP_AUTH_USER', $params['SMTP_AUTH_USER']);
70        Site::setPreference('SMTP_SSL', $params['SMTP_SSL']);
71        Site::setPreference('SMTP_HELO', $params['SMTP_HELO']);
72        Site::setPreference('DKIM_DOMAIN', $params['DKIM_DOMAIN']);
73        Site::setPreference('DKIM_SELECTOR', $params['DKIM_SELECTOR']);
74        Site::setPreference('DKIM_KEY', $params['DKIM_KEY']);
75
76        if ($params['SMTP_AUTH_PASS'] !== '') {
77            Site::setPreference('SMTP_AUTH_PASS', $params['SMTP_AUTH_PASS']);
78        }
79
80        FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success');
81
82        $test = $params['test'] ?? '';
83
84        if ($test === 'on') {
85            $success = $this->email_service->send(new SiteUser(), $user, $user, 'test', 'test', 'test');
86
87            if ($success) {
88                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($user->email())), 'success');
89            } else {
90                FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
91            }
92
93            return redirect(route(EmailPreferencesPage::class));
94        }
95
96        return redirect(route(ControlPanel::class));
97    }
98}
99