1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg 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 22743d1d63SGreg Roachuse function in_array; 23b48bb5e9SGreg Roachuse function mb_substr; 24b48bb5e9SGreg Roach 25a25f0a04SGreg Roach/** 2676692c8bSGreg Roach * Provide an interface to the wt_site_setting table. 27a25f0a04SGreg Roach */ 28c1010edaSGreg Roachclass Site 29c1010edaSGreg Roach{ 30743d1d63SGreg Roach // The following preferences contain sensitive data, and should not be logged. 31743d1d63SGreg Roach private const SENSITIVE_PREFERENCES = [ 328a07c98eSGreg Roach 'SMTP_AUTH_PASS', 33743d1d63SGreg Roach ]; 34743d1d63SGreg Roach 356808d533SGreg Roach // The following preferences contain unimportant data, and should not be logged. 366808d533SGreg Roach private const UNIMPORTANT_PREFERENCES = [ 378a07c98eSGreg Roach 'next_xref', 388a07c98eSGreg Roach ]; 398a07c98eSGreg Roach 408a07c98eSGreg Roach // Default values for some site preferences. 418a07c98eSGreg Roach protected const DEFAULT_PREFERENCES = [ 428a07c98eSGreg Roach 'ALLOW_CHANGE_GEDCOM' => '1', 43302fdb8dSGreg Roach 'HIDE_ADDR_EMAIL' => '1', 448a07c98eSGreg Roach 'HIDE_ADDR_FAX' => '1', 458a07c98eSGreg Roach 'HIDE_ADDR_PHON' => '1', 468a07c98eSGreg Roach 'HIDE_ADDR_WWW' => '1', 478a07c98eSGreg Roach 'HIDE_AFN' => '1', 488a07c98eSGreg Roach 'HIDE_ALIA' => '1', 498a07c98eSGreg Roach 'HIDE_ANCI' => '1', 508a07c98eSGreg Roach 'HIDE_ANUL' => '1', 518a07c98eSGreg Roach 'HIDE_ASSO' => '1', 528a07c98eSGreg Roach 'HIDE_BARM' => '1', 538a07c98eSGreg Roach 'HIDE_BIRT_FAMC' => '1', 548a07c98eSGreg Roach 'HIDE_CHR' => '1', 558a07c98eSGreg Roach 'HIDE_DIVF' => '1', 568a07c98eSGreg Roach 'HIDE_ENGA' => '1', 578a07c98eSGreg Roach 'HIDE_FAM_CENS' => '1', 588a07c98eSGreg Roach 'HIDE_FAM_RESI' => '1', 598a07c98eSGreg Roach 'HIDE_FCOM' => '1', 608a07c98eSGreg Roach 'HIDE_IDNO' => '1', 618a07c98eSGreg Roach 'HIDE_LDS' => '1', 628a07c98eSGreg Roach 'HIDE_MARC' => '1', 638a07c98eSGreg Roach 'HIDE_MARL' => '1', 648a07c98eSGreg Roach 'HIDE_MARS' => '1', 658a07c98eSGreg Roach 'HIDE_NAME_FONE' => '1', 668a07c98eSGreg Roach 'HIDE_NAME_NPFX' => '1', 678a07c98eSGreg Roach 'HIDE_NAME_NSFX' => '1', 688a07c98eSGreg Roach 'HIDE_NAME_ROMN' => '1', 698a07c98eSGreg Roach 'HIDE_NAME_SOUR' => '1', 708a07c98eSGreg Roach 'HIDE_ORDN' => '1', 718a07c98eSGreg Roach 'HIDE_PLAC_FONE' => '1', 72676cffa0SGreg Roach 'HIDE_PLAC_FORM' => '1', 738a07c98eSGreg Roach 'HIDE_PLAC_MAP' => '1', 748a07c98eSGreg Roach 'HIDE_PLAC_NOTE' => '1', 758a07c98eSGreg Roach 'HIDE_PLAC_ROMN' => '1', 768a07c98eSGreg Roach 'HIDE_REFN' => '1', 778a07c98eSGreg Roach 'HIDE_RFN' => '1', 788a07c98eSGreg Roach 'HIDE_RIN' => '1', 79c8183f29SGreg Roach 'HIDE_SOUR_DATA' => '1', 808a07c98eSGreg Roach 'HIDE_SOUR_DATE' => '1', 818a07c98eSGreg Roach 'HIDE_SOUR_EVEN' => '1', 828a07c98eSGreg Roach 'HIDE_SOUR_NOTE' => '1', 838a07c98eSGreg Roach 'HIDE_SOUR_QUAY' => '1', 848a07c98eSGreg Roach 'HIDE_SSN' => '1', 858a07c98eSGreg Roach 'HIDE_SUBM' => '1', 868a07c98eSGreg Roach 'INDEX_DIRECTORY' => Webtrees::DATA_DIR, 878a07c98eSGreg Roach 'LANGUAGE' => 'en-US', 888a07c98eSGreg Roach 'MULTIPLE_TREE_THRESHOLD' => '500', 898a07c98eSGreg Roach 'SMTP_ACTIVE' => 'internal', 908a07c98eSGreg Roach 'SMTP_AUTH' => '1', 918a07c98eSGreg Roach 'SMTP_HOST' => 'localhost', 928a07c98eSGreg Roach 'SMTP_PORT' => '25', 938a07c98eSGreg Roach 'SMTP_SSL' => 'none', 948a07c98eSGreg Roach 'THEME_DIR' => 'webtrees', 958a07c98eSGreg Roach 'TIMEZONE' => 'UTC', 968a07c98eSGreg Roach 'USE_REGISTRATION_MODULE' => '1', 976808d533SGreg Roach ]; 986808d533SGreg Roach 99a25f0a04SGreg Roach /** 100a25f0a04SGreg Roach * Everything from the wt_site_setting table. 101a25f0a04SGreg Roach * 1025d4b7ec2SGreg Roach * @var array<string,string> 103a25f0a04SGreg Roach */ 1048a07c98eSGreg Roach public static array $preferences = []; 105a25f0a04SGreg Roach 106a25f0a04SGreg Roach /** 107a25f0a04SGreg Roach * Set the site’s configuration settings. 108a25f0a04SGreg Roach * 109a25f0a04SGreg Roach * @param string $setting_name 11015d603e7SGreg Roach * @param string $setting_value 111c7ff4153SGreg Roach * 112c7ff4153SGreg Roach * @return void 113a25f0a04SGreg Roach */ 11424f2a3afSGreg Roach public static function setPreference(string $setting_name, string $setting_value): void 115c1010edaSGreg Roach { 11632f20c14SGreg Roach // The database column is only this long. 11732f20c14SGreg Roach $setting_value = mb_substr($setting_value, 0, 2000); 11832f20c14SGreg Roach 11915d603e7SGreg Roach if (self::getPreference($setting_name) !== $setting_value) { 120b48bb5e9SGreg Roach DB::table('site_setting')->updateOrInsert([ 121a25f0a04SGreg Roach 'setting_name' => $setting_name, 122b48bb5e9SGreg Roach ], [ 12332f20c14SGreg Roach 'setting_value' => $setting_value, 12413abd6f3SGreg Roach ]); 125a25f0a04SGreg Roach 12615d603e7SGreg Roach self::$preferences[$setting_name] = $setting_value; 127a25f0a04SGreg Roach 128743d1d63SGreg Roach if (in_array($setting_name, self::SENSITIVE_PREFERENCES, true)) { 129743d1d63SGreg Roach $setting_value = '********'; 130743d1d63SGreg Roach } 131743d1d63SGreg Roach 132b598eac0SGreg Roach if (!in_array($setting_name, self::UNIMPORTANT_PREFERENCES, true)) { 133847d5489SGreg Roach Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null); 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach } 1378a07c98eSGreg Roach 1388a07c98eSGreg Roach /** 1398a07c98eSGreg Roach * Get the site’s configuration settings 1408a07c98eSGreg Roach * 1418a07c98eSGreg Roach * @param string $setting_name 1428a07c98eSGreg Roach * 1438a07c98eSGreg Roach * @return string 1448a07c98eSGreg Roach */ 1458a07c98eSGreg Roach public static function getPreference(string $setting_name): string 1468a07c98eSGreg Roach { 1478a07c98eSGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 1488a07c98eSGreg Roach // so it is quicker to fetch them all in one go. 1498a07c98eSGreg Roach if (self::$preferences === []) { 1508a07c98eSGreg Roach self::$preferences = DB::table('site_setting') 1518a07c98eSGreg Roach ->pluck('setting_value', 'setting_name') 1528a07c98eSGreg Roach ->all(); 1538a07c98eSGreg Roach } 1548a07c98eSGreg Roach 1558a07c98eSGreg Roach return self::$preferences[$setting_name] ?? self::DEFAULT_PREFERENCES[$setting_name] ?? ''; 1568a07c98eSGreg Roach } 1576808d533SGreg Roach} 158