]> git.mxchange.org Git - friendica.git/blob - include/Core/PConfig.php
43735018e4ce143b2826ed61d61987d21cd9303e
[friendica.git] / include / Core / PConfig.php
1 <?php
2 namespace Friendica\Core;
3
4 use dbm;
5
6 /**
7  * @file include/Core/PConfig.php
8  * @brief contains the class with methods for the management
9  * of the user configuration
10  */
11
12 /**
13  * @brief Management of user configuration storage
14  * Note:
15  * Please do not store booleans - convert to 0/1 integer values
16  * The PConfig::get() functions return boolean false for keys that are unset,
17  * and this could lead to subtle bugs.
18  */
19 class PConfig {
20
21         /**
22          * @brief Loads all configuration values of a user's config family into a cached storage.
23          *
24          * All configuration values of the given user are stored in global cache
25          * which is available under the global variable $a->config[$uid].
26          *
27          * @param string $uid
28          *  The user_id
29          * @param string $family
30          *  The category of the configuration value
31          * @return void
32          */
33         public static function load($uid, $family) {
34                 $a = get_app();
35                 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
36                         dbesc($family),
37                         intval($uid)
38                 );
39                 if (dbm::is_result($r)) {
40                         foreach ($r as $rr) {
41                                 $k = $rr['k'];
42                                 $a->config[$uid][$family][$k] = $rr['v'];
43                         }
44                 } else if ($family != 'config') {
45                         // Negative caching
46                         $a->config[$uid][$family] = "!<unset>!";
47                 }
48         }
49
50         /**
51          * @brief Get a particular user's config variable given the category name
52          * ($family) and a key.
53          *
54          * Get a particular user's config value from the given category ($family)
55          * and the $key from a cached storage in $a->config[$uid].
56          *
57          * @param string $uid
58          *  The user_id
59          * @param string $family
60          *  The category of the configuration value
61          * @param string $key
62          *  The configuration key to query
63          * @param mixed $default_value optional
64          *  The value to return if key is not set (default: null)
65          * @param boolean $refresh optional
66          *  If true the config is loaded from the db and not from the cache (default: false)
67          * @return mixed Stored value or null if it does not exist
68          */
69         public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
70
71                 $a = get_app();
72
73                 if (!$refresh) {
74                         // Looking if the whole family isn't set
75                         if (isset($a->config[$uid][$family])) {
76                                 if ($a->config[$uid][$family] === '!<unset>!') {
77                                         return $default_value;
78                                 }
79                         }
80
81                         if (isset($a->config[$uid][$family][$key])) {
82                                 if ($a->config[$uid][$family][$key] === '!<unset>!') {
83                                         return $default_value;
84                                 }
85                                 return $a->config[$uid][$family][$key];
86                         }
87                 }
88
89                 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
90                         intval($uid),
91                         dbesc($family),
92                         dbesc($key)
93                 );
94
95                 if (count($ret)) {
96                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
97                         $a->config[$uid][$family][$key] = $val;
98
99                         return $val;
100                 } else {
101                         $a->config[$uid][$family][$key] = '!<unset>!';
102                 }
103                 return $default_value;
104         }
105
106         /**
107          * @brief Sets a configuration value for a user
108          *
109          * Stores a config value ($value) in the category ($family) under the key ($key)
110          * for the user_id $uid.
111          *
112          * @note Please do not store booleans - convert to 0/1 integer values!
113          *
114          * @param string $uid
115          *  The user_id
116          * @param string $family
117          *  The category of the configuration value
118          * @param string $key
119          *  The configuration key to set
120          * @param string $value
121          *  The value to store
122          * @return mixed Stored $value or false
123          */
124         public static function set($uid, $family, $key, $value) {
125
126                 $a = get_app();
127
128                 $stored = self::get($uid, $family, $key);
129
130                 if ($stored == $value) {
131                         return true;
132                 }
133
134                 // manage array value
135                 $dbvalue = (is_array($value) ? serialize($value):$value);
136
137                 $a->config[$uid][$family][$key] = $value;
138
139                 if (is_null($stored)) {
140                         $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
141                                 intval($uid),
142                                 dbesc($family),
143                                 dbesc($key),
144                                 dbesc($dbvalue),
145                                 dbesc($dbvalue)
146                         );
147                 } else {
148                         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
149                                 dbesc($dbvalue),
150                                 intval($uid),
151                                 dbesc($family),
152                                 dbesc($key)
153                         );
154                 }
155
156                 if ($ret) {
157                         return $value;
158                 }
159                 return $ret;
160         }
161
162         /**
163          * @brief Deletes the given key from the users's configuration.
164          *
165          * Removes the configured value from the stored cache in $a->config[$uid]
166          * and removes it from the database.
167          *
168          * @param string $uid The user_id
169          * @param string $family
170          *  The category of the configuration value
171          * @param string $key
172          *  The configuration key to delete
173          * @return mixed
174          */
175         public static function delete($uid,$family,$key) {
176
177                 $a = get_app();
178
179                 if (x($a->config[$uid][$family], $key)) {
180                         unset($a->config[$uid][$family][$key]);
181                 }
182
183                 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
184                         intval($uid),
185                         dbesc($family),
186                         dbesc($key)
187                 );
188
189                 return $ret;
190         }
191 }