Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

342 lines
8.9 KiB

  1. <?php
  2. /**
  3. *
  4. * Arbitrary configuration storage
  5. * Note:
  6. * Please do not store booleans - convert to 0/1 integer values
  7. * The get_?config() functions return boolean false for keys that are unset,
  8. * and this could lead to subtle bugs.
  9. *
  10. * There are a few places in the code (such as the admin panel) where boolean
  11. * configurations need to be fixed as of 10/08/2011.
  12. */
  13. // retrieve a "family" of config variables from database to cached storage
  14. if(! function_exists('load_config')) {
  15. function load_config($family) {
  16. global $a;
  17. $r = q("SELECT * FROM `config` WHERE `cat` = '%s'", dbesc($family));
  18. if(count($r)) {
  19. foreach($r as $rr) {
  20. $k = $rr['k'];
  21. if ($family === 'config') {
  22. $a->config[$k] = $rr['v'];
  23. } else {
  24. $a->config[$family][$k] = $rr['v'];
  25. }
  26. }
  27. } else if ($family != 'config') {
  28. // Negative caching
  29. $a->config[$family] = "!<unset>!";
  30. }
  31. }}
  32. // get a particular config variable given the family name
  33. // and key. Returns false if not set.
  34. // $instore is only used by the set_config function
  35. // to determine if the key already exists in the DB
  36. // If a key is found in the DB but doesn't exist in
  37. // local config cache, pull it into the cache so we don't have
  38. // to hit the DB again for this item.
  39. if(! function_exists('get_config')) {
  40. function get_config($family, $key, $instore = false) {
  41. global $a;
  42. if(! $instore) {
  43. // Looking if the whole family isn't set
  44. if(isset($a->config[$family])) {
  45. if($a->config[$family] === '!<unset>!') {
  46. return false;
  47. }
  48. }
  49. if(isset($a->config[$family][$key])) {
  50. if($a->config[$family][$key] === '!<unset>!') {
  51. return false;
  52. }
  53. return $a->config[$family][$key];
  54. }
  55. }
  56. // If APC is enabled then fetch the data from there, else try XCache
  57. /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
  58. if (apc_exists($family."|".$key)) {
  59. $val = apc_fetch($family."|".$key);
  60. $a->config[$family][$key] = $val;
  61. if ($val === '!<unset>!')
  62. return false;
  63. else
  64. return $val;
  65. }
  66. elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
  67. if (xcache_isset($family."|".$key)) {
  68. $val = xcache_fetch($family."|".$key);
  69. $a->config[$family][$key] = $val;
  70. if ($val === '!<unset>!')
  71. return false;
  72. else
  73. return $val;
  74. }
  75. */
  76. $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  77. dbesc($family),
  78. dbesc($key)
  79. );
  80. if(count($ret)) {
  81. // manage array value
  82. $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
  83. $a->config[$family][$key] = $val;
  84. // If APC is enabled then store the data there, else try XCache
  85. /*if (function_exists("apc_store"))
  86. apc_store($family."|".$key, $val, 600);
  87. elseif (function_exists("xcache_set"))
  88. xcache_set($family."|".$key, $val, 600);*/
  89. return $val;
  90. }
  91. else {
  92. $a->config[$family][$key] = '!<unset>!';
  93. // If APC is enabled then store the data there, else try XCache
  94. /*if (function_exists("apc_store"))
  95. apc_store($family."|".$key, '!<unset>!', 600);
  96. elseif (function_exists("xcache_set"))
  97. xcache_set($family."|".$key, '!<unset>!', 600);*/
  98. }
  99. return false;
  100. }}
  101. // Store a config value ($value) in the category ($family)
  102. // under the key ($key)
  103. // Return the value, or false if the database update failed
  104. if(! function_exists('set_config')) {
  105. function set_config($family,$key,$value) {
  106. global $a;
  107. // If $a->config[$family] has been previously set to '!<unset>!', then
  108. // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
  109. // $a->config[$family][$key] = $value will be equivalent to
  110. // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
  111. // so unset the family before assigning a value to a family's key
  112. if($a->config[$family] === '!<unset>!')
  113. unset($a->config[$family]);
  114. // manage array value
  115. $dbvalue = (is_array($value)?serialize($value):$value);
  116. $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
  117. if(get_config($family,$key,true) === false) {
  118. $a->config[$family][$key] = $value;
  119. $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
  120. dbesc($family),
  121. dbesc($key),
  122. dbesc($dbvalue)
  123. );
  124. if($ret)
  125. return $value;
  126. return $ret;
  127. }
  128. $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
  129. dbesc($dbvalue),
  130. dbesc($family),
  131. dbesc($key)
  132. );
  133. $a->config[$family][$key] = $value;
  134. // If APC is enabled then store the data there, else try XCache
  135. /*if (function_exists("apc_store"))
  136. apc_store($family."|".$key, $value, 600);
  137. elseif (function_exists("xcache_set"))
  138. xcache_set($family."|".$key, $value, 600);*/
  139. if($ret)
  140. return $value;
  141. return $ret;
  142. }}
  143. if(! function_exists('load_pconfig')) {
  144. function load_pconfig($uid,$family) {
  145. global $a;
  146. $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
  147. dbesc($family),
  148. intval($uid)
  149. );
  150. if(count($r)) {
  151. foreach($r as $rr) {
  152. $k = $rr['k'];
  153. $a->config[$uid][$family][$k] = $rr['v'];
  154. }
  155. } else if ($family != 'config') {
  156. // Negative caching
  157. $a->config[$uid][$family] = "!<unset>!";
  158. }
  159. }}
  160. if(! function_exists('get_pconfig')) {
  161. function get_pconfig($uid,$family, $key, $instore = false) {
  162. global $a;
  163. if(! $instore) {
  164. // Looking if the whole family isn't set
  165. if(isset($a->config[$uid][$family])) {
  166. if($a->config[$uid][$family] === '!<unset>!') {
  167. return false;
  168. }
  169. }
  170. if(isset($a->config[$uid][$family][$key])) {
  171. if($a->config[$uid][$family][$key] === '!<unset>!') {
  172. return false;
  173. }
  174. return $a->config[$uid][$family][$key];
  175. }
  176. }
  177. // If APC is enabled then fetch the data from there, else try XCache
  178. /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
  179. if (apc_exists($uid."|".$family."|".$key)) {
  180. $val = apc_fetch($uid."|".$family."|".$key);
  181. $a->config[$uid][$family][$key] = $val;
  182. if ($val === '!<unset>!')
  183. return false;
  184. else
  185. return $val;
  186. }
  187. elseif (function_exists("xcache_get") AND function_exists("xcache_isset"))
  188. if (xcache_isset($uid."|".$family."|".$key)) {
  189. $val = xcache_get($uid."|".$family."|".$key);
  190. $a->config[$uid][$family][$key] = $val;
  191. if ($val === '!<unset>!')
  192. return false;
  193. else
  194. return $val;
  195. }*/
  196. $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  197. intval($uid),
  198. dbesc($family),
  199. dbesc($key)
  200. );
  201. if(count($ret)) {
  202. $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
  203. $a->config[$uid][$family][$key] = $val;
  204. // If APC is enabled then store the data there, else try XCache
  205. /*if (function_exists("apc_store"))
  206. apc_store($uid."|".$family."|".$key, $val, 600);
  207. elseif (function_exists("xcache_set"))
  208. xcache_set($uid."|".$family."|".$key, $val, 600);*/
  209. return $val;
  210. }
  211. else {
  212. $a->config[$uid][$family][$key] = '!<unset>!';
  213. // If APC is enabled then store the data there, else try XCache
  214. /*if (function_exists("apc_store"))
  215. apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
  216. elseif (function_exists("xcache_set"))
  217. xcache_set($uid."|".$family."|".$key, '!<unset>!', 600);*/
  218. }
  219. return false;
  220. }}
  221. if(! function_exists('del_config')) {
  222. function del_config($family,$key) {
  223. global $a;
  224. if(x($a->config[$family],$key))
  225. unset($a->config[$family][$key]);
  226. $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
  227. dbesc($family),
  228. dbesc($key)
  229. );
  230. // If APC is enabled then delete the data from there, else try XCache
  231. /*if (function_exists("apc_delete"))
  232. apc_delete($family."|".$key);
  233. elseif (function_exists("xcache_unset"))
  234. xcache_unset($family."|".$key);*/
  235. return $ret;
  236. }}
  237. // Same as above functions except these are for personal config storage and take an
  238. // additional $uid argument.
  239. if(! function_exists('set_pconfig')) {
  240. function set_pconfig($uid,$family,$key,$value) {
  241. global $a;
  242. // manage array value
  243. $dbvalue = (is_array($value)?serialize($value):$value);
  244. if(get_pconfig($uid,$family,$key,true) === false) {
  245. $a->config[$uid][$family][$key] = $value;
  246. $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
  247. intval($uid),
  248. dbesc($family),
  249. dbesc($key),
  250. dbesc($dbvalue)
  251. );
  252. if($ret)
  253. return $value;
  254. return $ret;
  255. }
  256. $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
  257. dbesc($dbvalue),
  258. intval($uid),
  259. dbesc($family),
  260. dbesc($key)
  261. );
  262. $a->config[$uid][$family][$key] = $value;
  263. // If APC is enabled then store the data there, else try XCache
  264. /*if (function_exists("apc_store"))
  265. apc_store($uid."|".$family."|".$key, $value, 600);
  266. elseif (function_exists("xcache_set"))
  267. xcache_set($uid."|".$family."|".$key, $value, 600);*/
  268. if($ret)
  269. return $value;
  270. return $ret;
  271. }}
  272. if(! function_exists('del_pconfig')) {
  273. function del_pconfig($uid,$family,$key) {
  274. global $a;
  275. if(x($a->config[$uid][$family],$key))
  276. unset($a->config[$uid][$family][$key]);
  277. $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
  278. intval($uid),
  279. dbesc($family),
  280. dbesc($key)
  281. );
  282. return $ret;
  283. }}