]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/PConfig.php
Check and add a server
[friendica.git] / src / Model / Config / PConfig.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 for users, which is using the general DB-model backend for user-configs
27  */
28 class PConfig extends DbaConfig
29 {
30         /**
31          * Loads all configuration values and returns the loaded category as an array.
32          *
33          * @param int         $uid The id of the user to load
34          * @param string|null $cat The category of the configuration values to load
35          *
36          * @return array The config array
37          *
38          * @throws \Exception In case DB calls are invalid
39          */
40         public function load(int $uid, string $cat = null)
41         {
42                 $return = [];
43
44                 if (empty($cat)) {
45                         $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
46                 } else {
47                         $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
48                 }
49
50                 while ($config = $this->dba->fetch($configs)) {
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 user 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 int         $uid The id of the user to load
71          * @param string $cat The category of the configuration value
72          * @param string $key The configuration key to query
73          *
74          * @return array|string|null Stored value or null if it does not exist
75          *
76          * @throws \Exception In case DB calls are invalid
77          */
78         public function get(int $uid, string $cat, string $key)
79         {
80                 if (!$this->isConnected()) {
81                         return null;
82                 }
83
84                 $config = $this->dba->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
85                 if ($this->dba->isResult($config)) {
86                         $value = $this->toConfigValue($config['v']);
87
88                         // just return it in case it is set
89                         if (isset($value)) {
90                                 return $value;
91                         }
92                 }
93
94                 return null;
95         }
96
97         /**
98          * Stores a config value ($value) in the category ($cat) under the key ($key) for a
99          * given user ($uid).
100          *
101          * Note: Please do not store booleans - convert to 0/1 integer values!
102          *
103          * @param int    $uid   The id of the user to load
104          * @param string $cat   The category of the configuration value
105          * @param string $key   The configuration key to set
106          * @param mixed  $value The value to store
107          *
108          * @return bool Operation success
109          *
110          * @throws \Exception In case DB calls are invalid
111          */
112         public function set(int $uid, string $cat, string $key, $value)
113         {
114                 if (!$this->isConnected()) {
115                         return false;
116                 }
117
118                 // We store our setting values in a string variable.
119                 // So we have to do the conversion here so that the compare below works.
120                 // The exception are array values.
121                 $compare_value = (!is_array($value) ? (string)$value : $value);
122                 $stored_value  = $this->get($uid, $cat, $key);
123
124                 if (isset($stored_value) && ($stored_value === $compare_value)) {
125                         return true;
126                 }
127
128                 $dbvalue = $this->toDbValue($value);
129
130                 $result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
131
132                 return $result;
133         }
134
135         /**
136          * Removes the configured value of the given user.
137          *
138          * @param int    $uid The id of the user to load
139          * @param string $cat The category of the configuration value
140          * @param string $key The configuration key to delete
141          *
142          * @return bool Operation success
143          *
144          * @throws \Exception In case DB calls are invalid
145          */
146         public function delete(int $uid, string $cat, string $key)
147         {
148                 if (!$this->isConnected()) {
149                         return false;
150                 }
151
152                 return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
153         }
154 }