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