]> git.mxchange.org Git - friendica.git/blob - include/Core/Config.php
Merge from develop
[friendica.git] / include / Core / Config.php
1 <?php
2 namespace Friendica\Core;
3 /**
4  * @file include/Core/Config.php
5  *
6  *  @brief Contains the class with methods for system configuration
7  */
8
9
10 /**
11  * @brief Arbitrary sytem configuration storage
12  * Note:
13  * Please do not store booleans - convert to 0/1 integer values
14  * The Config::get() functions return boolean false for keys that are unset,
15  * and this could lead to subtle bugs.
16  *
17  * There are a few places in the code (such as the admin panel) where boolean
18  * configurations need to be fixed as of 10/08/2011.
19  */
20 class Config {
21
22         /**
23          * @brief Loads all configuration values of family into a cached storage.
24          *
25          * All configuration values of the system are stored in global cache
26          * which is available under the global variable $a->config
27          *
28          * @param string $family
29          *  The category of the configuration value
30          * @return void
31          */
32         public static function load($family) {
33                 global $a;
34
35                 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
36                 if(count($r)) {
37                         foreach($r as $rr) {
38                                 $k = $rr['k'];
39                                 if ($family === 'config') {
40                                         $a->config[$k] = $rr['v'];
41                                 } else {
42                                         $a->config[$family][$k] = $rr['v'];
43                                 }
44                         }
45                 } else if ($family != 'config') {
46                         // Negative caching
47                         $a->config[$family] = "!<unset>!";
48                 }
49         }
50
51         /**
52          * @brief Get a particular user's config variable given the category name
53          * ($family) and a key.
54          *
55          * Get a particular config value from the given category ($family)
56          * and the $key from a cached storage in $a->config[$uid].
57          * $instore is only used by the set_config function
58          * to determine if the key already exists in the DB
59          * If a key is found in the DB but doesn't exist in
60          * local config cache, pull it into the cache so we don't have
61          * to hit the DB again for this item.
62          *
63          * @param string $family
64          *  The category of the configuration value
65          * @param string $key
66          *  The configuration key to query
67          * @param mixed $default_value optional
68          *  The value to return if key is not set (default: null)
69          * @param boolean $refresh optional
70          *  If true the config is loaded from the db and not from the cache (default: false)
71          * @return mixed Stored value or null if it does not exist
72          */
73         public static function get($family, $key, $default_value=null, $refresh = false) {
74
75                 global $a;
76
77                 if(! $refresh) {
78                         // Looking if the whole family isn't set
79                         if(isset($a->config[$family])) {
80                                 if($a->config[$family] === '!<unset>!') {
81                                         return $default_value;
82                                 }
83                         }
84
85                         if(isset($a->config[$family][$key])) {
86                                 if($a->config[$family][$key] === '!<unset>!') {
87                                         return $default_value;
88                                 }
89                                 return $a->config[$family][$key];
90                         }
91                 }
92
93                 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
94                         dbesc($family),
95                         dbesc($key)
96                 );
97                 if(count($ret)) {
98                         // manage array value
99                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
100                         $a->config[$family][$key] = $val;
101
102                         return $val;
103                 }
104                 else {
105                         $a->config[$family][$key] = '!<unset>!';
106                 }
107                 return $default_value;
108         }
109
110         /**
111          * @brief Sets a configuration value for system config
112          *
113          * Stores a config value ($value) in the category ($family) under the key ($key)
114          * for the user_id $uid.
115          *
116          * Note: Please do not store booleans - convert to 0/1 integer values!
117          *
118          * @param string $family
119          *  The category of the configuration value
120          * @param string $key
121          *  The configuration key to set
122          * @param string $value
123          *  The value to store
124          * @return mixed Stored $value or false if the database update failed
125          */
126         public static function set($family,$key,$value) {
127                 global $a;
128
129                 $a->config[$family][$key] = $value;
130
131                 // manage array value
132                 $dbvalue = (is_array($value)?serialize($value):$value);
133                 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
134
135                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' )
136 ON DUPLICATE KEY UPDATE `v` = '%s'",
137                         dbesc($family),
138                         dbesc($key),
139                         dbesc($dbvalue),
140                         dbesc($dbvalue)
141                 );
142                 if($ret)
143                         return $value;
144                 return $ret;
145         }
146
147         /**
148          * @brief Deletes the given key from the system configuration.
149          *
150          * Removes the configured value from the stored cache in $a->config
151          * and removes it from the database.
152          *
153          * @param string $family
154          *  The category of the configuration value
155          * @param string $key
156          *  The configuration key to delete
157          * @return mixed
158          */
159         public static function delete($family,$key) {
160
161                 global $a;
162                 if(x($a->config[$family],$key))
163                         unset($a->config[$family][$key]);
164                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
165                         dbesc($family),
166                         dbesc($key)
167                 );
168
169                 return $ret;
170         }
171
172 }