xref: /webtrees/app/Site.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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;
21
22use function in_array;
23use function mb_substr;
24
25/**
26 * Provide an interface to the wt_site_setting table.
27 */
28class Site
29{
30    // The following preferences contain sensitive data, and should not be logged.
31    private const SENSITIVE_PREFERENCES = [
32        'SMTP_AUTH_PASS',
33    ];
34
35    // The following preferences contain unimportant data, and should not be logged.
36    private const UNIMPORTANT_PREFERENCES = [
37        'next_xref',
38    ];
39
40    // Default values for some site preferences.
41    protected const DEFAULT_PREFERENCES = [
42        'ALLOW_CHANGE_GEDCOM'     => '1',
43        'HIDE_ADDR_EMAIL'         => '1',
44        'HIDE_ADDR_FAX'           => '1',
45        'HIDE_ADDR_PHON'          => '1',
46        'HIDE_ADDR_WWW'           => '1',
47        'HIDE_AFN'                => '1',
48        'HIDE_ALIA'               => '1',
49        'HIDE_ANCI'               => '1',
50        'HIDE_ANUL'               => '1',
51        'HIDE_ASSO'               => '1',
52        'HIDE_BARM'               => '1',
53        'HIDE_BIRT_FAMC'          => '1',
54        'HIDE_CHR'                => '1',
55        'HIDE_DIVF'               => '1',
56        'HIDE_ENGA'               => '1',
57        'HIDE_FAM_CENS'           => '1',
58        'HIDE_FAM_RESI'           => '1',
59        'HIDE_FCOM'               => '1',
60        'HIDE_IDNO'               => '1',
61        'HIDE_LDS'                => '1',
62        'HIDE_MARC'               => '1',
63        'HIDE_MARL'               => '1',
64        'HIDE_MARS'               => '1',
65        'HIDE_NAME_FONE'          => '1',
66        'HIDE_NAME_NPFX'          => '1',
67        'HIDE_NAME_NSFX'          => '1',
68        'HIDE_NAME_ROMN'          => '1',
69        'HIDE_NAME_SOUR'          => '1',
70        'HIDE_ORDN'               => '1',
71        'HIDE_PLAC_FONE'          => '1',
72        'HIDE_PLAC_FORM'          => '1',
73        'HIDE_PLAC_MAP'           => '1',
74        'HIDE_PLAC_NOTE'          => '1',
75        'HIDE_PLAC_ROMN'          => '1',
76        'HIDE_REFN'               => '1',
77        'HIDE_RFN'                => '1',
78        'HIDE_RIN'                => '1',
79        'HIDE_SOUR_DATA'          => '1',
80        'HIDE_SOUR_DATE'          => '1',
81        'HIDE_SOUR_EVEN'          => '1',
82        'HIDE_SOUR_NOTE'          => '1',
83        'HIDE_SOUR_QUAY'          => '1',
84        'HIDE_SSN'                => '1',
85        'HIDE_SUBM'               => '1',
86        'INDEX_DIRECTORY'         => Webtrees::DATA_DIR,
87        'LANGUAGE'                => 'en-US',
88        'MULTIPLE_TREE_THRESHOLD' => '500',
89        'SMTP_ACTIVE'             => 'internal',
90        'SMTP_AUTH'               => '1',
91        'SMTP_HOST'               => 'localhost',
92        'SMTP_PORT'               => '25',
93        'SMTP_SSL'                => 'none',
94        'THEME_DIR'               => 'webtrees',
95        'TIMEZONE'                => 'UTC',
96        'USE_REGISTRATION_MODULE' => '1',
97    ];
98
99    /**
100     * Everything from the wt_site_setting table.
101     *
102     * @var array<string,string>
103     */
104    public static array $preferences = [];
105
106    /**
107     * Set the site’s configuration settings.
108     *
109     * @param string $setting_name
110     * @param string $setting_value
111     *
112     * @return void
113     */
114    public static function setPreference(string $setting_name, string $setting_value): void
115    {
116        // The database column is only this long.
117        $setting_value = mb_substr($setting_value, 0, 2000);
118
119        if (self::getPreference($setting_name) !== $setting_value) {
120            DB::table('site_setting')->updateOrInsert([
121                'setting_name' => $setting_name,
122            ], [
123                'setting_value' => $setting_value,
124            ]);
125
126            self::$preferences[$setting_name] = $setting_value;
127
128            if (in_array($setting_name, self::SENSITIVE_PREFERENCES, true)) {
129                $setting_value = '********';
130            }
131
132            if (!in_array($setting_name, self::UNIMPORTANT_PREFERENCES, true)) {
133                Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
134            }
135        }
136    }
137
138    /**
139     * Get the site’s configuration settings
140     *
141     * @param string $setting_name
142     *
143     * @return string
144     */
145    public static function getPreference(string $setting_name): string
146    {
147        // There are lots of settings, and we need to fetch lots of them on every page
148        // so it is quicker to fetch them all in one go.
149        if (self::$preferences === []) {
150            self::$preferences = DB::table('site_setting')
151                ->pluck('setting_value', 'setting_name')
152                ->all();
153        }
154
155        return self::$preferences[$setting_name] ?? self::DEFAULT_PREFERENCES[$setting_name] ?? '';
156    }
157}
158