xref: /webtrees/app/Site.php (revision 5d4b7ec24d2b390be41475c9b93e00d957e93fef)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://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
37a25f0a04SGreg Roach    /**
38a25f0a04SGreg Roach     * Everything from the wt_site_setting table.
39a25f0a04SGreg Roach     *
40*5d4b7ec2SGreg Roach     * @var array<string,string>
41a25f0a04SGreg Roach     */
4232f20c14SGreg Roach    public static $preferences = [];
43a25f0a04SGreg Roach
44a25f0a04SGreg Roach    /**
45a25f0a04SGreg Roach     * Get the site’s configuration settings
46a25f0a04SGreg Roach     *
47a25f0a04SGreg Roach     * @param string $setting_name
4815d603e7SGreg Roach     * @param string $default
49a25f0a04SGreg Roach     *
5015d603e7SGreg Roach     * @return string
51a25f0a04SGreg Roach     */
52acf76a54SGreg Roach    public static function getPreference(string $setting_name, string $default = ''): string
53c1010edaSGreg Roach    {
54a25f0a04SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
55a25f0a04SGreg Roach        // so it is quicker to fetch them all in one go.
5654c1ab5eSGreg Roach        if (self::$preferences === []) {
57b48bb5e9SGreg Roach            self::$preferences = DB::table('site_setting')
58b48bb5e9SGreg Roach                ->pluck('setting_value', 'setting_name')
59b48bb5e9SGreg Roach                ->all();
60a25f0a04SGreg Roach        }
61a25f0a04SGreg Roach
62acf76a54SGreg Roach        return self::$preferences[$setting_name] ?? $default;
63a25f0a04SGreg Roach    }
64a25f0a04SGreg Roach
65a25f0a04SGreg Roach    /**
66a25f0a04SGreg Roach     * Set the site’s configuration settings.
67a25f0a04SGreg Roach     *
68a25f0a04SGreg Roach     * @param string $setting_name
6915d603e7SGreg Roach     * @param string $setting_value
70c7ff4153SGreg Roach     *
71c7ff4153SGreg Roach     * @return void
72a25f0a04SGreg Roach     */
73e364afe4SGreg Roach    public static function setPreference($setting_name, $setting_value): void
74c1010edaSGreg Roach    {
7532f20c14SGreg Roach        // The database column is only this long.
7632f20c14SGreg Roach        $setting_value = mb_substr($setting_value, 0, 2000);
7732f20c14SGreg Roach
7815d603e7SGreg Roach        if (self::getPreference($setting_name) !== $setting_value) {
79b48bb5e9SGreg Roach            DB::table('site_setting')->updateOrInsert([
80a25f0a04SGreg Roach                'setting_name' => $setting_name,
81b48bb5e9SGreg Roach            ], [
8232f20c14SGreg Roach                'setting_value' => $setting_value,
8313abd6f3SGreg Roach            ]);
84a25f0a04SGreg Roach
8515d603e7SGreg Roach            self::$preferences[$setting_name] = $setting_value;
86a25f0a04SGreg Roach
87743d1d63SGreg Roach            if (in_array($setting_name, self::SENSITIVE_PREFERENCES, true)) {
88743d1d63SGreg Roach                $setting_value = '********';
89743d1d63SGreg Roach            }
90743d1d63SGreg Roach
91847d5489SGreg Roach            Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
92a25f0a04SGreg Roach        }
93a25f0a04SGreg Roach    }
94a25f0a04SGreg Roach}
95