xref: /webtrees/app/Site.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5b598eac0SGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22b48bb5e9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
233976b470SGreg Roach
24743d1d63SGreg Roachuse function in_array;
25b48bb5e9SGreg Roachuse function mb_substr;
26b48bb5e9SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * Provide an interface to the wt_site_setting table.
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Site
31c1010edaSGreg Roach{
32743d1d63SGreg Roach    // The following preferences contain sensitive data, and should not be logged.
33743d1d63SGreg Roach    private const SENSITIVE_PREFERENCES = [
34743d1d63SGreg Roach        'SMTP_AUTH_PASS'
35743d1d63SGreg Roach    ];
36743d1d63SGreg Roach
376808d533SGreg Roach    // The following preferences contain unimportant data, and should not be logged.
386808d533SGreg Roach    private const UNIMPORTANT_PREFERENCES = [
396808d533SGreg Roach        'next_xref'
406808d533SGreg Roach    ];
416808d533SGreg Roach
42a25f0a04SGreg Roach    /**
43a25f0a04SGreg Roach     * Everything from the wt_site_setting table.
44a25f0a04SGreg Roach     *
455d4b7ec2SGreg Roach     * @var array<string,string>
46a25f0a04SGreg Roach     */
4732f20c14SGreg Roach    public static $preferences = [];
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /**
50a25f0a04SGreg Roach     * Get the site’s configuration settings
51a25f0a04SGreg Roach     *
52a25f0a04SGreg Roach     * @param string $setting_name
5315d603e7SGreg Roach     * @param string $default
54a25f0a04SGreg Roach     *
5515d603e7SGreg Roach     * @return string
56a25f0a04SGreg Roach     */
57acf76a54SGreg Roach    public static function getPreference(string $setting_name, string $default = ''): string
58c1010edaSGreg Roach    {
59a25f0a04SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
60a25f0a04SGreg Roach        // so it is quicker to fetch them all in one go.
6154c1ab5eSGreg Roach        if (self::$preferences === []) {
62b48bb5e9SGreg Roach            self::$preferences = DB::table('site_setting')
63b48bb5e9SGreg Roach                ->pluck('setting_value', 'setting_name')
64b48bb5e9SGreg Roach                ->all();
65a25f0a04SGreg Roach        }
66a25f0a04SGreg Roach
67acf76a54SGreg Roach        return self::$preferences[$setting_name] ?? $default;
68a25f0a04SGreg Roach    }
69a25f0a04SGreg Roach
70a25f0a04SGreg Roach    /**
71a25f0a04SGreg Roach     * Set the site’s configuration settings.
72a25f0a04SGreg Roach     *
73a25f0a04SGreg Roach     * @param string $setting_name
7415d603e7SGreg Roach     * @param string $setting_value
75c7ff4153SGreg Roach     *
76c7ff4153SGreg Roach     * @return void
77a25f0a04SGreg Roach     */
78e364afe4SGreg Roach    public static function setPreference($setting_name, $setting_value): void
79c1010edaSGreg Roach    {
8032f20c14SGreg Roach        // The database column is only this long.
8132f20c14SGreg Roach        $setting_value = mb_substr($setting_value, 0, 2000);
8232f20c14SGreg Roach
8315d603e7SGreg Roach        if (self::getPreference($setting_name) !== $setting_value) {
84b48bb5e9SGreg Roach            DB::table('site_setting')->updateOrInsert([
85a25f0a04SGreg Roach                'setting_name' => $setting_name,
86b48bb5e9SGreg Roach            ], [
8732f20c14SGreg Roach                'setting_value' => $setting_value,
8813abd6f3SGreg Roach            ]);
89a25f0a04SGreg Roach
9015d603e7SGreg Roach            self::$preferences[$setting_name] = $setting_value;
91a25f0a04SGreg Roach
92743d1d63SGreg Roach            if (in_array($setting_name, self::SENSITIVE_PREFERENCES, true)) {
93743d1d63SGreg Roach                $setting_value = '********';
94743d1d63SGreg Roach            }
95743d1d63SGreg Roach
96b598eac0SGreg Roach            if (!in_array($setting_name, self::UNIMPORTANT_PREFERENCES, true)) {
97847d5489SGreg Roach                Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
98a25f0a04SGreg Roach            }
99a25f0a04SGreg Roach        }
100a25f0a04SGreg Roach    }
1016808d533SGreg Roach}
102