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