]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/Config.php
Check and add a server
[friendica.git] / src / Model / Config / Config.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Config;
23
24
25 /**
26  * The Config model backend, which is using the general DB-model backend for configs
27  */
28 class Config extends DbaConfig
29 {
30         /**
31          * Loads all configuration values and returns the loaded category as an array.
32          *
33          * @param string|null $cat The category of the configuration values to load
34          *
35          * @return array The config array
36          *
37          * @throws \Exception In case DB calls are invalid
38          */
39         public function load(string $cat = null)
40         {
41                 $return = [];
42
43                 if (empty($cat)) {
44                         $configs = $this->dba->select('config', ['cat', 'v', 'k']);
45                 } else {
46                         $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
47                 }
48
49                 while ($config = $this->dba->fetch($configs)) {
50
51                         $key   = $config['k'];
52                         $value = $this->toConfigValue($config['v']);
53
54                         // just save it in case it is set
55                         if (isset($value)) {
56                                 $return[$config['cat']][$key] = $value;
57                         }
58                 }
59                 $this->dba->close($configs);
60
61                 return $return;
62         }
63
64         /**
65          * Get a particular, system-wide config variable out of the DB with the
66          * given category name ($cat) and a key ($key).
67          *
68          * Note: Boolean variables are defined as 0/1 in the database
69          *
70          * @param string $cat The category of the configuration value
71          * @param string $key The configuration key to query
72          *
73          * @return array|string|null Stored value or null if it does not exist
74          *
75          * @throws \Exception In case DB calls are invalid
76          */
77         public function get(string $cat, string $key)
78         {
79                 if (!$this->isConnected()) {
80                         return null;
81                 }
82
83                 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
84                 if ($this->dba->isResult($config)) {
85                         $value = $this->toConfigValue($config['v']);
86
87                         // just return it in case it is set
88                         if (isset($value)) {
89                                 return $value;
90                         }
91                 }
92
93                 return null;
94         }
95
96         /**
97          * Stores a config value ($value) in the category ($cat) under the key ($key).
98          *
99          * Note: Please do not store booleans - convert to 0/1 integer values!
100          *
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
104          *
105          * @return bool Operation success
106          *
107          * @throws \Exception In case DB calls are invalid
108          */
109         public function set(string $cat, string $key, $value)
110         {
111                 if (!$this->isConnected()) {
112                         return false;
113                 }
114
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);
120
121                 if (isset($stored_value) && ($stored_value === $compare_value)) {
122                         return true;
123                 }
124
125                 $dbvalue = $this->toDbValue($value);
126
127                 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
128
129                 return $result;
130         }
131
132         /**
133          * Removes the configured value from the database.
134          *
135          * @param string $cat The category of the configuration value
136          * @param string $key The configuration key to delete
137          *
138          * @return bool Operation success
139          *
140          * @throws \Exception In case DB calls are invalid
141          */
142         public function delete(string $cat, string $key)
143         {
144                 if (!$this->isConnected()) {
145                         return false;
146                 }
147
148                 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);
149         }
150 }