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.

ConfigCache.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Friendica\Core\Config\Cache;
  3. use ParagonIE\HiddenString\HiddenString;
  4. /**
  5. * The Friendica config cache for the application
  6. * Initial, all *.config.php files are loaded into this cache with the
  7. * ConfigFileLoader ( @see ConfigFileLoader )
  8. */
  9. class ConfigCache implements IConfigCache, IPConfigCache
  10. {
  11. /**
  12. * @var array
  13. */
  14. private $config;
  15. /**
  16. * @var bool
  17. */
  18. private $hidePasswordOutput;
  19. /**
  20. * @param array $config A initial config array
  21. * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
  22. */
  23. public function __construct(array $config = [], $hidePasswordOutput = true)
  24. {
  25. $this->hidePasswordOutput = $hidePasswordOutput;
  26. $this->load($config);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function load(array $config, $overwrite = false)
  32. {
  33. $categories = array_keys($config);
  34. foreach ($categories as $category) {
  35. if (is_array($config[$category])) {
  36. $keys = array_keys($config[$category]);
  37. foreach ($keys as $key) {
  38. $value = $config[$category][$key];
  39. if (isset($value)) {
  40. if ($overwrite) {
  41. $this->set($category, $key, $value);
  42. } else {
  43. $this->setDefault($category, $key, $value);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function get($cat, $key = null)
  54. {
  55. if (isset($this->config[$cat][$key])) {
  56. return $this->config[$cat][$key];
  57. } elseif (!isset($key) && isset($this->config[$cat])) {
  58. return $this->config[$cat];
  59. } else {
  60. return null;
  61. }
  62. }
  63. /**
  64. * Sets a default value in the config cache. Ignores already existing keys.
  65. *
  66. * @param string $cat Config category
  67. * @param string $k Config key
  68. * @param mixed $v Default value to set
  69. */
  70. private function setDefault($cat, $k, $v)
  71. {
  72. if (!isset($this->config[$cat][$k])) {
  73. $this->set($cat, $k, $v);
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function set($cat, $key, $value)
  80. {
  81. if (!isset($this->config[$cat])) {
  82. $this->config[$cat] = [];
  83. }
  84. if ($this->hidePasswordOutput &&
  85. $key == 'password' &&
  86. is_string($value)) {
  87. $this->config[$cat][$key] = new HiddenString((string) $value);
  88. } else {
  89. $this->config[$cat][$key] = $value;
  90. }
  91. return true;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function delete($cat, $key)
  97. {
  98. if (isset($this->config[$cat][$key])) {
  99. unset($this->config[$cat][$key]);
  100. if (count($this->config[$cat]) == 0) {
  101. unset($this->config[$cat]);
  102. }
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function loadP($uid, array $config)
  112. {
  113. $categories = array_keys($config);
  114. foreach ($categories as $category) {
  115. if (isset($config[$category]) && is_array($config[$category])) {
  116. $keys = array_keys($config[$category]);
  117. foreach ($keys as $key) {
  118. $value = $config[$category][$key];
  119. if (isset($value)) {
  120. $this->setP($uid, $category, $key, $value);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getP($uid, $cat, $key = null)
  130. {
  131. if (isset($this->config[$uid][$cat][$key])) {
  132. return $this->config[$uid][$cat][$key];
  133. } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
  134. return $this->config[$uid][$cat];
  135. } else {
  136. return null;
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function setP($uid, $cat, $key, $value)
  143. {
  144. if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
  145. $this->config[$uid] = [];
  146. }
  147. if (!isset($this->config[$uid][$cat])) {
  148. $this->config[$uid][$cat] = [];
  149. }
  150. $this->config[$uid][$cat][$key] = $value;
  151. return true;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function deleteP($uid, $cat, $key)
  157. {
  158. if (isset($this->config[$uid][$cat][$key])) {
  159. unset($this->config[$uid][$cat][$key]);
  160. if (count($this->config[$uid][$cat]) == 0) {
  161. unset($this->config[$uid][$cat]);
  162. if (count($this->config[$uid]) == 0) {
  163. unset($this->config[$uid]);
  164. }
  165. }
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. /**
  172. * Returns the whole configuration
  173. *
  174. * @return array The configuration
  175. */
  176. public function getAll()
  177. {
  178. return $this->config;
  179. }
  180. /**
  181. * Returns an array with missing categories/Keys
  182. *
  183. * @param array $config The array to check
  184. *
  185. * @return array
  186. */
  187. public function keyDiff(array $config)
  188. {
  189. $return = [];
  190. $categories = array_keys($config);
  191. foreach ($categories as $category) {
  192. if (is_array($config[$category])) {
  193. $keys = array_keys($config[$category]);
  194. foreach ($keys as $key) {
  195. if (!isset($this->config[$category][$key])) {
  196. $return[$category][$key] = $config[$category][$key];
  197. }
  198. }
  199. }
  200. }
  201. return $return;
  202. }
  203. }