]> git.mxchange.org Git - friendica.git/blob - include/Core/PConfig.php
Prevent a memory Access Violation when the database isn't connected
[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 ORDER BY `cat`, `k`, `id`",
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(! $refresh) {
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                 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
87                         intval($uid),
88                         dbesc($family),
89                         dbesc($key)
90                 );
91
92                 if(count($ret)) {
93                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
94                         $a->config[$uid][$family][$key] = $val;
95
96                         return $val;
97                 }
98                 else {
99                         $a->config[$uid][$family][$key] = '!<unset>!';
100                 }
101                 return $default_value;
102         }
103
104         /**
105          * @brief Sets a configuration value for a user
106          *
107          * Stores a config value ($value) in the category ($family) under the key ($key)
108          * for the user_id $uid.
109          *
110          * @note Please do not store booleans - convert to 0/1 integer values!
111          *
112          * @param string $uid
113          *  The user_id
114          * @param string $family
115          *  The category of the configuration value
116          * @param string $key
117          *  The configuration key to set
118          * @param string $value
119          *  The value to store
120          * @return mixed Stored $value or false
121          */
122         public static function set($uid, $family, $key, $value) {
123
124                 global $a;
125
126                 // manage array value
127                 $dbvalue = (is_array($value)?serialize($value):$value);
128
129                 $a->config[$uid][$family][$key] = $value;
130
131                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' )
132 ON DUPLICATE KEY UPDATE `v` = '%s'",
133                         intval($uid),
134                         dbesc($family),
135                         dbesc($key),
136                         dbesc($dbvalue),
137                         dbesc($dbvalue)
138                 );
139                 if ($ret) {
140                         return $value;
141                 }
142                 return $ret;
143         }
144
145         /**
146          * @brief Deletes the given key from the users's configuration.
147          *
148          * Removes the configured value from the stored cache in $a->config[$uid]
149          * and removes it from the database.
150          *
151          * @param string $uid The user_id
152          * @param string $family
153          *  The category of the configuration value
154          * @param string $key
155          *  The configuration key to delete
156          * @return mixed
157          */
158         public static function delete($uid,$family,$key) {
159
160                 global $a;
161                 if(x($a->config[$uid][$family],$key))
162                         unset($a->config[$uid][$family][$key]);
163                 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
164                         intval($uid),
165                         dbesc($family),
166                         dbesc($key)
167                 );
168                 return $ret;
169         }
170 }