]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
9f6c49bb415d775694d00d6662f0a402e897dffd
[friendica.git] / src / Core / PConfig.php
1 <?php
2 namespace Friendica\Core;
3
4 use Friendica\Database\Dbm;
5 use dba;
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                 dba::close($r);
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), array('limit' => 1));
93                 if (Dbm::is_result($ret)) {
94                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
95                         $a->config[$uid][$family][$key] = $val;
96                         self::$in_db[$uid][$family][$key] = true;
97
98                         return $val;
99                 } else {
100                         $a->config[$uid][$family][$key] = '!<unset>!';
101                         self::$in_db[$uid][$family][$key] = false;
102
103                         return $default_value;
104                 }
105         }
106
107         /**
108          * @brief Sets a configuration value for a user
109          *
110          * Stores a config value ($value) in the category ($family) under the key ($key)
111          * for the user_id $uid.
112          *
113          * @note Please do not store booleans - convert to 0/1 integer values!
114          *
115          * @param string $uid
116          *  The user_id
117          * @param string $family
118          *  The category of the configuration value
119          * @param string $key
120          *  The configuration key to set
121          * @param string $value
122          *  The value to store
123          * @return mixed Stored $value or false
124          */
125         public static function set($uid, $family, $key, $value) {
126
127                 $a = get_app();
128
129                 // We store our setting values in a string variable.
130                 // So we have to do the conversion here so that the compare below works.
131                 // The exception are array values.
132                 $dbvalue = (!is_array($value) ? (string)$value : $value);
133
134                 $stored = self::get($uid, $family, $key, null, true);
135
136                 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
137                         return true;
138                 }
139
140                 $a->config[$uid][$family][$key] = $dbvalue;
141
142                 // manage array value
143                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
144
145                 dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
146
147                 if ($ret) {
148                         self::$in_db[$uid][$family][$key] = true;
149                         return $value;
150                 }
151                 return $ret;
152         }
153
154         /**
155          * @brief Deletes the given key from the users's configuration.
156          *
157          * Removes the configured value from the stored cache in $a->config[$uid]
158          * and removes it from the database.
159          *
160          * @param string $uid The user_id
161          * @param string $family
162          *  The category of the configuration value
163          * @param string $key
164          *  The configuration key to delete
165          * @return mixed
166          */
167         public static function delete($uid,$family,$key) {
168
169                 $a = get_app();
170
171                 if (x($a->config[$uid][$family], $key)) {
172                         unset($a->config[$uid][$family][$key]);
173                         unset(self::$in_db[$uid][$family][$key]);
174                 }
175
176                 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
177
178                 return $ret;
179         }
180 }