2019-02-03 22:22:04 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
namespace Friendica\Core\Config\ValueObject;
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2023-01-01 21:10:37 +01:00
|
|
|
use Friendica\Core\Config\Util\ConfigFileManager;
|
2019-06-10 14:43:25 +02:00
|
|
|
use ParagonIE\HiddenString\HiddenString;
|
|
|
|
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
|
|
|
* The Friendica config cache for the application
|
|
|
|
* Initial, all *.config.php files are loaded into this cache with the
|
2023-01-01 21:10:37 +01:00
|
|
|
* ConfigFileManager ( @see ConfigFileManager )
|
2019-02-03 23:39:30 +01:00
|
|
|
*/
|
2020-01-19 22:23:44 +01:00
|
|
|
class Cache
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2021-12-14 10:14:24 +01:00
|
|
|
/** @var int Indicates that the cache entry is a default value - Lowest Priority */
|
|
|
|
const SOURCE_STATIC = 0;
|
2020-10-04 20:37:35 +02:00
|
|
|
/** @var int Indicates that the cache entry is set by file - Low Priority */
|
2021-12-14 10:14:24 +01:00
|
|
|
const SOURCE_FILE = 1;
|
2023-01-01 21:10:37 +01:00
|
|
|
/** @var int Indicates that the cache entry is manually set by the application (per admin page/console) - Middle Priority */
|
|
|
|
const SOURCE_DATA = 2;
|
2020-10-04 20:37:35 +02:00
|
|
|
/** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
|
|
|
|
const SOURCE_ENV = 3;
|
2020-10-06 20:06:52 +02:00
|
|
|
/** @var int Indicates that the cache entry is fixed and must not be changed */
|
2021-12-14 10:14:24 +01:00
|
|
|
const SOURCE_FIX = 5;
|
2020-10-04 20:37:35 +02:00
|
|
|
|
|
|
|
/** @var int Default value for a config source */
|
|
|
|
const SOURCE_DEFAULT = self::SOURCE_FILE;
|
|
|
|
|
2019-02-10 19:52:21 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
private $config = [];
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2020-10-04 20:37:35 +02:00
|
|
|
/**
|
|
|
|
* @var int[][]
|
|
|
|
*/
|
|
|
|
private $source = [];
|
|
|
|
|
2019-06-10 14:43:25 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hidePasswordOutput;
|
|
|
|
|
2019-02-03 23:39:30 +01:00
|
|
|
/**
|
2019-07-12 22:38:50 +02:00
|
|
|
* @param array $config A initial config array
|
2019-06-10 14:43:25 +02:00
|
|
|
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
|
2020-10-04 20:37:35 +02:00
|
|
|
* @param int $source Sets a source of the initial config values
|
2019-02-03 23:39:30 +01:00
|
|
|
*/
|
2023-01-06 01:02:47 +01:00
|
|
|
public function __construct(array $config = [], bool $hidePasswordOutput = true, int $source = self::SOURCE_DEFAULT)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-06-10 14:43:25 +02:00
|
|
|
$this->hidePasswordOutput = $hidePasswordOutput;
|
2020-10-04 20:37:35 +02:00
|
|
|
$this->load($config, $source);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-12 22:38:50 +02:00
|
|
|
* Tries to load the specified configuration array into the config array.
|
|
|
|
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
|
|
|
*
|
|
|
|
* @param array $config
|
2020-10-04 20:37:35 +02:00
|
|
|
* @param int $source Indicates the source of the config entry
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2020-10-04 20:37:35 +02:00
|
|
|
public function load(array $config, int $source = self::SOURCE_DEFAULT)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-10 19:52:21 +01:00
|
|
|
$categories = array_keys($config);
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
2019-03-17 09:57:34 +01:00
|
|
|
if (is_array($config[$category])) {
|
2019-02-10 19:52:21 +01:00
|
|
|
$keys = array_keys($config[$category]);
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
2023-01-06 01:02:47 +01:00
|
|
|
$this->set($category, $key, $config[$category][$key] ?? null, $source);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
2023-01-06 01:02:47 +01:00
|
|
|
} else {
|
|
|
|
$this->set($category, null, $config[$category], $source);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-12 22:38:50 +02:00
|
|
|
* Gets a value from the config cache.
|
|
|
|
*
|
2021-10-26 21:44:29 +02:00
|
|
|
* @param string $cat Config category
|
|
|
|
* @param string|null $key Config key
|
2019-07-12 22:38:50 +02:00
|
|
|
*
|
|
|
|
* @return null|mixed Returns the value of the Config entry or null if not set
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function get(string $cat, ?string $key = null)
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-10 19:52:21 +01:00
|
|
|
if (isset($this->config[$cat][$key])) {
|
|
|
|
return $this->config[$cat][$key];
|
2021-10-26 21:44:29 +02:00
|
|
|
} elseif (!isset($key) && isset($this->config[$cat])) {
|
2019-02-11 21:13:53 +01:00
|
|
|
return $this->config[$cat];
|
2019-02-03 22:22:04 +01:00
|
|
|
} else {
|
2019-02-22 23:51:13 +01:00
|
|
|
return null;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
2019-02-03 22:22:04 +01:00
|
|
|
|
2021-12-14 10:14:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the source value of the current, cached config value
|
|
|
|
*
|
|
|
|
* @param string $cat Config category
|
|
|
|
* @param string $key Config key
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getSource(string $cat, string $key): int
|
|
|
|
{
|
|
|
|
return $this->source[$cat][$key] ?? -1;
|
|
|
|
}
|
|
|
|
|
2023-01-01 21:10:37 +01:00
|
|
|
/**
|
|
|
|
* Returns the whole config array based on the given source type
|
|
|
|
*
|
|
|
|
* @param int $source Indicates the source of the config entry
|
|
|
|
*
|
|
|
|
* @return array The config array part of the given source
|
|
|
|
*/
|
|
|
|
public function getDataBySource(int $source): array
|
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$categories = array_keys($this->source);
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if (is_array($this->source[$category])) {
|
|
|
|
$keys = array_keys($this->source[$category]);
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if ($this->source[$category][$key] === $source) {
|
|
|
|
$data[$category][$key] = $this->config[$category][$key];
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 01:02:47 +01:00
|
|
|
} elseif (is_int($this->source[$category])) {
|
|
|
|
$data[$category] = null;
|
2023-01-01 21:10:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:22:04 +01:00
|
|
|
/**
|
2019-07-12 22:38:50 +02:00
|
|
|
* Sets a value in the config cache. Accepts raw output from the config table
|
|
|
|
*
|
2023-01-06 01:02:47 +01:00
|
|
|
* @param string $cat Config category
|
|
|
|
* @param ?string $key Config key
|
|
|
|
* @param ?mixed $value Value to set
|
|
|
|
* @param int $source The source of the current config key
|
2019-07-12 22:38:50 +02:00
|
|
|
*
|
|
|
|
* @return bool True, if the value is set
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2023-01-06 01:02:47 +01:00
|
|
|
public function set(string $cat, string $key = null, $value = null, int $source = self::SOURCE_DEFAULT): bool
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2023-01-06 01:02:47 +01:00
|
|
|
if (!isset($this->config[$cat]) && $key !== null) {
|
2019-02-10 19:52:21 +01:00
|
|
|
$this->config[$cat] = [];
|
2020-10-04 20:37:35 +02:00
|
|
|
$this->source[$cat] = [];
|
|
|
|
}
|
|
|
|
|
2023-01-06 01:02:47 +01:00
|
|
|
if ((isset($this->source[$cat][$key]) && $source < $this->source[$cat][$key]) ||
|
|
|
|
(is_int($this->source[$cat] ?? null) && $source < $this->source[$cat])) {
|
2020-10-04 20:37:35 +02:00
|
|
|
return false;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
2019-02-07 20:44:03 +01:00
|
|
|
|
2019-06-10 14:43:25 +02:00
|
|
|
if ($this->hidePasswordOutput &&
|
2020-10-04 20:37:35 +02:00
|
|
|
$key == 'password' &&
|
|
|
|
is_string($value)) {
|
2023-01-06 12:43:04 +01:00
|
|
|
$this->setCatKeyValueSource($cat, $key, new HiddenString($value), $source);
|
2022-12-28 03:32:52 +01:00
|
|
|
} else if (is_string($value)) {
|
2023-01-06 01:02:47 +01:00
|
|
|
$this->setCatKeyValueSource($cat, $key, self::toConfigValue($value), $source);
|
2019-06-10 14:43:25 +02:00
|
|
|
} else {
|
2023-01-06 01:02:47 +01:00
|
|
|
$this->setCatKeyValueSource($cat, $key, $value, $source);
|
2019-06-10 14:43:25 +02:00
|
|
|
}
|
2020-10-04 20:37:35 +02:00
|
|
|
|
2019-02-07 20:44:03 +01:00
|
|
|
return true;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 01:02:47 +01:00
|
|
|
private function setCatKeyValueSource(string $cat, string $key = null, $value = null, int $source = self::SOURCE_DEFAULT)
|
|
|
|
{
|
|
|
|
if (isset($key)) {
|
|
|
|
$this->config[$cat][$key] = $value;
|
|
|
|
$this->source[$cat][$key] = $source;
|
|
|
|
} else {
|
|
|
|
$this->config[$cat] = $value;
|
|
|
|
$this->source[$cat] = $source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 03:32:52 +01:00
|
|
|
/**
|
|
|
|
* Formats a DB value to a config value
|
|
|
|
* - null = The db-value isn't set
|
|
|
|
* - bool = The db-value is either '0' or '1'
|
|
|
|
* - array = The db-value is a serialized array
|
|
|
|
* - string = The db-value is a string
|
|
|
|
*
|
|
|
|
* Keep in mind that there aren't any numeric/integer config values in the database
|
|
|
|
*
|
|
|
|
* @param string|null $value
|
|
|
|
*
|
|
|
|
* @return null|array|string
|
|
|
|
*/
|
|
|
|
public static function toConfigValue(?string $value)
|
|
|
|
{
|
|
|
|
if (!isset($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:25:41 +01:00
|
|
|
if (preg_match("|^a:[0-9]+:{.*}$|s", $value)) {
|
|
|
|
return unserialize($value);
|
|
|
|
} else {
|
|
|
|
return $value;
|
2022-12-28 03:32:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:22:04 +01:00
|
|
|
/**
|
2019-07-12 22:38:50 +02:00
|
|
|
* Deletes a value from the config cache.
|
|
|
|
*
|
2023-01-06 01:02:47 +01:00
|
|
|
* @param string $cat Config category
|
|
|
|
* @param ?string $key Config key (if not set, the whole category will get deleted)
|
|
|
|
* @param int $source The source of the current config key
|
2019-07-12 22:38:50 +02:00
|
|
|
*
|
|
|
|
* @return bool true, if deleted
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2023-01-06 01:02:47 +01:00
|
|
|
public function delete(string $cat, string $key = null, int $source = self::SOURCE_DEFAULT): bool
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
2019-02-10 19:52:21 +01:00
|
|
|
if (isset($this->config[$cat][$key])) {
|
2023-01-06 01:02:47 +01:00
|
|
|
$this->config[$cat][$key] = null;
|
|
|
|
$this->source[$cat][$key] = $source;
|
|
|
|
if (empty(array_filter($this->config[$cat], function($value) { return !is_null($value); }))) {
|
|
|
|
$this->config[$cat] = null;
|
|
|
|
$this->source[$cat] = $source;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
2023-01-06 01:02:47 +01:00
|
|
|
} elseif (isset($this->config[$cat])) {
|
|
|
|
$this->config[$cat] = null;
|
|
|
|
$this->source[$cat] = $source;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
2023-01-06 01:02:47 +01:00
|
|
|
|
|
|
|
return true;
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the whole configuration
|
|
|
|
*
|
2021-10-26 21:44:29 +02:00
|
|
|
* @return string[][] The configuration
|
2019-02-03 22:22:04 +01:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function getAll(): array
|
2019-02-03 22:22:04 +01:00
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
2019-03-30 18:54:22 +01:00
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
/**
|
|
|
|
* Returns an array with missing categories/Keys
|
|
|
|
*
|
2021-10-26 21:44:29 +02:00
|
|
|
* @param string[][] $config The array to check
|
2019-03-30 19:08:47 +01:00
|
|
|
*
|
2021-10-26 21:44:29 +02:00
|
|
|
* @return string[][]
|
2019-03-30 19:08:47 +01:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function keyDiff(array $config): array
|
2019-03-30 19:08:47 +01:00
|
|
|
{
|
|
|
|
$return = [];
|
2019-03-30 18:54:22 +01:00
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
$categories = array_keys($config);
|
2019-03-30 18:54:22 +01:00
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
foreach ($categories as $category) {
|
|
|
|
if (is_array($config[$category])) {
|
|
|
|
$keys = array_keys($config[$category]);
|
2019-03-30 18:54:22 +01:00
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
foreach ($keys as $key) {
|
2023-01-08 02:28:27 +01:00
|
|
|
if (!key_exists($key, $this->config[$category] ?? [])) {
|
2019-03-30 19:08:47 +01:00
|
|
|
$return[$category][$key] = $config[$category][$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 18:54:22 +01:00
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
return $return;
|
|
|
|
}
|
2023-01-03 14:18:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges a new Cache into the existing one and returns the merged Cache
|
|
|
|
*
|
|
|
|
* @param Cache $cache The cache, which should get merged into this Cache
|
|
|
|
*
|
|
|
|
* @return Cache The merged Cache
|
|
|
|
*/
|
|
|
|
public function merge(Cache $cache): Cache
|
|
|
|
{
|
|
|
|
$newConfig = $this->config;
|
|
|
|
$newSource = $this->source;
|
|
|
|
|
|
|
|
$categories = array_keys($cache->config);
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if (is_array($cache->config[$category])) {
|
|
|
|
$keys = array_keys($cache->config[$category]);
|
|
|
|
|
2023-01-08 02:28:47 +01:00
|
|
|
if (is_null($newConfig[$category] ?? null)) {
|
|
|
|
$newConfig[$category] = [];
|
|
|
|
$newSource[$category] = [];
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:18:53 +01:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
$newConfig[$category][$key] = $cache->config[$category][$key];
|
|
|
|
$newSource[$category][$key] = $cache->source[$category][$key];
|
|
|
|
}
|
2023-01-06 01:02:47 +01:00
|
|
|
} else {
|
|
|
|
$newConfig[$category] = $cache->config[$category];
|
|
|
|
$newSource[$category] = $cache->source[$category];
|
2023-01-03 14:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$newCache = new Cache();
|
|
|
|
$newCache->config = $newConfig;
|
|
|
|
$newCache->source = $newSource;
|
|
|
|
|
|
|
|
return $newCache;
|
|
|
|
}
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|