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.

177 lines
4.7 KiB

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