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.

189 lines
4.8KB

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