Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $this->config[$cat][$key] = new HiddenString($value);
  87. } else {
  88. $this->config[$cat][$key] = $value;
  89. }
  90. return true;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function delete($cat, $key)
  96. {
  97. if (isset($this->config[$cat][$key])) {
  98. unset($this->config[$cat][$key]);
  99. if (count($this->config[$cat]) == 0) {
  100. unset($this->config[$cat]);
  101. }
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function loadP($uid, array $config)
  111. {
  112. $categories = array_keys($config);
  113. foreach ($categories as $category) {
  114. if (isset($config[$category]) && is_array($config[$category])) {
  115. $keys = array_keys($config[$category]);
  116. foreach ($keys as $key) {
  117. $value = $config[$category][$key];
  118. if (isset($value)) {
  119. $this->setP($uid, $category, $key, $value);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getP($uid, $cat, $key = null)
  129. {
  130. if (isset($this->config[$uid][$cat][$key])) {
  131. return $this->config[$uid][$cat][$key];
  132. } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
  133. return $this->config[$uid][$cat];
  134. } else {
  135. return null;
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function setP($uid, $cat, $key, $value)
  142. {
  143. if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
  144. $this->config[$uid] = [];
  145. }
  146. if (!isset($this->config[$uid][$cat])) {
  147. $this->config[$uid][$cat] = [];
  148. }
  149. $this->config[$uid][$cat][$key] = $value;
  150. return true;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function deleteP($uid, $cat, $key)
  156. {
  157. if (isset($this->config[$uid][$cat][$key])) {
  158. unset($this->config[$uid][$cat][$key]);
  159. if (count($this->config[$uid][$cat]) == 0) {
  160. unset($this->config[$uid][$cat]);
  161. if (count($this->config[$uid]) == 0) {
  162. unset($this->config[$uid]);
  163. }
  164. }
  165. return true;
  166. } else {
  167. return false;
  168. }
  169. }
  170. /**
  171. * Returns the whole configuration
  172. *
  173. * @return array The configuration
  174. */
  175. public function getAll()
  176. {
  177. return $this->config;
  178. }
  179. /**
  180. * Returns an array with missing categories/Keys
  181. *
  182. * @param array $config The array to check
  183. *
  184. * @return array
  185. */
  186. public function keyDiff(array $config)
  187. {
  188. $return = [];
  189. $categories = array_keys($config);
  190. foreach ($categories as $category) {
  191. if (is_array($config[$category])) {
  192. $keys = array_keys($config[$category]);
  193. foreach ($keys as $key) {
  194. if (!isset($this->config[$category][$key])) {
  195. $return[$category][$key] = $config[$category][$key];
  196. }
  197. }
  198. }
  199. }
  200. return $return;
  201. }
  202. }