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