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.
 
 
 
 
 
 

206 lines
5.3 KiB

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