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.
 
 
 
 
 
 

193 lines
5.5 KiB

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