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