]> git.mxchange.org Git - friendica.git/blob - include/Core/Config.php
79424c9a191529be9de31ffddac383127385681d
[friendica.git] / include / Core / Config.php
1 <?php
2 namespace Friendica\Core;
3 /**
4  * @file include/Core/Config.php
5  *
6  *  @brief Contains the class with methods for system configuration
7  */
8
9
10 /**
11  * @brief Arbitrary sytem configuration storage
12  * Note:
13  * Please do not store booleans - convert to 0/1 integer values
14  * The Config::get() functions return boolean false for keys that are unset,
15  * and this could lead to subtle bugs.
16  *
17  * There are a few places in the code (such as the admin panel) where boolean
18  * configurations need to be fixed as of 10/08/2011.
19  */
20 class Config {
21
22         /**
23          * @brief Loads all configuration values of family into a cached storage.
24          *
25          * All configuration values of the system are stored in global cache
26          * which is available under the global variable $a->config
27          *
28          * @param string $family
29          *  The category of the configuration value
30          * @return void
31          */
32         public static function load($family) {
33                 $a = get_app();
34
35                 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family));
36                 if (dbm::is_result($r)) {
37                         foreach ($r as $rr) {
38                                 $k = $rr['k'];
39                                 if ($family === 'config') {
40                                         $a->config[$k] = $rr['v'];
41                                 } else {
42                                         $a->config[$family][$k] = $rr['v'];
43                                 }
44                         }
45                 } else if ($family != 'config') {
46                         // Negative caching
47                         $a->config[$family] = "!<unset>!";
48                 }
49         }
50
51         /**
52          * @brief Get a particular user's config variable given the category name
53          * ($family) and a key.
54          *
55          * Get a particular config value from the given category ($family)
56          * and the $key from a cached storage in $a->config[$uid].
57          * $instore is only used by the set_config function
58          * to determine if the key already exists in the DB
59          * If a key is found in the DB but doesn't exist in
60          * local config cache, pull it into the cache so we don't have
61          * to hit the DB again for this item.
62          *
63          * @param string $family
64          *  The category of the configuration value
65          * @param string $key
66          *  The configuration key to query
67          * @param mixed $default_value optional
68          *  The value to return if key is not set (default: null)
69          * @param boolean $refresh optional
70          *  If true the config is loaded from the db and not from the cache (default: false)
71          * @return mixed Stored value or null if it does not exist
72          */
73         public static function get($family, $key, $default_value = null, $refresh = false) {
74
75                 $a = get_app();
76
77                 if (!$refresh) {
78                         // Looking if the whole family isn't set
79                         if (isset($a->config[$family])) {
80                                 if ($a->config[$family] === '!<unset>!') {
81                                         return $default_value;
82                                 }
83                         }
84
85                         if (isset($a->config[$family][$key])) {
86                                 if ($a->config[$family][$key] === '!<unset>!') {
87                                         return $default_value;
88                                 }
89                                 return $a->config[$family][$key];
90                         }
91                 }
92
93                 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
94                         dbesc($family),
95                         dbesc($key)
96                 );
97                 if (count($ret)) {
98                         // manage array value
99                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
100                         $a->config[$family][$key] = $val;
101
102                         return $val;
103                 } else {
104                         $a->config[$family][$key] = '!<unset>!';
105                 }
106                 return $default_value;
107         }
108
109         /**
110          * @brief Sets a configuration value for system config
111          *
112          * Stores a config value ($value) in the category ($family) under the key ($key)
113          * for the user_id $uid.
114          *
115          * Note: Please do not store booleans - convert to 0/1 integer values!
116          *
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 if the database update failed
124          */
125         public static function set($family, $key, $value) {
126                 $a = get_app();
127
128                 $stored = self::get($family, $key);
129
130                 if ($stored == $value) {
131                         return true;
132                 }
133
134                 $a->config[$family][$key] = $value;
135
136                 // manage array value
137                 $dbvalue = (is_array($value) ? serialize($value) : $value);
138                 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
139
140                 if (is_null($stored)) {
141                         $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
142                                 dbesc($family),
143                                 dbesc($key),
144                                 dbesc($dbvalue),
145                                 dbesc($dbvalue)
146                         );
147                 } else {
148                         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
149                                 dbesc($dbvalue),
150                                 dbesc($family),
151                                 dbesc($key)
152                         );
153                 }
154                 if ($ret) {
155                         return $value;
156                 }
157                 return $ret;
158         }
159
160         /**
161          * @brief Deletes the given key from the system configuration.
162          *
163          * Removes the configured value from the stored cache in $a->config
164          * and removes it from the database.
165          *
166          * @param string $family
167          *  The category of the configuration value
168          * @param string $key
169          *  The configuration key to delete
170          * @return mixed
171          */
172         public static function delete($family, $key) {
173
174                 $a = get_app();
175                 if (x($a->config[$family],$key)) {
176                         unset($a->config[$family][$key]);
177                 }
178                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
179                         dbesc($family),
180                         dbesc($key)
181                 );
182
183                 return $ret;
184         }
185
186 }