Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

Config.php 4.2KB

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