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.

152 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\BaseObject;
  11. require_once 'include/dba.php';
  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. // Database isn't ready or populated yet
  28. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  29. return;
  30. }
  31. if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
  32. self::$adapter = new Config\PreloadPConfigAdapter($uid);
  33. } else {
  34. self::$adapter = new Config\JITPConfigAdapter($uid);
  35. }
  36. }
  37. /**
  38. * @brief Loads all configuration values of a user's config family into a cached storage.
  39. *
  40. * All configuration values of the given user are stored in global cache
  41. * which is available under the global variable $a->config[$uid].
  42. *
  43. * @param string $uid The user_id
  44. * @param string $family The category of the configuration value
  45. *
  46. * @return void
  47. */
  48. public static function load($uid, $family)
  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($uid);
  56. }
  57. self::$adapter->load($uid, $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 user's config value from the given category ($family)
  64. * and the $key from a cached storage in $a->config[$uid].
  65. *
  66. * @param string $uid The user_id
  67. * @param string $family The category of the configuration value
  68. * @param string $key The configuration key to query
  69. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  70. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  71. *
  72. * @return mixed Stored value or null if it does not exist
  73. */
  74. public static function get($uid, $family, $key, $default_value = null, $refresh = false)
  75. {
  76. // Database isn't ready or populated yet
  77. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  78. return;
  79. }
  80. if (empty(self::$adapter)) {
  81. self::init($uid);
  82. }
  83. return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
  84. }
  85. /**
  86. * @brief Sets a configuration value for a user
  87. *
  88. * Stores a config value ($value) in the category ($family) under the key ($key)
  89. * for the user_id $uid.
  90. *
  91. * @note Please do not store booleans - convert to 0/1 integer values!
  92. *
  93. * @param string $uid The user_id
  94. * @param string $family The category of the configuration value
  95. * @param string $key The configuration key to set
  96. * @param string $value The value to store
  97. *
  98. * @return mixed Stored $value or false
  99. */
  100. public static function set($uid, $family, $key, $value)
  101. {
  102. // Database isn't ready or populated yet
  103. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  104. return false;
  105. }
  106. if (empty(self::$adapter)) {
  107. self::init($uid);
  108. }
  109. return self::$adapter->set($uid, $family, $key, $value);
  110. }
  111. /**
  112. * @brief Deletes the given key from the users's configuration.
  113. *
  114. * Removes the configured value from the stored cache in $a->config[$uid]
  115. * and removes it from the database.
  116. *
  117. * @param string $uid The user_id
  118. * @param string $family The category of the configuration value
  119. * @param string $key The configuration key to delete
  120. *
  121. * @return mixed
  122. */
  123. public static function delete($uid, $family, $key)
  124. {
  125. // Database isn't ready or populated yet
  126. if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) {
  127. return false;
  128. }
  129. if (empty(self::$adapter)) {
  130. self::init($uid);
  131. }
  132. return self::$adapter->delete($uid, $family, $key);
  133. }
  134. }