xref: /webtrees/app/Http/RequestHandlers/EmailPreferencesAction.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1b4144a6dSGreg Roach<?php
2b4144a6dSGreg Roach
3b4144a6dSGreg Roach/**
4b4144a6dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6b4144a6dSGreg Roach * This program is free software: you can redistribute it and/or modify
7b4144a6dSGreg Roach * it under the terms of the GNU General Public License as published by
8b4144a6dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b4144a6dSGreg Roach * (at your option) any later version.
10b4144a6dSGreg Roach * This program is distributed in the hope that it will be useful,
11b4144a6dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b4144a6dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b4144a6dSGreg Roach * GNU General Public License for more details.
14b4144a6dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b4144a6dSGreg Roach */
17b4144a6dSGreg Roach
18b4144a6dSGreg Roachdeclare(strict_types=1);
19b4144a6dSGreg Roach
20b4144a6dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21b4144a6dSGreg Roach
22b4144a6dSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
23b4144a6dSGreg Roachuse Fisharebest\Webtrees\I18N;
24b4144a6dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
25b4144a6dSGreg Roachuse Fisharebest\Webtrees\Site;
26b4144a6dSGreg Roachuse Fisharebest\Webtrees\SiteUser;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28b4144a6dSGreg Roachuse Psr\Http\Message\ResponseInterface;
29b4144a6dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30b4144a6dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31b4144a6dSGreg Roach
32b4144a6dSGreg Roachuse function e;
33b4144a6dSGreg Roachuse function redirect;
34b4144a6dSGreg Roachuse function route;
35b4144a6dSGreg Roach
36b4144a6dSGreg Roach/**
37b4144a6dSGreg Roach * Edit the email preferences.
38b4144a6dSGreg Roach */
39b4144a6dSGreg Roachclass EmailPreferencesAction implements RequestHandlerInterface
40b4144a6dSGreg Roach{
41c4943cffSGreg Roach    private EmailService $email_service;
42b4144a6dSGreg Roach
43b4144a6dSGreg Roach    /**
44b4144a6dSGreg Roach     * @param EmailService $email_service
45b4144a6dSGreg Roach     */
46b4144a6dSGreg Roach    public function __construct(EmailService $email_service)
47b4144a6dSGreg Roach    {
48b4144a6dSGreg Roach        $this->email_service = $email_service;
49b4144a6dSGreg Roach    }
50b4144a6dSGreg Roach
51b4144a6dSGreg Roach    /**
52b4144a6dSGreg Roach     * @param ServerRequestInterface $request
53b4144a6dSGreg Roach     *
54b4144a6dSGreg Roach     * @return ResponseInterface
55b4144a6dSGreg Roach     */
56b4144a6dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
57b4144a6dSGreg Roach    {
58b55cbc6bSGreg Roach        $user          = Validator::attributes($request)->user();
59748dbe15SGreg Roach        $active        = Validator::parsedBody($request)->string('SMTP_ACTIVE');
60748dbe15SGreg Roach        $disp_name     = Validator::parsedBody($request)->string('SMTP_DISP_NAME');
61748dbe15SGreg Roach        $from_name     = Validator::parsedBody($request)->string('SMTP_FROM_NAME');
62748dbe15SGreg Roach        $host          = Validator::parsedBody($request)->string('SMTP_HOST');
63748dbe15SGreg Roach        $port          = Validator::parsedBody($request)->string('SMTP_PORT');
64748dbe15SGreg Roach        $auth          = Validator::parsedBody($request)->string('SMTP_AUTH');
65748dbe15SGreg Roach        $auth_user     = Validator::parsedBody($request)->string('SMTP_AUTH_USER');
66748dbe15SGreg Roach        $auth_pass     = Validator::parsedBody($request)->string('SMTP_AUTH_PASS');
67748dbe15SGreg Roach        $ssl           = Validator::parsedBody($request)->string('SMTP_SSL');
68748dbe15SGreg Roach        $helo          = Validator::parsedBody($request)->string('SMTP_HELO');
69748dbe15SGreg Roach        $dkim_domain   = Validator::parsedBody($request)->string('DKIM_DOMAIN');
70748dbe15SGreg Roach        $dkim_selector = Validator::parsedBody($request)->string('DKIM_SELECTOR');
71748dbe15SGreg Roach        $dkim_key      = Validator::parsedBody($request)->string('DKIM_KEY');
72748dbe15SGreg Roach        $test          = Validator::parsedBody($request)->boolean('test', false);
73b4144a6dSGreg Roach
74748dbe15SGreg Roach        Site::setPreference('SMTP_ACTIVE', $active);
75748dbe15SGreg Roach        Site::setPreference('SMTP_DISP_NAME', $disp_name);
76748dbe15SGreg Roach        Site::setPreference('SMTP_FROM_NAME', $from_name);
77748dbe15SGreg Roach        Site::setPreference('SMTP_HOST', $host);
78748dbe15SGreg Roach        Site::setPreference('SMTP_PORT', $port);
79748dbe15SGreg Roach        Site::setPreference('SMTP_AUTH', $auth);
80748dbe15SGreg Roach        Site::setPreference('SMTP_AUTH_USER', $auth_user);
81748dbe15SGreg Roach        Site::setPreference('SMTP_SSL', $ssl);
82748dbe15SGreg Roach        Site::setPreference('SMTP_HELO', $helo);
83748dbe15SGreg Roach        Site::setPreference('DKIM_DOMAIN', $dkim_domain);
84748dbe15SGreg Roach        Site::setPreference('DKIM_SELECTOR', $dkim_selector);
85748dbe15SGreg Roach        Site::setPreference('DKIM_KEY', $dkim_key);
86748dbe15SGreg Roach
87748dbe15SGreg Roach        if ($auth_pass !== '') {
88748dbe15SGreg Roach            Site::setPreference('SMTP_AUTH_PASS', $auth_pass);
89b4144a6dSGreg Roach        }
90b4144a6dSGreg Roach
91b4144a6dSGreg Roach        FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success');
92b4144a6dSGreg Roach
93748dbe15SGreg Roach        if ($test) {
94b4144a6dSGreg Roach            $success = $this->email_service->send(new SiteUser(), $user, $user, 'test', 'test', 'test');
95b4144a6dSGreg Roach
96b4144a6dSGreg Roach            if ($success) {
97b4144a6dSGreg Roach                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($user->email())), 'success');
98b4144a6dSGreg Roach            } else {
99b4144a6dSGreg Roach                FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
100b4144a6dSGreg Roach            }
101b4144a6dSGreg Roach
102b4144a6dSGreg Roach            return redirect(route(EmailPreferencesPage::class));
103b4144a6dSGreg Roach        }
104b4144a6dSGreg Roach
105b4144a6dSGreg Roach        return redirect(route(ControlPanel::class));
106b4144a6dSGreg Roach    }
107b4144a6dSGreg Roach}
108