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