]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
f3c5dc13a1800c9e7b015c8c233519221968ca73
[friendica.git] / src / Core / Config.php
1 <?php
2 namespace Friendica\Core;
3
4 use dba;
5 use dbm;
6
7 /**
8  * @file include/Core/Config.php
9  *
10  *  @brief Contains the class with methods for system configuration
11  */
12
13
14 /**
15  * @brief Arbitrary sytem configuration storage
16  * Note:
17  * Please do not store booleans - convert to 0/1 integer values
18  * The Config::get() functions return boolean false for keys that are unset,
19  * and this could lead to subtle bugs.
20  *
21  * There are a few places in the code (such as the admin panel) where boolean
22  * configurations need to be fixed as of 10/08/2011.
23  */
24 class Config {
25
26         private static $cache;
27         private static $in_db;
28
29         /**
30          * @brief Loads all configuration values of family into a cached storage.
31          *
32          * All configuration values of the system are stored in global cache
33          * which is available under the global variable $a->config
34          *
35          * @param string $family
36          *  The category of the configuration value
37          * @return void
38          */
39         public static function load($family = "config") {
40
41                 // We don't preload "system" anymore.
42                 // This reduces the number of database reads a lot.
43                 if ($family === 'system') {
44                         return;
45                 }
46
47                 $a = get_app();
48
49                 $r = dba::select('config', array('v', 'k'), array('cat' => $family));
50                 while ($rr = dba::fetch($r)) {
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          * @brief Get a particular user's config variable given the category name
64          * ($family) and a key.
65          *
66          * Get a particular config value from the given category ($family)
67          * and the $key from a cached storage in $a->config[$uid].
68          * $instore is only used by the set_config function
69          * to determine if the key already exists in the DB
70          * If a key is found in the DB but doesn't exist in
71          * local config cache, pull it into the cache so we don't have
72          * to hit the DB again for this item.
73          *
74          * @param string $family
75          *  The category of the configuration value
76          * @param string $key
77          *  The configuration key to query
78          * @param mixed $default_value optional
79          *  The value to return if key is not set (default: null)
80          * @param boolean $refresh optional
81          *  If true the config is loaded from the db and not from the cache (default: false)
82          * @return mixed Stored value or null if it does not exist
83          */
84         public static function get($family, $key, $default_value = null, $refresh = false) {
85
86                 $a = get_app();
87
88                 if (!$refresh) {
89
90                         // Do we have the cached value? Then return it
91                         if (isset(self::$cache[$family][$key])) {
92                                 if (self::$cache[$family][$key] === '!<unset>!') {
93                                         return $default_value;
94                                 } else {
95                                         return self::$cache[$family][$key];
96                                 }
97                         }
98                 }
99
100                 $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), array('limit' => 1));
101                 if (dbm::is_result($ret)) {
102                         // manage array value
103                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
104
105                         // Assign the value from the database to the cache
106                         self::$cache[$family][$key] = $val;
107                         self::$in_db[$family][$key] = true;
108                         return $val;
109                 } elseif (isset($a->config[$family][$key])) {
110
111                         // Assign the value (mostly) from the .htconfig.php to the cache
112                         self::$cache[$family][$key] = $a->config[$family][$key];
113                         self::$in_db[$family][$key] = false;
114
115                         return $a->config[$family][$key];
116                 }
117
118                 self::$cache[$family][$key] = '!<unset>!';
119                 self::$in_db[$family][$key] = false;
120
121                 return $default_value;
122         }
123
124         /**
125          * @brief Sets a configuration value for system config
126          *
127          * Stores a config value ($value) in the category ($family) under the key ($key)
128          * for the user_id $uid.
129          *
130          * Note: Please do not store booleans - convert to 0/1 integer values!
131          *
132          * @param string $family
133          *  The category of the configuration value
134          * @param string $key
135          *  The configuration key to set
136          * @param string $value
137          *  The value to store
138          * @return mixed Stored $value or false if the database update failed
139          */
140         public static function set($family, $key, $value) {
141                 $a = get_app();
142
143                 // We store our setting values in a string variable.
144                 // So we have to do the conversion here so that the compare below works.
145                 // The exception are array values.
146                 $dbvalue = (!is_array($value) ? (string)$value : $value);
147
148                 $stored = self::get($family, $key, null, true);
149
150                 if (($stored === $dbvalue) && self::$in_db[$family][$key]) {
151                         return true;
152                 }
153
154                 if ($family === 'config') {
155                         $a->config[$key] = $dbvalue;
156                 } elseif ($family != 'system') {
157                         $a->config[$family][$key] = $dbvalue;
158                 }
159
160                 // Assign the just added value to the cache
161                 self::$cache[$family][$key] = $dbvalue;
162
163                 // manage array value
164                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
165
166                 dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true);
167
168                 if ($ret) {
169                         self::$in_db[$family][$key] = true;
170                         return $value;
171                 }
172                 return $ret;
173         }
174
175         /**
176          * @brief Deletes the given key from the system configuration.
177          *
178          * Removes the configured value from the stored cache in $a->config
179          * and removes it from the database.
180          *
181          * @param string $family
182          *  The category of the configuration value
183          * @param string $key
184          *  The configuration key to delete
185          * @return mixed
186          */
187         public static function delete($family, $key) {
188
189                 if (isset(self::$cache[$family][$key])) {
190                         unset(self::$cache[$family][$key]);
191                         unset(self::$in_db[$family][$key]);
192                 }
193
194                 $ret = dba::delete('config', array('cat' => $family, 'k' => $key));
195
196                 return $ret;
197         }
198 }