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