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