Completed
Push — develop ( 778c00...2ca406 )
by Greg
24:21 queued 15:55
created

EmailPreferencesPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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