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