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.
 
 
 
 
 
 

176 lines
5.0 KiB

  1. <?php
  2. /**
  3. * @file src/Core/PConfig.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\Database\DBM;
  7. use dba;
  8. /**
  9. * @file include/Core/PConfig.php
  10. * @brief contains the class with methods for the management
  11. * of the user configuration
  12. */
  13. /**
  14. * @brief Management of user configuration storage
  15. * Note:
  16. * Please do not store booleans - convert to 0/1 integer values
  17. * The PConfig::get() functions return boolean false for keys that are unset,
  18. * and this could lead to subtle bugs.
  19. */
  20. class PConfig
  21. {
  22. private static $in_db;
  23. /**
  24. * @brief Loads all configuration values of a user's config family into a cached storage.
  25. *
  26. * All configuration values of the given user are stored in global cache
  27. * which is available under the global variable $a->config[$uid].
  28. *
  29. * @param string $uid The user_id
  30. * @param string $family The category of the configuration value
  31. *
  32. * @return void
  33. */
  34. public static function load($uid, $family)
  35. {
  36. $a = get_app();
  37. $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
  38. if (DBM::is_result($r)) {
  39. while ($rr = dba::fetch($r)) {
  40. $k = $rr['k'];
  41. $a->config[$uid][$family][$k] = $rr['v'];
  42. self::$in_db[$uid][$family][$k] = true;
  43. }
  44. } else if ($family != 'config') {
  45. // Negative caching
  46. $a->config[$uid][$family] = "!<unset>!";
  47. }
  48. dba::close($r);
  49. }
  50. /**
  51. * @brief Get a particular user's config variable given the category name
  52. * ($family) and a key.
  53. *
  54. * Get a particular user's config value from the given category ($family)
  55. * and the $key from a cached storage in $a->config[$uid].
  56. *
  57. * @param string $uid The user_id
  58. * @param string $family The category of the configuration value
  59. * @param string $key The configuration key to query
  60. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  61. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  62. *
  63. * @return mixed Stored value or null if it does not exist
  64. */
  65. public static function get($uid, $family, $key, $default_value = null, $refresh = false)
  66. {
  67. $a = get_app();
  68. if (!$refresh) {
  69. // Looking if the whole family isn't set
  70. if (isset($a->config[$uid][$family])) {
  71. if ($a->config[$uid][$family] === '!<unset>!') {
  72. return $default_value;
  73. }
  74. }
  75. if (isset($a->config[$uid][$family][$key])) {
  76. if ($a->config[$uid][$family][$key] === '!<unset>!') {
  77. return $default_value;
  78. }
  79. return $a->config[$uid][$family][$key];
  80. }
  81. }
  82. $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array('limit' => 1));
  83. if (DBM::is_result($ret)) {
  84. $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
  85. $a->config[$uid][$family][$key] = $val;
  86. self::$in_db[$uid][$family][$key] = true;
  87. return $val;
  88. } else {
  89. $a->config[$uid][$family][$key] = '!<unset>!';
  90. self::$in_db[$uid][$family][$key] = false;
  91. return $default_value;
  92. }
  93. }
  94. /**
  95. * @brief Sets a configuration value for a user
  96. *
  97. * Stores a config value ($value) in the category ($family) under the key ($key)
  98. * for the user_id $uid.
  99. *
  100. * @note Please do not store booleans - convert to 0/1 integer values!
  101. *
  102. * @param string $uid The user_id
  103. * @param string $family The category of the configuration value
  104. * @param string $key The configuration key to set
  105. * @param string $value The value to store
  106. *
  107. * @return mixed Stored $value or false
  108. */
  109. public static function set($uid, $family, $key, $value)
  110. {
  111. $a = get_app();
  112. // We store our setting values in a string variable.
  113. // So we have to do the conversion here so that the compare below works.
  114. // The exception are array values.
  115. $dbvalue = (!is_array($value) ? (string)$value : $value);
  116. $stored = self::get($uid, $family, $key, null, true);
  117. if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
  118. return true;
  119. }
  120. $a->config[$uid][$family][$key] = $dbvalue;
  121. // manage array value
  122. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  123. dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
  124. if ($ret) {
  125. self::$in_db[$uid][$family][$key] = true;
  126. return $value;
  127. }
  128. return $ret;
  129. }
  130. /**
  131. * @brief Deletes the given key from the users's configuration.
  132. *
  133. * Removes the configured value from the stored cache in $a->config[$uid]
  134. * and removes it from the database.
  135. *
  136. * @param string $uid The user_id
  137. * @param string $family The category of the configuration value
  138. * @param string $key The configuration key to delete
  139. *
  140. * @return mixed
  141. */
  142. public static function delete($uid, $family, $key)
  143. {
  144. $a = get_app();
  145. if (x($a->config[$uid][$family], $key)) {
  146. unset($a->config[$uid][$family][$key]);
  147. unset(self::$in_db[$uid][$family][$key]);
  148. }
  149. $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
  150. return $ret;
  151. }
  152. }