]> git.mxchange.org Git - friendica.git/blob - include/Core/PConfig.php
More usage of dbm::is_result($r) instead of count($r):
[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                 $a = get_app();
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 (dbm::is_result($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                 $a = get_app();
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                 } else {
98                         $a->config[$uid][$family][$key] = '!<unset>!';
99                 }
100                 return $default_value;
101         }
102
103         /**
104          * @brief Sets a configuration value for a user
105          *
106          * Stores a config value ($value) in the category ($family) under the key ($key)
107          * for the user_id $uid.
108          *
109          * @note Please do not store booleans - convert to 0/1 integer values!
110          *
111          * @param string $uid
112          *  The user_id
113          * @param string $family
114          *  The category of the configuration value
115          * @param string $key
116          *  The configuration key to set
117          * @param string $value
118          *  The value to store
119          * @return mixed Stored $value or false
120          */
121         public static function set($uid, $family, $key, $value) {
122
123                 $a = get_app();
124
125                 $stored = self::get($uid, $family, $key);
126
127                 if ($stored == $value) {
128                         return true;
129                 }
130
131                 // manage array value
132                 $dbvalue = (is_array($value) ? serialize($value):$value);
133
134                 $a->config[$uid][$family][$key] = $value;
135
136                 if (is_null($stored)) {
137                         $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
138                                 intval($uid),
139                                 dbesc($family),
140                                 dbesc($key),
141                                 dbesc($dbvalue),
142                                 dbesc($dbvalue)
143                         );
144                 } else {
145                         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
146                                 dbesc($dbvalue),
147                                 intval($uid),
148                                 dbesc($family),
149                                 dbesc($key)
150                         );
151                 }
152
153                 if ($ret) {
154                         return $value;
155                 }
156                 return $ret;
157         }
158
159         /**
160          * @brief Deletes the given key from the users's configuration.
161          *
162          * Removes the configured value from the stored cache in $a->config[$uid]
163          * and removes it from the database.
164          *
165          * @param string $uid The user_id
166          * @param string $family
167          *  The category of the configuration value
168          * @param string $key
169          *  The configuration key to delete
170          * @return mixed
171          */
172         public static function delete($uid,$family,$key) {
173
174                 $a = get_app();
175
176                 if (x($a->config[$uid][$family], $key)) {
177                         unset($a->config[$uid][$family][$key]);
178                 }
179
180                 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
181                         intval($uid),
182                         dbesc($family),
183                         dbesc($key)
184                 );
185
186                 return $ret;
187         }
188 }