]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Bugfix: Calls to a renamed function had been changed
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * @file src/Core/PConfig.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Database\DBM;
8 use dba;
9
10 /**
11  * @file include/Core/PConfig.php
12  * @brief contains the class with methods for the management
13  * of the user configuration
14  */
15
16 /**
17  * @brief Management of user configuration storage
18  * Note:
19  * Please do not store booleans - convert to 0/1 integer values
20  * The PConfig::get() functions return boolean false for keys that are unset,
21  * and this could lead to subtle bugs.
22  */
23 class PConfig
24 {
25         private static $in_db;
26
27         /**
28          * @brief Loads all configuration values of a user's config family into a cached storage.
29          *
30          * All configuration values of the given user are stored in global cache
31          * which is available under the global variable $a->config[$uid].
32          *
33          * @param string $uid    The user_id
34          * @param string $family The category of the configuration value
35          *
36          * @return void
37          */
38         public static function load($uid, $family)
39         {
40                 $a = get_app();
41
42                 $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
43                 if (DBM::is_result($r)) {
44                         while ($rr = dba::fetch($r)) {
45                                 $k = $rr['k'];
46                                 $a->config[$uid][$family][$k] = $rr['v'];
47                                 self::$in_db[$uid][$family][$k] = true;
48                         }
49                 } else if ($family != 'config') {
50                         // Negative caching
51                         $a->config[$uid][$family] = "!<unset>!";
52                 }
53                 dba::close($r);
54         }
55
56         /**
57          * @brief Get a particular user's config variable given the category name
58          * ($family) and a key.
59          *
60          * Get a particular user's config value from the given category ($family)
61          * and the $key from a cached storage in $a->config[$uid].
62          *
63          * @param string  $uid           The user_id
64          * @param string  $family        The category of the configuration value
65          * @param string  $key           The configuration key to query
66          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
67          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
68          *
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    The user_id
115          * @param string $family The category of the configuration value
116          * @param string $key    The configuration key to set
117          * @param string $value  The value to store
118          *
119          * @return mixed Stored $value or false
120          */
121         public static function set($uid, $family, $key, $value)
122         {
123                 $a = get_app();
124
125                 // We store our setting values in a string variable.
126                 // So we have to do the conversion here so that the compare below works.
127                 // The exception are array values.
128                 $dbvalue = (!is_array($value) ? (string)$value : $value);
129
130                 $stored = self::get($uid, $family, $key, null, true);
131
132                 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
133                         return true;
134                 }
135
136                 $a->config[$uid][$family][$key] = $dbvalue;
137
138                 // manage array value
139                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
140
141                 dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
142
143                 if ($ret) {
144                         self::$in_db[$uid][$family][$key] = true;
145                         return $value;
146                 }
147                 return $ret;
148         }
149
150         /**
151          * @brief Deletes the given key from the users's configuration.
152          *
153          * Removes the configured value from the stored cache in $a->config[$uid]
154          * and removes it from the database.
155          *
156          * @param string $uid    The user_id
157          * @param string $family The category of the configuration value
158          * @param string $key    The configuration key to delete
159          *
160          * @return mixed
161          */
162         public static function delete($uid, $family, $key)
163         {
164                 $a = get_app();
165
166                 if (x($a->config[$uid][$family], $key)) {
167                         unset($a->config[$uid][$family][$key]);
168                         unset(self::$in_db[$uid][$family][$key]);
169                 }
170
171                 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
172
173                 return $ret;
174         }
175 }