xref: /webtrees/app/Http/RequestHandlers/EmailPreferencesPage.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
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\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\EmailService;
25use Fisharebest\Webtrees\Site;
26use Fisharebest\Webtrees\Webtrees;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30use Throwable;
31
32use function filter_var;
33use function gethostbyaddr;
34use function gethostbyname;
35use function gethostname;
36
37use const FILTER_VALIDATE_DOMAIN;
38
39/**
40 * Edit the email preferences.
41 */
42class EmailPreferencesPage implements RequestHandlerInterface
43{
44    use ViewResponseTrait;
45
46    private EmailService $email_service;
47
48    /**
49     * AdminSiteController constructor.
50     *
51     * @param EmailService $email_service
52     */
53    public function __construct(EmailService $email_service)
54    {
55        $this->email_service = $email_service;
56    }
57
58    /**
59     * @param ServerRequestInterface $request
60     *
61     * @return ResponseInterface
62     */
63    public function handle(ServerRequestInterface $request): ResponseInterface
64    {
65        $mail_ssl_options       = $this->email_service->mailSslOptions();
66        $mail_transport_options = $this->email_service->mailTransportOptions();
67
68        $title = I18N::translate('Sending email');
69
70        $SMTP_ACTIVE    = Site::getPreference('SMTP_ACTIVE');
71        $SMTP_AUTH      = Site::getPreference('SMTP_AUTH');
72        $SMTP_AUTH_USER = Site::getPreference('SMTP_AUTH_USER');
73        $SMTP_DISP_NAME = Site::getPreference('SMTP_DISP_NAME');
74        $SMTP_FROM_NAME = Site::getPreference('SMTP_FROM_NAME');
75        $SMTP_HELO      = Site::getPreference('SMTP_HELO');
76        $SMTP_HOST      = Site::getPreference('SMTP_HOST');
77        $SMTP_PORT      = Site::getPreference('SMTP_PORT');
78        $SMTP_SSL       = Site::getPreference('SMTP_SSL');
79        $DKIM_DOMAIN    = Site::getPreference('DKIM_DOMAIN');
80        $DKIM_SELECTOR  = Site::getPreference('DKIM_SELECTOR');
81        $DKIM_KEY       = Site::getPreference('DKIM_KEY');
82
83        $hostname = gethostbyaddr(gethostbyname(gethostname() ?: 'localhost'));
84
85        // Defaults
86        $SMTP_PORT      = $SMTP_PORT ?: '25';
87        $SMTP_HELO      = $SMTP_HELO ?: $hostname;
88        $SMTP_FROM_NAME = $SMTP_FROM_NAME ?: ('no-reply@' . $SMTP_HELO);
89        $SMTP_DISP_NAME = $SMTP_DISP_NAME ?: Webtrees::NAME;
90
91
92        $smtp_from_name_valid = $this->email_service->isValidEmail($SMTP_FROM_NAME);
93        $smtp_helo_valid      = filter_var($SMTP_HELO, FILTER_VALIDATE_DOMAIN);
94
95        $this->layout = 'layouts/administration';
96
97        return $this->viewResponse('admin/site-mail', [
98            'mail_ssl_options'       => $mail_ssl_options,
99            'mail_transport_options' => $mail_transport_options,
100            'title'                  => $title,
101            'smtp_helo_valid'        => $smtp_helo_valid,
102            'smtp_from_name_valid'   => $smtp_from_name_valid,
103            'SMTP_ACTIVE'            => $SMTP_ACTIVE,
104            'SMTP_AUTH'              => $SMTP_AUTH,
105            'SMTP_AUTH_USER'         => $SMTP_AUTH_USER,
106            'SMTP_DISP_NAME'         => $SMTP_DISP_NAME,
107            'SMTP_FROM_NAME'         => $SMTP_FROM_NAME,
108            'SMTP_HELO'              => $SMTP_HELO,
109            'SMTP_HOST'              => $SMTP_HOST,
110            'SMTP_PORT'              => $SMTP_PORT,
111            'SMTP_SSL'               => $SMTP_SSL,
112            'DKIM_DOMAIN'            => $DKIM_DOMAIN,
113            'DKIM_SELECTOR'          => $DKIM_SELECTOR,
114            'DKIM_KEY'               => $DKIM_KEY,
115        ]);
116    }
117}
118