]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
e5a852e40393fb05ff701485b7773819f3160d01
[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                 if (dbm::is_result($r)) {
41                         while ($rr = dba::fetch($r)) {
42                                 $k = $rr['k'];
43                                 $a->config[$uid][$family][$k] = $rr['v'];
44                                 self::$in_db[$uid][$family][$k] = true;
45                         }
46                 } else if ($family != 'config') {
47                         // Negative caching
48                         $a->config[$uid][$family] = "!<unset>!";
49                 }
50         }
51
52         /**
53          * @brief Get a particular user's config variable given the category name
54          * ($family) and a key.
55          *
56          * Get a particular user's config value from the given category ($family)
57          * and the $key from a cached storage in $a->config[$uid].
58          *
59          * @param string $uid
60          *  The user_id
61          * @param string $family
62          *  The category of the configuration value
63          * @param string $key
64          *  The configuration key to query
65          * @param mixed $default_value optional
66          *  The value to return if key is not set (default: null)
67          * @param boolean $refresh optional
68          *  If true the config is loaded from the db and not from the cache (default: false)
69          * @return mixed Stored value or null if it does not exist
70          */
71         public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
72
73                 $a = get_app();
74
75                 if (!$refresh) {
76                         // Looking if the whole family isn't set
77                         if (isset($a->config[$uid][$family])) {
78                                 if ($a->config[$uid][$family] === '!<unset>!') {
79                                         return $default_value;
80                                 }
81                         }
82
83                         if (isset($a->config[$uid][$family][$key])) {
84                                 if ($a->config[$uid][$family][$key] === '!<unset>!') {
85                                         return $default_value;
86                                 }
87                                 return $a->config[$uid][$family][$key];
88                         }
89                 }
90
91                 $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array('limit' => 1));
92                 if (dbm::is_result($ret)) {
93                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
94                         $a->config[$uid][$family][$key] = $val;
95                         self::$in_db[$uid][$family][$key] = true;
96
97                         return $val;
98                 } else {
99                         $a->config[$uid][$family][$key] = '!<unset>!';
100                         self::$in_db[$uid][$family][$key] = false;
101
102                         return $default_value;
103                 }
104         }
105
106         /**
107          * @brief Sets a configuration value for a user
108          *
109          * Stores a config value ($value) in the category ($family) under the key ($key)
110          * for the user_id $uid.
111          *
112          * @note Please do not store booleans - convert to 0/1 integer values!
113          *
114          * @param string $uid
115          *  The user_id
116          * @param string $family
117          *  The category of the configuration value
118          * @param string $key
119          *  The configuration key to set
120          * @param string $value
121          *  The value to store
122          * @return mixed Stored $value or false
123          */
124         public static function set($uid, $family, $key, $value) {
125
126                 $a = get_app();
127
128                 // We store our setting values in a string variable.
129                 // So we have to do the conversion here so that the compare below works.
130                 // The exception are array values.
131                 $dbvalue = (!is_array($value) ? (string)$value : $value);
132
133                 $stored = self::get($uid, $family, $key, null, true);
134
135                 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
136                         return true;
137                 }
138
139                 $a->config[$uid][$family][$key] = $dbvalue;
140
141                 // manage array value
142                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
143
144                 if (is_null($stored) || !self::$in_db[$uid][$family][$key]) {
145                         dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
146                 } else {
147                         dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
148                 }
149
150                 if ($ret) {
151                         self::$in_db[$uid][$family][$key] = true;
152                         return $value;
153                 }
154                 return $ret;
155         }
156
157         /**
158          * @brief Deletes the given key from the users's configuration.
159          *
160          * Removes the configured value from the stored cache in $a->config[$uid]
161          * and removes it from the database.
162          *
163          * @param string $uid The user_id
164          * @param string $family
165          *  The category of the configuration value
166          * @param string $key
167          *  The configuration key to delete
168          * @return mixed
169          */
170         public static function delete($uid,$family,$key) {
171
172                 $a = get_app();
173
174                 if (x($a->config[$uid][$family], $key)) {
175                         unset($a->config[$uid][$family][$key]);
176                         unset(self::$in_db[$uid][$family][$key]);
177                 }
178
179                 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
180
181                 return $ret;
182         }
183 }