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\Model\Config;
26 * The Config model backend, which is using the general DB-model backend for configs
28 class Config extends DbaConfig
31 * Loads all configuration values and returns the loaded category as an array.
33 * @param string|null $cat The category of the configuration values to load
35 * @return array The config array
37 * @throws \Exception In case DB calls are invalid
39 public function load(string $cat = null)
44 $configs = $this->dba->select('config', ['cat', 'v', 'k']);
46 $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
49 while ($config = $this->dba->fetch($configs)) {
52 $value = $this->toConfigValue($config['v']);
54 // just save it in case it is set
56 $return[$config['cat']][$key] = $value;
59 $this->dba->close($configs);
65 * Get a particular, system-wide config variable out of the DB with the
66 * given category name ($cat) and a key ($key).
68 * Note: Boolean variables are defined as 0/1 in the database
70 * @param string $cat The category of the configuration value
71 * @param string $key The configuration key to query
73 * @return array|string|null Stored value or null if it does not exist
75 * @throws \Exception In case DB calls are invalid
77 public function get(string $cat, string $key)
79 if (!$this->isConnected()) {
83 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
84 if ($this->dba->isResult($config)) {
85 $value = $this->toConfigValue($config['v']);
87 // just return it in case it is set
97 * Stores a config value ($value) in the category ($cat) under the key ($key).
99 * Note: Please do not store booleans - convert to 0/1 integer values!
101 * @param string $cat The category of the configuration value
102 * @param string $key The configuration key to set
103 * @param mixed $value The value to store
105 * @return bool Operation success
107 * @throws \Exception In case DB calls are invalid
109 public function set(string $cat, string $key, $value)
111 if (!$this->isConnected()) {
115 // We store our setting values in a string variable.
116 // So we have to do the conversion here so that the compare below works.
117 // The exception are array values.
118 $compare_value = (!is_array($value) ? (string)$value : $value);
119 $stored_value = $this->get($cat, $key);
121 if (isset($stored_value) && ($stored_value === $compare_value)) {
125 $dbvalue = $this->toDbValue($value);
127 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
133 * Removes the configured value from the database.
135 * @param string $cat The category of the configuration value
136 * @param string $key The configuration key to delete
138 * @return bool Operation success
140 * @throws \Exception In case DB calls are invalid
142 public function delete(string $cat, string $key)
144 if (!$this->isConnected()) {
148 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);