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