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.

153 lines
4.1KB

  1. <?php
  2. /**
  3. * User Configuration Class
  4. *
  5. * @file include/Core/PConfig.php
  6. *
  7. * @brief Contains the class with methods for user configuration
  8. */
  9. namespace Friendica\Core;
  10. use Friendica\App;
  11. use Friendica\BaseObject;
  12. /**
  13. * @brief Management of user configuration storage
  14. * Note:
  15. * Please do not store booleans - convert to 0/1 integer values
  16. * The PConfig::get() functions return boolean false for keys that are unset,
  17. * and this could lead to subtle bugs.
  18. */
  19. class PConfig extends BaseObject
  20. {
  21. /**
  22. * @var Friendica\Core\Config\IPConfigAdapter
  23. */
  24. private static $adapter = null;
  25. public static function init($uid)
  26. {
  27. $a = self::getApp();
  28. // Database isn't ready or populated yet
  29. if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  30. return;
  31. }
  32. if ($a->getConfigValue('system', 'config_adapter') == 'preload') {
  33. self::$adapter = new Config\PreloadPConfigAdapter($uid);
  34. } else {
  35. self::$adapter = new Config\JITPConfigAdapter($uid);
  36. }
  37. }
  38. /**
  39. * @brief Loads all configuration values of a user's config family into a cached storage.
  40. *
  41. * All configuration values of the given user are stored in global cache
  42. * which is available under the global variable $a->config[$uid].
  43. *
  44. * @param string $uid The user_id
  45. * @param string $family The category of the configuration value
  46. *
  47. * @return void
  48. */
  49. public static function load($uid, $family)
  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($uid);
  57. }
  58. self::$adapter->load($uid, $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 user's config value from the given category ($family)
  65. * and the $key from a cached storage in $a->config[$uid].
  66. *
  67. * @param string $uid The user_id
  68. * @param string $family The category of the configuration value
  69. * @param string $key The configuration key to query
  70. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  71. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  72. *
  73. * @return mixed Stored value or null if it does not exist
  74. */
  75. public static function get($uid, $family, $key, $default_value = null, $refresh = false)
  76. {
  77. // Database isn't ready or populated yet
  78. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  79. return;
  80. }
  81. if (empty(self::$adapter)) {
  82. self::init($uid);
  83. }
  84. return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
  85. }
  86. /**
  87. * @brief Sets a configuration value for a user
  88. *
  89. * Stores a config value ($value) in the category ($family) under the key ($key)
  90. * for the user_id $uid.
  91. *
  92. * @note Please do not store booleans - convert to 0/1 integer values!
  93. *
  94. * @param string $uid The user_id
  95. * @param string $family The category of the configuration value
  96. * @param string $key The configuration key to set
  97. * @param string $value The value to store
  98. *
  99. * @return bool Operation success
  100. */
  101. public static function set($uid, $family, $key, $value)
  102. {
  103. // Database isn't ready or populated yet
  104. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  105. return false;
  106. }
  107. if (empty(self::$adapter)) {
  108. self::init($uid);
  109. }
  110. return self::$adapter->set($uid, $family, $key, $value);
  111. }
  112. /**
  113. * @brief Deletes the given key from the users's configuration.
  114. *
  115. * Removes the configured value from the stored cache in $a->config[$uid]
  116. * and removes it from the database.
  117. *
  118. * @param string $uid The user_id
  119. * @param string $family The category of the configuration value
  120. * @param string $key The configuration key to delete
  121. *
  122. * @return mixed
  123. */
  124. public static function delete($uid, $family, $key)
  125. {
  126. // Database isn't ready or populated yet
  127. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  128. return false;
  129. }
  130. if (empty(self::$adapter)) {
  131. self::init($uid);
  132. }
  133. return self::$adapter->delete($uid, $family, $key);
  134. }
  135. }