]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/Repository/PConfig.php
Merge pull request #12562 from MrPetovan/bug/notices
[friendica.git] / src / Core / PConfig / Repository / PConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Core\PConfig\Repository;
23
24 use Friendica\App\Mode;
25 use Friendica\Core\Config\Util\ValueConversion;
26 use Friendica\Core\PConfig\Exception\PConfigPersistenceException;
27 use Friendica\Database\Database;
28
29 /**
30  * The Config model backend for users, which is using the general DB-model backend for user-configs
31  */
32 class PConfig
33 {
34         protected static $table_name = 'pconfig';
35
36         /** @var Database */
37         protected $db;
38         /** @var Mode */
39         protected $mode;
40
41         public function __construct(Database $db, Mode $mode)
42         {
43                 $this->db   = $db;
44                 $this->mode = $mode;
45         }
46
47         /**
48          * Checks if the model is currently connected
49          *
50          * @return bool
51          */
52         public function isConnected(): bool
53         {
54                 return $this->db->isConnected() & !$this->mode->isInstall();
55         }
56
57         /**
58          * Loads all configuration values and returns the loaded category as an array.
59          *
60          * @param int         $uid The id of the user to load
61          * @param string|null $cat The category of the configuration values to load
62          *
63          * @return string[][] The config array
64          *
65          * @throws PConfigPersistenceException In case the persistence layer throws errors
66          */
67         public function load(int $uid, ?string $cat = null): array
68         {
69                 $return = [];
70
71                 try {
72                         if (empty($cat)) {
73                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['uid' => $uid]);
74                         } else {
75                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
76                         }
77
78                         while ($config = $this->db->fetch($configs)) {
79                                 $key   = $config['k'];
80                                 $value = ValueConversion::toConfigValue($config['v']);
81
82                                 // just save it in case it is set
83                                 if (isset($value)) {
84                                         $return[$config['cat']][$key] = $value;
85                                 }
86                         }
87                 } catch (\Exception $exception) {
88                         throw new PConfigPersistenceException(sprintf('Cannot load config category %s for user %d', $cat, $uid), $exception);
89                 } finally {
90                         $this->db->close($configs);
91                 }
92
93                 return $return;
94         }
95
96         /**
97          * Get a particular user config variable out of the DB with the
98          * given category name ($cat) and a key ($key).
99          *
100          * Note: Boolean variables are defined as 0/1 in the database
101          *
102          * @param int         $uid The id of the user to load
103          * @param string $cat The category of the configuration value
104          * @param string $key The configuration key to query
105          *
106          * @return array|string|null Stored value or null if it does not exist
107          *
108          * @throws PConfigPersistenceException In case the persistence layer throws errors
109          */
110         public function get(int $uid, string $cat, string $key)
111         {
112                 if (!$this->isConnected()) {
113                         return null;
114                 }
115
116                 try {
117                         $config = $this->db->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
118                         if ($this->db->isResult($config)) {
119                                 $value = ValueConversion::toConfigValue($config['v']);
120
121                                 // just return it in case it is set
122                                 if (isset($value)) {
123                                         return $value;
124                                 }
125                         }
126                 } catch (\Exception $exception) {
127                         throw new PConfigPersistenceException(sprintf('Cannot get config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
128                 }
129
130                 return null;
131         }
132
133         /**
134          * Stores a config value ($value) in the category ($cat) under the key ($key) for a
135          * given user ($uid).
136          *
137          * Note: Please do not store booleans - convert to 0/1 integer values!
138          *
139          * @param int    $uid   The id of the user to load
140          * @param string $cat   The category of the configuration value
141          * @param string $key   The configuration key to set
142          * @param mixed  $value The value to store
143          *
144          * @return bool Operation success
145          *
146          * @throws PConfigPersistenceException In case the persistence layer throws errors
147          */
148         public function set(int $uid, string $cat, string $key, $value): bool
149         {
150                 if (!$this->isConnected()) {
151                         return false;
152                 }
153
154                 // We store our setting values in a string variable.
155                 // So we have to do the conversion here so that the compare below works.
156                 // The exception are array values.
157                 $compare_value = (!is_array($value) ? (string)$value : $value);
158                 $stored_value  = $this->get($uid, $cat, $key);
159
160                 if (isset($stored_value) && ($stored_value === $compare_value)) {
161                         return true;
162                 }
163
164                 try {
165                         $dbValue = ValueConversion::toDbValue($value);
166                         return $this->db->update(static::$table_name, ['v' => $dbValue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
167                 } catch (\Exception $exception) {
168                         throw new PConfigPersistenceException(sprintf('Cannot set config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
169                 }
170         }
171
172         /**
173          * Removes the configured value of the given user.
174          *
175          * @param int    $uid The id of the user to load
176          * @param string $cat The category of the configuration value
177          * @param string $key The configuration key to delete
178          *
179          * @return bool Operation success
180          *
181          * @throws PConfigPersistenceException In case the persistence layer throws errors
182          */
183         public function delete(int $uid, string $cat, string $key): bool
184         {
185                 if (!$this->isConnected()) {
186                         return false;
187                 }
188
189                 try {
190                         return $this->db->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
191                 } catch (\Exception $exception) {
192                         throw new PConfigPersistenceException(sprintf('Cannot delete config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
193                 }
194         }
195 }