]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Merge pull request #1 from friendica/develop
[friendica.git] / src / Core / PConfig.php
1 <?php
2 namespace Friendica\Core;
3
4 use dbm;
5
6 /**
7  * @file include/Core/PConfig.php
8  * @brief contains the class with methods for the management
9  * of the user configuration
10  */
11
12 /**
13  * @brief Management of user configuration storage
14  * Note:
15  * Please do not store booleans - convert to 0/1 integer values
16  * The PConfig::get() functions return boolean false for keys that are unset,
17  * and this could lead to subtle bugs.
18  */
19 class PConfig {
20
21         private static $in_db;
22
23         /**
24          * @brief Loads all configuration values of a user's config family into a cached storage.
25          *
26          * All configuration values of the given user are stored in global cache
27          * which is available under the global variable $a->config[$uid].
28          *
29          * @param string $uid
30          *  The user_id
31          * @param string $family
32          *  The category of the configuration value
33          * @return void
34          */
35         public static function load($uid, $family) {
36                 $a = get_app();
37                 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
38                         dbesc($family),
39                         intval($uid)
40                 );
41                 if (dbm::is_result($r)) {
42                         foreach ($r as $rr) {
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 = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
93                         intval($uid),
94                         dbesc($family),
95                         dbesc($key)
96                 );
97
98                 if (count($ret)) {
99                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
100                         $a->config[$uid][$family][$key] = $val;
101                         self::$in_db[$uid][$family][$key] = true;
102
103                         return $val;
104                 } else {
105                         $a->config[$uid][$family][$key] = '!<unset>!';
106                         self::$in_db[$uid][$family][$key] = false;
107
108                         return $default_value;
109                 }
110         }
111
112         /**
113          * @brief Sets a configuration value for a user
114          *
115          * Stores a config value ($value) in the category ($family) under the key ($key)
116          * for the user_id $uid.
117          *
118          * @note Please do not store booleans - convert to 0/1 integer values!
119          *
120          * @param string $uid
121          *  The user_id
122          * @param string $family
123          *  The category of the configuration value
124          * @param string $key
125          *  The configuration key to set
126          * @param string $value
127          *  The value to store
128          * @return mixed Stored $value or false
129          */
130         public static function set($uid, $family, $key, $value) {
131
132                 $a = get_app();
133
134                 // We store our setting values in a string variable.
135                 // So we have to do the conversion here so that the compare below works.
136                 // The exception are array values.
137                 $dbvalue = (!is_array($value) ? (string)$value : $value);
138
139                 $stored = self::get($uid, $family, $key, null, true);
140
141                 if (($stored === $dbvalue) AND self::$in_db[$uid][$family][$key]) {
142                         return true;
143                 }
144
145                 $a->config[$uid][$family][$key] = $dbvalue;
146
147                 // manage array value
148                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
149
150                 if (is_null($stored) OR !self::$in_db[$uid][$family][$key]) {
151                         $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
152                                 intval($uid),
153                                 dbesc($family),
154                                 dbesc($key),
155                                 dbesc($dbvalue),
156                                 dbesc($dbvalue)
157                         );
158                 } else {
159                         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
160                                 dbesc($dbvalue),
161                                 intval($uid),
162                                 dbesc($family),
163                                 dbesc($key)
164                         );
165                 }
166
167                 if ($ret) {
168                         self::$in_db[$uid][$family][$key] = true;
169                         return $value;
170                 }
171                 return $ret;
172         }
173
174         /**
175          * @brief Deletes the given key from the users's configuration.
176          *
177          * Removes the configured value from the stored cache in $a->config[$uid]
178          * and removes it from the database.
179          *
180          * @param string $uid The user_id
181          * @param string $family
182          *  The category of the configuration value
183          * @param string $key
184          *  The configuration key to delete
185          * @return mixed
186          */
187         public static function delete($uid,$family,$key) {
188
189                 $a = get_app();
190
191                 if (x($a->config[$uid][$family], $key)) {
192                         unset($a->config[$uid][$family][$key]);
193                         unset(self::$in_db[$uid][$family][$key]);
194                 }
195
196                 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
197                         intval($uid),
198                         dbesc($family),
199                         dbesc($key)
200                 );
201
202                 return $ret;
203         }
204 }