]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge remote branch 'upstream/master'
[friendica.git] / include / config.php
1 <?php
2
3 /**
4  *
5  * Arbitrary configuration storage
6  * Note:
7  * Please do not store booleans - convert to 0/1 integer values
8  * The get_?config() functions return boolean false for keys that are unset,
9  * and this could lead to subtle bugs.  
10  *
11  * There are a few places in the code (such as the admin panel) where boolean
12  * configurations need to be fixed as of 10/08/2011.
13  */
14
15
16 // retrieve a "family" of config variables from database to cached storage
17
18 if(! function_exists('load_config')) {
19 function load_config($family) {
20         global $a;
21         $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
22                 dbesc($family)
23         );
24         if(count($r)) {
25                 foreach($r as $rr) {
26                         $k = $rr['k'];
27                         if ($rr['cat'] === 'config') {
28                                 $a->config[$k] = $rr['v'];
29                         } else {
30                                 $a->config[$family][$k] = $rr['v'];
31                         }
32                 }
33         }
34 }}
35
36 // get a particular config variable given the family name
37 // and key. Returns false if not set.
38 // $instore is only used by the set_config function
39 // to determine if the key already exists in the DB
40 // If a key is found in the DB but doesn't exist in
41 // local config cache, pull it into the cache so we don't have
42 // to hit the DB again for this item.
43
44 if(! function_exists('get_config')) {
45 function get_config($family, $key, $instore = false) {
46
47         global $a;
48
49         if(! $instore) {
50                 if(isset($a->config[$family][$key])) {
51                         if($a->config[$family][$key] === '!<unset>!') {
52                                 return false;
53                         }
54                         return $a->config[$family][$key];
55                 }
56         }
57         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
58                 dbesc($family),
59                 dbesc($key)
60         );
61         if(count($ret)) {
62                 // manage array value
63                 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
64                 $a->config[$family][$key] = $val;
65                 return $val;
66         }
67         else {
68                 $a->config[$family][$key] = '!<unset>!';
69         }
70         return false;
71 }}
72
73 // Store a config value ($value) in the category ($family)
74 // under the key ($key)
75 // Return the value, or false if the database update failed
76
77 if(! function_exists('set_config')) {
78 function set_config($family,$key,$value) {
79         global $a;
80         // manage array value
81         $dbvalue = (is_array($value)?serialize($value):$value);
82         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
83         if(get_config($family,$key,true) === false) {
84                 $a->config[$family][$key] = $value;
85                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
86                         dbesc($family),
87                         dbesc($key),
88                         dbesc($dbvalue)
89                 );
90                 if($ret) 
91                         return $value;
92                 return $ret;
93         }
94         
95         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
96                 dbesc($dbvalue),
97                 dbesc($family),
98                 dbesc($key)
99         );
100
101         $a->config[$family][$key] = $value;
102
103         if($ret)
104                 return $value;
105         return $ret;
106 }}
107
108
109 if(! function_exists('load_pconfig')) {
110 function load_pconfig($uid,$family) {
111         global $a;
112         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
113                 dbesc($family),
114                 intval($uid)
115         );
116         if(count($r)) {
117                 foreach($r as $rr) {
118                         $k = $rr['k'];
119                         $a->config[$uid][$family][$k] = $rr['v'];
120                 }
121         }
122 }}
123
124
125
126 if(! function_exists('get_pconfig')) {
127 function get_pconfig($uid,$family, $key, $instore = false) {
128
129         global $a;
130
131         if(! $instore) {
132                 if(isset($a->config[$uid][$family][$key])) {
133                         if($a->config[$uid][$family][$key] === '!<unset>!') {
134                                 return false;
135                         }
136                         return $a->config[$uid][$family][$key];
137                 }
138         }
139
140         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
141                 intval($uid),
142                 dbesc($family),
143                 dbesc($key)
144         );
145
146         if(count($ret)) {
147                 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
148                 $a->config[$uid][$family][$key] = $val;
149                 return $val;
150         }
151         else {
152                 $a->config[$uid][$family][$key] = '!<unset>!';
153         }
154         return false;
155 }}
156
157 if(! function_exists('del_config')) {
158 function del_config($family,$key) {
159
160         global $a;
161         if(x($a->config[$family],$key))
162                 unset($a->config[$family][$key]);
163         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
164                 dbesc($family),
165                 dbesc($key)
166         );
167         return $ret;
168 }}
169
170
171
172 // Same as above functions except these are for personal config storage and take an
173 // additional $uid argument.
174
175 if(! function_exists('set_pconfig')) {
176 function set_pconfig($uid,$family,$key,$value) {
177
178         global $a;
179
180         // manage array value
181         $dbvalue = (is_array($value)?serialize($value):$value);
182
183         if(get_pconfig($uid,$family,$key,true) === false) {
184                 $a->config[$uid][$family][$key] = $value;
185                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
186                         intval($uid),
187                         dbesc($family),
188                         dbesc($key),
189                         dbesc($dbvalue)
190                 );
191                 if($ret) 
192                         return $value;
193                 return $ret;
194         }
195         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
196                 dbesc($dbvalue),
197                 intval($uid),
198                 dbesc($family),
199                 dbesc($key)
200         );
201
202         $a->config[$uid][$family][$key] = $value;
203
204         if($ret)
205                 return $value;
206         return $ret;
207 }}
208
209 if(! function_exists('del_pconfig')) {
210 function del_pconfig($uid,$family,$key) {
211
212         global $a;
213         if(x($a->config[$uid][$family],$key))
214                 unset($a->config[$uid][$family][$key]);
215         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
216                 intval($uid),
217                 dbesc($family),
218                 dbesc($key)
219         );
220         return $ret;
221 }}