xref: /webtrees/app/Http/RequestHandlers/EmailPreferencesAction.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
1b4144a6dSGreg Roach<?php
2b4144a6dSGreg Roach
3b4144a6dSGreg Roach/**
4b4144a6dSGreg Roach * webtrees: online genealogy
5b4144a6dSGreg Roach * Copyright (C) 2019 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
15b4144a6dSGreg Roach * along with this program. If not, see <http://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;
27b4144a6dSGreg Roachuse Fisharebest\Webtrees\User;
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{
41b4144a6dSGreg Roach    /** @var EmailService */
42b4144a6dSGreg Roach    private $email_service;
43b4144a6dSGreg Roach
44b4144a6dSGreg Roach    /**
45b4144a6dSGreg Roach     * AdminSiteController constructor.
46b4144a6dSGreg Roach     *
47b4144a6dSGreg Roach     * @param EmailService $email_service
48b4144a6dSGreg Roach     */
49b4144a6dSGreg Roach    public function __construct(EmailService $email_service)
50b4144a6dSGreg Roach    {
51b4144a6dSGreg Roach        $this->email_service = $email_service;
52b4144a6dSGreg Roach    }
53b4144a6dSGreg Roach
54b4144a6dSGreg Roach    /**
55b4144a6dSGreg Roach     * @param ServerRequestInterface $request
56b4144a6dSGreg Roach     *
57b4144a6dSGreg Roach     * @return ResponseInterface
58b4144a6dSGreg Roach     */
59b4144a6dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
60b4144a6dSGreg Roach    {
61b4144a6dSGreg Roach        $user = $request->getAttribute('user');
62b4144a6dSGreg Roach        assert($user instanceof User);
63b4144a6dSGreg Roach
64*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
65b4144a6dSGreg Roach
66b4144a6dSGreg Roach        Site::setPreference('SMTP_ACTIVE', $params['SMTP_ACTIVE']);
67b4144a6dSGreg Roach        Site::setPreference('SMTP_FROM_NAME', $params['SMTP_FROM_NAME']);
68b4144a6dSGreg Roach        Site::setPreference('SMTP_HOST', $params['SMTP_HOST']);
69b4144a6dSGreg Roach        Site::setPreference('SMTP_PORT', $params['SMTP_PORT']);
70b4144a6dSGreg Roach        Site::setPreference('SMTP_AUTH', $params['SMTP_AUTH']);
71b4144a6dSGreg Roach        Site::setPreference('SMTP_AUTH_USER', $params['SMTP_AUTH_USER']);
72b4144a6dSGreg Roach        Site::setPreference('SMTP_SSL', $params['SMTP_SSL']);
73b4144a6dSGreg Roach        Site::setPreference('SMTP_HELO', $params['SMTP_HELO']);
74b4144a6dSGreg Roach        Site::setPreference('DKIM_DOMAIN', $params['DKIM_DOMAIN']);
75b4144a6dSGreg Roach        Site::setPreference('DKIM_SELECTOR', $params['DKIM_SELECTOR']);
76b4144a6dSGreg Roach        Site::setPreference('DKIM_KEY', $params['DKIM_KEY']);
77b4144a6dSGreg Roach
78b4144a6dSGreg Roach        if ($params['SMTP_AUTH_PASS'] !== '') {
79b4144a6dSGreg Roach            Site::setPreference('SMTP_AUTH_PASS', $params['SMTP_AUTH_PASS']);
80b4144a6dSGreg Roach        }
81b4144a6dSGreg Roach
82b4144a6dSGreg Roach        FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success');
83b4144a6dSGreg Roach
84b4144a6dSGreg Roach        $test = $params['test'] ?? '';
85b4144a6dSGreg Roach
86b4144a6dSGreg Roach        if ($test === 'on') {
87b4144a6dSGreg Roach            $success = $this->email_service->send(new SiteUser(), $user, $user, 'test', 'test', 'test');
88b4144a6dSGreg Roach
89b4144a6dSGreg Roach            if ($success) {
90b4144a6dSGreg Roach                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($user->email())), 'success');
91b4144a6dSGreg Roach            } else {
92b4144a6dSGreg Roach                FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
93b4144a6dSGreg Roach            }
94b4144a6dSGreg Roach
95b4144a6dSGreg Roach            return redirect(route(EmailPreferencesPage::class));
96b4144a6dSGreg Roach        }
97b4144a6dSGreg Roach
98b4144a6dSGreg Roach        return redirect(route(ControlPanel::class));
99b4144a6dSGreg Roach    }
100b4144a6dSGreg Roach}
101