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