xref: /webtrees/app/Http/RequestHandlers/EmailPreferencesAction.php (revision d9b5e2c0943622e28687fb94cd61268192232b06)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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     * @param EmailService $email_service
45     */
46    public function __construct(EmailService $email_service)
47    {
48        $this->email_service = $email_service;
49    }
50
51    /**
52     * @param ServerRequestInterface $request
53     *
54     * @return ResponseInterface
55     */
56    public function handle(ServerRequestInterface $request): ResponseInterface
57    {
58        $user          = Validator::attributes($request)->user();
59        $active        = Validator::parsedBody($request)->string('SMTP_ACTIVE');
60        $disp_name     = Validator::parsedBody($request)->string('SMTP_DISP_NAME');
61        $from_name     = Validator::parsedBody($request)->string('SMTP_FROM_NAME');
62        $host          = Validator::parsedBody($request)->string('SMTP_HOST');
63        $port          = Validator::parsedBody($request)->string('SMTP_PORT');
64        $auth          = Validator::parsedBody($request)->string('SMTP_AUTH');
65        $auth_user     = Validator::parsedBody($request)->string('SMTP_AUTH_USER');
66        $auth_pass     = Validator::parsedBody($request)->string('SMTP_AUTH_PASS');
67        $ssl           = Validator::parsedBody($request)->string('SMTP_SSL');
68        $helo          = Validator::parsedBody($request)->string('SMTP_HELO');
69        $dkim_domain   = Validator::parsedBody($request)->string('DKIM_DOMAIN');
70        $dkim_selector = Validator::parsedBody($request)->string('DKIM_SELECTOR');
71        $dkim_key      = Validator::parsedBody($request)->string('DKIM_KEY');
72        $test          = Validator::parsedBody($request)->boolean('test', false);
73
74        Site::setPreference('SMTP_ACTIVE', $active);
75        Site::setPreference('SMTP_DISP_NAME', $disp_name);
76        Site::setPreference('SMTP_FROM_NAME', $from_name);
77        Site::setPreference('SMTP_HOST', $host);
78        Site::setPreference('SMTP_PORT', $port);
79        Site::setPreference('SMTP_AUTH', $auth);
80        Site::setPreference('SMTP_AUTH_USER', $auth_user);
81        Site::setPreference('SMTP_SSL', $ssl);
82        Site::setPreference('SMTP_HELO', $helo);
83        Site::setPreference('DKIM_DOMAIN', $dkim_domain);
84        Site::setPreference('DKIM_SELECTOR', $dkim_selector);
85        Site::setPreference('DKIM_KEY', $dkim_key);
86
87        if ($auth_pass !== '') {
88            Site::setPreference('SMTP_AUTH_PASS', $auth_pass);
89        }
90
91        FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success');
92
93        if ($test) {
94            $success = $this->email_service->send(new SiteUser(), $user, $user, 'test', 'test', 'test');
95
96            if ($success) {
97                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($user->email())), 'success');
98            } else {
99                FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
100            }
101
102            return redirect(route(EmailPreferencesPage::class));
103        }
104
105        return redirect(route(ControlPanel::class));
106    }
107}
108