1<?php 2namespace Fisharebest\Webtrees; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19/** 20 * class Site - Provide an interface to the wt_site_setting table. 21 */ 22class Site { 23 /** 24 * Everything from the wt_site_setting table. 25 * 26 * @var array 27 */ 28 private static $settings = null; 29 30 /** 31 * Get the site’s configuration settings 32 * 33 * @param string $setting_name 34 * 35 * @return string|null 36 */ 37 public static function getPreference($setting_name) { 38 // There are lots of settings, and we need to fetch lots of them on every page 39 // so it is quicker to fetch them all in one go. 40 if (self::$settings === null) { 41 self::$settings = Database::prepare( 42 "SELECT SQL_CACHE setting_name, setting_value FROM `##site_setting`" 43 )->fetchAssoc(); 44 } 45 46 // A setting that hasn't yet been set? 47 if (!array_key_exists($setting_name, self::$settings)) { 48 self::$settings[$setting_name] = null; 49 } 50 51 return self::$settings[$setting_name]; 52 } 53 54 /** 55 * Set the site’s configuration settings. 56 * 57 * @param string $setting_name 58 * @param string|int|bool $setting_value 59 */ 60 public static function setPreference($setting_name, $setting_value) { 61 // Only need to update the database if the setting has actually changed. 62 if (self::getPreference($setting_name) != $setting_value) { 63 if ($setting_value === null) { 64 Database::prepare( 65 "DELETE FROM `##site_setting` WHERE setting_name = :setting_name" 66 )->execute(array( 67 'setting_name' => $setting_name, 68 )); 69 } else { 70 Database::prepare( 71 "REPLACE INTO `##site_setting` (setting_name, setting_value)" . 72 " VALUES (:setting_name, LEFT(:setting_value, 255))" 73 )->execute(array( 74 'setting_name' => $setting_name, 75 'setting_value' => $setting_value, 76 )); 77 } 78 79 self::$settings[$setting_name] = $setting_value; 80 81 Log::addConfigurationLog('Site setting "' . $setting_name . '" set to "' . $setting_value . '"'); 82 } 83 } 84} 85