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