3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Config\Model;
24 use Friendica\Database\Database;
27 * The Config model backend, which is using the general DB-model backend for configs
35 * @param Database $dba The database connection of this model
37 public function __construct(Database $dba)
43 * Checks if the model is currently connected
47 public function isConnected()
49 return $this->dba->isConnected();
53 * Loads all configuration values and returns the loaded category as an array.
55 * @param string|null $cat The category of the configuration values to load
57 * @return array The config array
59 * @throws \Exception In case DB calls are invalid
61 public function load(string $cat = null)
66 $configs = $this->dba->select('config', ['cat', 'v', 'k']);
68 $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
71 while ($config = $this->dba->fetch($configs)) {
74 $value = DbaUtils::toConfigValue($config['v']);
76 // just save it in case it is set
78 $return[$config['cat']][$key] = $value;
81 $this->dba->close($configs);
87 * Get a particular, system-wide config variable out of the DB with the
88 * given category name ($cat) and a key ($key).
90 * Note: Boolean variables are defined as 0/1 in the database
92 * @param string $cat The category of the configuration value
93 * @param string $key The configuration key to query
95 * @return array|string|null Stored value or null if it does not exist
97 * @throws \Exception In case DB calls are invalid
99 public function get(string $cat, string $key)
101 if (!$this->isConnected()) {
105 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
106 if ($this->dba->isResult($config)) {
107 $value = DbaUtils::toConfigValue($config['v']);
109 // just return it in case it is set
119 * Stores a config value ($value) in the category ($cat) under the key ($key).
121 * Note: Please do not store booleans - convert to 0/1 integer values!
123 * @param string $cat The category of the configuration value
124 * @param string $key The configuration key to set
125 * @param mixed $value The value to store
127 * @return bool Operation success
129 * @throws \Exception In case DB calls are invalid
131 public function set(string $cat, string $key, $value)
133 if (!$this->isConnected()) {
137 // We store our setting values in a string variable.
138 // So we have to do the conversion here so that the compare below works.
139 // The exception are array values.
140 $compare_value = (!is_array($value) ? (string)$value : $value);
141 $stored_value = $this->get($cat, $key);
143 if (isset($stored_value) && ($stored_value === $compare_value)) {
147 $dbvalue = DbaUtils::toDbValue($value);
149 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
155 * Removes the configured value from the database.
157 * @param string $cat The category of the configuration value
158 * @param string $key The configuration key to delete
160 * @return bool Operation success
162 * @throws \Exception In case DB calls are invalid
164 public function delete(string $cat, string $key)
166 if (!$this->isConnected()) {
170 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);