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.

187 lines
4.9KB

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