xref: /webtrees/app/Site.php (revision b45eba7a8f95143507ff14772c1e2a243489ae21)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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    /**
43     * Everything from the wt_site_setting table.
44     *
45     * @var array<string,string>
46     */
47    public static $preferences = [];
48
49    /**
50     * Get the site’s configuration settings
51     *
52     * @param string $setting_name
53     * @param string $default
54     *
55     * @return string
56     */
57    public static function getPreference(string $setting_name, string $default = ''): string
58    {
59        // There are lots of settings, and we need to fetch lots of them on every page
60        // so it is quicker to fetch them all in one go.
61        if (self::$preferences === []) {
62            self::$preferences = DB::table('site_setting')
63                ->pluck('setting_value', 'setting_name')
64                ->all();
65        }
66
67        return self::$preferences[$setting_name] ?? $default;
68    }
69
70    /**
71     * Set the site’s configuration settings.
72     *
73     * @param string $setting_name
74     * @param string $setting_value
75     *
76     * @return void
77     */
78    public static function setPreference($setting_name, $setting_value): void
79    {
80        // The database column is only this long.
81        $setting_value = mb_substr($setting_value, 0, 2000);
82
83        if (self::getPreference($setting_name) !== $setting_value) {
84            DB::table('site_setting')->updateOrInsert([
85                'setting_name' => $setting_name,
86            ], [
87                'setting_value' => $setting_value,
88            ]);
89
90            self::$preferences[$setting_name] = $setting_value;
91
92            if (in_array($setting_name, self::SENSITIVE_PREFERENCES, true)) {
93                $setting_value = '********';
94            }
95
96            if (!in_array($setting_name, self::UNIMPORTANT_PREFERENCES)) {
97                Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
98            }
99        }
100    }
101}
102