]> git.mxchange.org Git - friendica.git/blob - include/Core/Config.php
Merge remote-tracking branch 'upstream/develop' into 1701-index-again
[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         private static $cache;
26
27         /**
28          * @brief Loads all configuration values of family into a cached storage.
29          *
30          * All configuration values of the system are stored in global cache
31          * which is available under the global variable $a->config
32          *
33          * @param string $family
34          *  The category of the configuration value
35          * @return void
36          */
37         public static function load($family = "config") {
38
39                 // We don't preload "system" anymore.
40                 // This reduces the number of database reads a lot.
41                 if ($family === 'system') {
42                         return;
43                 }
44
45                 $a = get_app();
46
47                 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
48                 if (dbm::is_result($r)) {
49                         foreach ($r as $rr) {
50                                 $k = $rr['k'];
51                                 if ($family === 'config') {
52                                         $a->config[$k] = $rr['v'];
53                                 } else {
54                                         $a->config[$family][$k] = $rr['v'];
55                                         self::$cache[$family][$k] = $rr['v'];
56                                 }
57                         }
58                 }
59         }
60
61         /**
62          * @brief Get a particular user's config variable given the category name
63          * ($family) and a key.
64          *
65          * Get a particular config value from the given category ($family)
66          * and the $key from a cached storage in $a->config[$uid].
67          * $instore is only used by the set_config function
68          * to determine if the key already exists in the DB
69          * If a key is found in the DB but doesn't exist in
70          * local config cache, pull it into the cache so we don't have
71          * to hit the DB again for this item.
72          *
73          * @param string $family
74          *  The category of the configuration value
75          * @param string $key
76          *  The configuration key to query
77          * @param mixed $default_value optional
78          *  The value to return if key is not set (default: null)
79          * @param boolean $refresh optional
80          *  If true the config is loaded from the db and not from the cache (default: false)
81          * @return mixed Stored value or null if it does not exist
82          */
83         public static function get($family, $key, $default_value = null, $refresh = false) {
84
85                 $a = get_app();
86
87                 if (!$refresh) {
88
89                         // Do we have the cached value? Then return it
90                         if (isset(self::$cache[$family][$key])) {
91                                 if (self::$cache[$family][$key] === '!<unset>!') {
92                                         return $default_value;
93                                 } else {
94                                         return self::$cache[$family][$key];
95                                 }
96                         }
97                 }
98
99                 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
100                         dbesc($family),
101                         dbesc($key)
102                 );
103                 if (dbm::is_result($ret)) {
104                         // manage array value
105                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
106
107                         // Assign the value from the database to the cache
108                         self::$cache[$family][$key] = $val;
109                         return $val;
110                 } elseif (isset($a->config[$family][$key])) {
111
112                         // Assign the value (mostly) from the .htconfig.php to the cache
113                         self::$cache[$family][$key] = $a->config[$family][$key];
114
115                         return $a->config[$family][$key];
116                 }
117
118                 self::$cache[$family][$key] = '!<unset>!';
119
120                 return $default_value;
121         }
122
123         /**
124          * @brief Sets a configuration value for system config
125          *
126          * Stores a config value ($value) in the category ($family) under the key ($key)
127          * for the user_id $uid.
128          *
129          * Note: Please do not store booleans - convert to 0/1 integer values!
130          *
131          * @param string $family
132          *  The category of the configuration value
133          * @param string $key
134          *  The configuration key to set
135          * @param string $value
136          *  The value to store
137          * @return mixed Stored $value or false if the database update failed
138          */
139         public static function set($family, $key, $value) {
140                 $a = get_app();
141
142                 $stored = self::get($family, $key);
143
144                 if ($stored === $value) {
145                         return true;
146                 }
147
148                 if ($family === 'config') {
149                         $a->config[$key] = $value;
150                 } elseif ($family != 'system') {
151                         $a->config[$family][$key] = $value;
152                 }
153
154                 // Assign the just added value to the cache
155                 self::$cache[$family][$key] = $value;
156
157                 // manage array value
158                 $dbvalue = (is_array($value) ? serialize($value) : $value);
159                 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
160
161                 if (is_null($stored)) {
162                         $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
163                                 dbesc($family),
164                                 dbesc($key),
165                                 dbesc($dbvalue),
166                                 dbesc($dbvalue)
167                         );
168                 } else {
169                         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
170                                 dbesc($dbvalue),
171                                 dbesc($family),
172                                 dbesc($key)
173                         );
174                 }
175                 if ($ret) {
176                         return $value;
177                 }
178                 return $ret;
179         }
180
181         /**
182          * @brief Deletes the given key from the system configuration.
183          *
184          * Removes the configured value from the stored cache in $a->config
185          * and removes it from the database.
186          *
187          * @param string $family
188          *  The category of the configuration value
189          * @param string $key
190          *  The configuration key to delete
191          * @return mixed
192          */
193         public static function delete($family, $key) {
194
195                 if (isset(self::$cache[$family][$key])) {
196                         unset(self::$cache[$family][$key]);
197                 }
198                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
199                         dbesc($family),
200                         dbesc($key)
201                 );
202
203                 return $ret;
204         }
205 }