]> git.mxchange.org Git - friendica.git/blob - include/Core/Config.php
Split poco discovery in smaller function calls
[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                 // We store our setting values in a string variable.
143                 // So we have to do the conversion here so that the compare below works.
144                 // The exception are array values.
145                 $dbvalue = (!is_array($value) ? (string)$value : $value);
146
147                 $stored = self::get($family, $key);
148
149                 if ($stored === $dbvalue) {
150                         return true;
151                 }
152
153                 if ($family === 'config') {
154                         $a->config[$key] = $dbvalue;
155                 } elseif ($family != 'system') {
156                         $a->config[$family][$key] = $dbvalue;
157                 }
158
159                 // Assign the just added value to the cache
160                 self::$cache[$family][$key] = $dbvalue;
161
162                 // manage array value
163                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
164
165                 if (is_null($stored)) {
166                         $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
167                                 dbesc($family),
168                                 dbesc($key),
169                                 dbesc($dbvalue),
170                                 dbesc($dbvalue)
171                         );
172                 } else {
173                         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
174                                 dbesc($dbvalue),
175                                 dbesc($family),
176                                 dbesc($key)
177                         );
178                 }
179                 if ($ret) {
180                         return $value;
181                 }
182                 return $ret;
183         }
184
185         /**
186          * @brief Deletes the given key from the system configuration.
187          *
188          * Removes the configured value from the stored cache in $a->config
189          * and removes it from the database.
190          *
191          * @param string $family
192          *  The category of the configuration value
193          * @param string $key
194          *  The configuration key to delete
195          * @return mixed
196          */
197         public static function delete($family, $key) {
198
199                 if (isset(self::$cache[$family][$key])) {
200                         unset(self::$cache[$family][$key]);
201                 }
202                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
203                         dbesc($family),
204                         dbesc($key)
205                 );
206
207                 return $ret;
208         }
209 }