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