]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
219831ad989acd6f3d9f9a51e0f36fccb6c168db
[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  * 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 The category of the configuration value
36          *
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                 dba::close($r);
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        The category of the configuration value
76          * @param string  $key           The configuration key to query
77          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
78          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
79          *
80          * @return mixed Stored value or null if it does not exist
81          */
82         public static function get($family, $key, $default_value = null, $refresh = false)
83         {
84                 $a = get_app();
85
86                 if (!$refresh) {
87                         // Do we have the cached value? Then return it
88                         if (isset(self::$cache[$family][$key])) {
89                                 if (self::$cache[$family][$key] === '!<unset>!') {
90                                         return $default_value;
91                                 } else {
92                                         return self::$cache[$family][$key];
93                                 }
94                         }
95                 }
96
97                 $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), array('limit' => 1));
98                 if (DBM::is_result($ret)) {
99                         // manage array value
100                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
101
102                         // Assign the value from the database to the cache
103                         self::$cache[$family][$key] = $val;
104                         self::$in_db[$family][$key] = true;
105                         return $val;
106                 } elseif (isset($a->config[$family][$key])) {
107                         // Assign the value (mostly) from the .htconfig.php to the cache
108                         self::$cache[$family][$key] = $a->config[$family][$key];
109                         self::$in_db[$family][$key] = false;
110
111                         return $a->config[$family][$key];
112                 }
113
114                 self::$cache[$family][$key] = '!<unset>!';
115                 self::$in_db[$family][$key] = false;
116
117                 return $default_value;
118         }
119
120         /**
121          * @brief Sets a configuration value for system config
122          *
123          * Stores a config value ($value) in the category ($family) under the key ($key)
124          * for the user_id $uid.
125          *
126          * Note: Please do not store booleans - convert to 0/1 integer values!
127          *
128          * @param string $family The category of the configuration value
129          * @param string $key    The configuration key to set
130          * @param string $value  The value to store
131          *
132          * @return mixed Stored $value or false if the database update failed
133          */
134         public static function set($family, $key, $value)
135         {
136                 $a = get_app();
137
138                 // We store our setting values in a string variable.
139                 // So we have to do the conversion here so that the compare below works.
140                 // The exception are array values.
141                 $dbvalue = (!is_array($value) ? (string)$value : $value);
142
143                 $stored = self::get($family, $key, null, true);
144
145                 if (($stored === $dbvalue) && self::$in_db[$family][$key]) {
146                         return true;
147                 }
148
149                 if ($family === 'config') {
150                         $a->config[$key] = $dbvalue;
151                 } elseif ($family != 'system') {
152                         $a->config[$family][$key] = $dbvalue;
153                 }
154
155                 // Assign the just added value to the cache
156                 self::$cache[$family][$key] = $dbvalue;
157
158                 // manage array value
159                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
160
161                 dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true);
162
163                 if ($ret) {
164                         self::$in_db[$family][$key] = true;
165                         return $value;
166                 }
167                 return $ret;
168         }
169
170         /**
171          * @brief Deletes the given key from the system configuration.
172          *
173          * Removes the configured value from the stored cache in $a->config
174          * and removes it from the database.
175          *
176          * @param string $family The category of the configuration value
177          * @param string $key    The configuration key to delete
178          *
179          * @return mixed
180          */
181         public static function delete($family, $key)
182         {
183                 if (isset(self::$cache[$family][$key])) {
184                         unset(self::$cache[$family][$key]);
185                         unset(self::$in_db[$family][$key]);
186                 }
187
188                 $ret = dba::delete('config', array('cat' => $family, 'k' => $key));
189
190                 return $ret;
191         }
192 }