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.

154 lines
4.2KB

  1. <?php
  2. /**
  3. * System Configuration Class
  4. *
  5. * @file include/Core/Config.php
  6. *
  7. * @brief Contains the class with methods for system configuration
  8. */
  9. namespace Friendica\Core;
  10. use Friendica\BaseObject;
  11. use Friendica\Core\Config;
  12. require_once 'include/dba.php';
  13. /**
  14. * @brief Arbitrary system configuration storage
  15. *
  16. * Note:
  17. * If we ever would decide to return exactly the variable type as entered,
  18. * we will have fun with the additional features. :-)
  19. */
  20. class Config extends BaseObject
  21. {
  22. /**
  23. * @var Friendica\Core\Config\IConfigAdapter
  24. */
  25. private static $adapter = null;
  26. public static function init()
  27. {
  28. // Database isn't ready or populated yet
  29. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  30. return;
  31. }
  32. if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
  33. self::$adapter = new Config\PreloadConfigAdapter();
  34. } else {
  35. self::$adapter = new Config\JITConfigAdapter();
  36. }
  37. }
  38. /**
  39. * @brief Loads all configuration values of family into a cached storage.
  40. *
  41. * All configuration values of the system are stored in global cache
  42. * which is available under the global variable $a->config
  43. *
  44. * @param string $family The category of the configuration value
  45. *
  46. * @return void
  47. */
  48. public static function load($family = "config")
  49. {
  50. // Database isn't ready or populated yet
  51. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  52. return;
  53. }
  54. if (empty(self::$adapter)) {
  55. self::init();
  56. }
  57. self::$adapter->load($family);
  58. }
  59. /**
  60. * @brief Get a particular user's config variable given the category name
  61. * ($family) and a key.
  62. *
  63. * Get a particular config value from the given category ($family)
  64. * and the $key from a cached storage in $a->config[$uid].
  65. * $instore is only used by the set_config function
  66. * to determine if the key already exists in the DB
  67. * If a key is found in the DB but doesn't exist in
  68. * local config cache, pull it into the cache so we don't have
  69. * to hit the DB again for this item.
  70. *
  71. * @param string $family The category of the configuration value
  72. * @param string $key The configuration key to query
  73. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  74. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  75. *
  76. * @return mixed Stored value or null if it does not exist
  77. */
  78. public static function get($family, $key, $default_value = null, $refresh = false)
  79. {
  80. // Database isn't ready or populated yet, fallback to file config
  81. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  82. return self::getApp()->getConfigValue($family, $key, $default_value);
  83. }
  84. if (empty(self::$adapter)) {
  85. self::init();
  86. }
  87. return self::$adapter->get($family, $key, $default_value, $refresh);
  88. }
  89. /**
  90. * @brief Sets a configuration value for system config
  91. *
  92. * Stores a config value ($value) in the category ($family) under the key ($key)
  93. * for the user_id $uid.
  94. *
  95. * Note: Please do not store booleans - convert to 0/1 integer values!
  96. *
  97. * @param string $family The category of the configuration value
  98. * @param string $key The configuration key to set
  99. * @param mixed $value The value to store
  100. *
  101. * @return mixed Stored $value or false if the database update failed
  102. */
  103. public static function set($family, $key, $value)
  104. {
  105. // Database isn't ready or populated yet
  106. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  107. return false;
  108. }
  109. if (empty(self::$adapter)) {
  110. self::init();
  111. }
  112. return self::$adapter->set($family, $key, $value);
  113. }
  114. /**
  115. * @brief Deletes the given key from the system configuration.
  116. *
  117. * Removes the configured value from the stored cache in $a->config
  118. * and removes it from the database.
  119. *
  120. * @param string $family The category of the configuration value
  121. * @param string $key The configuration key to delete
  122. *
  123. * @return mixed
  124. */
  125. public static function delete($family, $key)
  126. {
  127. // Database isn't ready or populated yet
  128. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  129. return false;
  130. }
  131. if (empty(self::$adapter)) {
  132. self::init();
  133. }
  134. return self::$adapter->delete($family, $key);
  135. }
  136. }