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