]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge branch 'master' of github.com:fabrixxm/friendica
[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         
81         // manage array value
82         $dbvalue = (is_array($value)?serialize($value):$value);
83
84         if(get_config($family,$key,true) === false) {
85                 $a->config[$family][$key] = $value;
86                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
87                         dbesc($family),
88                         dbesc($key),
89                         dbesc($dbvalue)
90                 );
91                 if($ret) 
92                         return $value;
93                 return $ret;
94         }
95         
96         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
97                 dbesc($dbvalue),
98                 dbesc($family),
99                 dbesc($key)
100         );
101
102         $a->config[$family][$key] = $value;
103
104         if($ret)
105                 return $value;
106         return $ret;
107 }}
108
109
110 if(! function_exists('load_pconfig')) {
111 function load_pconfig($uid,$family) {
112         global $a;
113         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
114                 dbesc($family),
115                 intval($uid)
116         );
117         if(count($r)) {
118                 foreach($r as $rr) {
119                         $k = $rr['k'];
120                         $a->config[$uid][$family][$k] = $rr['v'];
121                 }
122         }
123 }}
124
125
126
127 if(! function_exists('get_pconfig')) {
128 function get_pconfig($uid,$family, $key, $instore = false) {
129
130         global $a;
131
132         if(! $instore) {
133                 if(isset($a->config[$uid][$family][$key])) {
134                         if($a->config[$uid][$family][$key] === '!<unset>!') {
135                                 return false;
136                         }
137                         return $a->config[$uid][$family][$key];
138                 }
139         }
140
141         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
142                 intval($uid),
143                 dbesc($family),
144                 dbesc($key)
145         );
146
147         if(count($ret)) {
148                 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
149                 $a->config[$uid][$family][$key] = $val;
150                 return $val;
151         }
152         else {
153                 $a->config[$uid][$family][$key] = '!<unset>!';
154         }
155         return false;
156 }}
157
158 if(! function_exists('del_config')) {
159 function del_config($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' LIMIT 1",
165                 dbesc($cat),
166                 dbesc($key)
167         );
168         return $ret;
169 }}
170
171
172
173 // Same as above functions except these are for personal config storage and take an
174 // additional $uid argument.
175
176 if(! function_exists('set_pconfig')) {
177 function set_pconfig($uid,$family,$key,$value) {
178
179         global $a;
180
181         // manage array value
182         $dbvalue = (is_array($value)?serialize($value):$value);
183
184         if(get_pconfig($uid,$family,$key,true) === false) {
185                 $a->config[$uid][$family][$key] = $value;
186                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
187                         intval($uid),
188                         dbesc($family),
189                         dbesc($key),
190                         dbesc($dbvalue)
191                 );
192                 if($ret) 
193                         return $value;
194                 return $ret;
195         }
196         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
197                 dbesc($dbvalue),
198                 intval($uid),
199                 dbesc($family),
200                 dbesc($key)
201         );
202
203         $a->config[$uid][$family][$key] = $value;
204
205         if($ret)
206                 return $value;
207         return $ret;
208 }}
209
210 if(! function_exists('del_pconfig')) {
211 function del_pconfig($uid,$family,$key) {
212
213         global $a;
214         if(x($a->config[$uid][$family],$key))
215                 unset($a->config[$uid][$family][$key]);
216         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
217                 intval($uid),
218                 dbesc($family),
219                 dbesc($key)
220         );
221         return $ret;
222 }}