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