@@ -16,19 +16,19 @@ |
||
| 16 | 16 | * @since 27.0.0 |
| 17 | 17 | */ |
| 18 | 18 | class AppUpdateEvent extends Event { |
| 19 | - /** |
|
| 20 | - * @since 27.0.0 |
|
| 21 | - */ |
|
| 22 | - public function __construct( |
|
| 23 | - private readonly string $appId, |
|
| 24 | - ) { |
|
| 25 | - parent::__construct(); |
|
| 26 | - } |
|
| 19 | + /** |
|
| 20 | + * @since 27.0.0 |
|
| 21 | + */ |
|
| 22 | + public function __construct( |
|
| 23 | + private readonly string $appId, |
|
| 24 | + ) { |
|
| 25 | + parent::__construct(); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @since 27.0.0 |
|
| 30 | - */ |
|
| 31 | - public function getAppId(): string { |
|
| 32 | - return $this->appId; |
|
| 33 | - } |
|
| 28 | + /** |
|
| 29 | + * @since 27.0.0 |
|
| 30 | + */ |
|
| 31 | + public function getAppId(): string { |
|
| 32 | + return $this->appId; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -17,219 +17,219 @@ |
||
| 17 | 17 | * @experimental 31.0.0 |
| 18 | 18 | */ |
| 19 | 19 | class ConfigLexiconEntry { |
| 20 | - /** @experimental 32.0.0 */ |
|
| 21 | - public const RENAME_INVERT_BOOLEAN = 1; |
|
| 22 | - |
|
| 23 | - private string $definition = ''; |
|
| 24 | - private ?string $default = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @param string $key config key |
|
| 28 | - * @param ValueType $type type of config value |
|
| 29 | - * @param string $definition optional description of config key available when using occ command |
|
| 30 | - * @param bool $lazy set config value as lazy |
|
| 31 | - * @param int $flags set flags |
|
| 32 | - * @param string|null $rename previous config key to migrate config value from |
|
| 33 | - * @param bool $deprecated set config key as deprecated |
|
| 34 | - * |
|
| 35 | - * @experimental 31.0.0 |
|
| 36 | - * @psalm-suppress PossiblyInvalidCast |
|
| 37 | - * @psalm-suppress RiskyCast |
|
| 38 | - */ |
|
| 39 | - public function __construct( |
|
| 40 | - private readonly string $key, |
|
| 41 | - private readonly ValueType $type, |
|
| 42 | - private null|string|int|float|bool|array $defaultRaw = null, |
|
| 43 | - string $definition = '', |
|
| 44 | - private readonly bool $lazy = false, |
|
| 45 | - private readonly int $flags = 0, |
|
| 46 | - private readonly bool $deprecated = false, |
|
| 47 | - private readonly ?string $rename = null, |
|
| 48 | - private readonly int $options = 0, |
|
| 49 | - ) { |
|
| 50 | - /** @psalm-suppress UndefinedClass */ |
|
| 51 | - if (\OC::$CLI) { // only store definition if ran from CLI |
|
| 52 | - $this->definition = $definition; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * returns the config key |
|
| 58 | - * |
|
| 59 | - * @return string config key |
|
| 60 | - * @experimental 31.0.0 |
|
| 61 | - */ |
|
| 62 | - public function getKey(): string { |
|
| 63 | - return $this->key; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * get expected type for config value |
|
| 68 | - * |
|
| 69 | - * @return ValueType |
|
| 70 | - * @experimental 31.0.0 |
|
| 71 | - */ |
|
| 72 | - public function getValueType(): ValueType { |
|
| 73 | - return $this->type; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $default |
|
| 78 | - * @return string |
|
| 79 | - * @experimental 31.0.0 |
|
| 80 | - */ |
|
| 81 | - private function convertFromString(string $default): string { |
|
| 82 | - return $default; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param int $default |
|
| 87 | - * @return string |
|
| 88 | - * @experimental 31.0.0 |
|
| 89 | - */ |
|
| 90 | - private function convertFromInt(int $default): string { |
|
| 91 | - return (string)$default; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param float $default |
|
| 96 | - * @return string |
|
| 97 | - * @experimental 31.0.0 |
|
| 98 | - */ |
|
| 99 | - private function convertFromFloat(float $default): string { |
|
| 100 | - return (string)$default; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param bool $default |
|
| 105 | - * @return string |
|
| 106 | - * @experimental 31.0.0 |
|
| 107 | - */ |
|
| 108 | - private function convertFromBool(bool $default): string { |
|
| 109 | - return ($default) ? '1' : '0'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param array $default |
|
| 114 | - * @return string |
|
| 115 | - * @experimental 31.0.0 |
|
| 116 | - */ |
|
| 117 | - private function convertFromArray(array $default): string { |
|
| 118 | - return json_encode($default); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * returns default value |
|
| 123 | - * |
|
| 124 | - * @return string|null NULL if no default is set |
|
| 125 | - * @experimental 31.0.0 |
|
| 126 | - */ |
|
| 127 | - public function getDefault(): ?string { |
|
| 128 | - if ($this->defaultRaw === null) { |
|
| 129 | - return null; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($this->default === null) { |
|
| 133 | - $this->default = $this->convertToString($this->defaultRaw); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $this->default; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * convert $entry into string, based on the expected type for config value |
|
| 141 | - * |
|
| 142 | - * @param string|int|float|bool|array $entry |
|
| 143 | - * |
|
| 144 | - * @return string |
|
| 145 | - * @experimental 31.0.0 |
|
| 146 | - * @psalm-suppress PossiblyInvalidCast arrays are managed pre-cast |
|
| 147 | - * @psalm-suppress RiskyCast |
|
| 148 | - */ |
|
| 149 | - public function convertToString(string|int|float|bool|array $entry): string { |
|
| 150 | - // in case $default is array but is not expected to be an array... |
|
| 151 | - if ($this->getValueType() !== ValueType::ARRAY && is_array($entry)) { |
|
| 152 | - $entry = json_encode($entry, JSON_THROW_ON_ERROR); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return match ($this->getValueType()) { |
|
| 156 | - ValueType::MIXED => (string)$entry, |
|
| 157 | - ValueType::STRING => $this->convertFromString((string)$entry), |
|
| 158 | - ValueType::INT => $this->convertFromInt((int)$entry), |
|
| 159 | - ValueType::FLOAT => $this->convertFromFloat((float)$entry), |
|
| 160 | - ValueType::BOOL => $this->convertFromBool((bool)$entry), |
|
| 161 | - ValueType::ARRAY => $this->convertFromArray((array)$entry) |
|
| 162 | - }; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * returns definition |
|
| 167 | - * |
|
| 168 | - * @return string |
|
| 169 | - * @experimental 31.0.0 |
|
| 170 | - */ |
|
| 171 | - public function getDefinition(): string { |
|
| 172 | - return $this->definition; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * returns if config key is set as lazy |
|
| 177 | - * |
|
| 178 | - * @see IAppConfig for details on lazy config values |
|
| 179 | - * @return bool TRUE if config value is lazy |
|
| 180 | - * @experimental 31.0.0 |
|
| 181 | - */ |
|
| 182 | - public function isLazy(): bool { |
|
| 183 | - return $this->lazy; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * returns flags |
|
| 188 | - * |
|
| 189 | - * @see IAppConfig for details on sensitive config values |
|
| 190 | - * @return int bitflag about the config value |
|
| 191 | - * @experimental 31.0.0 |
|
| 192 | - */ |
|
| 193 | - public function getFlags(): int { |
|
| 194 | - return $this->flags; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param int $flag |
|
| 199 | - * |
|
| 200 | - * @return bool TRUE is config value bitflag contains $flag |
|
| 201 | - * @experimental 31.0.0 |
|
| 202 | - */ |
|
| 203 | - public function isFlagged(int $flag): bool { |
|
| 204 | - return (($flag & $this->getFlags()) === $flag); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * should be called/used only during migration/upgrade. |
|
| 209 | - * link to an old config key. |
|
| 210 | - * |
|
| 211 | - * @return string|null not NULL if value can be imported from a previous key |
|
| 212 | - * @experimental 32.0.0 |
|
| 213 | - */ |
|
| 214 | - public function getRename(): ?string { |
|
| 215 | - return $this->rename; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @experimental 32.0.0 |
|
| 220 | - * @return bool TRUE if $option was set during the creation of the entry. |
|
| 221 | - */ |
|
| 222 | - public function hasOption(int $option): bool { |
|
| 223 | - return (($option & $this->options) !== 0); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * returns if config key is set as deprecated |
|
| 228 | - * |
|
| 229 | - * @return bool TRUE if config si deprecated |
|
| 230 | - * @experimental 31.0.0 |
|
| 231 | - */ |
|
| 232 | - public function isDeprecated(): bool { |
|
| 233 | - return $this->deprecated; |
|
| 234 | - } |
|
| 20 | + /** @experimental 32.0.0 */ |
|
| 21 | + public const RENAME_INVERT_BOOLEAN = 1; |
|
| 22 | + |
|
| 23 | + private string $definition = ''; |
|
| 24 | + private ?string $default = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @param string $key config key |
|
| 28 | + * @param ValueType $type type of config value |
|
| 29 | + * @param string $definition optional description of config key available when using occ command |
|
| 30 | + * @param bool $lazy set config value as lazy |
|
| 31 | + * @param int $flags set flags |
|
| 32 | + * @param string|null $rename previous config key to migrate config value from |
|
| 33 | + * @param bool $deprecated set config key as deprecated |
|
| 34 | + * |
|
| 35 | + * @experimental 31.0.0 |
|
| 36 | + * @psalm-suppress PossiblyInvalidCast |
|
| 37 | + * @psalm-suppress RiskyCast |
|
| 38 | + */ |
|
| 39 | + public function __construct( |
|
| 40 | + private readonly string $key, |
|
| 41 | + private readonly ValueType $type, |
|
| 42 | + private null|string|int|float|bool|array $defaultRaw = null, |
|
| 43 | + string $definition = '', |
|
| 44 | + private readonly bool $lazy = false, |
|
| 45 | + private readonly int $flags = 0, |
|
| 46 | + private readonly bool $deprecated = false, |
|
| 47 | + private readonly ?string $rename = null, |
|
| 48 | + private readonly int $options = 0, |
|
| 49 | + ) { |
|
| 50 | + /** @psalm-suppress UndefinedClass */ |
|
| 51 | + if (\OC::$CLI) { // only store definition if ran from CLI |
|
| 52 | + $this->definition = $definition; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * returns the config key |
|
| 58 | + * |
|
| 59 | + * @return string config key |
|
| 60 | + * @experimental 31.0.0 |
|
| 61 | + */ |
|
| 62 | + public function getKey(): string { |
|
| 63 | + return $this->key; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * get expected type for config value |
|
| 68 | + * |
|
| 69 | + * @return ValueType |
|
| 70 | + * @experimental 31.0.0 |
|
| 71 | + */ |
|
| 72 | + public function getValueType(): ValueType { |
|
| 73 | + return $this->type; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $default |
|
| 78 | + * @return string |
|
| 79 | + * @experimental 31.0.0 |
|
| 80 | + */ |
|
| 81 | + private function convertFromString(string $default): string { |
|
| 82 | + return $default; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param int $default |
|
| 87 | + * @return string |
|
| 88 | + * @experimental 31.0.0 |
|
| 89 | + */ |
|
| 90 | + private function convertFromInt(int $default): string { |
|
| 91 | + return (string)$default; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param float $default |
|
| 96 | + * @return string |
|
| 97 | + * @experimental 31.0.0 |
|
| 98 | + */ |
|
| 99 | + private function convertFromFloat(float $default): string { |
|
| 100 | + return (string)$default; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param bool $default |
|
| 105 | + * @return string |
|
| 106 | + * @experimental 31.0.0 |
|
| 107 | + */ |
|
| 108 | + private function convertFromBool(bool $default): string { |
|
| 109 | + return ($default) ? '1' : '0'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param array $default |
|
| 114 | + * @return string |
|
| 115 | + * @experimental 31.0.0 |
|
| 116 | + */ |
|
| 117 | + private function convertFromArray(array $default): string { |
|
| 118 | + return json_encode($default); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * returns default value |
|
| 123 | + * |
|
| 124 | + * @return string|null NULL if no default is set |
|
| 125 | + * @experimental 31.0.0 |
|
| 126 | + */ |
|
| 127 | + public function getDefault(): ?string { |
|
| 128 | + if ($this->defaultRaw === null) { |
|
| 129 | + return null; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($this->default === null) { |
|
| 133 | + $this->default = $this->convertToString($this->defaultRaw); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $this->default; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * convert $entry into string, based on the expected type for config value |
|
| 141 | + * |
|
| 142 | + * @param string|int|float|bool|array $entry |
|
| 143 | + * |
|
| 144 | + * @return string |
|
| 145 | + * @experimental 31.0.0 |
|
| 146 | + * @psalm-suppress PossiblyInvalidCast arrays are managed pre-cast |
|
| 147 | + * @psalm-suppress RiskyCast |
|
| 148 | + */ |
|
| 149 | + public function convertToString(string|int|float|bool|array $entry): string { |
|
| 150 | + // in case $default is array but is not expected to be an array... |
|
| 151 | + if ($this->getValueType() !== ValueType::ARRAY && is_array($entry)) { |
|
| 152 | + $entry = json_encode($entry, JSON_THROW_ON_ERROR); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return match ($this->getValueType()) { |
|
| 156 | + ValueType::MIXED => (string)$entry, |
|
| 157 | + ValueType::STRING => $this->convertFromString((string)$entry), |
|
| 158 | + ValueType::INT => $this->convertFromInt((int)$entry), |
|
| 159 | + ValueType::FLOAT => $this->convertFromFloat((float)$entry), |
|
| 160 | + ValueType::BOOL => $this->convertFromBool((bool)$entry), |
|
| 161 | + ValueType::ARRAY => $this->convertFromArray((array)$entry) |
|
| 162 | + }; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * returns definition |
|
| 167 | + * |
|
| 168 | + * @return string |
|
| 169 | + * @experimental 31.0.0 |
|
| 170 | + */ |
|
| 171 | + public function getDefinition(): string { |
|
| 172 | + return $this->definition; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * returns if config key is set as lazy |
|
| 177 | + * |
|
| 178 | + * @see IAppConfig for details on lazy config values |
|
| 179 | + * @return bool TRUE if config value is lazy |
|
| 180 | + * @experimental 31.0.0 |
|
| 181 | + */ |
|
| 182 | + public function isLazy(): bool { |
|
| 183 | + return $this->lazy; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * returns flags |
|
| 188 | + * |
|
| 189 | + * @see IAppConfig for details on sensitive config values |
|
| 190 | + * @return int bitflag about the config value |
|
| 191 | + * @experimental 31.0.0 |
|
| 192 | + */ |
|
| 193 | + public function getFlags(): int { |
|
| 194 | + return $this->flags; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param int $flag |
|
| 199 | + * |
|
| 200 | + * @return bool TRUE is config value bitflag contains $flag |
|
| 201 | + * @experimental 31.0.0 |
|
| 202 | + */ |
|
| 203 | + public function isFlagged(int $flag): bool { |
|
| 204 | + return (($flag & $this->getFlags()) === $flag); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * should be called/used only during migration/upgrade. |
|
| 209 | + * link to an old config key. |
|
| 210 | + * |
|
| 211 | + * @return string|null not NULL if value can be imported from a previous key |
|
| 212 | + * @experimental 32.0.0 |
|
| 213 | + */ |
|
| 214 | + public function getRename(): ?string { |
|
| 215 | + return $this->rename; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @experimental 32.0.0 |
|
| 220 | + * @return bool TRUE if $option was set during the creation of the entry. |
|
| 221 | + */ |
|
| 222 | + public function hasOption(int $option): bool { |
|
| 223 | + return (($option & $this->options) !== 0); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * returns if config key is set as deprecated |
|
| 228 | + * |
|
| 229 | + * @return bool TRUE if config si deprecated |
|
| 230 | + * @experimental 31.0.0 |
|
| 231 | + */ |
|
| 232 | + public function isDeprecated(): bool { |
|
| 233 | + return $this->deprecated; |
|
| 234 | + } |
|
| 235 | 235 | } |
@@ -31,841 +31,841 @@ |
||
| 31 | 31 | * upgrading and removing apps. |
| 32 | 32 | */ |
| 33 | 33 | class OC_App { |
| 34 | - private static $altLogin = []; |
|
| 35 | - private static $alreadyRegistered = []; |
|
| 36 | - public const supportedApp = 300; |
|
| 37 | - public const officialApp = 200; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * clean the appId |
|
| 41 | - * |
|
| 42 | - * @psalm-taint-escape file |
|
| 43 | - * @psalm-taint-escape include |
|
| 44 | - * @psalm-taint-escape html |
|
| 45 | - * @psalm-taint-escape has_quotes |
|
| 46 | - * |
|
| 47 | - * @deprecated 31.0.0 use IAppManager::cleanAppId |
|
| 48 | - */ |
|
| 49 | - public static function cleanAppId(string $app): string { |
|
| 50 | - return str_replace(['<', '>', '"', "'", '\0', '/', '\\', '..'], '', $app); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Check if an app is loaded |
|
| 55 | - * |
|
| 56 | - * @param string $app |
|
| 57 | - * @return bool |
|
| 58 | - * @deprecated 27.0.0 use IAppManager::isAppLoaded |
|
| 59 | - */ |
|
| 60 | - public static function isAppLoaded(string $app): bool { |
|
| 61 | - return \OC::$server->get(IAppManager::class)->isAppLoaded($app); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * loads all apps |
|
| 66 | - * |
|
| 67 | - * @param string[] $types |
|
| 68 | - * @return bool |
|
| 69 | - * |
|
| 70 | - * This function walks through the Nextcloud directory and loads all apps |
|
| 71 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 72 | - * exists. |
|
| 73 | - * |
|
| 74 | - * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 75 | - * |
|
| 76 | - * @deprecated 29.0.0 use IAppManager::loadApps instead |
|
| 77 | - */ |
|
| 78 | - public static function loadApps(array $types = []): bool { |
|
| 79 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 80 | - // This should be done before calling this method so that appmanager can be used |
|
| 81 | - return false; |
|
| 82 | - } |
|
| 83 | - return \OC::$server->get(IAppManager::class)->loadApps($types); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * load a single app |
|
| 88 | - * |
|
| 89 | - * @param string $app |
|
| 90 | - * @throws Exception |
|
| 91 | - * @deprecated 27.0.0 use IAppManager::loadApp |
|
| 92 | - */ |
|
| 93 | - public static function loadApp(string $app): void { |
|
| 94 | - \OC::$server->get(IAppManager::class)->loadApp($app); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @internal |
|
| 99 | - * @param string $app |
|
| 100 | - * @param string $path |
|
| 101 | - * @param bool $force |
|
| 102 | - */ |
|
| 103 | - public static function registerAutoloading(string $app, string $path, bool $force = false) { |
|
| 104 | - $key = $app . '-' . $path; |
|
| 105 | - if (!$force && isset(self::$alreadyRegistered[$key])) { |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - self::$alreadyRegistered[$key] = true; |
|
| 110 | - |
|
| 111 | - // Register on PSR-4 composer autoloader |
|
| 112 | - $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 113 | - \OC::$server->registerNamespace($app, $appNamespace); |
|
| 114 | - |
|
| 115 | - if (file_exists($path . '/composer/autoload.php')) { |
|
| 116 | - require_once $path . '/composer/autoload.php'; |
|
| 117 | - } else { |
|
| 118 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Register Test namespace only when testing |
|
| 122 | - if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 123 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * check if an app is of a specific type |
|
| 129 | - * |
|
| 130 | - * @param string $app |
|
| 131 | - * @param array $types |
|
| 132 | - * @return bool |
|
| 133 | - * @deprecated 27.0.0 use IAppManager::isType |
|
| 134 | - */ |
|
| 135 | - public static function isType(string $app, array $types): bool { |
|
| 136 | - return \OC::$server->get(IAppManager::class)->isType($app, $types); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * read app types from info.xml and cache them in the database |
|
| 141 | - */ |
|
| 142 | - public static function setAppTypes(string $app) { |
|
| 143 | - $appManager = \OC::$server->getAppManager(); |
|
| 144 | - $appData = $appManager->getAppInfo($app); |
|
| 145 | - if (!is_array($appData)) { |
|
| 146 | - return; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if (isset($appData['types'])) { |
|
| 150 | - $appTypes = implode(',', $appData['types']); |
|
| 151 | - } else { |
|
| 152 | - $appTypes = ''; |
|
| 153 | - $appData['types'] = []; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $config = \OC::$server->getConfig(); |
|
| 157 | - $config->setAppValue($app, 'types', $appTypes); |
|
| 158 | - |
|
| 159 | - if ($appManager->hasProtectedAppType($appData['types'])) { |
|
| 160 | - $enabled = $config->getAppValue($app, 'enabled', 'yes'); |
|
| 161 | - if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 162 | - $config->setAppValue($app, 'enabled', 'yes'); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Returns apps enabled for the current user. |
|
| 169 | - * |
|
| 170 | - * @param bool $forceRefresh whether to refresh the cache |
|
| 171 | - * @param bool $all whether to return apps for all users, not only the |
|
| 172 | - * currently logged in one |
|
| 173 | - * @return list<string> |
|
| 174 | - */ |
|
| 175 | - public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array { |
|
| 176 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 177 | - return []; |
|
| 178 | - } |
|
| 179 | - // in incognito mode or when logged out, $user will be false, |
|
| 180 | - // which is also the case during an upgrade |
|
| 181 | - $appManager = \OC::$server->getAppManager(); |
|
| 182 | - if ($all) { |
|
| 183 | - $user = null; |
|
| 184 | - } else { |
|
| 185 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if (is_null($user)) { |
|
| 189 | - $apps = $appManager->getEnabledApps(); |
|
| 190 | - } else { |
|
| 191 | - $apps = $appManager->getEnabledAppsForUser($user); |
|
| 192 | - } |
|
| 193 | - $apps = array_filter($apps, function ($app) { |
|
| 194 | - return $app !== 'files';//we add this manually |
|
| 195 | - }); |
|
| 196 | - sort($apps); |
|
| 197 | - array_unshift($apps, 'files'); |
|
| 198 | - return $apps; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * enables an app |
|
| 203 | - * |
|
| 204 | - * @param string $appId |
|
| 205 | - * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 206 | - * @throws \Exception |
|
| 207 | - * @return void |
|
| 208 | - * |
|
| 209 | - * This function set an app as enabled in appconfig. |
|
| 210 | - */ |
|
| 211 | - public function enable(string $appId, |
|
| 212 | - array $groups = []) { |
|
| 213 | - // Check if app is already downloaded |
|
| 214 | - /** @var Installer $installer */ |
|
| 215 | - $installer = Server::get(Installer::class); |
|
| 216 | - $isDownloaded = $installer->isDownloaded($appId); |
|
| 217 | - |
|
| 218 | - if (!$isDownloaded) { |
|
| 219 | - $installer->downloadApp($appId); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $installer->installApp($appId); |
|
| 223 | - |
|
| 224 | - $appManager = \OC::$server->getAppManager(); |
|
| 225 | - if ($groups !== []) { |
|
| 226 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 227 | - $groupsList = []; |
|
| 228 | - foreach ($groups as $group) { |
|
| 229 | - $groupItem = $groupManager->get($group); |
|
| 230 | - if ($groupItem instanceof \OCP\IGroup) { |
|
| 231 | - $groupsList[] = $groupManager->get($group); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - $appManager->enableAppForGroups($appId, $groupsList); |
|
| 235 | - } else { |
|
| 236 | - $appManager->enableApp($appId); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Get the path where to install apps |
|
| 242 | - */ |
|
| 243 | - public static function getInstallPath(): ?string { |
|
| 244 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 245 | - if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 246 | - return $dir['path']; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - Server::get(LoggerInterface::class)->error('No application directories are marked as writable.', ['app' => 'core']); |
|
| 251 | - return null; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Find the apps root for an app id. |
|
| 257 | - * |
|
| 258 | - * If multiple copies are found, the apps root the latest version is returned. |
|
| 259 | - * |
|
| 260 | - * @param string $appId |
|
| 261 | - * @param bool $ignoreCache ignore cache and rebuild it |
|
| 262 | - * @return false|array{path: string, url: string} the apps root shape |
|
| 263 | - */ |
|
| 264 | - public static function findAppInDirectories(string $appId, bool $ignoreCache = false) { |
|
| 265 | - $sanitizedAppId = self::cleanAppId($appId); |
|
| 266 | - if ($sanitizedAppId !== $appId) { |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - static $app_dir = []; |
|
| 270 | - |
|
| 271 | - if (isset($app_dir[$appId]) && !$ignoreCache) { |
|
| 272 | - return $app_dir[$appId]; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - $possibleApps = []; |
|
| 276 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 277 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 278 | - $possibleApps[] = $dir; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - if (empty($possibleApps)) { |
|
| 283 | - return false; |
|
| 284 | - } elseif (count($possibleApps) === 1) { |
|
| 285 | - $dir = array_shift($possibleApps); |
|
| 286 | - $app_dir[$appId] = $dir; |
|
| 287 | - return $dir; |
|
| 288 | - } else { |
|
| 289 | - $versionToLoad = []; |
|
| 290 | - foreach ($possibleApps as $possibleApp) { |
|
| 291 | - $version = self::getAppVersionByPath($possibleApp['path'] . '/' . $appId); |
|
| 292 | - if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 293 | - $versionToLoad = [ |
|
| 294 | - 'dir' => $possibleApp, |
|
| 295 | - 'version' => $version, |
|
| 296 | - ]; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - $app_dir[$appId] = $versionToLoad['dir']; |
|
| 300 | - return $versionToLoad['dir']; |
|
| 301 | - //TODO - write test |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Get the directory for the given app. |
|
| 307 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 308 | - * |
|
| 309 | - * @psalm-taint-specialize |
|
| 310 | - * |
|
| 311 | - * @param string $appId |
|
| 312 | - * @param bool $refreshAppPath should be set to true only during install/upgrade |
|
| 313 | - * @return string|false |
|
| 314 | - * @deprecated 11.0.0 use Server::get(IAppManager)->getAppPath() |
|
| 315 | - */ |
|
| 316 | - public static function getAppPath(string $appId, bool $refreshAppPath = false) { |
|
| 317 | - $appId = self::cleanAppId($appId); |
|
| 318 | - if ($appId === '') { |
|
| 319 | - return false; |
|
| 320 | - } elseif ($appId === 'core') { |
|
| 321 | - return __DIR__ . '/../../../core'; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if (($dir = self::findAppInDirectories($appId, $refreshAppPath)) != false) { |
|
| 325 | - return $dir['path'] . '/' . $appId; |
|
| 326 | - } |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get the path for the given app on the access |
|
| 332 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 333 | - * |
|
| 334 | - * @param string $appId |
|
| 335 | - * @return string|false |
|
| 336 | - * @deprecated 18.0.0 use \OC::$server->getAppManager()->getAppWebPath() |
|
| 337 | - */ |
|
| 338 | - public static function getAppWebPath(string $appId) { |
|
| 339 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 340 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 341 | - } |
|
| 342 | - return false; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * get app's version based on it's path |
|
| 347 | - * |
|
| 348 | - * @param string $path |
|
| 349 | - * @return string |
|
| 350 | - */ |
|
| 351 | - public static function getAppVersionByPath(string $path): string { |
|
| 352 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 353 | - $appData = Server::get(IAppManager::class)->getAppInfoByPath($infoFile); |
|
| 354 | - return $appData['version'] ?? ''; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * get the id of loaded app |
|
| 359 | - * |
|
| 360 | - * @return string |
|
| 361 | - */ |
|
| 362 | - public static function getCurrentApp(): string { |
|
| 363 | - if (\OC::$CLI) { |
|
| 364 | - return ''; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - $request = \OC::$server->getRequest(); |
|
| 368 | - $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 369 | - $topFolder = substr($script, 0, strpos($script, '/') ?: 0); |
|
| 370 | - if (empty($topFolder)) { |
|
| 371 | - try { |
|
| 372 | - $path_info = $request->getPathInfo(); |
|
| 373 | - } catch (Exception $e) { |
|
| 374 | - // Can happen from unit tests because the script name is `./vendor/bin/phpunit` or something a like then. |
|
| 375 | - \OC::$server->get(LoggerInterface::class)->error('Failed to detect current app from script path', ['exception' => $e]); |
|
| 376 | - return ''; |
|
| 377 | - } |
|
| 378 | - if ($path_info) { |
|
| 379 | - $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - if ($topFolder == 'apps') { |
|
| 383 | - $length = strlen($topFolder); |
|
| 384 | - return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: ''; |
|
| 385 | - } else { |
|
| 386 | - return $topFolder; |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @param array $entry |
|
| 392 | - * @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface |
|
| 393 | - */ |
|
| 394 | - public static function registerLogIn(array $entry) { |
|
| 395 | - Server::get(LoggerInterface::class)->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface'); |
|
| 396 | - self::$altLogin[] = $entry; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * @return array |
|
| 401 | - */ |
|
| 402 | - public static function getAlternativeLogIns(): array { |
|
| 403 | - /** @var Coordinator $bootstrapCoordinator */ |
|
| 404 | - $bootstrapCoordinator = Server::get(Coordinator::class); |
|
| 405 | - |
|
| 406 | - foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) { |
|
| 407 | - if (!in_array(IAlternativeLogin::class, class_implements($registration->getService()), true)) { |
|
| 408 | - Server::get(LoggerInterface::class)->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [ |
|
| 409 | - 'option' => $registration->getService(), |
|
| 410 | - 'interface' => IAlternativeLogin::class, |
|
| 411 | - 'app' => $registration->getAppId(), |
|
| 412 | - ]); |
|
| 413 | - continue; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - try { |
|
| 417 | - /** @var IAlternativeLogin $provider */ |
|
| 418 | - $provider = Server::get($registration->getService()); |
|
| 419 | - } catch (ContainerExceptionInterface $e) { |
|
| 420 | - Server::get(LoggerInterface::class)->error('Alternative login option {option} can not be initialized.', |
|
| 421 | - [ |
|
| 422 | - 'exception' => $e, |
|
| 423 | - 'option' => $registration->getService(), |
|
| 424 | - 'app' => $registration->getAppId(), |
|
| 425 | - ]); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - try { |
|
| 429 | - $provider->load(); |
|
| 430 | - |
|
| 431 | - self::$altLogin[] = [ |
|
| 432 | - 'name' => $provider->getLabel(), |
|
| 433 | - 'href' => $provider->getLink(), |
|
| 434 | - 'class' => $provider->getClass(), |
|
| 435 | - ]; |
|
| 436 | - } catch (Throwable $e) { |
|
| 437 | - Server::get(LoggerInterface::class)->error('Alternative login option {option} had an error while loading.', |
|
| 438 | - [ |
|
| 439 | - 'exception' => $e, |
|
| 440 | - 'option' => $registration->getService(), |
|
| 441 | - 'app' => $registration->getAppId(), |
|
| 442 | - ]); |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - return self::$altLogin; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * get a list of all apps in the apps folder |
|
| 451 | - * |
|
| 452 | - * @return string[] an array of app names (string IDs) |
|
| 453 | - * @deprecated 31.0.0 Use IAppManager::getAllAppsInAppsFolders instead |
|
| 454 | - */ |
|
| 455 | - public static function getAllApps(): array { |
|
| 456 | - return Server::get(IAppManager::class)->getAllAppsInAppsFolders(); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * List all supported apps |
|
| 461 | - * |
|
| 462 | - * @deprecated 32.0.0 Use \OCP\Support\Subscription\IRegistry::delegateGetSupportedApps instead |
|
| 463 | - */ |
|
| 464 | - public function getSupportedApps(): array { |
|
| 465 | - $subscriptionRegistry = Server::get(\OCP\Support\Subscription\IRegistry::class); |
|
| 466 | - $supportedApps = $subscriptionRegistry->delegateGetSupportedApps(); |
|
| 467 | - return $supportedApps; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * List all apps, this is used in apps.php |
|
| 472 | - * |
|
| 473 | - * @return array |
|
| 474 | - */ |
|
| 475 | - public function listAllApps(): array { |
|
| 476 | - $appManager = \OC::$server->getAppManager(); |
|
| 477 | - |
|
| 478 | - $installedApps = $appManager->getAllAppsInAppsFolders(); |
|
| 479 | - //we don't want to show configuration for these |
|
| 480 | - $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 481 | - $appList = []; |
|
| 482 | - $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 483 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 484 | - $supportedApps = $this->getSupportedApps(); |
|
| 485 | - |
|
| 486 | - foreach ($installedApps as $app) { |
|
| 487 | - if (!in_array($app, $blacklist)) { |
|
| 488 | - $info = $appManager->getAppInfo($app, false, $langCode); |
|
| 489 | - if (!is_array($info)) { |
|
| 490 | - Server::get(LoggerInterface::class)->error('Could not read app info file for app "' . $app . '"', ['app' => 'core']); |
|
| 491 | - continue; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - if (!isset($info['name'])) { |
|
| 495 | - Server::get(LoggerInterface::class)->error('App id "' . $app . '" has no name in appinfo', ['app' => 'core']); |
|
| 496 | - continue; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no'); |
|
| 500 | - $info['groups'] = null; |
|
| 501 | - if ($enabled === 'yes') { |
|
| 502 | - $active = true; |
|
| 503 | - } elseif ($enabled === 'no') { |
|
| 504 | - $active = false; |
|
| 505 | - } else { |
|
| 506 | - $active = true; |
|
| 507 | - $info['groups'] = $enabled; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - $info['active'] = $active; |
|
| 511 | - |
|
| 512 | - if ($appManager->isShipped($app)) { |
|
| 513 | - $info['internal'] = true; |
|
| 514 | - $info['level'] = self::officialApp; |
|
| 515 | - $info['removable'] = false; |
|
| 516 | - } else { |
|
| 517 | - $info['internal'] = false; |
|
| 518 | - $info['removable'] = true; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - if (in_array($app, $supportedApps)) { |
|
| 522 | - $info['level'] = self::supportedApp; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - $appPath = self::getAppPath($app); |
|
| 526 | - if ($appPath !== false) { |
|
| 527 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 528 | - if (file_exists($appIcon)) { |
|
| 529 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 530 | - $info['previewAsIcon'] = true; |
|
| 531 | - } else { |
|
| 532 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 533 | - if (file_exists($appIcon)) { |
|
| 534 | - $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 535 | - $info['previewAsIcon'] = true; |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - // fix documentation |
|
| 540 | - if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 541 | - foreach ($info['documentation'] as $key => $url) { |
|
| 542 | - // If it is not an absolute URL we assume it is a key |
|
| 543 | - // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 544 | - if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 545 | - $url = $urlGenerator->linkToDocs($url); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - $info['documentation'][$key] = $url; |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $info['version'] = $appManager->getAppVersion($app); |
|
| 553 | - $appList[] = $info; |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - return $appList; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - public static function shouldUpgrade(string $app): bool { |
|
| 561 | - $versions = self::getAppVersions(); |
|
| 562 | - $currentVersion = Server::get(\OCP\App\IAppManager::class)->getAppVersion($app); |
|
| 563 | - if ($currentVersion && isset($versions[$app])) { |
|
| 564 | - $installedVersion = $versions[$app]; |
|
| 565 | - if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 566 | - return true; |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - return false; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Adjust the number of version parts of $version1 to match |
|
| 574 | - * the number of version parts of $version2. |
|
| 575 | - * |
|
| 576 | - * @param string $version1 version to adjust |
|
| 577 | - * @param string $version2 version to take the number of parts from |
|
| 578 | - * @return string shortened $version1 |
|
| 579 | - */ |
|
| 580 | - private static function adjustVersionParts(string $version1, string $version2): string { |
|
| 581 | - $version1 = explode('.', $version1); |
|
| 582 | - $version2 = explode('.', $version2); |
|
| 583 | - // reduce $version1 to match the number of parts in $version2 |
|
| 584 | - while (count($version1) > count($version2)) { |
|
| 585 | - array_pop($version1); |
|
| 586 | - } |
|
| 587 | - // if $version1 does not have enough parts, add some |
|
| 588 | - while (count($version1) < count($version2)) { |
|
| 589 | - $version1[] = '0'; |
|
| 590 | - } |
|
| 591 | - return implode('.', $version1); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Check whether the current Nextcloud version matches the given |
|
| 596 | - * application's version requirements. |
|
| 597 | - * |
|
| 598 | - * The comparison is made based on the number of parts that the |
|
| 599 | - * app info version has. For example for ownCloud 6.0.3 if the |
|
| 600 | - * app info version is expecting version 6.0, the comparison is |
|
| 601 | - * made on the first two parts of the ownCloud version. |
|
| 602 | - * This means that it's possible to specify "requiremin" => 6 |
|
| 603 | - * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 604 | - * |
|
| 605 | - * @param string $ocVersion Nextcloud version to check against |
|
| 606 | - * @param array $appInfo app info (from xml) |
|
| 607 | - * |
|
| 608 | - * @return boolean true if compatible, otherwise false |
|
| 609 | - */ |
|
| 610 | - public static function isAppCompatible(string $ocVersion, array $appInfo, bool $ignoreMax = false): bool { |
|
| 611 | - $requireMin = ''; |
|
| 612 | - $requireMax = ''; |
|
| 613 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 614 | - $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 615 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 616 | - $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 617 | - } elseif (isset($appInfo['requiremin'])) { |
|
| 618 | - $requireMin = $appInfo['requiremin']; |
|
| 619 | - } elseif (isset($appInfo['require'])) { |
|
| 620 | - $requireMin = $appInfo['require']; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 624 | - $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 625 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 626 | - $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 627 | - } elseif (isset($appInfo['requiremax'])) { |
|
| 628 | - $requireMax = $appInfo['requiremax']; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - if (!empty($requireMin) |
|
| 632 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 633 | - ) { |
|
| 634 | - return false; |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - if (!$ignoreMax && !empty($requireMax) |
|
| 638 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 639 | - ) { |
|
| 640 | - return false; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - return true; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * get the installed version of all apps |
|
| 648 | - * @deprecated 32.0.0 Use IAppManager::getAppInstalledVersions or IAppConfig::getAppInstalledVersions instead |
|
| 649 | - */ |
|
| 650 | - public static function getAppVersions(): array { |
|
| 651 | - return Server::get(IAppConfig::class)->getAppInstalledVersions(); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * update the database for the app and call the update script |
|
| 656 | - * |
|
| 657 | - * @param string $appId |
|
| 658 | - * @return bool |
|
| 659 | - */ |
|
| 660 | - public static function updateApp(string $appId): bool { |
|
| 661 | - // for apps distributed with core, we refresh app path in case the downloaded version |
|
| 662 | - // have been installed in custom apps and not in the default path |
|
| 663 | - $appPath = self::getAppPath($appId, true); |
|
| 664 | - if ($appPath === false) { |
|
| 665 | - return false; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - if (is_file($appPath . '/appinfo/database.xml')) { |
|
| 669 | - Server::get(LoggerInterface::class)->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId); |
|
| 670 | - return false; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - \OC::$server->getAppManager()->clearAppsCache(); |
|
| 674 | - $l = \OC::$server->getL10N('core'); |
|
| 675 | - $appData = Server::get(\OCP\App\IAppManager::class)->getAppInfo($appId, false, $l->getLanguageCode()); |
|
| 676 | - |
|
| 677 | - $ignoreMaxApps = \OC::$server->getConfig()->getSystemValue('app_install_overwrite', []); |
|
| 678 | - $ignoreMax = in_array($appId, $ignoreMaxApps, true); |
|
| 679 | - \OC_App::checkAppDependencies( |
|
| 680 | - \OC::$server->getConfig(), |
|
| 681 | - $l, |
|
| 682 | - $appData, |
|
| 683 | - $ignoreMax |
|
| 684 | - ); |
|
| 685 | - |
|
| 686 | - self::registerAutoloading($appId, $appPath, true); |
|
| 687 | - self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 688 | - |
|
| 689 | - $ms = new MigrationService($appId, \OC::$server->get(\OC\DB\Connection::class)); |
|
| 690 | - $ms->migrate(); |
|
| 691 | - |
|
| 692 | - self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 693 | - self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 694 | - // update appversion in app manager |
|
| 695 | - \OC::$server->getAppManager()->clearAppsCache(); |
|
| 696 | - \OC::$server->getAppManager()->getAppVersion($appId, false); |
|
| 697 | - |
|
| 698 | - self::setupBackgroundJobs($appData['background-jobs']); |
|
| 699 | - |
|
| 700 | - //set remote/public handlers |
|
| 701 | - if (array_key_exists('ocsid', $appData)) { |
|
| 702 | - \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 703 | - } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 704 | - \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 705 | - } |
|
| 706 | - foreach ($appData['remote'] as $name => $path) { |
|
| 707 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 708 | - } |
|
| 709 | - foreach ($appData['public'] as $name => $path) { |
|
| 710 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - self::setAppTypes($appId); |
|
| 714 | - |
|
| 715 | - $version = Server::get(\OCP\App\IAppManager::class)->getAppVersion($appId); |
|
| 716 | - \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version); |
|
| 717 | - |
|
| 718 | - // migrate eventual new config keys in the process |
|
| 719 | - /** @psalm-suppress InternalMethod */ |
|
| 720 | - Server::get(ConfigManager::class)->migrateConfigLexiconKeys($appId); |
|
| 721 | - |
|
| 722 | - \OC::$server->get(IEventDispatcher::class)->dispatchTyped(new AppUpdateEvent($appId)); |
|
| 723 | - \OC::$server->get(IEventDispatcher::class)->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 724 | - ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 725 | - )); |
|
| 726 | - |
|
| 727 | - return true; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * @param string $appId |
|
| 732 | - * @param string[] $steps |
|
| 733 | - * @throws \OC\NeedsUpdateException |
|
| 734 | - */ |
|
| 735 | - public static function executeRepairSteps(string $appId, array $steps) { |
|
| 736 | - if (empty($steps)) { |
|
| 737 | - return; |
|
| 738 | - } |
|
| 739 | - // load the app |
|
| 740 | - self::loadApp($appId); |
|
| 741 | - |
|
| 742 | - $dispatcher = Server::get(IEventDispatcher::class); |
|
| 743 | - |
|
| 744 | - // load the steps |
|
| 745 | - $r = Server::get(Repair::class); |
|
| 746 | - foreach ($steps as $step) { |
|
| 747 | - try { |
|
| 748 | - $r->addStep($step); |
|
| 749 | - } catch (Exception $ex) { |
|
| 750 | - $dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage())); |
|
| 751 | - logger('core')->error('Failed to add app migration step ' . $step, ['exception' => $ex]); |
|
| 752 | - } |
|
| 753 | - } |
|
| 754 | - // run the steps |
|
| 755 | - $r->run(); |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - public static function setupBackgroundJobs(array $jobs) { |
|
| 759 | - $queue = \OC::$server->getJobList(); |
|
| 760 | - foreach ($jobs as $job) { |
|
| 761 | - $queue->add($job); |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * @param string $appId |
|
| 767 | - * @param string[] $steps |
|
| 768 | - */ |
|
| 769 | - private static function setupLiveMigrations(string $appId, array $steps) { |
|
| 770 | - $queue = \OC::$server->getJobList(); |
|
| 771 | - foreach ($steps as $step) { |
|
| 772 | - $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 773 | - 'app' => $appId, |
|
| 774 | - 'step' => $step]); |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - protected static function findBestL10NOption(array $options, string $lang): string { |
|
| 779 | - // only a single option |
|
| 780 | - if (isset($options['@value'])) { |
|
| 781 | - return $options['@value']; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - $fallback = $similarLangFallback = $englishFallback = false; |
|
| 785 | - |
|
| 786 | - $lang = strtolower($lang); |
|
| 787 | - $similarLang = $lang; |
|
| 788 | - if (strpos($similarLang, '_')) { |
|
| 789 | - // For "de_DE" we want to find "de" and the other way around |
|
| 790 | - $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - foreach ($options as $option) { |
|
| 794 | - if (is_array($option)) { |
|
| 795 | - if ($fallback === false) { |
|
| 796 | - $fallback = $option['@value']; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - if (!isset($option['@attributes']['lang'])) { |
|
| 800 | - continue; |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 804 | - if ($attributeLang === $lang) { |
|
| 805 | - return $option['@value']; |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - if ($attributeLang === $similarLang) { |
|
| 809 | - $similarLangFallback = $option['@value']; |
|
| 810 | - } elseif (str_starts_with($attributeLang, $similarLang . '_')) { |
|
| 811 | - if ($similarLangFallback === false) { |
|
| 812 | - $similarLangFallback = $option['@value']; |
|
| 813 | - } |
|
| 814 | - } |
|
| 815 | - } else { |
|
| 816 | - $englishFallback = $option; |
|
| 817 | - } |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - if ($similarLangFallback !== false) { |
|
| 821 | - return $similarLangFallback; |
|
| 822 | - } elseif ($englishFallback !== false) { |
|
| 823 | - return $englishFallback; |
|
| 824 | - } |
|
| 825 | - return (string)$fallback; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * parses the app data array and enhanced the 'description' value |
|
| 830 | - * |
|
| 831 | - * @param array $data the app data |
|
| 832 | - * @param string $lang |
|
| 833 | - * @return array improved app data |
|
| 834 | - */ |
|
| 835 | - public static function parseAppInfo(array $data, $lang = null): array { |
|
| 836 | - if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 837 | - $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 838 | - } |
|
| 839 | - if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 840 | - $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 841 | - } |
|
| 842 | - if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 843 | - $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 844 | - } elseif (isset($data['description']) && is_string($data['description'])) { |
|
| 845 | - $data['description'] = trim($data['description']); |
|
| 846 | - } else { |
|
| 847 | - $data['description'] = ''; |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - return $data; |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - /** |
|
| 854 | - * @param \OCP\IConfig $config |
|
| 855 | - * @param \OCP\IL10N $l |
|
| 856 | - * @param array $info |
|
| 857 | - * @throws \Exception |
|
| 858 | - */ |
|
| 859 | - public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info, bool $ignoreMax) { |
|
| 860 | - $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 861 | - $missing = $dependencyAnalyzer->analyze($info, $ignoreMax); |
|
| 862 | - if (!empty($missing)) { |
|
| 863 | - $missingMsg = implode(PHP_EOL, $missing); |
|
| 864 | - throw new \Exception( |
|
| 865 | - $l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s', |
|
| 866 | - [$info['name'], $missingMsg] |
|
| 867 | - ) |
|
| 868 | - ); |
|
| 869 | - } |
|
| 870 | - } |
|
| 34 | + private static $altLogin = []; |
|
| 35 | + private static $alreadyRegistered = []; |
|
| 36 | + public const supportedApp = 300; |
|
| 37 | + public const officialApp = 200; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * clean the appId |
|
| 41 | + * |
|
| 42 | + * @psalm-taint-escape file |
|
| 43 | + * @psalm-taint-escape include |
|
| 44 | + * @psalm-taint-escape html |
|
| 45 | + * @psalm-taint-escape has_quotes |
|
| 46 | + * |
|
| 47 | + * @deprecated 31.0.0 use IAppManager::cleanAppId |
|
| 48 | + */ |
|
| 49 | + public static function cleanAppId(string $app): string { |
|
| 50 | + return str_replace(['<', '>', '"', "'", '\0', '/', '\\', '..'], '', $app); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Check if an app is loaded |
|
| 55 | + * |
|
| 56 | + * @param string $app |
|
| 57 | + * @return bool |
|
| 58 | + * @deprecated 27.0.0 use IAppManager::isAppLoaded |
|
| 59 | + */ |
|
| 60 | + public static function isAppLoaded(string $app): bool { |
|
| 61 | + return \OC::$server->get(IAppManager::class)->isAppLoaded($app); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * loads all apps |
|
| 66 | + * |
|
| 67 | + * @param string[] $types |
|
| 68 | + * @return bool |
|
| 69 | + * |
|
| 70 | + * This function walks through the Nextcloud directory and loads all apps |
|
| 71 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 72 | + * exists. |
|
| 73 | + * |
|
| 74 | + * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 75 | + * |
|
| 76 | + * @deprecated 29.0.0 use IAppManager::loadApps instead |
|
| 77 | + */ |
|
| 78 | + public static function loadApps(array $types = []): bool { |
|
| 79 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 80 | + // This should be done before calling this method so that appmanager can be used |
|
| 81 | + return false; |
|
| 82 | + } |
|
| 83 | + return \OC::$server->get(IAppManager::class)->loadApps($types); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * load a single app |
|
| 88 | + * |
|
| 89 | + * @param string $app |
|
| 90 | + * @throws Exception |
|
| 91 | + * @deprecated 27.0.0 use IAppManager::loadApp |
|
| 92 | + */ |
|
| 93 | + public static function loadApp(string $app): void { |
|
| 94 | + \OC::$server->get(IAppManager::class)->loadApp($app); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @internal |
|
| 99 | + * @param string $app |
|
| 100 | + * @param string $path |
|
| 101 | + * @param bool $force |
|
| 102 | + */ |
|
| 103 | + public static function registerAutoloading(string $app, string $path, bool $force = false) { |
|
| 104 | + $key = $app . '-' . $path; |
|
| 105 | + if (!$force && isset(self::$alreadyRegistered[$key])) { |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + self::$alreadyRegistered[$key] = true; |
|
| 110 | + |
|
| 111 | + // Register on PSR-4 composer autoloader |
|
| 112 | + $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 113 | + \OC::$server->registerNamespace($app, $appNamespace); |
|
| 114 | + |
|
| 115 | + if (file_exists($path . '/composer/autoload.php')) { |
|
| 116 | + require_once $path . '/composer/autoload.php'; |
|
| 117 | + } else { |
|
| 118 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Register Test namespace only when testing |
|
| 122 | + if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 123 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * check if an app is of a specific type |
|
| 129 | + * |
|
| 130 | + * @param string $app |
|
| 131 | + * @param array $types |
|
| 132 | + * @return bool |
|
| 133 | + * @deprecated 27.0.0 use IAppManager::isType |
|
| 134 | + */ |
|
| 135 | + public static function isType(string $app, array $types): bool { |
|
| 136 | + return \OC::$server->get(IAppManager::class)->isType($app, $types); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * read app types from info.xml and cache them in the database |
|
| 141 | + */ |
|
| 142 | + public static function setAppTypes(string $app) { |
|
| 143 | + $appManager = \OC::$server->getAppManager(); |
|
| 144 | + $appData = $appManager->getAppInfo($app); |
|
| 145 | + if (!is_array($appData)) { |
|
| 146 | + return; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if (isset($appData['types'])) { |
|
| 150 | + $appTypes = implode(',', $appData['types']); |
|
| 151 | + } else { |
|
| 152 | + $appTypes = ''; |
|
| 153 | + $appData['types'] = []; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $config = \OC::$server->getConfig(); |
|
| 157 | + $config->setAppValue($app, 'types', $appTypes); |
|
| 158 | + |
|
| 159 | + if ($appManager->hasProtectedAppType($appData['types'])) { |
|
| 160 | + $enabled = $config->getAppValue($app, 'enabled', 'yes'); |
|
| 161 | + if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 162 | + $config->setAppValue($app, 'enabled', 'yes'); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Returns apps enabled for the current user. |
|
| 169 | + * |
|
| 170 | + * @param bool $forceRefresh whether to refresh the cache |
|
| 171 | + * @param bool $all whether to return apps for all users, not only the |
|
| 172 | + * currently logged in one |
|
| 173 | + * @return list<string> |
|
| 174 | + */ |
|
| 175 | + public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array { |
|
| 176 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 177 | + return []; |
|
| 178 | + } |
|
| 179 | + // in incognito mode or when logged out, $user will be false, |
|
| 180 | + // which is also the case during an upgrade |
|
| 181 | + $appManager = \OC::$server->getAppManager(); |
|
| 182 | + if ($all) { |
|
| 183 | + $user = null; |
|
| 184 | + } else { |
|
| 185 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if (is_null($user)) { |
|
| 189 | + $apps = $appManager->getEnabledApps(); |
|
| 190 | + } else { |
|
| 191 | + $apps = $appManager->getEnabledAppsForUser($user); |
|
| 192 | + } |
|
| 193 | + $apps = array_filter($apps, function ($app) { |
|
| 194 | + return $app !== 'files';//we add this manually |
|
| 195 | + }); |
|
| 196 | + sort($apps); |
|
| 197 | + array_unshift($apps, 'files'); |
|
| 198 | + return $apps; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * enables an app |
|
| 203 | + * |
|
| 204 | + * @param string $appId |
|
| 205 | + * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 206 | + * @throws \Exception |
|
| 207 | + * @return void |
|
| 208 | + * |
|
| 209 | + * This function set an app as enabled in appconfig. |
|
| 210 | + */ |
|
| 211 | + public function enable(string $appId, |
|
| 212 | + array $groups = []) { |
|
| 213 | + // Check if app is already downloaded |
|
| 214 | + /** @var Installer $installer */ |
|
| 215 | + $installer = Server::get(Installer::class); |
|
| 216 | + $isDownloaded = $installer->isDownloaded($appId); |
|
| 217 | + |
|
| 218 | + if (!$isDownloaded) { |
|
| 219 | + $installer->downloadApp($appId); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $installer->installApp($appId); |
|
| 223 | + |
|
| 224 | + $appManager = \OC::$server->getAppManager(); |
|
| 225 | + if ($groups !== []) { |
|
| 226 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 227 | + $groupsList = []; |
|
| 228 | + foreach ($groups as $group) { |
|
| 229 | + $groupItem = $groupManager->get($group); |
|
| 230 | + if ($groupItem instanceof \OCP\IGroup) { |
|
| 231 | + $groupsList[] = $groupManager->get($group); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + $appManager->enableAppForGroups($appId, $groupsList); |
|
| 235 | + } else { |
|
| 236 | + $appManager->enableApp($appId); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Get the path where to install apps |
|
| 242 | + */ |
|
| 243 | + public static function getInstallPath(): ?string { |
|
| 244 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 245 | + if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 246 | + return $dir['path']; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + Server::get(LoggerInterface::class)->error('No application directories are marked as writable.', ['app' => 'core']); |
|
| 251 | + return null; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Find the apps root for an app id. |
|
| 257 | + * |
|
| 258 | + * If multiple copies are found, the apps root the latest version is returned. |
|
| 259 | + * |
|
| 260 | + * @param string $appId |
|
| 261 | + * @param bool $ignoreCache ignore cache and rebuild it |
|
| 262 | + * @return false|array{path: string, url: string} the apps root shape |
|
| 263 | + */ |
|
| 264 | + public static function findAppInDirectories(string $appId, bool $ignoreCache = false) { |
|
| 265 | + $sanitizedAppId = self::cleanAppId($appId); |
|
| 266 | + if ($sanitizedAppId !== $appId) { |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + static $app_dir = []; |
|
| 270 | + |
|
| 271 | + if (isset($app_dir[$appId]) && !$ignoreCache) { |
|
| 272 | + return $app_dir[$appId]; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + $possibleApps = []; |
|
| 276 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 277 | + if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 278 | + $possibleApps[] = $dir; |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + if (empty($possibleApps)) { |
|
| 283 | + return false; |
|
| 284 | + } elseif (count($possibleApps) === 1) { |
|
| 285 | + $dir = array_shift($possibleApps); |
|
| 286 | + $app_dir[$appId] = $dir; |
|
| 287 | + return $dir; |
|
| 288 | + } else { |
|
| 289 | + $versionToLoad = []; |
|
| 290 | + foreach ($possibleApps as $possibleApp) { |
|
| 291 | + $version = self::getAppVersionByPath($possibleApp['path'] . '/' . $appId); |
|
| 292 | + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 293 | + $versionToLoad = [ |
|
| 294 | + 'dir' => $possibleApp, |
|
| 295 | + 'version' => $version, |
|
| 296 | + ]; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + $app_dir[$appId] = $versionToLoad['dir']; |
|
| 300 | + return $versionToLoad['dir']; |
|
| 301 | + //TODO - write test |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Get the directory for the given app. |
|
| 307 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 308 | + * |
|
| 309 | + * @psalm-taint-specialize |
|
| 310 | + * |
|
| 311 | + * @param string $appId |
|
| 312 | + * @param bool $refreshAppPath should be set to true only during install/upgrade |
|
| 313 | + * @return string|false |
|
| 314 | + * @deprecated 11.0.0 use Server::get(IAppManager)->getAppPath() |
|
| 315 | + */ |
|
| 316 | + public static function getAppPath(string $appId, bool $refreshAppPath = false) { |
|
| 317 | + $appId = self::cleanAppId($appId); |
|
| 318 | + if ($appId === '') { |
|
| 319 | + return false; |
|
| 320 | + } elseif ($appId === 'core') { |
|
| 321 | + return __DIR__ . '/../../../core'; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if (($dir = self::findAppInDirectories($appId, $refreshAppPath)) != false) { |
|
| 325 | + return $dir['path'] . '/' . $appId; |
|
| 326 | + } |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get the path for the given app on the access |
|
| 332 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 333 | + * |
|
| 334 | + * @param string $appId |
|
| 335 | + * @return string|false |
|
| 336 | + * @deprecated 18.0.0 use \OC::$server->getAppManager()->getAppWebPath() |
|
| 337 | + */ |
|
| 338 | + public static function getAppWebPath(string $appId) { |
|
| 339 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 340 | + return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 341 | + } |
|
| 342 | + return false; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * get app's version based on it's path |
|
| 347 | + * |
|
| 348 | + * @param string $path |
|
| 349 | + * @return string |
|
| 350 | + */ |
|
| 351 | + public static function getAppVersionByPath(string $path): string { |
|
| 352 | + $infoFile = $path . '/appinfo/info.xml'; |
|
| 353 | + $appData = Server::get(IAppManager::class)->getAppInfoByPath($infoFile); |
|
| 354 | + return $appData['version'] ?? ''; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * get the id of loaded app |
|
| 359 | + * |
|
| 360 | + * @return string |
|
| 361 | + */ |
|
| 362 | + public static function getCurrentApp(): string { |
|
| 363 | + if (\OC::$CLI) { |
|
| 364 | + return ''; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + $request = \OC::$server->getRequest(); |
|
| 368 | + $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 369 | + $topFolder = substr($script, 0, strpos($script, '/') ?: 0); |
|
| 370 | + if (empty($topFolder)) { |
|
| 371 | + try { |
|
| 372 | + $path_info = $request->getPathInfo(); |
|
| 373 | + } catch (Exception $e) { |
|
| 374 | + // Can happen from unit tests because the script name is `./vendor/bin/phpunit` or something a like then. |
|
| 375 | + \OC::$server->get(LoggerInterface::class)->error('Failed to detect current app from script path', ['exception' => $e]); |
|
| 376 | + return ''; |
|
| 377 | + } |
|
| 378 | + if ($path_info) { |
|
| 379 | + $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + if ($topFolder == 'apps') { |
|
| 383 | + $length = strlen($topFolder); |
|
| 384 | + return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: ''; |
|
| 385 | + } else { |
|
| 386 | + return $topFolder; |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @param array $entry |
|
| 392 | + * @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface |
|
| 393 | + */ |
|
| 394 | + public static function registerLogIn(array $entry) { |
|
| 395 | + Server::get(LoggerInterface::class)->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface'); |
|
| 396 | + self::$altLogin[] = $entry; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * @return array |
|
| 401 | + */ |
|
| 402 | + public static function getAlternativeLogIns(): array { |
|
| 403 | + /** @var Coordinator $bootstrapCoordinator */ |
|
| 404 | + $bootstrapCoordinator = Server::get(Coordinator::class); |
|
| 405 | + |
|
| 406 | + foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) { |
|
| 407 | + if (!in_array(IAlternativeLogin::class, class_implements($registration->getService()), true)) { |
|
| 408 | + Server::get(LoggerInterface::class)->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [ |
|
| 409 | + 'option' => $registration->getService(), |
|
| 410 | + 'interface' => IAlternativeLogin::class, |
|
| 411 | + 'app' => $registration->getAppId(), |
|
| 412 | + ]); |
|
| 413 | + continue; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + try { |
|
| 417 | + /** @var IAlternativeLogin $provider */ |
|
| 418 | + $provider = Server::get($registration->getService()); |
|
| 419 | + } catch (ContainerExceptionInterface $e) { |
|
| 420 | + Server::get(LoggerInterface::class)->error('Alternative login option {option} can not be initialized.', |
|
| 421 | + [ |
|
| 422 | + 'exception' => $e, |
|
| 423 | + 'option' => $registration->getService(), |
|
| 424 | + 'app' => $registration->getAppId(), |
|
| 425 | + ]); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + try { |
|
| 429 | + $provider->load(); |
|
| 430 | + |
|
| 431 | + self::$altLogin[] = [ |
|
| 432 | + 'name' => $provider->getLabel(), |
|
| 433 | + 'href' => $provider->getLink(), |
|
| 434 | + 'class' => $provider->getClass(), |
|
| 435 | + ]; |
|
| 436 | + } catch (Throwable $e) { |
|
| 437 | + Server::get(LoggerInterface::class)->error('Alternative login option {option} had an error while loading.', |
|
| 438 | + [ |
|
| 439 | + 'exception' => $e, |
|
| 440 | + 'option' => $registration->getService(), |
|
| 441 | + 'app' => $registration->getAppId(), |
|
| 442 | + ]); |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + return self::$altLogin; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * get a list of all apps in the apps folder |
|
| 451 | + * |
|
| 452 | + * @return string[] an array of app names (string IDs) |
|
| 453 | + * @deprecated 31.0.0 Use IAppManager::getAllAppsInAppsFolders instead |
|
| 454 | + */ |
|
| 455 | + public static function getAllApps(): array { |
|
| 456 | + return Server::get(IAppManager::class)->getAllAppsInAppsFolders(); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * List all supported apps |
|
| 461 | + * |
|
| 462 | + * @deprecated 32.0.0 Use \OCP\Support\Subscription\IRegistry::delegateGetSupportedApps instead |
|
| 463 | + */ |
|
| 464 | + public function getSupportedApps(): array { |
|
| 465 | + $subscriptionRegistry = Server::get(\OCP\Support\Subscription\IRegistry::class); |
|
| 466 | + $supportedApps = $subscriptionRegistry->delegateGetSupportedApps(); |
|
| 467 | + return $supportedApps; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * List all apps, this is used in apps.php |
|
| 472 | + * |
|
| 473 | + * @return array |
|
| 474 | + */ |
|
| 475 | + public function listAllApps(): array { |
|
| 476 | + $appManager = \OC::$server->getAppManager(); |
|
| 477 | + |
|
| 478 | + $installedApps = $appManager->getAllAppsInAppsFolders(); |
|
| 479 | + //we don't want to show configuration for these |
|
| 480 | + $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 481 | + $appList = []; |
|
| 482 | + $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 483 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 484 | + $supportedApps = $this->getSupportedApps(); |
|
| 485 | + |
|
| 486 | + foreach ($installedApps as $app) { |
|
| 487 | + if (!in_array($app, $blacklist)) { |
|
| 488 | + $info = $appManager->getAppInfo($app, false, $langCode); |
|
| 489 | + if (!is_array($info)) { |
|
| 490 | + Server::get(LoggerInterface::class)->error('Could not read app info file for app "' . $app . '"', ['app' => 'core']); |
|
| 491 | + continue; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + if (!isset($info['name'])) { |
|
| 495 | + Server::get(LoggerInterface::class)->error('App id "' . $app . '" has no name in appinfo', ['app' => 'core']); |
|
| 496 | + continue; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no'); |
|
| 500 | + $info['groups'] = null; |
|
| 501 | + if ($enabled === 'yes') { |
|
| 502 | + $active = true; |
|
| 503 | + } elseif ($enabled === 'no') { |
|
| 504 | + $active = false; |
|
| 505 | + } else { |
|
| 506 | + $active = true; |
|
| 507 | + $info['groups'] = $enabled; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + $info['active'] = $active; |
|
| 511 | + |
|
| 512 | + if ($appManager->isShipped($app)) { |
|
| 513 | + $info['internal'] = true; |
|
| 514 | + $info['level'] = self::officialApp; |
|
| 515 | + $info['removable'] = false; |
|
| 516 | + } else { |
|
| 517 | + $info['internal'] = false; |
|
| 518 | + $info['removable'] = true; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + if (in_array($app, $supportedApps)) { |
|
| 522 | + $info['level'] = self::supportedApp; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + $appPath = self::getAppPath($app); |
|
| 526 | + if ($appPath !== false) { |
|
| 527 | + $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 528 | + if (file_exists($appIcon)) { |
|
| 529 | + $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 530 | + $info['previewAsIcon'] = true; |
|
| 531 | + } else { |
|
| 532 | + $appIcon = $appPath . '/img/app.svg'; |
|
| 533 | + if (file_exists($appIcon)) { |
|
| 534 | + $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 535 | + $info['previewAsIcon'] = true; |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + // fix documentation |
|
| 540 | + if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 541 | + foreach ($info['documentation'] as $key => $url) { |
|
| 542 | + // If it is not an absolute URL we assume it is a key |
|
| 543 | + // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 544 | + if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 545 | + $url = $urlGenerator->linkToDocs($url); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + $info['documentation'][$key] = $url; |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $info['version'] = $appManager->getAppVersion($app); |
|
| 553 | + $appList[] = $info; |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + return $appList; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + public static function shouldUpgrade(string $app): bool { |
|
| 561 | + $versions = self::getAppVersions(); |
|
| 562 | + $currentVersion = Server::get(\OCP\App\IAppManager::class)->getAppVersion($app); |
|
| 563 | + if ($currentVersion && isset($versions[$app])) { |
|
| 564 | + $installedVersion = $versions[$app]; |
|
| 565 | + if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 566 | + return true; |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + return false; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Adjust the number of version parts of $version1 to match |
|
| 574 | + * the number of version parts of $version2. |
|
| 575 | + * |
|
| 576 | + * @param string $version1 version to adjust |
|
| 577 | + * @param string $version2 version to take the number of parts from |
|
| 578 | + * @return string shortened $version1 |
|
| 579 | + */ |
|
| 580 | + private static function adjustVersionParts(string $version1, string $version2): string { |
|
| 581 | + $version1 = explode('.', $version1); |
|
| 582 | + $version2 = explode('.', $version2); |
|
| 583 | + // reduce $version1 to match the number of parts in $version2 |
|
| 584 | + while (count($version1) > count($version2)) { |
|
| 585 | + array_pop($version1); |
|
| 586 | + } |
|
| 587 | + // if $version1 does not have enough parts, add some |
|
| 588 | + while (count($version1) < count($version2)) { |
|
| 589 | + $version1[] = '0'; |
|
| 590 | + } |
|
| 591 | + return implode('.', $version1); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Check whether the current Nextcloud version matches the given |
|
| 596 | + * application's version requirements. |
|
| 597 | + * |
|
| 598 | + * The comparison is made based on the number of parts that the |
|
| 599 | + * app info version has. For example for ownCloud 6.0.3 if the |
|
| 600 | + * app info version is expecting version 6.0, the comparison is |
|
| 601 | + * made on the first two parts of the ownCloud version. |
|
| 602 | + * This means that it's possible to specify "requiremin" => 6 |
|
| 603 | + * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 604 | + * |
|
| 605 | + * @param string $ocVersion Nextcloud version to check against |
|
| 606 | + * @param array $appInfo app info (from xml) |
|
| 607 | + * |
|
| 608 | + * @return boolean true if compatible, otherwise false |
|
| 609 | + */ |
|
| 610 | + public static function isAppCompatible(string $ocVersion, array $appInfo, bool $ignoreMax = false): bool { |
|
| 611 | + $requireMin = ''; |
|
| 612 | + $requireMax = ''; |
|
| 613 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 614 | + $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 615 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 616 | + $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 617 | + } elseif (isset($appInfo['requiremin'])) { |
|
| 618 | + $requireMin = $appInfo['requiremin']; |
|
| 619 | + } elseif (isset($appInfo['require'])) { |
|
| 620 | + $requireMin = $appInfo['require']; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 624 | + $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 625 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 626 | + $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 627 | + } elseif (isset($appInfo['requiremax'])) { |
|
| 628 | + $requireMax = $appInfo['requiremax']; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + if (!empty($requireMin) |
|
| 632 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 633 | + ) { |
|
| 634 | + return false; |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + if (!$ignoreMax && !empty($requireMax) |
|
| 638 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 639 | + ) { |
|
| 640 | + return false; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + return true; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * get the installed version of all apps |
|
| 648 | + * @deprecated 32.0.0 Use IAppManager::getAppInstalledVersions or IAppConfig::getAppInstalledVersions instead |
|
| 649 | + */ |
|
| 650 | + public static function getAppVersions(): array { |
|
| 651 | + return Server::get(IAppConfig::class)->getAppInstalledVersions(); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * update the database for the app and call the update script |
|
| 656 | + * |
|
| 657 | + * @param string $appId |
|
| 658 | + * @return bool |
|
| 659 | + */ |
|
| 660 | + public static function updateApp(string $appId): bool { |
|
| 661 | + // for apps distributed with core, we refresh app path in case the downloaded version |
|
| 662 | + // have been installed in custom apps and not in the default path |
|
| 663 | + $appPath = self::getAppPath($appId, true); |
|
| 664 | + if ($appPath === false) { |
|
| 665 | + return false; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + if (is_file($appPath . '/appinfo/database.xml')) { |
|
| 669 | + Server::get(LoggerInterface::class)->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId); |
|
| 670 | + return false; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + \OC::$server->getAppManager()->clearAppsCache(); |
|
| 674 | + $l = \OC::$server->getL10N('core'); |
|
| 675 | + $appData = Server::get(\OCP\App\IAppManager::class)->getAppInfo($appId, false, $l->getLanguageCode()); |
|
| 676 | + |
|
| 677 | + $ignoreMaxApps = \OC::$server->getConfig()->getSystemValue('app_install_overwrite', []); |
|
| 678 | + $ignoreMax = in_array($appId, $ignoreMaxApps, true); |
|
| 679 | + \OC_App::checkAppDependencies( |
|
| 680 | + \OC::$server->getConfig(), |
|
| 681 | + $l, |
|
| 682 | + $appData, |
|
| 683 | + $ignoreMax |
|
| 684 | + ); |
|
| 685 | + |
|
| 686 | + self::registerAutoloading($appId, $appPath, true); |
|
| 687 | + self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 688 | + |
|
| 689 | + $ms = new MigrationService($appId, \OC::$server->get(\OC\DB\Connection::class)); |
|
| 690 | + $ms->migrate(); |
|
| 691 | + |
|
| 692 | + self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 693 | + self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 694 | + // update appversion in app manager |
|
| 695 | + \OC::$server->getAppManager()->clearAppsCache(); |
|
| 696 | + \OC::$server->getAppManager()->getAppVersion($appId, false); |
|
| 697 | + |
|
| 698 | + self::setupBackgroundJobs($appData['background-jobs']); |
|
| 699 | + |
|
| 700 | + //set remote/public handlers |
|
| 701 | + if (array_key_exists('ocsid', $appData)) { |
|
| 702 | + \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 703 | + } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 704 | + \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 705 | + } |
|
| 706 | + foreach ($appData['remote'] as $name => $path) { |
|
| 707 | + \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 708 | + } |
|
| 709 | + foreach ($appData['public'] as $name => $path) { |
|
| 710 | + \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + self::setAppTypes($appId); |
|
| 714 | + |
|
| 715 | + $version = Server::get(\OCP\App\IAppManager::class)->getAppVersion($appId); |
|
| 716 | + \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version); |
|
| 717 | + |
|
| 718 | + // migrate eventual new config keys in the process |
|
| 719 | + /** @psalm-suppress InternalMethod */ |
|
| 720 | + Server::get(ConfigManager::class)->migrateConfigLexiconKeys($appId); |
|
| 721 | + |
|
| 722 | + \OC::$server->get(IEventDispatcher::class)->dispatchTyped(new AppUpdateEvent($appId)); |
|
| 723 | + \OC::$server->get(IEventDispatcher::class)->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 724 | + ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 725 | + )); |
|
| 726 | + |
|
| 727 | + return true; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * @param string $appId |
|
| 732 | + * @param string[] $steps |
|
| 733 | + * @throws \OC\NeedsUpdateException |
|
| 734 | + */ |
|
| 735 | + public static function executeRepairSteps(string $appId, array $steps) { |
|
| 736 | + if (empty($steps)) { |
|
| 737 | + return; |
|
| 738 | + } |
|
| 739 | + // load the app |
|
| 740 | + self::loadApp($appId); |
|
| 741 | + |
|
| 742 | + $dispatcher = Server::get(IEventDispatcher::class); |
|
| 743 | + |
|
| 744 | + // load the steps |
|
| 745 | + $r = Server::get(Repair::class); |
|
| 746 | + foreach ($steps as $step) { |
|
| 747 | + try { |
|
| 748 | + $r->addStep($step); |
|
| 749 | + } catch (Exception $ex) { |
|
| 750 | + $dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage())); |
|
| 751 | + logger('core')->error('Failed to add app migration step ' . $step, ['exception' => $ex]); |
|
| 752 | + } |
|
| 753 | + } |
|
| 754 | + // run the steps |
|
| 755 | + $r->run(); |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + public static function setupBackgroundJobs(array $jobs) { |
|
| 759 | + $queue = \OC::$server->getJobList(); |
|
| 760 | + foreach ($jobs as $job) { |
|
| 761 | + $queue->add($job); |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * @param string $appId |
|
| 767 | + * @param string[] $steps |
|
| 768 | + */ |
|
| 769 | + private static function setupLiveMigrations(string $appId, array $steps) { |
|
| 770 | + $queue = \OC::$server->getJobList(); |
|
| 771 | + foreach ($steps as $step) { |
|
| 772 | + $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 773 | + 'app' => $appId, |
|
| 774 | + 'step' => $step]); |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + protected static function findBestL10NOption(array $options, string $lang): string { |
|
| 779 | + // only a single option |
|
| 780 | + if (isset($options['@value'])) { |
|
| 781 | + return $options['@value']; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + $fallback = $similarLangFallback = $englishFallback = false; |
|
| 785 | + |
|
| 786 | + $lang = strtolower($lang); |
|
| 787 | + $similarLang = $lang; |
|
| 788 | + if (strpos($similarLang, '_')) { |
|
| 789 | + // For "de_DE" we want to find "de" and the other way around |
|
| 790 | + $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + foreach ($options as $option) { |
|
| 794 | + if (is_array($option)) { |
|
| 795 | + if ($fallback === false) { |
|
| 796 | + $fallback = $option['@value']; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + if (!isset($option['@attributes']['lang'])) { |
|
| 800 | + continue; |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 804 | + if ($attributeLang === $lang) { |
|
| 805 | + return $option['@value']; |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + if ($attributeLang === $similarLang) { |
|
| 809 | + $similarLangFallback = $option['@value']; |
|
| 810 | + } elseif (str_starts_with($attributeLang, $similarLang . '_')) { |
|
| 811 | + if ($similarLangFallback === false) { |
|
| 812 | + $similarLangFallback = $option['@value']; |
|
| 813 | + } |
|
| 814 | + } |
|
| 815 | + } else { |
|
| 816 | + $englishFallback = $option; |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + if ($similarLangFallback !== false) { |
|
| 821 | + return $similarLangFallback; |
|
| 822 | + } elseif ($englishFallback !== false) { |
|
| 823 | + return $englishFallback; |
|
| 824 | + } |
|
| 825 | + return (string)$fallback; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * parses the app data array and enhanced the 'description' value |
|
| 830 | + * |
|
| 831 | + * @param array $data the app data |
|
| 832 | + * @param string $lang |
|
| 833 | + * @return array improved app data |
|
| 834 | + */ |
|
| 835 | + public static function parseAppInfo(array $data, $lang = null): array { |
|
| 836 | + if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 837 | + $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 838 | + } |
|
| 839 | + if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 840 | + $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 841 | + } |
|
| 842 | + if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 843 | + $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 844 | + } elseif (isset($data['description']) && is_string($data['description'])) { |
|
| 845 | + $data['description'] = trim($data['description']); |
|
| 846 | + } else { |
|
| 847 | + $data['description'] = ''; |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + return $data; |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + /** |
|
| 854 | + * @param \OCP\IConfig $config |
|
| 855 | + * @param \OCP\IL10N $l |
|
| 856 | + * @param array $info |
|
| 857 | + * @throws \Exception |
|
| 858 | + */ |
|
| 859 | + public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info, bool $ignoreMax) { |
|
| 860 | + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 861 | + $missing = $dependencyAnalyzer->analyze($info, $ignoreMax); |
|
| 862 | + if (!empty($missing)) { |
|
| 863 | + $missingMsg = implode(PHP_EOL, $missing); |
|
| 864 | + throw new \Exception( |
|
| 865 | + $l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s', |
|
| 866 | + [$info['name'], $missingMsg] |
|
| 867 | + ) |
|
| 868 | + ); |
|
| 869 | + } |
|
| 870 | + } |
|
| 871 | 871 | } |
@@ -250,1462 +250,1462 @@ |
||
| 250 | 250 | * TODO: hookup all manager classes |
| 251 | 251 | */ |
| 252 | 252 | class Server extends ServerContainer implements IServerContainer { |
| 253 | - /** @var string */ |
|
| 254 | - private $webRoot; |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * @param string $webRoot |
|
| 258 | - * @param \OC\Config $config |
|
| 259 | - */ |
|
| 260 | - public function __construct($webRoot, \OC\Config $config) { |
|
| 261 | - parent::__construct(); |
|
| 262 | - $this->webRoot = $webRoot; |
|
| 263 | - |
|
| 264 | - // To find out if we are running from CLI or not |
|
| 265 | - $this->registerParameter('isCLI', \OC::$CLI); |
|
| 266 | - $this->registerParameter('serverRoot', \OC::$SERVERROOT); |
|
| 267 | - |
|
| 268 | - $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
| 269 | - return $c; |
|
| 270 | - }); |
|
| 271 | - $this->registerService(\OCP\IServerContainer::class, function (ContainerInterface $c) { |
|
| 272 | - return $c; |
|
| 273 | - }); |
|
| 274 | - |
|
| 275 | - $this->registerAlias(\OCP\Calendar\IManager::class, \OC\Calendar\Manager::class); |
|
| 276 | - |
|
| 277 | - $this->registerAlias(\OCP\Calendar\Resource\IManager::class, \OC\Calendar\Resource\Manager::class); |
|
| 278 | - |
|
| 279 | - $this->registerAlias(\OCP\Calendar\Room\IManager::class, \OC\Calendar\Room\Manager::class); |
|
| 280 | - |
|
| 281 | - $this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class); |
|
| 282 | - |
|
| 283 | - $this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class); |
|
| 284 | - $this->registerAlias(ITemplateManager::class, TemplateManager::class); |
|
| 285 | - $this->registerAlias(\OCP\Template\ITemplateManager::class, \OC\Template\TemplateManager::class); |
|
| 286 | - |
|
| 287 | - $this->registerAlias(IActionFactory::class, ActionFactory::class); |
|
| 288 | - |
|
| 289 | - $this->registerService(View::class, function (Server $c) { |
|
| 290 | - return new View(); |
|
| 291 | - }, false); |
|
| 292 | - |
|
| 293 | - $this->registerService(IPreview::class, function (ContainerInterface $c) { |
|
| 294 | - return new PreviewManager( |
|
| 295 | - $c->get(\OCP\IConfig::class), |
|
| 296 | - $c->get(IRootFolder::class), |
|
| 297 | - new \OC\Preview\Storage\Root( |
|
| 298 | - $c->get(IRootFolder::class), |
|
| 299 | - $c->get(SystemConfig::class) |
|
| 300 | - ), |
|
| 301 | - $c->get(IEventDispatcher::class), |
|
| 302 | - $c->get(GeneratorHelper::class), |
|
| 303 | - $c->get(ISession::class)->get('user_id'), |
|
| 304 | - $c->get(Coordinator::class), |
|
| 305 | - $c->get(IServerContainer::class), |
|
| 306 | - $c->get(IBinaryFinder::class), |
|
| 307 | - $c->get(IMagickSupport::class) |
|
| 308 | - ); |
|
| 309 | - }); |
|
| 310 | - $this->registerAlias(IMimeIconProvider::class, MimeIconProvider::class); |
|
| 311 | - |
|
| 312 | - $this->registerService(\OC\Preview\Watcher::class, function (ContainerInterface $c) { |
|
| 313 | - return new \OC\Preview\Watcher( |
|
| 314 | - new \OC\Preview\Storage\Root( |
|
| 315 | - $c->get(IRootFolder::class), |
|
| 316 | - $c->get(SystemConfig::class) |
|
| 317 | - ) |
|
| 318 | - ); |
|
| 319 | - }); |
|
| 320 | - |
|
| 321 | - $this->registerService(IProfiler::class, function (Server $c) { |
|
| 322 | - return new Profiler($c->get(SystemConfig::class)); |
|
| 323 | - }); |
|
| 324 | - |
|
| 325 | - $this->registerService(Encryption\Manager::class, function (Server $c): Encryption\Manager { |
|
| 326 | - $view = new View(); |
|
| 327 | - $util = new Encryption\Util( |
|
| 328 | - $view, |
|
| 329 | - $c->get(IUserManager::class), |
|
| 330 | - $c->get(IGroupManager::class), |
|
| 331 | - $c->get(\OCP\IConfig::class) |
|
| 332 | - ); |
|
| 333 | - return new Encryption\Manager( |
|
| 334 | - $c->get(\OCP\IConfig::class), |
|
| 335 | - $c->get(LoggerInterface::class), |
|
| 336 | - $c->getL10N('core'), |
|
| 337 | - new View(), |
|
| 338 | - $util, |
|
| 339 | - new ArrayCache() |
|
| 340 | - ); |
|
| 341 | - }); |
|
| 342 | - $this->registerAlias(\OCP\Encryption\IManager::class, Encryption\Manager::class); |
|
| 343 | - |
|
| 344 | - $this->registerService(IFile::class, function (ContainerInterface $c) { |
|
| 345 | - $util = new Encryption\Util( |
|
| 346 | - new View(), |
|
| 347 | - $c->get(IUserManager::class), |
|
| 348 | - $c->get(IGroupManager::class), |
|
| 349 | - $c->get(\OCP\IConfig::class) |
|
| 350 | - ); |
|
| 351 | - return new Encryption\File( |
|
| 352 | - $util, |
|
| 353 | - $c->get(IRootFolder::class), |
|
| 354 | - $c->get(\OCP\Share\IManager::class) |
|
| 355 | - ); |
|
| 356 | - }); |
|
| 357 | - |
|
| 358 | - $this->registerService(IStorage::class, function (ContainerInterface $c) { |
|
| 359 | - $view = new View(); |
|
| 360 | - $util = new Encryption\Util( |
|
| 361 | - $view, |
|
| 362 | - $c->get(IUserManager::class), |
|
| 363 | - $c->get(IGroupManager::class), |
|
| 364 | - $c->get(\OCP\IConfig::class) |
|
| 365 | - ); |
|
| 366 | - |
|
| 367 | - return new Encryption\Keys\Storage( |
|
| 368 | - $view, |
|
| 369 | - $util, |
|
| 370 | - $c->get(ICrypto::class), |
|
| 371 | - $c->get(\OCP\IConfig::class) |
|
| 372 | - ); |
|
| 373 | - }); |
|
| 374 | - |
|
| 375 | - $this->registerAlias(\OCP\ITagManager::class, TagManager::class); |
|
| 376 | - |
|
| 377 | - $this->registerService('SystemTagManagerFactory', function (ContainerInterface $c) { |
|
| 378 | - /** @var \OCP\IConfig $config */ |
|
| 379 | - $config = $c->get(\OCP\IConfig::class); |
|
| 380 | - $factoryClass = $config->getSystemValue('systemtags.managerFactory', SystemTagManagerFactory::class); |
|
| 381 | - return new $factoryClass($this); |
|
| 382 | - }); |
|
| 383 | - $this->registerService(ISystemTagManager::class, function (ContainerInterface $c) { |
|
| 384 | - return $c->get('SystemTagManagerFactory')->getManager(); |
|
| 385 | - }); |
|
| 386 | - /** @deprecated 19.0.0 */ |
|
| 387 | - $this->registerDeprecatedAlias('SystemTagManager', ISystemTagManager::class); |
|
| 388 | - |
|
| 389 | - $this->registerService(ISystemTagObjectMapper::class, function (ContainerInterface $c) { |
|
| 390 | - return $c->get('SystemTagManagerFactory')->getObjectMapper(); |
|
| 391 | - }); |
|
| 392 | - $this->registerAlias(IFileAccess::class, FileAccess::class); |
|
| 393 | - $this->registerService('RootFolder', function (ContainerInterface $c) { |
|
| 394 | - $manager = \OC\Files\Filesystem::getMountManager(); |
|
| 395 | - $view = new View(); |
|
| 396 | - /** @var IUserSession $userSession */ |
|
| 397 | - $userSession = $c->get(IUserSession::class); |
|
| 398 | - $root = new Root( |
|
| 399 | - $manager, |
|
| 400 | - $view, |
|
| 401 | - $userSession->getUser(), |
|
| 402 | - $c->get(IUserMountCache::class), |
|
| 403 | - $this->get(LoggerInterface::class), |
|
| 404 | - $this->get(IUserManager::class), |
|
| 405 | - $this->get(IEventDispatcher::class), |
|
| 406 | - $this->get(ICacheFactory::class), |
|
| 407 | - ); |
|
| 408 | - |
|
| 409 | - $previewConnector = new \OC\Preview\WatcherConnector( |
|
| 410 | - $root, |
|
| 411 | - $c->get(SystemConfig::class), |
|
| 412 | - $this->get(IEventDispatcher::class) |
|
| 413 | - ); |
|
| 414 | - $previewConnector->connectWatcher(); |
|
| 415 | - |
|
| 416 | - return $root; |
|
| 417 | - }); |
|
| 418 | - $this->registerService(HookConnector::class, function (ContainerInterface $c) { |
|
| 419 | - return new HookConnector( |
|
| 420 | - $c->get(IRootFolder::class), |
|
| 421 | - new View(), |
|
| 422 | - $c->get(IEventDispatcher::class), |
|
| 423 | - $c->get(LoggerInterface::class) |
|
| 424 | - ); |
|
| 425 | - }); |
|
| 426 | - |
|
| 427 | - $this->registerService(IRootFolder::class, function (ContainerInterface $c) { |
|
| 428 | - return new LazyRoot(function () use ($c) { |
|
| 429 | - return $c->get('RootFolder'); |
|
| 430 | - }); |
|
| 431 | - }); |
|
| 432 | - |
|
| 433 | - $this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class); |
|
| 434 | - |
|
| 435 | - $this->registerService(DisplayNameCache::class, function (ContainerInterface $c) { |
|
| 436 | - return $c->get(\OC\User\Manager::class)->getDisplayNameCache(); |
|
| 437 | - }); |
|
| 438 | - |
|
| 439 | - $this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) { |
|
| 440 | - $groupManager = new \OC\Group\Manager( |
|
| 441 | - $this->get(IUserManager::class), |
|
| 442 | - $this->get(IEventDispatcher::class), |
|
| 443 | - $this->get(LoggerInterface::class), |
|
| 444 | - $this->get(ICacheFactory::class), |
|
| 445 | - $this->get(IRemoteAddress::class), |
|
| 446 | - ); |
|
| 447 | - return $groupManager; |
|
| 448 | - }); |
|
| 449 | - |
|
| 450 | - $this->registerService(Store::class, function (ContainerInterface $c) { |
|
| 451 | - $session = $c->get(ISession::class); |
|
| 452 | - if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 453 | - $tokenProvider = $c->get(IProvider::class); |
|
| 454 | - } else { |
|
| 455 | - $tokenProvider = null; |
|
| 456 | - } |
|
| 457 | - $logger = $c->get(LoggerInterface::class); |
|
| 458 | - $crypto = $c->get(ICrypto::class); |
|
| 459 | - return new Store($session, $logger, $crypto, $tokenProvider); |
|
| 460 | - }); |
|
| 461 | - $this->registerAlias(IStore::class, Store::class); |
|
| 462 | - $this->registerAlias(IProvider::class, Authentication\Token\Manager::class); |
|
| 463 | - $this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class); |
|
| 464 | - |
|
| 465 | - $this->registerService(\OC\User\Session::class, function (Server $c) { |
|
| 466 | - $manager = $c->get(IUserManager::class); |
|
| 467 | - $session = new \OC\Session\Memory(); |
|
| 468 | - $timeFactory = new TimeFactory(); |
|
| 469 | - // Token providers might require a working database. This code |
|
| 470 | - // might however be called when Nextcloud is not yet setup. |
|
| 471 | - if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 472 | - $provider = $c->get(IProvider::class); |
|
| 473 | - } else { |
|
| 474 | - $provider = null; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - $userSession = new \OC\User\Session( |
|
| 478 | - $manager, |
|
| 479 | - $session, |
|
| 480 | - $timeFactory, |
|
| 481 | - $provider, |
|
| 482 | - $c->get(\OCP\IConfig::class), |
|
| 483 | - $c->get(ISecureRandom::class), |
|
| 484 | - $c->get('LockdownManager'), |
|
| 485 | - $c->get(LoggerInterface::class), |
|
| 486 | - $c->get(IEventDispatcher::class), |
|
| 487 | - ); |
|
| 488 | - /** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */ |
|
| 489 | - $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) { |
|
| 490 | - \OC_Hook::emit('OC_User', 'pre_createUser', ['run' => true, 'uid' => $uid, 'password' => $password]); |
|
| 491 | - }); |
|
| 492 | - /** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */ |
|
| 493 | - $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) { |
|
| 494 | - /** @var \OC\User\User $user */ |
|
| 495 | - \OC_Hook::emit('OC_User', 'post_createUser', ['uid' => $user->getUID(), 'password' => $password]); |
|
| 496 | - }); |
|
| 497 | - /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
| 498 | - $userSession->listen('\OC\User', 'preDelete', function ($user) { |
|
| 499 | - /** @var \OC\User\User $user */ |
|
| 500 | - \OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $user->getUID()]); |
|
| 501 | - }); |
|
| 502 | - /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
| 503 | - $userSession->listen('\OC\User', 'postDelete', function ($user) { |
|
| 504 | - /** @var \OC\User\User $user */ |
|
| 505 | - \OC_Hook::emit('OC_User', 'post_deleteUser', ['uid' => $user->getUID()]); |
|
| 506 | - }); |
|
| 507 | - $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) { |
|
| 508 | - /** @var \OC\User\User $user */ |
|
| 509 | - \OC_Hook::emit('OC_User', 'pre_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); |
|
| 510 | - }); |
|
| 511 | - $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) { |
|
| 512 | - /** @var \OC\User\User $user */ |
|
| 513 | - \OC_Hook::emit('OC_User', 'post_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); |
|
| 514 | - }); |
|
| 515 | - $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) { |
|
| 516 | - \OC_Hook::emit('OC_User', 'pre_login', ['run' => true, 'uid' => $uid, 'password' => $password]); |
|
| 517 | - |
|
| 518 | - /** @var IEventDispatcher $dispatcher */ |
|
| 519 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 520 | - $dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password)); |
|
| 521 | - }); |
|
| 522 | - $userSession->listen('\OC\User', 'postLogin', function ($user, $loginName, $password, $isTokenLogin) { |
|
| 523 | - /** @var \OC\User\User $user */ |
|
| 524 | - \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'loginName' => $loginName, 'password' => $password, 'isTokenLogin' => $isTokenLogin]); |
|
| 525 | - |
|
| 526 | - /** @var IEventDispatcher $dispatcher */ |
|
| 527 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 528 | - $dispatcher->dispatchTyped(new UserLoggedInEvent($user, $loginName, $password, $isTokenLogin)); |
|
| 529 | - }); |
|
| 530 | - $userSession->listen('\OC\User', 'preRememberedLogin', function ($uid) { |
|
| 531 | - /** @var IEventDispatcher $dispatcher */ |
|
| 532 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 533 | - $dispatcher->dispatchTyped(new BeforeUserLoggedInWithCookieEvent($uid)); |
|
| 534 | - }); |
|
| 535 | - $userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) { |
|
| 536 | - /** @var \OC\User\User $user */ |
|
| 537 | - \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); |
|
| 538 | - |
|
| 539 | - /** @var IEventDispatcher $dispatcher */ |
|
| 540 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 541 | - $dispatcher->dispatchTyped(new UserLoggedInWithCookieEvent($user, $password)); |
|
| 542 | - }); |
|
| 543 | - $userSession->listen('\OC\User', 'logout', function ($user) { |
|
| 544 | - \OC_Hook::emit('OC_User', 'logout', []); |
|
| 545 | - |
|
| 546 | - /** @var IEventDispatcher $dispatcher */ |
|
| 547 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 548 | - $dispatcher->dispatchTyped(new BeforeUserLoggedOutEvent($user)); |
|
| 549 | - }); |
|
| 550 | - $userSession->listen('\OC\User', 'postLogout', function ($user) { |
|
| 551 | - /** @var IEventDispatcher $dispatcher */ |
|
| 552 | - $dispatcher = $this->get(IEventDispatcher::class); |
|
| 553 | - $dispatcher->dispatchTyped(new UserLoggedOutEvent($user)); |
|
| 554 | - }); |
|
| 555 | - $userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) { |
|
| 556 | - /** @var \OC\User\User $user */ |
|
| 557 | - \OC_Hook::emit('OC_User', 'changeUser', ['run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue]); |
|
| 558 | - }); |
|
| 559 | - return $userSession; |
|
| 560 | - }); |
|
| 561 | - $this->registerAlias(\OCP\IUserSession::class, \OC\User\Session::class); |
|
| 562 | - |
|
| 563 | - $this->registerAlias(\OCP\Authentication\TwoFactorAuth\IRegistry::class, \OC\Authentication\TwoFactorAuth\Registry::class); |
|
| 564 | - |
|
| 565 | - $this->registerAlias(INavigationManager::class, \OC\NavigationManager::class); |
|
| 566 | - |
|
| 567 | - $this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class); |
|
| 568 | - |
|
| 569 | - $this->registerService(\OC\SystemConfig::class, function ($c) use ($config) { |
|
| 570 | - return new \OC\SystemConfig($config); |
|
| 571 | - }); |
|
| 572 | - |
|
| 573 | - $this->registerAlias(IAppConfig::class, \OC\AppConfig::class); |
|
| 574 | - $this->registerAlias(IUserConfig::class, \OC\Config\UserConfig::class); |
|
| 575 | - $this->registerAlias(IAppManager::class, AppManager::class); |
|
| 576 | - |
|
| 577 | - $this->registerService(IFactory::class, function (Server $c) { |
|
| 578 | - return new \OC\L10N\Factory( |
|
| 579 | - $c->get(\OCP\IConfig::class), |
|
| 580 | - $c->getRequest(), |
|
| 581 | - $c->get(IUserSession::class), |
|
| 582 | - $c->get(ICacheFactory::class), |
|
| 583 | - \OC::$SERVERROOT, |
|
| 584 | - $c->get(IAppManager::class), |
|
| 585 | - ); |
|
| 586 | - }); |
|
| 587 | - |
|
| 588 | - $this->registerAlias(IURLGenerator::class, URLGenerator::class); |
|
| 589 | - |
|
| 590 | - $this->registerService(ICache::class, function ($c) { |
|
| 591 | - return new Cache\File(); |
|
| 592 | - }); |
|
| 593 | - |
|
| 594 | - $this->registerService(Factory::class, function (Server $c) { |
|
| 595 | - $profiler = $c->get(IProfiler::class); |
|
| 596 | - $arrayCacheFactory = new \OC\Memcache\Factory(fn () => '', $c->get(LoggerInterface::class), |
|
| 597 | - $profiler, |
|
| 598 | - ArrayCache::class, |
|
| 599 | - ArrayCache::class, |
|
| 600 | - ArrayCache::class |
|
| 601 | - ); |
|
| 602 | - /** @var SystemConfig $config */ |
|
| 603 | - $config = $c->get(SystemConfig::class); |
|
| 604 | - /** @var ServerVersion $serverVersion */ |
|
| 605 | - $serverVersion = $c->get(ServerVersion::class); |
|
| 606 | - |
|
| 607 | - if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { |
|
| 608 | - $logQuery = $config->getValue('log_query'); |
|
| 609 | - $prefixClosure = function () use ($logQuery, $serverVersion): ?string { |
|
| 610 | - if (!$logQuery) { |
|
| 611 | - try { |
|
| 612 | - $v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(true); |
|
| 613 | - } catch (\Doctrine\DBAL\Exception $e) { |
|
| 614 | - // Database service probably unavailable |
|
| 615 | - // Probably related to https://github.com/nextcloud/server/issues/37424 |
|
| 616 | - return null; |
|
| 617 | - } |
|
| 618 | - } else { |
|
| 619 | - // If the log_query is enabled, we can not get the app versions |
|
| 620 | - // as that does a query, which will be logged and the logging |
|
| 621 | - // depends on redis and here we are back again in the same function. |
|
| 622 | - $v = [ |
|
| 623 | - 'log_query' => 'enabled', |
|
| 624 | - ]; |
|
| 625 | - } |
|
| 626 | - $v['core'] = implode(',', $serverVersion->getVersion()); |
|
| 627 | - $version = implode(',', array_keys($v)) . implode(',', $v); |
|
| 628 | - $instanceId = \OC_Util::getInstanceId(); |
|
| 629 | - $path = \OC::$SERVERROOT; |
|
| 630 | - return md5($instanceId . '-' . $version . '-' . $path); |
|
| 631 | - }; |
|
| 632 | - return new \OC\Memcache\Factory($prefixClosure, |
|
| 633 | - $c->get(LoggerInterface::class), |
|
| 634 | - $profiler, |
|
| 635 | - /** @psalm-taint-escape callable */ |
|
| 636 | - $config->getValue('memcache.local', null), |
|
| 637 | - /** @psalm-taint-escape callable */ |
|
| 638 | - $config->getValue('memcache.distributed', null), |
|
| 639 | - /** @psalm-taint-escape callable */ |
|
| 640 | - $config->getValue('memcache.locking', null), |
|
| 641 | - /** @psalm-taint-escape callable */ |
|
| 642 | - $config->getValue('redis_log_file') |
|
| 643 | - ); |
|
| 644 | - } |
|
| 645 | - return $arrayCacheFactory; |
|
| 646 | - }); |
|
| 647 | - $this->registerAlias(ICacheFactory::class, Factory::class); |
|
| 648 | - |
|
| 649 | - $this->registerService('RedisFactory', function (Server $c) { |
|
| 650 | - $systemConfig = $c->get(SystemConfig::class); |
|
| 651 | - return new RedisFactory($systemConfig, $c->get(IEventLogger::class)); |
|
| 652 | - }); |
|
| 653 | - |
|
| 654 | - $this->registerService(\OCP\Activity\IManager::class, function (Server $c) { |
|
| 655 | - $l10n = $this->get(IFactory::class)->get('lib'); |
|
| 656 | - return new \OC\Activity\Manager( |
|
| 657 | - $c->getRequest(), |
|
| 658 | - $c->get(IUserSession::class), |
|
| 659 | - $c->get(\OCP\IConfig::class), |
|
| 660 | - $c->get(IValidator::class), |
|
| 661 | - $c->get(IRichTextFormatter::class), |
|
| 662 | - $l10n |
|
| 663 | - ); |
|
| 664 | - }); |
|
| 665 | - |
|
| 666 | - $this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) { |
|
| 667 | - return new \OC\Activity\EventMerger( |
|
| 668 | - $c->getL10N('lib') |
|
| 669 | - ); |
|
| 670 | - }); |
|
| 671 | - $this->registerAlias(IValidator::class, Validator::class); |
|
| 672 | - |
|
| 673 | - $this->registerService(AvatarManager::class, function (Server $c) { |
|
| 674 | - return new AvatarManager( |
|
| 675 | - $c->get(IUserSession::class), |
|
| 676 | - $c->get(\OC\User\Manager::class), |
|
| 677 | - $c->getAppDataDir('avatar'), |
|
| 678 | - $c->getL10N('lib'), |
|
| 679 | - $c->get(LoggerInterface::class), |
|
| 680 | - $c->get(\OCP\IConfig::class), |
|
| 681 | - $c->get(IAccountManager::class), |
|
| 682 | - $c->get(KnownUserService::class) |
|
| 683 | - ); |
|
| 684 | - }); |
|
| 685 | - |
|
| 686 | - $this->registerAlias(IAvatarManager::class, AvatarManager::class); |
|
| 687 | - |
|
| 688 | - $this->registerAlias(\OCP\Support\CrashReport\IRegistry::class, \OC\Support\CrashReport\Registry::class); |
|
| 689 | - $this->registerAlias(\OCP\Support\Subscription\IRegistry::class, \OC\Support\Subscription\Registry::class); |
|
| 690 | - $this->registerAlias(\OCP\Support\Subscription\IAssertion::class, \OC\Support\Subscription\Assertion::class); |
|
| 691 | - |
|
| 692 | - /** Only used by the PsrLoggerAdapter should not be used by apps */ |
|
| 693 | - $this->registerService(\OC\Log::class, function (Server $c) { |
|
| 694 | - $logType = $c->get(AllConfig::class)->getSystemValue('log_type', 'file'); |
|
| 695 | - $factory = new LogFactory($c, $this->get(SystemConfig::class)); |
|
| 696 | - $logger = $factory->get($logType); |
|
| 697 | - $registry = $c->get(\OCP\Support\CrashReport\IRegistry::class); |
|
| 698 | - |
|
| 699 | - return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry); |
|
| 700 | - }); |
|
| 701 | - // PSR-3 logger |
|
| 702 | - $this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class); |
|
| 703 | - |
|
| 704 | - $this->registerService(ILogFactory::class, function (Server $c) { |
|
| 705 | - return new LogFactory($c, $this->get(SystemConfig::class)); |
|
| 706 | - }); |
|
| 707 | - |
|
| 708 | - $this->registerAlias(IJobList::class, \OC\BackgroundJob\JobList::class); |
|
| 709 | - |
|
| 710 | - $this->registerService(Router::class, function (Server $c) { |
|
| 711 | - $cacheFactory = $c->get(ICacheFactory::class); |
|
| 712 | - if ($cacheFactory->isLocalCacheAvailable()) { |
|
| 713 | - $router = $c->resolve(CachingRouter::class); |
|
| 714 | - } else { |
|
| 715 | - $router = $c->resolve(Router::class); |
|
| 716 | - } |
|
| 717 | - return $router; |
|
| 718 | - }); |
|
| 719 | - $this->registerAlias(IRouter::class, Router::class); |
|
| 720 | - |
|
| 721 | - $this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) { |
|
| 722 | - $config = $c->get(\OCP\IConfig::class); |
|
| 723 | - if (ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) { |
|
| 724 | - $backend = new \OC\Security\RateLimiting\Backend\MemoryCacheBackend( |
|
| 725 | - $c->get(AllConfig::class), |
|
| 726 | - $this->get(ICacheFactory::class), |
|
| 727 | - new \OC\AppFramework\Utility\TimeFactory() |
|
| 728 | - ); |
|
| 729 | - } else { |
|
| 730 | - $backend = new \OC\Security\RateLimiting\Backend\DatabaseBackend( |
|
| 731 | - $c->get(AllConfig::class), |
|
| 732 | - $c->get(IDBConnection::class), |
|
| 733 | - new \OC\AppFramework\Utility\TimeFactory() |
|
| 734 | - ); |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - return $backend; |
|
| 738 | - }); |
|
| 739 | - |
|
| 740 | - $this->registerAlias(\OCP\Security\ISecureRandom::class, SecureRandom::class); |
|
| 741 | - $this->registerAlias(\OCP\Security\IRemoteHostValidator::class, \OC\Security\RemoteHostValidator::class); |
|
| 742 | - $this->registerAlias(IVerificationToken::class, VerificationToken::class); |
|
| 743 | - |
|
| 744 | - $this->registerAlias(ICrypto::class, Crypto::class); |
|
| 745 | - |
|
| 746 | - $this->registerAlias(IHasher::class, Hasher::class); |
|
| 747 | - |
|
| 748 | - $this->registerAlias(ICredentialsManager::class, CredentialsManager::class); |
|
| 749 | - |
|
| 750 | - $this->registerAlias(IDBConnection::class, ConnectionAdapter::class); |
|
| 751 | - $this->registerService(Connection::class, function (Server $c) { |
|
| 752 | - $systemConfig = $c->get(SystemConfig::class); |
|
| 753 | - $factory = new \OC\DB\ConnectionFactory($systemConfig, $c->get(ICacheFactory::class)); |
|
| 754 | - $type = $systemConfig->getValue('dbtype', 'sqlite'); |
|
| 755 | - if (!$factory->isValidType($type)) { |
|
| 756 | - throw new \OC\DatabaseException('Invalid database type'); |
|
| 757 | - } |
|
| 758 | - $connection = $factory->getConnection($type, []); |
|
| 759 | - return $connection; |
|
| 760 | - }); |
|
| 761 | - |
|
| 762 | - $this->registerAlias(ICertificateManager::class, CertificateManager::class); |
|
| 763 | - $this->registerAlias(IClientService::class, ClientService::class); |
|
| 764 | - $this->registerService(NegativeDnsCache::class, function (ContainerInterface $c) { |
|
| 765 | - return new NegativeDnsCache( |
|
| 766 | - $c->get(ICacheFactory::class), |
|
| 767 | - ); |
|
| 768 | - }); |
|
| 769 | - $this->registerDeprecatedAlias('HttpClientService', IClientService::class); |
|
| 770 | - $this->registerService(IEventLogger::class, function (ContainerInterface $c) { |
|
| 771 | - return new EventLogger($c->get(SystemConfig::class), $c->get(LoggerInterface::class), $c->get(Log::class)); |
|
| 772 | - }); |
|
| 773 | - |
|
| 774 | - $this->registerService(IQueryLogger::class, function (ContainerInterface $c) { |
|
| 775 | - $queryLogger = new QueryLogger(); |
|
| 776 | - if ($c->get(SystemConfig::class)->getValue('debug', false)) { |
|
| 777 | - // In debug mode, module is being activated by default |
|
| 778 | - $queryLogger->activate(); |
|
| 779 | - } |
|
| 780 | - return $queryLogger; |
|
| 781 | - }); |
|
| 782 | - |
|
| 783 | - $this->registerAlias(ITempManager::class, TempManager::class); |
|
| 784 | - $this->registerAlias(IDateTimeZone::class, DateTimeZone::class); |
|
| 785 | - |
|
| 786 | - $this->registerService(IDateTimeFormatter::class, function (Server $c) { |
|
| 787 | - $language = $c->get(\OCP\IConfig::class)->getUserValue($c->get(ISession::class)->get('user_id'), 'core', 'lang', null); |
|
| 788 | - |
|
| 789 | - return new DateTimeFormatter( |
|
| 790 | - $c->get(IDateTimeZone::class)->getTimeZone(), |
|
| 791 | - $c->getL10N('lib', $language) |
|
| 792 | - ); |
|
| 793 | - }); |
|
| 794 | - |
|
| 795 | - $this->registerService(IUserMountCache::class, function (ContainerInterface $c) { |
|
| 796 | - $mountCache = $c->get(UserMountCache::class); |
|
| 797 | - $listener = new UserMountCacheListener($mountCache); |
|
| 798 | - $listener->listen($c->get(IUserManager::class)); |
|
| 799 | - return $mountCache; |
|
| 800 | - }); |
|
| 801 | - |
|
| 802 | - $this->registerService(IMountProviderCollection::class, function (ContainerInterface $c) { |
|
| 803 | - $loader = $c->get(IStorageFactory::class); |
|
| 804 | - $mountCache = $c->get(IUserMountCache::class); |
|
| 805 | - $eventLogger = $c->get(IEventLogger::class); |
|
| 806 | - $manager = new MountProviderCollection($loader, $mountCache, $eventLogger); |
|
| 807 | - |
|
| 808 | - // builtin providers |
|
| 809 | - |
|
| 810 | - $config = $c->get(\OCP\IConfig::class); |
|
| 811 | - $logger = $c->get(LoggerInterface::class); |
|
| 812 | - $objectStoreConfig = $c->get(PrimaryObjectStoreConfig::class); |
|
| 813 | - $manager->registerProvider(new CacheMountProvider($config)); |
|
| 814 | - $manager->registerHomeProvider(new LocalHomeMountProvider()); |
|
| 815 | - $manager->registerHomeProvider(new ObjectHomeMountProvider($objectStoreConfig)); |
|
| 816 | - $manager->registerRootProvider(new RootMountProvider($objectStoreConfig, $config)); |
|
| 817 | - $manager->registerRootProvider(new ObjectStorePreviewCacheMountProvider($logger, $config)); |
|
| 818 | - |
|
| 819 | - return $manager; |
|
| 820 | - }); |
|
| 821 | - |
|
| 822 | - $this->registerService(IBus::class, function (ContainerInterface $c) { |
|
| 823 | - $busClass = $c->get(\OCP\IConfig::class)->getSystemValueString('commandbus'); |
|
| 824 | - if ($busClass) { |
|
| 825 | - [$app, $class] = explode('::', $busClass, 2); |
|
| 826 | - if ($c->get(IAppManager::class)->isEnabledForUser($app)) { |
|
| 827 | - $c->get(IAppManager::class)->loadApp($app); |
|
| 828 | - return $c->get($class); |
|
| 829 | - } else { |
|
| 830 | - throw new ServiceUnavailableException("The app providing the command bus ($app) is not enabled"); |
|
| 831 | - } |
|
| 832 | - } else { |
|
| 833 | - $jobList = $c->get(IJobList::class); |
|
| 834 | - return new CronBus($jobList); |
|
| 835 | - } |
|
| 836 | - }); |
|
| 837 | - $this->registerDeprecatedAlias('AsyncCommandBus', IBus::class); |
|
| 838 | - $this->registerAlias(ITrustedDomainHelper::class, TrustedDomainHelper::class); |
|
| 839 | - $this->registerAlias(IThrottler::class, Throttler::class); |
|
| 840 | - |
|
| 841 | - $this->registerService(\OC\Security\Bruteforce\Backend\IBackend::class, function ($c) { |
|
| 842 | - $config = $c->get(\OCP\IConfig::class); |
|
| 843 | - if (!$config->getSystemValueBool('auth.bruteforce.protection.force.database', false) |
|
| 844 | - && ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) { |
|
| 845 | - $backend = $c->get(\OC\Security\Bruteforce\Backend\MemoryCacheBackend::class); |
|
| 846 | - } else { |
|
| 847 | - $backend = $c->get(\OC\Security\Bruteforce\Backend\DatabaseBackend::class); |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - return $backend; |
|
| 851 | - }); |
|
| 852 | - |
|
| 853 | - $this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class); |
|
| 854 | - $this->registerService(Checker::class, function (ContainerInterface $c) { |
|
| 855 | - // IConfig requires a working database. This code |
|
| 856 | - // might however be called when Nextcloud is not yet setup. |
|
| 857 | - if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 858 | - $config = $c->get(\OCP\IConfig::class); |
|
| 859 | - $appConfig = $c->get(\OCP\IAppConfig::class); |
|
| 860 | - } else { |
|
| 861 | - $config = null; |
|
| 862 | - $appConfig = null; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - return new Checker( |
|
| 866 | - $c->get(ServerVersion::class), |
|
| 867 | - $c->get(EnvironmentHelper::class), |
|
| 868 | - new FileAccessHelper(), |
|
| 869 | - new AppLocator(), |
|
| 870 | - $config, |
|
| 871 | - $appConfig, |
|
| 872 | - $c->get(ICacheFactory::class), |
|
| 873 | - $c->get(IAppManager::class), |
|
| 874 | - $c->get(IMimeTypeDetector::class) |
|
| 875 | - ); |
|
| 876 | - }); |
|
| 877 | - $this->registerService(\OCP\IRequest::class, function (ContainerInterface $c) { |
|
| 878 | - if (isset($this['urlParams'])) { |
|
| 879 | - $urlParams = $this['urlParams']; |
|
| 880 | - } else { |
|
| 881 | - $urlParams = []; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - if (defined('PHPUNIT_RUN') && PHPUNIT_RUN |
|
| 885 | - && in_array('fakeinput', stream_get_wrappers()) |
|
| 886 | - ) { |
|
| 887 | - $stream = 'fakeinput://data'; |
|
| 888 | - } else { |
|
| 889 | - $stream = 'php://input'; |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - return new Request( |
|
| 893 | - [ |
|
| 894 | - 'get' => $_GET, |
|
| 895 | - 'post' => $_POST, |
|
| 896 | - 'files' => $_FILES, |
|
| 897 | - 'server' => $_SERVER, |
|
| 898 | - 'env' => $_ENV, |
|
| 899 | - 'cookies' => $_COOKIE, |
|
| 900 | - 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) |
|
| 901 | - ? $_SERVER['REQUEST_METHOD'] |
|
| 902 | - : '', |
|
| 903 | - 'urlParams' => $urlParams, |
|
| 904 | - ], |
|
| 905 | - $this->get(IRequestId::class), |
|
| 906 | - $this->get(\OCP\IConfig::class), |
|
| 907 | - $this->get(CsrfTokenManager::class), |
|
| 908 | - $stream |
|
| 909 | - ); |
|
| 910 | - }); |
|
| 911 | - |
|
| 912 | - $this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId { |
|
| 913 | - return new RequestId( |
|
| 914 | - $_SERVER['UNIQUE_ID'] ?? '', |
|
| 915 | - $this->get(ISecureRandom::class) |
|
| 916 | - ); |
|
| 917 | - }); |
|
| 918 | - |
|
| 919 | - $this->registerService(IMailer::class, function (Server $c) { |
|
| 920 | - return new Mailer( |
|
| 921 | - $c->get(\OCP\IConfig::class), |
|
| 922 | - $c->get(LoggerInterface::class), |
|
| 923 | - $c->get(Defaults::class), |
|
| 924 | - $c->get(IURLGenerator::class), |
|
| 925 | - $c->getL10N('lib'), |
|
| 926 | - $c->get(IEventDispatcher::class), |
|
| 927 | - $c->get(IFactory::class) |
|
| 928 | - ); |
|
| 929 | - }); |
|
| 930 | - |
|
| 931 | - /** @since 30.0.0 */ |
|
| 932 | - $this->registerAlias(\OCP\Mail\Provider\IManager::class, \OC\Mail\Provider\Manager::class); |
|
| 933 | - |
|
| 934 | - $this->registerService(ILDAPProviderFactory::class, function (ContainerInterface $c) { |
|
| 935 | - $config = $c->get(\OCP\IConfig::class); |
|
| 936 | - $factoryClass = $config->getSystemValue('ldapProviderFactory', null); |
|
| 937 | - if (is_null($factoryClass) || !class_exists($factoryClass)) { |
|
| 938 | - return new NullLDAPProviderFactory($this); |
|
| 939 | - } |
|
| 940 | - /** @var \OCP\LDAP\ILDAPProviderFactory $factory */ |
|
| 941 | - return new $factoryClass($this); |
|
| 942 | - }); |
|
| 943 | - $this->registerService(ILDAPProvider::class, function (ContainerInterface $c) { |
|
| 944 | - $factory = $c->get(ILDAPProviderFactory::class); |
|
| 945 | - return $factory->getLDAPProvider(); |
|
| 946 | - }); |
|
| 947 | - $this->registerService(ILockingProvider::class, function (ContainerInterface $c) { |
|
| 948 | - $ini = $c->get(IniGetWrapper::class); |
|
| 949 | - $config = $c->get(\OCP\IConfig::class); |
|
| 950 | - $ttl = $config->getSystemValueInt('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time'))); |
|
| 951 | - if ($config->getSystemValueBool('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { |
|
| 952 | - /** @var \OC\Memcache\Factory $memcacheFactory */ |
|
| 953 | - $memcacheFactory = $c->get(ICacheFactory::class); |
|
| 954 | - $memcache = $memcacheFactory->createLocking('lock'); |
|
| 955 | - if (!($memcache instanceof \OC\Memcache\NullCache)) { |
|
| 956 | - $timeFactory = $c->get(ITimeFactory::class); |
|
| 957 | - return new MemcacheLockingProvider($memcache, $timeFactory, $ttl); |
|
| 958 | - } |
|
| 959 | - return new DBLockingProvider( |
|
| 960 | - $c->get(IDBConnection::class), |
|
| 961 | - new TimeFactory(), |
|
| 962 | - $ttl, |
|
| 963 | - !\OC::$CLI |
|
| 964 | - ); |
|
| 965 | - } |
|
| 966 | - return new NoopLockingProvider(); |
|
| 967 | - }); |
|
| 968 | - |
|
| 969 | - $this->registerService(ILockManager::class, function (Server $c): LockManager { |
|
| 970 | - return new LockManager(); |
|
| 971 | - }); |
|
| 972 | - |
|
| 973 | - $this->registerAlias(ILockdownManager::class, 'LockdownManager'); |
|
| 974 | - $this->registerService(SetupManager::class, function ($c) { |
|
| 975 | - // create the setupmanager through the mount manager to resolve the cyclic dependency |
|
| 976 | - return $c->get(\OC\Files\Mount\Manager::class)->getSetupManager(); |
|
| 977 | - }); |
|
| 978 | - $this->registerAlias(IMountManager::class, \OC\Files\Mount\Manager::class); |
|
| 979 | - |
|
| 980 | - $this->registerService(IMimeTypeDetector::class, function (ContainerInterface $c) { |
|
| 981 | - return new \OC\Files\Type\Detection( |
|
| 982 | - $c->get(IURLGenerator::class), |
|
| 983 | - $c->get(LoggerInterface::class), |
|
| 984 | - \OC::$configDir, |
|
| 985 | - \OC::$SERVERROOT . '/resources/config/' |
|
| 986 | - ); |
|
| 987 | - }); |
|
| 988 | - |
|
| 989 | - $this->registerAlias(IMimeTypeLoader::class, Loader::class); |
|
| 990 | - $this->registerService(BundleFetcher::class, function () { |
|
| 991 | - return new BundleFetcher($this->getL10N('lib')); |
|
| 992 | - }); |
|
| 993 | - $this->registerAlias(\OCP\Notification\IManager::class, Manager::class); |
|
| 994 | - |
|
| 995 | - $this->registerService(CapabilitiesManager::class, function (ContainerInterface $c) { |
|
| 996 | - $manager = new CapabilitiesManager($c->get(LoggerInterface::class)); |
|
| 997 | - $manager->registerCapability(function () use ($c) { |
|
| 998 | - return new \OC\OCS\CoreCapabilities($c->get(\OCP\IConfig::class)); |
|
| 999 | - }); |
|
| 1000 | - $manager->registerCapability(function () use ($c) { |
|
| 1001 | - return $c->get(\OC\Security\Bruteforce\Capabilities::class); |
|
| 1002 | - }); |
|
| 1003 | - return $manager; |
|
| 1004 | - }); |
|
| 1005 | - |
|
| 1006 | - $this->registerService(ICommentsManager::class, function (Server $c) { |
|
| 1007 | - $config = $c->get(\OCP\IConfig::class); |
|
| 1008 | - $factoryClass = $config->getSystemValue('comments.managerFactory', CommentsManagerFactory::class); |
|
| 1009 | - /** @var \OCP\Comments\ICommentsManagerFactory $factory */ |
|
| 1010 | - $factory = new $factoryClass($this); |
|
| 1011 | - $manager = $factory->getManager(); |
|
| 1012 | - |
|
| 1013 | - $manager->registerDisplayNameResolver('user', function ($id) use ($c) { |
|
| 1014 | - $manager = $c->get(IUserManager::class); |
|
| 1015 | - $userDisplayName = $manager->getDisplayName($id); |
|
| 1016 | - if ($userDisplayName === null) { |
|
| 1017 | - $l = $c->get(IFactory::class)->get('core'); |
|
| 1018 | - return $l->t('Unknown account'); |
|
| 1019 | - } |
|
| 1020 | - return $userDisplayName; |
|
| 1021 | - }); |
|
| 1022 | - |
|
| 1023 | - return $manager; |
|
| 1024 | - }); |
|
| 1025 | - |
|
| 1026 | - $this->registerAlias(\OC_Defaults::class, 'ThemingDefaults'); |
|
| 1027 | - $this->registerService('ThemingDefaults', function (Server $c) { |
|
| 1028 | - try { |
|
| 1029 | - $classExists = class_exists('OCA\Theming\ThemingDefaults'); |
|
| 1030 | - } catch (\OCP\AutoloadNotAllowedException $e) { |
|
| 1031 | - // App disabled or in maintenance mode |
|
| 1032 | - $classExists = false; |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isEnabledForAnyone('theming') && $c->get(TrustedDomainHelper::class)->isTrustedDomain($c->getRequest()->getInsecureServerHost())) { |
|
| 1036 | - $backgroundService = new BackgroundService( |
|
| 1037 | - $c->get(IRootFolder::class), |
|
| 1038 | - $c->getAppDataDir('theming'), |
|
| 1039 | - $c->get(IAppConfig::class), |
|
| 1040 | - $c->get(\OCP\IConfig::class), |
|
| 1041 | - $c->get(ISession::class)->get('user_id'), |
|
| 1042 | - ); |
|
| 1043 | - $imageManager = new ImageManager( |
|
| 1044 | - $c->get(\OCP\IConfig::class), |
|
| 1045 | - $c->getAppDataDir('theming'), |
|
| 1046 | - $c->get(IURLGenerator::class), |
|
| 1047 | - $c->get(ICacheFactory::class), |
|
| 1048 | - $c->get(LoggerInterface::class), |
|
| 1049 | - $c->get(ITempManager::class), |
|
| 1050 | - $backgroundService, |
|
| 1051 | - ); |
|
| 1052 | - return new ThemingDefaults( |
|
| 1053 | - $c->get(\OCP\IConfig::class), |
|
| 1054 | - $c->get(\OCP\IAppConfig::class), |
|
| 1055 | - $c->getL10N('theming'), |
|
| 1056 | - $c->get(IUserSession::class), |
|
| 1057 | - $c->get(IURLGenerator::class), |
|
| 1058 | - $c->get(ICacheFactory::class), |
|
| 1059 | - new Util($c->get(ServerVersion::class), $c->get(\OCP\IConfig::class), $this->get(IAppManager::class), $c->getAppDataDir('theming'), $imageManager), |
|
| 1060 | - $imageManager, |
|
| 1061 | - $c->get(IAppManager::class), |
|
| 1062 | - $c->get(INavigationManager::class), |
|
| 1063 | - $backgroundService, |
|
| 1064 | - ); |
|
| 1065 | - } |
|
| 1066 | - return new \OC_Defaults(); |
|
| 1067 | - }); |
|
| 1068 | - $this->registerService(JSCombiner::class, function (Server $c) { |
|
| 1069 | - return new JSCombiner( |
|
| 1070 | - $c->getAppDataDir('js'), |
|
| 1071 | - $c->get(IURLGenerator::class), |
|
| 1072 | - $this->get(ICacheFactory::class), |
|
| 1073 | - $c->get(SystemConfig::class), |
|
| 1074 | - $c->get(LoggerInterface::class) |
|
| 1075 | - ); |
|
| 1076 | - }); |
|
| 1077 | - $this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class); |
|
| 1078 | - |
|
| 1079 | - $this->registerService('CryptoWrapper', function (ContainerInterface $c) { |
|
| 1080 | - // FIXME: Instantiated here due to cyclic dependency |
|
| 1081 | - $request = new Request( |
|
| 1082 | - [ |
|
| 1083 | - 'get' => $_GET, |
|
| 1084 | - 'post' => $_POST, |
|
| 1085 | - 'files' => $_FILES, |
|
| 1086 | - 'server' => $_SERVER, |
|
| 1087 | - 'env' => $_ENV, |
|
| 1088 | - 'cookies' => $_COOKIE, |
|
| 1089 | - 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) |
|
| 1090 | - ? $_SERVER['REQUEST_METHOD'] |
|
| 1091 | - : null, |
|
| 1092 | - ], |
|
| 1093 | - $c->get(IRequestId::class), |
|
| 1094 | - $c->get(\OCP\IConfig::class) |
|
| 1095 | - ); |
|
| 1096 | - |
|
| 1097 | - return new CryptoWrapper( |
|
| 1098 | - $c->get(ICrypto::class), |
|
| 1099 | - $c->get(ISecureRandom::class), |
|
| 1100 | - $request |
|
| 1101 | - ); |
|
| 1102 | - }); |
|
| 1103 | - $this->registerService(SessionStorage::class, function (ContainerInterface $c) { |
|
| 1104 | - return new SessionStorage($c->get(ISession::class)); |
|
| 1105 | - }); |
|
| 1106 | - $this->registerAlias(\OCP\Security\IContentSecurityPolicyManager::class, ContentSecurityPolicyManager::class); |
|
| 1107 | - |
|
| 1108 | - $this->registerService(IProviderFactory::class, function (ContainerInterface $c) { |
|
| 1109 | - $config = $c->get(\OCP\IConfig::class); |
|
| 1110 | - $factoryClass = $config->getSystemValue('sharing.managerFactory', ProviderFactory::class); |
|
| 1111 | - /** @var \OCP\Share\IProviderFactory $factory */ |
|
| 1112 | - return $c->get($factoryClass); |
|
| 1113 | - }); |
|
| 1114 | - |
|
| 1115 | - $this->registerAlias(\OCP\Share\IManager::class, \OC\Share20\Manager::class); |
|
| 1116 | - |
|
| 1117 | - $this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function (Server $c) { |
|
| 1118 | - $instance = new Collaboration\Collaborators\Search($c); |
|
| 1119 | - |
|
| 1120 | - // register default plugins |
|
| 1121 | - $instance->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => UserPlugin::class]); |
|
| 1122 | - $instance->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => GroupPlugin::class]); |
|
| 1123 | - $instance->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => MailPlugin::class]); |
|
| 1124 | - $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => RemotePlugin::class]); |
|
| 1125 | - $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE_GROUP', 'class' => RemoteGroupPlugin::class]); |
|
| 1126 | - |
|
| 1127 | - return $instance; |
|
| 1128 | - }); |
|
| 1129 | - $this->registerAlias(\OCP\Collaboration\Collaborators\ISearchResult::class, \OC\Collaboration\Collaborators\SearchResult::class); |
|
| 1130 | - |
|
| 1131 | - $this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class); |
|
| 1132 | - |
|
| 1133 | - $this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class); |
|
| 1134 | - $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class); |
|
| 1135 | - |
|
| 1136 | - $this->registerAlias(IReferenceManager::class, ReferenceManager::class); |
|
| 1137 | - $this->registerAlias(ITeamManager::class, TeamManager::class); |
|
| 1138 | - |
|
| 1139 | - $this->registerDeprecatedAlias('SettingsManager', \OC\Settings\Manager::class); |
|
| 1140 | - $this->registerAlias(\OCP\Settings\IManager::class, \OC\Settings\Manager::class); |
|
| 1141 | - $this->registerService(\OC\Files\AppData\Factory::class, function (ContainerInterface $c) { |
|
| 1142 | - return new \OC\Files\AppData\Factory( |
|
| 1143 | - $c->get(IRootFolder::class), |
|
| 1144 | - $c->get(SystemConfig::class) |
|
| 1145 | - ); |
|
| 1146 | - }); |
|
| 1147 | - |
|
| 1148 | - $this->registerService('LockdownManager', function (ContainerInterface $c) { |
|
| 1149 | - return new LockdownManager(function () use ($c) { |
|
| 1150 | - return $c->get(ISession::class); |
|
| 1151 | - }); |
|
| 1152 | - }); |
|
| 1153 | - |
|
| 1154 | - $this->registerService(\OCP\OCS\IDiscoveryService::class, function (ContainerInterface $c) { |
|
| 1155 | - return new DiscoveryService( |
|
| 1156 | - $c->get(ICacheFactory::class), |
|
| 1157 | - $c->get(IClientService::class) |
|
| 1158 | - ); |
|
| 1159 | - }); |
|
| 1160 | - $this->registerAlias(IOCMDiscoveryService::class, OCMDiscoveryService::class); |
|
| 1161 | - |
|
| 1162 | - $this->registerService(ICloudIdManager::class, function (ContainerInterface $c) { |
|
| 1163 | - return new CloudIdManager( |
|
| 1164 | - $c->get(\OCP\Contacts\IManager::class), |
|
| 1165 | - $c->get(IURLGenerator::class), |
|
| 1166 | - $c->get(IUserManager::class), |
|
| 1167 | - $c->get(ICacheFactory::class), |
|
| 1168 | - $c->get(IEventDispatcher::class), |
|
| 1169 | - ); |
|
| 1170 | - }); |
|
| 1171 | - |
|
| 1172 | - $this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class); |
|
| 1173 | - $this->registerAlias(ICloudFederationProviderManager::class, CloudFederationProviderManager::class); |
|
| 1174 | - $this->registerService(ICloudFederationFactory::class, function (Server $c) { |
|
| 1175 | - return new CloudFederationFactory(); |
|
| 1176 | - }); |
|
| 253 | + /** @var string */ |
|
| 254 | + private $webRoot; |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * @param string $webRoot |
|
| 258 | + * @param \OC\Config $config |
|
| 259 | + */ |
|
| 260 | + public function __construct($webRoot, \OC\Config $config) { |
|
| 261 | + parent::__construct(); |
|
| 262 | + $this->webRoot = $webRoot; |
|
| 263 | + |
|
| 264 | + // To find out if we are running from CLI or not |
|
| 265 | + $this->registerParameter('isCLI', \OC::$CLI); |
|
| 266 | + $this->registerParameter('serverRoot', \OC::$SERVERROOT); |
|
| 267 | + |
|
| 268 | + $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
| 269 | + return $c; |
|
| 270 | + }); |
|
| 271 | + $this->registerService(\OCP\IServerContainer::class, function (ContainerInterface $c) { |
|
| 272 | + return $c; |
|
| 273 | + }); |
|
| 274 | + |
|
| 275 | + $this->registerAlias(\OCP\Calendar\IManager::class, \OC\Calendar\Manager::class); |
|
| 276 | + |
|
| 277 | + $this->registerAlias(\OCP\Calendar\Resource\IManager::class, \OC\Calendar\Resource\Manager::class); |
|
| 278 | + |
|
| 279 | + $this->registerAlias(\OCP\Calendar\Room\IManager::class, \OC\Calendar\Room\Manager::class); |
|
| 280 | + |
|
| 281 | + $this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class); |
|
| 282 | + |
|
| 283 | + $this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class); |
|
| 284 | + $this->registerAlias(ITemplateManager::class, TemplateManager::class); |
|
| 285 | + $this->registerAlias(\OCP\Template\ITemplateManager::class, \OC\Template\TemplateManager::class); |
|
| 286 | + |
|
| 287 | + $this->registerAlias(IActionFactory::class, ActionFactory::class); |
|
| 288 | + |
|
| 289 | + $this->registerService(View::class, function (Server $c) { |
|
| 290 | + return new View(); |
|
| 291 | + }, false); |
|
| 292 | + |
|
| 293 | + $this->registerService(IPreview::class, function (ContainerInterface $c) { |
|
| 294 | + return new PreviewManager( |
|
| 295 | + $c->get(\OCP\IConfig::class), |
|
| 296 | + $c->get(IRootFolder::class), |
|
| 297 | + new \OC\Preview\Storage\Root( |
|
| 298 | + $c->get(IRootFolder::class), |
|
| 299 | + $c->get(SystemConfig::class) |
|
| 300 | + ), |
|
| 301 | + $c->get(IEventDispatcher::class), |
|
| 302 | + $c->get(GeneratorHelper::class), |
|
| 303 | + $c->get(ISession::class)->get('user_id'), |
|
| 304 | + $c->get(Coordinator::class), |
|
| 305 | + $c->get(IServerContainer::class), |
|
| 306 | + $c->get(IBinaryFinder::class), |
|
| 307 | + $c->get(IMagickSupport::class) |
|
| 308 | + ); |
|
| 309 | + }); |
|
| 310 | + $this->registerAlias(IMimeIconProvider::class, MimeIconProvider::class); |
|
| 311 | + |
|
| 312 | + $this->registerService(\OC\Preview\Watcher::class, function (ContainerInterface $c) { |
|
| 313 | + return new \OC\Preview\Watcher( |
|
| 314 | + new \OC\Preview\Storage\Root( |
|
| 315 | + $c->get(IRootFolder::class), |
|
| 316 | + $c->get(SystemConfig::class) |
|
| 317 | + ) |
|
| 318 | + ); |
|
| 319 | + }); |
|
| 320 | + |
|
| 321 | + $this->registerService(IProfiler::class, function (Server $c) { |
|
| 322 | + return new Profiler($c->get(SystemConfig::class)); |
|
| 323 | + }); |
|
| 324 | + |
|
| 325 | + $this->registerService(Encryption\Manager::class, function (Server $c): Encryption\Manager { |
|
| 326 | + $view = new View(); |
|
| 327 | + $util = new Encryption\Util( |
|
| 328 | + $view, |
|
| 329 | + $c->get(IUserManager::class), |
|
| 330 | + $c->get(IGroupManager::class), |
|
| 331 | + $c->get(\OCP\IConfig::class) |
|
| 332 | + ); |
|
| 333 | + return new Encryption\Manager( |
|
| 334 | + $c->get(\OCP\IConfig::class), |
|
| 335 | + $c->get(LoggerInterface::class), |
|
| 336 | + $c->getL10N('core'), |
|
| 337 | + new View(), |
|
| 338 | + $util, |
|
| 339 | + new ArrayCache() |
|
| 340 | + ); |
|
| 341 | + }); |
|
| 342 | + $this->registerAlias(\OCP\Encryption\IManager::class, Encryption\Manager::class); |
|
| 343 | + |
|
| 344 | + $this->registerService(IFile::class, function (ContainerInterface $c) { |
|
| 345 | + $util = new Encryption\Util( |
|
| 346 | + new View(), |
|
| 347 | + $c->get(IUserManager::class), |
|
| 348 | + $c->get(IGroupManager::class), |
|
| 349 | + $c->get(\OCP\IConfig::class) |
|
| 350 | + ); |
|
| 351 | + return new Encryption\File( |
|
| 352 | + $util, |
|
| 353 | + $c->get(IRootFolder::class), |
|
| 354 | + $c->get(\OCP\Share\IManager::class) |
|
| 355 | + ); |
|
| 356 | + }); |
|
| 357 | + |
|
| 358 | + $this->registerService(IStorage::class, function (ContainerInterface $c) { |
|
| 359 | + $view = new View(); |
|
| 360 | + $util = new Encryption\Util( |
|
| 361 | + $view, |
|
| 362 | + $c->get(IUserManager::class), |
|
| 363 | + $c->get(IGroupManager::class), |
|
| 364 | + $c->get(\OCP\IConfig::class) |
|
| 365 | + ); |
|
| 366 | + |
|
| 367 | + return new Encryption\Keys\Storage( |
|
| 368 | + $view, |
|
| 369 | + $util, |
|
| 370 | + $c->get(ICrypto::class), |
|
| 371 | + $c->get(\OCP\IConfig::class) |
|
| 372 | + ); |
|
| 373 | + }); |
|
| 374 | + |
|
| 375 | + $this->registerAlias(\OCP\ITagManager::class, TagManager::class); |
|
| 376 | + |
|
| 377 | + $this->registerService('SystemTagManagerFactory', function (ContainerInterface $c) { |
|
| 378 | + /** @var \OCP\IConfig $config */ |
|
| 379 | + $config = $c->get(\OCP\IConfig::class); |
|
| 380 | + $factoryClass = $config->getSystemValue('systemtags.managerFactory', SystemTagManagerFactory::class); |
|
| 381 | + return new $factoryClass($this); |
|
| 382 | + }); |
|
| 383 | + $this->registerService(ISystemTagManager::class, function (ContainerInterface $c) { |
|
| 384 | + return $c->get('SystemTagManagerFactory')->getManager(); |
|
| 385 | + }); |
|
| 386 | + /** @deprecated 19.0.0 */ |
|
| 387 | + $this->registerDeprecatedAlias('SystemTagManager', ISystemTagManager::class); |
|
| 388 | + |
|
| 389 | + $this->registerService(ISystemTagObjectMapper::class, function (ContainerInterface $c) { |
|
| 390 | + return $c->get('SystemTagManagerFactory')->getObjectMapper(); |
|
| 391 | + }); |
|
| 392 | + $this->registerAlias(IFileAccess::class, FileAccess::class); |
|
| 393 | + $this->registerService('RootFolder', function (ContainerInterface $c) { |
|
| 394 | + $manager = \OC\Files\Filesystem::getMountManager(); |
|
| 395 | + $view = new View(); |
|
| 396 | + /** @var IUserSession $userSession */ |
|
| 397 | + $userSession = $c->get(IUserSession::class); |
|
| 398 | + $root = new Root( |
|
| 399 | + $manager, |
|
| 400 | + $view, |
|
| 401 | + $userSession->getUser(), |
|
| 402 | + $c->get(IUserMountCache::class), |
|
| 403 | + $this->get(LoggerInterface::class), |
|
| 404 | + $this->get(IUserManager::class), |
|
| 405 | + $this->get(IEventDispatcher::class), |
|
| 406 | + $this->get(ICacheFactory::class), |
|
| 407 | + ); |
|
| 408 | + |
|
| 409 | + $previewConnector = new \OC\Preview\WatcherConnector( |
|
| 410 | + $root, |
|
| 411 | + $c->get(SystemConfig::class), |
|
| 412 | + $this->get(IEventDispatcher::class) |
|
| 413 | + ); |
|
| 414 | + $previewConnector->connectWatcher(); |
|
| 415 | + |
|
| 416 | + return $root; |
|
| 417 | + }); |
|
| 418 | + $this->registerService(HookConnector::class, function (ContainerInterface $c) { |
|
| 419 | + return new HookConnector( |
|
| 420 | + $c->get(IRootFolder::class), |
|
| 421 | + new View(), |
|
| 422 | + $c->get(IEventDispatcher::class), |
|
| 423 | + $c->get(LoggerInterface::class) |
|
| 424 | + ); |
|
| 425 | + }); |
|
| 426 | + |
|
| 427 | + $this->registerService(IRootFolder::class, function (ContainerInterface $c) { |
|
| 428 | + return new LazyRoot(function () use ($c) { |
|
| 429 | + return $c->get('RootFolder'); |
|
| 430 | + }); |
|
| 431 | + }); |
|
| 432 | + |
|
| 433 | + $this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class); |
|
| 434 | + |
|
| 435 | + $this->registerService(DisplayNameCache::class, function (ContainerInterface $c) { |
|
| 436 | + return $c->get(\OC\User\Manager::class)->getDisplayNameCache(); |
|
| 437 | + }); |
|
| 438 | + |
|
| 439 | + $this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) { |
|
| 440 | + $groupManager = new \OC\Group\Manager( |
|
| 441 | + $this->get(IUserManager::class), |
|
| 442 | + $this->get(IEventDispatcher::class), |
|
| 443 | + $this->get(LoggerInterface::class), |
|
| 444 | + $this->get(ICacheFactory::class), |
|
| 445 | + $this->get(IRemoteAddress::class), |
|
| 446 | + ); |
|
| 447 | + return $groupManager; |
|
| 448 | + }); |
|
| 449 | + |
|
| 450 | + $this->registerService(Store::class, function (ContainerInterface $c) { |
|
| 451 | + $session = $c->get(ISession::class); |
|
| 452 | + if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 453 | + $tokenProvider = $c->get(IProvider::class); |
|
| 454 | + } else { |
|
| 455 | + $tokenProvider = null; |
|
| 456 | + } |
|
| 457 | + $logger = $c->get(LoggerInterface::class); |
|
| 458 | + $crypto = $c->get(ICrypto::class); |
|
| 459 | + return new Store($session, $logger, $crypto, $tokenProvider); |
|
| 460 | + }); |
|
| 461 | + $this->registerAlias(IStore::class, Store::class); |
|
| 462 | + $this->registerAlias(IProvider::class, Authentication\Token\Manager::class); |
|
| 463 | + $this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class); |
|
| 464 | + |
|
| 465 | + $this->registerService(\OC\User\Session::class, function (Server $c) { |
|
| 466 | + $manager = $c->get(IUserManager::class); |
|
| 467 | + $session = new \OC\Session\Memory(); |
|
| 468 | + $timeFactory = new TimeFactory(); |
|
| 469 | + // Token providers might require a working database. This code |
|
| 470 | + // might however be called when Nextcloud is not yet setup. |
|
| 471 | + if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 472 | + $provider = $c->get(IProvider::class); |
|
| 473 | + } else { |
|
| 474 | + $provider = null; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + $userSession = new \OC\User\Session( |
|
| 478 | + $manager, |
|
| 479 | + $session, |
|
| 480 | + $timeFactory, |
|
| 481 | + $provider, |
|
| 482 | + $c->get(\OCP\IConfig::class), |
|
| 483 | + $c->get(ISecureRandom::class), |
|
| 484 | + $c->get('LockdownManager'), |
|
| 485 | + $c->get(LoggerInterface::class), |
|
| 486 | + $c->get(IEventDispatcher::class), |
|
| 487 | + ); |
|
| 488 | + /** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */ |
|
| 489 | + $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) { |
|
| 490 | + \OC_Hook::emit('OC_User', 'pre_createUser', ['run' => true, 'uid' => $uid, 'password' => $password]); |
|
| 491 | + }); |
|
| 492 | + /** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */ |
|
| 493 | + $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) { |
|
| 494 | + /** @var \OC\User\User $user */ |
|
| 495 | + \OC_Hook::emit('OC_User', 'post_createUser', ['uid' => $user->getUID(), 'password' => $password]); |
|
| 496 | + }); |
|
| 497 | + /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ |
|
| 498 | + $userSession->listen('\OC\User', 'preDelete', function ($user) { |
|
| 499 | + /** @var \OC\User\User $user */ |
|
| 500 | + \OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $user->getUID()]); |
|
| 501 | + }); |
|
| 502 | + /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ |
|
| 503 | + $userSession->listen('\OC\User', 'postDelete', function ($user) { |
|
| 504 | + /** @var \OC\User\User $user */ |
|
| 505 | + \OC_Hook::emit('OC_User', 'post_deleteUser', ['uid' => $user->getUID()]); |
|
| 506 | + }); |
|
| 507 | + $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) { |
|
| 508 | + /** @var \OC\User\User $user */ |
|
| 509 | + \OC_Hook::emit('OC_User', 'pre_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); |
|
| 510 | + }); |
|
| 511 | + $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) { |
|
| 512 | + /** @var \OC\User\User $user */ |
|
| 513 | + \OC_Hook::emit('OC_User', 'post_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); |
|
| 514 | + }); |
|
| 515 | + $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) { |
|
| 516 | + \OC_Hook::emit('OC_User', 'pre_login', ['run' => true, 'uid' => $uid, 'password' => $password]); |
|
| 517 | + |
|
| 518 | + /** @var IEventDispatcher $dispatcher */ |
|
| 519 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 520 | + $dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password)); |
|
| 521 | + }); |
|
| 522 | + $userSession->listen('\OC\User', 'postLogin', function ($user, $loginName, $password, $isTokenLogin) { |
|
| 523 | + /** @var \OC\User\User $user */ |
|
| 524 | + \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'loginName' => $loginName, 'password' => $password, 'isTokenLogin' => $isTokenLogin]); |
|
| 525 | + |
|
| 526 | + /** @var IEventDispatcher $dispatcher */ |
|
| 527 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 528 | + $dispatcher->dispatchTyped(new UserLoggedInEvent($user, $loginName, $password, $isTokenLogin)); |
|
| 529 | + }); |
|
| 530 | + $userSession->listen('\OC\User', 'preRememberedLogin', function ($uid) { |
|
| 531 | + /** @var IEventDispatcher $dispatcher */ |
|
| 532 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 533 | + $dispatcher->dispatchTyped(new BeforeUserLoggedInWithCookieEvent($uid)); |
|
| 534 | + }); |
|
| 535 | + $userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) { |
|
| 536 | + /** @var \OC\User\User $user */ |
|
| 537 | + \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); |
|
| 538 | + |
|
| 539 | + /** @var IEventDispatcher $dispatcher */ |
|
| 540 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 541 | + $dispatcher->dispatchTyped(new UserLoggedInWithCookieEvent($user, $password)); |
|
| 542 | + }); |
|
| 543 | + $userSession->listen('\OC\User', 'logout', function ($user) { |
|
| 544 | + \OC_Hook::emit('OC_User', 'logout', []); |
|
| 545 | + |
|
| 546 | + /** @var IEventDispatcher $dispatcher */ |
|
| 547 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 548 | + $dispatcher->dispatchTyped(new BeforeUserLoggedOutEvent($user)); |
|
| 549 | + }); |
|
| 550 | + $userSession->listen('\OC\User', 'postLogout', function ($user) { |
|
| 551 | + /** @var IEventDispatcher $dispatcher */ |
|
| 552 | + $dispatcher = $this->get(IEventDispatcher::class); |
|
| 553 | + $dispatcher->dispatchTyped(new UserLoggedOutEvent($user)); |
|
| 554 | + }); |
|
| 555 | + $userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) { |
|
| 556 | + /** @var \OC\User\User $user */ |
|
| 557 | + \OC_Hook::emit('OC_User', 'changeUser', ['run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue]); |
|
| 558 | + }); |
|
| 559 | + return $userSession; |
|
| 560 | + }); |
|
| 561 | + $this->registerAlias(\OCP\IUserSession::class, \OC\User\Session::class); |
|
| 562 | + |
|
| 563 | + $this->registerAlias(\OCP\Authentication\TwoFactorAuth\IRegistry::class, \OC\Authentication\TwoFactorAuth\Registry::class); |
|
| 564 | + |
|
| 565 | + $this->registerAlias(INavigationManager::class, \OC\NavigationManager::class); |
|
| 566 | + |
|
| 567 | + $this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class); |
|
| 568 | + |
|
| 569 | + $this->registerService(\OC\SystemConfig::class, function ($c) use ($config) { |
|
| 570 | + return new \OC\SystemConfig($config); |
|
| 571 | + }); |
|
| 572 | + |
|
| 573 | + $this->registerAlias(IAppConfig::class, \OC\AppConfig::class); |
|
| 574 | + $this->registerAlias(IUserConfig::class, \OC\Config\UserConfig::class); |
|
| 575 | + $this->registerAlias(IAppManager::class, AppManager::class); |
|
| 576 | + |
|
| 577 | + $this->registerService(IFactory::class, function (Server $c) { |
|
| 578 | + return new \OC\L10N\Factory( |
|
| 579 | + $c->get(\OCP\IConfig::class), |
|
| 580 | + $c->getRequest(), |
|
| 581 | + $c->get(IUserSession::class), |
|
| 582 | + $c->get(ICacheFactory::class), |
|
| 583 | + \OC::$SERVERROOT, |
|
| 584 | + $c->get(IAppManager::class), |
|
| 585 | + ); |
|
| 586 | + }); |
|
| 587 | + |
|
| 588 | + $this->registerAlias(IURLGenerator::class, URLGenerator::class); |
|
| 589 | + |
|
| 590 | + $this->registerService(ICache::class, function ($c) { |
|
| 591 | + return new Cache\File(); |
|
| 592 | + }); |
|
| 593 | + |
|
| 594 | + $this->registerService(Factory::class, function (Server $c) { |
|
| 595 | + $profiler = $c->get(IProfiler::class); |
|
| 596 | + $arrayCacheFactory = new \OC\Memcache\Factory(fn () => '', $c->get(LoggerInterface::class), |
|
| 597 | + $profiler, |
|
| 598 | + ArrayCache::class, |
|
| 599 | + ArrayCache::class, |
|
| 600 | + ArrayCache::class |
|
| 601 | + ); |
|
| 602 | + /** @var SystemConfig $config */ |
|
| 603 | + $config = $c->get(SystemConfig::class); |
|
| 604 | + /** @var ServerVersion $serverVersion */ |
|
| 605 | + $serverVersion = $c->get(ServerVersion::class); |
|
| 606 | + |
|
| 607 | + if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { |
|
| 608 | + $logQuery = $config->getValue('log_query'); |
|
| 609 | + $prefixClosure = function () use ($logQuery, $serverVersion): ?string { |
|
| 610 | + if (!$logQuery) { |
|
| 611 | + try { |
|
| 612 | + $v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(true); |
|
| 613 | + } catch (\Doctrine\DBAL\Exception $e) { |
|
| 614 | + // Database service probably unavailable |
|
| 615 | + // Probably related to https://github.com/nextcloud/server/issues/37424 |
|
| 616 | + return null; |
|
| 617 | + } |
|
| 618 | + } else { |
|
| 619 | + // If the log_query is enabled, we can not get the app versions |
|
| 620 | + // as that does a query, which will be logged and the logging |
|
| 621 | + // depends on redis and here we are back again in the same function. |
|
| 622 | + $v = [ |
|
| 623 | + 'log_query' => 'enabled', |
|
| 624 | + ]; |
|
| 625 | + } |
|
| 626 | + $v['core'] = implode(',', $serverVersion->getVersion()); |
|
| 627 | + $version = implode(',', array_keys($v)) . implode(',', $v); |
|
| 628 | + $instanceId = \OC_Util::getInstanceId(); |
|
| 629 | + $path = \OC::$SERVERROOT; |
|
| 630 | + return md5($instanceId . '-' . $version . '-' . $path); |
|
| 631 | + }; |
|
| 632 | + return new \OC\Memcache\Factory($prefixClosure, |
|
| 633 | + $c->get(LoggerInterface::class), |
|
| 634 | + $profiler, |
|
| 635 | + /** @psalm-taint-escape callable */ |
|
| 636 | + $config->getValue('memcache.local', null), |
|
| 637 | + /** @psalm-taint-escape callable */ |
|
| 638 | + $config->getValue('memcache.distributed', null), |
|
| 639 | + /** @psalm-taint-escape callable */ |
|
| 640 | + $config->getValue('memcache.locking', null), |
|
| 641 | + /** @psalm-taint-escape callable */ |
|
| 642 | + $config->getValue('redis_log_file') |
|
| 643 | + ); |
|
| 644 | + } |
|
| 645 | + return $arrayCacheFactory; |
|
| 646 | + }); |
|
| 647 | + $this->registerAlias(ICacheFactory::class, Factory::class); |
|
| 648 | + |
|
| 649 | + $this->registerService('RedisFactory', function (Server $c) { |
|
| 650 | + $systemConfig = $c->get(SystemConfig::class); |
|
| 651 | + return new RedisFactory($systemConfig, $c->get(IEventLogger::class)); |
|
| 652 | + }); |
|
| 653 | + |
|
| 654 | + $this->registerService(\OCP\Activity\IManager::class, function (Server $c) { |
|
| 655 | + $l10n = $this->get(IFactory::class)->get('lib'); |
|
| 656 | + return new \OC\Activity\Manager( |
|
| 657 | + $c->getRequest(), |
|
| 658 | + $c->get(IUserSession::class), |
|
| 659 | + $c->get(\OCP\IConfig::class), |
|
| 660 | + $c->get(IValidator::class), |
|
| 661 | + $c->get(IRichTextFormatter::class), |
|
| 662 | + $l10n |
|
| 663 | + ); |
|
| 664 | + }); |
|
| 665 | + |
|
| 666 | + $this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) { |
|
| 667 | + return new \OC\Activity\EventMerger( |
|
| 668 | + $c->getL10N('lib') |
|
| 669 | + ); |
|
| 670 | + }); |
|
| 671 | + $this->registerAlias(IValidator::class, Validator::class); |
|
| 672 | + |
|
| 673 | + $this->registerService(AvatarManager::class, function (Server $c) { |
|
| 674 | + return new AvatarManager( |
|
| 675 | + $c->get(IUserSession::class), |
|
| 676 | + $c->get(\OC\User\Manager::class), |
|
| 677 | + $c->getAppDataDir('avatar'), |
|
| 678 | + $c->getL10N('lib'), |
|
| 679 | + $c->get(LoggerInterface::class), |
|
| 680 | + $c->get(\OCP\IConfig::class), |
|
| 681 | + $c->get(IAccountManager::class), |
|
| 682 | + $c->get(KnownUserService::class) |
|
| 683 | + ); |
|
| 684 | + }); |
|
| 685 | + |
|
| 686 | + $this->registerAlias(IAvatarManager::class, AvatarManager::class); |
|
| 687 | + |
|
| 688 | + $this->registerAlias(\OCP\Support\CrashReport\IRegistry::class, \OC\Support\CrashReport\Registry::class); |
|
| 689 | + $this->registerAlias(\OCP\Support\Subscription\IRegistry::class, \OC\Support\Subscription\Registry::class); |
|
| 690 | + $this->registerAlias(\OCP\Support\Subscription\IAssertion::class, \OC\Support\Subscription\Assertion::class); |
|
| 691 | + |
|
| 692 | + /** Only used by the PsrLoggerAdapter should not be used by apps */ |
|
| 693 | + $this->registerService(\OC\Log::class, function (Server $c) { |
|
| 694 | + $logType = $c->get(AllConfig::class)->getSystemValue('log_type', 'file'); |
|
| 695 | + $factory = new LogFactory($c, $this->get(SystemConfig::class)); |
|
| 696 | + $logger = $factory->get($logType); |
|
| 697 | + $registry = $c->get(\OCP\Support\CrashReport\IRegistry::class); |
|
| 698 | + |
|
| 699 | + return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry); |
|
| 700 | + }); |
|
| 701 | + // PSR-3 logger |
|
| 702 | + $this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class); |
|
| 703 | + |
|
| 704 | + $this->registerService(ILogFactory::class, function (Server $c) { |
|
| 705 | + return new LogFactory($c, $this->get(SystemConfig::class)); |
|
| 706 | + }); |
|
| 707 | + |
|
| 708 | + $this->registerAlias(IJobList::class, \OC\BackgroundJob\JobList::class); |
|
| 709 | + |
|
| 710 | + $this->registerService(Router::class, function (Server $c) { |
|
| 711 | + $cacheFactory = $c->get(ICacheFactory::class); |
|
| 712 | + if ($cacheFactory->isLocalCacheAvailable()) { |
|
| 713 | + $router = $c->resolve(CachingRouter::class); |
|
| 714 | + } else { |
|
| 715 | + $router = $c->resolve(Router::class); |
|
| 716 | + } |
|
| 717 | + return $router; |
|
| 718 | + }); |
|
| 719 | + $this->registerAlias(IRouter::class, Router::class); |
|
| 720 | + |
|
| 721 | + $this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) { |
|
| 722 | + $config = $c->get(\OCP\IConfig::class); |
|
| 723 | + if (ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) { |
|
| 724 | + $backend = new \OC\Security\RateLimiting\Backend\MemoryCacheBackend( |
|
| 725 | + $c->get(AllConfig::class), |
|
| 726 | + $this->get(ICacheFactory::class), |
|
| 727 | + new \OC\AppFramework\Utility\TimeFactory() |
|
| 728 | + ); |
|
| 729 | + } else { |
|
| 730 | + $backend = new \OC\Security\RateLimiting\Backend\DatabaseBackend( |
|
| 731 | + $c->get(AllConfig::class), |
|
| 732 | + $c->get(IDBConnection::class), |
|
| 733 | + new \OC\AppFramework\Utility\TimeFactory() |
|
| 734 | + ); |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + return $backend; |
|
| 738 | + }); |
|
| 739 | + |
|
| 740 | + $this->registerAlias(\OCP\Security\ISecureRandom::class, SecureRandom::class); |
|
| 741 | + $this->registerAlias(\OCP\Security\IRemoteHostValidator::class, \OC\Security\RemoteHostValidator::class); |
|
| 742 | + $this->registerAlias(IVerificationToken::class, VerificationToken::class); |
|
| 743 | + |
|
| 744 | + $this->registerAlias(ICrypto::class, Crypto::class); |
|
| 745 | + |
|
| 746 | + $this->registerAlias(IHasher::class, Hasher::class); |
|
| 747 | + |
|
| 748 | + $this->registerAlias(ICredentialsManager::class, CredentialsManager::class); |
|
| 749 | + |
|
| 750 | + $this->registerAlias(IDBConnection::class, ConnectionAdapter::class); |
|
| 751 | + $this->registerService(Connection::class, function (Server $c) { |
|
| 752 | + $systemConfig = $c->get(SystemConfig::class); |
|
| 753 | + $factory = new \OC\DB\ConnectionFactory($systemConfig, $c->get(ICacheFactory::class)); |
|
| 754 | + $type = $systemConfig->getValue('dbtype', 'sqlite'); |
|
| 755 | + if (!$factory->isValidType($type)) { |
|
| 756 | + throw new \OC\DatabaseException('Invalid database type'); |
|
| 757 | + } |
|
| 758 | + $connection = $factory->getConnection($type, []); |
|
| 759 | + return $connection; |
|
| 760 | + }); |
|
| 761 | + |
|
| 762 | + $this->registerAlias(ICertificateManager::class, CertificateManager::class); |
|
| 763 | + $this->registerAlias(IClientService::class, ClientService::class); |
|
| 764 | + $this->registerService(NegativeDnsCache::class, function (ContainerInterface $c) { |
|
| 765 | + return new NegativeDnsCache( |
|
| 766 | + $c->get(ICacheFactory::class), |
|
| 767 | + ); |
|
| 768 | + }); |
|
| 769 | + $this->registerDeprecatedAlias('HttpClientService', IClientService::class); |
|
| 770 | + $this->registerService(IEventLogger::class, function (ContainerInterface $c) { |
|
| 771 | + return new EventLogger($c->get(SystemConfig::class), $c->get(LoggerInterface::class), $c->get(Log::class)); |
|
| 772 | + }); |
|
| 773 | + |
|
| 774 | + $this->registerService(IQueryLogger::class, function (ContainerInterface $c) { |
|
| 775 | + $queryLogger = new QueryLogger(); |
|
| 776 | + if ($c->get(SystemConfig::class)->getValue('debug', false)) { |
|
| 777 | + // In debug mode, module is being activated by default |
|
| 778 | + $queryLogger->activate(); |
|
| 779 | + } |
|
| 780 | + return $queryLogger; |
|
| 781 | + }); |
|
| 782 | + |
|
| 783 | + $this->registerAlias(ITempManager::class, TempManager::class); |
|
| 784 | + $this->registerAlias(IDateTimeZone::class, DateTimeZone::class); |
|
| 785 | + |
|
| 786 | + $this->registerService(IDateTimeFormatter::class, function (Server $c) { |
|
| 787 | + $language = $c->get(\OCP\IConfig::class)->getUserValue($c->get(ISession::class)->get('user_id'), 'core', 'lang', null); |
|
| 788 | + |
|
| 789 | + return new DateTimeFormatter( |
|
| 790 | + $c->get(IDateTimeZone::class)->getTimeZone(), |
|
| 791 | + $c->getL10N('lib', $language) |
|
| 792 | + ); |
|
| 793 | + }); |
|
| 794 | + |
|
| 795 | + $this->registerService(IUserMountCache::class, function (ContainerInterface $c) { |
|
| 796 | + $mountCache = $c->get(UserMountCache::class); |
|
| 797 | + $listener = new UserMountCacheListener($mountCache); |
|
| 798 | + $listener->listen($c->get(IUserManager::class)); |
|
| 799 | + return $mountCache; |
|
| 800 | + }); |
|
| 801 | + |
|
| 802 | + $this->registerService(IMountProviderCollection::class, function (ContainerInterface $c) { |
|
| 803 | + $loader = $c->get(IStorageFactory::class); |
|
| 804 | + $mountCache = $c->get(IUserMountCache::class); |
|
| 805 | + $eventLogger = $c->get(IEventLogger::class); |
|
| 806 | + $manager = new MountProviderCollection($loader, $mountCache, $eventLogger); |
|
| 807 | + |
|
| 808 | + // builtin providers |
|
| 809 | + |
|
| 810 | + $config = $c->get(\OCP\IConfig::class); |
|
| 811 | + $logger = $c->get(LoggerInterface::class); |
|
| 812 | + $objectStoreConfig = $c->get(PrimaryObjectStoreConfig::class); |
|
| 813 | + $manager->registerProvider(new CacheMountProvider($config)); |
|
| 814 | + $manager->registerHomeProvider(new LocalHomeMountProvider()); |
|
| 815 | + $manager->registerHomeProvider(new ObjectHomeMountProvider($objectStoreConfig)); |
|
| 816 | + $manager->registerRootProvider(new RootMountProvider($objectStoreConfig, $config)); |
|
| 817 | + $manager->registerRootProvider(new ObjectStorePreviewCacheMountProvider($logger, $config)); |
|
| 818 | + |
|
| 819 | + return $manager; |
|
| 820 | + }); |
|
| 821 | + |
|
| 822 | + $this->registerService(IBus::class, function (ContainerInterface $c) { |
|
| 823 | + $busClass = $c->get(\OCP\IConfig::class)->getSystemValueString('commandbus'); |
|
| 824 | + if ($busClass) { |
|
| 825 | + [$app, $class] = explode('::', $busClass, 2); |
|
| 826 | + if ($c->get(IAppManager::class)->isEnabledForUser($app)) { |
|
| 827 | + $c->get(IAppManager::class)->loadApp($app); |
|
| 828 | + return $c->get($class); |
|
| 829 | + } else { |
|
| 830 | + throw new ServiceUnavailableException("The app providing the command bus ($app) is not enabled"); |
|
| 831 | + } |
|
| 832 | + } else { |
|
| 833 | + $jobList = $c->get(IJobList::class); |
|
| 834 | + return new CronBus($jobList); |
|
| 835 | + } |
|
| 836 | + }); |
|
| 837 | + $this->registerDeprecatedAlias('AsyncCommandBus', IBus::class); |
|
| 838 | + $this->registerAlias(ITrustedDomainHelper::class, TrustedDomainHelper::class); |
|
| 839 | + $this->registerAlias(IThrottler::class, Throttler::class); |
|
| 840 | + |
|
| 841 | + $this->registerService(\OC\Security\Bruteforce\Backend\IBackend::class, function ($c) { |
|
| 842 | + $config = $c->get(\OCP\IConfig::class); |
|
| 843 | + if (!$config->getSystemValueBool('auth.bruteforce.protection.force.database', false) |
|
| 844 | + && ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) { |
|
| 845 | + $backend = $c->get(\OC\Security\Bruteforce\Backend\MemoryCacheBackend::class); |
|
| 846 | + } else { |
|
| 847 | + $backend = $c->get(\OC\Security\Bruteforce\Backend\DatabaseBackend::class); |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + return $backend; |
|
| 851 | + }); |
|
| 852 | + |
|
| 853 | + $this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class); |
|
| 854 | + $this->registerService(Checker::class, function (ContainerInterface $c) { |
|
| 855 | + // IConfig requires a working database. This code |
|
| 856 | + // might however be called when Nextcloud is not yet setup. |
|
| 857 | + if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { |
|
| 858 | + $config = $c->get(\OCP\IConfig::class); |
|
| 859 | + $appConfig = $c->get(\OCP\IAppConfig::class); |
|
| 860 | + } else { |
|
| 861 | + $config = null; |
|
| 862 | + $appConfig = null; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + return new Checker( |
|
| 866 | + $c->get(ServerVersion::class), |
|
| 867 | + $c->get(EnvironmentHelper::class), |
|
| 868 | + new FileAccessHelper(), |
|
| 869 | + new AppLocator(), |
|
| 870 | + $config, |
|
| 871 | + $appConfig, |
|
| 872 | + $c->get(ICacheFactory::class), |
|
| 873 | + $c->get(IAppManager::class), |
|
| 874 | + $c->get(IMimeTypeDetector::class) |
|
| 875 | + ); |
|
| 876 | + }); |
|
| 877 | + $this->registerService(\OCP\IRequest::class, function (ContainerInterface $c) { |
|
| 878 | + if (isset($this['urlParams'])) { |
|
| 879 | + $urlParams = $this['urlParams']; |
|
| 880 | + } else { |
|
| 881 | + $urlParams = []; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + if (defined('PHPUNIT_RUN') && PHPUNIT_RUN |
|
| 885 | + && in_array('fakeinput', stream_get_wrappers()) |
|
| 886 | + ) { |
|
| 887 | + $stream = 'fakeinput://data'; |
|
| 888 | + } else { |
|
| 889 | + $stream = 'php://input'; |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + return new Request( |
|
| 893 | + [ |
|
| 894 | + 'get' => $_GET, |
|
| 895 | + 'post' => $_POST, |
|
| 896 | + 'files' => $_FILES, |
|
| 897 | + 'server' => $_SERVER, |
|
| 898 | + 'env' => $_ENV, |
|
| 899 | + 'cookies' => $_COOKIE, |
|
| 900 | + 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) |
|
| 901 | + ? $_SERVER['REQUEST_METHOD'] |
|
| 902 | + : '', |
|
| 903 | + 'urlParams' => $urlParams, |
|
| 904 | + ], |
|
| 905 | + $this->get(IRequestId::class), |
|
| 906 | + $this->get(\OCP\IConfig::class), |
|
| 907 | + $this->get(CsrfTokenManager::class), |
|
| 908 | + $stream |
|
| 909 | + ); |
|
| 910 | + }); |
|
| 911 | + |
|
| 912 | + $this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId { |
|
| 913 | + return new RequestId( |
|
| 914 | + $_SERVER['UNIQUE_ID'] ?? '', |
|
| 915 | + $this->get(ISecureRandom::class) |
|
| 916 | + ); |
|
| 917 | + }); |
|
| 918 | + |
|
| 919 | + $this->registerService(IMailer::class, function (Server $c) { |
|
| 920 | + return new Mailer( |
|
| 921 | + $c->get(\OCP\IConfig::class), |
|
| 922 | + $c->get(LoggerInterface::class), |
|
| 923 | + $c->get(Defaults::class), |
|
| 924 | + $c->get(IURLGenerator::class), |
|
| 925 | + $c->getL10N('lib'), |
|
| 926 | + $c->get(IEventDispatcher::class), |
|
| 927 | + $c->get(IFactory::class) |
|
| 928 | + ); |
|
| 929 | + }); |
|
| 930 | + |
|
| 931 | + /** @since 30.0.0 */ |
|
| 932 | + $this->registerAlias(\OCP\Mail\Provider\IManager::class, \OC\Mail\Provider\Manager::class); |
|
| 933 | + |
|
| 934 | + $this->registerService(ILDAPProviderFactory::class, function (ContainerInterface $c) { |
|
| 935 | + $config = $c->get(\OCP\IConfig::class); |
|
| 936 | + $factoryClass = $config->getSystemValue('ldapProviderFactory', null); |
|
| 937 | + if (is_null($factoryClass) || !class_exists($factoryClass)) { |
|
| 938 | + return new NullLDAPProviderFactory($this); |
|
| 939 | + } |
|
| 940 | + /** @var \OCP\LDAP\ILDAPProviderFactory $factory */ |
|
| 941 | + return new $factoryClass($this); |
|
| 942 | + }); |
|
| 943 | + $this->registerService(ILDAPProvider::class, function (ContainerInterface $c) { |
|
| 944 | + $factory = $c->get(ILDAPProviderFactory::class); |
|
| 945 | + return $factory->getLDAPProvider(); |
|
| 946 | + }); |
|
| 947 | + $this->registerService(ILockingProvider::class, function (ContainerInterface $c) { |
|
| 948 | + $ini = $c->get(IniGetWrapper::class); |
|
| 949 | + $config = $c->get(\OCP\IConfig::class); |
|
| 950 | + $ttl = $config->getSystemValueInt('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time'))); |
|
| 951 | + if ($config->getSystemValueBool('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { |
|
| 952 | + /** @var \OC\Memcache\Factory $memcacheFactory */ |
|
| 953 | + $memcacheFactory = $c->get(ICacheFactory::class); |
|
| 954 | + $memcache = $memcacheFactory->createLocking('lock'); |
|
| 955 | + if (!($memcache instanceof \OC\Memcache\NullCache)) { |
|
| 956 | + $timeFactory = $c->get(ITimeFactory::class); |
|
| 957 | + return new MemcacheLockingProvider($memcache, $timeFactory, $ttl); |
|
| 958 | + } |
|
| 959 | + return new DBLockingProvider( |
|
| 960 | + $c->get(IDBConnection::class), |
|
| 961 | + new TimeFactory(), |
|
| 962 | + $ttl, |
|
| 963 | + !\OC::$CLI |
|
| 964 | + ); |
|
| 965 | + } |
|
| 966 | + return new NoopLockingProvider(); |
|
| 967 | + }); |
|
| 968 | + |
|
| 969 | + $this->registerService(ILockManager::class, function (Server $c): LockManager { |
|
| 970 | + return new LockManager(); |
|
| 971 | + }); |
|
| 972 | + |
|
| 973 | + $this->registerAlias(ILockdownManager::class, 'LockdownManager'); |
|
| 974 | + $this->registerService(SetupManager::class, function ($c) { |
|
| 975 | + // create the setupmanager through the mount manager to resolve the cyclic dependency |
|
| 976 | + return $c->get(\OC\Files\Mount\Manager::class)->getSetupManager(); |
|
| 977 | + }); |
|
| 978 | + $this->registerAlias(IMountManager::class, \OC\Files\Mount\Manager::class); |
|
| 979 | + |
|
| 980 | + $this->registerService(IMimeTypeDetector::class, function (ContainerInterface $c) { |
|
| 981 | + return new \OC\Files\Type\Detection( |
|
| 982 | + $c->get(IURLGenerator::class), |
|
| 983 | + $c->get(LoggerInterface::class), |
|
| 984 | + \OC::$configDir, |
|
| 985 | + \OC::$SERVERROOT . '/resources/config/' |
|
| 986 | + ); |
|
| 987 | + }); |
|
| 988 | + |
|
| 989 | + $this->registerAlias(IMimeTypeLoader::class, Loader::class); |
|
| 990 | + $this->registerService(BundleFetcher::class, function () { |
|
| 991 | + return new BundleFetcher($this->getL10N('lib')); |
|
| 992 | + }); |
|
| 993 | + $this->registerAlias(\OCP\Notification\IManager::class, Manager::class); |
|
| 994 | + |
|
| 995 | + $this->registerService(CapabilitiesManager::class, function (ContainerInterface $c) { |
|
| 996 | + $manager = new CapabilitiesManager($c->get(LoggerInterface::class)); |
|
| 997 | + $manager->registerCapability(function () use ($c) { |
|
| 998 | + return new \OC\OCS\CoreCapabilities($c->get(\OCP\IConfig::class)); |
|
| 999 | + }); |
|
| 1000 | + $manager->registerCapability(function () use ($c) { |
|
| 1001 | + return $c->get(\OC\Security\Bruteforce\Capabilities::class); |
|
| 1002 | + }); |
|
| 1003 | + return $manager; |
|
| 1004 | + }); |
|
| 1005 | + |
|
| 1006 | + $this->registerService(ICommentsManager::class, function (Server $c) { |
|
| 1007 | + $config = $c->get(\OCP\IConfig::class); |
|
| 1008 | + $factoryClass = $config->getSystemValue('comments.managerFactory', CommentsManagerFactory::class); |
|
| 1009 | + /** @var \OCP\Comments\ICommentsManagerFactory $factory */ |
|
| 1010 | + $factory = new $factoryClass($this); |
|
| 1011 | + $manager = $factory->getManager(); |
|
| 1012 | + |
|
| 1013 | + $manager->registerDisplayNameResolver('user', function ($id) use ($c) { |
|
| 1014 | + $manager = $c->get(IUserManager::class); |
|
| 1015 | + $userDisplayName = $manager->getDisplayName($id); |
|
| 1016 | + if ($userDisplayName === null) { |
|
| 1017 | + $l = $c->get(IFactory::class)->get('core'); |
|
| 1018 | + return $l->t('Unknown account'); |
|
| 1019 | + } |
|
| 1020 | + return $userDisplayName; |
|
| 1021 | + }); |
|
| 1022 | + |
|
| 1023 | + return $manager; |
|
| 1024 | + }); |
|
| 1025 | + |
|
| 1026 | + $this->registerAlias(\OC_Defaults::class, 'ThemingDefaults'); |
|
| 1027 | + $this->registerService('ThemingDefaults', function (Server $c) { |
|
| 1028 | + try { |
|
| 1029 | + $classExists = class_exists('OCA\Theming\ThemingDefaults'); |
|
| 1030 | + } catch (\OCP\AutoloadNotAllowedException $e) { |
|
| 1031 | + // App disabled or in maintenance mode |
|
| 1032 | + $classExists = false; |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isEnabledForAnyone('theming') && $c->get(TrustedDomainHelper::class)->isTrustedDomain($c->getRequest()->getInsecureServerHost())) { |
|
| 1036 | + $backgroundService = new BackgroundService( |
|
| 1037 | + $c->get(IRootFolder::class), |
|
| 1038 | + $c->getAppDataDir('theming'), |
|
| 1039 | + $c->get(IAppConfig::class), |
|
| 1040 | + $c->get(\OCP\IConfig::class), |
|
| 1041 | + $c->get(ISession::class)->get('user_id'), |
|
| 1042 | + ); |
|
| 1043 | + $imageManager = new ImageManager( |
|
| 1044 | + $c->get(\OCP\IConfig::class), |
|
| 1045 | + $c->getAppDataDir('theming'), |
|
| 1046 | + $c->get(IURLGenerator::class), |
|
| 1047 | + $c->get(ICacheFactory::class), |
|
| 1048 | + $c->get(LoggerInterface::class), |
|
| 1049 | + $c->get(ITempManager::class), |
|
| 1050 | + $backgroundService, |
|
| 1051 | + ); |
|
| 1052 | + return new ThemingDefaults( |
|
| 1053 | + $c->get(\OCP\IConfig::class), |
|
| 1054 | + $c->get(\OCP\IAppConfig::class), |
|
| 1055 | + $c->getL10N('theming'), |
|
| 1056 | + $c->get(IUserSession::class), |
|
| 1057 | + $c->get(IURLGenerator::class), |
|
| 1058 | + $c->get(ICacheFactory::class), |
|
| 1059 | + new Util($c->get(ServerVersion::class), $c->get(\OCP\IConfig::class), $this->get(IAppManager::class), $c->getAppDataDir('theming'), $imageManager), |
|
| 1060 | + $imageManager, |
|
| 1061 | + $c->get(IAppManager::class), |
|
| 1062 | + $c->get(INavigationManager::class), |
|
| 1063 | + $backgroundService, |
|
| 1064 | + ); |
|
| 1065 | + } |
|
| 1066 | + return new \OC_Defaults(); |
|
| 1067 | + }); |
|
| 1068 | + $this->registerService(JSCombiner::class, function (Server $c) { |
|
| 1069 | + return new JSCombiner( |
|
| 1070 | + $c->getAppDataDir('js'), |
|
| 1071 | + $c->get(IURLGenerator::class), |
|
| 1072 | + $this->get(ICacheFactory::class), |
|
| 1073 | + $c->get(SystemConfig::class), |
|
| 1074 | + $c->get(LoggerInterface::class) |
|
| 1075 | + ); |
|
| 1076 | + }); |
|
| 1077 | + $this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class); |
|
| 1078 | + |
|
| 1079 | + $this->registerService('CryptoWrapper', function (ContainerInterface $c) { |
|
| 1080 | + // FIXME: Instantiated here due to cyclic dependency |
|
| 1081 | + $request = new Request( |
|
| 1082 | + [ |
|
| 1083 | + 'get' => $_GET, |
|
| 1084 | + 'post' => $_POST, |
|
| 1085 | + 'files' => $_FILES, |
|
| 1086 | + 'server' => $_SERVER, |
|
| 1087 | + 'env' => $_ENV, |
|
| 1088 | + 'cookies' => $_COOKIE, |
|
| 1089 | + 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) |
|
| 1090 | + ? $_SERVER['REQUEST_METHOD'] |
|
| 1091 | + : null, |
|
| 1092 | + ], |
|
| 1093 | + $c->get(IRequestId::class), |
|
| 1094 | + $c->get(\OCP\IConfig::class) |
|
| 1095 | + ); |
|
| 1096 | + |
|
| 1097 | + return new CryptoWrapper( |
|
| 1098 | + $c->get(ICrypto::class), |
|
| 1099 | + $c->get(ISecureRandom::class), |
|
| 1100 | + $request |
|
| 1101 | + ); |
|
| 1102 | + }); |
|
| 1103 | + $this->registerService(SessionStorage::class, function (ContainerInterface $c) { |
|
| 1104 | + return new SessionStorage($c->get(ISession::class)); |
|
| 1105 | + }); |
|
| 1106 | + $this->registerAlias(\OCP\Security\IContentSecurityPolicyManager::class, ContentSecurityPolicyManager::class); |
|
| 1107 | + |
|
| 1108 | + $this->registerService(IProviderFactory::class, function (ContainerInterface $c) { |
|
| 1109 | + $config = $c->get(\OCP\IConfig::class); |
|
| 1110 | + $factoryClass = $config->getSystemValue('sharing.managerFactory', ProviderFactory::class); |
|
| 1111 | + /** @var \OCP\Share\IProviderFactory $factory */ |
|
| 1112 | + return $c->get($factoryClass); |
|
| 1113 | + }); |
|
| 1114 | + |
|
| 1115 | + $this->registerAlias(\OCP\Share\IManager::class, \OC\Share20\Manager::class); |
|
| 1116 | + |
|
| 1117 | + $this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function (Server $c) { |
|
| 1118 | + $instance = new Collaboration\Collaborators\Search($c); |
|
| 1119 | + |
|
| 1120 | + // register default plugins |
|
| 1121 | + $instance->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => UserPlugin::class]); |
|
| 1122 | + $instance->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => GroupPlugin::class]); |
|
| 1123 | + $instance->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => MailPlugin::class]); |
|
| 1124 | + $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => RemotePlugin::class]); |
|
| 1125 | + $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE_GROUP', 'class' => RemoteGroupPlugin::class]); |
|
| 1126 | + |
|
| 1127 | + return $instance; |
|
| 1128 | + }); |
|
| 1129 | + $this->registerAlias(\OCP\Collaboration\Collaborators\ISearchResult::class, \OC\Collaboration\Collaborators\SearchResult::class); |
|
| 1130 | + |
|
| 1131 | + $this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class); |
|
| 1132 | + |
|
| 1133 | + $this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class); |
|
| 1134 | + $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class); |
|
| 1135 | + |
|
| 1136 | + $this->registerAlias(IReferenceManager::class, ReferenceManager::class); |
|
| 1137 | + $this->registerAlias(ITeamManager::class, TeamManager::class); |
|
| 1138 | + |
|
| 1139 | + $this->registerDeprecatedAlias('SettingsManager', \OC\Settings\Manager::class); |
|
| 1140 | + $this->registerAlias(\OCP\Settings\IManager::class, \OC\Settings\Manager::class); |
|
| 1141 | + $this->registerService(\OC\Files\AppData\Factory::class, function (ContainerInterface $c) { |
|
| 1142 | + return new \OC\Files\AppData\Factory( |
|
| 1143 | + $c->get(IRootFolder::class), |
|
| 1144 | + $c->get(SystemConfig::class) |
|
| 1145 | + ); |
|
| 1146 | + }); |
|
| 1147 | + |
|
| 1148 | + $this->registerService('LockdownManager', function (ContainerInterface $c) { |
|
| 1149 | + return new LockdownManager(function () use ($c) { |
|
| 1150 | + return $c->get(ISession::class); |
|
| 1151 | + }); |
|
| 1152 | + }); |
|
| 1153 | + |
|
| 1154 | + $this->registerService(\OCP\OCS\IDiscoveryService::class, function (ContainerInterface $c) { |
|
| 1155 | + return new DiscoveryService( |
|
| 1156 | + $c->get(ICacheFactory::class), |
|
| 1157 | + $c->get(IClientService::class) |
|
| 1158 | + ); |
|
| 1159 | + }); |
|
| 1160 | + $this->registerAlias(IOCMDiscoveryService::class, OCMDiscoveryService::class); |
|
| 1161 | + |
|
| 1162 | + $this->registerService(ICloudIdManager::class, function (ContainerInterface $c) { |
|
| 1163 | + return new CloudIdManager( |
|
| 1164 | + $c->get(\OCP\Contacts\IManager::class), |
|
| 1165 | + $c->get(IURLGenerator::class), |
|
| 1166 | + $c->get(IUserManager::class), |
|
| 1167 | + $c->get(ICacheFactory::class), |
|
| 1168 | + $c->get(IEventDispatcher::class), |
|
| 1169 | + ); |
|
| 1170 | + }); |
|
| 1171 | + |
|
| 1172 | + $this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class); |
|
| 1173 | + $this->registerAlias(ICloudFederationProviderManager::class, CloudFederationProviderManager::class); |
|
| 1174 | + $this->registerService(ICloudFederationFactory::class, function (Server $c) { |
|
| 1175 | + return new CloudFederationFactory(); |
|
| 1176 | + }); |
|
| 1177 | 1177 | |
| 1178 | - $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class); |
|
| 1178 | + $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class); |
|
| 1179 | 1179 | |
| 1180 | - $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class); |
|
| 1181 | - $this->registerAlias(\Psr\Clock\ClockInterface::class, \OCP\AppFramework\Utility\ITimeFactory::class); |
|
| 1180 | + $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class); |
|
| 1181 | + $this->registerAlias(\Psr\Clock\ClockInterface::class, \OCP\AppFramework\Utility\ITimeFactory::class); |
|
| 1182 | 1182 | |
| 1183 | - $this->registerService(Defaults::class, function (Server $c) { |
|
| 1184 | - return new Defaults( |
|
| 1185 | - $c->get('ThemingDefaults') |
|
| 1186 | - ); |
|
| 1187 | - }); |
|
| 1183 | + $this->registerService(Defaults::class, function (Server $c) { |
|
| 1184 | + return new Defaults( |
|
| 1185 | + $c->get('ThemingDefaults') |
|
| 1186 | + ); |
|
| 1187 | + }); |
|
| 1188 | 1188 | |
| 1189 | - $this->registerService(\OCP\ISession::class, function (ContainerInterface $c) { |
|
| 1190 | - return $c->get(\OCP\IUserSession::class)->getSession(); |
|
| 1191 | - }, false); |
|
| 1189 | + $this->registerService(\OCP\ISession::class, function (ContainerInterface $c) { |
|
| 1190 | + return $c->get(\OCP\IUserSession::class)->getSession(); |
|
| 1191 | + }, false); |
|
| 1192 | 1192 | |
| 1193 | - $this->registerService(IShareHelper::class, function (ContainerInterface $c) { |
|
| 1194 | - return new ShareHelper( |
|
| 1195 | - $c->get(\OCP\Share\IManager::class) |
|
| 1196 | - ); |
|
| 1197 | - }); |
|
| 1193 | + $this->registerService(IShareHelper::class, function (ContainerInterface $c) { |
|
| 1194 | + return new ShareHelper( |
|
| 1195 | + $c->get(\OCP\Share\IManager::class) |
|
| 1196 | + ); |
|
| 1197 | + }); |
|
| 1198 | 1198 | |
| 1199 | - $this->registerService(Installer::class, function (ContainerInterface $c) { |
|
| 1200 | - return new Installer( |
|
| 1201 | - $c->get(AppFetcher::class), |
|
| 1202 | - $c->get(IClientService::class), |
|
| 1203 | - $c->get(ITempManager::class), |
|
| 1204 | - $c->get(LoggerInterface::class), |
|
| 1205 | - $c->get(\OCP\IConfig::class), |
|
| 1206 | - \OC::$CLI |
|
| 1207 | - ); |
|
| 1208 | - }); |
|
| 1199 | + $this->registerService(Installer::class, function (ContainerInterface $c) { |
|
| 1200 | + return new Installer( |
|
| 1201 | + $c->get(AppFetcher::class), |
|
| 1202 | + $c->get(IClientService::class), |
|
| 1203 | + $c->get(ITempManager::class), |
|
| 1204 | + $c->get(LoggerInterface::class), |
|
| 1205 | + $c->get(\OCP\IConfig::class), |
|
| 1206 | + \OC::$CLI |
|
| 1207 | + ); |
|
| 1208 | + }); |
|
| 1209 | 1209 | |
| 1210 | - $this->registerService(IApiFactory::class, function (ContainerInterface $c) { |
|
| 1211 | - return new ApiFactory($c->get(IClientService::class)); |
|
| 1212 | - }); |
|
| 1210 | + $this->registerService(IApiFactory::class, function (ContainerInterface $c) { |
|
| 1211 | + return new ApiFactory($c->get(IClientService::class)); |
|
| 1212 | + }); |
|
| 1213 | 1213 | |
| 1214 | - $this->registerService(IInstanceFactory::class, function (ContainerInterface $c) { |
|
| 1215 | - $memcacheFactory = $c->get(ICacheFactory::class); |
|
| 1216 | - return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->get(IClientService::class)); |
|
| 1217 | - }); |
|
| 1214 | + $this->registerService(IInstanceFactory::class, function (ContainerInterface $c) { |
|
| 1215 | + $memcacheFactory = $c->get(ICacheFactory::class); |
|
| 1216 | + return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->get(IClientService::class)); |
|
| 1217 | + }); |
|
| 1218 | 1218 | |
| 1219 | - $this->registerAlias(IContactsStore::class, ContactsStore::class); |
|
| 1220 | - $this->registerAlias(IAccountManager::class, AccountManager::class); |
|
| 1219 | + $this->registerAlias(IContactsStore::class, ContactsStore::class); |
|
| 1220 | + $this->registerAlias(IAccountManager::class, AccountManager::class); |
|
| 1221 | 1221 | |
| 1222 | - $this->registerAlias(IStorageFactory::class, StorageFactory::class); |
|
| 1222 | + $this->registerAlias(IStorageFactory::class, StorageFactory::class); |
|
| 1223 | 1223 | |
| 1224 | - $this->registerAlias(\OCP\Dashboard\IManager::class, \OC\Dashboard\Manager::class); |
|
| 1224 | + $this->registerAlias(\OCP\Dashboard\IManager::class, \OC\Dashboard\Manager::class); |
|
| 1225 | 1225 | |
| 1226 | - $this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class); |
|
| 1227 | - $this->registerAlias(IFilesMetadataManager::class, FilesMetadataManager::class); |
|
| 1226 | + $this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class); |
|
| 1227 | + $this->registerAlias(IFilesMetadataManager::class, FilesMetadataManager::class); |
|
| 1228 | 1228 | |
| 1229 | - $this->registerAlias(ISubAdmin::class, SubAdmin::class); |
|
| 1229 | + $this->registerAlias(ISubAdmin::class, SubAdmin::class); |
|
| 1230 | 1230 | |
| 1231 | - $this->registerAlias(IInitialStateService::class, InitialStateService::class); |
|
| 1231 | + $this->registerAlias(IInitialStateService::class, InitialStateService::class); |
|
| 1232 | 1232 | |
| 1233 | - $this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class); |
|
| 1233 | + $this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class); |
|
| 1234 | 1234 | |
| 1235 | - $this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class); |
|
| 1235 | + $this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class); |
|
| 1236 | 1236 | |
| 1237 | - $this->registerAlias(IBroker::class, Broker::class); |
|
| 1237 | + $this->registerAlias(IBroker::class, Broker::class); |
|
| 1238 | 1238 | |
| 1239 | - $this->registerAlias(\OCP\Files\AppData\IAppDataFactory::class, \OC\Files\AppData\Factory::class); |
|
| 1239 | + $this->registerAlias(\OCP\Files\AppData\IAppDataFactory::class, \OC\Files\AppData\Factory::class); |
|
| 1240 | 1240 | |
| 1241 | - $this->registerAlias(\OCP\Files\IFilenameValidator::class, \OC\Files\FilenameValidator::class); |
|
| 1241 | + $this->registerAlias(\OCP\Files\IFilenameValidator::class, \OC\Files\FilenameValidator::class); |
|
| 1242 | 1242 | |
| 1243 | - $this->registerAlias(IBinaryFinder::class, BinaryFinder::class); |
|
| 1243 | + $this->registerAlias(IBinaryFinder::class, BinaryFinder::class); |
|
| 1244 | 1244 | |
| 1245 | - $this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class); |
|
| 1245 | + $this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class); |
|
| 1246 | 1246 | |
| 1247 | - $this->registerAlias(ITranslationManager::class, TranslationManager::class); |
|
| 1247 | + $this->registerAlias(ITranslationManager::class, TranslationManager::class); |
|
| 1248 | 1248 | |
| 1249 | - $this->registerAlias(IConversionManager::class, ConversionManager::class); |
|
| 1249 | + $this->registerAlias(IConversionManager::class, ConversionManager::class); |
|
| 1250 | 1250 | |
| 1251 | - $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class); |
|
| 1251 | + $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class); |
|
| 1252 | 1252 | |
| 1253 | - $this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class); |
|
| 1253 | + $this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class); |
|
| 1254 | 1254 | |
| 1255 | - $this->registerAlias(\OCP\TextProcessing\IManager::class, \OC\TextProcessing\Manager::class); |
|
| 1255 | + $this->registerAlias(\OCP\TextProcessing\IManager::class, \OC\TextProcessing\Manager::class); |
|
| 1256 | 1256 | |
| 1257 | - $this->registerAlias(\OCP\TextToImage\IManager::class, \OC\TextToImage\Manager::class); |
|
| 1257 | + $this->registerAlias(\OCP\TextToImage\IManager::class, \OC\TextToImage\Manager::class); |
|
| 1258 | 1258 | |
| 1259 | - $this->registerAlias(ILimiter::class, Limiter::class); |
|
| 1259 | + $this->registerAlias(ILimiter::class, Limiter::class); |
|
| 1260 | 1260 | |
| 1261 | - $this->registerAlias(IPhoneNumberUtil::class, PhoneNumberUtil::class); |
|
| 1261 | + $this->registerAlias(IPhoneNumberUtil::class, PhoneNumberUtil::class); |
|
| 1262 | 1262 | |
| 1263 | - $this->registerAlias(ICapabilityAwareOCMProvider::class, OCMProvider::class); |
|
| 1264 | - $this->registerDeprecatedAlias(IOCMProvider::class, OCMProvider::class); |
|
| 1263 | + $this->registerAlias(ICapabilityAwareOCMProvider::class, OCMProvider::class); |
|
| 1264 | + $this->registerDeprecatedAlias(IOCMProvider::class, OCMProvider::class); |
|
| 1265 | 1265 | |
| 1266 | - $this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class); |
|
| 1266 | + $this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class); |
|
| 1267 | 1267 | |
| 1268 | - $this->registerAlias(IProfileManager::class, ProfileManager::class); |
|
| 1268 | + $this->registerAlias(IProfileManager::class, ProfileManager::class); |
|
| 1269 | 1269 | |
| 1270 | - $this->registerAlias(IAvailabilityCoordinator::class, AvailabilityCoordinator::class); |
|
| 1270 | + $this->registerAlias(IAvailabilityCoordinator::class, AvailabilityCoordinator::class); |
|
| 1271 | 1271 | |
| 1272 | - $this->registerAlias(IDeclarativeManager::class, DeclarativeManager::class); |
|
| 1272 | + $this->registerAlias(IDeclarativeManager::class, DeclarativeManager::class); |
|
| 1273 | 1273 | |
| 1274 | - $this->registerAlias(\OCP\TaskProcessing\IManager::class, \OC\TaskProcessing\Manager::class); |
|
| 1274 | + $this->registerAlias(\OCP\TaskProcessing\IManager::class, \OC\TaskProcessing\Manager::class); |
|
| 1275 | 1275 | |
| 1276 | - $this->registerAlias(IRemoteAddress::class, RemoteAddress::class); |
|
| 1276 | + $this->registerAlias(IRemoteAddress::class, RemoteAddress::class); |
|
| 1277 | 1277 | |
| 1278 | - $this->registerAlias(\OCP\Security\Ip\IFactory::class, \OC\Security\Ip\Factory::class); |
|
| 1279 | - |
|
| 1280 | - $this->registerAlias(IRichTextFormatter::class, \OC\RichObjectStrings\RichTextFormatter::class); |
|
| 1281 | - |
|
| 1282 | - $this->registerAlias(ISignatureManager::class, SignatureManager::class); |
|
| 1283 | - |
|
| 1284 | - $this->connectDispatcher(); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - public function boot() { |
|
| 1288 | - /** @var HookConnector $hookConnector */ |
|
| 1289 | - $hookConnector = $this->get(HookConnector::class); |
|
| 1290 | - $hookConnector->viewToNode(); |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - private function connectDispatcher(): void { |
|
| 1294 | - /** @var IEventDispatcher $eventDispatcher */ |
|
| 1295 | - $eventDispatcher = $this->get(IEventDispatcher::class); |
|
| 1296 | - $eventDispatcher->addServiceListener(LoginFailed::class, LoginFailedListener::class); |
|
| 1297 | - $eventDispatcher->addServiceListener(PostLoginEvent::class, UserLoggedInListener::class); |
|
| 1298 | - $eventDispatcher->addServiceListener(UserChangedEvent::class, UserChangedListener::class); |
|
| 1299 | - $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); |
|
| 1300 | - |
|
| 1301 | - FilesMetadataManager::loadListeners($eventDispatcher); |
|
| 1302 | - GenerateBlurhashMetadata::loadListeners($eventDispatcher); |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - /** |
|
| 1306 | - * @return \OCP\Contacts\IManager |
|
| 1307 | - * @deprecated 20.0.0 |
|
| 1308 | - */ |
|
| 1309 | - public function getContactsManager() { |
|
| 1310 | - return $this->get(\OCP\Contacts\IManager::class); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * @return \OC\Encryption\Manager |
|
| 1315 | - * @deprecated 20.0.0 |
|
| 1316 | - */ |
|
| 1317 | - public function getEncryptionManager() { |
|
| 1318 | - return $this->get(\OCP\Encryption\IManager::class); |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - /** |
|
| 1322 | - * @return \OC\Encryption\File |
|
| 1323 | - * @deprecated 20.0.0 |
|
| 1324 | - */ |
|
| 1325 | - public function getEncryptionFilesHelper() { |
|
| 1326 | - return $this->get(IFile::class); |
|
| 1327 | - } |
|
| 1328 | - |
|
| 1329 | - /** |
|
| 1330 | - * The current request object holding all information about the request |
|
| 1331 | - * currently being processed is returned from this method. |
|
| 1332 | - * In case the current execution was not initiated by a web request null is returned |
|
| 1333 | - * |
|
| 1334 | - * @return \OCP\IRequest |
|
| 1335 | - * @deprecated 20.0.0 |
|
| 1336 | - */ |
|
| 1337 | - public function getRequest() { |
|
| 1338 | - return $this->get(IRequest::class); |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * Returns the root folder of ownCloud's data directory |
|
| 1343 | - * |
|
| 1344 | - * @return IRootFolder |
|
| 1345 | - * @deprecated 20.0.0 |
|
| 1346 | - */ |
|
| 1347 | - public function getRootFolder() { |
|
| 1348 | - return $this->get(IRootFolder::class); |
|
| 1349 | - } |
|
| 1350 | - |
|
| 1351 | - /** |
|
| 1352 | - * Returns the root folder of ownCloud's data directory |
|
| 1353 | - * This is the lazy variant so this gets only initialized once it |
|
| 1354 | - * is actually used. |
|
| 1355 | - * |
|
| 1356 | - * @return IRootFolder |
|
| 1357 | - * @deprecated 20.0.0 |
|
| 1358 | - */ |
|
| 1359 | - public function getLazyRootFolder() { |
|
| 1360 | - return $this->get(IRootFolder::class); |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - /** |
|
| 1364 | - * Returns a view to ownCloud's files folder |
|
| 1365 | - * |
|
| 1366 | - * @param string $userId user ID |
|
| 1367 | - * @return \OCP\Files\Folder|null |
|
| 1368 | - * @deprecated 20.0.0 |
|
| 1369 | - */ |
|
| 1370 | - public function getUserFolder($userId = null) { |
|
| 1371 | - if ($userId === null) { |
|
| 1372 | - $user = $this->get(IUserSession::class)->getUser(); |
|
| 1373 | - if (!$user) { |
|
| 1374 | - return null; |
|
| 1375 | - } |
|
| 1376 | - $userId = $user->getUID(); |
|
| 1377 | - } |
|
| 1378 | - $root = $this->get(IRootFolder::class); |
|
| 1379 | - return $root->getUserFolder($userId); |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - /** |
|
| 1383 | - * @return \OC\User\Manager |
|
| 1384 | - * @deprecated 20.0.0 |
|
| 1385 | - */ |
|
| 1386 | - public function getUserManager() { |
|
| 1387 | - return $this->get(IUserManager::class); |
|
| 1388 | - } |
|
| 1389 | - |
|
| 1390 | - /** |
|
| 1391 | - * @return \OC\Group\Manager |
|
| 1392 | - * @deprecated 20.0.0 |
|
| 1393 | - */ |
|
| 1394 | - public function getGroupManager() { |
|
| 1395 | - return $this->get(IGroupManager::class); |
|
| 1396 | - } |
|
| 1397 | - |
|
| 1398 | - /** |
|
| 1399 | - * @return \OC\User\Session |
|
| 1400 | - * @deprecated 20.0.0 |
|
| 1401 | - */ |
|
| 1402 | - public function getUserSession() { |
|
| 1403 | - return $this->get(IUserSession::class); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - /** |
|
| 1407 | - * @return \OCP\ISession |
|
| 1408 | - * @deprecated 20.0.0 |
|
| 1409 | - */ |
|
| 1410 | - public function getSession() { |
|
| 1411 | - return $this->get(Session::class)->getSession(); |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - /** |
|
| 1415 | - * @param \OCP\ISession $session |
|
| 1416 | - * @return void |
|
| 1417 | - */ |
|
| 1418 | - public function setSession(\OCP\ISession $session) { |
|
| 1419 | - $this->get(SessionStorage::class)->setSession($session); |
|
| 1420 | - $this->get(Session::class)->setSession($session); |
|
| 1421 | - $this->get(Store::class)->setSession($session); |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - /** |
|
| 1425 | - * @return \OCP\IConfig |
|
| 1426 | - * @deprecated 20.0.0 |
|
| 1427 | - */ |
|
| 1428 | - public function getConfig() { |
|
| 1429 | - return $this->get(AllConfig::class); |
|
| 1430 | - } |
|
| 1431 | - |
|
| 1432 | - /** |
|
| 1433 | - * @return \OC\SystemConfig |
|
| 1434 | - * @deprecated 20.0.0 |
|
| 1435 | - */ |
|
| 1436 | - public function getSystemConfig() { |
|
| 1437 | - return $this->get(SystemConfig::class); |
|
| 1438 | - } |
|
| 1439 | - |
|
| 1440 | - /** |
|
| 1441 | - * @return IFactory |
|
| 1442 | - * @deprecated 20.0.0 |
|
| 1443 | - */ |
|
| 1444 | - public function getL10NFactory() { |
|
| 1445 | - return $this->get(IFactory::class); |
|
| 1446 | - } |
|
| 1447 | - |
|
| 1448 | - /** |
|
| 1449 | - * get an L10N instance |
|
| 1450 | - * |
|
| 1451 | - * @param string $app appid |
|
| 1452 | - * @param string $lang |
|
| 1453 | - * @return IL10N |
|
| 1454 | - * @deprecated 20.0.0 use DI of {@see IL10N} or {@see IFactory} instead, or {@see \OCP\Util::getL10N()} as a last resort |
|
| 1455 | - */ |
|
| 1456 | - public function getL10N($app, $lang = null) { |
|
| 1457 | - return $this->get(IFactory::class)->get($app, $lang); |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - /** |
|
| 1461 | - * @return IURLGenerator |
|
| 1462 | - * @deprecated 20.0.0 |
|
| 1463 | - */ |
|
| 1464 | - public function getURLGenerator() { |
|
| 1465 | - return $this->get(IURLGenerator::class); |
|
| 1466 | - } |
|
| 1467 | - |
|
| 1468 | - /** |
|
| 1469 | - * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use |
|
| 1470 | - * getMemCacheFactory() instead. |
|
| 1471 | - * |
|
| 1472 | - * @return ICache |
|
| 1473 | - * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache |
|
| 1474 | - */ |
|
| 1475 | - public function getCache() { |
|
| 1476 | - return $this->get(ICache::class); |
|
| 1477 | - } |
|
| 1478 | - |
|
| 1479 | - /** |
|
| 1480 | - * Returns an \OCP\CacheFactory instance |
|
| 1481 | - * |
|
| 1482 | - * @return \OCP\ICacheFactory |
|
| 1483 | - * @deprecated 20.0.0 |
|
| 1484 | - */ |
|
| 1485 | - public function getMemCacheFactory() { |
|
| 1486 | - return $this->get(ICacheFactory::class); |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - /** |
|
| 1490 | - * Returns the current session |
|
| 1491 | - * |
|
| 1492 | - * @return \OCP\IDBConnection |
|
| 1493 | - * @deprecated 20.0.0 |
|
| 1494 | - */ |
|
| 1495 | - public function getDatabaseConnection() { |
|
| 1496 | - return $this->get(IDBConnection::class); |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - /** |
|
| 1500 | - * Returns the activity manager |
|
| 1501 | - * |
|
| 1502 | - * @return \OCP\Activity\IManager |
|
| 1503 | - * @deprecated 20.0.0 |
|
| 1504 | - */ |
|
| 1505 | - public function getActivityManager() { |
|
| 1506 | - return $this->get(\OCP\Activity\IManager::class); |
|
| 1507 | - } |
|
| 1508 | - |
|
| 1509 | - /** |
|
| 1510 | - * Returns an job list for controlling background jobs |
|
| 1511 | - * |
|
| 1512 | - * @return IJobList |
|
| 1513 | - * @deprecated 20.0.0 |
|
| 1514 | - */ |
|
| 1515 | - public function getJobList() { |
|
| 1516 | - return $this->get(IJobList::class); |
|
| 1517 | - } |
|
| 1518 | - |
|
| 1519 | - /** |
|
| 1520 | - * Returns a SecureRandom instance |
|
| 1521 | - * |
|
| 1522 | - * @return \OCP\Security\ISecureRandom |
|
| 1523 | - * @deprecated 20.0.0 |
|
| 1524 | - */ |
|
| 1525 | - public function getSecureRandom() { |
|
| 1526 | - return $this->get(ISecureRandom::class); |
|
| 1527 | - } |
|
| 1528 | - |
|
| 1529 | - /** |
|
| 1530 | - * Returns a Crypto instance |
|
| 1531 | - * |
|
| 1532 | - * @return ICrypto |
|
| 1533 | - * @deprecated 20.0.0 |
|
| 1534 | - */ |
|
| 1535 | - public function getCrypto() { |
|
| 1536 | - return $this->get(ICrypto::class); |
|
| 1537 | - } |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * Returns a Hasher instance |
|
| 1541 | - * |
|
| 1542 | - * @return IHasher |
|
| 1543 | - * @deprecated 20.0.0 |
|
| 1544 | - */ |
|
| 1545 | - public function getHasher() { |
|
| 1546 | - return $this->get(IHasher::class); |
|
| 1547 | - } |
|
| 1548 | - |
|
| 1549 | - /** |
|
| 1550 | - * Get the certificate manager |
|
| 1551 | - * |
|
| 1552 | - * @return \OCP\ICertificateManager |
|
| 1553 | - */ |
|
| 1554 | - public function getCertificateManager() { |
|
| 1555 | - return $this->get(ICertificateManager::class); |
|
| 1556 | - } |
|
| 1557 | - |
|
| 1558 | - /** |
|
| 1559 | - * Get the manager for temporary files and folders |
|
| 1560 | - * |
|
| 1561 | - * @return \OCP\ITempManager |
|
| 1562 | - * @deprecated 20.0.0 |
|
| 1563 | - */ |
|
| 1564 | - public function getTempManager() { |
|
| 1565 | - return $this->get(ITempManager::class); |
|
| 1566 | - } |
|
| 1567 | - |
|
| 1568 | - /** |
|
| 1569 | - * Get the app manager |
|
| 1570 | - * |
|
| 1571 | - * @return \OCP\App\IAppManager |
|
| 1572 | - * @deprecated 20.0.0 |
|
| 1573 | - */ |
|
| 1574 | - public function getAppManager() { |
|
| 1575 | - return $this->get(IAppManager::class); |
|
| 1576 | - } |
|
| 1577 | - |
|
| 1578 | - /** |
|
| 1579 | - * Creates a new mailer |
|
| 1580 | - * |
|
| 1581 | - * @return IMailer |
|
| 1582 | - * @deprecated 20.0.0 |
|
| 1583 | - */ |
|
| 1584 | - public function getMailer() { |
|
| 1585 | - return $this->get(IMailer::class); |
|
| 1586 | - } |
|
| 1587 | - |
|
| 1588 | - /** |
|
| 1589 | - * Get the webroot |
|
| 1590 | - * |
|
| 1591 | - * @return string |
|
| 1592 | - * @deprecated 20.0.0 |
|
| 1593 | - */ |
|
| 1594 | - public function getWebRoot() { |
|
| 1595 | - return $this->webRoot; |
|
| 1596 | - } |
|
| 1597 | - |
|
| 1598 | - /** |
|
| 1599 | - * Get the locking provider |
|
| 1600 | - * |
|
| 1601 | - * @return ILockingProvider |
|
| 1602 | - * @since 8.1.0 |
|
| 1603 | - * @deprecated 20.0.0 |
|
| 1604 | - */ |
|
| 1605 | - public function getLockingProvider() { |
|
| 1606 | - return $this->get(ILockingProvider::class); |
|
| 1607 | - } |
|
| 1608 | - |
|
| 1609 | - /** |
|
| 1610 | - * Get the MimeTypeDetector |
|
| 1611 | - * |
|
| 1612 | - * @return IMimeTypeDetector |
|
| 1613 | - * @deprecated 20.0.0 |
|
| 1614 | - */ |
|
| 1615 | - public function getMimeTypeDetector() { |
|
| 1616 | - return $this->get(IMimeTypeDetector::class); |
|
| 1617 | - } |
|
| 1618 | - |
|
| 1619 | - /** |
|
| 1620 | - * Get the MimeTypeLoader |
|
| 1621 | - * |
|
| 1622 | - * @return IMimeTypeLoader |
|
| 1623 | - * @deprecated 20.0.0 |
|
| 1624 | - */ |
|
| 1625 | - public function getMimeTypeLoader() { |
|
| 1626 | - return $this->get(IMimeTypeLoader::class); |
|
| 1627 | - } |
|
| 1628 | - |
|
| 1629 | - /** |
|
| 1630 | - * Get the Notification Manager |
|
| 1631 | - * |
|
| 1632 | - * @return \OCP\Notification\IManager |
|
| 1633 | - * @since 8.2.0 |
|
| 1634 | - * @deprecated 20.0.0 |
|
| 1635 | - */ |
|
| 1636 | - public function getNotificationManager() { |
|
| 1637 | - return $this->get(\OCP\Notification\IManager::class); |
|
| 1638 | - } |
|
| 1639 | - |
|
| 1640 | - /** |
|
| 1641 | - * @return \OCA\Theming\ThemingDefaults |
|
| 1642 | - * @deprecated 20.0.0 |
|
| 1643 | - */ |
|
| 1644 | - public function getThemingDefaults() { |
|
| 1645 | - return $this->get('ThemingDefaults'); |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - /** |
|
| 1649 | - * @return \OC\IntegrityCheck\Checker |
|
| 1650 | - * @deprecated 20.0.0 |
|
| 1651 | - */ |
|
| 1652 | - public function getIntegrityCodeChecker() { |
|
| 1653 | - return $this->get('IntegrityCodeChecker'); |
|
| 1654 | - } |
|
| 1655 | - |
|
| 1656 | - /** |
|
| 1657 | - * @return CsrfTokenManager |
|
| 1658 | - * @deprecated 20.0.0 |
|
| 1659 | - */ |
|
| 1660 | - public function getCsrfTokenManager() { |
|
| 1661 | - return $this->get(CsrfTokenManager::class); |
|
| 1662 | - } |
|
| 1663 | - |
|
| 1664 | - /** |
|
| 1665 | - * @return ContentSecurityPolicyNonceManager |
|
| 1666 | - * @deprecated 20.0.0 |
|
| 1667 | - */ |
|
| 1668 | - public function getContentSecurityPolicyNonceManager() { |
|
| 1669 | - return $this->get(ContentSecurityPolicyNonceManager::class); |
|
| 1670 | - } |
|
| 1671 | - |
|
| 1672 | - /** |
|
| 1673 | - * @return \OCP\Settings\IManager |
|
| 1674 | - * @deprecated 20.0.0 |
|
| 1675 | - */ |
|
| 1676 | - public function getSettingsManager() { |
|
| 1677 | - return $this->get(\OC\Settings\Manager::class); |
|
| 1678 | - } |
|
| 1679 | - |
|
| 1680 | - /** |
|
| 1681 | - * @return \OCP\Files\IAppData |
|
| 1682 | - * @deprecated 20.0.0 Use get(\OCP\Files\AppData\IAppDataFactory::class)->get($app) instead |
|
| 1683 | - */ |
|
| 1684 | - public function getAppDataDir($app) { |
|
| 1685 | - /** @var \OC\Files\AppData\Factory $factory */ |
|
| 1686 | - $factory = $this->get(\OC\Files\AppData\Factory::class); |
|
| 1687 | - return $factory->get($app); |
|
| 1688 | - } |
|
| 1689 | - |
|
| 1690 | - /** |
|
| 1691 | - * @return \OCP\Federation\ICloudIdManager |
|
| 1692 | - * @deprecated 20.0.0 |
|
| 1693 | - */ |
|
| 1694 | - public function getCloudIdManager() { |
|
| 1695 | - return $this->get(ICloudIdManager::class); |
|
| 1696 | - } |
|
| 1697 | - |
|
| 1698 | - private function registerDeprecatedAlias(string $alias, string $target) { |
|
| 1699 | - $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias) { |
|
| 1700 | - try { |
|
| 1701 | - /** @var LoggerInterface $logger */ |
|
| 1702 | - $logger = $container->get(LoggerInterface::class); |
|
| 1703 | - $logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']); |
|
| 1704 | - } catch (ContainerExceptionInterface $e) { |
|
| 1705 | - // Could not get logger. Continue |
|
| 1706 | - } |
|
| 1707 | - |
|
| 1708 | - return $container->get($target); |
|
| 1709 | - }, false); |
|
| 1710 | - } |
|
| 1278 | + $this->registerAlias(\OCP\Security\Ip\IFactory::class, \OC\Security\Ip\Factory::class); |
|
| 1279 | + |
|
| 1280 | + $this->registerAlias(IRichTextFormatter::class, \OC\RichObjectStrings\RichTextFormatter::class); |
|
| 1281 | + |
|
| 1282 | + $this->registerAlias(ISignatureManager::class, SignatureManager::class); |
|
| 1283 | + |
|
| 1284 | + $this->connectDispatcher(); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + public function boot() { |
|
| 1288 | + /** @var HookConnector $hookConnector */ |
|
| 1289 | + $hookConnector = $this->get(HookConnector::class); |
|
| 1290 | + $hookConnector->viewToNode(); |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + private function connectDispatcher(): void { |
|
| 1294 | + /** @var IEventDispatcher $eventDispatcher */ |
|
| 1295 | + $eventDispatcher = $this->get(IEventDispatcher::class); |
|
| 1296 | + $eventDispatcher->addServiceListener(LoginFailed::class, LoginFailedListener::class); |
|
| 1297 | + $eventDispatcher->addServiceListener(PostLoginEvent::class, UserLoggedInListener::class); |
|
| 1298 | + $eventDispatcher->addServiceListener(UserChangedEvent::class, UserChangedListener::class); |
|
| 1299 | + $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); |
|
| 1300 | + |
|
| 1301 | + FilesMetadataManager::loadListeners($eventDispatcher); |
|
| 1302 | + GenerateBlurhashMetadata::loadListeners($eventDispatcher); |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + /** |
|
| 1306 | + * @return \OCP\Contacts\IManager |
|
| 1307 | + * @deprecated 20.0.0 |
|
| 1308 | + */ |
|
| 1309 | + public function getContactsManager() { |
|
| 1310 | + return $this->get(\OCP\Contacts\IManager::class); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * @return \OC\Encryption\Manager |
|
| 1315 | + * @deprecated 20.0.0 |
|
| 1316 | + */ |
|
| 1317 | + public function getEncryptionManager() { |
|
| 1318 | + return $this->get(\OCP\Encryption\IManager::class); |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + /** |
|
| 1322 | + * @return \OC\Encryption\File |
|
| 1323 | + * @deprecated 20.0.0 |
|
| 1324 | + */ |
|
| 1325 | + public function getEncryptionFilesHelper() { |
|
| 1326 | + return $this->get(IFile::class); |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + /** |
|
| 1330 | + * The current request object holding all information about the request |
|
| 1331 | + * currently being processed is returned from this method. |
|
| 1332 | + * In case the current execution was not initiated by a web request null is returned |
|
| 1333 | + * |
|
| 1334 | + * @return \OCP\IRequest |
|
| 1335 | + * @deprecated 20.0.0 |
|
| 1336 | + */ |
|
| 1337 | + public function getRequest() { |
|
| 1338 | + return $this->get(IRequest::class); |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * Returns the root folder of ownCloud's data directory |
|
| 1343 | + * |
|
| 1344 | + * @return IRootFolder |
|
| 1345 | + * @deprecated 20.0.0 |
|
| 1346 | + */ |
|
| 1347 | + public function getRootFolder() { |
|
| 1348 | + return $this->get(IRootFolder::class); |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + /** |
|
| 1352 | + * Returns the root folder of ownCloud's data directory |
|
| 1353 | + * This is the lazy variant so this gets only initialized once it |
|
| 1354 | + * is actually used. |
|
| 1355 | + * |
|
| 1356 | + * @return IRootFolder |
|
| 1357 | + * @deprecated 20.0.0 |
|
| 1358 | + */ |
|
| 1359 | + public function getLazyRootFolder() { |
|
| 1360 | + return $this->get(IRootFolder::class); |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + /** |
|
| 1364 | + * Returns a view to ownCloud's files folder |
|
| 1365 | + * |
|
| 1366 | + * @param string $userId user ID |
|
| 1367 | + * @return \OCP\Files\Folder|null |
|
| 1368 | + * @deprecated 20.0.0 |
|
| 1369 | + */ |
|
| 1370 | + public function getUserFolder($userId = null) { |
|
| 1371 | + if ($userId === null) { |
|
| 1372 | + $user = $this->get(IUserSession::class)->getUser(); |
|
| 1373 | + if (!$user) { |
|
| 1374 | + return null; |
|
| 1375 | + } |
|
| 1376 | + $userId = $user->getUID(); |
|
| 1377 | + } |
|
| 1378 | + $root = $this->get(IRootFolder::class); |
|
| 1379 | + return $root->getUserFolder($userId); |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + /** |
|
| 1383 | + * @return \OC\User\Manager |
|
| 1384 | + * @deprecated 20.0.0 |
|
| 1385 | + */ |
|
| 1386 | + public function getUserManager() { |
|
| 1387 | + return $this->get(IUserManager::class); |
|
| 1388 | + } |
|
| 1389 | + |
|
| 1390 | + /** |
|
| 1391 | + * @return \OC\Group\Manager |
|
| 1392 | + * @deprecated 20.0.0 |
|
| 1393 | + */ |
|
| 1394 | + public function getGroupManager() { |
|
| 1395 | + return $this->get(IGroupManager::class); |
|
| 1396 | + } |
|
| 1397 | + |
|
| 1398 | + /** |
|
| 1399 | + * @return \OC\User\Session |
|
| 1400 | + * @deprecated 20.0.0 |
|
| 1401 | + */ |
|
| 1402 | + public function getUserSession() { |
|
| 1403 | + return $this->get(IUserSession::class); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + /** |
|
| 1407 | + * @return \OCP\ISession |
|
| 1408 | + * @deprecated 20.0.0 |
|
| 1409 | + */ |
|
| 1410 | + public function getSession() { |
|
| 1411 | + return $this->get(Session::class)->getSession(); |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + /** |
|
| 1415 | + * @param \OCP\ISession $session |
|
| 1416 | + * @return void |
|
| 1417 | + */ |
|
| 1418 | + public function setSession(\OCP\ISession $session) { |
|
| 1419 | + $this->get(SessionStorage::class)->setSession($session); |
|
| 1420 | + $this->get(Session::class)->setSession($session); |
|
| 1421 | + $this->get(Store::class)->setSession($session); |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + /** |
|
| 1425 | + * @return \OCP\IConfig |
|
| 1426 | + * @deprecated 20.0.0 |
|
| 1427 | + */ |
|
| 1428 | + public function getConfig() { |
|
| 1429 | + return $this->get(AllConfig::class); |
|
| 1430 | + } |
|
| 1431 | + |
|
| 1432 | + /** |
|
| 1433 | + * @return \OC\SystemConfig |
|
| 1434 | + * @deprecated 20.0.0 |
|
| 1435 | + */ |
|
| 1436 | + public function getSystemConfig() { |
|
| 1437 | + return $this->get(SystemConfig::class); |
|
| 1438 | + } |
|
| 1439 | + |
|
| 1440 | + /** |
|
| 1441 | + * @return IFactory |
|
| 1442 | + * @deprecated 20.0.0 |
|
| 1443 | + */ |
|
| 1444 | + public function getL10NFactory() { |
|
| 1445 | + return $this->get(IFactory::class); |
|
| 1446 | + } |
|
| 1447 | + |
|
| 1448 | + /** |
|
| 1449 | + * get an L10N instance |
|
| 1450 | + * |
|
| 1451 | + * @param string $app appid |
|
| 1452 | + * @param string $lang |
|
| 1453 | + * @return IL10N |
|
| 1454 | + * @deprecated 20.0.0 use DI of {@see IL10N} or {@see IFactory} instead, or {@see \OCP\Util::getL10N()} as a last resort |
|
| 1455 | + */ |
|
| 1456 | + public function getL10N($app, $lang = null) { |
|
| 1457 | + return $this->get(IFactory::class)->get($app, $lang); |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + /** |
|
| 1461 | + * @return IURLGenerator |
|
| 1462 | + * @deprecated 20.0.0 |
|
| 1463 | + */ |
|
| 1464 | + public function getURLGenerator() { |
|
| 1465 | + return $this->get(IURLGenerator::class); |
|
| 1466 | + } |
|
| 1467 | + |
|
| 1468 | + /** |
|
| 1469 | + * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use |
|
| 1470 | + * getMemCacheFactory() instead. |
|
| 1471 | + * |
|
| 1472 | + * @return ICache |
|
| 1473 | + * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache |
|
| 1474 | + */ |
|
| 1475 | + public function getCache() { |
|
| 1476 | + return $this->get(ICache::class); |
|
| 1477 | + } |
|
| 1478 | + |
|
| 1479 | + /** |
|
| 1480 | + * Returns an \OCP\CacheFactory instance |
|
| 1481 | + * |
|
| 1482 | + * @return \OCP\ICacheFactory |
|
| 1483 | + * @deprecated 20.0.0 |
|
| 1484 | + */ |
|
| 1485 | + public function getMemCacheFactory() { |
|
| 1486 | + return $this->get(ICacheFactory::class); |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + /** |
|
| 1490 | + * Returns the current session |
|
| 1491 | + * |
|
| 1492 | + * @return \OCP\IDBConnection |
|
| 1493 | + * @deprecated 20.0.0 |
|
| 1494 | + */ |
|
| 1495 | + public function getDatabaseConnection() { |
|
| 1496 | + return $this->get(IDBConnection::class); |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + /** |
|
| 1500 | + * Returns the activity manager |
|
| 1501 | + * |
|
| 1502 | + * @return \OCP\Activity\IManager |
|
| 1503 | + * @deprecated 20.0.0 |
|
| 1504 | + */ |
|
| 1505 | + public function getActivityManager() { |
|
| 1506 | + return $this->get(\OCP\Activity\IManager::class); |
|
| 1507 | + } |
|
| 1508 | + |
|
| 1509 | + /** |
|
| 1510 | + * Returns an job list for controlling background jobs |
|
| 1511 | + * |
|
| 1512 | + * @return IJobList |
|
| 1513 | + * @deprecated 20.0.0 |
|
| 1514 | + */ |
|
| 1515 | + public function getJobList() { |
|
| 1516 | + return $this->get(IJobList::class); |
|
| 1517 | + } |
|
| 1518 | + |
|
| 1519 | + /** |
|
| 1520 | + * Returns a SecureRandom instance |
|
| 1521 | + * |
|
| 1522 | + * @return \OCP\Security\ISecureRandom |
|
| 1523 | + * @deprecated 20.0.0 |
|
| 1524 | + */ |
|
| 1525 | + public function getSecureRandom() { |
|
| 1526 | + return $this->get(ISecureRandom::class); |
|
| 1527 | + } |
|
| 1528 | + |
|
| 1529 | + /** |
|
| 1530 | + * Returns a Crypto instance |
|
| 1531 | + * |
|
| 1532 | + * @return ICrypto |
|
| 1533 | + * @deprecated 20.0.0 |
|
| 1534 | + */ |
|
| 1535 | + public function getCrypto() { |
|
| 1536 | + return $this->get(ICrypto::class); |
|
| 1537 | + } |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * Returns a Hasher instance |
|
| 1541 | + * |
|
| 1542 | + * @return IHasher |
|
| 1543 | + * @deprecated 20.0.0 |
|
| 1544 | + */ |
|
| 1545 | + public function getHasher() { |
|
| 1546 | + return $this->get(IHasher::class); |
|
| 1547 | + } |
|
| 1548 | + |
|
| 1549 | + /** |
|
| 1550 | + * Get the certificate manager |
|
| 1551 | + * |
|
| 1552 | + * @return \OCP\ICertificateManager |
|
| 1553 | + */ |
|
| 1554 | + public function getCertificateManager() { |
|
| 1555 | + return $this->get(ICertificateManager::class); |
|
| 1556 | + } |
|
| 1557 | + |
|
| 1558 | + /** |
|
| 1559 | + * Get the manager for temporary files and folders |
|
| 1560 | + * |
|
| 1561 | + * @return \OCP\ITempManager |
|
| 1562 | + * @deprecated 20.0.0 |
|
| 1563 | + */ |
|
| 1564 | + public function getTempManager() { |
|
| 1565 | + return $this->get(ITempManager::class); |
|
| 1566 | + } |
|
| 1567 | + |
|
| 1568 | + /** |
|
| 1569 | + * Get the app manager |
|
| 1570 | + * |
|
| 1571 | + * @return \OCP\App\IAppManager |
|
| 1572 | + * @deprecated 20.0.0 |
|
| 1573 | + */ |
|
| 1574 | + public function getAppManager() { |
|
| 1575 | + return $this->get(IAppManager::class); |
|
| 1576 | + } |
|
| 1577 | + |
|
| 1578 | + /** |
|
| 1579 | + * Creates a new mailer |
|
| 1580 | + * |
|
| 1581 | + * @return IMailer |
|
| 1582 | + * @deprecated 20.0.0 |
|
| 1583 | + */ |
|
| 1584 | + public function getMailer() { |
|
| 1585 | + return $this->get(IMailer::class); |
|
| 1586 | + } |
|
| 1587 | + |
|
| 1588 | + /** |
|
| 1589 | + * Get the webroot |
|
| 1590 | + * |
|
| 1591 | + * @return string |
|
| 1592 | + * @deprecated 20.0.0 |
|
| 1593 | + */ |
|
| 1594 | + public function getWebRoot() { |
|
| 1595 | + return $this->webRoot; |
|
| 1596 | + } |
|
| 1597 | + |
|
| 1598 | + /** |
|
| 1599 | + * Get the locking provider |
|
| 1600 | + * |
|
| 1601 | + * @return ILockingProvider |
|
| 1602 | + * @since 8.1.0 |
|
| 1603 | + * @deprecated 20.0.0 |
|
| 1604 | + */ |
|
| 1605 | + public function getLockingProvider() { |
|
| 1606 | + return $this->get(ILockingProvider::class); |
|
| 1607 | + } |
|
| 1608 | + |
|
| 1609 | + /** |
|
| 1610 | + * Get the MimeTypeDetector |
|
| 1611 | + * |
|
| 1612 | + * @return IMimeTypeDetector |
|
| 1613 | + * @deprecated 20.0.0 |
|
| 1614 | + */ |
|
| 1615 | + public function getMimeTypeDetector() { |
|
| 1616 | + return $this->get(IMimeTypeDetector::class); |
|
| 1617 | + } |
|
| 1618 | + |
|
| 1619 | + /** |
|
| 1620 | + * Get the MimeTypeLoader |
|
| 1621 | + * |
|
| 1622 | + * @return IMimeTypeLoader |
|
| 1623 | + * @deprecated 20.0.0 |
|
| 1624 | + */ |
|
| 1625 | + public function getMimeTypeLoader() { |
|
| 1626 | + return $this->get(IMimeTypeLoader::class); |
|
| 1627 | + } |
|
| 1628 | + |
|
| 1629 | + /** |
|
| 1630 | + * Get the Notification Manager |
|
| 1631 | + * |
|
| 1632 | + * @return \OCP\Notification\IManager |
|
| 1633 | + * @since 8.2.0 |
|
| 1634 | + * @deprecated 20.0.0 |
|
| 1635 | + */ |
|
| 1636 | + public function getNotificationManager() { |
|
| 1637 | + return $this->get(\OCP\Notification\IManager::class); |
|
| 1638 | + } |
|
| 1639 | + |
|
| 1640 | + /** |
|
| 1641 | + * @return \OCA\Theming\ThemingDefaults |
|
| 1642 | + * @deprecated 20.0.0 |
|
| 1643 | + */ |
|
| 1644 | + public function getThemingDefaults() { |
|
| 1645 | + return $this->get('ThemingDefaults'); |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + /** |
|
| 1649 | + * @return \OC\IntegrityCheck\Checker |
|
| 1650 | + * @deprecated 20.0.0 |
|
| 1651 | + */ |
|
| 1652 | + public function getIntegrityCodeChecker() { |
|
| 1653 | + return $this->get('IntegrityCodeChecker'); |
|
| 1654 | + } |
|
| 1655 | + |
|
| 1656 | + /** |
|
| 1657 | + * @return CsrfTokenManager |
|
| 1658 | + * @deprecated 20.0.0 |
|
| 1659 | + */ |
|
| 1660 | + public function getCsrfTokenManager() { |
|
| 1661 | + return $this->get(CsrfTokenManager::class); |
|
| 1662 | + } |
|
| 1663 | + |
|
| 1664 | + /** |
|
| 1665 | + * @return ContentSecurityPolicyNonceManager |
|
| 1666 | + * @deprecated 20.0.0 |
|
| 1667 | + */ |
|
| 1668 | + public function getContentSecurityPolicyNonceManager() { |
|
| 1669 | + return $this->get(ContentSecurityPolicyNonceManager::class); |
|
| 1670 | + } |
|
| 1671 | + |
|
| 1672 | + /** |
|
| 1673 | + * @return \OCP\Settings\IManager |
|
| 1674 | + * @deprecated 20.0.0 |
|
| 1675 | + */ |
|
| 1676 | + public function getSettingsManager() { |
|
| 1677 | + return $this->get(\OC\Settings\Manager::class); |
|
| 1678 | + } |
|
| 1679 | + |
|
| 1680 | + /** |
|
| 1681 | + * @return \OCP\Files\IAppData |
|
| 1682 | + * @deprecated 20.0.0 Use get(\OCP\Files\AppData\IAppDataFactory::class)->get($app) instead |
|
| 1683 | + */ |
|
| 1684 | + public function getAppDataDir($app) { |
|
| 1685 | + /** @var \OC\Files\AppData\Factory $factory */ |
|
| 1686 | + $factory = $this->get(\OC\Files\AppData\Factory::class); |
|
| 1687 | + return $factory->get($app); |
|
| 1688 | + } |
|
| 1689 | + |
|
| 1690 | + /** |
|
| 1691 | + * @return \OCP\Federation\ICloudIdManager |
|
| 1692 | + * @deprecated 20.0.0 |
|
| 1693 | + */ |
|
| 1694 | + public function getCloudIdManager() { |
|
| 1695 | + return $this->get(ICloudIdManager::class); |
|
| 1696 | + } |
|
| 1697 | + |
|
| 1698 | + private function registerDeprecatedAlias(string $alias, string $target) { |
|
| 1699 | + $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias) { |
|
| 1700 | + try { |
|
| 1701 | + /** @var LoggerInterface $logger */ |
|
| 1702 | + $logger = $container->get(LoggerInterface::class); |
|
| 1703 | + $logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']); |
|
| 1704 | + } catch (ContainerExceptionInterface $e) { |
|
| 1705 | + // Could not get logger. Continue |
|
| 1706 | + } |
|
| 1707 | + |
|
| 1708 | + return $container->get($target); |
|
| 1709 | + }, false); |
|
| 1710 | + } |
|
| 1711 | 1711 | } |
@@ -25,226 +25,226 @@ |
||
| 25 | 25 | * @since 32.0.0 |
| 26 | 26 | */ |
| 27 | 27 | class ConfigManager { |
| 28 | - /** @var AppConfig|null $appConfig */ |
|
| 29 | - private ?IAppConfig $appConfig = null; |
|
| 30 | - /** @var UserConfig|null $userConfig */ |
|
| 31 | - private ?IUserConfig $userConfig = null; |
|
| 32 | - |
|
| 33 | - public function __construct( |
|
| 34 | - private readonly LoggerInterface $logger, |
|
| 35 | - ) { |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Use the rename values from the list of ConfigLexiconEntry defined in each app ConfigLexicon |
|
| 40 | - * to migrate config value to a new config key. |
|
| 41 | - * Migration will only occur if new config key has no value in database. |
|
| 42 | - * The previous value from the key set in rename will be deleted from the database when migration |
|
| 43 | - * is over. |
|
| 44 | - * |
|
| 45 | - * This method should be mainly called during a new upgrade or when a new app is enabled. |
|
| 46 | - * |
|
| 47 | - * @see ConfigLexiconEntry |
|
| 48 | - * @internal |
|
| 49 | - * @since 32.0.0 |
|
| 50 | - * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance |
|
| 51 | - */ |
|
| 52 | - public function migrateConfigLexiconKeys(?string $appId = null): void { |
|
| 53 | - if ($appId === null) { |
|
| 54 | - $this->migrateConfigLexiconKeys('core'); |
|
| 55 | - $appManager = Server::get(IAppManager::class); |
|
| 56 | - foreach ($appManager->getEnabledApps() as $app) { |
|
| 57 | - $this->migrateConfigLexiconKeys($app); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $this->loadConfigServices(); |
|
| 64 | - |
|
| 65 | - // it is required to ignore aliases when moving config values |
|
| 66 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 67 | - $this->userConfig->ignoreLexiconAliases(true); |
|
| 68 | - |
|
| 69 | - $this->migrateAppConfigKeys($appId); |
|
| 70 | - $this->migrateUserConfigKeys($appId); |
|
| 71 | - |
|
| 72 | - // switch back to normal behavior |
|
| 73 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 74 | - $this->userConfig->ignoreLexiconAliases(false); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * config services cannot be load at __construct() or install will fail |
|
| 79 | - */ |
|
| 80 | - private function loadConfigServices(): void { |
|
| 81 | - if ($this->appConfig === null) { |
|
| 82 | - $this->appConfig = Server::get(IAppConfig::class); |
|
| 83 | - } |
|
| 84 | - if ($this->userConfig === null) { |
|
| 85 | - $this->userConfig = Server::get(IUserConfig::class); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get details from lexicon related to AppConfig and search for entries with rename to initiate |
|
| 91 | - * a migration to new config key |
|
| 92 | - */ |
|
| 93 | - private function migrateAppConfigKeys(string $appId): void { |
|
| 94 | - $lexicon = $this->appConfig->getConfigDetailsFromLexicon($appId); |
|
| 95 | - foreach ($lexicon['entries'] as $entry) { |
|
| 96 | - // only interested in entries with rename set |
|
| 97 | - if ($entry->getRename() === null) { |
|
| 98 | - continue; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // only migrate if rename config key has a value and the new config key hasn't |
|
| 102 | - if ($this->appConfig->hasKey($appId, $entry->getRename()) |
|
| 103 | - && !$this->appConfig->hasKey($appId, $entry->getKey())) { |
|
| 104 | - try { |
|
| 105 | - $this->migrateAppConfigValue($appId, $entry); |
|
| 106 | - } catch (TypeConflictException $e) { |
|
| 107 | - $this->logger->error('could not migrate AppConfig value', ['appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 108 | - continue; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // we only delete previous config value if migration went fine. |
|
| 113 | - $this->appConfig->deleteKey($appId, $entry->getRename()); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get details from lexicon related to UserConfig and search for entries with rename to initiate |
|
| 119 | - * a migration to new config key |
|
| 120 | - */ |
|
| 121 | - private function migrateUserConfigKeys(string $appId): void { |
|
| 122 | - $lexicon = $this->userConfig->getConfigDetailsFromLexicon($appId); |
|
| 123 | - foreach ($lexicon['entries'] as $entry) { |
|
| 124 | - // only interested in keys with rename set |
|
| 125 | - if ($entry->getRename() === null) { |
|
| 126 | - continue; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - foreach ($this->userConfig->getValuesByUsers($appId, $entry->getRename()) as $userId => $value) { |
|
| 130 | - if ($this->userConfig->hasKey($userId, $appId, $entry->getKey())) { |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - try { |
|
| 135 | - $this->migrateUserConfigValue($userId, $appId, $entry); |
|
| 136 | - } catch (TypeConflictException $e) { |
|
| 137 | - $this->logger->error('could not migrate UserConfig value', ['userId' => $userId, 'appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $this->userConfig->deleteUserConfig($userId, $appId, $entry->getRename()); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * converting value from rename to the new key |
|
| 149 | - * |
|
| 150 | - * @throws TypeConflictException if previous value does not fit the expected type |
|
| 151 | - */ |
|
| 152 | - private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry): void { |
|
| 153 | - $value = $this->appConfig->getValueMixed($appId, $entry->getRename(), lazy: null); |
|
| 154 | - switch ($entry->getValueType()) { |
|
| 155 | - case ValueType::STRING: |
|
| 156 | - $this->appConfig->setValueString($appId, $entry->getKey(), $value); |
|
| 157 | - return; |
|
| 158 | - |
|
| 159 | - case ValueType::INT: |
|
| 160 | - $this->appConfig->setValueInt($appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 161 | - return; |
|
| 162 | - |
|
| 163 | - case ValueType::FLOAT: |
|
| 164 | - $this->appConfig->setValueFloat($appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 165 | - return; |
|
| 166 | - |
|
| 167 | - case ValueType::BOOL: |
|
| 168 | - $this->appConfig->setValueBool($appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 169 | - return; |
|
| 170 | - |
|
| 171 | - case ValueType::ARRAY: |
|
| 172 | - $this->appConfig->setValueArray($appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 173 | - return; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * converting value from rename to the new key |
|
| 179 | - * |
|
| 180 | - * @throws TypeConflictException if previous value does not fit the expected type |
|
| 181 | - */ |
|
| 182 | - private function migrateUserConfigValue(string $userId, string $appId, ConfigLexiconEntry $entry): void { |
|
| 183 | - $value = $this->userConfig->getValueMixed($userId, $appId, $entry->getRename(), lazy: null); |
|
| 184 | - switch ($entry->getValueType()) { |
|
| 185 | - case ValueType::STRING: |
|
| 186 | - $this->userConfig->setValueString($userId, $appId, $entry->getKey(), $value); |
|
| 187 | - return; |
|
| 188 | - |
|
| 189 | - case ValueType::INT: |
|
| 190 | - $this->userConfig->setValueInt($userId, $appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 191 | - return; |
|
| 192 | - |
|
| 193 | - case ValueType::FLOAT: |
|
| 194 | - $this->userConfig->setValueFloat($userId, $appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 195 | - return; |
|
| 196 | - |
|
| 197 | - case ValueType::BOOL: |
|
| 198 | - $this->userConfig->setValueBool($userId, $appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 199 | - return; |
|
| 200 | - |
|
| 201 | - case ValueType::ARRAY: |
|
| 202 | - $this->userConfig->setValueArray($userId, $appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 203 | - return; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - public function convertToInt(string $value): int { |
|
| 208 | - if (!is_numeric($value) || (float)$value <> (int)$value) { |
|
| 209 | - throw new TypeConflictException('Value is not an integer'); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return (int)$value; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - public function convertToFloat(string $value): float { |
|
| 216 | - if (!is_numeric($value)) { |
|
| 217 | - throw new TypeConflictException('Value is not a float'); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return (float)$value; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - public function convertToBool(string $value, ?ConfigLexiconEntry $entry = null): bool { |
|
| 224 | - if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 225 | - $valueBool = true; |
|
| 226 | - } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 227 | - $valueBool = false; |
|
| 228 | - } else { |
|
| 229 | - throw new TypeConflictException('Value cannot be converted to boolean'); |
|
| 230 | - } |
|
| 231 | - if ($entry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN) === true) { |
|
| 232 | - $valueBool = !$valueBool; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $valueBool; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - public function convertToArray(string $value): array { |
|
| 239 | - try { |
|
| 240 | - $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 241 | - } catch (JsonException) { |
|
| 242 | - throw new TypeConflictException('Value is not a valid json'); |
|
| 243 | - } |
|
| 244 | - if (!is_array($valueArray)) { |
|
| 245 | - throw new TypeConflictException('Value is not an array'); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return $valueArray; |
|
| 249 | - } |
|
| 28 | + /** @var AppConfig|null $appConfig */ |
|
| 29 | + private ?IAppConfig $appConfig = null; |
|
| 30 | + /** @var UserConfig|null $userConfig */ |
|
| 31 | + private ?IUserConfig $userConfig = null; |
|
| 32 | + |
|
| 33 | + public function __construct( |
|
| 34 | + private readonly LoggerInterface $logger, |
|
| 35 | + ) { |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Use the rename values from the list of ConfigLexiconEntry defined in each app ConfigLexicon |
|
| 40 | + * to migrate config value to a new config key. |
|
| 41 | + * Migration will only occur if new config key has no value in database. |
|
| 42 | + * The previous value from the key set in rename will be deleted from the database when migration |
|
| 43 | + * is over. |
|
| 44 | + * |
|
| 45 | + * This method should be mainly called during a new upgrade or when a new app is enabled. |
|
| 46 | + * |
|
| 47 | + * @see ConfigLexiconEntry |
|
| 48 | + * @internal |
|
| 49 | + * @since 32.0.0 |
|
| 50 | + * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance |
|
| 51 | + */ |
|
| 52 | + public function migrateConfigLexiconKeys(?string $appId = null): void { |
|
| 53 | + if ($appId === null) { |
|
| 54 | + $this->migrateConfigLexiconKeys('core'); |
|
| 55 | + $appManager = Server::get(IAppManager::class); |
|
| 56 | + foreach ($appManager->getEnabledApps() as $app) { |
|
| 57 | + $this->migrateConfigLexiconKeys($app); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $this->loadConfigServices(); |
|
| 64 | + |
|
| 65 | + // it is required to ignore aliases when moving config values |
|
| 66 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 67 | + $this->userConfig->ignoreLexiconAliases(true); |
|
| 68 | + |
|
| 69 | + $this->migrateAppConfigKeys($appId); |
|
| 70 | + $this->migrateUserConfigKeys($appId); |
|
| 71 | + |
|
| 72 | + // switch back to normal behavior |
|
| 73 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 74 | + $this->userConfig->ignoreLexiconAliases(false); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * config services cannot be load at __construct() or install will fail |
|
| 79 | + */ |
|
| 80 | + private function loadConfigServices(): void { |
|
| 81 | + if ($this->appConfig === null) { |
|
| 82 | + $this->appConfig = Server::get(IAppConfig::class); |
|
| 83 | + } |
|
| 84 | + if ($this->userConfig === null) { |
|
| 85 | + $this->userConfig = Server::get(IUserConfig::class); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get details from lexicon related to AppConfig and search for entries with rename to initiate |
|
| 91 | + * a migration to new config key |
|
| 92 | + */ |
|
| 93 | + private function migrateAppConfigKeys(string $appId): void { |
|
| 94 | + $lexicon = $this->appConfig->getConfigDetailsFromLexicon($appId); |
|
| 95 | + foreach ($lexicon['entries'] as $entry) { |
|
| 96 | + // only interested in entries with rename set |
|
| 97 | + if ($entry->getRename() === null) { |
|
| 98 | + continue; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // only migrate if rename config key has a value and the new config key hasn't |
|
| 102 | + if ($this->appConfig->hasKey($appId, $entry->getRename()) |
|
| 103 | + && !$this->appConfig->hasKey($appId, $entry->getKey())) { |
|
| 104 | + try { |
|
| 105 | + $this->migrateAppConfigValue($appId, $entry); |
|
| 106 | + } catch (TypeConflictException $e) { |
|
| 107 | + $this->logger->error('could not migrate AppConfig value', ['appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 108 | + continue; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // we only delete previous config value if migration went fine. |
|
| 113 | + $this->appConfig->deleteKey($appId, $entry->getRename()); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get details from lexicon related to UserConfig and search for entries with rename to initiate |
|
| 119 | + * a migration to new config key |
|
| 120 | + */ |
|
| 121 | + private function migrateUserConfigKeys(string $appId): void { |
|
| 122 | + $lexicon = $this->userConfig->getConfigDetailsFromLexicon($appId); |
|
| 123 | + foreach ($lexicon['entries'] as $entry) { |
|
| 124 | + // only interested in keys with rename set |
|
| 125 | + if ($entry->getRename() === null) { |
|
| 126 | + continue; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + foreach ($this->userConfig->getValuesByUsers($appId, $entry->getRename()) as $userId => $value) { |
|
| 130 | + if ($this->userConfig->hasKey($userId, $appId, $entry->getKey())) { |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + try { |
|
| 135 | + $this->migrateUserConfigValue($userId, $appId, $entry); |
|
| 136 | + } catch (TypeConflictException $e) { |
|
| 137 | + $this->logger->error('could not migrate UserConfig value', ['userId' => $userId, 'appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $this->userConfig->deleteUserConfig($userId, $appId, $entry->getRename()); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * converting value from rename to the new key |
|
| 149 | + * |
|
| 150 | + * @throws TypeConflictException if previous value does not fit the expected type |
|
| 151 | + */ |
|
| 152 | + private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry): void { |
|
| 153 | + $value = $this->appConfig->getValueMixed($appId, $entry->getRename(), lazy: null); |
|
| 154 | + switch ($entry->getValueType()) { |
|
| 155 | + case ValueType::STRING: |
|
| 156 | + $this->appConfig->setValueString($appId, $entry->getKey(), $value); |
|
| 157 | + return; |
|
| 158 | + |
|
| 159 | + case ValueType::INT: |
|
| 160 | + $this->appConfig->setValueInt($appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 161 | + return; |
|
| 162 | + |
|
| 163 | + case ValueType::FLOAT: |
|
| 164 | + $this->appConfig->setValueFloat($appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 165 | + return; |
|
| 166 | + |
|
| 167 | + case ValueType::BOOL: |
|
| 168 | + $this->appConfig->setValueBool($appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 169 | + return; |
|
| 170 | + |
|
| 171 | + case ValueType::ARRAY: |
|
| 172 | + $this->appConfig->setValueArray($appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 173 | + return; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * converting value from rename to the new key |
|
| 179 | + * |
|
| 180 | + * @throws TypeConflictException if previous value does not fit the expected type |
|
| 181 | + */ |
|
| 182 | + private function migrateUserConfigValue(string $userId, string $appId, ConfigLexiconEntry $entry): void { |
|
| 183 | + $value = $this->userConfig->getValueMixed($userId, $appId, $entry->getRename(), lazy: null); |
|
| 184 | + switch ($entry->getValueType()) { |
|
| 185 | + case ValueType::STRING: |
|
| 186 | + $this->userConfig->setValueString($userId, $appId, $entry->getKey(), $value); |
|
| 187 | + return; |
|
| 188 | + |
|
| 189 | + case ValueType::INT: |
|
| 190 | + $this->userConfig->setValueInt($userId, $appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 191 | + return; |
|
| 192 | + |
|
| 193 | + case ValueType::FLOAT: |
|
| 194 | + $this->userConfig->setValueFloat($userId, $appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 195 | + return; |
|
| 196 | + |
|
| 197 | + case ValueType::BOOL: |
|
| 198 | + $this->userConfig->setValueBool($userId, $appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 199 | + return; |
|
| 200 | + |
|
| 201 | + case ValueType::ARRAY: |
|
| 202 | + $this->userConfig->setValueArray($userId, $appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 203 | + return; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + public function convertToInt(string $value): int { |
|
| 208 | + if (!is_numeric($value) || (float)$value <> (int)$value) { |
|
| 209 | + throw new TypeConflictException('Value is not an integer'); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return (int)$value; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + public function convertToFloat(string $value): float { |
|
| 216 | + if (!is_numeric($value)) { |
|
| 217 | + throw new TypeConflictException('Value is not a float'); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return (float)$value; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + public function convertToBool(string $value, ?ConfigLexiconEntry $entry = null): bool { |
|
| 224 | + if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 225 | + $valueBool = true; |
|
| 226 | + } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 227 | + $valueBool = false; |
|
| 228 | + } else { |
|
| 229 | + throw new TypeConflictException('Value cannot be converted to boolean'); |
|
| 230 | + } |
|
| 231 | + if ($entry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN) === true) { |
|
| 232 | + $valueBool = !$valueBool; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $valueBool; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + public function convertToArray(string $value): array { |
|
| 239 | + try { |
|
| 240 | + $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 241 | + } catch (JsonException) { |
|
| 242 | + throw new TypeConflictException('Value is not a valid json'); |
|
| 243 | + } |
|
| 244 | + if (!is_array($valueArray)) { |
|
| 245 | + throw new TypeConflictException('Value is not an array'); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return $valueArray; |
|
| 249 | + } |
|
| 250 | 250 | } |
@@ -46,1983 +46,1983 @@ |
||
| 46 | 46 | * @since 31.0.0 |
| 47 | 47 | */ |
| 48 | 48 | class UserConfig implements IUserConfig { |
| 49 | - private const USER_MAX_LENGTH = 64; |
|
| 50 | - private const APP_MAX_LENGTH = 32; |
|
| 51 | - private const KEY_MAX_LENGTH = 64; |
|
| 52 | - private const INDEX_MAX_LENGTH = 64; |
|
| 53 | - private const ENCRYPTION_PREFIX = '$UserConfigEncryption$'; |
|
| 54 | - private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX) |
|
| 55 | - |
|
| 56 | - /** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 57 | - private array $fastCache = []; // cache for normal config keys |
|
| 58 | - /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 59 | - private array $lazyCache = []; // cache for lazy config keys |
|
| 60 | - /** @var array<string, array<string, array<string, array<string, mixed>>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ |
|
| 61 | - private array $valueDetails = []; // type for all config values |
|
| 62 | - /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 63 | - private array $fastLoaded = []; |
|
| 64 | - /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 65 | - private array $lazyLoaded = []; |
|
| 66 | - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 67 | - private array $configLexiconDetails = []; |
|
| 68 | - private bool $ignoreLexiconAliases = false; |
|
| 69 | - |
|
| 70 | - public function __construct( |
|
| 71 | - protected IDBConnection $connection, |
|
| 72 | - protected IConfig $config, |
|
| 73 | - protected LoggerInterface $logger, |
|
| 74 | - protected ICrypto $crypto, |
|
| 75 | - ) { |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @inheritDoc |
|
| 80 | - * |
|
| 81 | - * @param string $appId optional id of app |
|
| 82 | - * |
|
| 83 | - * @return list<string> list of userIds |
|
| 84 | - * @since 31.0.0 |
|
| 85 | - */ |
|
| 86 | - public function getUserIds(string $appId = ''): array { |
|
| 87 | - $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true); |
|
| 88 | - |
|
| 89 | - $qb = $this->connection->getQueryBuilder(); |
|
| 90 | - $qb->from('preferences'); |
|
| 91 | - $qb->select('userid'); |
|
| 92 | - $qb->groupBy('userid'); |
|
| 93 | - if ($appId !== '') { |
|
| 94 | - $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $result = $qb->executeQuery(); |
|
| 98 | - $rows = $result->fetchAll(); |
|
| 99 | - $userIds = []; |
|
| 100 | - foreach ($rows as $row) { |
|
| 101 | - $userIds[] = $row['userid']; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $userIds; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @inheritDoc |
|
| 109 | - * |
|
| 110 | - * @return list<string> list of app ids |
|
| 111 | - * @since 31.0.0 |
|
| 112 | - */ |
|
| 113 | - public function getApps(string $userId): array { |
|
| 114 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 115 | - $this->loadConfigAll($userId); |
|
| 116 | - $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); |
|
| 117 | - sort($apps); |
|
| 118 | - |
|
| 119 | - return array_values(array_unique($apps)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @inheritDoc |
|
| 124 | - * |
|
| 125 | - * @param string $userId id of the user |
|
| 126 | - * @param string $app id of the app |
|
| 127 | - * |
|
| 128 | - * @return list<string> list of stored config keys |
|
| 129 | - * @since 31.0.0 |
|
| 130 | - */ |
|
| 131 | - public function getKeys(string $userId, string $app): array { |
|
| 132 | - $this->assertParams($userId, $app); |
|
| 133 | - $this->loadConfigAll($userId); |
|
| 134 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 135 | - $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); |
|
| 136 | - sort($keys); |
|
| 137 | - |
|
| 138 | - return array_values(array_unique($keys)); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @inheritDoc |
|
| 143 | - * |
|
| 144 | - * @param string $userId id of the user |
|
| 145 | - * @param string $app id of the app |
|
| 146 | - * @param string $key config key |
|
| 147 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 148 | - * |
|
| 149 | - * @return bool TRUE if key exists |
|
| 150 | - * @since 31.0.0 |
|
| 151 | - */ |
|
| 152 | - public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 153 | - $this->assertParams($userId, $app, $key); |
|
| 154 | - $this->loadConfig($userId, $lazy); |
|
| 155 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 156 | - |
|
| 157 | - if ($lazy === null) { |
|
| 158 | - $appCache = $this->getValues($userId, $app); |
|
| 159 | - return isset($appCache[$key]); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ($lazy) { |
|
| 163 | - return isset($this->lazyCache[$userId][$app][$key]); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return isset($this->fastCache[$userId][$app][$key]); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @inheritDoc |
|
| 171 | - * |
|
| 172 | - * @param string $userId id of the user |
|
| 173 | - * @param string $app id of the app |
|
| 174 | - * @param string $key config key |
|
| 175 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 176 | - * |
|
| 177 | - * @return bool |
|
| 178 | - * @throws UnknownKeyException if config key is not known |
|
| 179 | - * @since 31.0.0 |
|
| 180 | - */ |
|
| 181 | - public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 182 | - $this->assertParams($userId, $app, $key); |
|
| 183 | - $this->loadConfig($userId, $lazy); |
|
| 184 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 185 | - |
|
| 186 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 187 | - throw new UnknownKeyException('unknown config key'); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @inheritDoc |
|
| 195 | - * |
|
| 196 | - * @param string $userId id of the user |
|
| 197 | - * @param string $app id of the app |
|
| 198 | - * @param string $key config key |
|
| 199 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 200 | - * |
|
| 201 | - * @return bool |
|
| 202 | - * @throws UnknownKeyException if config key is not known |
|
| 203 | - * @since 31.0.0 |
|
| 204 | - */ |
|
| 205 | - public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 206 | - $this->assertParams($userId, $app, $key); |
|
| 207 | - $this->loadConfig($userId, $lazy); |
|
| 208 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 209 | - |
|
| 210 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 211 | - throw new UnknownKeyException('unknown config key'); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @inheritDoc |
|
| 219 | - * |
|
| 220 | - * @param string $userId id of the user |
|
| 221 | - * @param string $app if of the app |
|
| 222 | - * @param string $key config key |
|
| 223 | - * |
|
| 224 | - * @return bool TRUE if config is lazy loaded |
|
| 225 | - * @throws UnknownKeyException if config key is not known |
|
| 226 | - * @see IUserConfig for details about lazy loading |
|
| 227 | - * @since 31.0.0 |
|
| 228 | - */ |
|
| 229 | - public function isLazy(string $userId, string $app, string $key): bool { |
|
| 230 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 231 | - |
|
| 232 | - // there is a huge probability the non-lazy config are already loaded |
|
| 233 | - // meaning that we can start by only checking if a current non-lazy key exists |
|
| 234 | - if ($this->hasKey($userId, $app, $key, false)) { |
|
| 235 | - // meaning key is not lazy. |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // as key is not found as non-lazy, we load and search in the lazy config |
|
| 240 | - if ($this->hasKey($userId, $app, $key, true)) { |
|
| 241 | - return true; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - throw new UnknownKeyException('unknown config key'); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @inheritDoc |
|
| 249 | - * |
|
| 250 | - * @param string $userId id of the user |
|
| 251 | - * @param string $app id of the app |
|
| 252 | - * @param string $prefix config keys prefix to search |
|
| 253 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 254 | - * |
|
| 255 | - * @return array<string, string|int|float|bool|array> [key => value] |
|
| 256 | - * @since 31.0.0 |
|
| 257 | - */ |
|
| 258 | - public function getValues( |
|
| 259 | - string $userId, |
|
| 260 | - string $app, |
|
| 261 | - string $prefix = '', |
|
| 262 | - bool $filtered = false, |
|
| 263 | - ): array { |
|
| 264 | - $this->assertParams($userId, $app, $prefix); |
|
| 265 | - // if we want to filter values, we need to get sensitivity |
|
| 266 | - $this->loadConfigAll($userId); |
|
| 267 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 268 | - $values = array_filter( |
|
| 269 | - $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), |
|
| 270 | - function (string $key) use ($prefix): bool { |
|
| 271 | - // filter values based on $prefix |
|
| 272 | - return str_starts_with($key, $prefix); |
|
| 273 | - }, ARRAY_FILTER_USE_KEY |
|
| 274 | - ); |
|
| 275 | - |
|
| 276 | - return $values; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @inheritDoc |
|
| 281 | - * |
|
| 282 | - * @param string $userId id of the user |
|
| 283 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 284 | - * |
|
| 285 | - * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]] |
|
| 286 | - * @since 31.0.0 |
|
| 287 | - */ |
|
| 288 | - public function getAllValues(string $userId, bool $filtered = false): array { |
|
| 289 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 290 | - $this->loadConfigAll($userId); |
|
| 291 | - |
|
| 292 | - $result = []; |
|
| 293 | - foreach ($this->getApps($userId) as $app) { |
|
| 294 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 295 | - $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); |
|
| 296 | - $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - return $result; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @inheritDoc |
|
| 304 | - * |
|
| 305 | - * @param string $userId id of the user |
|
| 306 | - * @param string $key config key |
|
| 307 | - * @param bool $lazy search within lazy loaded config |
|
| 308 | - * @param ValueType|null $typedAs enforce type for the returned values |
|
| 309 | - * |
|
| 310 | - * @return array<string, string|int|float|bool|array> [appId => value] |
|
| 311 | - * @since 31.0.0 |
|
| 312 | - */ |
|
| 313 | - public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { |
|
| 314 | - $this->assertParams($userId, '', $key, allowEmptyApp: true); |
|
| 315 | - $this->loadConfig($userId, $lazy); |
|
| 316 | - |
|
| 317 | - /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 318 | - if ($lazy) { |
|
| 319 | - $cache = $this->lazyCache[$userId]; |
|
| 320 | - } else { |
|
| 321 | - $cache = $this->fastCache[$userId]; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $values = []; |
|
| 325 | - foreach (array_keys($cache) as $app) { |
|
| 326 | - if (isset($cache[$app][$key])) { |
|
| 327 | - $value = $cache[$app][$key]; |
|
| 328 | - try { |
|
| 329 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 330 | - $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy)); |
|
| 331 | - } catch (IncorrectTypeException|UnknownKeyException) { |
|
| 332 | - } |
|
| 333 | - $values[$app] = $value; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - return $values; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @inheritDoc |
|
| 343 | - * |
|
| 344 | - * @param string $app id of the app |
|
| 345 | - * @param string $key config key |
|
| 346 | - * @param ValueType|null $typedAs enforce type for the returned values |
|
| 347 | - * @param array|null $userIds limit to a list of user ids |
|
| 348 | - * |
|
| 349 | - * @return array<string, string|int|float|bool|array> [userId => value] |
|
| 350 | - * @since 31.0.0 |
|
| 351 | - */ |
|
| 352 | - public function getValuesByUsers( |
|
| 353 | - string $app, |
|
| 354 | - string $key, |
|
| 355 | - ?ValueType $typedAs = null, |
|
| 356 | - ?array $userIds = null, |
|
| 357 | - ): array { |
|
| 358 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 359 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 360 | - |
|
| 361 | - $qb = $this->connection->getQueryBuilder(); |
|
| 362 | - $qb->select('userid', 'configvalue', 'type') |
|
| 363 | - ->from('preferences') |
|
| 364 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 365 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 366 | - |
|
| 367 | - $values = []; |
|
| 368 | - // this nested function will execute current Query and store result within $values. |
|
| 369 | - $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult { |
|
| 370 | - $result = $qb->executeQuery(); |
|
| 371 | - while ($row = $result->fetch()) { |
|
| 372 | - $value = $row['configvalue']; |
|
| 373 | - try { |
|
| 374 | - $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type'])); |
|
| 375 | - } catch (IncorrectTypeException) { |
|
| 376 | - } |
|
| 377 | - $values[$row['userid']] = $value; |
|
| 378 | - } |
|
| 379 | - return $result; |
|
| 380 | - }; |
|
| 381 | - |
|
| 382 | - // if no userIds to filter, we execute query as it is and returns all values ... |
|
| 383 | - if ($userIds === null) { |
|
| 384 | - $result = $executeAndStoreValue($qb); |
|
| 385 | - $result->closeCursor(); |
|
| 386 | - return $values; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values |
|
| 390 | - $result = null; |
|
| 391 | - $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); |
|
| 392 | - foreach (array_chunk($userIds, 50, true) as $chunk) { |
|
| 393 | - $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 394 | - $result = $executeAndStoreValue($qb); |
|
| 395 | - } |
|
| 396 | - $result?->closeCursor(); |
|
| 397 | - |
|
| 398 | - return $values; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @inheritDoc |
|
| 403 | - * |
|
| 404 | - * @param string $app id of the app |
|
| 405 | - * @param string $key config key |
|
| 406 | - * @param string $value config value |
|
| 407 | - * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string |
|
| 408 | - * |
|
| 409 | - * @return Generator<string> |
|
| 410 | - * @since 31.0.0 |
|
| 411 | - */ |
|
| 412 | - public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator { |
|
| 413 | - return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @inheritDoc |
|
| 418 | - * |
|
| 419 | - * @param string $app id of the app |
|
| 420 | - * @param string $key config key |
|
| 421 | - * @param int $value config value |
|
| 422 | - * |
|
| 423 | - * @return Generator<string> |
|
| 424 | - * @since 31.0.0 |
|
| 425 | - */ |
|
| 426 | - public function searchUsersByValueInt(string $app, string $key, int $value): Generator { |
|
| 427 | - return $this->searchUsersByValueString($app, $key, (string)$value); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * @inheritDoc |
|
| 432 | - * |
|
| 433 | - * @param string $app id of the app |
|
| 434 | - * @param string $key config key |
|
| 435 | - * @param array $values list of config values |
|
| 436 | - * |
|
| 437 | - * @return Generator<string> |
|
| 438 | - * @since 31.0.0 |
|
| 439 | - */ |
|
| 440 | - public function searchUsersByValues(string $app, string $key, array $values): Generator { |
|
| 441 | - return $this->searchUsersByTypedValue($app, $key, $values); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * @inheritDoc |
|
| 446 | - * |
|
| 447 | - * @param string $app id of the app |
|
| 448 | - * @param string $key config key |
|
| 449 | - * @param bool $value config value |
|
| 450 | - * |
|
| 451 | - * @return Generator<string> |
|
| 452 | - * @since 31.0.0 |
|
| 453 | - */ |
|
| 454 | - public function searchUsersByValueBool(string $app, string $key, bool $value): Generator { |
|
| 455 | - $values = ['0', 'off', 'false', 'no']; |
|
| 456 | - if ($value) { |
|
| 457 | - $values = ['1', 'on', 'true', 'yes']; |
|
| 458 | - } |
|
| 459 | - return $this->searchUsersByValues($app, $key, $values); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * returns a list of users with config key set to a specific value, or within the list of |
|
| 464 | - * possible values |
|
| 465 | - * |
|
| 466 | - * @param string $app |
|
| 467 | - * @param string $key |
|
| 468 | - * @param string|array $value |
|
| 469 | - * @param bool $caseInsensitive |
|
| 470 | - * |
|
| 471 | - * @return Generator<string> |
|
| 472 | - */ |
|
| 473 | - private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator { |
|
| 474 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 475 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 476 | - |
|
| 477 | - $qb = $this->connection->getQueryBuilder(); |
|
| 478 | - $qb->from('preferences'); |
|
| 479 | - $qb->select('userid'); |
|
| 480 | - $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 481 | - $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 482 | - |
|
| 483 | - // search within 'indexed' OR 'configvalue' only if 'flags' is set as not indexed |
|
| 484 | - // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed |
|
| 485 | - $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; |
|
| 486 | - if (is_array($value)) { |
|
| 487 | - $where = $qb->expr()->orX( |
|
| 488 | - $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 489 | - $qb->expr()->andX( |
|
| 490 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 491 | - $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 492 | - ) |
|
| 493 | - ); |
|
| 494 | - } else { |
|
| 495 | - if ($caseInsensitive) { |
|
| 496 | - $where = $qb->expr()->orX( |
|
| 497 | - $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), |
|
| 498 | - $qb->expr()->andX( |
|
| 499 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 500 | - $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) |
|
| 501 | - ) |
|
| 502 | - ); |
|
| 503 | - } else { |
|
| 504 | - $where = $qb->expr()->orX( |
|
| 505 | - $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), |
|
| 506 | - $qb->expr()->andX( |
|
| 507 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 508 | - $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) |
|
| 509 | - ) |
|
| 510 | - ); |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - $qb->andWhere($where); |
|
| 515 | - $result = $qb->executeQuery(); |
|
| 516 | - while ($row = $result->fetch()) { |
|
| 517 | - yield $row['userid']; |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Get the config value as string. |
|
| 523 | - * If the value does not exist the given default will be returned. |
|
| 524 | - * |
|
| 525 | - * Set lazy to `null` to ignore it and get the value from either source. |
|
| 526 | - * |
|
| 527 | - * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 528 | - * |
|
| 529 | - * @param string $userId id of the user |
|
| 530 | - * @param string $app id of the app |
|
| 531 | - * @param string $key config key |
|
| 532 | - * @param string $default config value |
|
| 533 | - * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 534 | - * |
|
| 535 | - * @return string the value or $default |
|
| 536 | - * @throws TypeConflictException |
|
| 537 | - * @internal |
|
| 538 | - * @since 31.0.0 |
|
| 539 | - * @see IUserConfig for explanation about lazy loading |
|
| 540 | - * @see getValueString() |
|
| 541 | - * @see getValueInt() |
|
| 542 | - * @see getValueFloat() |
|
| 543 | - * @see getValueBool() |
|
| 544 | - * @see getValueArray() |
|
| 545 | - */ |
|
| 546 | - public function getValueMixed( |
|
| 547 | - string $userId, |
|
| 548 | - string $app, |
|
| 549 | - string $key, |
|
| 550 | - string $default = '', |
|
| 551 | - ?bool $lazy = false, |
|
| 552 | - ): string { |
|
| 553 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 554 | - try { |
|
| 555 | - $lazy ??= $this->isLazy($userId, $app, $key); |
|
| 556 | - } catch (UnknownKeyException) { |
|
| 557 | - return $default; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - return $this->getTypedValue( |
|
| 561 | - $userId, |
|
| 562 | - $app, |
|
| 563 | - $key, |
|
| 564 | - $default, |
|
| 565 | - $lazy, |
|
| 566 | - ValueType::MIXED |
|
| 567 | - ); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * @inheritDoc |
|
| 572 | - * |
|
| 573 | - * @param string $userId id of the user |
|
| 574 | - * @param string $app id of the app |
|
| 575 | - * @param string $key config key |
|
| 576 | - * @param string $default default value |
|
| 577 | - * @param bool $lazy search within lazy loaded config |
|
| 578 | - * |
|
| 579 | - * @return string stored config value or $default if not set in database |
|
| 580 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 581 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 582 | - * @since 31.0.0 |
|
| 583 | - * @see IUserConfig for explanation about lazy loading |
|
| 584 | - */ |
|
| 585 | - public function getValueString( |
|
| 586 | - string $userId, |
|
| 587 | - string $app, |
|
| 588 | - string $key, |
|
| 589 | - string $default = '', |
|
| 590 | - bool $lazy = false, |
|
| 591 | - ): string { |
|
| 592 | - return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * @inheritDoc |
|
| 597 | - * |
|
| 598 | - * @param string $userId id of the user |
|
| 599 | - * @param string $app id of the app |
|
| 600 | - * @param string $key config key |
|
| 601 | - * @param int $default default value |
|
| 602 | - * @param bool $lazy search within lazy loaded config |
|
| 603 | - * |
|
| 604 | - * @return int stored config value or $default if not set in database |
|
| 605 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 606 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 607 | - * @since 31.0.0 |
|
| 608 | - * @see IUserConfig for explanation about lazy loading |
|
| 609 | - */ |
|
| 610 | - public function getValueInt( |
|
| 611 | - string $userId, |
|
| 612 | - string $app, |
|
| 613 | - string $key, |
|
| 614 | - int $default = 0, |
|
| 615 | - bool $lazy = false, |
|
| 616 | - ): int { |
|
| 617 | - return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * @inheritDoc |
|
| 622 | - * |
|
| 623 | - * @param string $userId id of the user |
|
| 624 | - * @param string $app id of the app |
|
| 625 | - * @param string $key config key |
|
| 626 | - * @param float $default default value |
|
| 627 | - * @param bool $lazy search within lazy loaded config |
|
| 628 | - * |
|
| 629 | - * @return float stored config value or $default if not set in database |
|
| 630 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 631 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 632 | - * @since 31.0.0 |
|
| 633 | - * @see IUserConfig for explanation about lazy loading |
|
| 634 | - */ |
|
| 635 | - public function getValueFloat( |
|
| 636 | - string $userId, |
|
| 637 | - string $app, |
|
| 638 | - string $key, |
|
| 639 | - float $default = 0, |
|
| 640 | - bool $lazy = false, |
|
| 641 | - ): float { |
|
| 642 | - return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * @inheritDoc |
|
| 647 | - * |
|
| 648 | - * @param string $userId id of the user |
|
| 649 | - * @param string $app id of the app |
|
| 650 | - * @param string $key config key |
|
| 651 | - * @param bool $default default value |
|
| 652 | - * @param bool $lazy search within lazy loaded config |
|
| 653 | - * |
|
| 654 | - * @return bool stored config value or $default if not set in database |
|
| 655 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 656 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 657 | - * @since 31.0.0 |
|
| 658 | - * @see IUserConfig for explanation about lazy loading |
|
| 659 | - */ |
|
| 660 | - public function getValueBool( |
|
| 661 | - string $userId, |
|
| 662 | - string $app, |
|
| 663 | - string $key, |
|
| 664 | - bool $default = false, |
|
| 665 | - bool $lazy = false, |
|
| 666 | - ): bool { |
|
| 667 | - $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)); |
|
| 668 | - return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * @inheritDoc |
|
| 673 | - * |
|
| 674 | - * @param string $userId id of the user |
|
| 675 | - * @param string $app id of the app |
|
| 676 | - * @param string $key config key |
|
| 677 | - * @param array $default default value |
|
| 678 | - * @param bool $lazy search within lazy loaded config |
|
| 679 | - * |
|
| 680 | - * @return array stored config value or $default if not set in database |
|
| 681 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 682 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 683 | - * @since 31.0.0 |
|
| 684 | - * @see IUserConfig for explanation about lazy loading |
|
| 685 | - */ |
|
| 686 | - public function getValueArray( |
|
| 687 | - string $userId, |
|
| 688 | - string $app, |
|
| 689 | - string $key, |
|
| 690 | - array $default = [], |
|
| 691 | - bool $lazy = false, |
|
| 692 | - ): array { |
|
| 693 | - try { |
|
| 694 | - $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 695 | - $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 696 | - |
|
| 697 | - return is_array($value) ? $value : []; |
|
| 698 | - } catch (JsonException) { |
|
| 699 | - return []; |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * @param string $userId |
|
| 705 | - * @param string $app id of the app |
|
| 706 | - * @param string $key config key |
|
| 707 | - * @param string $default default value |
|
| 708 | - * @param bool $lazy search within lazy loaded config |
|
| 709 | - * @param ValueType $type value type |
|
| 710 | - * |
|
| 711 | - * @return string |
|
| 712 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 713 | - */ |
|
| 714 | - private function getTypedValue( |
|
| 715 | - string $userId, |
|
| 716 | - string $app, |
|
| 717 | - string $key, |
|
| 718 | - string $default, |
|
| 719 | - bool $lazy, |
|
| 720 | - ValueType $type, |
|
| 721 | - ): string { |
|
| 722 | - $this->assertParams($userId, $app, $key); |
|
| 723 | - $origKey = $key; |
|
| 724 | - if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default)) { |
|
| 725 | - // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 726 | - return $default; |
|
| 727 | - } |
|
| 728 | - $this->loadConfig($userId, $lazy); |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * We ignore check if mixed type is requested. |
|
| 732 | - * If type of stored value is set as mixed, we don't filter. |
|
| 733 | - * If type of stored value is defined, we compare with the one requested. |
|
| 734 | - */ |
|
| 735 | - $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 736 | - if ($type !== ValueType::MIXED |
|
| 737 | - && $knownType !== null |
|
| 738 | - && $knownType !== ValueType::MIXED |
|
| 739 | - && $type !== $knownType) { |
|
| 740 | - $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 741 | - throw new TypeConflictException('conflict with value type from database'); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * - the pair $app/$key cannot exist in both array, |
|
| 746 | - * - we should still return an existing non-lazy value even if current method |
|
| 747 | - * is called with $lazy is true |
|
| 748 | - * |
|
| 749 | - * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 750 | - */ |
|
| 751 | - if (isset($this->lazyCache[$userId][$app][$key])) { |
|
| 752 | - $value = $this->lazyCache[$userId][$app][$key]; |
|
| 753 | - } elseif (isset($this->fastCache[$userId][$app][$key])) { |
|
| 754 | - $value = $this->fastCache[$userId][$app][$key]; |
|
| 755 | - } else { |
|
| 756 | - return $default; |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 760 | - |
|
| 761 | - // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 762 | - // interested to check options in case a modification of the value is needed |
|
| 763 | - // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 764 | - if ($origKey !== $key && $type === ValueType::BOOL) { |
|
| 765 | - $configManager = Server::get(ConfigManager::class); |
|
| 766 | - $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - return $value; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * @inheritDoc |
|
| 774 | - * |
|
| 775 | - * @param string $userId id of the user |
|
| 776 | - * @param string $app id of the app |
|
| 777 | - * @param string $key config key |
|
| 778 | - * |
|
| 779 | - * @return ValueType type of the value |
|
| 780 | - * @throws UnknownKeyException if config key is not known |
|
| 781 | - * @throws IncorrectTypeException if config value type is not known |
|
| 782 | - * @since 31.0.0 |
|
| 783 | - */ |
|
| 784 | - public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { |
|
| 785 | - $this->assertParams($userId, $app, $key); |
|
| 786 | - $this->loadConfig($userId, $lazy); |
|
| 787 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 788 | - |
|
| 789 | - if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { |
|
| 790 | - throw new UnknownKeyException('unknown config key'); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - return $this->valueDetails[$userId][$app][$key]['type']; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - /** |
|
| 797 | - * @inheritDoc |
|
| 798 | - * |
|
| 799 | - * @param string $userId id of the user |
|
| 800 | - * @param string $app id of the app |
|
| 801 | - * @param string $key config key |
|
| 802 | - * @param bool $lazy lazy loading |
|
| 803 | - * |
|
| 804 | - * @return int flags applied to value |
|
| 805 | - * @throws UnknownKeyException if config key is not known |
|
| 806 | - * @throws IncorrectTypeException if config value type is not known |
|
| 807 | - * @since 31.0.0 |
|
| 808 | - */ |
|
| 809 | - public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { |
|
| 810 | - $this->assertParams($userId, $app, $key); |
|
| 811 | - $this->loadConfig($userId, $lazy); |
|
| 812 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 813 | - |
|
| 814 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 815 | - throw new UnknownKeyException('unknown config key'); |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - return $this->valueDetails[$userId][$app][$key]['flags']; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * Store a config key and its value in database as VALUE_MIXED |
|
| 823 | - * |
|
| 824 | - * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 825 | - * |
|
| 826 | - * @param string $userId id of the user |
|
| 827 | - * @param string $app id of the app |
|
| 828 | - * @param string $key config key |
|
| 829 | - * @param string $value config value |
|
| 830 | - * @param bool $lazy set config as lazy loaded |
|
| 831 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 832 | - * |
|
| 833 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 834 | - * @throws TypeConflictException if type from database is not VALUE_MIXED |
|
| 835 | - * @internal |
|
| 836 | - * @since 31.0.0 |
|
| 837 | - * @see IUserConfig for explanation about lazy loading |
|
| 838 | - * @see setValueString() |
|
| 839 | - * @see setValueInt() |
|
| 840 | - * @see setValueFloat() |
|
| 841 | - * @see setValueBool() |
|
| 842 | - * @see setValueArray() |
|
| 843 | - */ |
|
| 844 | - public function setValueMixed( |
|
| 845 | - string $userId, |
|
| 846 | - string $app, |
|
| 847 | - string $key, |
|
| 848 | - string $value, |
|
| 849 | - bool $lazy = false, |
|
| 850 | - int $flags = 0, |
|
| 851 | - ): bool { |
|
| 852 | - return $this->setTypedValue( |
|
| 853 | - $userId, |
|
| 854 | - $app, |
|
| 855 | - $key, |
|
| 856 | - $value, |
|
| 857 | - $lazy, |
|
| 858 | - $flags, |
|
| 859 | - ValueType::MIXED |
|
| 860 | - ); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - |
|
| 864 | - /** |
|
| 865 | - * @inheritDoc |
|
| 866 | - * |
|
| 867 | - * @param string $userId id of the user |
|
| 868 | - * @param string $app id of the app |
|
| 869 | - * @param string $key config key |
|
| 870 | - * @param string $value config value |
|
| 871 | - * @param bool $lazy set config as lazy loaded |
|
| 872 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 873 | - * |
|
| 874 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 875 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 876 | - * @since 31.0.0 |
|
| 877 | - * @see IUserConfig for explanation about lazy loading |
|
| 878 | - */ |
|
| 879 | - public function setValueString( |
|
| 880 | - string $userId, |
|
| 881 | - string $app, |
|
| 882 | - string $key, |
|
| 883 | - string $value, |
|
| 884 | - bool $lazy = false, |
|
| 885 | - int $flags = 0, |
|
| 886 | - ): bool { |
|
| 887 | - return $this->setTypedValue( |
|
| 888 | - $userId, |
|
| 889 | - $app, |
|
| 890 | - $key, |
|
| 891 | - $value, |
|
| 892 | - $lazy, |
|
| 893 | - $flags, |
|
| 894 | - ValueType::STRING |
|
| 895 | - ); |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - /** |
|
| 899 | - * @inheritDoc |
|
| 900 | - * |
|
| 901 | - * @param string $userId id of the user |
|
| 902 | - * @param string $app id of the app |
|
| 903 | - * @param string $key config key |
|
| 904 | - * @param int $value config value |
|
| 905 | - * @param bool $lazy set config as lazy loaded |
|
| 906 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 907 | - * |
|
| 908 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 909 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 910 | - * @since 31.0.0 |
|
| 911 | - * @see IUserConfig for explanation about lazy loading |
|
| 912 | - */ |
|
| 913 | - public function setValueInt( |
|
| 914 | - string $userId, |
|
| 915 | - string $app, |
|
| 916 | - string $key, |
|
| 917 | - int $value, |
|
| 918 | - bool $lazy = false, |
|
| 919 | - int $flags = 0, |
|
| 920 | - ): bool { |
|
| 921 | - if ($value > 2000000000) { |
|
| 922 | - $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - return $this->setTypedValue( |
|
| 926 | - $userId, |
|
| 927 | - $app, |
|
| 928 | - $key, |
|
| 929 | - (string)$value, |
|
| 930 | - $lazy, |
|
| 931 | - $flags, |
|
| 932 | - ValueType::INT |
|
| 933 | - ); |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - /** |
|
| 937 | - * @inheritDoc |
|
| 938 | - * |
|
| 939 | - * @param string $userId id of the user |
|
| 940 | - * @param string $app id of the app |
|
| 941 | - * @param string $key config key |
|
| 942 | - * @param float $value config value |
|
| 943 | - * @param bool $lazy set config as lazy loaded |
|
| 944 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 945 | - * |
|
| 946 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 947 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 948 | - * @since 31.0.0 |
|
| 949 | - * @see IUserConfig for explanation about lazy loading |
|
| 950 | - */ |
|
| 951 | - public function setValueFloat( |
|
| 952 | - string $userId, |
|
| 953 | - string $app, |
|
| 954 | - string $key, |
|
| 955 | - float $value, |
|
| 956 | - bool $lazy = false, |
|
| 957 | - int $flags = 0, |
|
| 958 | - ): bool { |
|
| 959 | - return $this->setTypedValue( |
|
| 960 | - $userId, |
|
| 961 | - $app, |
|
| 962 | - $key, |
|
| 963 | - (string)$value, |
|
| 964 | - $lazy, |
|
| 965 | - $flags, |
|
| 966 | - ValueType::FLOAT |
|
| 967 | - ); |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - /** |
|
| 971 | - * @inheritDoc |
|
| 972 | - * |
|
| 973 | - * @param string $userId id of the user |
|
| 974 | - * @param string $app id of the app |
|
| 975 | - * @param string $key config key |
|
| 976 | - * @param bool $value config value |
|
| 977 | - * @param bool $lazy set config as lazy loaded |
|
| 978 | - * |
|
| 979 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 980 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 981 | - * @since 31.0.0 |
|
| 982 | - * @see IUserConfig for explanation about lazy loading |
|
| 983 | - */ |
|
| 984 | - public function setValueBool( |
|
| 985 | - string $userId, |
|
| 986 | - string $app, |
|
| 987 | - string $key, |
|
| 988 | - bool $value, |
|
| 989 | - bool $lazy = false, |
|
| 990 | - int $flags = 0, |
|
| 991 | - ): bool { |
|
| 992 | - return $this->setTypedValue( |
|
| 993 | - $userId, |
|
| 994 | - $app, |
|
| 995 | - $key, |
|
| 996 | - ($value) ? '1' : '0', |
|
| 997 | - $lazy, |
|
| 998 | - $flags, |
|
| 999 | - ValueType::BOOL |
|
| 1000 | - ); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * @inheritDoc |
|
| 1005 | - * |
|
| 1006 | - * @param string $userId id of the user |
|
| 1007 | - * @param string $app id of the app |
|
| 1008 | - * @param string $key config key |
|
| 1009 | - * @param array $value config value |
|
| 1010 | - * @param bool $lazy set config as lazy loaded |
|
| 1011 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 1012 | - * |
|
| 1013 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 1014 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1015 | - * @throws JsonException |
|
| 1016 | - * @since 31.0.0 |
|
| 1017 | - * @see IUserConfig for explanation about lazy loading |
|
| 1018 | - */ |
|
| 1019 | - public function setValueArray( |
|
| 1020 | - string $userId, |
|
| 1021 | - string $app, |
|
| 1022 | - string $key, |
|
| 1023 | - array $value, |
|
| 1024 | - bool $lazy = false, |
|
| 1025 | - int $flags = 0, |
|
| 1026 | - ): bool { |
|
| 1027 | - try { |
|
| 1028 | - return $this->setTypedValue( |
|
| 1029 | - $userId, |
|
| 1030 | - $app, |
|
| 1031 | - $key, |
|
| 1032 | - json_encode($value, JSON_THROW_ON_ERROR), |
|
| 1033 | - $lazy, |
|
| 1034 | - $flags, |
|
| 1035 | - ValueType::ARRAY |
|
| 1036 | - ); |
|
| 1037 | - } catch (JsonException $e) { |
|
| 1038 | - $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 1039 | - throw $e; |
|
| 1040 | - } |
|
| 1041 | - } |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * Store a config key and its value in database |
|
| 1045 | - * |
|
| 1046 | - * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 1047 | - * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 1048 | - * altered. |
|
| 1049 | - * |
|
| 1050 | - * @param string $userId id of the user |
|
| 1051 | - * @param string $app id of the app |
|
| 1052 | - * @param string $key config key |
|
| 1053 | - * @param string $value config value |
|
| 1054 | - * @param bool $lazy config set as lazy loaded |
|
| 1055 | - * @param ValueType $type value type |
|
| 1056 | - * |
|
| 1057 | - * @return bool TRUE if value was updated in database |
|
| 1058 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1059 | - * @see IUserConfig for explanation about lazy loading |
|
| 1060 | - */ |
|
| 1061 | - private function setTypedValue( |
|
| 1062 | - string $userId, |
|
| 1063 | - string $app, |
|
| 1064 | - string $key, |
|
| 1065 | - string $value, |
|
| 1066 | - bool $lazy, |
|
| 1067 | - int $flags, |
|
| 1068 | - ValueType $type, |
|
| 1069 | - ): bool { |
|
| 1070 | - // Primary email addresses are always(!) expected to be lowercase |
|
| 1071 | - if ($app === 'settings' && $key === 'email') { |
|
| 1072 | - $value = strtolower($value); |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - $this->assertParams($userId, $app, $key); |
|
| 1076 | - if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) { |
|
| 1077 | - // returns false as database is not updated |
|
| 1078 | - return false; |
|
| 1079 | - } |
|
| 1080 | - $this->loadConfig($userId, $lazy); |
|
| 1081 | - |
|
| 1082 | - $inserted = $refreshCache = false; |
|
| 1083 | - $origValue = $value; |
|
| 1084 | - $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); |
|
| 1085 | - if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1086 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1087 | - $flags |= self::FLAG_SENSITIVE; |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - // if requested, we fill the 'indexed' field with current value |
|
| 1091 | - $indexed = ''; |
|
| 1092 | - if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { |
|
| 1093 | - if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { |
|
| 1094 | - $this->logger->warning('sensitive value are not to be indexed'); |
|
| 1095 | - } elseif (strlen($value) > self::USER_MAX_LENGTH) { |
|
| 1096 | - $this->logger->warning('value is too lengthy to be indexed'); |
|
| 1097 | - } else { |
|
| 1098 | - $indexed = $value; |
|
| 1099 | - } |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - if ($this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1103 | - /** |
|
| 1104 | - * no update if key is already known with set lazy status and value is |
|
| 1105 | - * not different, unless sensitivity is switched from false to true. |
|
| 1106 | - */ |
|
| 1107 | - if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type) |
|
| 1108 | - && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1109 | - return false; |
|
| 1110 | - } |
|
| 1111 | - } else { |
|
| 1112 | - /** |
|
| 1113 | - * if key is not known yet, we try to insert. |
|
| 1114 | - * It might fail if the key exists with a different lazy flag. |
|
| 1115 | - */ |
|
| 1116 | - try { |
|
| 1117 | - $insert = $this->connection->getQueryBuilder(); |
|
| 1118 | - $insert->insert('preferences') |
|
| 1119 | - ->setValue('userid', $insert->createNamedParameter($userId)) |
|
| 1120 | - ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 1121 | - ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1122 | - ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1123 | - ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1124 | - ->setValue('indexed', $insert->createNamedParameter($indexed)) |
|
| 1125 | - ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 1126 | - ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 1127 | - $insert->executeStatement(); |
|
| 1128 | - $inserted = true; |
|
| 1129 | - } catch (DBException $e) { |
|
| 1130 | - if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 1131 | - // TODO: throw exception or just log and returns false !? |
|
| 1132 | - throw $e; |
|
| 1133 | - } |
|
| 1134 | - } |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - /** |
|
| 1138 | - * We cannot insert a new row, meaning we need to update an already existing one |
|
| 1139 | - */ |
|
| 1140 | - if (!$inserted) { |
|
| 1141 | - $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 1142 | - if ($currType === null) { // this might happen when switching lazy loading status |
|
| 1143 | - $this->loadConfigAll($userId); |
|
| 1144 | - $currType = $this->valueDetails[$userId][$app][$key]['type']; |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - /** |
|
| 1148 | - * We only log a warning and set it to VALUE_MIXED. |
|
| 1149 | - */ |
|
| 1150 | - if ($currType === null) { |
|
| 1151 | - $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]); |
|
| 1152 | - $currType = ValueType::MIXED; |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - /** |
|
| 1156 | - * we only accept a different type from the one stored in database |
|
| 1157 | - * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 1158 | - */ |
|
| 1159 | - if ($currType !== ValueType::MIXED && |
|
| 1160 | - $currType !== $type) { |
|
| 1161 | - try { |
|
| 1162 | - $currTypeDef = $currType->getDefinition(); |
|
| 1163 | - $typeDef = $type->getDefinition(); |
|
| 1164 | - } catch (IncorrectTypeException) { |
|
| 1165 | - $currTypeDef = $currType->value; |
|
| 1166 | - $typeDef = $type->value; |
|
| 1167 | - } |
|
| 1168 | - throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')'); |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - if ($lazy !== $this->isLazy($userId, $app, $key)) { |
|
| 1172 | - $refreshCache = true; |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - $update = $this->connection->getQueryBuilder(); |
|
| 1176 | - $update->update('preferences') |
|
| 1177 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1178 | - ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1179 | - ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1180 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1181 | - ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1182 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1183 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1184 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1185 | - |
|
| 1186 | - $update->executeStatement(); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - if ($refreshCache) { |
|
| 1190 | - $this->clearCache($userId); |
|
| 1191 | - return true; |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - // update local cache |
|
| 1195 | - if ($lazy) { |
|
| 1196 | - $this->lazyCache[$userId][$app][$key] = $value; |
|
| 1197 | - } else { |
|
| 1198 | - $this->fastCache[$userId][$app][$key] = $value; |
|
| 1199 | - } |
|
| 1200 | - $this->valueDetails[$userId][$app][$key] = [ |
|
| 1201 | - 'type' => $type, |
|
| 1202 | - 'flags' => $flags |
|
| 1203 | - ]; |
|
| 1204 | - |
|
| 1205 | - return true; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - /** |
|
| 1209 | - * Change the type of config value. |
|
| 1210 | - * |
|
| 1211 | - * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 1212 | - * |
|
| 1213 | - * @param string $userId id of the user |
|
| 1214 | - * @param string $app id of the app |
|
| 1215 | - * @param string $key config key |
|
| 1216 | - * @param ValueType $type value type |
|
| 1217 | - * |
|
| 1218 | - * @return bool TRUE if database update were necessary |
|
| 1219 | - * @throws UnknownKeyException if $key is now known in database |
|
| 1220 | - * @throws IncorrectTypeException if $type is not valid |
|
| 1221 | - * @internal |
|
| 1222 | - * @since 31.0.0 |
|
| 1223 | - */ |
|
| 1224 | - public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { |
|
| 1225 | - $this->assertParams($userId, $app, $key); |
|
| 1226 | - $this->loadConfigAll($userId); |
|
| 1227 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1228 | - $this->isLazy($userId, $app, $key); // confirm key exists |
|
| 1229 | - |
|
| 1230 | - $update = $this->connection->getQueryBuilder(); |
|
| 1231 | - $update->update('preferences') |
|
| 1232 | - ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1233 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1234 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1235 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1236 | - $update->executeStatement(); |
|
| 1237 | - |
|
| 1238 | - $this->valueDetails[$userId][$app][$key]['type'] = $type; |
|
| 1239 | - |
|
| 1240 | - return true; |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * @inheritDoc |
|
| 1245 | - * |
|
| 1246 | - * @param string $userId id of the user |
|
| 1247 | - * @param string $app id of the app |
|
| 1248 | - * @param string $key config key |
|
| 1249 | - * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 1250 | - * |
|
| 1251 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1252 | - * @since 31.0.0 |
|
| 1253 | - */ |
|
| 1254 | - public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { |
|
| 1255 | - $this->assertParams($userId, $app, $key); |
|
| 1256 | - $this->loadConfigAll($userId); |
|
| 1257 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1258 | - |
|
| 1259 | - try { |
|
| 1260 | - if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { |
|
| 1261 | - return false; |
|
| 1262 | - } |
|
| 1263 | - } catch (UnknownKeyException) { |
|
| 1264 | - return false; |
|
| 1265 | - } |
|
| 1266 | - |
|
| 1267 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1268 | - if ($lazy) { |
|
| 1269 | - $cache = $this->lazyCache; |
|
| 1270 | - } else { |
|
| 1271 | - $cache = $this->fastCache; |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - if (!isset($cache[$userId][$app][$key])) { |
|
| 1275 | - throw new UnknownKeyException('unknown config key'); |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - $value = $cache[$userId][$app][$key]; |
|
| 1279 | - $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1280 | - if ($sensitive) { |
|
| 1281 | - $flags |= self::FLAG_SENSITIVE; |
|
| 1282 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1283 | - } else { |
|
| 1284 | - $flags &= ~self::FLAG_SENSITIVE; |
|
| 1285 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - $update = $this->connection->getQueryBuilder(); |
|
| 1289 | - $update->update('preferences') |
|
| 1290 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1291 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1292 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1293 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1294 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1295 | - $update->executeStatement(); |
|
| 1296 | - |
|
| 1297 | - $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1298 | - |
|
| 1299 | - return true; |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - /** |
|
| 1303 | - * @inheritDoc |
|
| 1304 | - * |
|
| 1305 | - * @param string $app |
|
| 1306 | - * @param string $key |
|
| 1307 | - * @param bool $sensitive |
|
| 1308 | - * |
|
| 1309 | - * @since 31.0.0 |
|
| 1310 | - */ |
|
| 1311 | - public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { |
|
| 1312 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1313 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1314 | - |
|
| 1315 | - foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1316 | - try { |
|
| 1317 | - $this->updateSensitive($userId, $app, $key, $sensitive); |
|
| 1318 | - } catch (UnknownKeyException) { |
|
| 1319 | - // should not happen and can be ignored |
|
| 1320 | - } |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - // we clear all cache |
|
| 1324 | - $this->clearCacheAll(); |
|
| 1325 | - } |
|
| 1326 | - |
|
| 1327 | - /** |
|
| 1328 | - * @inheritDoc |
|
| 1329 | - * |
|
| 1330 | - * @param string $userId |
|
| 1331 | - * @param string $app |
|
| 1332 | - * @param string $key |
|
| 1333 | - * @param bool $indexed |
|
| 1334 | - * |
|
| 1335 | - * @return bool |
|
| 1336 | - * @throws DBException |
|
| 1337 | - * @throws IncorrectTypeException |
|
| 1338 | - * @throws UnknownKeyException |
|
| 1339 | - * @since 31.0.0 |
|
| 1340 | - */ |
|
| 1341 | - public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { |
|
| 1342 | - $this->assertParams($userId, $app, $key); |
|
| 1343 | - $this->loadConfigAll($userId); |
|
| 1344 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1345 | - |
|
| 1346 | - try { |
|
| 1347 | - if ($indexed === $this->isIndexed($userId, $app, $key, null)) { |
|
| 1348 | - return false; |
|
| 1349 | - } |
|
| 1350 | - } catch (UnknownKeyException) { |
|
| 1351 | - return false; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1355 | - if ($lazy) { |
|
| 1356 | - $cache = $this->lazyCache; |
|
| 1357 | - } else { |
|
| 1358 | - $cache = $this->fastCache; |
|
| 1359 | - } |
|
| 1360 | - |
|
| 1361 | - if (!isset($cache[$userId][$app][$key])) { |
|
| 1362 | - throw new UnknownKeyException('unknown config key'); |
|
| 1363 | - } |
|
| 1364 | - |
|
| 1365 | - $value = $cache[$userId][$app][$key]; |
|
| 1366 | - $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1367 | - if ($indexed) { |
|
| 1368 | - $indexed = $value; |
|
| 1369 | - } else { |
|
| 1370 | - $flags &= ~self::FLAG_INDEXED; |
|
| 1371 | - $indexed = ''; |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - $update = $this->connection->getQueryBuilder(); |
|
| 1375 | - $update->update('preferences') |
|
| 1376 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1377 | - ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1378 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1379 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1380 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1381 | - $update->executeStatement(); |
|
| 1382 | - |
|
| 1383 | - $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1384 | - |
|
| 1385 | - return true; |
|
| 1386 | - } |
|
| 1387 | - |
|
| 1388 | - |
|
| 1389 | - /** |
|
| 1390 | - * @inheritDoc |
|
| 1391 | - * |
|
| 1392 | - * @param string $app |
|
| 1393 | - * @param string $key |
|
| 1394 | - * @param bool $indexed |
|
| 1395 | - * |
|
| 1396 | - * @since 31.0.0 |
|
| 1397 | - */ |
|
| 1398 | - public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { |
|
| 1399 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1400 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1401 | - |
|
| 1402 | - foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1403 | - try { |
|
| 1404 | - $this->updateIndexed($userId, $app, $key, $indexed); |
|
| 1405 | - } catch (UnknownKeyException) { |
|
| 1406 | - // should not happen and can be ignored |
|
| 1407 | - } |
|
| 1408 | - } |
|
| 1409 | - |
|
| 1410 | - // we clear all cache |
|
| 1411 | - $this->clearCacheAll(); |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - /** |
|
| 1415 | - * @inheritDoc |
|
| 1416 | - * |
|
| 1417 | - * @param string $userId id of the user |
|
| 1418 | - * @param string $app id of the app |
|
| 1419 | - * @param string $key config key |
|
| 1420 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1421 | - * |
|
| 1422 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1423 | - * @since 31.0.0 |
|
| 1424 | - */ |
|
| 1425 | - public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { |
|
| 1426 | - $this->assertParams($userId, $app, $key); |
|
| 1427 | - $this->loadConfigAll($userId); |
|
| 1428 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1429 | - |
|
| 1430 | - try { |
|
| 1431 | - if ($lazy === $this->isLazy($userId, $app, $key)) { |
|
| 1432 | - return false; |
|
| 1433 | - } |
|
| 1434 | - } catch (UnknownKeyException) { |
|
| 1435 | - return false; |
|
| 1436 | - } |
|
| 1437 | - |
|
| 1438 | - $update = $this->connection->getQueryBuilder(); |
|
| 1439 | - $update->update('preferences') |
|
| 1440 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1441 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1442 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1443 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1444 | - $update->executeStatement(); |
|
| 1445 | - |
|
| 1446 | - // At this point, it is a lot safer to clean cache |
|
| 1447 | - $this->clearCache($userId); |
|
| 1448 | - |
|
| 1449 | - return true; |
|
| 1450 | - } |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * @inheritDoc |
|
| 1454 | - * |
|
| 1455 | - * @param string $app id of the app |
|
| 1456 | - * @param string $key config key |
|
| 1457 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1458 | - * |
|
| 1459 | - * @since 31.0.0 |
|
| 1460 | - */ |
|
| 1461 | - public function updateGlobalLazy(string $app, string $key, bool $lazy): void { |
|
| 1462 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1463 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1464 | - |
|
| 1465 | - $update = $this->connection->getQueryBuilder(); |
|
| 1466 | - $update->update('preferences') |
|
| 1467 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1468 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1469 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1470 | - $update->executeStatement(); |
|
| 1471 | - |
|
| 1472 | - $this->clearCacheAll(); |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * @inheritDoc |
|
| 1477 | - * |
|
| 1478 | - * @param string $userId id of the user |
|
| 1479 | - * @param string $app id of the app |
|
| 1480 | - * @param string $key config key |
|
| 1481 | - * |
|
| 1482 | - * @return array |
|
| 1483 | - * @throws UnknownKeyException if config key is not known in database |
|
| 1484 | - * @since 31.0.0 |
|
| 1485 | - */ |
|
| 1486 | - public function getDetails(string $userId, string $app, string $key): array { |
|
| 1487 | - $this->assertParams($userId, $app, $key); |
|
| 1488 | - $this->loadConfigAll($userId); |
|
| 1489 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1490 | - |
|
| 1491 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1492 | - |
|
| 1493 | - if ($lazy) { |
|
| 1494 | - $cache = $this->lazyCache[$userId]; |
|
| 1495 | - } else { |
|
| 1496 | - $cache = $this->fastCache[$userId]; |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - $type = $this->getValueType($userId, $app, $key); |
|
| 1500 | - try { |
|
| 1501 | - $typeString = $type->getDefinition(); |
|
| 1502 | - } catch (IncorrectTypeException $e) { |
|
| 1503 | - $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1504 | - $typeString = (string)$type->value; |
|
| 1505 | - } |
|
| 1506 | - |
|
| 1507 | - if (!isset($cache[$app][$key])) { |
|
| 1508 | - throw new UnknownKeyException('unknown config key'); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - $value = $cache[$app][$key]; |
|
| 1512 | - $sensitive = $this->isSensitive($userId, $app, $key, null); |
|
| 1513 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1514 | - |
|
| 1515 | - return [ |
|
| 1516 | - 'userId' => $userId, |
|
| 1517 | - 'app' => $app, |
|
| 1518 | - 'key' => $key, |
|
| 1519 | - 'value' => $value, |
|
| 1520 | - 'type' => $type->value, |
|
| 1521 | - 'lazy' => $lazy, |
|
| 1522 | - 'typeString' => $typeString, |
|
| 1523 | - 'sensitive' => $sensitive |
|
| 1524 | - ]; |
|
| 1525 | - } |
|
| 1526 | - |
|
| 1527 | - /** |
|
| 1528 | - * @inheritDoc |
|
| 1529 | - * |
|
| 1530 | - * @param string $userId id of the user |
|
| 1531 | - * @param string $app id of the app |
|
| 1532 | - * @param string $key config key |
|
| 1533 | - * |
|
| 1534 | - * @since 31.0.0 |
|
| 1535 | - */ |
|
| 1536 | - public function deleteUserConfig(string $userId, string $app, string $key): void { |
|
| 1537 | - $this->assertParams($userId, $app, $key); |
|
| 1538 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1539 | - |
|
| 1540 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1541 | - $qb->delete('preferences') |
|
| 1542 | - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) |
|
| 1543 | - ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1544 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1545 | - $qb->executeStatement(); |
|
| 1546 | - |
|
| 1547 | - unset($this->lazyCache[$userId][$app][$key]); |
|
| 1548 | - unset($this->fastCache[$userId][$app][$key]); |
|
| 1549 | - unset($this->valueDetails[$userId][$app][$key]); |
|
| 1550 | - } |
|
| 1551 | - |
|
| 1552 | - /** |
|
| 1553 | - * @inheritDoc |
|
| 1554 | - * |
|
| 1555 | - * @param string $app id of the app |
|
| 1556 | - * @param string $key config key |
|
| 1557 | - * |
|
| 1558 | - * @since 31.0.0 |
|
| 1559 | - */ |
|
| 1560 | - public function deleteKey(string $app, string $key): void { |
|
| 1561 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1562 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1563 | - |
|
| 1564 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1565 | - $qb->delete('preferences') |
|
| 1566 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1567 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1568 | - $qb->executeStatement(); |
|
| 1569 | - |
|
| 1570 | - $this->clearCacheAll(); |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - /** |
|
| 1574 | - * @inheritDoc |
|
| 1575 | - * |
|
| 1576 | - * @param string $app id of the app |
|
| 1577 | - * |
|
| 1578 | - * @since 31.0.0 |
|
| 1579 | - */ |
|
| 1580 | - public function deleteApp(string $app): void { |
|
| 1581 | - $this->assertParams('', $app, allowEmptyUser: true); |
|
| 1582 | - |
|
| 1583 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1584 | - $qb->delete('preferences') |
|
| 1585 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1586 | - $qb->executeStatement(); |
|
| 1587 | - |
|
| 1588 | - $this->clearCacheAll(); |
|
| 1589 | - } |
|
| 1590 | - |
|
| 1591 | - public function deleteAllUserConfig(string $userId): void { |
|
| 1592 | - $this->assertParams($userId, '', allowEmptyApp: true); |
|
| 1593 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1594 | - $qb->delete('preferences') |
|
| 1595 | - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1596 | - $qb->executeStatement(); |
|
| 1597 | - |
|
| 1598 | - $this->clearCache($userId); |
|
| 1599 | - } |
|
| 1600 | - |
|
| 1601 | - /** |
|
| 1602 | - * @inheritDoc |
|
| 1603 | - * |
|
| 1604 | - * @param string $userId id of the user |
|
| 1605 | - * @param bool $reload set to TRUE to refill cache instantly after clearing it. |
|
| 1606 | - * |
|
| 1607 | - * @since 31.0.0 |
|
| 1608 | - */ |
|
| 1609 | - public function clearCache(string $userId, bool $reload = false): void { |
|
| 1610 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 1611 | - $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; |
|
| 1612 | - $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = []; |
|
| 1613 | - |
|
| 1614 | - if (!$reload) { |
|
| 1615 | - return; |
|
| 1616 | - } |
|
| 1617 | - |
|
| 1618 | - $this->loadConfigAll($userId); |
|
| 1619 | - } |
|
| 1620 | - |
|
| 1621 | - /** |
|
| 1622 | - * @inheritDoc |
|
| 1623 | - * |
|
| 1624 | - * @since 31.0.0 |
|
| 1625 | - */ |
|
| 1626 | - public function clearCacheAll(): void { |
|
| 1627 | - $this->lazyLoaded = $this->fastLoaded = []; |
|
| 1628 | - $this->lazyCache = $this->fastCache = $this->valueDetails = []; |
|
| 1629 | - } |
|
| 1630 | - |
|
| 1631 | - /** |
|
| 1632 | - * For debug purpose. |
|
| 1633 | - * Returns the cached data. |
|
| 1634 | - * |
|
| 1635 | - * @return array |
|
| 1636 | - * @since 31.0.0 |
|
| 1637 | - * @internal |
|
| 1638 | - */ |
|
| 1639 | - public function statusCache(): array { |
|
| 1640 | - return [ |
|
| 1641 | - 'fastLoaded' => $this->fastLoaded, |
|
| 1642 | - 'fastCache' => $this->fastCache, |
|
| 1643 | - 'lazyLoaded' => $this->lazyLoaded, |
|
| 1644 | - 'lazyCache' => $this->lazyCache, |
|
| 1645 | - 'valueDetails' => $this->valueDetails, |
|
| 1646 | - ]; |
|
| 1647 | - } |
|
| 1648 | - |
|
| 1649 | - /** |
|
| 1650 | - * @param int $needle bitflag to search |
|
| 1651 | - * @param int $flags all flags |
|
| 1652 | - * |
|
| 1653 | - * @return bool TRUE if bitflag $needle is set in $flags |
|
| 1654 | - */ |
|
| 1655 | - private function isFlagged(int $needle, int $flags): bool { |
|
| 1656 | - return (($needle & $flags) !== 0); |
|
| 1657 | - } |
|
| 1658 | - |
|
| 1659 | - /** |
|
| 1660 | - * Confirm the string set for app and key fit the database description |
|
| 1661 | - * |
|
| 1662 | - * @param string $userId |
|
| 1663 | - * @param string $app assert $app fit in database |
|
| 1664 | - * @param string $prefKey assert config key fit in database |
|
| 1665 | - * @param bool $allowEmptyUser |
|
| 1666 | - * @param bool $allowEmptyApp $app can be empty string |
|
| 1667 | - * @param ValueType|null $valueType assert value type is only one type |
|
| 1668 | - */ |
|
| 1669 | - private function assertParams( |
|
| 1670 | - string $userId = '', |
|
| 1671 | - string $app = '', |
|
| 1672 | - string $prefKey = '', |
|
| 1673 | - bool $allowEmptyUser = false, |
|
| 1674 | - bool $allowEmptyApp = false, |
|
| 1675 | - ): void { |
|
| 1676 | - if (!$allowEmptyUser && $userId === '') { |
|
| 1677 | - throw new InvalidArgumentException('userId cannot be an empty string'); |
|
| 1678 | - } |
|
| 1679 | - if (!$allowEmptyApp && $app === '') { |
|
| 1680 | - throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1681 | - } |
|
| 1682 | - if (strlen($userId) > self::USER_MAX_LENGTH) { |
|
| 1683 | - throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')'); |
|
| 1684 | - } |
|
| 1685 | - if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1686 | - throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')'); |
|
| 1687 | - } |
|
| 1688 | - if (strlen($prefKey) > self::KEY_MAX_LENGTH) { |
|
| 1689 | - throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1690 | - } |
|
| 1691 | - } |
|
| 1692 | - |
|
| 1693 | - private function loadConfigAll(string $userId): void { |
|
| 1694 | - $this->loadConfig($userId, null); |
|
| 1695 | - } |
|
| 1696 | - |
|
| 1697 | - /** |
|
| 1698 | - * Load normal config or config set as lazy loaded |
|
| 1699 | - * |
|
| 1700 | - * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1701 | - */ |
|
| 1702 | - private function loadConfig(string $userId, ?bool $lazy = false): void { |
|
| 1703 | - if ($this->isLoaded($userId, $lazy)) { |
|
| 1704 | - return; |
|
| 1705 | - } |
|
| 1706 | - |
|
| 1707 | - if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log |
|
| 1708 | - $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); |
|
| 1709 | - } |
|
| 1710 | - |
|
| 1711 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1712 | - $qb->from('preferences'); |
|
| 1713 | - $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); |
|
| 1714 | - $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1715 | - |
|
| 1716 | - // we only need value from lazy when loadConfig does not specify it |
|
| 1717 | - if ($lazy !== null) { |
|
| 1718 | - $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1719 | - } else { |
|
| 1720 | - $qb->addSelect('lazy'); |
|
| 1721 | - } |
|
| 1722 | - |
|
| 1723 | - $result = $qb->executeQuery(); |
|
| 1724 | - $rows = $result->fetchAll(); |
|
| 1725 | - foreach ($rows as $row) { |
|
| 1726 | - if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1727 | - $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1728 | - } else { |
|
| 1729 | - $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1730 | - } |
|
| 1731 | - $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']]; |
|
| 1732 | - } |
|
| 1733 | - $result->closeCursor(); |
|
| 1734 | - $this->setAsLoaded($userId, $lazy); |
|
| 1735 | - } |
|
| 1736 | - |
|
| 1737 | - /** |
|
| 1738 | - * if $lazy is: |
|
| 1739 | - * - false: will returns true if fast config are loaded |
|
| 1740 | - * - true : will returns true if lazy config are loaded |
|
| 1741 | - * - null : will returns true if both config are loaded |
|
| 1742 | - * |
|
| 1743 | - * @param string $userId |
|
| 1744 | - * @param bool $lazy |
|
| 1745 | - * |
|
| 1746 | - * @return bool |
|
| 1747 | - */ |
|
| 1748 | - private function isLoaded(string $userId, ?bool $lazy): bool { |
|
| 1749 | - if ($lazy === null) { |
|
| 1750 | - return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false); |
|
| 1751 | - } |
|
| 1752 | - |
|
| 1753 | - return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false; |
|
| 1754 | - } |
|
| 1755 | - |
|
| 1756 | - /** |
|
| 1757 | - * if $lazy is: |
|
| 1758 | - * - false: set fast config as loaded |
|
| 1759 | - * - true : set lazy config as loaded |
|
| 1760 | - * - null : set both config as loaded |
|
| 1761 | - * |
|
| 1762 | - * @param string $userId |
|
| 1763 | - * @param bool $lazy |
|
| 1764 | - */ |
|
| 1765 | - private function setAsLoaded(string $userId, ?bool $lazy): void { |
|
| 1766 | - if ($lazy === null) { |
|
| 1767 | - $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true; |
|
| 1768 | - return; |
|
| 1769 | - } |
|
| 1770 | - |
|
| 1771 | - // We also create empty entry to keep both fastLoaded/lazyLoaded synced |
|
| 1772 | - if ($lazy) { |
|
| 1773 | - $this->lazyLoaded[$userId] = true; |
|
| 1774 | - $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false; |
|
| 1775 | - $this->fastCache[$userId] = $this->fastCache[$userId] ?? []; |
|
| 1776 | - } else { |
|
| 1777 | - $this->fastLoaded[$userId] = true; |
|
| 1778 | - $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false; |
|
| 1779 | - $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? []; |
|
| 1780 | - } |
|
| 1781 | - } |
|
| 1782 | - |
|
| 1783 | - /** |
|
| 1784 | - * **Warning:** this will load all lazy values from the database |
|
| 1785 | - * |
|
| 1786 | - * @param string $userId id of the user |
|
| 1787 | - * @param string $app id of the app |
|
| 1788 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 1789 | - * |
|
| 1790 | - * @return array<string, string|int|float|bool|array> |
|
| 1791 | - */ |
|
| 1792 | - private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array { |
|
| 1793 | - foreach ($values as $key => $value) { |
|
| 1794 | - //$key = (string)$key; |
|
| 1795 | - try { |
|
| 1796 | - $type = $this->getValueType($userId, $app, (string)$key); |
|
| 1797 | - } catch (UnknownKeyException) { |
|
| 1798 | - continue; |
|
| 1799 | - } |
|
| 1800 | - |
|
| 1801 | - if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1802 | - if ($filtered) { |
|
| 1803 | - $value = IConfig::SENSITIVE_VALUE; |
|
| 1804 | - $type = ValueType::STRING; |
|
| 1805 | - } else { |
|
| 1806 | - $this->decryptSensitiveValue($userId, $app, (string)$key, $value); |
|
| 1807 | - } |
|
| 1808 | - } |
|
| 1809 | - |
|
| 1810 | - $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1811 | - } |
|
| 1812 | - |
|
| 1813 | - return $values; |
|
| 1814 | - } |
|
| 1815 | - |
|
| 1816 | - /** |
|
| 1817 | - * convert string value to the expected type |
|
| 1818 | - * |
|
| 1819 | - * @param string $value |
|
| 1820 | - * @param ValueType $type |
|
| 1821 | - * |
|
| 1822 | - * @return string|int|float|bool|array |
|
| 1823 | - */ |
|
| 1824 | - private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array { |
|
| 1825 | - switch ($type) { |
|
| 1826 | - case ValueType::INT: |
|
| 1827 | - return (int)$value; |
|
| 1828 | - case ValueType::FLOAT: |
|
| 1829 | - return (float)$value; |
|
| 1830 | - case ValueType::BOOL: |
|
| 1831 | - return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1832 | - case ValueType::ARRAY: |
|
| 1833 | - try { |
|
| 1834 | - return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1835 | - } catch (JsonException) { |
|
| 1836 | - // ignoreable |
|
| 1837 | - } |
|
| 1838 | - break; |
|
| 1839 | - } |
|
| 1840 | - return $value; |
|
| 1841 | - } |
|
| 1842 | - |
|
| 1843 | - |
|
| 1844 | - /** |
|
| 1845 | - * will change referenced $value with the decrypted value in case of encrypted (sensitive value) |
|
| 1846 | - * |
|
| 1847 | - * @param string $userId |
|
| 1848 | - * @param string $app |
|
| 1849 | - * @param string $key |
|
| 1850 | - * @param string $value |
|
| 1851 | - */ |
|
| 1852 | - private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { |
|
| 1853 | - if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1854 | - return; |
|
| 1855 | - } |
|
| 1856 | - |
|
| 1857 | - if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1858 | - return; |
|
| 1859 | - } |
|
| 1860 | - |
|
| 1861 | - try { |
|
| 1862 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1863 | - } catch (\Exception $e) { |
|
| 1864 | - $this->logger->warning('could not decrypt sensitive value', [ |
|
| 1865 | - 'userId' => $userId, |
|
| 1866 | - 'app' => $app, |
|
| 1867 | - 'key' => $key, |
|
| 1868 | - 'value' => $value, |
|
| 1869 | - 'exception' => $e |
|
| 1870 | - ]); |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - /** |
|
| 1875 | - * Match and apply current use of config values with defined lexicon. |
|
| 1876 | - * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1877 | - * |
|
| 1878 | - * @throws UnknownKeyException |
|
| 1879 | - * @throws TypeConflictException |
|
| 1880 | - * @return bool FALSE if conflict with defined lexicon were observed in the process |
|
| 1881 | - */ |
|
| 1882 | - private function matchAndApplyLexiconDefinition( |
|
| 1883 | - string $userId, |
|
| 1884 | - string $app, |
|
| 1885 | - string &$key, |
|
| 1886 | - ?bool &$lazy = null, |
|
| 1887 | - ValueType &$type = ValueType::MIXED, |
|
| 1888 | - int &$flags = 0, |
|
| 1889 | - string &$default = '', |
|
| 1890 | - ): bool { |
|
| 1891 | - $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1892 | - if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1893 | - // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1894 | - $key = $configDetails['aliases'][$key]; |
|
| 1895 | - } |
|
| 1896 | - |
|
| 1897 | - if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1898 | - return $this->applyLexiconStrictness($configDetails['strictness'], 'The user config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1899 | - } |
|
| 1900 | - |
|
| 1901 | - // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1902 | - if ($lazy === null) { |
|
| 1903 | - return true; |
|
| 1904 | - } |
|
| 1905 | - |
|
| 1906 | - /** @var ConfigLexiconEntry $configValue */ |
|
| 1907 | - $configValue = $configDetails['entries'][$key]; |
|
| 1908 | - if ($type === ValueType::MIXED) { |
|
| 1909 | - // we overwrite if value was requested as mixed |
|
| 1910 | - $type = $configValue->getValueType(); |
|
| 1911 | - } elseif ($configValue->getValueType() !== $type) { |
|
| 1912 | - throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1913 | - } |
|
| 1914 | - |
|
| 1915 | - $lazy = $configValue->isLazy(); |
|
| 1916 | - $flags = $configValue->getFlags(); |
|
| 1917 | - if ($configValue->isDeprecated()) { |
|
| 1918 | - $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1919 | - } |
|
| 1920 | - |
|
| 1921 | - $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; |
|
| 1922 | - if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1923 | - // if key exists there should be no need to extract default |
|
| 1924 | - return true; |
|
| 1925 | - } |
|
| 1926 | - |
|
| 1927 | - // default from Lexicon got priority but it can still be overwritten by admin |
|
| 1928 | - $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault() ?? $default; |
|
| 1929 | - |
|
| 1930 | - // returning false will make get() returning $default and set() not changing value in database |
|
| 1931 | - return !$enforcedValue; |
|
| 1932 | - } |
|
| 1933 | - |
|
| 1934 | - /** |
|
| 1935 | - * get default value set in config/config.php if stored in key: |
|
| 1936 | - * |
|
| 1937 | - * 'lexicon.default.userconfig' => [ |
|
| 1938 | - * <appId> => [ |
|
| 1939 | - * <configKey> => 'my value', |
|
| 1940 | - * ] |
|
| 1941 | - * ], |
|
| 1942 | - * |
|
| 1943 | - * The entry is converted to string to fit the expected type when managing default value |
|
| 1944 | - */ |
|
| 1945 | - private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string { |
|
| 1946 | - $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null; |
|
| 1947 | - if ($default === null) { |
|
| 1948 | - // no system default, using default default. |
|
| 1949 | - return null; |
|
| 1950 | - } |
|
| 1951 | - |
|
| 1952 | - return $configValue->convertToString($default); |
|
| 1953 | - } |
|
| 1954 | - |
|
| 1955 | - /** |
|
| 1956 | - * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1957 | - * |
|
| 1958 | - * @see IConfigLexicon::getStrictness() |
|
| 1959 | - * @param ConfigLexiconStrictness|null $strictness |
|
| 1960 | - * @param string $line |
|
| 1961 | - * |
|
| 1962 | - * @return bool TRUE if conflict can be fully ignored |
|
| 1963 | - * @throws UnknownKeyException |
|
| 1964 | - */ |
|
| 1965 | - private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, string $line = ''): bool { |
|
| 1966 | - if ($strictness === null) { |
|
| 1967 | - return true; |
|
| 1968 | - } |
|
| 1969 | - |
|
| 1970 | - switch ($strictness) { |
|
| 1971 | - case ConfigLexiconStrictness::IGNORE: |
|
| 1972 | - return true; |
|
| 1973 | - case ConfigLexiconStrictness::NOTICE: |
|
| 1974 | - $this->logger->notice($line); |
|
| 1975 | - return true; |
|
| 1976 | - case ConfigLexiconStrictness::WARNING: |
|
| 1977 | - $this->logger->warning($line); |
|
| 1978 | - return false; |
|
| 1979 | - case ConfigLexiconStrictness::EXCEPTION: |
|
| 1980 | - throw new UnknownKeyException($line); |
|
| 1981 | - } |
|
| 1982 | - |
|
| 1983 | - throw new UnknownKeyException($line); |
|
| 1984 | - } |
|
| 1985 | - |
|
| 1986 | - /** |
|
| 1987 | - * extract details from registered $appId's config lexicon |
|
| 1988 | - * |
|
| 1989 | - * @param string $appId |
|
| 1990 | - * @internal |
|
| 1991 | - * |
|
| 1992 | - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1993 | - */ |
|
| 1994 | - public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1995 | - if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1996 | - $entries = $aliases = []; |
|
| 1997 | - $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1998 | - $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1999 | - foreach ($configLexicon?->getUserConfigs() ?? [] as $configEntry) { |
|
| 2000 | - $entries[$configEntry->getKey()] = $configEntry; |
|
| 2001 | - if ($configEntry->getRename() !== null) { |
|
| 2002 | - $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 2003 | - } |
|
| 2004 | - } |
|
| 2005 | - |
|
| 2006 | - $this->configLexiconDetails[$appId] = [ |
|
| 2007 | - 'entries' => $entries, |
|
| 2008 | - 'aliases' => $aliases, |
|
| 2009 | - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 2010 | - ]; |
|
| 2011 | - } |
|
| 2012 | - |
|
| 2013 | - return $this->configLexiconDetails[$appId]; |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 2017 | - return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 2018 | - } |
|
| 2019 | - |
|
| 2020 | - /** |
|
| 2021 | - * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 2022 | - * |
|
| 2023 | - * @internal |
|
| 2024 | - */ |
|
| 2025 | - public function ignoreLexiconAliases(bool $ignore): void { |
|
| 2026 | - $this->ignoreLexiconAliases = $ignore; |
|
| 2027 | - } |
|
| 49 | + private const USER_MAX_LENGTH = 64; |
|
| 50 | + private const APP_MAX_LENGTH = 32; |
|
| 51 | + private const KEY_MAX_LENGTH = 64; |
|
| 52 | + private const INDEX_MAX_LENGTH = 64; |
|
| 53 | + private const ENCRYPTION_PREFIX = '$UserConfigEncryption$'; |
|
| 54 | + private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX) |
|
| 55 | + |
|
| 56 | + /** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 57 | + private array $fastCache = []; // cache for normal config keys |
|
| 58 | + /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 59 | + private array $lazyCache = []; // cache for lazy config keys |
|
| 60 | + /** @var array<string, array<string, array<string, array<string, mixed>>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ |
|
| 61 | + private array $valueDetails = []; // type for all config values |
|
| 62 | + /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 63 | + private array $fastLoaded = []; |
|
| 64 | + /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 65 | + private array $lazyLoaded = []; |
|
| 66 | + /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 67 | + private array $configLexiconDetails = []; |
|
| 68 | + private bool $ignoreLexiconAliases = false; |
|
| 69 | + |
|
| 70 | + public function __construct( |
|
| 71 | + protected IDBConnection $connection, |
|
| 72 | + protected IConfig $config, |
|
| 73 | + protected LoggerInterface $logger, |
|
| 74 | + protected ICrypto $crypto, |
|
| 75 | + ) { |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @inheritDoc |
|
| 80 | + * |
|
| 81 | + * @param string $appId optional id of app |
|
| 82 | + * |
|
| 83 | + * @return list<string> list of userIds |
|
| 84 | + * @since 31.0.0 |
|
| 85 | + */ |
|
| 86 | + public function getUserIds(string $appId = ''): array { |
|
| 87 | + $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true); |
|
| 88 | + |
|
| 89 | + $qb = $this->connection->getQueryBuilder(); |
|
| 90 | + $qb->from('preferences'); |
|
| 91 | + $qb->select('userid'); |
|
| 92 | + $qb->groupBy('userid'); |
|
| 93 | + if ($appId !== '') { |
|
| 94 | + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $result = $qb->executeQuery(); |
|
| 98 | + $rows = $result->fetchAll(); |
|
| 99 | + $userIds = []; |
|
| 100 | + foreach ($rows as $row) { |
|
| 101 | + $userIds[] = $row['userid']; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $userIds; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @inheritDoc |
|
| 109 | + * |
|
| 110 | + * @return list<string> list of app ids |
|
| 111 | + * @since 31.0.0 |
|
| 112 | + */ |
|
| 113 | + public function getApps(string $userId): array { |
|
| 114 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 115 | + $this->loadConfigAll($userId); |
|
| 116 | + $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); |
|
| 117 | + sort($apps); |
|
| 118 | + |
|
| 119 | + return array_values(array_unique($apps)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @inheritDoc |
|
| 124 | + * |
|
| 125 | + * @param string $userId id of the user |
|
| 126 | + * @param string $app id of the app |
|
| 127 | + * |
|
| 128 | + * @return list<string> list of stored config keys |
|
| 129 | + * @since 31.0.0 |
|
| 130 | + */ |
|
| 131 | + public function getKeys(string $userId, string $app): array { |
|
| 132 | + $this->assertParams($userId, $app); |
|
| 133 | + $this->loadConfigAll($userId); |
|
| 134 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 135 | + $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); |
|
| 136 | + sort($keys); |
|
| 137 | + |
|
| 138 | + return array_values(array_unique($keys)); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @inheritDoc |
|
| 143 | + * |
|
| 144 | + * @param string $userId id of the user |
|
| 145 | + * @param string $app id of the app |
|
| 146 | + * @param string $key config key |
|
| 147 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 148 | + * |
|
| 149 | + * @return bool TRUE if key exists |
|
| 150 | + * @since 31.0.0 |
|
| 151 | + */ |
|
| 152 | + public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 153 | + $this->assertParams($userId, $app, $key); |
|
| 154 | + $this->loadConfig($userId, $lazy); |
|
| 155 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 156 | + |
|
| 157 | + if ($lazy === null) { |
|
| 158 | + $appCache = $this->getValues($userId, $app); |
|
| 159 | + return isset($appCache[$key]); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ($lazy) { |
|
| 163 | + return isset($this->lazyCache[$userId][$app][$key]); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return isset($this->fastCache[$userId][$app][$key]); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @inheritDoc |
|
| 171 | + * |
|
| 172 | + * @param string $userId id of the user |
|
| 173 | + * @param string $app id of the app |
|
| 174 | + * @param string $key config key |
|
| 175 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 176 | + * |
|
| 177 | + * @return bool |
|
| 178 | + * @throws UnknownKeyException if config key is not known |
|
| 179 | + * @since 31.0.0 |
|
| 180 | + */ |
|
| 181 | + public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 182 | + $this->assertParams($userId, $app, $key); |
|
| 183 | + $this->loadConfig($userId, $lazy); |
|
| 184 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 185 | + |
|
| 186 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 187 | + throw new UnknownKeyException('unknown config key'); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @inheritDoc |
|
| 195 | + * |
|
| 196 | + * @param string $userId id of the user |
|
| 197 | + * @param string $app id of the app |
|
| 198 | + * @param string $key config key |
|
| 199 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 200 | + * |
|
| 201 | + * @return bool |
|
| 202 | + * @throws UnknownKeyException if config key is not known |
|
| 203 | + * @since 31.0.0 |
|
| 204 | + */ |
|
| 205 | + public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 206 | + $this->assertParams($userId, $app, $key); |
|
| 207 | + $this->loadConfig($userId, $lazy); |
|
| 208 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 209 | + |
|
| 210 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 211 | + throw new UnknownKeyException('unknown config key'); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @inheritDoc |
|
| 219 | + * |
|
| 220 | + * @param string $userId id of the user |
|
| 221 | + * @param string $app if of the app |
|
| 222 | + * @param string $key config key |
|
| 223 | + * |
|
| 224 | + * @return bool TRUE if config is lazy loaded |
|
| 225 | + * @throws UnknownKeyException if config key is not known |
|
| 226 | + * @see IUserConfig for details about lazy loading |
|
| 227 | + * @since 31.0.0 |
|
| 228 | + */ |
|
| 229 | + public function isLazy(string $userId, string $app, string $key): bool { |
|
| 230 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 231 | + |
|
| 232 | + // there is a huge probability the non-lazy config are already loaded |
|
| 233 | + // meaning that we can start by only checking if a current non-lazy key exists |
|
| 234 | + if ($this->hasKey($userId, $app, $key, false)) { |
|
| 235 | + // meaning key is not lazy. |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // as key is not found as non-lazy, we load and search in the lazy config |
|
| 240 | + if ($this->hasKey($userId, $app, $key, true)) { |
|
| 241 | + return true; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + throw new UnknownKeyException('unknown config key'); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @inheritDoc |
|
| 249 | + * |
|
| 250 | + * @param string $userId id of the user |
|
| 251 | + * @param string $app id of the app |
|
| 252 | + * @param string $prefix config keys prefix to search |
|
| 253 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 254 | + * |
|
| 255 | + * @return array<string, string|int|float|bool|array> [key => value] |
|
| 256 | + * @since 31.0.0 |
|
| 257 | + */ |
|
| 258 | + public function getValues( |
|
| 259 | + string $userId, |
|
| 260 | + string $app, |
|
| 261 | + string $prefix = '', |
|
| 262 | + bool $filtered = false, |
|
| 263 | + ): array { |
|
| 264 | + $this->assertParams($userId, $app, $prefix); |
|
| 265 | + // if we want to filter values, we need to get sensitivity |
|
| 266 | + $this->loadConfigAll($userId); |
|
| 267 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 268 | + $values = array_filter( |
|
| 269 | + $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), |
|
| 270 | + function (string $key) use ($prefix): bool { |
|
| 271 | + // filter values based on $prefix |
|
| 272 | + return str_starts_with($key, $prefix); |
|
| 273 | + }, ARRAY_FILTER_USE_KEY |
|
| 274 | + ); |
|
| 275 | + |
|
| 276 | + return $values; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @inheritDoc |
|
| 281 | + * |
|
| 282 | + * @param string $userId id of the user |
|
| 283 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 284 | + * |
|
| 285 | + * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]] |
|
| 286 | + * @since 31.0.0 |
|
| 287 | + */ |
|
| 288 | + public function getAllValues(string $userId, bool $filtered = false): array { |
|
| 289 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 290 | + $this->loadConfigAll($userId); |
|
| 291 | + |
|
| 292 | + $result = []; |
|
| 293 | + foreach ($this->getApps($userId) as $app) { |
|
| 294 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 295 | + $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); |
|
| 296 | + $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + return $result; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @inheritDoc |
|
| 304 | + * |
|
| 305 | + * @param string $userId id of the user |
|
| 306 | + * @param string $key config key |
|
| 307 | + * @param bool $lazy search within lazy loaded config |
|
| 308 | + * @param ValueType|null $typedAs enforce type for the returned values |
|
| 309 | + * |
|
| 310 | + * @return array<string, string|int|float|bool|array> [appId => value] |
|
| 311 | + * @since 31.0.0 |
|
| 312 | + */ |
|
| 313 | + public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { |
|
| 314 | + $this->assertParams($userId, '', $key, allowEmptyApp: true); |
|
| 315 | + $this->loadConfig($userId, $lazy); |
|
| 316 | + |
|
| 317 | + /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 318 | + if ($lazy) { |
|
| 319 | + $cache = $this->lazyCache[$userId]; |
|
| 320 | + } else { |
|
| 321 | + $cache = $this->fastCache[$userId]; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $values = []; |
|
| 325 | + foreach (array_keys($cache) as $app) { |
|
| 326 | + if (isset($cache[$app][$key])) { |
|
| 327 | + $value = $cache[$app][$key]; |
|
| 328 | + try { |
|
| 329 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 330 | + $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy)); |
|
| 331 | + } catch (IncorrectTypeException|UnknownKeyException) { |
|
| 332 | + } |
|
| 333 | + $values[$app] = $value; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + return $values; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @inheritDoc |
|
| 343 | + * |
|
| 344 | + * @param string $app id of the app |
|
| 345 | + * @param string $key config key |
|
| 346 | + * @param ValueType|null $typedAs enforce type for the returned values |
|
| 347 | + * @param array|null $userIds limit to a list of user ids |
|
| 348 | + * |
|
| 349 | + * @return array<string, string|int|float|bool|array> [userId => value] |
|
| 350 | + * @since 31.0.0 |
|
| 351 | + */ |
|
| 352 | + public function getValuesByUsers( |
|
| 353 | + string $app, |
|
| 354 | + string $key, |
|
| 355 | + ?ValueType $typedAs = null, |
|
| 356 | + ?array $userIds = null, |
|
| 357 | + ): array { |
|
| 358 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 359 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 360 | + |
|
| 361 | + $qb = $this->connection->getQueryBuilder(); |
|
| 362 | + $qb->select('userid', 'configvalue', 'type') |
|
| 363 | + ->from('preferences') |
|
| 364 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 365 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 366 | + |
|
| 367 | + $values = []; |
|
| 368 | + // this nested function will execute current Query and store result within $values. |
|
| 369 | + $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult { |
|
| 370 | + $result = $qb->executeQuery(); |
|
| 371 | + while ($row = $result->fetch()) { |
|
| 372 | + $value = $row['configvalue']; |
|
| 373 | + try { |
|
| 374 | + $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type'])); |
|
| 375 | + } catch (IncorrectTypeException) { |
|
| 376 | + } |
|
| 377 | + $values[$row['userid']] = $value; |
|
| 378 | + } |
|
| 379 | + return $result; |
|
| 380 | + }; |
|
| 381 | + |
|
| 382 | + // if no userIds to filter, we execute query as it is and returns all values ... |
|
| 383 | + if ($userIds === null) { |
|
| 384 | + $result = $executeAndStoreValue($qb); |
|
| 385 | + $result->closeCursor(); |
|
| 386 | + return $values; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values |
|
| 390 | + $result = null; |
|
| 391 | + $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); |
|
| 392 | + foreach (array_chunk($userIds, 50, true) as $chunk) { |
|
| 393 | + $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 394 | + $result = $executeAndStoreValue($qb); |
|
| 395 | + } |
|
| 396 | + $result?->closeCursor(); |
|
| 397 | + |
|
| 398 | + return $values; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @inheritDoc |
|
| 403 | + * |
|
| 404 | + * @param string $app id of the app |
|
| 405 | + * @param string $key config key |
|
| 406 | + * @param string $value config value |
|
| 407 | + * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string |
|
| 408 | + * |
|
| 409 | + * @return Generator<string> |
|
| 410 | + * @since 31.0.0 |
|
| 411 | + */ |
|
| 412 | + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator { |
|
| 413 | + return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @inheritDoc |
|
| 418 | + * |
|
| 419 | + * @param string $app id of the app |
|
| 420 | + * @param string $key config key |
|
| 421 | + * @param int $value config value |
|
| 422 | + * |
|
| 423 | + * @return Generator<string> |
|
| 424 | + * @since 31.0.0 |
|
| 425 | + */ |
|
| 426 | + public function searchUsersByValueInt(string $app, string $key, int $value): Generator { |
|
| 427 | + return $this->searchUsersByValueString($app, $key, (string)$value); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * @inheritDoc |
|
| 432 | + * |
|
| 433 | + * @param string $app id of the app |
|
| 434 | + * @param string $key config key |
|
| 435 | + * @param array $values list of config values |
|
| 436 | + * |
|
| 437 | + * @return Generator<string> |
|
| 438 | + * @since 31.0.0 |
|
| 439 | + */ |
|
| 440 | + public function searchUsersByValues(string $app, string $key, array $values): Generator { |
|
| 441 | + return $this->searchUsersByTypedValue($app, $key, $values); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * @inheritDoc |
|
| 446 | + * |
|
| 447 | + * @param string $app id of the app |
|
| 448 | + * @param string $key config key |
|
| 449 | + * @param bool $value config value |
|
| 450 | + * |
|
| 451 | + * @return Generator<string> |
|
| 452 | + * @since 31.0.0 |
|
| 453 | + */ |
|
| 454 | + public function searchUsersByValueBool(string $app, string $key, bool $value): Generator { |
|
| 455 | + $values = ['0', 'off', 'false', 'no']; |
|
| 456 | + if ($value) { |
|
| 457 | + $values = ['1', 'on', 'true', 'yes']; |
|
| 458 | + } |
|
| 459 | + return $this->searchUsersByValues($app, $key, $values); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * returns a list of users with config key set to a specific value, or within the list of |
|
| 464 | + * possible values |
|
| 465 | + * |
|
| 466 | + * @param string $app |
|
| 467 | + * @param string $key |
|
| 468 | + * @param string|array $value |
|
| 469 | + * @param bool $caseInsensitive |
|
| 470 | + * |
|
| 471 | + * @return Generator<string> |
|
| 472 | + */ |
|
| 473 | + private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator { |
|
| 474 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 475 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 476 | + |
|
| 477 | + $qb = $this->connection->getQueryBuilder(); |
|
| 478 | + $qb->from('preferences'); |
|
| 479 | + $qb->select('userid'); |
|
| 480 | + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 481 | + $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 482 | + |
|
| 483 | + // search within 'indexed' OR 'configvalue' only if 'flags' is set as not indexed |
|
| 484 | + // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed |
|
| 485 | + $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; |
|
| 486 | + if (is_array($value)) { |
|
| 487 | + $where = $qb->expr()->orX( |
|
| 488 | + $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 489 | + $qb->expr()->andX( |
|
| 490 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 491 | + $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 492 | + ) |
|
| 493 | + ); |
|
| 494 | + } else { |
|
| 495 | + if ($caseInsensitive) { |
|
| 496 | + $where = $qb->expr()->orX( |
|
| 497 | + $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), |
|
| 498 | + $qb->expr()->andX( |
|
| 499 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 500 | + $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) |
|
| 501 | + ) |
|
| 502 | + ); |
|
| 503 | + } else { |
|
| 504 | + $where = $qb->expr()->orX( |
|
| 505 | + $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), |
|
| 506 | + $qb->expr()->andX( |
|
| 507 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 508 | + $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) |
|
| 509 | + ) |
|
| 510 | + ); |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + $qb->andWhere($where); |
|
| 515 | + $result = $qb->executeQuery(); |
|
| 516 | + while ($row = $result->fetch()) { |
|
| 517 | + yield $row['userid']; |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Get the config value as string. |
|
| 523 | + * If the value does not exist the given default will be returned. |
|
| 524 | + * |
|
| 525 | + * Set lazy to `null` to ignore it and get the value from either source. |
|
| 526 | + * |
|
| 527 | + * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 528 | + * |
|
| 529 | + * @param string $userId id of the user |
|
| 530 | + * @param string $app id of the app |
|
| 531 | + * @param string $key config key |
|
| 532 | + * @param string $default config value |
|
| 533 | + * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 534 | + * |
|
| 535 | + * @return string the value or $default |
|
| 536 | + * @throws TypeConflictException |
|
| 537 | + * @internal |
|
| 538 | + * @since 31.0.0 |
|
| 539 | + * @see IUserConfig for explanation about lazy loading |
|
| 540 | + * @see getValueString() |
|
| 541 | + * @see getValueInt() |
|
| 542 | + * @see getValueFloat() |
|
| 543 | + * @see getValueBool() |
|
| 544 | + * @see getValueArray() |
|
| 545 | + */ |
|
| 546 | + public function getValueMixed( |
|
| 547 | + string $userId, |
|
| 548 | + string $app, |
|
| 549 | + string $key, |
|
| 550 | + string $default = '', |
|
| 551 | + ?bool $lazy = false, |
|
| 552 | + ): string { |
|
| 553 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 554 | + try { |
|
| 555 | + $lazy ??= $this->isLazy($userId, $app, $key); |
|
| 556 | + } catch (UnknownKeyException) { |
|
| 557 | + return $default; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + return $this->getTypedValue( |
|
| 561 | + $userId, |
|
| 562 | + $app, |
|
| 563 | + $key, |
|
| 564 | + $default, |
|
| 565 | + $lazy, |
|
| 566 | + ValueType::MIXED |
|
| 567 | + ); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * @inheritDoc |
|
| 572 | + * |
|
| 573 | + * @param string $userId id of the user |
|
| 574 | + * @param string $app id of the app |
|
| 575 | + * @param string $key config key |
|
| 576 | + * @param string $default default value |
|
| 577 | + * @param bool $lazy search within lazy loaded config |
|
| 578 | + * |
|
| 579 | + * @return string stored config value or $default if not set in database |
|
| 580 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 581 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 582 | + * @since 31.0.0 |
|
| 583 | + * @see IUserConfig for explanation about lazy loading |
|
| 584 | + */ |
|
| 585 | + public function getValueString( |
|
| 586 | + string $userId, |
|
| 587 | + string $app, |
|
| 588 | + string $key, |
|
| 589 | + string $default = '', |
|
| 590 | + bool $lazy = false, |
|
| 591 | + ): string { |
|
| 592 | + return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * @inheritDoc |
|
| 597 | + * |
|
| 598 | + * @param string $userId id of the user |
|
| 599 | + * @param string $app id of the app |
|
| 600 | + * @param string $key config key |
|
| 601 | + * @param int $default default value |
|
| 602 | + * @param bool $lazy search within lazy loaded config |
|
| 603 | + * |
|
| 604 | + * @return int stored config value or $default if not set in database |
|
| 605 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 606 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 607 | + * @since 31.0.0 |
|
| 608 | + * @see IUserConfig for explanation about lazy loading |
|
| 609 | + */ |
|
| 610 | + public function getValueInt( |
|
| 611 | + string $userId, |
|
| 612 | + string $app, |
|
| 613 | + string $key, |
|
| 614 | + int $default = 0, |
|
| 615 | + bool $lazy = false, |
|
| 616 | + ): int { |
|
| 617 | + return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * @inheritDoc |
|
| 622 | + * |
|
| 623 | + * @param string $userId id of the user |
|
| 624 | + * @param string $app id of the app |
|
| 625 | + * @param string $key config key |
|
| 626 | + * @param float $default default value |
|
| 627 | + * @param bool $lazy search within lazy loaded config |
|
| 628 | + * |
|
| 629 | + * @return float stored config value or $default if not set in database |
|
| 630 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 631 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 632 | + * @since 31.0.0 |
|
| 633 | + * @see IUserConfig for explanation about lazy loading |
|
| 634 | + */ |
|
| 635 | + public function getValueFloat( |
|
| 636 | + string $userId, |
|
| 637 | + string $app, |
|
| 638 | + string $key, |
|
| 639 | + float $default = 0, |
|
| 640 | + bool $lazy = false, |
|
| 641 | + ): float { |
|
| 642 | + return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * @inheritDoc |
|
| 647 | + * |
|
| 648 | + * @param string $userId id of the user |
|
| 649 | + * @param string $app id of the app |
|
| 650 | + * @param string $key config key |
|
| 651 | + * @param bool $default default value |
|
| 652 | + * @param bool $lazy search within lazy loaded config |
|
| 653 | + * |
|
| 654 | + * @return bool stored config value or $default if not set in database |
|
| 655 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 656 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 657 | + * @since 31.0.0 |
|
| 658 | + * @see IUserConfig for explanation about lazy loading |
|
| 659 | + */ |
|
| 660 | + public function getValueBool( |
|
| 661 | + string $userId, |
|
| 662 | + string $app, |
|
| 663 | + string $key, |
|
| 664 | + bool $default = false, |
|
| 665 | + bool $lazy = false, |
|
| 666 | + ): bool { |
|
| 667 | + $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)); |
|
| 668 | + return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * @inheritDoc |
|
| 673 | + * |
|
| 674 | + * @param string $userId id of the user |
|
| 675 | + * @param string $app id of the app |
|
| 676 | + * @param string $key config key |
|
| 677 | + * @param array $default default value |
|
| 678 | + * @param bool $lazy search within lazy loaded config |
|
| 679 | + * |
|
| 680 | + * @return array stored config value or $default if not set in database |
|
| 681 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 682 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 683 | + * @since 31.0.0 |
|
| 684 | + * @see IUserConfig for explanation about lazy loading |
|
| 685 | + */ |
|
| 686 | + public function getValueArray( |
|
| 687 | + string $userId, |
|
| 688 | + string $app, |
|
| 689 | + string $key, |
|
| 690 | + array $default = [], |
|
| 691 | + bool $lazy = false, |
|
| 692 | + ): array { |
|
| 693 | + try { |
|
| 694 | + $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 695 | + $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 696 | + |
|
| 697 | + return is_array($value) ? $value : []; |
|
| 698 | + } catch (JsonException) { |
|
| 699 | + return []; |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * @param string $userId |
|
| 705 | + * @param string $app id of the app |
|
| 706 | + * @param string $key config key |
|
| 707 | + * @param string $default default value |
|
| 708 | + * @param bool $lazy search within lazy loaded config |
|
| 709 | + * @param ValueType $type value type |
|
| 710 | + * |
|
| 711 | + * @return string |
|
| 712 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 713 | + */ |
|
| 714 | + private function getTypedValue( |
|
| 715 | + string $userId, |
|
| 716 | + string $app, |
|
| 717 | + string $key, |
|
| 718 | + string $default, |
|
| 719 | + bool $lazy, |
|
| 720 | + ValueType $type, |
|
| 721 | + ): string { |
|
| 722 | + $this->assertParams($userId, $app, $key); |
|
| 723 | + $origKey = $key; |
|
| 724 | + if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default)) { |
|
| 725 | + // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 726 | + return $default; |
|
| 727 | + } |
|
| 728 | + $this->loadConfig($userId, $lazy); |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * We ignore check if mixed type is requested. |
|
| 732 | + * If type of stored value is set as mixed, we don't filter. |
|
| 733 | + * If type of stored value is defined, we compare with the one requested. |
|
| 734 | + */ |
|
| 735 | + $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 736 | + if ($type !== ValueType::MIXED |
|
| 737 | + && $knownType !== null |
|
| 738 | + && $knownType !== ValueType::MIXED |
|
| 739 | + && $type !== $knownType) { |
|
| 740 | + $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 741 | + throw new TypeConflictException('conflict with value type from database'); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * - the pair $app/$key cannot exist in both array, |
|
| 746 | + * - we should still return an existing non-lazy value even if current method |
|
| 747 | + * is called with $lazy is true |
|
| 748 | + * |
|
| 749 | + * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 750 | + */ |
|
| 751 | + if (isset($this->lazyCache[$userId][$app][$key])) { |
|
| 752 | + $value = $this->lazyCache[$userId][$app][$key]; |
|
| 753 | + } elseif (isset($this->fastCache[$userId][$app][$key])) { |
|
| 754 | + $value = $this->fastCache[$userId][$app][$key]; |
|
| 755 | + } else { |
|
| 756 | + return $default; |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 760 | + |
|
| 761 | + // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 762 | + // interested to check options in case a modification of the value is needed |
|
| 763 | + // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 764 | + if ($origKey !== $key && $type === ValueType::BOOL) { |
|
| 765 | + $configManager = Server::get(ConfigManager::class); |
|
| 766 | + $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + return $value; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * @inheritDoc |
|
| 774 | + * |
|
| 775 | + * @param string $userId id of the user |
|
| 776 | + * @param string $app id of the app |
|
| 777 | + * @param string $key config key |
|
| 778 | + * |
|
| 779 | + * @return ValueType type of the value |
|
| 780 | + * @throws UnknownKeyException if config key is not known |
|
| 781 | + * @throws IncorrectTypeException if config value type is not known |
|
| 782 | + * @since 31.0.0 |
|
| 783 | + */ |
|
| 784 | + public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { |
|
| 785 | + $this->assertParams($userId, $app, $key); |
|
| 786 | + $this->loadConfig($userId, $lazy); |
|
| 787 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 788 | + |
|
| 789 | + if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { |
|
| 790 | + throw new UnknownKeyException('unknown config key'); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + return $this->valueDetails[$userId][$app][$key]['type']; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + /** |
|
| 797 | + * @inheritDoc |
|
| 798 | + * |
|
| 799 | + * @param string $userId id of the user |
|
| 800 | + * @param string $app id of the app |
|
| 801 | + * @param string $key config key |
|
| 802 | + * @param bool $lazy lazy loading |
|
| 803 | + * |
|
| 804 | + * @return int flags applied to value |
|
| 805 | + * @throws UnknownKeyException if config key is not known |
|
| 806 | + * @throws IncorrectTypeException if config value type is not known |
|
| 807 | + * @since 31.0.0 |
|
| 808 | + */ |
|
| 809 | + public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { |
|
| 810 | + $this->assertParams($userId, $app, $key); |
|
| 811 | + $this->loadConfig($userId, $lazy); |
|
| 812 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 813 | + |
|
| 814 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 815 | + throw new UnknownKeyException('unknown config key'); |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + return $this->valueDetails[$userId][$app][$key]['flags']; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * Store a config key and its value in database as VALUE_MIXED |
|
| 823 | + * |
|
| 824 | + * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 825 | + * |
|
| 826 | + * @param string $userId id of the user |
|
| 827 | + * @param string $app id of the app |
|
| 828 | + * @param string $key config key |
|
| 829 | + * @param string $value config value |
|
| 830 | + * @param bool $lazy set config as lazy loaded |
|
| 831 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 832 | + * |
|
| 833 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 834 | + * @throws TypeConflictException if type from database is not VALUE_MIXED |
|
| 835 | + * @internal |
|
| 836 | + * @since 31.0.0 |
|
| 837 | + * @see IUserConfig for explanation about lazy loading |
|
| 838 | + * @see setValueString() |
|
| 839 | + * @see setValueInt() |
|
| 840 | + * @see setValueFloat() |
|
| 841 | + * @see setValueBool() |
|
| 842 | + * @see setValueArray() |
|
| 843 | + */ |
|
| 844 | + public function setValueMixed( |
|
| 845 | + string $userId, |
|
| 846 | + string $app, |
|
| 847 | + string $key, |
|
| 848 | + string $value, |
|
| 849 | + bool $lazy = false, |
|
| 850 | + int $flags = 0, |
|
| 851 | + ): bool { |
|
| 852 | + return $this->setTypedValue( |
|
| 853 | + $userId, |
|
| 854 | + $app, |
|
| 855 | + $key, |
|
| 856 | + $value, |
|
| 857 | + $lazy, |
|
| 858 | + $flags, |
|
| 859 | + ValueType::MIXED |
|
| 860 | + ); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + |
|
| 864 | + /** |
|
| 865 | + * @inheritDoc |
|
| 866 | + * |
|
| 867 | + * @param string $userId id of the user |
|
| 868 | + * @param string $app id of the app |
|
| 869 | + * @param string $key config key |
|
| 870 | + * @param string $value config value |
|
| 871 | + * @param bool $lazy set config as lazy loaded |
|
| 872 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 873 | + * |
|
| 874 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 875 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 876 | + * @since 31.0.0 |
|
| 877 | + * @see IUserConfig for explanation about lazy loading |
|
| 878 | + */ |
|
| 879 | + public function setValueString( |
|
| 880 | + string $userId, |
|
| 881 | + string $app, |
|
| 882 | + string $key, |
|
| 883 | + string $value, |
|
| 884 | + bool $lazy = false, |
|
| 885 | + int $flags = 0, |
|
| 886 | + ): bool { |
|
| 887 | + return $this->setTypedValue( |
|
| 888 | + $userId, |
|
| 889 | + $app, |
|
| 890 | + $key, |
|
| 891 | + $value, |
|
| 892 | + $lazy, |
|
| 893 | + $flags, |
|
| 894 | + ValueType::STRING |
|
| 895 | + ); |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + /** |
|
| 899 | + * @inheritDoc |
|
| 900 | + * |
|
| 901 | + * @param string $userId id of the user |
|
| 902 | + * @param string $app id of the app |
|
| 903 | + * @param string $key config key |
|
| 904 | + * @param int $value config value |
|
| 905 | + * @param bool $lazy set config as lazy loaded |
|
| 906 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 907 | + * |
|
| 908 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 909 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 910 | + * @since 31.0.0 |
|
| 911 | + * @see IUserConfig for explanation about lazy loading |
|
| 912 | + */ |
|
| 913 | + public function setValueInt( |
|
| 914 | + string $userId, |
|
| 915 | + string $app, |
|
| 916 | + string $key, |
|
| 917 | + int $value, |
|
| 918 | + bool $lazy = false, |
|
| 919 | + int $flags = 0, |
|
| 920 | + ): bool { |
|
| 921 | + if ($value > 2000000000) { |
|
| 922 | + $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + return $this->setTypedValue( |
|
| 926 | + $userId, |
|
| 927 | + $app, |
|
| 928 | + $key, |
|
| 929 | + (string)$value, |
|
| 930 | + $lazy, |
|
| 931 | + $flags, |
|
| 932 | + ValueType::INT |
|
| 933 | + ); |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + /** |
|
| 937 | + * @inheritDoc |
|
| 938 | + * |
|
| 939 | + * @param string $userId id of the user |
|
| 940 | + * @param string $app id of the app |
|
| 941 | + * @param string $key config key |
|
| 942 | + * @param float $value config value |
|
| 943 | + * @param bool $lazy set config as lazy loaded |
|
| 944 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 945 | + * |
|
| 946 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 947 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 948 | + * @since 31.0.0 |
|
| 949 | + * @see IUserConfig for explanation about lazy loading |
|
| 950 | + */ |
|
| 951 | + public function setValueFloat( |
|
| 952 | + string $userId, |
|
| 953 | + string $app, |
|
| 954 | + string $key, |
|
| 955 | + float $value, |
|
| 956 | + bool $lazy = false, |
|
| 957 | + int $flags = 0, |
|
| 958 | + ): bool { |
|
| 959 | + return $this->setTypedValue( |
|
| 960 | + $userId, |
|
| 961 | + $app, |
|
| 962 | + $key, |
|
| 963 | + (string)$value, |
|
| 964 | + $lazy, |
|
| 965 | + $flags, |
|
| 966 | + ValueType::FLOAT |
|
| 967 | + ); |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + /** |
|
| 971 | + * @inheritDoc |
|
| 972 | + * |
|
| 973 | + * @param string $userId id of the user |
|
| 974 | + * @param string $app id of the app |
|
| 975 | + * @param string $key config key |
|
| 976 | + * @param bool $value config value |
|
| 977 | + * @param bool $lazy set config as lazy loaded |
|
| 978 | + * |
|
| 979 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 980 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 981 | + * @since 31.0.0 |
|
| 982 | + * @see IUserConfig for explanation about lazy loading |
|
| 983 | + */ |
|
| 984 | + public function setValueBool( |
|
| 985 | + string $userId, |
|
| 986 | + string $app, |
|
| 987 | + string $key, |
|
| 988 | + bool $value, |
|
| 989 | + bool $lazy = false, |
|
| 990 | + int $flags = 0, |
|
| 991 | + ): bool { |
|
| 992 | + return $this->setTypedValue( |
|
| 993 | + $userId, |
|
| 994 | + $app, |
|
| 995 | + $key, |
|
| 996 | + ($value) ? '1' : '0', |
|
| 997 | + $lazy, |
|
| 998 | + $flags, |
|
| 999 | + ValueType::BOOL |
|
| 1000 | + ); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * @inheritDoc |
|
| 1005 | + * |
|
| 1006 | + * @param string $userId id of the user |
|
| 1007 | + * @param string $app id of the app |
|
| 1008 | + * @param string $key config key |
|
| 1009 | + * @param array $value config value |
|
| 1010 | + * @param bool $lazy set config as lazy loaded |
|
| 1011 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 1012 | + * |
|
| 1013 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 1014 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1015 | + * @throws JsonException |
|
| 1016 | + * @since 31.0.0 |
|
| 1017 | + * @see IUserConfig for explanation about lazy loading |
|
| 1018 | + */ |
|
| 1019 | + public function setValueArray( |
|
| 1020 | + string $userId, |
|
| 1021 | + string $app, |
|
| 1022 | + string $key, |
|
| 1023 | + array $value, |
|
| 1024 | + bool $lazy = false, |
|
| 1025 | + int $flags = 0, |
|
| 1026 | + ): bool { |
|
| 1027 | + try { |
|
| 1028 | + return $this->setTypedValue( |
|
| 1029 | + $userId, |
|
| 1030 | + $app, |
|
| 1031 | + $key, |
|
| 1032 | + json_encode($value, JSON_THROW_ON_ERROR), |
|
| 1033 | + $lazy, |
|
| 1034 | + $flags, |
|
| 1035 | + ValueType::ARRAY |
|
| 1036 | + ); |
|
| 1037 | + } catch (JsonException $e) { |
|
| 1038 | + $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 1039 | + throw $e; |
|
| 1040 | + } |
|
| 1041 | + } |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * Store a config key and its value in database |
|
| 1045 | + * |
|
| 1046 | + * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 1047 | + * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 1048 | + * altered. |
|
| 1049 | + * |
|
| 1050 | + * @param string $userId id of the user |
|
| 1051 | + * @param string $app id of the app |
|
| 1052 | + * @param string $key config key |
|
| 1053 | + * @param string $value config value |
|
| 1054 | + * @param bool $lazy config set as lazy loaded |
|
| 1055 | + * @param ValueType $type value type |
|
| 1056 | + * |
|
| 1057 | + * @return bool TRUE if value was updated in database |
|
| 1058 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1059 | + * @see IUserConfig for explanation about lazy loading |
|
| 1060 | + */ |
|
| 1061 | + private function setTypedValue( |
|
| 1062 | + string $userId, |
|
| 1063 | + string $app, |
|
| 1064 | + string $key, |
|
| 1065 | + string $value, |
|
| 1066 | + bool $lazy, |
|
| 1067 | + int $flags, |
|
| 1068 | + ValueType $type, |
|
| 1069 | + ): bool { |
|
| 1070 | + // Primary email addresses are always(!) expected to be lowercase |
|
| 1071 | + if ($app === 'settings' && $key === 'email') { |
|
| 1072 | + $value = strtolower($value); |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + $this->assertParams($userId, $app, $key); |
|
| 1076 | + if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) { |
|
| 1077 | + // returns false as database is not updated |
|
| 1078 | + return false; |
|
| 1079 | + } |
|
| 1080 | + $this->loadConfig($userId, $lazy); |
|
| 1081 | + |
|
| 1082 | + $inserted = $refreshCache = false; |
|
| 1083 | + $origValue = $value; |
|
| 1084 | + $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); |
|
| 1085 | + if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1086 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1087 | + $flags |= self::FLAG_SENSITIVE; |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + // if requested, we fill the 'indexed' field with current value |
|
| 1091 | + $indexed = ''; |
|
| 1092 | + if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { |
|
| 1093 | + if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { |
|
| 1094 | + $this->logger->warning('sensitive value are not to be indexed'); |
|
| 1095 | + } elseif (strlen($value) > self::USER_MAX_LENGTH) { |
|
| 1096 | + $this->logger->warning('value is too lengthy to be indexed'); |
|
| 1097 | + } else { |
|
| 1098 | + $indexed = $value; |
|
| 1099 | + } |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + if ($this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1103 | + /** |
|
| 1104 | + * no update if key is already known with set lazy status and value is |
|
| 1105 | + * not different, unless sensitivity is switched from false to true. |
|
| 1106 | + */ |
|
| 1107 | + if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type) |
|
| 1108 | + && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1109 | + return false; |
|
| 1110 | + } |
|
| 1111 | + } else { |
|
| 1112 | + /** |
|
| 1113 | + * if key is not known yet, we try to insert. |
|
| 1114 | + * It might fail if the key exists with a different lazy flag. |
|
| 1115 | + */ |
|
| 1116 | + try { |
|
| 1117 | + $insert = $this->connection->getQueryBuilder(); |
|
| 1118 | + $insert->insert('preferences') |
|
| 1119 | + ->setValue('userid', $insert->createNamedParameter($userId)) |
|
| 1120 | + ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 1121 | + ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1122 | + ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1123 | + ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1124 | + ->setValue('indexed', $insert->createNamedParameter($indexed)) |
|
| 1125 | + ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 1126 | + ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 1127 | + $insert->executeStatement(); |
|
| 1128 | + $inserted = true; |
|
| 1129 | + } catch (DBException $e) { |
|
| 1130 | + if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 1131 | + // TODO: throw exception or just log and returns false !? |
|
| 1132 | + throw $e; |
|
| 1133 | + } |
|
| 1134 | + } |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + /** |
|
| 1138 | + * We cannot insert a new row, meaning we need to update an already existing one |
|
| 1139 | + */ |
|
| 1140 | + if (!$inserted) { |
|
| 1141 | + $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 1142 | + if ($currType === null) { // this might happen when switching lazy loading status |
|
| 1143 | + $this->loadConfigAll($userId); |
|
| 1144 | + $currType = $this->valueDetails[$userId][$app][$key]['type']; |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + /** |
|
| 1148 | + * We only log a warning and set it to VALUE_MIXED. |
|
| 1149 | + */ |
|
| 1150 | + if ($currType === null) { |
|
| 1151 | + $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]); |
|
| 1152 | + $currType = ValueType::MIXED; |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + /** |
|
| 1156 | + * we only accept a different type from the one stored in database |
|
| 1157 | + * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 1158 | + */ |
|
| 1159 | + if ($currType !== ValueType::MIXED && |
|
| 1160 | + $currType !== $type) { |
|
| 1161 | + try { |
|
| 1162 | + $currTypeDef = $currType->getDefinition(); |
|
| 1163 | + $typeDef = $type->getDefinition(); |
|
| 1164 | + } catch (IncorrectTypeException) { |
|
| 1165 | + $currTypeDef = $currType->value; |
|
| 1166 | + $typeDef = $type->value; |
|
| 1167 | + } |
|
| 1168 | + throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')'); |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + if ($lazy !== $this->isLazy($userId, $app, $key)) { |
|
| 1172 | + $refreshCache = true; |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + $update = $this->connection->getQueryBuilder(); |
|
| 1176 | + $update->update('preferences') |
|
| 1177 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1178 | + ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1179 | + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1180 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1181 | + ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1182 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1183 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1184 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1185 | + |
|
| 1186 | + $update->executeStatement(); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + if ($refreshCache) { |
|
| 1190 | + $this->clearCache($userId); |
|
| 1191 | + return true; |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + // update local cache |
|
| 1195 | + if ($lazy) { |
|
| 1196 | + $this->lazyCache[$userId][$app][$key] = $value; |
|
| 1197 | + } else { |
|
| 1198 | + $this->fastCache[$userId][$app][$key] = $value; |
|
| 1199 | + } |
|
| 1200 | + $this->valueDetails[$userId][$app][$key] = [ |
|
| 1201 | + 'type' => $type, |
|
| 1202 | + 'flags' => $flags |
|
| 1203 | + ]; |
|
| 1204 | + |
|
| 1205 | + return true; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + /** |
|
| 1209 | + * Change the type of config value. |
|
| 1210 | + * |
|
| 1211 | + * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 1212 | + * |
|
| 1213 | + * @param string $userId id of the user |
|
| 1214 | + * @param string $app id of the app |
|
| 1215 | + * @param string $key config key |
|
| 1216 | + * @param ValueType $type value type |
|
| 1217 | + * |
|
| 1218 | + * @return bool TRUE if database update were necessary |
|
| 1219 | + * @throws UnknownKeyException if $key is now known in database |
|
| 1220 | + * @throws IncorrectTypeException if $type is not valid |
|
| 1221 | + * @internal |
|
| 1222 | + * @since 31.0.0 |
|
| 1223 | + */ |
|
| 1224 | + public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { |
|
| 1225 | + $this->assertParams($userId, $app, $key); |
|
| 1226 | + $this->loadConfigAll($userId); |
|
| 1227 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1228 | + $this->isLazy($userId, $app, $key); // confirm key exists |
|
| 1229 | + |
|
| 1230 | + $update = $this->connection->getQueryBuilder(); |
|
| 1231 | + $update->update('preferences') |
|
| 1232 | + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1233 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1234 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1235 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1236 | + $update->executeStatement(); |
|
| 1237 | + |
|
| 1238 | + $this->valueDetails[$userId][$app][$key]['type'] = $type; |
|
| 1239 | + |
|
| 1240 | + return true; |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * @inheritDoc |
|
| 1245 | + * |
|
| 1246 | + * @param string $userId id of the user |
|
| 1247 | + * @param string $app id of the app |
|
| 1248 | + * @param string $key config key |
|
| 1249 | + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 1250 | + * |
|
| 1251 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1252 | + * @since 31.0.0 |
|
| 1253 | + */ |
|
| 1254 | + public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { |
|
| 1255 | + $this->assertParams($userId, $app, $key); |
|
| 1256 | + $this->loadConfigAll($userId); |
|
| 1257 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1258 | + |
|
| 1259 | + try { |
|
| 1260 | + if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { |
|
| 1261 | + return false; |
|
| 1262 | + } |
|
| 1263 | + } catch (UnknownKeyException) { |
|
| 1264 | + return false; |
|
| 1265 | + } |
|
| 1266 | + |
|
| 1267 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1268 | + if ($lazy) { |
|
| 1269 | + $cache = $this->lazyCache; |
|
| 1270 | + } else { |
|
| 1271 | + $cache = $this->fastCache; |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + if (!isset($cache[$userId][$app][$key])) { |
|
| 1275 | + throw new UnknownKeyException('unknown config key'); |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + $value = $cache[$userId][$app][$key]; |
|
| 1279 | + $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1280 | + if ($sensitive) { |
|
| 1281 | + $flags |= self::FLAG_SENSITIVE; |
|
| 1282 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1283 | + } else { |
|
| 1284 | + $flags &= ~self::FLAG_SENSITIVE; |
|
| 1285 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + $update = $this->connection->getQueryBuilder(); |
|
| 1289 | + $update->update('preferences') |
|
| 1290 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1291 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1292 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1293 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1294 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1295 | + $update->executeStatement(); |
|
| 1296 | + |
|
| 1297 | + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1298 | + |
|
| 1299 | + return true; |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + /** |
|
| 1303 | + * @inheritDoc |
|
| 1304 | + * |
|
| 1305 | + * @param string $app |
|
| 1306 | + * @param string $key |
|
| 1307 | + * @param bool $sensitive |
|
| 1308 | + * |
|
| 1309 | + * @since 31.0.0 |
|
| 1310 | + */ |
|
| 1311 | + public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { |
|
| 1312 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1313 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1314 | + |
|
| 1315 | + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1316 | + try { |
|
| 1317 | + $this->updateSensitive($userId, $app, $key, $sensitive); |
|
| 1318 | + } catch (UnknownKeyException) { |
|
| 1319 | + // should not happen and can be ignored |
|
| 1320 | + } |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + // we clear all cache |
|
| 1324 | + $this->clearCacheAll(); |
|
| 1325 | + } |
|
| 1326 | + |
|
| 1327 | + /** |
|
| 1328 | + * @inheritDoc |
|
| 1329 | + * |
|
| 1330 | + * @param string $userId |
|
| 1331 | + * @param string $app |
|
| 1332 | + * @param string $key |
|
| 1333 | + * @param bool $indexed |
|
| 1334 | + * |
|
| 1335 | + * @return bool |
|
| 1336 | + * @throws DBException |
|
| 1337 | + * @throws IncorrectTypeException |
|
| 1338 | + * @throws UnknownKeyException |
|
| 1339 | + * @since 31.0.0 |
|
| 1340 | + */ |
|
| 1341 | + public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { |
|
| 1342 | + $this->assertParams($userId, $app, $key); |
|
| 1343 | + $this->loadConfigAll($userId); |
|
| 1344 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1345 | + |
|
| 1346 | + try { |
|
| 1347 | + if ($indexed === $this->isIndexed($userId, $app, $key, null)) { |
|
| 1348 | + return false; |
|
| 1349 | + } |
|
| 1350 | + } catch (UnknownKeyException) { |
|
| 1351 | + return false; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1355 | + if ($lazy) { |
|
| 1356 | + $cache = $this->lazyCache; |
|
| 1357 | + } else { |
|
| 1358 | + $cache = $this->fastCache; |
|
| 1359 | + } |
|
| 1360 | + |
|
| 1361 | + if (!isset($cache[$userId][$app][$key])) { |
|
| 1362 | + throw new UnknownKeyException('unknown config key'); |
|
| 1363 | + } |
|
| 1364 | + |
|
| 1365 | + $value = $cache[$userId][$app][$key]; |
|
| 1366 | + $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1367 | + if ($indexed) { |
|
| 1368 | + $indexed = $value; |
|
| 1369 | + } else { |
|
| 1370 | + $flags &= ~self::FLAG_INDEXED; |
|
| 1371 | + $indexed = ''; |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + $update = $this->connection->getQueryBuilder(); |
|
| 1375 | + $update->update('preferences') |
|
| 1376 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1377 | + ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1378 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1379 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1380 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1381 | + $update->executeStatement(); |
|
| 1382 | + |
|
| 1383 | + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1384 | + |
|
| 1385 | + return true; |
|
| 1386 | + } |
|
| 1387 | + |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * @inheritDoc |
|
| 1391 | + * |
|
| 1392 | + * @param string $app |
|
| 1393 | + * @param string $key |
|
| 1394 | + * @param bool $indexed |
|
| 1395 | + * |
|
| 1396 | + * @since 31.0.0 |
|
| 1397 | + */ |
|
| 1398 | + public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { |
|
| 1399 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1400 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1401 | + |
|
| 1402 | + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1403 | + try { |
|
| 1404 | + $this->updateIndexed($userId, $app, $key, $indexed); |
|
| 1405 | + } catch (UnknownKeyException) { |
|
| 1406 | + // should not happen and can be ignored |
|
| 1407 | + } |
|
| 1408 | + } |
|
| 1409 | + |
|
| 1410 | + // we clear all cache |
|
| 1411 | + $this->clearCacheAll(); |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + /** |
|
| 1415 | + * @inheritDoc |
|
| 1416 | + * |
|
| 1417 | + * @param string $userId id of the user |
|
| 1418 | + * @param string $app id of the app |
|
| 1419 | + * @param string $key config key |
|
| 1420 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1421 | + * |
|
| 1422 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1423 | + * @since 31.0.0 |
|
| 1424 | + */ |
|
| 1425 | + public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { |
|
| 1426 | + $this->assertParams($userId, $app, $key); |
|
| 1427 | + $this->loadConfigAll($userId); |
|
| 1428 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1429 | + |
|
| 1430 | + try { |
|
| 1431 | + if ($lazy === $this->isLazy($userId, $app, $key)) { |
|
| 1432 | + return false; |
|
| 1433 | + } |
|
| 1434 | + } catch (UnknownKeyException) { |
|
| 1435 | + return false; |
|
| 1436 | + } |
|
| 1437 | + |
|
| 1438 | + $update = $this->connection->getQueryBuilder(); |
|
| 1439 | + $update->update('preferences') |
|
| 1440 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1441 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1442 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1443 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1444 | + $update->executeStatement(); |
|
| 1445 | + |
|
| 1446 | + // At this point, it is a lot safer to clean cache |
|
| 1447 | + $this->clearCache($userId); |
|
| 1448 | + |
|
| 1449 | + return true; |
|
| 1450 | + } |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * @inheritDoc |
|
| 1454 | + * |
|
| 1455 | + * @param string $app id of the app |
|
| 1456 | + * @param string $key config key |
|
| 1457 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1458 | + * |
|
| 1459 | + * @since 31.0.0 |
|
| 1460 | + */ |
|
| 1461 | + public function updateGlobalLazy(string $app, string $key, bool $lazy): void { |
|
| 1462 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1463 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1464 | + |
|
| 1465 | + $update = $this->connection->getQueryBuilder(); |
|
| 1466 | + $update->update('preferences') |
|
| 1467 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1468 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1469 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1470 | + $update->executeStatement(); |
|
| 1471 | + |
|
| 1472 | + $this->clearCacheAll(); |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * @inheritDoc |
|
| 1477 | + * |
|
| 1478 | + * @param string $userId id of the user |
|
| 1479 | + * @param string $app id of the app |
|
| 1480 | + * @param string $key config key |
|
| 1481 | + * |
|
| 1482 | + * @return array |
|
| 1483 | + * @throws UnknownKeyException if config key is not known in database |
|
| 1484 | + * @since 31.0.0 |
|
| 1485 | + */ |
|
| 1486 | + public function getDetails(string $userId, string $app, string $key): array { |
|
| 1487 | + $this->assertParams($userId, $app, $key); |
|
| 1488 | + $this->loadConfigAll($userId); |
|
| 1489 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1490 | + |
|
| 1491 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1492 | + |
|
| 1493 | + if ($lazy) { |
|
| 1494 | + $cache = $this->lazyCache[$userId]; |
|
| 1495 | + } else { |
|
| 1496 | + $cache = $this->fastCache[$userId]; |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + $type = $this->getValueType($userId, $app, $key); |
|
| 1500 | + try { |
|
| 1501 | + $typeString = $type->getDefinition(); |
|
| 1502 | + } catch (IncorrectTypeException $e) { |
|
| 1503 | + $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1504 | + $typeString = (string)$type->value; |
|
| 1505 | + } |
|
| 1506 | + |
|
| 1507 | + if (!isset($cache[$app][$key])) { |
|
| 1508 | + throw new UnknownKeyException('unknown config key'); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + $value = $cache[$app][$key]; |
|
| 1512 | + $sensitive = $this->isSensitive($userId, $app, $key, null); |
|
| 1513 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1514 | + |
|
| 1515 | + return [ |
|
| 1516 | + 'userId' => $userId, |
|
| 1517 | + 'app' => $app, |
|
| 1518 | + 'key' => $key, |
|
| 1519 | + 'value' => $value, |
|
| 1520 | + 'type' => $type->value, |
|
| 1521 | + 'lazy' => $lazy, |
|
| 1522 | + 'typeString' => $typeString, |
|
| 1523 | + 'sensitive' => $sensitive |
|
| 1524 | + ]; |
|
| 1525 | + } |
|
| 1526 | + |
|
| 1527 | + /** |
|
| 1528 | + * @inheritDoc |
|
| 1529 | + * |
|
| 1530 | + * @param string $userId id of the user |
|
| 1531 | + * @param string $app id of the app |
|
| 1532 | + * @param string $key config key |
|
| 1533 | + * |
|
| 1534 | + * @since 31.0.0 |
|
| 1535 | + */ |
|
| 1536 | + public function deleteUserConfig(string $userId, string $app, string $key): void { |
|
| 1537 | + $this->assertParams($userId, $app, $key); |
|
| 1538 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1539 | + |
|
| 1540 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1541 | + $qb->delete('preferences') |
|
| 1542 | + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) |
|
| 1543 | + ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1544 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1545 | + $qb->executeStatement(); |
|
| 1546 | + |
|
| 1547 | + unset($this->lazyCache[$userId][$app][$key]); |
|
| 1548 | + unset($this->fastCache[$userId][$app][$key]); |
|
| 1549 | + unset($this->valueDetails[$userId][$app][$key]); |
|
| 1550 | + } |
|
| 1551 | + |
|
| 1552 | + /** |
|
| 1553 | + * @inheritDoc |
|
| 1554 | + * |
|
| 1555 | + * @param string $app id of the app |
|
| 1556 | + * @param string $key config key |
|
| 1557 | + * |
|
| 1558 | + * @since 31.0.0 |
|
| 1559 | + */ |
|
| 1560 | + public function deleteKey(string $app, string $key): void { |
|
| 1561 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1562 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1563 | + |
|
| 1564 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1565 | + $qb->delete('preferences') |
|
| 1566 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1567 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1568 | + $qb->executeStatement(); |
|
| 1569 | + |
|
| 1570 | + $this->clearCacheAll(); |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + /** |
|
| 1574 | + * @inheritDoc |
|
| 1575 | + * |
|
| 1576 | + * @param string $app id of the app |
|
| 1577 | + * |
|
| 1578 | + * @since 31.0.0 |
|
| 1579 | + */ |
|
| 1580 | + public function deleteApp(string $app): void { |
|
| 1581 | + $this->assertParams('', $app, allowEmptyUser: true); |
|
| 1582 | + |
|
| 1583 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1584 | + $qb->delete('preferences') |
|
| 1585 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1586 | + $qb->executeStatement(); |
|
| 1587 | + |
|
| 1588 | + $this->clearCacheAll(); |
|
| 1589 | + } |
|
| 1590 | + |
|
| 1591 | + public function deleteAllUserConfig(string $userId): void { |
|
| 1592 | + $this->assertParams($userId, '', allowEmptyApp: true); |
|
| 1593 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1594 | + $qb->delete('preferences') |
|
| 1595 | + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1596 | + $qb->executeStatement(); |
|
| 1597 | + |
|
| 1598 | + $this->clearCache($userId); |
|
| 1599 | + } |
|
| 1600 | + |
|
| 1601 | + /** |
|
| 1602 | + * @inheritDoc |
|
| 1603 | + * |
|
| 1604 | + * @param string $userId id of the user |
|
| 1605 | + * @param bool $reload set to TRUE to refill cache instantly after clearing it. |
|
| 1606 | + * |
|
| 1607 | + * @since 31.0.0 |
|
| 1608 | + */ |
|
| 1609 | + public function clearCache(string $userId, bool $reload = false): void { |
|
| 1610 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 1611 | + $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; |
|
| 1612 | + $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = []; |
|
| 1613 | + |
|
| 1614 | + if (!$reload) { |
|
| 1615 | + return; |
|
| 1616 | + } |
|
| 1617 | + |
|
| 1618 | + $this->loadConfigAll($userId); |
|
| 1619 | + } |
|
| 1620 | + |
|
| 1621 | + /** |
|
| 1622 | + * @inheritDoc |
|
| 1623 | + * |
|
| 1624 | + * @since 31.0.0 |
|
| 1625 | + */ |
|
| 1626 | + public function clearCacheAll(): void { |
|
| 1627 | + $this->lazyLoaded = $this->fastLoaded = []; |
|
| 1628 | + $this->lazyCache = $this->fastCache = $this->valueDetails = []; |
|
| 1629 | + } |
|
| 1630 | + |
|
| 1631 | + /** |
|
| 1632 | + * For debug purpose. |
|
| 1633 | + * Returns the cached data. |
|
| 1634 | + * |
|
| 1635 | + * @return array |
|
| 1636 | + * @since 31.0.0 |
|
| 1637 | + * @internal |
|
| 1638 | + */ |
|
| 1639 | + public function statusCache(): array { |
|
| 1640 | + return [ |
|
| 1641 | + 'fastLoaded' => $this->fastLoaded, |
|
| 1642 | + 'fastCache' => $this->fastCache, |
|
| 1643 | + 'lazyLoaded' => $this->lazyLoaded, |
|
| 1644 | + 'lazyCache' => $this->lazyCache, |
|
| 1645 | + 'valueDetails' => $this->valueDetails, |
|
| 1646 | + ]; |
|
| 1647 | + } |
|
| 1648 | + |
|
| 1649 | + /** |
|
| 1650 | + * @param int $needle bitflag to search |
|
| 1651 | + * @param int $flags all flags |
|
| 1652 | + * |
|
| 1653 | + * @return bool TRUE if bitflag $needle is set in $flags |
|
| 1654 | + */ |
|
| 1655 | + private function isFlagged(int $needle, int $flags): bool { |
|
| 1656 | + return (($needle & $flags) !== 0); |
|
| 1657 | + } |
|
| 1658 | + |
|
| 1659 | + /** |
|
| 1660 | + * Confirm the string set for app and key fit the database description |
|
| 1661 | + * |
|
| 1662 | + * @param string $userId |
|
| 1663 | + * @param string $app assert $app fit in database |
|
| 1664 | + * @param string $prefKey assert config key fit in database |
|
| 1665 | + * @param bool $allowEmptyUser |
|
| 1666 | + * @param bool $allowEmptyApp $app can be empty string |
|
| 1667 | + * @param ValueType|null $valueType assert value type is only one type |
|
| 1668 | + */ |
|
| 1669 | + private function assertParams( |
|
| 1670 | + string $userId = '', |
|
| 1671 | + string $app = '', |
|
| 1672 | + string $prefKey = '', |
|
| 1673 | + bool $allowEmptyUser = false, |
|
| 1674 | + bool $allowEmptyApp = false, |
|
| 1675 | + ): void { |
|
| 1676 | + if (!$allowEmptyUser && $userId === '') { |
|
| 1677 | + throw new InvalidArgumentException('userId cannot be an empty string'); |
|
| 1678 | + } |
|
| 1679 | + if (!$allowEmptyApp && $app === '') { |
|
| 1680 | + throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1681 | + } |
|
| 1682 | + if (strlen($userId) > self::USER_MAX_LENGTH) { |
|
| 1683 | + throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')'); |
|
| 1684 | + } |
|
| 1685 | + if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1686 | + throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')'); |
|
| 1687 | + } |
|
| 1688 | + if (strlen($prefKey) > self::KEY_MAX_LENGTH) { |
|
| 1689 | + throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1690 | + } |
|
| 1691 | + } |
|
| 1692 | + |
|
| 1693 | + private function loadConfigAll(string $userId): void { |
|
| 1694 | + $this->loadConfig($userId, null); |
|
| 1695 | + } |
|
| 1696 | + |
|
| 1697 | + /** |
|
| 1698 | + * Load normal config or config set as lazy loaded |
|
| 1699 | + * |
|
| 1700 | + * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1701 | + */ |
|
| 1702 | + private function loadConfig(string $userId, ?bool $lazy = false): void { |
|
| 1703 | + if ($this->isLoaded($userId, $lazy)) { |
|
| 1704 | + return; |
|
| 1705 | + } |
|
| 1706 | + |
|
| 1707 | + if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log |
|
| 1708 | + $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); |
|
| 1709 | + } |
|
| 1710 | + |
|
| 1711 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1712 | + $qb->from('preferences'); |
|
| 1713 | + $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); |
|
| 1714 | + $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1715 | + |
|
| 1716 | + // we only need value from lazy when loadConfig does not specify it |
|
| 1717 | + if ($lazy !== null) { |
|
| 1718 | + $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1719 | + } else { |
|
| 1720 | + $qb->addSelect('lazy'); |
|
| 1721 | + } |
|
| 1722 | + |
|
| 1723 | + $result = $qb->executeQuery(); |
|
| 1724 | + $rows = $result->fetchAll(); |
|
| 1725 | + foreach ($rows as $row) { |
|
| 1726 | + if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1727 | + $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1728 | + } else { |
|
| 1729 | + $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1730 | + } |
|
| 1731 | + $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']]; |
|
| 1732 | + } |
|
| 1733 | + $result->closeCursor(); |
|
| 1734 | + $this->setAsLoaded($userId, $lazy); |
|
| 1735 | + } |
|
| 1736 | + |
|
| 1737 | + /** |
|
| 1738 | + * if $lazy is: |
|
| 1739 | + * - false: will returns true if fast config are loaded |
|
| 1740 | + * - true : will returns true if lazy config are loaded |
|
| 1741 | + * - null : will returns true if both config are loaded |
|
| 1742 | + * |
|
| 1743 | + * @param string $userId |
|
| 1744 | + * @param bool $lazy |
|
| 1745 | + * |
|
| 1746 | + * @return bool |
|
| 1747 | + */ |
|
| 1748 | + private function isLoaded(string $userId, ?bool $lazy): bool { |
|
| 1749 | + if ($lazy === null) { |
|
| 1750 | + return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false); |
|
| 1751 | + } |
|
| 1752 | + |
|
| 1753 | + return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false; |
|
| 1754 | + } |
|
| 1755 | + |
|
| 1756 | + /** |
|
| 1757 | + * if $lazy is: |
|
| 1758 | + * - false: set fast config as loaded |
|
| 1759 | + * - true : set lazy config as loaded |
|
| 1760 | + * - null : set both config as loaded |
|
| 1761 | + * |
|
| 1762 | + * @param string $userId |
|
| 1763 | + * @param bool $lazy |
|
| 1764 | + */ |
|
| 1765 | + private function setAsLoaded(string $userId, ?bool $lazy): void { |
|
| 1766 | + if ($lazy === null) { |
|
| 1767 | + $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true; |
|
| 1768 | + return; |
|
| 1769 | + } |
|
| 1770 | + |
|
| 1771 | + // We also create empty entry to keep both fastLoaded/lazyLoaded synced |
|
| 1772 | + if ($lazy) { |
|
| 1773 | + $this->lazyLoaded[$userId] = true; |
|
| 1774 | + $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false; |
|
| 1775 | + $this->fastCache[$userId] = $this->fastCache[$userId] ?? []; |
|
| 1776 | + } else { |
|
| 1777 | + $this->fastLoaded[$userId] = true; |
|
| 1778 | + $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false; |
|
| 1779 | + $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? []; |
|
| 1780 | + } |
|
| 1781 | + } |
|
| 1782 | + |
|
| 1783 | + /** |
|
| 1784 | + * **Warning:** this will load all lazy values from the database |
|
| 1785 | + * |
|
| 1786 | + * @param string $userId id of the user |
|
| 1787 | + * @param string $app id of the app |
|
| 1788 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 1789 | + * |
|
| 1790 | + * @return array<string, string|int|float|bool|array> |
|
| 1791 | + */ |
|
| 1792 | + private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array { |
|
| 1793 | + foreach ($values as $key => $value) { |
|
| 1794 | + //$key = (string)$key; |
|
| 1795 | + try { |
|
| 1796 | + $type = $this->getValueType($userId, $app, (string)$key); |
|
| 1797 | + } catch (UnknownKeyException) { |
|
| 1798 | + continue; |
|
| 1799 | + } |
|
| 1800 | + |
|
| 1801 | + if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1802 | + if ($filtered) { |
|
| 1803 | + $value = IConfig::SENSITIVE_VALUE; |
|
| 1804 | + $type = ValueType::STRING; |
|
| 1805 | + } else { |
|
| 1806 | + $this->decryptSensitiveValue($userId, $app, (string)$key, $value); |
|
| 1807 | + } |
|
| 1808 | + } |
|
| 1809 | + |
|
| 1810 | + $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1811 | + } |
|
| 1812 | + |
|
| 1813 | + return $values; |
|
| 1814 | + } |
|
| 1815 | + |
|
| 1816 | + /** |
|
| 1817 | + * convert string value to the expected type |
|
| 1818 | + * |
|
| 1819 | + * @param string $value |
|
| 1820 | + * @param ValueType $type |
|
| 1821 | + * |
|
| 1822 | + * @return string|int|float|bool|array |
|
| 1823 | + */ |
|
| 1824 | + private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array { |
|
| 1825 | + switch ($type) { |
|
| 1826 | + case ValueType::INT: |
|
| 1827 | + return (int)$value; |
|
| 1828 | + case ValueType::FLOAT: |
|
| 1829 | + return (float)$value; |
|
| 1830 | + case ValueType::BOOL: |
|
| 1831 | + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1832 | + case ValueType::ARRAY: |
|
| 1833 | + try { |
|
| 1834 | + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1835 | + } catch (JsonException) { |
|
| 1836 | + // ignoreable |
|
| 1837 | + } |
|
| 1838 | + break; |
|
| 1839 | + } |
|
| 1840 | + return $value; |
|
| 1841 | + } |
|
| 1842 | + |
|
| 1843 | + |
|
| 1844 | + /** |
|
| 1845 | + * will change referenced $value with the decrypted value in case of encrypted (sensitive value) |
|
| 1846 | + * |
|
| 1847 | + * @param string $userId |
|
| 1848 | + * @param string $app |
|
| 1849 | + * @param string $key |
|
| 1850 | + * @param string $value |
|
| 1851 | + */ |
|
| 1852 | + private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { |
|
| 1853 | + if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1854 | + return; |
|
| 1855 | + } |
|
| 1856 | + |
|
| 1857 | + if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1858 | + return; |
|
| 1859 | + } |
|
| 1860 | + |
|
| 1861 | + try { |
|
| 1862 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1863 | + } catch (\Exception $e) { |
|
| 1864 | + $this->logger->warning('could not decrypt sensitive value', [ |
|
| 1865 | + 'userId' => $userId, |
|
| 1866 | + 'app' => $app, |
|
| 1867 | + 'key' => $key, |
|
| 1868 | + 'value' => $value, |
|
| 1869 | + 'exception' => $e |
|
| 1870 | + ]); |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + /** |
|
| 1875 | + * Match and apply current use of config values with defined lexicon. |
|
| 1876 | + * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1877 | + * |
|
| 1878 | + * @throws UnknownKeyException |
|
| 1879 | + * @throws TypeConflictException |
|
| 1880 | + * @return bool FALSE if conflict with defined lexicon were observed in the process |
|
| 1881 | + */ |
|
| 1882 | + private function matchAndApplyLexiconDefinition( |
|
| 1883 | + string $userId, |
|
| 1884 | + string $app, |
|
| 1885 | + string &$key, |
|
| 1886 | + ?bool &$lazy = null, |
|
| 1887 | + ValueType &$type = ValueType::MIXED, |
|
| 1888 | + int &$flags = 0, |
|
| 1889 | + string &$default = '', |
|
| 1890 | + ): bool { |
|
| 1891 | + $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1892 | + if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1893 | + // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1894 | + $key = $configDetails['aliases'][$key]; |
|
| 1895 | + } |
|
| 1896 | + |
|
| 1897 | + if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1898 | + return $this->applyLexiconStrictness($configDetails['strictness'], 'The user config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1899 | + } |
|
| 1900 | + |
|
| 1901 | + // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1902 | + if ($lazy === null) { |
|
| 1903 | + return true; |
|
| 1904 | + } |
|
| 1905 | + |
|
| 1906 | + /** @var ConfigLexiconEntry $configValue */ |
|
| 1907 | + $configValue = $configDetails['entries'][$key]; |
|
| 1908 | + if ($type === ValueType::MIXED) { |
|
| 1909 | + // we overwrite if value was requested as mixed |
|
| 1910 | + $type = $configValue->getValueType(); |
|
| 1911 | + } elseif ($configValue->getValueType() !== $type) { |
|
| 1912 | + throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1913 | + } |
|
| 1914 | + |
|
| 1915 | + $lazy = $configValue->isLazy(); |
|
| 1916 | + $flags = $configValue->getFlags(); |
|
| 1917 | + if ($configValue->isDeprecated()) { |
|
| 1918 | + $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1919 | + } |
|
| 1920 | + |
|
| 1921 | + $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; |
|
| 1922 | + if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1923 | + // if key exists there should be no need to extract default |
|
| 1924 | + return true; |
|
| 1925 | + } |
|
| 1926 | + |
|
| 1927 | + // default from Lexicon got priority but it can still be overwritten by admin |
|
| 1928 | + $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault() ?? $default; |
|
| 1929 | + |
|
| 1930 | + // returning false will make get() returning $default and set() not changing value in database |
|
| 1931 | + return !$enforcedValue; |
|
| 1932 | + } |
|
| 1933 | + |
|
| 1934 | + /** |
|
| 1935 | + * get default value set in config/config.php if stored in key: |
|
| 1936 | + * |
|
| 1937 | + * 'lexicon.default.userconfig' => [ |
|
| 1938 | + * <appId> => [ |
|
| 1939 | + * <configKey> => 'my value', |
|
| 1940 | + * ] |
|
| 1941 | + * ], |
|
| 1942 | + * |
|
| 1943 | + * The entry is converted to string to fit the expected type when managing default value |
|
| 1944 | + */ |
|
| 1945 | + private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string { |
|
| 1946 | + $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null; |
|
| 1947 | + if ($default === null) { |
|
| 1948 | + // no system default, using default default. |
|
| 1949 | + return null; |
|
| 1950 | + } |
|
| 1951 | + |
|
| 1952 | + return $configValue->convertToString($default); |
|
| 1953 | + } |
|
| 1954 | + |
|
| 1955 | + /** |
|
| 1956 | + * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1957 | + * |
|
| 1958 | + * @see IConfigLexicon::getStrictness() |
|
| 1959 | + * @param ConfigLexiconStrictness|null $strictness |
|
| 1960 | + * @param string $line |
|
| 1961 | + * |
|
| 1962 | + * @return bool TRUE if conflict can be fully ignored |
|
| 1963 | + * @throws UnknownKeyException |
|
| 1964 | + */ |
|
| 1965 | + private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, string $line = ''): bool { |
|
| 1966 | + if ($strictness === null) { |
|
| 1967 | + return true; |
|
| 1968 | + } |
|
| 1969 | + |
|
| 1970 | + switch ($strictness) { |
|
| 1971 | + case ConfigLexiconStrictness::IGNORE: |
|
| 1972 | + return true; |
|
| 1973 | + case ConfigLexiconStrictness::NOTICE: |
|
| 1974 | + $this->logger->notice($line); |
|
| 1975 | + return true; |
|
| 1976 | + case ConfigLexiconStrictness::WARNING: |
|
| 1977 | + $this->logger->warning($line); |
|
| 1978 | + return false; |
|
| 1979 | + case ConfigLexiconStrictness::EXCEPTION: |
|
| 1980 | + throw new UnknownKeyException($line); |
|
| 1981 | + } |
|
| 1982 | + |
|
| 1983 | + throw new UnknownKeyException($line); |
|
| 1984 | + } |
|
| 1985 | + |
|
| 1986 | + /** |
|
| 1987 | + * extract details from registered $appId's config lexicon |
|
| 1988 | + * |
|
| 1989 | + * @param string $appId |
|
| 1990 | + * @internal |
|
| 1991 | + * |
|
| 1992 | + * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1993 | + */ |
|
| 1994 | + public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1995 | + if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1996 | + $entries = $aliases = []; |
|
| 1997 | + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1998 | + $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1999 | + foreach ($configLexicon?->getUserConfigs() ?? [] as $configEntry) { |
|
| 2000 | + $entries[$configEntry->getKey()] = $configEntry; |
|
| 2001 | + if ($configEntry->getRename() !== null) { |
|
| 2002 | + $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 2003 | + } |
|
| 2004 | + } |
|
| 2005 | + |
|
| 2006 | + $this->configLexiconDetails[$appId] = [ |
|
| 2007 | + 'entries' => $entries, |
|
| 2008 | + 'aliases' => $aliases, |
|
| 2009 | + 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 2010 | + ]; |
|
| 2011 | + } |
|
| 2012 | + |
|
| 2013 | + return $this->configLexiconDetails[$appId]; |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 2017 | + return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 2018 | + } |
|
| 2019 | + |
|
| 2020 | + /** |
|
| 2021 | + * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 2022 | + * |
|
| 2023 | + * @internal |
|
| 2024 | + */ |
|
| 2025 | + public function ignoreLexiconAliases(bool $ignore): void { |
|
| 2026 | + $this->ignoreLexiconAliases = $ignore; |
|
| 2027 | + } |
|
| 2028 | 2028 | } |
@@ -34,918 +34,918 @@ |
||
| 34 | 34 | use Psr\Log\LoggerInterface; |
| 35 | 35 | |
| 36 | 36 | class AppManager implements IAppManager { |
| 37 | - /** |
|
| 38 | - * Apps with these types can not be enabled for certain groups only |
|
| 39 | - * @var string[] |
|
| 40 | - */ |
|
| 41 | - protected $protectedAppTypes = [ |
|
| 42 | - 'filesystem', |
|
| 43 | - 'prelogin', |
|
| 44 | - 'authentication', |
|
| 45 | - 'logging', |
|
| 46 | - 'prevent_group_restriction', |
|
| 47 | - ]; |
|
| 48 | - |
|
| 49 | - /** @var string[] $appId => $enabled */ |
|
| 50 | - private array $enabledAppsCache = []; |
|
| 51 | - |
|
| 52 | - /** @var string[]|null */ |
|
| 53 | - private ?array $shippedApps = null; |
|
| 54 | - |
|
| 55 | - private array $alwaysEnabled = []; |
|
| 56 | - private array $defaultEnabled = []; |
|
| 57 | - |
|
| 58 | - /** @var array */ |
|
| 59 | - private array $appInfos = []; |
|
| 60 | - |
|
| 61 | - /** @var array */ |
|
| 62 | - private array $appVersions = []; |
|
| 63 | - |
|
| 64 | - /** @var array */ |
|
| 65 | - private array $autoDisabledApps = []; |
|
| 66 | - private array $appTypes = []; |
|
| 67 | - |
|
| 68 | - /** @var array<string, true> */ |
|
| 69 | - private array $loadedApps = []; |
|
| 70 | - |
|
| 71 | - private ?AppConfig $appConfig = null; |
|
| 72 | - private ?IURLGenerator $urlGenerator = null; |
|
| 73 | - private ?INavigationManager $navigationManager = null; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Be extremely careful when injecting classes here. The AppManager is used by the installer, |
|
| 77 | - * so it needs to work before installation. See how AppConfig and IURLGenerator are injected for reference |
|
| 78 | - */ |
|
| 79 | - public function __construct( |
|
| 80 | - private IUserSession $userSession, |
|
| 81 | - private IConfig $config, |
|
| 82 | - private IGroupManager $groupManager, |
|
| 83 | - private ICacheFactory $memCacheFactory, |
|
| 84 | - private IEventDispatcher $dispatcher, |
|
| 85 | - private LoggerInterface $logger, |
|
| 86 | - private ServerVersion $serverVersion, |
|
| 87 | - private ConfigManager $configManager, |
|
| 88 | - ) { |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - private function getNavigationManager(): INavigationManager { |
|
| 92 | - if ($this->navigationManager === null) { |
|
| 93 | - $this->navigationManager = Server::get(INavigationManager::class); |
|
| 94 | - } |
|
| 95 | - return $this->navigationManager; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - public function getAppIcon(string $appId, bool $dark = false): ?string { |
|
| 99 | - $possibleIcons = $dark ? [$appId . '-dark.svg', 'app-dark.svg'] : [$appId . '.svg', 'app.svg']; |
|
| 100 | - $icon = null; |
|
| 101 | - foreach ($possibleIcons as $iconName) { |
|
| 102 | - try { |
|
| 103 | - $icon = $this->getUrlGenerator()->imagePath($appId, $iconName); |
|
| 104 | - break; |
|
| 105 | - } catch (\RuntimeException $e) { |
|
| 106 | - // ignore |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - return $icon; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - private function getAppConfig(): AppConfig { |
|
| 113 | - if ($this->appConfig !== null) { |
|
| 114 | - return $this->appConfig; |
|
| 115 | - } |
|
| 116 | - if (!$this->config->getSystemValueBool('installed', false)) { |
|
| 117 | - throw new \Exception('Nextcloud is not installed yet, AppConfig is not available'); |
|
| 118 | - } |
|
| 119 | - $this->appConfig = Server::get(AppConfig::class); |
|
| 120 | - return $this->appConfig; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - private function getUrlGenerator(): IURLGenerator { |
|
| 124 | - if ($this->urlGenerator !== null) { |
|
| 125 | - return $this->urlGenerator; |
|
| 126 | - } |
|
| 127 | - if (!$this->config->getSystemValueBool('installed', false)) { |
|
| 128 | - throw new \Exception('Nextcloud is not installed yet, AppConfig is not available'); |
|
| 129 | - } |
|
| 130 | - $this->urlGenerator = Server::get(IURLGenerator::class); |
|
| 131 | - return $this->urlGenerator; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * For all enabled apps, return the value of their 'enabled' config key. |
|
| 136 | - * |
|
| 137 | - * @return array<string,string> appId => enabled (may be 'yes', or a json encoded list of group ids) |
|
| 138 | - */ |
|
| 139 | - private function getEnabledAppsValues(): array { |
|
| 140 | - if (!$this->enabledAppsCache) { |
|
| 141 | - /** @var array<string,string> */ |
|
| 142 | - $values = $this->getAppConfig()->searchValues('enabled', false, IAppConfig::VALUE_STRING); |
|
| 143 | - |
|
| 144 | - $alwaysEnabledApps = $this->getAlwaysEnabledApps(); |
|
| 145 | - foreach ($alwaysEnabledApps as $appId) { |
|
| 146 | - $values[$appId] = 'yes'; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $this->enabledAppsCache = array_filter($values, function ($value) { |
|
| 150 | - return $value !== 'no'; |
|
| 151 | - }); |
|
| 152 | - ksort($this->enabledAppsCache); |
|
| 153 | - } |
|
| 154 | - return $this->enabledAppsCache; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Deprecated alias |
|
| 159 | - * |
|
| 160 | - * @return string[] |
|
| 161 | - */ |
|
| 162 | - public function getInstalledApps() { |
|
| 163 | - return $this->getEnabledApps(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * List all enabled apps, either for everyone or for some groups |
|
| 168 | - * |
|
| 169 | - * @return list<string> |
|
| 170 | - */ |
|
| 171 | - public function getEnabledApps(): array { |
|
| 172 | - return array_keys($this->getEnabledAppsValues()); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get a list of all apps in the apps folder |
|
| 177 | - * |
|
| 178 | - * @return list<string> an array of app names (string IDs) |
|
| 179 | - */ |
|
| 180 | - public function getAllAppsInAppsFolders(): array { |
|
| 181 | - $apps = []; |
|
| 182 | - |
|
| 183 | - foreach (\OC::$APPSROOTS as $apps_dir) { |
|
| 184 | - if (!is_readable($apps_dir['path'])) { |
|
| 185 | - $this->logger->warning('unable to read app folder : ' . $apps_dir['path'], ['app' => 'core']); |
|
| 186 | - continue; |
|
| 187 | - } |
|
| 188 | - $dh = opendir($apps_dir['path']); |
|
| 189 | - |
|
| 190 | - if (is_resource($dh)) { |
|
| 191 | - while (($file = readdir($dh)) !== false) { |
|
| 192 | - if ( |
|
| 193 | - $file[0] != '.' && |
|
| 194 | - is_dir($apps_dir['path'] . '/' . $file) && |
|
| 195 | - is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml') |
|
| 196 | - ) { |
|
| 197 | - $apps[] = $file; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return array_values(array_unique($apps)); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * List all apps enabled for a user |
|
| 208 | - * |
|
| 209 | - * @param \OCP\IUser $user |
|
| 210 | - * @return list<string> |
|
| 211 | - */ |
|
| 212 | - public function getEnabledAppsForUser(IUser $user) { |
|
| 213 | - $apps = $this->getEnabledAppsValues(); |
|
| 214 | - $appsForUser = array_filter($apps, function ($enabled) use ($user) { |
|
| 215 | - return $this->checkAppForUser($enabled, $user); |
|
| 216 | - }); |
|
| 217 | - return array_keys($appsForUser); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - public function getEnabledAppsForGroup(IGroup $group): array { |
|
| 221 | - $apps = $this->getEnabledAppsValues(); |
|
| 222 | - $appsForGroups = array_filter($apps, function ($enabled) use ($group) { |
|
| 223 | - return $this->checkAppForGroups($enabled, $group); |
|
| 224 | - }); |
|
| 225 | - return array_keys($appsForGroups); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Loads all apps |
|
| 230 | - * |
|
| 231 | - * @param string[] $types |
|
| 232 | - * @return bool |
|
| 233 | - * |
|
| 234 | - * This function walks through the Nextcloud directory and loads all apps |
|
| 235 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 236 | - * exists. |
|
| 237 | - * |
|
| 238 | - * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 239 | - */ |
|
| 240 | - public function loadApps(array $types = []): bool { |
|
| 241 | - if ($this->config->getSystemValueBool('maintenance', false)) { |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - // Load the enabled apps here |
|
| 245 | - $apps = \OC_App::getEnabledApps(); |
|
| 246 | - |
|
| 247 | - // Add each apps' folder as allowed class path |
|
| 248 | - foreach ($apps as $app) { |
|
| 249 | - // If the app is already loaded then autoloading it makes no sense |
|
| 250 | - if (!$this->isAppLoaded($app)) { |
|
| 251 | - $path = \OC_App::getAppPath($app); |
|
| 252 | - if ($path !== false) { |
|
| 253 | - \OC_App::registerAutoloading($app, $path); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // prevent app loading from printing output |
|
| 259 | - ob_start(); |
|
| 260 | - foreach ($apps as $app) { |
|
| 261 | - if (!$this->isAppLoaded($app) && ($types === [] || $this->isType($app, $types))) { |
|
| 262 | - try { |
|
| 263 | - $this->loadApp($app); |
|
| 264 | - } catch (\Throwable $e) { |
|
| 265 | - $this->logger->emergency('Error during app loading: ' . $e->getMessage(), [ |
|
| 266 | - 'exception' => $e, |
|
| 267 | - 'app' => $app, |
|
| 268 | - ]); |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - ob_end_clean(); |
|
| 273 | - |
|
| 274 | - return true; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * check if an app is of a specific type |
|
| 279 | - * |
|
| 280 | - * @param string $app |
|
| 281 | - * @param array $types |
|
| 282 | - * @return bool |
|
| 283 | - */ |
|
| 284 | - public function isType(string $app, array $types): bool { |
|
| 285 | - $appTypes = $this->getAppTypes($app); |
|
| 286 | - foreach ($types as $type) { |
|
| 287 | - if (in_array($type, $appTypes, true)) { |
|
| 288 | - return true; |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - return false; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * get the types of an app |
|
| 296 | - * |
|
| 297 | - * @param string $app |
|
| 298 | - * @return string[] |
|
| 299 | - */ |
|
| 300 | - private function getAppTypes(string $app): array { |
|
| 301 | - //load the cache |
|
| 302 | - if (count($this->appTypes) === 0) { |
|
| 303 | - $this->appTypes = $this->getAppConfig()->getValues(false, 'types') ?: []; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if (isset($this->appTypes[$app])) { |
|
| 307 | - return explode(',', $this->appTypes[$app]); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return []; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @return array |
|
| 315 | - */ |
|
| 316 | - public function getAutoDisabledApps(): array { |
|
| 317 | - return $this->autoDisabledApps; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function getAppRestriction(string $appId): array { |
|
| 321 | - $values = $this->getEnabledAppsValues(); |
|
| 322 | - |
|
| 323 | - if (!isset($values[$appId])) { |
|
| 324 | - return []; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - if ($values[$appId] === 'yes' || $values[$appId] === 'no') { |
|
| 328 | - return []; |
|
| 329 | - } |
|
| 330 | - return json_decode($values[$appId], true); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Check if an app is enabled for user |
|
| 335 | - * |
|
| 336 | - * @param string $appId |
|
| 337 | - * @param \OCP\IUser|null $user (optional) if not defined, the currently logged in user will be used |
|
| 338 | - * @return bool |
|
| 339 | - */ |
|
| 340 | - public function isEnabledForUser($appId, $user = null) { |
|
| 341 | - if ($this->isAlwaysEnabled($appId)) { |
|
| 342 | - return true; |
|
| 343 | - } |
|
| 344 | - if ($user === null) { |
|
| 345 | - $user = $this->userSession->getUser(); |
|
| 346 | - } |
|
| 347 | - $enabledAppsValues = $this->getEnabledAppsValues(); |
|
| 348 | - if (isset($enabledAppsValues[$appId])) { |
|
| 349 | - return $this->checkAppForUser($enabledAppsValues[$appId], $user); |
|
| 350 | - } else { |
|
| 351 | - return false; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - private function checkAppForUser(string $enabled, ?IUser $user): bool { |
|
| 356 | - if ($enabled === 'yes') { |
|
| 357 | - return true; |
|
| 358 | - } elseif ($user === null) { |
|
| 359 | - return false; |
|
| 360 | - } else { |
|
| 361 | - if (empty($enabled)) { |
|
| 362 | - return false; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $groupIds = json_decode($enabled); |
|
| 366 | - |
|
| 367 | - if (!is_array($groupIds)) { |
|
| 368 | - $jsonError = json_last_error(); |
|
| 369 | - $jsonErrorMsg = json_last_error_msg(); |
|
| 370 | - // this really should never happen (if it does, the admin should check the `enabled` key value via `occ config:list` because it's bogus for some reason) |
|
| 371 | - $this->logger->warning('AppManager::checkAppForUser - can\'t decode group IDs listed in app\'s enabled config key: ' . print_r($enabled, true) . ' - JSON error (' . $jsonError . ') ' . $jsonErrorMsg); |
|
| 372 | - return false; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 376 | - foreach ($userGroups as $groupId) { |
|
| 377 | - if (in_array($groupId, $groupIds, true)) { |
|
| 378 | - return true; |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - return false; |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - private function checkAppForGroups(string $enabled, IGroup $group): bool { |
|
| 386 | - if ($enabled === 'yes') { |
|
| 387 | - return true; |
|
| 388 | - } else { |
|
| 389 | - if (empty($enabled)) { |
|
| 390 | - return false; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - $groupIds = json_decode($enabled); |
|
| 394 | - |
|
| 395 | - if (!is_array($groupIds)) { |
|
| 396 | - $jsonError = json_last_error(); |
|
| 397 | - $jsonErrorMsg = json_last_error_msg(); |
|
| 398 | - // this really should never happen (if it does, the admin should check the `enabled` key value via `occ config:list` because it's bogus for some reason) |
|
| 399 | - $this->logger->warning('AppManager::checkAppForGroups - can\'t decode group IDs listed in app\'s enabled config key: ' . print_r($enabled, true) . ' - JSON error (' . $jsonError . ') ' . $jsonErrorMsg); |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - return in_array($group->getGID(), $groupIds); |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Check if an app is enabled in the instance |
|
| 409 | - * |
|
| 410 | - * Notice: This actually checks if the app is enabled and not only if it is installed. |
|
| 411 | - * |
|
| 412 | - * @param string $appId |
|
| 413 | - */ |
|
| 414 | - public function isInstalled($appId): bool { |
|
| 415 | - return $this->isEnabledForAnyone($appId); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - public function isEnabledForAnyone(string $appId): bool { |
|
| 419 | - $enabledAppsValues = $this->getEnabledAppsValues(); |
|
| 420 | - return isset($enabledAppsValues[$appId]); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Overwrite the `max-version` requirement for this app. |
|
| 425 | - */ |
|
| 426 | - public function overwriteNextcloudRequirement(string $appId): void { |
|
| 427 | - $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
|
| 428 | - if (!in_array($appId, $ignoreMaxApps, true)) { |
|
| 429 | - $ignoreMaxApps[] = $appId; |
|
| 430 | - } |
|
| 431 | - $this->config->setSystemValue('app_install_overwrite', $ignoreMaxApps); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Remove the `max-version` overwrite for this app. |
|
| 436 | - * This means this app now again can not be enabled if the `max-version` is smaller than the current Nextcloud version. |
|
| 437 | - */ |
|
| 438 | - public function removeOverwriteNextcloudRequirement(string $appId): void { |
|
| 439 | - $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
|
| 440 | - $ignoreMaxApps = array_filter($ignoreMaxApps, fn (string $id) => $id !== $appId); |
|
| 441 | - $this->config->setSystemValue('app_install_overwrite', $ignoreMaxApps); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - public function loadApp(string $app): void { |
|
| 445 | - if (isset($this->loadedApps[$app])) { |
|
| 446 | - return; |
|
| 447 | - } |
|
| 448 | - $this->loadedApps[$app] = true; |
|
| 449 | - $appPath = \OC_App::getAppPath($app); |
|
| 450 | - if ($appPath === false) { |
|
| 451 | - return; |
|
| 452 | - } |
|
| 453 | - $eventLogger = \OC::$server->get(IEventLogger::class); |
|
| 454 | - $eventLogger->start("bootstrap:load_app:$app", "Load app: $app"); |
|
| 455 | - |
|
| 456 | - // in case someone calls loadApp() directly |
|
| 457 | - \OC_App::registerAutoloading($app, $appPath); |
|
| 458 | - |
|
| 459 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 460 | - $this->logger->error('/appinfo/app.php is not supported anymore, use \OCP\AppFramework\Bootstrap\IBootstrap on the application class instead.', [ |
|
| 461 | - 'app' => $app, |
|
| 462 | - ]); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - $coordinator = Server::get(Coordinator::class); |
|
| 466 | - $coordinator->bootApp($app); |
|
| 467 | - |
|
| 468 | - $eventLogger->start("bootstrap:load_app:$app:info", "Load info.xml for $app and register any services defined in it"); |
|
| 469 | - $info = $this->getAppInfo($app); |
|
| 470 | - if (!empty($info['activity'])) { |
|
| 471 | - $activityManager = \OC::$server->get(IActivityManager::class); |
|
| 472 | - if (!empty($info['activity']['filters'])) { |
|
| 473 | - foreach ($info['activity']['filters'] as $filter) { |
|
| 474 | - $activityManager->registerFilter($filter); |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - if (!empty($info['activity']['settings'])) { |
|
| 478 | - foreach ($info['activity']['settings'] as $setting) { |
|
| 479 | - $activityManager->registerSetting($setting); |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - if (!empty($info['activity']['providers'])) { |
|
| 483 | - foreach ($info['activity']['providers'] as $provider) { |
|
| 484 | - $activityManager->registerProvider($provider); |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - if (!empty($info['settings'])) { |
|
| 490 | - $settingsManager = \OC::$server->get(ISettingsManager::class); |
|
| 491 | - if (!empty($info['settings']['admin'])) { |
|
| 492 | - foreach ($info['settings']['admin'] as $setting) { |
|
| 493 | - $settingsManager->registerSetting('admin', $setting); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - if (!empty($info['settings']['admin-section'])) { |
|
| 497 | - foreach ($info['settings']['admin-section'] as $section) { |
|
| 498 | - $settingsManager->registerSection('admin', $section); |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - if (!empty($info['settings']['personal'])) { |
|
| 502 | - foreach ($info['settings']['personal'] as $setting) { |
|
| 503 | - $settingsManager->registerSetting('personal', $setting); |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - if (!empty($info['settings']['personal-section'])) { |
|
| 507 | - foreach ($info['settings']['personal-section'] as $section) { |
|
| 508 | - $settingsManager->registerSection('personal', $section); |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - if (!empty($info['collaboration']['plugins'])) { |
|
| 514 | - // deal with one or many plugin entries |
|
| 515 | - $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 516 | - [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 517 | - $collaboratorSearch = null; |
|
| 518 | - $autoCompleteManager = null; |
|
| 519 | - foreach ($plugins as $plugin) { |
|
| 520 | - if ($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 521 | - $pluginInfo = [ |
|
| 522 | - 'shareType' => $plugin['@attributes']['share-type'], |
|
| 523 | - 'class' => $plugin['@value'], |
|
| 524 | - ]; |
|
| 525 | - $collaboratorSearch ??= \OC::$server->get(ICollaboratorSearch::class); |
|
| 526 | - $collaboratorSearch->registerPlugin($pluginInfo); |
|
| 527 | - } elseif ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 528 | - $autoCompleteManager ??= \OC::$server->get(IAutoCompleteManager::class); |
|
| 529 | - $autoCompleteManager->registerSorter($plugin['@value']); |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - $eventLogger->end("bootstrap:load_app:$app:info"); |
|
| 534 | - |
|
| 535 | - $eventLogger->end("bootstrap:load_app:$app"); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Check if an app is loaded |
|
| 540 | - * @param string $app app id |
|
| 541 | - * @since 26.0.0 |
|
| 542 | - */ |
|
| 543 | - public function isAppLoaded(string $app): bool { |
|
| 544 | - return isset($this->loadedApps[$app]); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Enable an app for every user |
|
| 549 | - * |
|
| 550 | - * @param string $appId |
|
| 551 | - * @param bool $forceEnable |
|
| 552 | - * @throws AppPathNotFoundException |
|
| 553 | - * @throws \InvalidArgumentException if the application is not installed yet |
|
| 554 | - */ |
|
| 555 | - public function enableApp(string $appId, bool $forceEnable = false): void { |
|
| 556 | - // Check if app exists |
|
| 557 | - $this->getAppPath($appId); |
|
| 558 | - |
|
| 559 | - if ($this->config->getAppValue($appId, 'installed_version', '') === '') { |
|
| 560 | - throw new \InvalidArgumentException("$appId is not installed, cannot be enabled."); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - if ($forceEnable) { |
|
| 564 | - $this->overwriteNextcloudRequirement($appId); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - $this->enabledAppsCache[$appId] = 'yes'; |
|
| 568 | - $this->getAppConfig()->setValue($appId, 'enabled', 'yes'); |
|
| 569 | - $this->dispatcher->dispatchTyped(new AppEnableEvent($appId)); |
|
| 570 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent( |
|
| 571 | - ManagerEvent::EVENT_APP_ENABLE, $appId |
|
| 572 | - )); |
|
| 573 | - $this->clearAppsCache(); |
|
| 574 | - |
|
| 575 | - $this->configManager->migrateConfigLexiconKeys($appId); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Whether a list of types contains a protected app type |
|
| 580 | - * |
|
| 581 | - * @param string[] $types |
|
| 582 | - * @return bool |
|
| 583 | - */ |
|
| 584 | - public function hasProtectedAppType($types) { |
|
| 585 | - if (empty($types)) { |
|
| 586 | - return false; |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $protectedTypes = array_intersect($this->protectedAppTypes, $types); |
|
| 590 | - return !empty($protectedTypes); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Enable an app only for specific groups |
|
| 595 | - * |
|
| 596 | - * @param string $appId |
|
| 597 | - * @param IGroup[] $groups |
|
| 598 | - * @param bool $forceEnable |
|
| 599 | - * @throws \InvalidArgumentException if app can't be enabled for groups |
|
| 600 | - * @throws AppPathNotFoundException |
|
| 601 | - */ |
|
| 602 | - public function enableAppForGroups(string $appId, array $groups, bool $forceEnable = false): void { |
|
| 603 | - // Check if app exists |
|
| 604 | - $this->getAppPath($appId); |
|
| 605 | - |
|
| 606 | - $info = $this->getAppInfo($appId); |
|
| 607 | - if (!empty($info['types']) && $this->hasProtectedAppType($info['types'])) { |
|
| 608 | - throw new \InvalidArgumentException("$appId can't be enabled for groups."); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - if ($this->config->getAppValue($appId, 'installed_version', '') === '') { |
|
| 612 | - throw new \InvalidArgumentException("$appId is not installed, cannot be enabled."); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - if ($forceEnable) { |
|
| 616 | - $this->overwriteNextcloudRequirement($appId); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** @var string[] $groupIds */ |
|
| 620 | - $groupIds = array_map(function ($group) { |
|
| 621 | - /** @var IGroup $group */ |
|
| 622 | - return ($group instanceof IGroup) |
|
| 623 | - ? $group->getGID() |
|
| 624 | - : $group; |
|
| 625 | - }, $groups); |
|
| 626 | - |
|
| 627 | - $this->enabledAppsCache[$appId] = json_encode($groupIds); |
|
| 628 | - $this->getAppConfig()->setValue($appId, 'enabled', json_encode($groupIds)); |
|
| 629 | - $this->dispatcher->dispatchTyped(new AppEnableEvent($appId, $groupIds)); |
|
| 630 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent( |
|
| 631 | - ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups |
|
| 632 | - )); |
|
| 633 | - $this->clearAppsCache(); |
|
| 634 | - |
|
| 635 | - $this->configManager->migrateConfigLexiconKeys($appId); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Disable an app for every user |
|
| 640 | - * |
|
| 641 | - * @param string $appId |
|
| 642 | - * @param bool $automaticDisabled |
|
| 643 | - * @throws \Exception if app can't be disabled |
|
| 644 | - */ |
|
| 645 | - public function disableApp($appId, $automaticDisabled = false): void { |
|
| 646 | - if ($this->isAlwaysEnabled($appId)) { |
|
| 647 | - throw new \Exception("$appId can't be disabled."); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - if ($automaticDisabled) { |
|
| 651 | - $previousSetting = $this->getAppConfig()->getValue($appId, 'enabled', 'yes'); |
|
| 652 | - if ($previousSetting !== 'yes' && $previousSetting !== 'no') { |
|
| 653 | - $previousSetting = json_decode($previousSetting, true); |
|
| 654 | - } |
|
| 655 | - $this->autoDisabledApps[$appId] = $previousSetting; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - unset($this->enabledAppsCache[$appId]); |
|
| 659 | - $this->getAppConfig()->setValue($appId, 'enabled', 'no'); |
|
| 660 | - |
|
| 661 | - // run uninstall steps |
|
| 662 | - $appData = $this->getAppInfo($appId); |
|
| 663 | - if (!is_null($appData)) { |
|
| 664 | - \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']); |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - $this->dispatcher->dispatchTyped(new AppDisableEvent($appId)); |
|
| 668 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent( |
|
| 669 | - ManagerEvent::EVENT_APP_DISABLE, $appId |
|
| 670 | - )); |
|
| 671 | - $this->clearAppsCache(); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Get the directory for the given app. |
|
| 676 | - * |
|
| 677 | - * @throws AppPathNotFoundException if app folder can't be found |
|
| 678 | - */ |
|
| 679 | - public function getAppPath(string $appId): string { |
|
| 680 | - $appPath = \OC_App::getAppPath($appId); |
|
| 681 | - if ($appPath === false) { |
|
| 682 | - throw new AppPathNotFoundException('Could not find path for ' . $appId); |
|
| 683 | - } |
|
| 684 | - return $appPath; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Get the web path for the given app. |
|
| 689 | - * |
|
| 690 | - * @param string $appId |
|
| 691 | - * @return string |
|
| 692 | - * @throws AppPathNotFoundException if app path can't be found |
|
| 693 | - */ |
|
| 694 | - public function getAppWebPath(string $appId): string { |
|
| 695 | - $appWebPath = \OC_App::getAppWebPath($appId); |
|
| 696 | - if ($appWebPath === false) { |
|
| 697 | - throw new AppPathNotFoundException('Could not find web path for ' . $appId); |
|
| 698 | - } |
|
| 699 | - return $appWebPath; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Clear the cached list of apps when enabling/disabling an app |
|
| 704 | - */ |
|
| 705 | - public function clearAppsCache(): void { |
|
| 706 | - $this->appInfos = []; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Returns a list of apps that need upgrade |
|
| 711 | - * |
|
| 712 | - * @param string $version Nextcloud version as array of version components |
|
| 713 | - * @return array list of app info from apps that need an upgrade |
|
| 714 | - * |
|
| 715 | - * @internal |
|
| 716 | - */ |
|
| 717 | - public function getAppsNeedingUpgrade($version) { |
|
| 718 | - $appsToUpgrade = []; |
|
| 719 | - $apps = $this->getEnabledApps(); |
|
| 720 | - foreach ($apps as $appId) { |
|
| 721 | - $appInfo = $this->getAppInfo($appId); |
|
| 722 | - $appDbVersion = $this->getAppConfig()->getValue($appId, 'installed_version'); |
|
| 723 | - if ($appDbVersion |
|
| 724 | - && isset($appInfo['version']) |
|
| 725 | - && version_compare($appInfo['version'], $appDbVersion, '>') |
|
| 726 | - && \OC_App::isAppCompatible($version, $appInfo) |
|
| 727 | - ) { |
|
| 728 | - $appsToUpgrade[] = $appInfo; |
|
| 729 | - } |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - return $appsToUpgrade; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * Returns the app information from "appinfo/info.xml". |
|
| 737 | - * |
|
| 738 | - * @param string|null $lang |
|
| 739 | - * @return array|null app info |
|
| 740 | - */ |
|
| 741 | - public function getAppInfo(string $appId, bool $path = false, $lang = null) { |
|
| 742 | - if ($path) { |
|
| 743 | - throw new \InvalidArgumentException('Calling IAppManager::getAppInfo() with a path is no longer supported. Please call IAppManager::getAppInfoByPath() instead and verify that the path is good before calling.'); |
|
| 744 | - } |
|
| 745 | - if ($lang === null && isset($this->appInfos[$appId])) { |
|
| 746 | - return $this->appInfos[$appId]; |
|
| 747 | - } |
|
| 748 | - try { |
|
| 749 | - $appPath = $this->getAppPath($appId); |
|
| 750 | - } catch (AppPathNotFoundException) { |
|
| 751 | - return null; |
|
| 752 | - } |
|
| 753 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 754 | - |
|
| 755 | - $data = $this->getAppInfoByPath($file, $lang); |
|
| 756 | - |
|
| 757 | - if ($lang === null) { |
|
| 758 | - $this->appInfos[$appId] = $data; |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - return $data; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - public function getAppInfoByPath(string $path, ?string $lang = null): ?array { |
|
| 765 | - if (!str_ends_with($path, '/appinfo/info.xml')) { |
|
| 766 | - return null; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - $parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo')); |
|
| 770 | - $data = $parser->parse($path); |
|
| 771 | - |
|
| 772 | - if (is_array($data)) { |
|
| 773 | - $data = \OC_App::parseAppInfo($data, $lang); |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - return $data; |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - public function getAppVersion(string $appId, bool $useCache = true): string { |
|
| 780 | - if (!$useCache || !isset($this->appVersions[$appId])) { |
|
| 781 | - if ($appId === 'core') { |
|
| 782 | - $this->appVersions[$appId] = $this->serverVersion->getVersionString(); |
|
| 783 | - } else { |
|
| 784 | - $appInfo = $this->getAppInfo($appId); |
|
| 785 | - $this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0'; |
|
| 786 | - } |
|
| 787 | - } |
|
| 788 | - return $this->appVersions[$appId]; |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Returns the installed versions of all apps |
|
| 793 | - * |
|
| 794 | - * @return array<string, string> |
|
| 795 | - */ |
|
| 796 | - public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 797 | - return $this->getAppConfig()->getAppInstalledVersions($onlyEnabled); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * Returns a list of apps incompatible with the given version |
|
| 802 | - * |
|
| 803 | - * @param string $version Nextcloud version as array of version components |
|
| 804 | - * |
|
| 805 | - * @return array list of app info from incompatible apps |
|
| 806 | - * |
|
| 807 | - * @internal |
|
| 808 | - */ |
|
| 809 | - public function getIncompatibleApps(string $version): array { |
|
| 810 | - $apps = $this->getEnabledApps(); |
|
| 811 | - $incompatibleApps = []; |
|
| 812 | - foreach ($apps as $appId) { |
|
| 813 | - $info = $this->getAppInfo($appId); |
|
| 814 | - if ($info === null) { |
|
| 815 | - $incompatibleApps[] = ['id' => $appId, 'name' => $appId]; |
|
| 816 | - } elseif (!\OC_App::isAppCompatible($version, $info)) { |
|
| 817 | - $incompatibleApps[] = $info; |
|
| 818 | - } |
|
| 819 | - } |
|
| 820 | - return $incompatibleApps; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * @inheritdoc |
|
| 825 | - * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped() |
|
| 826 | - */ |
|
| 827 | - public function isShipped($appId) { |
|
| 828 | - $this->loadShippedJson(); |
|
| 829 | - return in_array($appId, $this->shippedApps, true); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - private function isAlwaysEnabled(string $appId): bool { |
|
| 833 | - if ($appId === 'core') { |
|
| 834 | - return true; |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - $alwaysEnabled = $this->getAlwaysEnabledApps(); |
|
| 838 | - return in_array($appId, $alwaysEnabled, true); |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - /** |
|
| 842 | - * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson() |
|
| 843 | - * @throws \Exception |
|
| 844 | - */ |
|
| 845 | - private function loadShippedJson(): void { |
|
| 846 | - if ($this->shippedApps === null) { |
|
| 847 | - $shippedJson = \OC::$SERVERROOT . '/core/shipped.json'; |
|
| 848 | - if (!file_exists($shippedJson)) { |
|
| 849 | - throw new \Exception("File not found: $shippedJson"); |
|
| 850 | - } |
|
| 851 | - $content = json_decode(file_get_contents($shippedJson), true); |
|
| 852 | - $this->shippedApps = $content['shippedApps']; |
|
| 853 | - $this->alwaysEnabled = $content['alwaysEnabled']; |
|
| 854 | - $this->defaultEnabled = $content['defaultEnabled']; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * @inheritdoc |
|
| 860 | - */ |
|
| 861 | - public function getAlwaysEnabledApps() { |
|
| 862 | - $this->loadShippedJson(); |
|
| 863 | - return $this->alwaysEnabled; |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * @inheritdoc |
|
| 868 | - */ |
|
| 869 | - public function isDefaultEnabled(string $appId): bool { |
|
| 870 | - return (in_array($appId, $this->getDefaultEnabledApps())); |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * @inheritdoc |
|
| 875 | - */ |
|
| 876 | - public function getDefaultEnabledApps(): array { |
|
| 877 | - $this->loadShippedJson(); |
|
| 878 | - |
|
| 879 | - return $this->defaultEnabled; |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * @inheritdoc |
|
| 884 | - */ |
|
| 885 | - public function getDefaultAppForUser(?IUser $user = null, bool $withFallbacks = true): string { |
|
| 886 | - $id = $this->getNavigationManager()->getDefaultEntryIdForUser($user, $withFallbacks); |
|
| 887 | - $entry = $this->getNavigationManager()->get($id); |
|
| 888 | - return (string)$entry['app']; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * @inheritdoc |
|
| 893 | - */ |
|
| 894 | - public function getDefaultApps(): array { |
|
| 895 | - $ids = $this->getNavigationManager()->getDefaultEntryIds(); |
|
| 896 | - |
|
| 897 | - return array_values(array_unique(array_map(function (string $id) { |
|
| 898 | - $entry = $this->getNavigationManager()->get($id); |
|
| 899 | - return (string)$entry['app']; |
|
| 900 | - }, $ids))); |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - /** |
|
| 904 | - * @inheritdoc |
|
| 905 | - */ |
|
| 906 | - public function setDefaultApps(array $defaultApps): void { |
|
| 907 | - $entries = $this->getNavigationManager()->getAll(); |
|
| 908 | - $ids = []; |
|
| 909 | - foreach ($defaultApps as $defaultApp) { |
|
| 910 | - foreach ($entries as $entry) { |
|
| 911 | - if ((string)$entry['app'] === $defaultApp) { |
|
| 912 | - $ids[] = (string)$entry['id']; |
|
| 913 | - break; |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - } |
|
| 917 | - $this->getNavigationManager()->setDefaultEntryIds($ids); |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - public function isBackendRequired(string $backend): bool { |
|
| 921 | - foreach ($this->appInfos as $appInfo) { |
|
| 922 | - foreach ($appInfo['dependencies']['backend'] as $appBackend) { |
|
| 923 | - if ($backend === $appBackend) { |
|
| 924 | - return true; |
|
| 925 | - } |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - return false; |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * Clean the appId from forbidden characters |
|
| 934 | - * |
|
| 935 | - * @psalm-taint-escape callable |
|
| 936 | - * @psalm-taint-escape cookie |
|
| 937 | - * @psalm-taint-escape file |
|
| 938 | - * @psalm-taint-escape has_quotes |
|
| 939 | - * @psalm-taint-escape header |
|
| 940 | - * @psalm-taint-escape html |
|
| 941 | - * @psalm-taint-escape include |
|
| 942 | - * @psalm-taint-escape ldap |
|
| 943 | - * @psalm-taint-escape shell |
|
| 944 | - * @psalm-taint-escape sql |
|
| 945 | - * @psalm-taint-escape unserialize |
|
| 946 | - */ |
|
| 947 | - public function cleanAppId(string $app): string { |
|
| 948 | - /* Only lowercase alphanumeric is allowed */ |
|
| 949 | - return preg_replace('/(^[0-9_]|[^a-z0-9_]+|_$)/', '', $app); |
|
| 950 | - } |
|
| 37 | + /** |
|
| 38 | + * Apps with these types can not be enabled for certain groups only |
|
| 39 | + * @var string[] |
|
| 40 | + */ |
|
| 41 | + protected $protectedAppTypes = [ |
|
| 42 | + 'filesystem', |
|
| 43 | + 'prelogin', |
|
| 44 | + 'authentication', |
|
| 45 | + 'logging', |
|
| 46 | + 'prevent_group_restriction', |
|
| 47 | + ]; |
|
| 48 | + |
|
| 49 | + /** @var string[] $appId => $enabled */ |
|
| 50 | + private array $enabledAppsCache = []; |
|
| 51 | + |
|
| 52 | + /** @var string[]|null */ |
|
| 53 | + private ?array $shippedApps = null; |
|
| 54 | + |
|
| 55 | + private array $alwaysEnabled = []; |
|
| 56 | + private array $defaultEnabled = []; |
|
| 57 | + |
|
| 58 | + /** @var array */ |
|
| 59 | + private array $appInfos = []; |
|
| 60 | + |
|
| 61 | + /** @var array */ |
|
| 62 | + private array $appVersions = []; |
|
| 63 | + |
|
| 64 | + /** @var array */ |
|
| 65 | + private array $autoDisabledApps = []; |
|
| 66 | + private array $appTypes = []; |
|
| 67 | + |
|
| 68 | + /** @var array<string, true> */ |
|
| 69 | + private array $loadedApps = []; |
|
| 70 | + |
|
| 71 | + private ?AppConfig $appConfig = null; |
|
| 72 | + private ?IURLGenerator $urlGenerator = null; |
|
| 73 | + private ?INavigationManager $navigationManager = null; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Be extremely careful when injecting classes here. The AppManager is used by the installer, |
|
| 77 | + * so it needs to work before installation. See how AppConfig and IURLGenerator are injected for reference |
|
| 78 | + */ |
|
| 79 | + public function __construct( |
|
| 80 | + private IUserSession $userSession, |
|
| 81 | + private IConfig $config, |
|
| 82 | + private IGroupManager $groupManager, |
|
| 83 | + private ICacheFactory $memCacheFactory, |
|
| 84 | + private IEventDispatcher $dispatcher, |
|
| 85 | + private LoggerInterface $logger, |
|
| 86 | + private ServerVersion $serverVersion, |
|
| 87 | + private ConfigManager $configManager, |
|
| 88 | + ) { |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + private function getNavigationManager(): INavigationManager { |
|
| 92 | + if ($this->navigationManager === null) { |
|
| 93 | + $this->navigationManager = Server::get(INavigationManager::class); |
|
| 94 | + } |
|
| 95 | + return $this->navigationManager; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + public function getAppIcon(string $appId, bool $dark = false): ?string { |
|
| 99 | + $possibleIcons = $dark ? [$appId . '-dark.svg', 'app-dark.svg'] : [$appId . '.svg', 'app.svg']; |
|
| 100 | + $icon = null; |
|
| 101 | + foreach ($possibleIcons as $iconName) { |
|
| 102 | + try { |
|
| 103 | + $icon = $this->getUrlGenerator()->imagePath($appId, $iconName); |
|
| 104 | + break; |
|
| 105 | + } catch (\RuntimeException $e) { |
|
| 106 | + // ignore |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + return $icon; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + private function getAppConfig(): AppConfig { |
|
| 113 | + if ($this->appConfig !== null) { |
|
| 114 | + return $this->appConfig; |
|
| 115 | + } |
|
| 116 | + if (!$this->config->getSystemValueBool('installed', false)) { |
|
| 117 | + throw new \Exception('Nextcloud is not installed yet, AppConfig is not available'); |
|
| 118 | + } |
|
| 119 | + $this->appConfig = Server::get(AppConfig::class); |
|
| 120 | + return $this->appConfig; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + private function getUrlGenerator(): IURLGenerator { |
|
| 124 | + if ($this->urlGenerator !== null) { |
|
| 125 | + return $this->urlGenerator; |
|
| 126 | + } |
|
| 127 | + if (!$this->config->getSystemValueBool('installed', false)) { |
|
| 128 | + throw new \Exception('Nextcloud is not installed yet, AppConfig is not available'); |
|
| 129 | + } |
|
| 130 | + $this->urlGenerator = Server::get(IURLGenerator::class); |
|
| 131 | + return $this->urlGenerator; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * For all enabled apps, return the value of their 'enabled' config key. |
|
| 136 | + * |
|
| 137 | + * @return array<string,string> appId => enabled (may be 'yes', or a json encoded list of group ids) |
|
| 138 | + */ |
|
| 139 | + private function getEnabledAppsValues(): array { |
|
| 140 | + if (!$this->enabledAppsCache) { |
|
| 141 | + /** @var array<string,string> */ |
|
| 142 | + $values = $this->getAppConfig()->searchValues('enabled', false, IAppConfig::VALUE_STRING); |
|
| 143 | + |
|
| 144 | + $alwaysEnabledApps = $this->getAlwaysEnabledApps(); |
|
| 145 | + foreach ($alwaysEnabledApps as $appId) { |
|
| 146 | + $values[$appId] = 'yes'; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $this->enabledAppsCache = array_filter($values, function ($value) { |
|
| 150 | + return $value !== 'no'; |
|
| 151 | + }); |
|
| 152 | + ksort($this->enabledAppsCache); |
|
| 153 | + } |
|
| 154 | + return $this->enabledAppsCache; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Deprecated alias |
|
| 159 | + * |
|
| 160 | + * @return string[] |
|
| 161 | + */ |
|
| 162 | + public function getInstalledApps() { |
|
| 163 | + return $this->getEnabledApps(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * List all enabled apps, either for everyone or for some groups |
|
| 168 | + * |
|
| 169 | + * @return list<string> |
|
| 170 | + */ |
|
| 171 | + public function getEnabledApps(): array { |
|
| 172 | + return array_keys($this->getEnabledAppsValues()); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get a list of all apps in the apps folder |
|
| 177 | + * |
|
| 178 | + * @return list<string> an array of app names (string IDs) |
|
| 179 | + */ |
|
| 180 | + public function getAllAppsInAppsFolders(): array { |
|
| 181 | + $apps = []; |
|
| 182 | + |
|
| 183 | + foreach (\OC::$APPSROOTS as $apps_dir) { |
|
| 184 | + if (!is_readable($apps_dir['path'])) { |
|
| 185 | + $this->logger->warning('unable to read app folder : ' . $apps_dir['path'], ['app' => 'core']); |
|
| 186 | + continue; |
|
| 187 | + } |
|
| 188 | + $dh = opendir($apps_dir['path']); |
|
| 189 | + |
|
| 190 | + if (is_resource($dh)) { |
|
| 191 | + while (($file = readdir($dh)) !== false) { |
|
| 192 | + if ( |
|
| 193 | + $file[0] != '.' && |
|
| 194 | + is_dir($apps_dir['path'] . '/' . $file) && |
|
| 195 | + is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml') |
|
| 196 | + ) { |
|
| 197 | + $apps[] = $file; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return array_values(array_unique($apps)); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * List all apps enabled for a user |
|
| 208 | + * |
|
| 209 | + * @param \OCP\IUser $user |
|
| 210 | + * @return list<string> |
|
| 211 | + */ |
|
| 212 | + public function getEnabledAppsForUser(IUser $user) { |
|
| 213 | + $apps = $this->getEnabledAppsValues(); |
|
| 214 | + $appsForUser = array_filter($apps, function ($enabled) use ($user) { |
|
| 215 | + return $this->checkAppForUser($enabled, $user); |
|
| 216 | + }); |
|
| 217 | + return array_keys($appsForUser); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + public function getEnabledAppsForGroup(IGroup $group): array { |
|
| 221 | + $apps = $this->getEnabledAppsValues(); |
|
| 222 | + $appsForGroups = array_filter($apps, function ($enabled) use ($group) { |
|
| 223 | + return $this->checkAppForGroups($enabled, $group); |
|
| 224 | + }); |
|
| 225 | + return array_keys($appsForGroups); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Loads all apps |
|
| 230 | + * |
|
| 231 | + * @param string[] $types |
|
| 232 | + * @return bool |
|
| 233 | + * |
|
| 234 | + * This function walks through the Nextcloud directory and loads all apps |
|
| 235 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 236 | + * exists. |
|
| 237 | + * |
|
| 238 | + * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 239 | + */ |
|
| 240 | + public function loadApps(array $types = []): bool { |
|
| 241 | + if ($this->config->getSystemValueBool('maintenance', false)) { |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + // Load the enabled apps here |
|
| 245 | + $apps = \OC_App::getEnabledApps(); |
|
| 246 | + |
|
| 247 | + // Add each apps' folder as allowed class path |
|
| 248 | + foreach ($apps as $app) { |
|
| 249 | + // If the app is already loaded then autoloading it makes no sense |
|
| 250 | + if (!$this->isAppLoaded($app)) { |
|
| 251 | + $path = \OC_App::getAppPath($app); |
|
| 252 | + if ($path !== false) { |
|
| 253 | + \OC_App::registerAutoloading($app, $path); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // prevent app loading from printing output |
|
| 259 | + ob_start(); |
|
| 260 | + foreach ($apps as $app) { |
|
| 261 | + if (!$this->isAppLoaded($app) && ($types === [] || $this->isType($app, $types))) { |
|
| 262 | + try { |
|
| 263 | + $this->loadApp($app); |
|
| 264 | + } catch (\Throwable $e) { |
|
| 265 | + $this->logger->emergency('Error during app loading: ' . $e->getMessage(), [ |
|
| 266 | + 'exception' => $e, |
|
| 267 | + 'app' => $app, |
|
| 268 | + ]); |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + ob_end_clean(); |
|
| 273 | + |
|
| 274 | + return true; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * check if an app is of a specific type |
|
| 279 | + * |
|
| 280 | + * @param string $app |
|
| 281 | + * @param array $types |
|
| 282 | + * @return bool |
|
| 283 | + */ |
|
| 284 | + public function isType(string $app, array $types): bool { |
|
| 285 | + $appTypes = $this->getAppTypes($app); |
|
| 286 | + foreach ($types as $type) { |
|
| 287 | + if (in_array($type, $appTypes, true)) { |
|
| 288 | + return true; |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + return false; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * get the types of an app |
|
| 296 | + * |
|
| 297 | + * @param string $app |
|
| 298 | + * @return string[] |
|
| 299 | + */ |
|
| 300 | + private function getAppTypes(string $app): array { |
|
| 301 | + //load the cache |
|
| 302 | + if (count($this->appTypes) === 0) { |
|
| 303 | + $this->appTypes = $this->getAppConfig()->getValues(false, 'types') ?: []; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + if (isset($this->appTypes[$app])) { |
|
| 307 | + return explode(',', $this->appTypes[$app]); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return []; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @return array |
|
| 315 | + */ |
|
| 316 | + public function getAutoDisabledApps(): array { |
|
| 317 | + return $this->autoDisabledApps; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function getAppRestriction(string $appId): array { |
|
| 321 | + $values = $this->getEnabledAppsValues(); |
|
| 322 | + |
|
| 323 | + if (!isset($values[$appId])) { |
|
| 324 | + return []; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + if ($values[$appId] === 'yes' || $values[$appId] === 'no') { |
|
| 328 | + return []; |
|
| 329 | + } |
|
| 330 | + return json_decode($values[$appId], true); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Check if an app is enabled for user |
|
| 335 | + * |
|
| 336 | + * @param string $appId |
|
| 337 | + * @param \OCP\IUser|null $user (optional) if not defined, the currently logged in user will be used |
|
| 338 | + * @return bool |
|
| 339 | + */ |
|
| 340 | + public function isEnabledForUser($appId, $user = null) { |
|
| 341 | + if ($this->isAlwaysEnabled($appId)) { |
|
| 342 | + return true; |
|
| 343 | + } |
|
| 344 | + if ($user === null) { |
|
| 345 | + $user = $this->userSession->getUser(); |
|
| 346 | + } |
|
| 347 | + $enabledAppsValues = $this->getEnabledAppsValues(); |
|
| 348 | + if (isset($enabledAppsValues[$appId])) { |
|
| 349 | + return $this->checkAppForUser($enabledAppsValues[$appId], $user); |
|
| 350 | + } else { |
|
| 351 | + return false; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + private function checkAppForUser(string $enabled, ?IUser $user): bool { |
|
| 356 | + if ($enabled === 'yes') { |
|
| 357 | + return true; |
|
| 358 | + } elseif ($user === null) { |
|
| 359 | + return false; |
|
| 360 | + } else { |
|
| 361 | + if (empty($enabled)) { |
|
| 362 | + return false; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $groupIds = json_decode($enabled); |
|
| 366 | + |
|
| 367 | + if (!is_array($groupIds)) { |
|
| 368 | + $jsonError = json_last_error(); |
|
| 369 | + $jsonErrorMsg = json_last_error_msg(); |
|
| 370 | + // this really should never happen (if it does, the admin should check the `enabled` key value via `occ config:list` because it's bogus for some reason) |
|
| 371 | + $this->logger->warning('AppManager::checkAppForUser - can\'t decode group IDs listed in app\'s enabled config key: ' . print_r($enabled, true) . ' - JSON error (' . $jsonError . ') ' . $jsonErrorMsg); |
|
| 372 | + return false; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 376 | + foreach ($userGroups as $groupId) { |
|
| 377 | + if (in_array($groupId, $groupIds, true)) { |
|
| 378 | + return true; |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + return false; |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + private function checkAppForGroups(string $enabled, IGroup $group): bool { |
|
| 386 | + if ($enabled === 'yes') { |
|
| 387 | + return true; |
|
| 388 | + } else { |
|
| 389 | + if (empty($enabled)) { |
|
| 390 | + return false; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + $groupIds = json_decode($enabled); |
|
| 394 | + |
|
| 395 | + if (!is_array($groupIds)) { |
|
| 396 | + $jsonError = json_last_error(); |
|
| 397 | + $jsonErrorMsg = json_last_error_msg(); |
|
| 398 | + // this really should never happen (if it does, the admin should check the `enabled` key value via `occ config:list` because it's bogus for some reason) |
|
| 399 | + $this->logger->warning('AppManager::checkAppForGroups - can\'t decode group IDs listed in app\'s enabled config key: ' . print_r($enabled, true) . ' - JSON error (' . $jsonError . ') ' . $jsonErrorMsg); |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + return in_array($group->getGID(), $groupIds); |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Check if an app is enabled in the instance |
|
| 409 | + * |
|
| 410 | + * Notice: This actually checks if the app is enabled and not only if it is installed. |
|
| 411 | + * |
|
| 412 | + * @param string $appId |
|
| 413 | + */ |
|
| 414 | + public function isInstalled($appId): bool { |
|
| 415 | + return $this->isEnabledForAnyone($appId); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + public function isEnabledForAnyone(string $appId): bool { |
|
| 419 | + $enabledAppsValues = $this->getEnabledAppsValues(); |
|
| 420 | + return isset($enabledAppsValues[$appId]); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Overwrite the `max-version` requirement for this app. |
|
| 425 | + */ |
|
| 426 | + public function overwriteNextcloudRequirement(string $appId): void { |
|
| 427 | + $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
|
| 428 | + if (!in_array($appId, $ignoreMaxApps, true)) { |
|
| 429 | + $ignoreMaxApps[] = $appId; |
|
| 430 | + } |
|
| 431 | + $this->config->setSystemValue('app_install_overwrite', $ignoreMaxApps); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Remove the `max-version` overwrite for this app. |
|
| 436 | + * This means this app now again can not be enabled if the `max-version` is smaller than the current Nextcloud version. |
|
| 437 | + */ |
|
| 438 | + public function removeOverwriteNextcloudRequirement(string $appId): void { |
|
| 439 | + $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
|
| 440 | + $ignoreMaxApps = array_filter($ignoreMaxApps, fn (string $id) => $id !== $appId); |
|
| 441 | + $this->config->setSystemValue('app_install_overwrite', $ignoreMaxApps); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + public function loadApp(string $app): void { |
|
| 445 | + if (isset($this->loadedApps[$app])) { |
|
| 446 | + return; |
|
| 447 | + } |
|
| 448 | + $this->loadedApps[$app] = true; |
|
| 449 | + $appPath = \OC_App::getAppPath($app); |
|
| 450 | + if ($appPath === false) { |
|
| 451 | + return; |
|
| 452 | + } |
|
| 453 | + $eventLogger = \OC::$server->get(IEventLogger::class); |
|
| 454 | + $eventLogger->start("bootstrap:load_app:$app", "Load app: $app"); |
|
| 455 | + |
|
| 456 | + // in case someone calls loadApp() directly |
|
| 457 | + \OC_App::registerAutoloading($app, $appPath); |
|
| 458 | + |
|
| 459 | + if (is_file($appPath . '/appinfo/app.php')) { |
|
| 460 | + $this->logger->error('/appinfo/app.php is not supported anymore, use \OCP\AppFramework\Bootstrap\IBootstrap on the application class instead.', [ |
|
| 461 | + 'app' => $app, |
|
| 462 | + ]); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + $coordinator = Server::get(Coordinator::class); |
|
| 466 | + $coordinator->bootApp($app); |
|
| 467 | + |
|
| 468 | + $eventLogger->start("bootstrap:load_app:$app:info", "Load info.xml for $app and register any services defined in it"); |
|
| 469 | + $info = $this->getAppInfo($app); |
|
| 470 | + if (!empty($info['activity'])) { |
|
| 471 | + $activityManager = \OC::$server->get(IActivityManager::class); |
|
| 472 | + if (!empty($info['activity']['filters'])) { |
|
| 473 | + foreach ($info['activity']['filters'] as $filter) { |
|
| 474 | + $activityManager->registerFilter($filter); |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + if (!empty($info['activity']['settings'])) { |
|
| 478 | + foreach ($info['activity']['settings'] as $setting) { |
|
| 479 | + $activityManager->registerSetting($setting); |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + if (!empty($info['activity']['providers'])) { |
|
| 483 | + foreach ($info['activity']['providers'] as $provider) { |
|
| 484 | + $activityManager->registerProvider($provider); |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + if (!empty($info['settings'])) { |
|
| 490 | + $settingsManager = \OC::$server->get(ISettingsManager::class); |
|
| 491 | + if (!empty($info['settings']['admin'])) { |
|
| 492 | + foreach ($info['settings']['admin'] as $setting) { |
|
| 493 | + $settingsManager->registerSetting('admin', $setting); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + if (!empty($info['settings']['admin-section'])) { |
|
| 497 | + foreach ($info['settings']['admin-section'] as $section) { |
|
| 498 | + $settingsManager->registerSection('admin', $section); |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + if (!empty($info['settings']['personal'])) { |
|
| 502 | + foreach ($info['settings']['personal'] as $setting) { |
|
| 503 | + $settingsManager->registerSetting('personal', $setting); |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + if (!empty($info['settings']['personal-section'])) { |
|
| 507 | + foreach ($info['settings']['personal-section'] as $section) { |
|
| 508 | + $settingsManager->registerSection('personal', $section); |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + if (!empty($info['collaboration']['plugins'])) { |
|
| 514 | + // deal with one or many plugin entries |
|
| 515 | + $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 516 | + [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 517 | + $collaboratorSearch = null; |
|
| 518 | + $autoCompleteManager = null; |
|
| 519 | + foreach ($plugins as $plugin) { |
|
| 520 | + if ($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 521 | + $pluginInfo = [ |
|
| 522 | + 'shareType' => $plugin['@attributes']['share-type'], |
|
| 523 | + 'class' => $plugin['@value'], |
|
| 524 | + ]; |
|
| 525 | + $collaboratorSearch ??= \OC::$server->get(ICollaboratorSearch::class); |
|
| 526 | + $collaboratorSearch->registerPlugin($pluginInfo); |
|
| 527 | + } elseif ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 528 | + $autoCompleteManager ??= \OC::$server->get(IAutoCompleteManager::class); |
|
| 529 | + $autoCompleteManager->registerSorter($plugin['@value']); |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + $eventLogger->end("bootstrap:load_app:$app:info"); |
|
| 534 | + |
|
| 535 | + $eventLogger->end("bootstrap:load_app:$app"); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Check if an app is loaded |
|
| 540 | + * @param string $app app id |
|
| 541 | + * @since 26.0.0 |
|
| 542 | + */ |
|
| 543 | + public function isAppLoaded(string $app): bool { |
|
| 544 | + return isset($this->loadedApps[$app]); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Enable an app for every user |
|
| 549 | + * |
|
| 550 | + * @param string $appId |
|
| 551 | + * @param bool $forceEnable |
|
| 552 | + * @throws AppPathNotFoundException |
|
| 553 | + * @throws \InvalidArgumentException if the application is not installed yet |
|
| 554 | + */ |
|
| 555 | + public function enableApp(string $appId, bool $forceEnable = false): void { |
|
| 556 | + // Check if app exists |
|
| 557 | + $this->getAppPath($appId); |
|
| 558 | + |
|
| 559 | + if ($this->config->getAppValue($appId, 'installed_version', '') === '') { |
|
| 560 | + throw new \InvalidArgumentException("$appId is not installed, cannot be enabled."); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + if ($forceEnable) { |
|
| 564 | + $this->overwriteNextcloudRequirement($appId); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + $this->enabledAppsCache[$appId] = 'yes'; |
|
| 568 | + $this->getAppConfig()->setValue($appId, 'enabled', 'yes'); |
|
| 569 | + $this->dispatcher->dispatchTyped(new AppEnableEvent($appId)); |
|
| 570 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent( |
|
| 571 | + ManagerEvent::EVENT_APP_ENABLE, $appId |
|
| 572 | + )); |
|
| 573 | + $this->clearAppsCache(); |
|
| 574 | + |
|
| 575 | + $this->configManager->migrateConfigLexiconKeys($appId); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Whether a list of types contains a protected app type |
|
| 580 | + * |
|
| 581 | + * @param string[] $types |
|
| 582 | + * @return bool |
|
| 583 | + */ |
|
| 584 | + public function hasProtectedAppType($types) { |
|
| 585 | + if (empty($types)) { |
|
| 586 | + return false; |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $protectedTypes = array_intersect($this->protectedAppTypes, $types); |
|
| 590 | + return !empty($protectedTypes); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Enable an app only for specific groups |
|
| 595 | + * |
|
| 596 | + * @param string $appId |
|
| 597 | + * @param IGroup[] $groups |
|
| 598 | + * @param bool $forceEnable |
|
| 599 | + * @throws \InvalidArgumentException if app can't be enabled for groups |
|
| 600 | + * @throws AppPathNotFoundException |
|
| 601 | + */ |
|
| 602 | + public function enableAppForGroups(string $appId, array $groups, bool $forceEnable = false): void { |
|
| 603 | + // Check if app exists |
|
| 604 | + $this->getAppPath($appId); |
|
| 605 | + |
|
| 606 | + $info = $this->getAppInfo($appId); |
|
| 607 | + if (!empty($info['types']) && $this->hasProtectedAppType($info['types'])) { |
|
| 608 | + throw new \InvalidArgumentException("$appId can't be enabled for groups."); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + if ($this->config->getAppValue($appId, 'installed_version', '') === '') { |
|
| 612 | + throw new \InvalidArgumentException("$appId is not installed, cannot be enabled."); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + if ($forceEnable) { |
|
| 616 | + $this->overwriteNextcloudRequirement($appId); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** @var string[] $groupIds */ |
|
| 620 | + $groupIds = array_map(function ($group) { |
|
| 621 | + /** @var IGroup $group */ |
|
| 622 | + return ($group instanceof IGroup) |
|
| 623 | + ? $group->getGID() |
|
| 624 | + : $group; |
|
| 625 | + }, $groups); |
|
| 626 | + |
|
| 627 | + $this->enabledAppsCache[$appId] = json_encode($groupIds); |
|
| 628 | + $this->getAppConfig()->setValue($appId, 'enabled', json_encode($groupIds)); |
|
| 629 | + $this->dispatcher->dispatchTyped(new AppEnableEvent($appId, $groupIds)); |
|
| 630 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent( |
|
| 631 | + ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups |
|
| 632 | + )); |
|
| 633 | + $this->clearAppsCache(); |
|
| 634 | + |
|
| 635 | + $this->configManager->migrateConfigLexiconKeys($appId); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Disable an app for every user |
|
| 640 | + * |
|
| 641 | + * @param string $appId |
|
| 642 | + * @param bool $automaticDisabled |
|
| 643 | + * @throws \Exception if app can't be disabled |
|
| 644 | + */ |
|
| 645 | + public function disableApp($appId, $automaticDisabled = false): void { |
|
| 646 | + if ($this->isAlwaysEnabled($appId)) { |
|
| 647 | + throw new \Exception("$appId can't be disabled."); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + if ($automaticDisabled) { |
|
| 651 | + $previousSetting = $this->getAppConfig()->getValue($appId, 'enabled', 'yes'); |
|
| 652 | + if ($previousSetting !== 'yes' && $previousSetting !== 'no') { |
|
| 653 | + $previousSetting = json_decode($previousSetting, true); |
|
| 654 | + } |
|
| 655 | + $this->autoDisabledApps[$appId] = $previousSetting; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + unset($this->enabledAppsCache[$appId]); |
|
| 659 | + $this->getAppConfig()->setValue($appId, 'enabled', 'no'); |
|
| 660 | + |
|
| 661 | + // run uninstall steps |
|
| 662 | + $appData = $this->getAppInfo($appId); |
|
| 663 | + if (!is_null($appData)) { |
|
| 664 | + \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']); |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + $this->dispatcher->dispatchTyped(new AppDisableEvent($appId)); |
|
| 668 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent( |
|
| 669 | + ManagerEvent::EVENT_APP_DISABLE, $appId |
|
| 670 | + )); |
|
| 671 | + $this->clearAppsCache(); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Get the directory for the given app. |
|
| 676 | + * |
|
| 677 | + * @throws AppPathNotFoundException if app folder can't be found |
|
| 678 | + */ |
|
| 679 | + public function getAppPath(string $appId): string { |
|
| 680 | + $appPath = \OC_App::getAppPath($appId); |
|
| 681 | + if ($appPath === false) { |
|
| 682 | + throw new AppPathNotFoundException('Could not find path for ' . $appId); |
|
| 683 | + } |
|
| 684 | + return $appPath; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Get the web path for the given app. |
|
| 689 | + * |
|
| 690 | + * @param string $appId |
|
| 691 | + * @return string |
|
| 692 | + * @throws AppPathNotFoundException if app path can't be found |
|
| 693 | + */ |
|
| 694 | + public function getAppWebPath(string $appId): string { |
|
| 695 | + $appWebPath = \OC_App::getAppWebPath($appId); |
|
| 696 | + if ($appWebPath === false) { |
|
| 697 | + throw new AppPathNotFoundException('Could not find web path for ' . $appId); |
|
| 698 | + } |
|
| 699 | + return $appWebPath; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Clear the cached list of apps when enabling/disabling an app |
|
| 704 | + */ |
|
| 705 | + public function clearAppsCache(): void { |
|
| 706 | + $this->appInfos = []; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Returns a list of apps that need upgrade |
|
| 711 | + * |
|
| 712 | + * @param string $version Nextcloud version as array of version components |
|
| 713 | + * @return array list of app info from apps that need an upgrade |
|
| 714 | + * |
|
| 715 | + * @internal |
|
| 716 | + */ |
|
| 717 | + public function getAppsNeedingUpgrade($version) { |
|
| 718 | + $appsToUpgrade = []; |
|
| 719 | + $apps = $this->getEnabledApps(); |
|
| 720 | + foreach ($apps as $appId) { |
|
| 721 | + $appInfo = $this->getAppInfo($appId); |
|
| 722 | + $appDbVersion = $this->getAppConfig()->getValue($appId, 'installed_version'); |
|
| 723 | + if ($appDbVersion |
|
| 724 | + && isset($appInfo['version']) |
|
| 725 | + && version_compare($appInfo['version'], $appDbVersion, '>') |
|
| 726 | + && \OC_App::isAppCompatible($version, $appInfo) |
|
| 727 | + ) { |
|
| 728 | + $appsToUpgrade[] = $appInfo; |
|
| 729 | + } |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + return $appsToUpgrade; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * Returns the app information from "appinfo/info.xml". |
|
| 737 | + * |
|
| 738 | + * @param string|null $lang |
|
| 739 | + * @return array|null app info |
|
| 740 | + */ |
|
| 741 | + public function getAppInfo(string $appId, bool $path = false, $lang = null) { |
|
| 742 | + if ($path) { |
|
| 743 | + throw new \InvalidArgumentException('Calling IAppManager::getAppInfo() with a path is no longer supported. Please call IAppManager::getAppInfoByPath() instead and verify that the path is good before calling.'); |
|
| 744 | + } |
|
| 745 | + if ($lang === null && isset($this->appInfos[$appId])) { |
|
| 746 | + return $this->appInfos[$appId]; |
|
| 747 | + } |
|
| 748 | + try { |
|
| 749 | + $appPath = $this->getAppPath($appId); |
|
| 750 | + } catch (AppPathNotFoundException) { |
|
| 751 | + return null; |
|
| 752 | + } |
|
| 753 | + $file = $appPath . '/appinfo/info.xml'; |
|
| 754 | + |
|
| 755 | + $data = $this->getAppInfoByPath($file, $lang); |
|
| 756 | + |
|
| 757 | + if ($lang === null) { |
|
| 758 | + $this->appInfos[$appId] = $data; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + return $data; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + public function getAppInfoByPath(string $path, ?string $lang = null): ?array { |
|
| 765 | + if (!str_ends_with($path, '/appinfo/info.xml')) { |
|
| 766 | + return null; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + $parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo')); |
|
| 770 | + $data = $parser->parse($path); |
|
| 771 | + |
|
| 772 | + if (is_array($data)) { |
|
| 773 | + $data = \OC_App::parseAppInfo($data, $lang); |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + return $data; |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + public function getAppVersion(string $appId, bool $useCache = true): string { |
|
| 780 | + if (!$useCache || !isset($this->appVersions[$appId])) { |
|
| 781 | + if ($appId === 'core') { |
|
| 782 | + $this->appVersions[$appId] = $this->serverVersion->getVersionString(); |
|
| 783 | + } else { |
|
| 784 | + $appInfo = $this->getAppInfo($appId); |
|
| 785 | + $this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0'; |
|
| 786 | + } |
|
| 787 | + } |
|
| 788 | + return $this->appVersions[$appId]; |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Returns the installed versions of all apps |
|
| 793 | + * |
|
| 794 | + * @return array<string, string> |
|
| 795 | + */ |
|
| 796 | + public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 797 | + return $this->getAppConfig()->getAppInstalledVersions($onlyEnabled); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * Returns a list of apps incompatible with the given version |
|
| 802 | + * |
|
| 803 | + * @param string $version Nextcloud version as array of version components |
|
| 804 | + * |
|
| 805 | + * @return array list of app info from incompatible apps |
|
| 806 | + * |
|
| 807 | + * @internal |
|
| 808 | + */ |
|
| 809 | + public function getIncompatibleApps(string $version): array { |
|
| 810 | + $apps = $this->getEnabledApps(); |
|
| 811 | + $incompatibleApps = []; |
|
| 812 | + foreach ($apps as $appId) { |
|
| 813 | + $info = $this->getAppInfo($appId); |
|
| 814 | + if ($info === null) { |
|
| 815 | + $incompatibleApps[] = ['id' => $appId, 'name' => $appId]; |
|
| 816 | + } elseif (!\OC_App::isAppCompatible($version, $info)) { |
|
| 817 | + $incompatibleApps[] = $info; |
|
| 818 | + } |
|
| 819 | + } |
|
| 820 | + return $incompatibleApps; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * @inheritdoc |
|
| 825 | + * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped() |
|
| 826 | + */ |
|
| 827 | + public function isShipped($appId) { |
|
| 828 | + $this->loadShippedJson(); |
|
| 829 | + return in_array($appId, $this->shippedApps, true); |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + private function isAlwaysEnabled(string $appId): bool { |
|
| 833 | + if ($appId === 'core') { |
|
| 834 | + return true; |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + $alwaysEnabled = $this->getAlwaysEnabledApps(); |
|
| 838 | + return in_array($appId, $alwaysEnabled, true); |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + /** |
|
| 842 | + * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson() |
|
| 843 | + * @throws \Exception |
|
| 844 | + */ |
|
| 845 | + private function loadShippedJson(): void { |
|
| 846 | + if ($this->shippedApps === null) { |
|
| 847 | + $shippedJson = \OC::$SERVERROOT . '/core/shipped.json'; |
|
| 848 | + if (!file_exists($shippedJson)) { |
|
| 849 | + throw new \Exception("File not found: $shippedJson"); |
|
| 850 | + } |
|
| 851 | + $content = json_decode(file_get_contents($shippedJson), true); |
|
| 852 | + $this->shippedApps = $content['shippedApps']; |
|
| 853 | + $this->alwaysEnabled = $content['alwaysEnabled']; |
|
| 854 | + $this->defaultEnabled = $content['defaultEnabled']; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * @inheritdoc |
|
| 860 | + */ |
|
| 861 | + public function getAlwaysEnabledApps() { |
|
| 862 | + $this->loadShippedJson(); |
|
| 863 | + return $this->alwaysEnabled; |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * @inheritdoc |
|
| 868 | + */ |
|
| 869 | + public function isDefaultEnabled(string $appId): bool { |
|
| 870 | + return (in_array($appId, $this->getDefaultEnabledApps())); |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * @inheritdoc |
|
| 875 | + */ |
|
| 876 | + public function getDefaultEnabledApps(): array { |
|
| 877 | + $this->loadShippedJson(); |
|
| 878 | + |
|
| 879 | + return $this->defaultEnabled; |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * @inheritdoc |
|
| 884 | + */ |
|
| 885 | + public function getDefaultAppForUser(?IUser $user = null, bool $withFallbacks = true): string { |
|
| 886 | + $id = $this->getNavigationManager()->getDefaultEntryIdForUser($user, $withFallbacks); |
|
| 887 | + $entry = $this->getNavigationManager()->get($id); |
|
| 888 | + return (string)$entry['app']; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * @inheritdoc |
|
| 893 | + */ |
|
| 894 | + public function getDefaultApps(): array { |
|
| 895 | + $ids = $this->getNavigationManager()->getDefaultEntryIds(); |
|
| 896 | + |
|
| 897 | + return array_values(array_unique(array_map(function (string $id) { |
|
| 898 | + $entry = $this->getNavigationManager()->get($id); |
|
| 899 | + return (string)$entry['app']; |
|
| 900 | + }, $ids))); |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + /** |
|
| 904 | + * @inheritdoc |
|
| 905 | + */ |
|
| 906 | + public function setDefaultApps(array $defaultApps): void { |
|
| 907 | + $entries = $this->getNavigationManager()->getAll(); |
|
| 908 | + $ids = []; |
|
| 909 | + foreach ($defaultApps as $defaultApp) { |
|
| 910 | + foreach ($entries as $entry) { |
|
| 911 | + if ((string)$entry['app'] === $defaultApp) { |
|
| 912 | + $ids[] = (string)$entry['id']; |
|
| 913 | + break; |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + } |
|
| 917 | + $this->getNavigationManager()->setDefaultEntryIds($ids); |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + public function isBackendRequired(string $backend): bool { |
|
| 921 | + foreach ($this->appInfos as $appInfo) { |
|
| 922 | + foreach ($appInfo['dependencies']['backend'] as $appBackend) { |
|
| 923 | + if ($backend === $appBackend) { |
|
| 924 | + return true; |
|
| 925 | + } |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + return false; |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * Clean the appId from forbidden characters |
|
| 934 | + * |
|
| 935 | + * @psalm-taint-escape callable |
|
| 936 | + * @psalm-taint-escape cookie |
|
| 937 | + * @psalm-taint-escape file |
|
| 938 | + * @psalm-taint-escape has_quotes |
|
| 939 | + * @psalm-taint-escape header |
|
| 940 | + * @psalm-taint-escape html |
|
| 941 | + * @psalm-taint-escape include |
|
| 942 | + * @psalm-taint-escape ldap |
|
| 943 | + * @psalm-taint-escape shell |
|
| 944 | + * @psalm-taint-escape sql |
|
| 945 | + * @psalm-taint-escape unserialize |
|
| 946 | + */ |
|
| 947 | + public function cleanAppId(string $app): string { |
|
| 948 | + /* Only lowercase alphanumeric is allowed */ |
|
| 949 | + return preg_replace('/(^[0-9_]|[^a-z0-9_]+|_$)/', '', $app); |
|
| 950 | + } |
|
| 951 | 951 | } |
@@ -48,1690 +48,1690 @@ |
||
| 48 | 48 | * @since 29.0.0 - Supporting types and lazy loading |
| 49 | 49 | */ |
| 50 | 50 | class AppConfig implements IAppConfig { |
| 51 | - private const APP_MAX_LENGTH = 32; |
|
| 52 | - private const KEY_MAX_LENGTH = 64; |
|
| 53 | - private const ENCRYPTION_PREFIX = '$AppConfigEncryption$'; |
|
| 54 | - private const ENCRYPTION_PREFIX_LENGTH = 21; // strlen(self::ENCRYPTION_PREFIX) |
|
| 55 | - |
|
| 56 | - /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 57 | - private array $fastCache = []; // cache for normal config keys |
|
| 58 | - /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 59 | - private array $lazyCache = []; // cache for lazy config keys |
|
| 60 | - /** @var array<string, array<string, int>> ['app_id' => ['config_key' => bitflag]] */ |
|
| 61 | - private array $valueTypes = []; // type for all config values |
|
| 62 | - private bool $fastLoaded = false; |
|
| 63 | - private bool $lazyLoaded = false; |
|
| 64 | - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 65 | - private array $configLexiconDetails = []; |
|
| 66 | - private bool $ignoreLexiconAliases = false; |
|
| 67 | - |
|
| 68 | - /** @var ?array<string, string> */ |
|
| 69 | - private ?array $appVersionsCache = null; |
|
| 70 | - |
|
| 71 | - public function __construct( |
|
| 72 | - protected IDBConnection $connection, |
|
| 73 | - protected LoggerInterface $logger, |
|
| 74 | - protected ICrypto $crypto, |
|
| 75 | - ) { |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @inheritDoc |
|
| 80 | - * |
|
| 81 | - * @return list<string> list of app ids |
|
| 82 | - * @since 7.0.0 |
|
| 83 | - */ |
|
| 84 | - public function getApps(): array { |
|
| 85 | - $this->loadConfigAll(); |
|
| 86 | - $apps = array_merge(array_keys($this->fastCache), array_keys($this->lazyCache)); |
|
| 87 | - sort($apps); |
|
| 88 | - |
|
| 89 | - return array_values(array_unique($apps)); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @inheritDoc |
|
| 94 | - * |
|
| 95 | - * @param string $app id of the app |
|
| 96 | - * |
|
| 97 | - * @return list<string> list of stored config keys |
|
| 98 | - * @since 29.0.0 |
|
| 99 | - */ |
|
| 100 | - public function getKeys(string $app): array { |
|
| 101 | - $this->assertParams($app); |
|
| 102 | - $this->loadConfigAll($app); |
|
| 103 | - $keys = array_merge(array_keys($this->fastCache[$app] ?? []), array_keys($this->lazyCache[$app] ?? [])); |
|
| 104 | - sort($keys); |
|
| 105 | - |
|
| 106 | - return array_values(array_unique($keys)); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @inheritDoc |
|
| 111 | - * |
|
| 112 | - * @param string $app id of the app |
|
| 113 | - * @param string $key config key |
|
| 114 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 115 | - * |
|
| 116 | - * @return bool TRUE if key exists |
|
| 117 | - * @since 7.0.0 |
|
| 118 | - * @since 29.0.0 Added the $lazy argument |
|
| 119 | - */ |
|
| 120 | - public function hasKey(string $app, string $key, ?bool $lazy = false): bool { |
|
| 121 | - $this->assertParams($app, $key); |
|
| 122 | - $this->loadConfig($app, $lazy); |
|
| 123 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 124 | - |
|
| 125 | - if ($lazy === null) { |
|
| 126 | - $appCache = $this->getAllValues($app); |
|
| 127 | - return isset($appCache[$key]); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($lazy) { |
|
| 131 | - return isset($this->lazyCache[$app][$key]); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return isset($this->fastCache[$app][$key]); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param string $app id of the app |
|
| 139 | - * @param string $key config key |
|
| 140 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 141 | - * |
|
| 142 | - * @return bool |
|
| 143 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 144 | - * @since 29.0.0 |
|
| 145 | - */ |
|
| 146 | - public function isSensitive(string $app, string $key, ?bool $lazy = false): bool { |
|
| 147 | - $this->assertParams($app, $key); |
|
| 148 | - $this->loadConfig(null, $lazy); |
|
| 149 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 150 | - |
|
| 151 | - if (!isset($this->valueTypes[$app][$key])) { |
|
| 152 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key]); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @inheritDoc |
|
| 160 | - * |
|
| 161 | - * @param string $app if of the app |
|
| 162 | - * @param string $key config key |
|
| 163 | - * |
|
| 164 | - * @return bool TRUE if config is lazy loaded |
|
| 165 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 166 | - * @see IAppConfig for details about lazy loading |
|
| 167 | - * @since 29.0.0 |
|
| 168 | - */ |
|
| 169 | - public function isLazy(string $app, string $key): bool { |
|
| 170 | - $this->assertParams($app, $key); |
|
| 171 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 172 | - |
|
| 173 | - // there is a huge probability the non-lazy config are already loaded |
|
| 174 | - if ($this->hasKey($app, $key, false)) { |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // key not found, we search in the lazy config |
|
| 179 | - if ($this->hasKey($app, $key, true)) { |
|
| 180 | - return true; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @inheritDoc |
|
| 189 | - * |
|
| 190 | - * @param string $app id of the app |
|
| 191 | - * @param string $prefix config keys prefix to search |
|
| 192 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 193 | - * |
|
| 194 | - * @return array<string, string|int|float|bool|array> [configKey => configValue] |
|
| 195 | - * @since 29.0.0 |
|
| 196 | - */ |
|
| 197 | - public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array { |
|
| 198 | - $this->assertParams($app, $prefix); |
|
| 199 | - // if we want to filter values, we need to get sensitivity |
|
| 200 | - $this->loadConfigAll($app); |
|
| 201 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 202 | - $values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])); |
|
| 203 | - $values = array_filter( |
|
| 204 | - $values, |
|
| 205 | - function (string $key) use ($prefix): bool { |
|
| 206 | - return str_starts_with($key, $prefix); // filter values based on $prefix |
|
| 207 | - }, ARRAY_FILTER_USE_KEY |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - if (!$filtered) { |
|
| 211 | - return $values; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Using the old (deprecated) list of sensitive values. |
|
| 216 | - */ |
|
| 217 | - foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) { |
|
| 218 | - $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 219 | - foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 220 | - $this->valueTypes[$app][$sensitiveKey] = ($this->valueTypes[$app][$sensitiveKey] ?? 0) | self::VALUE_SENSITIVE; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $result = []; |
|
| 225 | - foreach ($values as $key => $value) { |
|
| 226 | - $result[$key] = $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key] ?? 0) ? IConfig::SENSITIVE_VALUE : $value; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return $result; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @inheritDoc |
|
| 234 | - * |
|
| 235 | - * @param string $key config key |
|
| 236 | - * @param bool $lazy search within lazy loaded config |
|
| 237 | - * @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others) |
|
| 238 | - * |
|
| 239 | - * @return array<string, string|int|float|bool|array> [appId => configValue] |
|
| 240 | - * @since 29.0.0 |
|
| 241 | - */ |
|
| 242 | - public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array { |
|
| 243 | - $this->assertParams('', $key, true); |
|
| 244 | - $this->loadConfig(null, $lazy); |
|
| 245 | - |
|
| 246 | - /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 247 | - if ($lazy) { |
|
| 248 | - $cache = $this->lazyCache; |
|
| 249 | - } else { |
|
| 250 | - $cache = $this->fastCache; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $values = []; |
|
| 254 | - foreach (array_keys($cache) as $app) { |
|
| 255 | - if (isset($cache[$app][$key])) { |
|
| 256 | - $values[$app] = $this->convertTypedValue($cache[$app][$key], $typedAs ?? $this->getValueType((string)$app, $key, $lazy)); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return $values; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Get the config value as string. |
|
| 266 | - * If the value does not exist the given default will be returned. |
|
| 267 | - * |
|
| 268 | - * Set lazy to `null` to ignore it and get the value from either source. |
|
| 269 | - * |
|
| 270 | - * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 271 | - * |
|
| 272 | - * @param string $app id of the app |
|
| 273 | - * @param string $key config key |
|
| 274 | - * @param string $default config value |
|
| 275 | - * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 276 | - * |
|
| 277 | - * @return string the value or $default |
|
| 278 | - * @internal |
|
| 279 | - * @since 29.0.0 |
|
| 280 | - * @see IAppConfig for explanation about lazy loading |
|
| 281 | - * @see getValueString() |
|
| 282 | - * @see getValueInt() |
|
| 283 | - * @see getValueFloat() |
|
| 284 | - * @see getValueBool() |
|
| 285 | - * @see getValueArray() |
|
| 286 | - */ |
|
| 287 | - public function getValueMixed( |
|
| 288 | - string $app, |
|
| 289 | - string $key, |
|
| 290 | - string $default = '', |
|
| 291 | - ?bool $lazy = false, |
|
| 292 | - ): string { |
|
| 293 | - try { |
|
| 294 | - $lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy; |
|
| 295 | - } catch (AppConfigUnknownKeyException) { |
|
| 296 | - return $default; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - return $this->getTypedValue( |
|
| 300 | - $app, |
|
| 301 | - $key, |
|
| 302 | - $default, |
|
| 303 | - $lazy, |
|
| 304 | - self::VALUE_MIXED |
|
| 305 | - ); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @inheritDoc |
|
| 310 | - * |
|
| 311 | - * @param string $app id of the app |
|
| 312 | - * @param string $key config key |
|
| 313 | - * @param string $default default value |
|
| 314 | - * @param bool $lazy search within lazy loaded config |
|
| 315 | - * |
|
| 316 | - * @return string stored config value or $default if not set in database |
|
| 317 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 318 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 319 | - * @since 29.0.0 |
|
| 320 | - * @see IAppConfig for explanation about lazy loading |
|
| 321 | - */ |
|
| 322 | - public function getValueString( |
|
| 323 | - string $app, |
|
| 324 | - string $key, |
|
| 325 | - string $default = '', |
|
| 326 | - bool $lazy = false, |
|
| 327 | - ): string { |
|
| 328 | - return $this->getTypedValue($app, $key, $default, $lazy, self::VALUE_STRING); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @inheritDoc |
|
| 333 | - * |
|
| 334 | - * @param string $app id of the app |
|
| 335 | - * @param string $key config key |
|
| 336 | - * @param int $default default value |
|
| 337 | - * @param bool $lazy search within lazy loaded config |
|
| 338 | - * |
|
| 339 | - * @return int stored config value or $default if not set in database |
|
| 340 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 341 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 342 | - * @since 29.0.0 |
|
| 343 | - * @see IAppConfig for explanation about lazy loading |
|
| 344 | - */ |
|
| 345 | - public function getValueInt( |
|
| 346 | - string $app, |
|
| 347 | - string $key, |
|
| 348 | - int $default = 0, |
|
| 349 | - bool $lazy = false, |
|
| 350 | - ): int { |
|
| 351 | - return (int)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_INT); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * @inheritDoc |
|
| 356 | - * |
|
| 357 | - * @param string $app id of the app |
|
| 358 | - * @param string $key config key |
|
| 359 | - * @param float $default default value |
|
| 360 | - * @param bool $lazy search within lazy loaded config |
|
| 361 | - * |
|
| 362 | - * @return float stored config value or $default if not set in database |
|
| 363 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 364 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 365 | - * @since 29.0.0 |
|
| 366 | - * @see IAppConfig for explanation about lazy loading |
|
| 367 | - */ |
|
| 368 | - public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { |
|
| 369 | - return (float)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_FLOAT); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @inheritDoc |
|
| 374 | - * |
|
| 375 | - * @param string $app id of the app |
|
| 376 | - * @param string $key config key |
|
| 377 | - * @param bool $default default value |
|
| 378 | - * @param bool $lazy search within lazy loaded config |
|
| 379 | - * |
|
| 380 | - * @return bool stored config value or $default if not set in database |
|
| 381 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 382 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 383 | - * @since 29.0.0 |
|
| 384 | - * @see IAppConfig for explanation about lazy loading |
|
| 385 | - */ |
|
| 386 | - public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { |
|
| 387 | - $b = strtolower($this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL)); |
|
| 388 | - return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * @inheritDoc |
|
| 393 | - * |
|
| 394 | - * @param string $app id of the app |
|
| 395 | - * @param string $key config key |
|
| 396 | - * @param array $default default value |
|
| 397 | - * @param bool $lazy search within lazy loaded config |
|
| 398 | - * |
|
| 399 | - * @return array stored config value or $default if not set in database |
|
| 400 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 401 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 402 | - * @since 29.0.0 |
|
| 403 | - * @see IAppConfig for explanation about lazy loading |
|
| 404 | - */ |
|
| 405 | - public function getValueArray( |
|
| 406 | - string $app, |
|
| 407 | - string $key, |
|
| 408 | - array $default = [], |
|
| 409 | - bool $lazy = false, |
|
| 410 | - ): array { |
|
| 411 | - try { |
|
| 412 | - $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 413 | - $value = json_decode($this->getTypedValue($app, $key, $defaultJson, $lazy, self::VALUE_ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 414 | - |
|
| 415 | - return is_array($value) ? $value : []; |
|
| 416 | - } catch (JsonException) { |
|
| 417 | - return []; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * @param string $app id of the app |
|
| 423 | - * @param string $key config key |
|
| 424 | - * @param string $default default value |
|
| 425 | - * @param bool $lazy search within lazy loaded config |
|
| 426 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT}{@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 427 | - * |
|
| 428 | - * @return string |
|
| 429 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 430 | - * @throws InvalidArgumentException |
|
| 431 | - */ |
|
| 432 | - private function getTypedValue( |
|
| 433 | - string $app, |
|
| 434 | - string $key, |
|
| 435 | - string $default, |
|
| 436 | - bool $lazy, |
|
| 437 | - int $type, |
|
| 438 | - ): string { |
|
| 439 | - $this->assertParams($app, $key, valueType: $type); |
|
| 440 | - $origKey = $key; |
|
| 441 | - if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default)) { |
|
| 442 | - return $default; // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 443 | - } |
|
| 444 | - $this->loadConfig($app, $lazy); |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * We ignore check if mixed type is requested. |
|
| 448 | - * If type of stored value is set as mixed, we don't filter. |
|
| 449 | - * If type of stored value is defined, we compare with the one requested. |
|
| 450 | - */ |
|
| 451 | - $knownType = $this->valueTypes[$app][$key] ?? 0; |
|
| 452 | - if (!$this->isTyped(self::VALUE_MIXED, $type) |
|
| 453 | - && $knownType > 0 |
|
| 454 | - && !$this->isTyped(self::VALUE_MIXED, $knownType) |
|
| 455 | - && !$this->isTyped($type, $knownType)) { |
|
| 456 | - $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 457 | - throw new AppConfigTypeConflictException('conflict with value type from database'); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * - the pair $app/$key cannot exist in both array, |
|
| 462 | - * - we should still return an existing non-lazy value even if current method |
|
| 463 | - * is called with $lazy is true |
|
| 464 | - * |
|
| 465 | - * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 466 | - */ |
|
| 467 | - if (isset($this->lazyCache[$app][$key])) { |
|
| 468 | - $value = $this->lazyCache[$app][$key]; |
|
| 469 | - } elseif (isset($this->fastCache[$app][$key])) { |
|
| 470 | - $value = $this->fastCache[$app][$key]; |
|
| 471 | - } else { |
|
| 472 | - return $default; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $knownType); |
|
| 476 | - if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 477 | - // Only decrypt values that are stored encrypted |
|
| 478 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 482 | - // interested to check options in case a modification of the value is needed |
|
| 483 | - // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 484 | - if ($origKey !== $key && $type === self::VALUE_BOOL) { |
|
| 485 | - $configManager = Server::get(ConfigManager::class); |
|
| 486 | - $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - return $value; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * @inheritDoc |
|
| 494 | - * |
|
| 495 | - * @param string $app id of the app |
|
| 496 | - * @param string $key config key |
|
| 497 | - * |
|
| 498 | - * @return int type of the value |
|
| 499 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 500 | - * @since 29.0.0 |
|
| 501 | - * @see VALUE_STRING |
|
| 502 | - * @see VALUE_INT |
|
| 503 | - * @see VALUE_FLOAT |
|
| 504 | - * @see VALUE_BOOL |
|
| 505 | - * @see VALUE_ARRAY |
|
| 506 | - */ |
|
| 507 | - public function getValueType(string $app, string $key, ?bool $lazy = null): int { |
|
| 508 | - $type = self::VALUE_MIXED; |
|
| 509 | - $ignorable = $lazy ?? false; |
|
| 510 | - $this->matchAndApplyLexiconDefinition($app, $key, $ignorable, $type); |
|
| 511 | - if ($type !== self::VALUE_MIXED) { |
|
| 512 | - // a modified $type means config key is set in Lexicon |
|
| 513 | - return $type; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - $this->assertParams($app, $key); |
|
| 517 | - $this->loadConfig($app, $lazy); |
|
| 518 | - |
|
| 519 | - if (!isset($this->valueTypes[$app][$key])) { |
|
| 520 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - $type = $this->valueTypes[$app][$key]; |
|
| 524 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 525 | - return $type; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Store a config key and its value in database as VALUE_MIXED |
|
| 531 | - * |
|
| 532 | - * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 533 | - * |
|
| 534 | - * @param string $app id of the app |
|
| 535 | - * @param string $key config key |
|
| 536 | - * @param string $value config value |
|
| 537 | - * @param bool $lazy set config as lazy loaded |
|
| 538 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 539 | - * |
|
| 540 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 541 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED |
|
| 542 | - * @internal |
|
| 543 | - * @since 29.0.0 |
|
| 544 | - * @see IAppConfig for explanation about lazy loading |
|
| 545 | - * @see setValueString() |
|
| 546 | - * @see setValueInt() |
|
| 547 | - * @see setValueFloat() |
|
| 548 | - * @see setValueBool() |
|
| 549 | - * @see setValueArray() |
|
| 550 | - */ |
|
| 551 | - public function setValueMixed( |
|
| 552 | - string $app, |
|
| 553 | - string $key, |
|
| 554 | - string $value, |
|
| 555 | - bool $lazy = false, |
|
| 556 | - bool $sensitive = false, |
|
| 557 | - ): bool { |
|
| 558 | - return $this->setTypedValue( |
|
| 559 | - $app, |
|
| 560 | - $key, |
|
| 561 | - $value, |
|
| 562 | - $lazy, |
|
| 563 | - self::VALUE_MIXED | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 564 | - ); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * @inheritDoc |
|
| 570 | - * |
|
| 571 | - * @param string $app id of the app |
|
| 572 | - * @param string $key config key |
|
| 573 | - * @param string $value config value |
|
| 574 | - * @param bool $lazy set config as lazy loaded |
|
| 575 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 576 | - * |
|
| 577 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 578 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 579 | - * @since 29.0.0 |
|
| 580 | - * @see IAppConfig for explanation about lazy loading |
|
| 581 | - */ |
|
| 582 | - public function setValueString( |
|
| 583 | - string $app, |
|
| 584 | - string $key, |
|
| 585 | - string $value, |
|
| 586 | - bool $lazy = false, |
|
| 587 | - bool $sensitive = false, |
|
| 588 | - ): bool { |
|
| 589 | - return $this->setTypedValue( |
|
| 590 | - $app, |
|
| 591 | - $key, |
|
| 592 | - $value, |
|
| 593 | - $lazy, |
|
| 594 | - self::VALUE_STRING | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 595 | - ); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * @inheritDoc |
|
| 600 | - * |
|
| 601 | - * @param string $app id of the app |
|
| 602 | - * @param string $key config key |
|
| 603 | - * @param int $value config value |
|
| 604 | - * @param bool $lazy set config as lazy loaded |
|
| 605 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 606 | - * |
|
| 607 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 608 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 609 | - * @since 29.0.0 |
|
| 610 | - * @see IAppConfig for explanation about lazy loading |
|
| 611 | - */ |
|
| 612 | - public function setValueInt( |
|
| 613 | - string $app, |
|
| 614 | - string $key, |
|
| 615 | - int $value, |
|
| 616 | - bool $lazy = false, |
|
| 617 | - bool $sensitive = false, |
|
| 618 | - ): bool { |
|
| 619 | - if ($value > 2000000000) { |
|
| 620 | - $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - return $this->setTypedValue( |
|
| 624 | - $app, |
|
| 625 | - $key, |
|
| 626 | - (string)$value, |
|
| 627 | - $lazy, |
|
| 628 | - self::VALUE_INT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 629 | - ); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * @inheritDoc |
|
| 634 | - * |
|
| 635 | - * @param string $app id of the app |
|
| 636 | - * @param string $key config key |
|
| 637 | - * @param float $value config value |
|
| 638 | - * @param bool $lazy set config as lazy loaded |
|
| 639 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 640 | - * |
|
| 641 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 642 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 643 | - * @since 29.0.0 |
|
| 644 | - * @see IAppConfig for explanation about lazy loading |
|
| 645 | - */ |
|
| 646 | - public function setValueFloat( |
|
| 647 | - string $app, |
|
| 648 | - string $key, |
|
| 649 | - float $value, |
|
| 650 | - bool $lazy = false, |
|
| 651 | - bool $sensitive = false, |
|
| 652 | - ): bool { |
|
| 653 | - return $this->setTypedValue( |
|
| 654 | - $app, |
|
| 655 | - $key, |
|
| 656 | - (string)$value, |
|
| 657 | - $lazy, |
|
| 658 | - self::VALUE_FLOAT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 659 | - ); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * @inheritDoc |
|
| 664 | - * |
|
| 665 | - * @param string $app id of the app |
|
| 666 | - * @param string $key config key |
|
| 667 | - * @param bool $value config value |
|
| 668 | - * @param bool $lazy set config as lazy loaded |
|
| 669 | - * |
|
| 670 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 671 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 672 | - * @since 29.0.0 |
|
| 673 | - * @see IAppConfig for explanation about lazy loading |
|
| 674 | - */ |
|
| 675 | - public function setValueBool( |
|
| 676 | - string $app, |
|
| 677 | - string $key, |
|
| 678 | - bool $value, |
|
| 679 | - bool $lazy = false, |
|
| 680 | - ): bool { |
|
| 681 | - return $this->setTypedValue( |
|
| 682 | - $app, |
|
| 683 | - $key, |
|
| 684 | - ($value) ? '1' : '0', |
|
| 685 | - $lazy, |
|
| 686 | - self::VALUE_BOOL |
|
| 687 | - ); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * @inheritDoc |
|
| 692 | - * |
|
| 693 | - * @param string $app id of the app |
|
| 694 | - * @param string $key config key |
|
| 695 | - * @param array $value config value |
|
| 696 | - * @param bool $lazy set config as lazy loaded |
|
| 697 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 698 | - * |
|
| 699 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 700 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 701 | - * @throws JsonException |
|
| 702 | - * @since 29.0.0 |
|
| 703 | - * @see IAppConfig for explanation about lazy loading |
|
| 704 | - */ |
|
| 705 | - public function setValueArray( |
|
| 706 | - string $app, |
|
| 707 | - string $key, |
|
| 708 | - array $value, |
|
| 709 | - bool $lazy = false, |
|
| 710 | - bool $sensitive = false, |
|
| 711 | - ): bool { |
|
| 712 | - try { |
|
| 713 | - return $this->setTypedValue( |
|
| 714 | - $app, |
|
| 715 | - $key, |
|
| 716 | - json_encode($value, JSON_THROW_ON_ERROR), |
|
| 717 | - $lazy, |
|
| 718 | - self::VALUE_ARRAY | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 719 | - ); |
|
| 720 | - } catch (JsonException $e) { |
|
| 721 | - $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 722 | - throw $e; |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Store a config key and its value in database |
|
| 728 | - * |
|
| 729 | - * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 730 | - * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 731 | - * altered. |
|
| 732 | - * |
|
| 733 | - * @param string $app id of the app |
|
| 734 | - * @param string $key config key |
|
| 735 | - * @param string $value config value |
|
| 736 | - * @param bool $lazy config set as lazy loaded |
|
| 737 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 738 | - * |
|
| 739 | - * @return bool TRUE if value was updated in database |
|
| 740 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 741 | - * @see IAppConfig for explanation about lazy loading |
|
| 742 | - */ |
|
| 743 | - private function setTypedValue( |
|
| 744 | - string $app, |
|
| 745 | - string $key, |
|
| 746 | - string $value, |
|
| 747 | - bool $lazy, |
|
| 748 | - int $type, |
|
| 749 | - ): bool { |
|
| 750 | - $this->assertParams($app, $key); |
|
| 751 | - if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type)) { |
|
| 752 | - return false; // returns false as database is not updated |
|
| 753 | - } |
|
| 754 | - $this->loadConfig(null, $lazy); |
|
| 755 | - |
|
| 756 | - $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $type); |
|
| 757 | - $inserted = $refreshCache = false; |
|
| 758 | - |
|
| 759 | - $origValue = $value; |
|
| 760 | - if ($sensitive || ($this->hasKey($app, $key, $lazy) && $this->isSensitive($app, $key, $lazy))) { |
|
| 761 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - if ($this->hasKey($app, $key, $lazy)) { |
|
| 765 | - /** |
|
| 766 | - * no update if key is already known with set lazy status and value is |
|
| 767 | - * not different, unless sensitivity is switched from false to true. |
|
| 768 | - */ |
|
| 769 | - if ($origValue === $this->getTypedValue($app, $key, $value, $lazy, $type) |
|
| 770 | - && (!$sensitive || $this->isSensitive($app, $key, $lazy))) { |
|
| 771 | - return false; |
|
| 772 | - } |
|
| 773 | - } else { |
|
| 774 | - /** |
|
| 775 | - * if key is not known yet, we try to insert. |
|
| 776 | - * It might fail if the key exists with a different lazy flag. |
|
| 777 | - */ |
|
| 778 | - try { |
|
| 779 | - $insert = $this->connection->getQueryBuilder(); |
|
| 780 | - $insert->insert('appconfig') |
|
| 781 | - ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 782 | - ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 783 | - ->setValue('type', $insert->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 784 | - ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 785 | - ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 786 | - $insert->executeStatement(); |
|
| 787 | - $inserted = true; |
|
| 788 | - } catch (DBException $e) { |
|
| 789 | - if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 790 | - throw $e; // TODO: throw exception or just log and returns false !? |
|
| 791 | - } |
|
| 792 | - } |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * We cannot insert a new row, meaning we need to update an already existing one |
|
| 797 | - */ |
|
| 798 | - if (!$inserted) { |
|
| 799 | - $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 800 | - if ($currType === 0) { // this might happen when switching lazy loading status |
|
| 801 | - $this->loadConfigAll(); |
|
| 802 | - $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * This should only happen during the upgrade process from 28 to 29. |
|
| 807 | - * We only log a warning and set it to VALUE_MIXED. |
|
| 808 | - */ |
|
| 809 | - if ($currType === 0) { |
|
| 810 | - $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); |
|
| 811 | - $currType = self::VALUE_MIXED; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * we only accept a different type from the one stored in database |
|
| 816 | - * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 817 | - */ |
|
| 818 | - if (!$this->isTyped(self::VALUE_MIXED, $currType) && |
|
| 819 | - ($type | self::VALUE_SENSITIVE) !== ($currType | self::VALUE_SENSITIVE)) { |
|
| 820 | - try { |
|
| 821 | - $currType = $this->convertTypeToString($currType); |
|
| 822 | - $type = $this->convertTypeToString($type); |
|
| 823 | - } catch (AppConfigIncorrectTypeException) { |
|
| 824 | - // can be ignored, this was just needed for a better exception message. |
|
| 825 | - } |
|
| 826 | - throw new AppConfigTypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - // we fix $type if the stored value, or the new value as it might be changed, is set as sensitive |
|
| 830 | - if ($sensitive || $this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 831 | - $type |= self::VALUE_SENSITIVE; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - if ($lazy !== $this->isLazy($app, $key)) { |
|
| 835 | - $refreshCache = true; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - $update = $this->connection->getQueryBuilder(); |
|
| 839 | - $update->update('appconfig') |
|
| 840 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 841 | - ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 842 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 843 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 844 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 845 | - |
|
| 846 | - $update->executeStatement(); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - if ($refreshCache) { |
|
| 850 | - $this->clearCache(); |
|
| 851 | - return true; |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - // update local cache |
|
| 855 | - if ($lazy) { |
|
| 856 | - $this->lazyCache[$app][$key] = $value; |
|
| 857 | - } else { |
|
| 858 | - $this->fastCache[$app][$key] = $value; |
|
| 859 | - } |
|
| 860 | - $this->valueTypes[$app][$key] = $type; |
|
| 861 | - |
|
| 862 | - return true; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - /** |
|
| 866 | - * Change the type of config value. |
|
| 867 | - * |
|
| 868 | - * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 869 | - * |
|
| 870 | - * @param string $app id of the app |
|
| 871 | - * @param string $key config key |
|
| 872 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 873 | - * |
|
| 874 | - * @return bool TRUE if database update were necessary |
|
| 875 | - * @throws AppConfigUnknownKeyException if $key is now known in database |
|
| 876 | - * @throws AppConfigIncorrectTypeException if $type is not valid |
|
| 877 | - * @internal |
|
| 878 | - * @since 29.0.0 |
|
| 879 | - */ |
|
| 880 | - public function updateType(string $app, string $key, int $type = self::VALUE_MIXED): bool { |
|
| 881 | - $this->assertParams($app, $key); |
|
| 882 | - $this->loadConfigAll(); |
|
| 883 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 884 | - $this->isLazy($app, $key); // confirm key exists |
|
| 885 | - |
|
| 886 | - // type can only be one type |
|
| 887 | - if (!in_array($type, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 888 | - throw new AppConfigIncorrectTypeException('Unknown value type'); |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - $currType = $this->valueTypes[$app][$key]; |
|
| 892 | - if (($type | self::VALUE_SENSITIVE) === ($currType | self::VALUE_SENSITIVE)) { |
|
| 893 | - return false; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - // we complete with sensitive flag if the stored value is set as sensitive |
|
| 897 | - if ($this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 898 | - $type = $type | self::VALUE_SENSITIVE; |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - $update = $this->connection->getQueryBuilder(); |
|
| 902 | - $update->update('appconfig') |
|
| 903 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 904 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 905 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 906 | - $update->executeStatement(); |
|
| 907 | - $this->valueTypes[$app][$key] = $type; |
|
| 908 | - |
|
| 909 | - return true; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - |
|
| 913 | - /** |
|
| 914 | - * @inheritDoc |
|
| 915 | - * |
|
| 916 | - * @param string $app id of the app |
|
| 917 | - * @param string $key config key |
|
| 918 | - * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 919 | - * |
|
| 920 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 921 | - * @since 29.0.0 |
|
| 922 | - */ |
|
| 923 | - public function updateSensitive(string $app, string $key, bool $sensitive): bool { |
|
| 924 | - $this->assertParams($app, $key); |
|
| 925 | - $this->loadConfigAll(); |
|
| 926 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 927 | - |
|
| 928 | - try { |
|
| 929 | - if ($sensitive === $this->isSensitive($app, $key, null)) { |
|
| 930 | - return false; |
|
| 931 | - } |
|
| 932 | - } catch (AppConfigUnknownKeyException $e) { |
|
| 933 | - return false; |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - $lazy = $this->isLazy($app, $key); |
|
| 937 | - if ($lazy) { |
|
| 938 | - $cache = $this->lazyCache; |
|
| 939 | - } else { |
|
| 940 | - $cache = $this->fastCache; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - if (!isset($cache[$app][$key])) { |
|
| 944 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * type returned by getValueType() is already cleaned from sensitive flag |
|
| 949 | - * we just need to update it based on $sensitive and store it in database |
|
| 950 | - */ |
|
| 951 | - $type = $this->getValueType($app, $key); |
|
| 952 | - $value = $cache[$app][$key]; |
|
| 953 | - if ($sensitive) { |
|
| 954 | - $type |= self::VALUE_SENSITIVE; |
|
| 955 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 956 | - } else { |
|
| 957 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - $update = $this->connection->getQueryBuilder(); |
|
| 961 | - $update->update('appconfig') |
|
| 962 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 963 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 964 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 965 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 966 | - $update->executeStatement(); |
|
| 967 | - |
|
| 968 | - $this->valueTypes[$app][$key] = $type; |
|
| 969 | - |
|
| 970 | - return true; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - /** |
|
| 974 | - * @inheritDoc |
|
| 975 | - * |
|
| 976 | - * @param string $app id of the app |
|
| 977 | - * @param string $key config key |
|
| 978 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 979 | - * |
|
| 980 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 981 | - * @since 29.0.0 |
|
| 982 | - */ |
|
| 983 | - public function updateLazy(string $app, string $key, bool $lazy): bool { |
|
| 984 | - $this->assertParams($app, $key); |
|
| 985 | - $this->loadConfigAll(); |
|
| 986 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 987 | - |
|
| 988 | - try { |
|
| 989 | - if ($lazy === $this->isLazy($app, $key)) { |
|
| 990 | - return false; |
|
| 991 | - } |
|
| 992 | - } catch (AppConfigUnknownKeyException $e) { |
|
| 993 | - return false; |
|
| 994 | - } |
|
| 995 | - |
|
| 996 | - $update = $this->connection->getQueryBuilder(); |
|
| 997 | - $update->update('appconfig') |
|
| 998 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 999 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1000 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1001 | - $update->executeStatement(); |
|
| 1002 | - |
|
| 1003 | - // At this point, it is a lot safer to clean cache |
|
| 1004 | - $this->clearCache(); |
|
| 1005 | - |
|
| 1006 | - return true; |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - /** |
|
| 1010 | - * @inheritDoc |
|
| 1011 | - * |
|
| 1012 | - * @param string $app id of the app |
|
| 1013 | - * @param string $key config key |
|
| 1014 | - * |
|
| 1015 | - * @return array |
|
| 1016 | - * @throws AppConfigUnknownKeyException if config key is not known in database |
|
| 1017 | - * @since 29.0.0 |
|
| 1018 | - */ |
|
| 1019 | - public function getDetails(string $app, string $key): array { |
|
| 1020 | - $this->assertParams($app, $key); |
|
| 1021 | - $this->loadConfigAll(); |
|
| 1022 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1023 | - $lazy = $this->isLazy($app, $key); |
|
| 1024 | - |
|
| 1025 | - if ($lazy) { |
|
| 1026 | - $cache = $this->lazyCache; |
|
| 1027 | - } else { |
|
| 1028 | - $cache = $this->fastCache; |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - $type = $this->getValueType($app, $key); |
|
| 1032 | - try { |
|
| 1033 | - $typeString = $this->convertTypeToString($type); |
|
| 1034 | - } catch (AppConfigIncorrectTypeException $e) { |
|
| 1035 | - $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1036 | - $typeString = (string)$type; |
|
| 1037 | - } |
|
| 1038 | - |
|
| 1039 | - if (!isset($cache[$app][$key])) { |
|
| 1040 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 1041 | - } |
|
| 1042 | - |
|
| 1043 | - $value = $cache[$app][$key]; |
|
| 1044 | - $sensitive = $this->isSensitive($app, $key, null); |
|
| 1045 | - if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1046 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - return [ |
|
| 1050 | - 'app' => $app, |
|
| 1051 | - 'key' => $key, |
|
| 1052 | - 'value' => $value, |
|
| 1053 | - 'type' => $type, |
|
| 1054 | - 'lazy' => $lazy, |
|
| 1055 | - 'typeString' => $typeString, |
|
| 1056 | - 'sensitive' => $sensitive |
|
| 1057 | - ]; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * @param string $type |
|
| 1062 | - * |
|
| 1063 | - * @return int |
|
| 1064 | - * @throws AppConfigIncorrectTypeException |
|
| 1065 | - * @since 29.0.0 |
|
| 1066 | - */ |
|
| 1067 | - public function convertTypeToInt(string $type): int { |
|
| 1068 | - return match (strtolower($type)) { |
|
| 1069 | - 'mixed' => IAppConfig::VALUE_MIXED, |
|
| 1070 | - 'string' => IAppConfig::VALUE_STRING, |
|
| 1071 | - 'integer' => IAppConfig::VALUE_INT, |
|
| 1072 | - 'float' => IAppConfig::VALUE_FLOAT, |
|
| 1073 | - 'boolean' => IAppConfig::VALUE_BOOL, |
|
| 1074 | - 'array' => IAppConfig::VALUE_ARRAY, |
|
| 1075 | - default => throw new AppConfigIncorrectTypeException('Unknown type ' . $type) |
|
| 1076 | - }; |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - /** |
|
| 1080 | - * @param int $type |
|
| 1081 | - * |
|
| 1082 | - * @return string |
|
| 1083 | - * @throws AppConfigIncorrectTypeException |
|
| 1084 | - * @since 29.0.0 |
|
| 1085 | - */ |
|
| 1086 | - public function convertTypeToString(int $type): string { |
|
| 1087 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 1088 | - |
|
| 1089 | - return match ($type) { |
|
| 1090 | - IAppConfig::VALUE_MIXED => 'mixed', |
|
| 1091 | - IAppConfig::VALUE_STRING => 'string', |
|
| 1092 | - IAppConfig::VALUE_INT => 'integer', |
|
| 1093 | - IAppConfig::VALUE_FLOAT => 'float', |
|
| 1094 | - IAppConfig::VALUE_BOOL => 'boolean', |
|
| 1095 | - IAppConfig::VALUE_ARRAY => 'array', |
|
| 1096 | - default => throw new AppConfigIncorrectTypeException('Unknown numeric type ' . $type) |
|
| 1097 | - }; |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - /** |
|
| 1101 | - * @inheritDoc |
|
| 1102 | - * |
|
| 1103 | - * @param string $app id of the app |
|
| 1104 | - * @param string $key config key |
|
| 1105 | - * |
|
| 1106 | - * @since 29.0.0 |
|
| 1107 | - */ |
|
| 1108 | - public function deleteKey(string $app, string $key): void { |
|
| 1109 | - $this->assertParams($app, $key); |
|
| 1110 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1111 | - |
|
| 1112 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1113 | - $qb->delete('appconfig') |
|
| 1114 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1115 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1116 | - $qb->executeStatement(); |
|
| 1117 | - |
|
| 1118 | - unset($this->lazyCache[$app][$key]); |
|
| 1119 | - unset($this->fastCache[$app][$key]); |
|
| 1120 | - unset($this->valueTypes[$app][$key]); |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - /** |
|
| 1124 | - * @inheritDoc |
|
| 1125 | - * |
|
| 1126 | - * @param string $app id of the app |
|
| 1127 | - * |
|
| 1128 | - * @since 29.0.0 |
|
| 1129 | - */ |
|
| 1130 | - public function deleteApp(string $app): void { |
|
| 1131 | - $this->assertParams($app); |
|
| 1132 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1133 | - $qb->delete('appconfig') |
|
| 1134 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1135 | - $qb->executeStatement(); |
|
| 1136 | - |
|
| 1137 | - $this->clearCache(); |
|
| 1138 | - } |
|
| 1139 | - |
|
| 1140 | - /** |
|
| 1141 | - * @inheritDoc |
|
| 1142 | - * |
|
| 1143 | - * @param bool $reload set to TRUE to refill cache instantly after clearing it |
|
| 1144 | - * |
|
| 1145 | - * @since 29.0.0 |
|
| 1146 | - */ |
|
| 1147 | - public function clearCache(bool $reload = false): void { |
|
| 1148 | - $this->lazyLoaded = $this->fastLoaded = false; |
|
| 1149 | - $this->lazyCache = $this->fastCache = $this->valueTypes = []; |
|
| 1150 | - |
|
| 1151 | - if (!$reload) { |
|
| 1152 | - return; |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - $this->loadConfigAll(); |
|
| 1156 | - } |
|
| 1157 | - |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * For debug purpose. |
|
| 1161 | - * Returns the cached data. |
|
| 1162 | - * |
|
| 1163 | - * @return array |
|
| 1164 | - * @since 29.0.0 |
|
| 1165 | - * @internal |
|
| 1166 | - */ |
|
| 1167 | - public function statusCache(): array { |
|
| 1168 | - return [ |
|
| 1169 | - 'fastLoaded' => $this->fastLoaded, |
|
| 1170 | - 'fastCache' => $this->fastCache, |
|
| 1171 | - 'lazyLoaded' => $this->lazyLoaded, |
|
| 1172 | - 'lazyCache' => $this->lazyCache, |
|
| 1173 | - ]; |
|
| 1174 | - } |
|
| 1175 | - |
|
| 1176 | - /** |
|
| 1177 | - * @param int $needle bitflag to search |
|
| 1178 | - * @param int $type known value |
|
| 1179 | - * |
|
| 1180 | - * @return bool TRUE if bitflag $needle is set in $type |
|
| 1181 | - */ |
|
| 1182 | - private function isTyped(int $needle, int $type): bool { |
|
| 1183 | - return (($needle & $type) !== 0); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Confirm the string set for app and key fit the database description |
|
| 1188 | - * |
|
| 1189 | - * @param string $app assert $app fit in database |
|
| 1190 | - * @param string $configKey assert config key fit in database |
|
| 1191 | - * @param bool $allowEmptyApp $app can be empty string |
|
| 1192 | - * @param int $valueType assert value type is only one type |
|
| 1193 | - * |
|
| 1194 | - * @throws InvalidArgumentException |
|
| 1195 | - */ |
|
| 1196 | - private function assertParams(string $app = '', string $configKey = '', bool $allowEmptyApp = false, int $valueType = -1): void { |
|
| 1197 | - if (!$allowEmptyApp && $app === '') { |
|
| 1198 | - throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1199 | - } |
|
| 1200 | - if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1201 | - throw new InvalidArgumentException( |
|
| 1202 | - 'Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')' |
|
| 1203 | - ); |
|
| 1204 | - } |
|
| 1205 | - if (strlen($configKey) > self::KEY_MAX_LENGTH) { |
|
| 1206 | - throw new InvalidArgumentException('Value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1207 | - } |
|
| 1208 | - if ($valueType > -1) { |
|
| 1209 | - $valueType &= ~self::VALUE_SENSITIVE; |
|
| 1210 | - if (!in_array($valueType, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 1211 | - throw new InvalidArgumentException('Unknown value type'); |
|
| 1212 | - } |
|
| 1213 | - } |
|
| 1214 | - } |
|
| 1215 | - |
|
| 1216 | - private function loadConfigAll(?string $app = null): void { |
|
| 1217 | - $this->loadConfig($app, null); |
|
| 1218 | - } |
|
| 1219 | - |
|
| 1220 | - /** |
|
| 1221 | - * Load normal config or config set as lazy loaded |
|
| 1222 | - * |
|
| 1223 | - * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1224 | - */ |
|
| 1225 | - private function loadConfig(?string $app = null, ?bool $lazy = false): void { |
|
| 1226 | - if ($this->isLoaded($lazy)) { |
|
| 1227 | - return; |
|
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - // if lazy is null or true, we debug log |
|
| 1231 | - if (($lazy ?? true) !== false && $app !== null) { |
|
| 1232 | - $exception = new \RuntimeException('The loading of lazy AppConfig values have been triggered by app "' . $app . '"'); |
|
| 1233 | - $this->logger->debug($exception->getMessage(), ['exception' => $exception, 'app' => $app]); |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1237 | - $qb->from('appconfig'); |
|
| 1238 | - |
|
| 1239 | - // we only need value from lazy when loadConfig does not specify it |
|
| 1240 | - $qb->select('appid', 'configkey', 'configvalue', 'type'); |
|
| 1241 | - |
|
| 1242 | - if ($lazy !== null) { |
|
| 1243 | - $qb->where($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1244 | - } else { |
|
| 1245 | - $qb->addSelect('lazy'); |
|
| 1246 | - } |
|
| 1247 | - |
|
| 1248 | - $result = $qb->executeQuery(); |
|
| 1249 | - $rows = $result->fetchAll(); |
|
| 1250 | - foreach ($rows as $row) { |
|
| 1251 | - // most of the time, 'lazy' is not in the select because its value is already known |
|
| 1252 | - if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1253 | - $this->lazyCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1254 | - } else { |
|
| 1255 | - $this->fastCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1256 | - } |
|
| 1257 | - $this->valueTypes[$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); |
|
| 1258 | - } |
|
| 1259 | - $result->closeCursor(); |
|
| 1260 | - $this->setAsLoaded($lazy); |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - /** |
|
| 1264 | - * if $lazy is: |
|
| 1265 | - * - false: will returns true if fast config is loaded |
|
| 1266 | - * - true : will returns true if lazy config is loaded |
|
| 1267 | - * - null : will returns true if both config are loaded |
|
| 1268 | - * |
|
| 1269 | - * @param bool $lazy |
|
| 1270 | - * |
|
| 1271 | - * @return bool |
|
| 1272 | - */ |
|
| 1273 | - private function isLoaded(?bool $lazy): bool { |
|
| 1274 | - if ($lazy === null) { |
|
| 1275 | - return $this->lazyLoaded && $this->fastLoaded; |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - return $lazy ? $this->lazyLoaded : $this->fastLoaded; |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - /** |
|
| 1282 | - * if $lazy is: |
|
| 1283 | - * - false: set fast config as loaded |
|
| 1284 | - * - true : set lazy config as loaded |
|
| 1285 | - * - null : set both config as loaded |
|
| 1286 | - * |
|
| 1287 | - * @param bool $lazy |
|
| 1288 | - */ |
|
| 1289 | - private function setAsLoaded(?bool $lazy): void { |
|
| 1290 | - if ($lazy === null) { |
|
| 1291 | - $this->fastLoaded = true; |
|
| 1292 | - $this->lazyLoaded = true; |
|
| 1293 | - |
|
| 1294 | - return; |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - if ($lazy) { |
|
| 1298 | - $this->lazyLoaded = true; |
|
| 1299 | - } else { |
|
| 1300 | - $this->fastLoaded = true; |
|
| 1301 | - } |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - /** |
|
| 1305 | - * Gets the config value |
|
| 1306 | - * |
|
| 1307 | - * @param string $app app |
|
| 1308 | - * @param string $key key |
|
| 1309 | - * @param string $default = null, default value if the key does not exist |
|
| 1310 | - * |
|
| 1311 | - * @return string the value or $default |
|
| 1312 | - * @deprecated 29.0.0 use getValue*() |
|
| 1313 | - * |
|
| 1314 | - * This function gets a value from the appconfig table. If the key does |
|
| 1315 | - * not exist the default value will be returned |
|
| 1316 | - */ |
|
| 1317 | - public function getValue($app, $key, $default = null) { |
|
| 1318 | - $this->loadConfig($app); |
|
| 1319 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1320 | - |
|
| 1321 | - return $this->fastCache[$app][$key] ?? $default; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - /** |
|
| 1325 | - * Sets a value. If the key did not exist before it will be created. |
|
| 1326 | - * |
|
| 1327 | - * @param string $app app |
|
| 1328 | - * @param string $key key |
|
| 1329 | - * @param string|float|int $value value |
|
| 1330 | - * |
|
| 1331 | - * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 1332 | - * @throws AppConfigTypeConflictException |
|
| 1333 | - * @throws AppConfigUnknownKeyException |
|
| 1334 | - * @deprecated 29.0.0 |
|
| 1335 | - */ |
|
| 1336 | - public function setValue($app, $key, $value) { |
|
| 1337 | - /** |
|
| 1338 | - * TODO: would it be overkill, or decently improve performance, to catch |
|
| 1339 | - * call to this method with $key='enabled' and 'hide' config value related |
|
| 1340 | - * to $app when the app is disabled (by modifying entry in database: lazy=lazy+2) |
|
| 1341 | - * or enabled (lazy=lazy-2) |
|
| 1342 | - * |
|
| 1343 | - * this solution would remove the loading of config values from disabled app |
|
| 1344 | - * unless calling the method {@see loadConfigAll()} |
|
| 1345 | - */ |
|
| 1346 | - return $this->setTypedValue($app, $key, (string)$value, false, self::VALUE_MIXED); |
|
| 1347 | - } |
|
| 1348 | - |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 1352 | - * |
|
| 1353 | - * @param string|false $app |
|
| 1354 | - * @param string|false $key |
|
| 1355 | - * |
|
| 1356 | - * @return array|false |
|
| 1357 | - * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1358 | - */ |
|
| 1359 | - public function getValues($app, $key) { |
|
| 1360 | - if (($app !== false) === ($key !== false)) { |
|
| 1361 | - return false; |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - $key = ($key === false) ? '' : $key; |
|
| 1365 | - if (!$app) { |
|
| 1366 | - return $this->searchValues($key, false, self::VALUE_MIXED); |
|
| 1367 | - } else { |
|
| 1368 | - return $this->getAllValues($app, $key); |
|
| 1369 | - } |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * get all values of the app or and filters out sensitive data |
|
| 1374 | - * |
|
| 1375 | - * @param string $app |
|
| 1376 | - * |
|
| 1377 | - * @return array |
|
| 1378 | - * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1379 | - */ |
|
| 1380 | - public function getFilteredValues($app) { |
|
| 1381 | - return $this->getAllValues($app, filtered: true); |
|
| 1382 | - } |
|
| 1383 | - |
|
| 1384 | - |
|
| 1385 | - /** |
|
| 1386 | - * **Warning:** avoid default NULL value for $lazy as this will |
|
| 1387 | - * load all lazy values from the database |
|
| 1388 | - * |
|
| 1389 | - * @param string $app |
|
| 1390 | - * @param array<string, string> $values ['key' => 'value'] |
|
| 1391 | - * @param bool|null $lazy |
|
| 1392 | - * |
|
| 1393 | - * @return array<string, string|int|float|bool|array> |
|
| 1394 | - */ |
|
| 1395 | - private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { |
|
| 1396 | - foreach ($values as $key => $value) { |
|
| 1397 | - try { |
|
| 1398 | - $type = $this->getValueType($app, $key, $lazy); |
|
| 1399 | - } catch (AppConfigUnknownKeyException) { |
|
| 1400 | - continue; |
|
| 1401 | - } |
|
| 1402 | - |
|
| 1403 | - $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - return $values; |
|
| 1407 | - } |
|
| 1408 | - |
|
| 1409 | - /** |
|
| 1410 | - * convert string value to the expected type |
|
| 1411 | - * |
|
| 1412 | - * @param string $value |
|
| 1413 | - * @param int $type |
|
| 1414 | - * |
|
| 1415 | - * @return string|int|float|bool|array |
|
| 1416 | - */ |
|
| 1417 | - private function convertTypedValue(string $value, int $type): string|int|float|bool|array { |
|
| 1418 | - switch ($type) { |
|
| 1419 | - case self::VALUE_INT: |
|
| 1420 | - return (int)$value; |
|
| 1421 | - case self::VALUE_FLOAT: |
|
| 1422 | - return (float)$value; |
|
| 1423 | - case self::VALUE_BOOL: |
|
| 1424 | - return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1425 | - case self::VALUE_ARRAY: |
|
| 1426 | - try { |
|
| 1427 | - return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1428 | - } catch (JsonException $e) { |
|
| 1429 | - // ignoreable |
|
| 1430 | - } |
|
| 1431 | - break; |
|
| 1432 | - } |
|
| 1433 | - return $value; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - /** |
|
| 1437 | - * @param string $app |
|
| 1438 | - * |
|
| 1439 | - * @return string[] |
|
| 1440 | - * @deprecated 29.0.0 data sensitivity should be set when calling setValue*() |
|
| 1441 | - */ |
|
| 1442 | - private function getSensitiveKeys(string $app): array { |
|
| 1443 | - $sensitiveValues = [ |
|
| 1444 | - 'circles' => [ |
|
| 1445 | - '/^key_pairs$/', |
|
| 1446 | - '/^local_gskey$/', |
|
| 1447 | - ], |
|
| 1448 | - 'call_summary_bot' => [ |
|
| 1449 | - '/^secret_(.*)$/', |
|
| 1450 | - ], |
|
| 1451 | - 'external' => [ |
|
| 1452 | - '/^sites$/', |
|
| 1453 | - '/^jwt_token_privkey_(.*)$/', |
|
| 1454 | - ], |
|
| 1455 | - 'globalsiteselector' => [ |
|
| 1456 | - '/^gss\.jwt\.key$/', |
|
| 1457 | - ], |
|
| 1458 | - 'gpgmailer' => [ |
|
| 1459 | - '/^GpgServerKey$/', |
|
| 1460 | - ], |
|
| 1461 | - 'integration_discourse' => [ |
|
| 1462 | - '/^private_key$/', |
|
| 1463 | - '/^public_key$/', |
|
| 1464 | - ], |
|
| 1465 | - 'integration_dropbox' => [ |
|
| 1466 | - '/^client_id$/', |
|
| 1467 | - '/^client_secret$/', |
|
| 1468 | - ], |
|
| 1469 | - 'integration_github' => [ |
|
| 1470 | - '/^client_id$/', |
|
| 1471 | - '/^client_secret$/', |
|
| 1472 | - ], |
|
| 1473 | - 'integration_gitlab' => [ |
|
| 1474 | - '/^client_id$/', |
|
| 1475 | - '/^client_secret$/', |
|
| 1476 | - '/^oauth_instance_url$/', |
|
| 1477 | - ], |
|
| 1478 | - 'integration_google' => [ |
|
| 1479 | - '/^client_id$/', |
|
| 1480 | - '/^client_secret$/', |
|
| 1481 | - ], |
|
| 1482 | - 'integration_jira' => [ |
|
| 1483 | - '/^client_id$/', |
|
| 1484 | - '/^client_secret$/', |
|
| 1485 | - '/^forced_instance_url$/', |
|
| 1486 | - ], |
|
| 1487 | - 'integration_onedrive' => [ |
|
| 1488 | - '/^client_id$/', |
|
| 1489 | - '/^client_secret$/', |
|
| 1490 | - ], |
|
| 1491 | - 'integration_openproject' => [ |
|
| 1492 | - '/^client_id$/', |
|
| 1493 | - '/^client_secret$/', |
|
| 1494 | - '/^oauth_instance_url$/', |
|
| 1495 | - ], |
|
| 1496 | - 'integration_reddit' => [ |
|
| 1497 | - '/^client_id$/', |
|
| 1498 | - '/^client_secret$/', |
|
| 1499 | - ], |
|
| 1500 | - 'integration_suitecrm' => [ |
|
| 1501 | - '/^client_id$/', |
|
| 1502 | - '/^client_secret$/', |
|
| 1503 | - '/^oauth_instance_url$/', |
|
| 1504 | - ], |
|
| 1505 | - 'integration_twitter' => [ |
|
| 1506 | - '/^consumer_key$/', |
|
| 1507 | - '/^consumer_secret$/', |
|
| 1508 | - '/^followed_user$/', |
|
| 1509 | - ], |
|
| 1510 | - 'integration_zammad' => [ |
|
| 1511 | - '/^client_id$/', |
|
| 1512 | - '/^client_secret$/', |
|
| 1513 | - '/^oauth_instance_url$/', |
|
| 1514 | - ], |
|
| 1515 | - 'maps' => [ |
|
| 1516 | - '/^mapboxAPIKEY$/', |
|
| 1517 | - ], |
|
| 1518 | - 'notify_push' => [ |
|
| 1519 | - '/^cookie$/', |
|
| 1520 | - ], |
|
| 1521 | - 'onlyoffice' => [ |
|
| 1522 | - '/^jwt_secret$/', |
|
| 1523 | - ], |
|
| 1524 | - 'passwords' => [ |
|
| 1525 | - '/^SSEv1ServerKey$/', |
|
| 1526 | - ], |
|
| 1527 | - 'serverinfo' => [ |
|
| 1528 | - '/^token$/', |
|
| 1529 | - ], |
|
| 1530 | - 'spreed' => [ |
|
| 1531 | - '/^bridge_bot_password$/', |
|
| 1532 | - '/^hosted-signaling-server-(.*)$/', |
|
| 1533 | - '/^recording_servers$/', |
|
| 1534 | - '/^signaling_servers$/', |
|
| 1535 | - '/^signaling_ticket_secret$/', |
|
| 1536 | - '/^signaling_token_privkey_(.*)$/', |
|
| 1537 | - '/^signaling_token_pubkey_(.*)$/', |
|
| 1538 | - '/^sip_bridge_dialin_info$/', |
|
| 1539 | - '/^sip_bridge_shared_secret$/', |
|
| 1540 | - '/^stun_servers$/', |
|
| 1541 | - '/^turn_servers$/', |
|
| 1542 | - '/^turn_server_secret$/', |
|
| 1543 | - ], |
|
| 1544 | - 'support' => [ |
|
| 1545 | - '/^last_response$/', |
|
| 1546 | - '/^potential_subscription_key$/', |
|
| 1547 | - '/^subscription_key$/', |
|
| 1548 | - ], |
|
| 1549 | - 'theming' => [ |
|
| 1550 | - '/^imprintUrl$/', |
|
| 1551 | - '/^privacyUrl$/', |
|
| 1552 | - '/^slogan$/', |
|
| 1553 | - '/^url$/', |
|
| 1554 | - ], |
|
| 1555 | - 'twofactor_gateway' => [ |
|
| 1556 | - '/^.*token$/', |
|
| 1557 | - ], |
|
| 1558 | - 'user_ldap' => [ |
|
| 1559 | - '/^(s..)?ldap_agent_password$/', |
|
| 1560 | - ], |
|
| 1561 | - 'user_saml' => [ |
|
| 1562 | - '/^idp-x509cert$/', |
|
| 1563 | - ], |
|
| 1564 | - 'whiteboard' => [ |
|
| 1565 | - '/^jwt_secret_key$/', |
|
| 1566 | - ], |
|
| 1567 | - ]; |
|
| 1568 | - |
|
| 1569 | - return $sensitiveValues[$app] ?? []; |
|
| 1570 | - } |
|
| 1571 | - |
|
| 1572 | - /** |
|
| 1573 | - * Clear all the cached app config values |
|
| 1574 | - * New cache will be generated next time a config value is retrieved |
|
| 1575 | - * |
|
| 1576 | - * @deprecated 29.0.0 use {@see clearCache()} |
|
| 1577 | - */ |
|
| 1578 | - public function clearCachedConfig(): void { |
|
| 1579 | - $this->clearCache(); |
|
| 1580 | - } |
|
| 1581 | - |
|
| 1582 | - /** |
|
| 1583 | - * Match and apply current use of config values with defined lexicon. |
|
| 1584 | - * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1585 | - * |
|
| 1586 | - * @throws AppConfigUnknownKeyException |
|
| 1587 | - * @throws AppConfigTypeConflictException |
|
| 1588 | - * @return bool TRUE if everything is fine compared to lexicon or lexicon does not exist |
|
| 1589 | - */ |
|
| 1590 | - private function matchAndApplyLexiconDefinition( |
|
| 1591 | - string $app, |
|
| 1592 | - string &$key, |
|
| 1593 | - ?bool &$lazy = null, |
|
| 1594 | - int &$type = self::VALUE_MIXED, |
|
| 1595 | - string &$default = '', |
|
| 1596 | - ): bool { |
|
| 1597 | - if (in_array($key, |
|
| 1598 | - [ |
|
| 1599 | - 'enabled', |
|
| 1600 | - 'installed_version', |
|
| 1601 | - 'types', |
|
| 1602 | - ])) { |
|
| 1603 | - return true; // we don't break stuff for this list of config keys. |
|
| 1604 | - } |
|
| 1605 | - $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1606 | - if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1607 | - // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1608 | - $key = $configDetails['aliases'][$key]; |
|
| 1609 | - } |
|
| 1610 | - |
|
| 1611 | - if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1612 | - return $this->applyLexiconStrictness($configDetails['strictness'], 'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1613 | - } |
|
| 1614 | - |
|
| 1615 | - // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1616 | - if ($lazy === null) { |
|
| 1617 | - return true; |
|
| 1618 | - } |
|
| 1619 | - |
|
| 1620 | - /** @var ConfigLexiconEntry $configValue */ |
|
| 1621 | - $configValue = $configDetails['entries'][$key]; |
|
| 1622 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 1623 | - |
|
| 1624 | - $appConfigValueType = $configValue->getValueType()->toAppConfigFlag(); |
|
| 1625 | - if ($type === self::VALUE_MIXED) { |
|
| 1626 | - $type = $appConfigValueType; // we overwrite if value was requested as mixed |
|
| 1627 | - } elseif ($appConfigValueType !== $type) { |
|
| 1628 | - throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1629 | - } |
|
| 1630 | - |
|
| 1631 | - $lazy = $configValue->isLazy(); |
|
| 1632 | - $default = $configValue->getDefault() ?? $default; // default from Lexicon got priority |
|
| 1633 | - if ($configValue->isFlagged(self::FLAG_SENSITIVE)) { |
|
| 1634 | - $type |= self::VALUE_SENSITIVE; |
|
| 1635 | - } |
|
| 1636 | - if ($configValue->isDeprecated()) { |
|
| 1637 | - $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1638 | - } |
|
| 1639 | - |
|
| 1640 | - return true; |
|
| 1641 | - } |
|
| 1642 | - |
|
| 1643 | - /** |
|
| 1644 | - * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1645 | - * |
|
| 1646 | - * @param ConfigLexiconStrictness|null $strictness |
|
| 1647 | - * @param string $line |
|
| 1648 | - * |
|
| 1649 | - * @return bool TRUE if conflict can be fully ignored, FALSE if action should be not performed |
|
| 1650 | - * @throws AppConfigUnknownKeyException if strictness implies exception |
|
| 1651 | - * @see IConfigLexicon::getStrictness() |
|
| 1652 | - */ |
|
| 1653 | - private function applyLexiconStrictness( |
|
| 1654 | - ?ConfigLexiconStrictness $strictness, |
|
| 1655 | - string $line = '', |
|
| 1656 | - ): bool { |
|
| 1657 | - if ($strictness === null) { |
|
| 1658 | - return true; |
|
| 1659 | - } |
|
| 1660 | - |
|
| 1661 | - switch ($strictness) { |
|
| 1662 | - case ConfigLexiconStrictness::IGNORE: |
|
| 1663 | - return true; |
|
| 1664 | - case ConfigLexiconStrictness::NOTICE: |
|
| 1665 | - $this->logger->notice($line); |
|
| 1666 | - return true; |
|
| 1667 | - case ConfigLexiconStrictness::WARNING: |
|
| 1668 | - $this->logger->warning($line); |
|
| 1669 | - return false; |
|
| 1670 | - } |
|
| 1671 | - |
|
| 1672 | - throw new AppConfigUnknownKeyException($line); |
|
| 1673 | - } |
|
| 1674 | - |
|
| 1675 | - /** |
|
| 1676 | - * extract details from registered $appId's config lexicon |
|
| 1677 | - * |
|
| 1678 | - * @param string $appId |
|
| 1679 | - * @internal |
|
| 1680 | - * |
|
| 1681 | - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1682 | - */ |
|
| 1683 | - public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1684 | - if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1685 | - $entries = $aliases = []; |
|
| 1686 | - $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1687 | - $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1688 | - foreach ($configLexicon?->getAppConfigs() ?? [] as $configEntry) { |
|
| 1689 | - $entries[$configEntry->getKey()] = $configEntry; |
|
| 1690 | - if ($configEntry->getRename() !== null) { |
|
| 1691 | - $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 1692 | - } |
|
| 1693 | - } |
|
| 1694 | - |
|
| 1695 | - $this->configLexiconDetails[$appId] = [ |
|
| 1696 | - 'entries' => $entries, |
|
| 1697 | - 'aliases' => $aliases, |
|
| 1698 | - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 1699 | - ]; |
|
| 1700 | - } |
|
| 1701 | - |
|
| 1702 | - return $this->configLexiconDetails[$appId]; |
|
| 1703 | - } |
|
| 1704 | - |
|
| 1705 | - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 1706 | - return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 1707 | - } |
|
| 1708 | - |
|
| 1709 | - /** |
|
| 1710 | - * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 1711 | - * |
|
| 1712 | - * @internal |
|
| 1713 | - */ |
|
| 1714 | - public function ignoreLexiconAliases(bool $ignore): void { |
|
| 1715 | - $this->ignoreLexiconAliases = $ignore; |
|
| 1716 | - } |
|
| 1717 | - |
|
| 1718 | - /** |
|
| 1719 | - * Returns the installed versions of all apps |
|
| 1720 | - * |
|
| 1721 | - * @return array<string, string> |
|
| 1722 | - */ |
|
| 1723 | - public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 1724 | - if ($this->appVersionsCache === null) { |
|
| 1725 | - /** @var array<string, string> */ |
|
| 1726 | - $this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING); |
|
| 1727 | - } |
|
| 1728 | - if ($onlyEnabled) { |
|
| 1729 | - return array_filter( |
|
| 1730 | - $this->appVersionsCache, |
|
| 1731 | - fn (string $app): bool => $this->getValueString($app, 'enabled', 'no') !== 'no', |
|
| 1732 | - ARRAY_FILTER_USE_KEY |
|
| 1733 | - ); |
|
| 1734 | - } |
|
| 1735 | - return $this->appVersionsCache; |
|
| 1736 | - } |
|
| 51 | + private const APP_MAX_LENGTH = 32; |
|
| 52 | + private const KEY_MAX_LENGTH = 64; |
|
| 53 | + private const ENCRYPTION_PREFIX = '$AppConfigEncryption$'; |
|
| 54 | + private const ENCRYPTION_PREFIX_LENGTH = 21; // strlen(self::ENCRYPTION_PREFIX) |
|
| 55 | + |
|
| 56 | + /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 57 | + private array $fastCache = []; // cache for normal config keys |
|
| 58 | + /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 59 | + private array $lazyCache = []; // cache for lazy config keys |
|
| 60 | + /** @var array<string, array<string, int>> ['app_id' => ['config_key' => bitflag]] */ |
|
| 61 | + private array $valueTypes = []; // type for all config values |
|
| 62 | + private bool $fastLoaded = false; |
|
| 63 | + private bool $lazyLoaded = false; |
|
| 64 | + /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 65 | + private array $configLexiconDetails = []; |
|
| 66 | + private bool $ignoreLexiconAliases = false; |
|
| 67 | + |
|
| 68 | + /** @var ?array<string, string> */ |
|
| 69 | + private ?array $appVersionsCache = null; |
|
| 70 | + |
|
| 71 | + public function __construct( |
|
| 72 | + protected IDBConnection $connection, |
|
| 73 | + protected LoggerInterface $logger, |
|
| 74 | + protected ICrypto $crypto, |
|
| 75 | + ) { |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @inheritDoc |
|
| 80 | + * |
|
| 81 | + * @return list<string> list of app ids |
|
| 82 | + * @since 7.0.0 |
|
| 83 | + */ |
|
| 84 | + public function getApps(): array { |
|
| 85 | + $this->loadConfigAll(); |
|
| 86 | + $apps = array_merge(array_keys($this->fastCache), array_keys($this->lazyCache)); |
|
| 87 | + sort($apps); |
|
| 88 | + |
|
| 89 | + return array_values(array_unique($apps)); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @inheritDoc |
|
| 94 | + * |
|
| 95 | + * @param string $app id of the app |
|
| 96 | + * |
|
| 97 | + * @return list<string> list of stored config keys |
|
| 98 | + * @since 29.0.0 |
|
| 99 | + */ |
|
| 100 | + public function getKeys(string $app): array { |
|
| 101 | + $this->assertParams($app); |
|
| 102 | + $this->loadConfigAll($app); |
|
| 103 | + $keys = array_merge(array_keys($this->fastCache[$app] ?? []), array_keys($this->lazyCache[$app] ?? [])); |
|
| 104 | + sort($keys); |
|
| 105 | + |
|
| 106 | + return array_values(array_unique($keys)); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @inheritDoc |
|
| 111 | + * |
|
| 112 | + * @param string $app id of the app |
|
| 113 | + * @param string $key config key |
|
| 114 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 115 | + * |
|
| 116 | + * @return bool TRUE if key exists |
|
| 117 | + * @since 7.0.0 |
|
| 118 | + * @since 29.0.0 Added the $lazy argument |
|
| 119 | + */ |
|
| 120 | + public function hasKey(string $app, string $key, ?bool $lazy = false): bool { |
|
| 121 | + $this->assertParams($app, $key); |
|
| 122 | + $this->loadConfig($app, $lazy); |
|
| 123 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 124 | + |
|
| 125 | + if ($lazy === null) { |
|
| 126 | + $appCache = $this->getAllValues($app); |
|
| 127 | + return isset($appCache[$key]); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($lazy) { |
|
| 131 | + return isset($this->lazyCache[$app][$key]); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return isset($this->fastCache[$app][$key]); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param string $app id of the app |
|
| 139 | + * @param string $key config key |
|
| 140 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 141 | + * |
|
| 142 | + * @return bool |
|
| 143 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 144 | + * @since 29.0.0 |
|
| 145 | + */ |
|
| 146 | + public function isSensitive(string $app, string $key, ?bool $lazy = false): bool { |
|
| 147 | + $this->assertParams($app, $key); |
|
| 148 | + $this->loadConfig(null, $lazy); |
|
| 149 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 150 | + |
|
| 151 | + if (!isset($this->valueTypes[$app][$key])) { |
|
| 152 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key]); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @inheritDoc |
|
| 160 | + * |
|
| 161 | + * @param string $app if of the app |
|
| 162 | + * @param string $key config key |
|
| 163 | + * |
|
| 164 | + * @return bool TRUE if config is lazy loaded |
|
| 165 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 166 | + * @see IAppConfig for details about lazy loading |
|
| 167 | + * @since 29.0.0 |
|
| 168 | + */ |
|
| 169 | + public function isLazy(string $app, string $key): bool { |
|
| 170 | + $this->assertParams($app, $key); |
|
| 171 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 172 | + |
|
| 173 | + // there is a huge probability the non-lazy config are already loaded |
|
| 174 | + if ($this->hasKey($app, $key, false)) { |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // key not found, we search in the lazy config |
|
| 179 | + if ($this->hasKey($app, $key, true)) { |
|
| 180 | + return true; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @inheritDoc |
|
| 189 | + * |
|
| 190 | + * @param string $app id of the app |
|
| 191 | + * @param string $prefix config keys prefix to search |
|
| 192 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 193 | + * |
|
| 194 | + * @return array<string, string|int|float|bool|array> [configKey => configValue] |
|
| 195 | + * @since 29.0.0 |
|
| 196 | + */ |
|
| 197 | + public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array { |
|
| 198 | + $this->assertParams($app, $prefix); |
|
| 199 | + // if we want to filter values, we need to get sensitivity |
|
| 200 | + $this->loadConfigAll($app); |
|
| 201 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 202 | + $values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])); |
|
| 203 | + $values = array_filter( |
|
| 204 | + $values, |
|
| 205 | + function (string $key) use ($prefix): bool { |
|
| 206 | + return str_starts_with($key, $prefix); // filter values based on $prefix |
|
| 207 | + }, ARRAY_FILTER_USE_KEY |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + if (!$filtered) { |
|
| 211 | + return $values; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Using the old (deprecated) list of sensitive values. |
|
| 216 | + */ |
|
| 217 | + foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) { |
|
| 218 | + $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 219 | + foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 220 | + $this->valueTypes[$app][$sensitiveKey] = ($this->valueTypes[$app][$sensitiveKey] ?? 0) | self::VALUE_SENSITIVE; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $result = []; |
|
| 225 | + foreach ($values as $key => $value) { |
|
| 226 | + $result[$key] = $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key] ?? 0) ? IConfig::SENSITIVE_VALUE : $value; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return $result; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @inheritDoc |
|
| 234 | + * |
|
| 235 | + * @param string $key config key |
|
| 236 | + * @param bool $lazy search within lazy loaded config |
|
| 237 | + * @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others) |
|
| 238 | + * |
|
| 239 | + * @return array<string, string|int|float|bool|array> [appId => configValue] |
|
| 240 | + * @since 29.0.0 |
|
| 241 | + */ |
|
| 242 | + public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array { |
|
| 243 | + $this->assertParams('', $key, true); |
|
| 244 | + $this->loadConfig(null, $lazy); |
|
| 245 | + |
|
| 246 | + /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 247 | + if ($lazy) { |
|
| 248 | + $cache = $this->lazyCache; |
|
| 249 | + } else { |
|
| 250 | + $cache = $this->fastCache; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $values = []; |
|
| 254 | + foreach (array_keys($cache) as $app) { |
|
| 255 | + if (isset($cache[$app][$key])) { |
|
| 256 | + $values[$app] = $this->convertTypedValue($cache[$app][$key], $typedAs ?? $this->getValueType((string)$app, $key, $lazy)); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return $values; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Get the config value as string. |
|
| 266 | + * If the value does not exist the given default will be returned. |
|
| 267 | + * |
|
| 268 | + * Set lazy to `null` to ignore it and get the value from either source. |
|
| 269 | + * |
|
| 270 | + * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 271 | + * |
|
| 272 | + * @param string $app id of the app |
|
| 273 | + * @param string $key config key |
|
| 274 | + * @param string $default config value |
|
| 275 | + * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 276 | + * |
|
| 277 | + * @return string the value or $default |
|
| 278 | + * @internal |
|
| 279 | + * @since 29.0.0 |
|
| 280 | + * @see IAppConfig for explanation about lazy loading |
|
| 281 | + * @see getValueString() |
|
| 282 | + * @see getValueInt() |
|
| 283 | + * @see getValueFloat() |
|
| 284 | + * @see getValueBool() |
|
| 285 | + * @see getValueArray() |
|
| 286 | + */ |
|
| 287 | + public function getValueMixed( |
|
| 288 | + string $app, |
|
| 289 | + string $key, |
|
| 290 | + string $default = '', |
|
| 291 | + ?bool $lazy = false, |
|
| 292 | + ): string { |
|
| 293 | + try { |
|
| 294 | + $lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy; |
|
| 295 | + } catch (AppConfigUnknownKeyException) { |
|
| 296 | + return $default; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + return $this->getTypedValue( |
|
| 300 | + $app, |
|
| 301 | + $key, |
|
| 302 | + $default, |
|
| 303 | + $lazy, |
|
| 304 | + self::VALUE_MIXED |
|
| 305 | + ); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @inheritDoc |
|
| 310 | + * |
|
| 311 | + * @param string $app id of the app |
|
| 312 | + * @param string $key config key |
|
| 313 | + * @param string $default default value |
|
| 314 | + * @param bool $lazy search within lazy loaded config |
|
| 315 | + * |
|
| 316 | + * @return string stored config value or $default if not set in database |
|
| 317 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 318 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 319 | + * @since 29.0.0 |
|
| 320 | + * @see IAppConfig for explanation about lazy loading |
|
| 321 | + */ |
|
| 322 | + public function getValueString( |
|
| 323 | + string $app, |
|
| 324 | + string $key, |
|
| 325 | + string $default = '', |
|
| 326 | + bool $lazy = false, |
|
| 327 | + ): string { |
|
| 328 | + return $this->getTypedValue($app, $key, $default, $lazy, self::VALUE_STRING); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @inheritDoc |
|
| 333 | + * |
|
| 334 | + * @param string $app id of the app |
|
| 335 | + * @param string $key config key |
|
| 336 | + * @param int $default default value |
|
| 337 | + * @param bool $lazy search within lazy loaded config |
|
| 338 | + * |
|
| 339 | + * @return int stored config value or $default if not set in database |
|
| 340 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 341 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 342 | + * @since 29.0.0 |
|
| 343 | + * @see IAppConfig for explanation about lazy loading |
|
| 344 | + */ |
|
| 345 | + public function getValueInt( |
|
| 346 | + string $app, |
|
| 347 | + string $key, |
|
| 348 | + int $default = 0, |
|
| 349 | + bool $lazy = false, |
|
| 350 | + ): int { |
|
| 351 | + return (int)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_INT); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * @inheritDoc |
|
| 356 | + * |
|
| 357 | + * @param string $app id of the app |
|
| 358 | + * @param string $key config key |
|
| 359 | + * @param float $default default value |
|
| 360 | + * @param bool $lazy search within lazy loaded config |
|
| 361 | + * |
|
| 362 | + * @return float stored config value or $default if not set in database |
|
| 363 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 364 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 365 | + * @since 29.0.0 |
|
| 366 | + * @see IAppConfig for explanation about lazy loading |
|
| 367 | + */ |
|
| 368 | + public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { |
|
| 369 | + return (float)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_FLOAT); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @inheritDoc |
|
| 374 | + * |
|
| 375 | + * @param string $app id of the app |
|
| 376 | + * @param string $key config key |
|
| 377 | + * @param bool $default default value |
|
| 378 | + * @param bool $lazy search within lazy loaded config |
|
| 379 | + * |
|
| 380 | + * @return bool stored config value or $default if not set in database |
|
| 381 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 382 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 383 | + * @since 29.0.0 |
|
| 384 | + * @see IAppConfig for explanation about lazy loading |
|
| 385 | + */ |
|
| 386 | + public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { |
|
| 387 | + $b = strtolower($this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL)); |
|
| 388 | + return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * @inheritDoc |
|
| 393 | + * |
|
| 394 | + * @param string $app id of the app |
|
| 395 | + * @param string $key config key |
|
| 396 | + * @param array $default default value |
|
| 397 | + * @param bool $lazy search within lazy loaded config |
|
| 398 | + * |
|
| 399 | + * @return array stored config value or $default if not set in database |
|
| 400 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 401 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 402 | + * @since 29.0.0 |
|
| 403 | + * @see IAppConfig for explanation about lazy loading |
|
| 404 | + */ |
|
| 405 | + public function getValueArray( |
|
| 406 | + string $app, |
|
| 407 | + string $key, |
|
| 408 | + array $default = [], |
|
| 409 | + bool $lazy = false, |
|
| 410 | + ): array { |
|
| 411 | + try { |
|
| 412 | + $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 413 | + $value = json_decode($this->getTypedValue($app, $key, $defaultJson, $lazy, self::VALUE_ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 414 | + |
|
| 415 | + return is_array($value) ? $value : []; |
|
| 416 | + } catch (JsonException) { |
|
| 417 | + return []; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * @param string $app id of the app |
|
| 423 | + * @param string $key config key |
|
| 424 | + * @param string $default default value |
|
| 425 | + * @param bool $lazy search within lazy loaded config |
|
| 426 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT}{@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 427 | + * |
|
| 428 | + * @return string |
|
| 429 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 430 | + * @throws InvalidArgumentException |
|
| 431 | + */ |
|
| 432 | + private function getTypedValue( |
|
| 433 | + string $app, |
|
| 434 | + string $key, |
|
| 435 | + string $default, |
|
| 436 | + bool $lazy, |
|
| 437 | + int $type, |
|
| 438 | + ): string { |
|
| 439 | + $this->assertParams($app, $key, valueType: $type); |
|
| 440 | + $origKey = $key; |
|
| 441 | + if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default)) { |
|
| 442 | + return $default; // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 443 | + } |
|
| 444 | + $this->loadConfig($app, $lazy); |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * We ignore check if mixed type is requested. |
|
| 448 | + * If type of stored value is set as mixed, we don't filter. |
|
| 449 | + * If type of stored value is defined, we compare with the one requested. |
|
| 450 | + */ |
|
| 451 | + $knownType = $this->valueTypes[$app][$key] ?? 0; |
|
| 452 | + if (!$this->isTyped(self::VALUE_MIXED, $type) |
|
| 453 | + && $knownType > 0 |
|
| 454 | + && !$this->isTyped(self::VALUE_MIXED, $knownType) |
|
| 455 | + && !$this->isTyped($type, $knownType)) { |
|
| 456 | + $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 457 | + throw new AppConfigTypeConflictException('conflict with value type from database'); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * - the pair $app/$key cannot exist in both array, |
|
| 462 | + * - we should still return an existing non-lazy value even if current method |
|
| 463 | + * is called with $lazy is true |
|
| 464 | + * |
|
| 465 | + * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 466 | + */ |
|
| 467 | + if (isset($this->lazyCache[$app][$key])) { |
|
| 468 | + $value = $this->lazyCache[$app][$key]; |
|
| 469 | + } elseif (isset($this->fastCache[$app][$key])) { |
|
| 470 | + $value = $this->fastCache[$app][$key]; |
|
| 471 | + } else { |
|
| 472 | + return $default; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $knownType); |
|
| 476 | + if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 477 | + // Only decrypt values that are stored encrypted |
|
| 478 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 482 | + // interested to check options in case a modification of the value is needed |
|
| 483 | + // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 484 | + if ($origKey !== $key && $type === self::VALUE_BOOL) { |
|
| 485 | + $configManager = Server::get(ConfigManager::class); |
|
| 486 | + $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + return $value; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * @inheritDoc |
|
| 494 | + * |
|
| 495 | + * @param string $app id of the app |
|
| 496 | + * @param string $key config key |
|
| 497 | + * |
|
| 498 | + * @return int type of the value |
|
| 499 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 500 | + * @since 29.0.0 |
|
| 501 | + * @see VALUE_STRING |
|
| 502 | + * @see VALUE_INT |
|
| 503 | + * @see VALUE_FLOAT |
|
| 504 | + * @see VALUE_BOOL |
|
| 505 | + * @see VALUE_ARRAY |
|
| 506 | + */ |
|
| 507 | + public function getValueType(string $app, string $key, ?bool $lazy = null): int { |
|
| 508 | + $type = self::VALUE_MIXED; |
|
| 509 | + $ignorable = $lazy ?? false; |
|
| 510 | + $this->matchAndApplyLexiconDefinition($app, $key, $ignorable, $type); |
|
| 511 | + if ($type !== self::VALUE_MIXED) { |
|
| 512 | + // a modified $type means config key is set in Lexicon |
|
| 513 | + return $type; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + $this->assertParams($app, $key); |
|
| 517 | + $this->loadConfig($app, $lazy); |
|
| 518 | + |
|
| 519 | + if (!isset($this->valueTypes[$app][$key])) { |
|
| 520 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + $type = $this->valueTypes[$app][$key]; |
|
| 524 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 525 | + return $type; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Store a config key and its value in database as VALUE_MIXED |
|
| 531 | + * |
|
| 532 | + * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 533 | + * |
|
| 534 | + * @param string $app id of the app |
|
| 535 | + * @param string $key config key |
|
| 536 | + * @param string $value config value |
|
| 537 | + * @param bool $lazy set config as lazy loaded |
|
| 538 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 539 | + * |
|
| 540 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 541 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED |
|
| 542 | + * @internal |
|
| 543 | + * @since 29.0.0 |
|
| 544 | + * @see IAppConfig for explanation about lazy loading |
|
| 545 | + * @see setValueString() |
|
| 546 | + * @see setValueInt() |
|
| 547 | + * @see setValueFloat() |
|
| 548 | + * @see setValueBool() |
|
| 549 | + * @see setValueArray() |
|
| 550 | + */ |
|
| 551 | + public function setValueMixed( |
|
| 552 | + string $app, |
|
| 553 | + string $key, |
|
| 554 | + string $value, |
|
| 555 | + bool $lazy = false, |
|
| 556 | + bool $sensitive = false, |
|
| 557 | + ): bool { |
|
| 558 | + return $this->setTypedValue( |
|
| 559 | + $app, |
|
| 560 | + $key, |
|
| 561 | + $value, |
|
| 562 | + $lazy, |
|
| 563 | + self::VALUE_MIXED | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 564 | + ); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * @inheritDoc |
|
| 570 | + * |
|
| 571 | + * @param string $app id of the app |
|
| 572 | + * @param string $key config key |
|
| 573 | + * @param string $value config value |
|
| 574 | + * @param bool $lazy set config as lazy loaded |
|
| 575 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 576 | + * |
|
| 577 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 578 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 579 | + * @since 29.0.0 |
|
| 580 | + * @see IAppConfig for explanation about lazy loading |
|
| 581 | + */ |
|
| 582 | + public function setValueString( |
|
| 583 | + string $app, |
|
| 584 | + string $key, |
|
| 585 | + string $value, |
|
| 586 | + bool $lazy = false, |
|
| 587 | + bool $sensitive = false, |
|
| 588 | + ): bool { |
|
| 589 | + return $this->setTypedValue( |
|
| 590 | + $app, |
|
| 591 | + $key, |
|
| 592 | + $value, |
|
| 593 | + $lazy, |
|
| 594 | + self::VALUE_STRING | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 595 | + ); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * @inheritDoc |
|
| 600 | + * |
|
| 601 | + * @param string $app id of the app |
|
| 602 | + * @param string $key config key |
|
| 603 | + * @param int $value config value |
|
| 604 | + * @param bool $lazy set config as lazy loaded |
|
| 605 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 606 | + * |
|
| 607 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 608 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 609 | + * @since 29.0.0 |
|
| 610 | + * @see IAppConfig for explanation about lazy loading |
|
| 611 | + */ |
|
| 612 | + public function setValueInt( |
|
| 613 | + string $app, |
|
| 614 | + string $key, |
|
| 615 | + int $value, |
|
| 616 | + bool $lazy = false, |
|
| 617 | + bool $sensitive = false, |
|
| 618 | + ): bool { |
|
| 619 | + if ($value > 2000000000) { |
|
| 620 | + $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + return $this->setTypedValue( |
|
| 624 | + $app, |
|
| 625 | + $key, |
|
| 626 | + (string)$value, |
|
| 627 | + $lazy, |
|
| 628 | + self::VALUE_INT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 629 | + ); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * @inheritDoc |
|
| 634 | + * |
|
| 635 | + * @param string $app id of the app |
|
| 636 | + * @param string $key config key |
|
| 637 | + * @param float $value config value |
|
| 638 | + * @param bool $lazy set config as lazy loaded |
|
| 639 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 640 | + * |
|
| 641 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 642 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 643 | + * @since 29.0.0 |
|
| 644 | + * @see IAppConfig for explanation about lazy loading |
|
| 645 | + */ |
|
| 646 | + public function setValueFloat( |
|
| 647 | + string $app, |
|
| 648 | + string $key, |
|
| 649 | + float $value, |
|
| 650 | + bool $lazy = false, |
|
| 651 | + bool $sensitive = false, |
|
| 652 | + ): bool { |
|
| 653 | + return $this->setTypedValue( |
|
| 654 | + $app, |
|
| 655 | + $key, |
|
| 656 | + (string)$value, |
|
| 657 | + $lazy, |
|
| 658 | + self::VALUE_FLOAT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 659 | + ); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * @inheritDoc |
|
| 664 | + * |
|
| 665 | + * @param string $app id of the app |
|
| 666 | + * @param string $key config key |
|
| 667 | + * @param bool $value config value |
|
| 668 | + * @param bool $lazy set config as lazy loaded |
|
| 669 | + * |
|
| 670 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 671 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 672 | + * @since 29.0.0 |
|
| 673 | + * @see IAppConfig for explanation about lazy loading |
|
| 674 | + */ |
|
| 675 | + public function setValueBool( |
|
| 676 | + string $app, |
|
| 677 | + string $key, |
|
| 678 | + bool $value, |
|
| 679 | + bool $lazy = false, |
|
| 680 | + ): bool { |
|
| 681 | + return $this->setTypedValue( |
|
| 682 | + $app, |
|
| 683 | + $key, |
|
| 684 | + ($value) ? '1' : '0', |
|
| 685 | + $lazy, |
|
| 686 | + self::VALUE_BOOL |
|
| 687 | + ); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * @inheritDoc |
|
| 692 | + * |
|
| 693 | + * @param string $app id of the app |
|
| 694 | + * @param string $key config key |
|
| 695 | + * @param array $value config value |
|
| 696 | + * @param bool $lazy set config as lazy loaded |
|
| 697 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 698 | + * |
|
| 699 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 700 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 701 | + * @throws JsonException |
|
| 702 | + * @since 29.0.0 |
|
| 703 | + * @see IAppConfig for explanation about lazy loading |
|
| 704 | + */ |
|
| 705 | + public function setValueArray( |
|
| 706 | + string $app, |
|
| 707 | + string $key, |
|
| 708 | + array $value, |
|
| 709 | + bool $lazy = false, |
|
| 710 | + bool $sensitive = false, |
|
| 711 | + ): bool { |
|
| 712 | + try { |
|
| 713 | + return $this->setTypedValue( |
|
| 714 | + $app, |
|
| 715 | + $key, |
|
| 716 | + json_encode($value, JSON_THROW_ON_ERROR), |
|
| 717 | + $lazy, |
|
| 718 | + self::VALUE_ARRAY | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 719 | + ); |
|
| 720 | + } catch (JsonException $e) { |
|
| 721 | + $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 722 | + throw $e; |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Store a config key and its value in database |
|
| 728 | + * |
|
| 729 | + * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 730 | + * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 731 | + * altered. |
|
| 732 | + * |
|
| 733 | + * @param string $app id of the app |
|
| 734 | + * @param string $key config key |
|
| 735 | + * @param string $value config value |
|
| 736 | + * @param bool $lazy config set as lazy loaded |
|
| 737 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 738 | + * |
|
| 739 | + * @return bool TRUE if value was updated in database |
|
| 740 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 741 | + * @see IAppConfig for explanation about lazy loading |
|
| 742 | + */ |
|
| 743 | + private function setTypedValue( |
|
| 744 | + string $app, |
|
| 745 | + string $key, |
|
| 746 | + string $value, |
|
| 747 | + bool $lazy, |
|
| 748 | + int $type, |
|
| 749 | + ): bool { |
|
| 750 | + $this->assertParams($app, $key); |
|
| 751 | + if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type)) { |
|
| 752 | + return false; // returns false as database is not updated |
|
| 753 | + } |
|
| 754 | + $this->loadConfig(null, $lazy); |
|
| 755 | + |
|
| 756 | + $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $type); |
|
| 757 | + $inserted = $refreshCache = false; |
|
| 758 | + |
|
| 759 | + $origValue = $value; |
|
| 760 | + if ($sensitive || ($this->hasKey($app, $key, $lazy) && $this->isSensitive($app, $key, $lazy))) { |
|
| 761 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + if ($this->hasKey($app, $key, $lazy)) { |
|
| 765 | + /** |
|
| 766 | + * no update if key is already known with set lazy status and value is |
|
| 767 | + * not different, unless sensitivity is switched from false to true. |
|
| 768 | + */ |
|
| 769 | + if ($origValue === $this->getTypedValue($app, $key, $value, $lazy, $type) |
|
| 770 | + && (!$sensitive || $this->isSensitive($app, $key, $lazy))) { |
|
| 771 | + return false; |
|
| 772 | + } |
|
| 773 | + } else { |
|
| 774 | + /** |
|
| 775 | + * if key is not known yet, we try to insert. |
|
| 776 | + * It might fail if the key exists with a different lazy flag. |
|
| 777 | + */ |
|
| 778 | + try { |
|
| 779 | + $insert = $this->connection->getQueryBuilder(); |
|
| 780 | + $insert->insert('appconfig') |
|
| 781 | + ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 782 | + ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 783 | + ->setValue('type', $insert->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 784 | + ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 785 | + ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 786 | + $insert->executeStatement(); |
|
| 787 | + $inserted = true; |
|
| 788 | + } catch (DBException $e) { |
|
| 789 | + if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 790 | + throw $e; // TODO: throw exception or just log and returns false !? |
|
| 791 | + } |
|
| 792 | + } |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * We cannot insert a new row, meaning we need to update an already existing one |
|
| 797 | + */ |
|
| 798 | + if (!$inserted) { |
|
| 799 | + $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 800 | + if ($currType === 0) { // this might happen when switching lazy loading status |
|
| 801 | + $this->loadConfigAll(); |
|
| 802 | + $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * This should only happen during the upgrade process from 28 to 29. |
|
| 807 | + * We only log a warning and set it to VALUE_MIXED. |
|
| 808 | + */ |
|
| 809 | + if ($currType === 0) { |
|
| 810 | + $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); |
|
| 811 | + $currType = self::VALUE_MIXED; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * we only accept a different type from the one stored in database |
|
| 816 | + * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 817 | + */ |
|
| 818 | + if (!$this->isTyped(self::VALUE_MIXED, $currType) && |
|
| 819 | + ($type | self::VALUE_SENSITIVE) !== ($currType | self::VALUE_SENSITIVE)) { |
|
| 820 | + try { |
|
| 821 | + $currType = $this->convertTypeToString($currType); |
|
| 822 | + $type = $this->convertTypeToString($type); |
|
| 823 | + } catch (AppConfigIncorrectTypeException) { |
|
| 824 | + // can be ignored, this was just needed for a better exception message. |
|
| 825 | + } |
|
| 826 | + throw new AppConfigTypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + // we fix $type if the stored value, or the new value as it might be changed, is set as sensitive |
|
| 830 | + if ($sensitive || $this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 831 | + $type |= self::VALUE_SENSITIVE; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + if ($lazy !== $this->isLazy($app, $key)) { |
|
| 835 | + $refreshCache = true; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + $update = $this->connection->getQueryBuilder(); |
|
| 839 | + $update->update('appconfig') |
|
| 840 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 841 | + ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 842 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 843 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 844 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 845 | + |
|
| 846 | + $update->executeStatement(); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + if ($refreshCache) { |
|
| 850 | + $this->clearCache(); |
|
| 851 | + return true; |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + // update local cache |
|
| 855 | + if ($lazy) { |
|
| 856 | + $this->lazyCache[$app][$key] = $value; |
|
| 857 | + } else { |
|
| 858 | + $this->fastCache[$app][$key] = $value; |
|
| 859 | + } |
|
| 860 | + $this->valueTypes[$app][$key] = $type; |
|
| 861 | + |
|
| 862 | + return true; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + /** |
|
| 866 | + * Change the type of config value. |
|
| 867 | + * |
|
| 868 | + * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 869 | + * |
|
| 870 | + * @param string $app id of the app |
|
| 871 | + * @param string $key config key |
|
| 872 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 873 | + * |
|
| 874 | + * @return bool TRUE if database update were necessary |
|
| 875 | + * @throws AppConfigUnknownKeyException if $key is now known in database |
|
| 876 | + * @throws AppConfigIncorrectTypeException if $type is not valid |
|
| 877 | + * @internal |
|
| 878 | + * @since 29.0.0 |
|
| 879 | + */ |
|
| 880 | + public function updateType(string $app, string $key, int $type = self::VALUE_MIXED): bool { |
|
| 881 | + $this->assertParams($app, $key); |
|
| 882 | + $this->loadConfigAll(); |
|
| 883 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 884 | + $this->isLazy($app, $key); // confirm key exists |
|
| 885 | + |
|
| 886 | + // type can only be one type |
|
| 887 | + if (!in_array($type, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 888 | + throw new AppConfigIncorrectTypeException('Unknown value type'); |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + $currType = $this->valueTypes[$app][$key]; |
|
| 892 | + if (($type | self::VALUE_SENSITIVE) === ($currType | self::VALUE_SENSITIVE)) { |
|
| 893 | + return false; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + // we complete with sensitive flag if the stored value is set as sensitive |
|
| 897 | + if ($this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 898 | + $type = $type | self::VALUE_SENSITIVE; |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + $update = $this->connection->getQueryBuilder(); |
|
| 902 | + $update->update('appconfig') |
|
| 903 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 904 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 905 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 906 | + $update->executeStatement(); |
|
| 907 | + $this->valueTypes[$app][$key] = $type; |
|
| 908 | + |
|
| 909 | + return true; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + |
|
| 913 | + /** |
|
| 914 | + * @inheritDoc |
|
| 915 | + * |
|
| 916 | + * @param string $app id of the app |
|
| 917 | + * @param string $key config key |
|
| 918 | + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 919 | + * |
|
| 920 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 921 | + * @since 29.0.0 |
|
| 922 | + */ |
|
| 923 | + public function updateSensitive(string $app, string $key, bool $sensitive): bool { |
|
| 924 | + $this->assertParams($app, $key); |
|
| 925 | + $this->loadConfigAll(); |
|
| 926 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 927 | + |
|
| 928 | + try { |
|
| 929 | + if ($sensitive === $this->isSensitive($app, $key, null)) { |
|
| 930 | + return false; |
|
| 931 | + } |
|
| 932 | + } catch (AppConfigUnknownKeyException $e) { |
|
| 933 | + return false; |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + $lazy = $this->isLazy($app, $key); |
|
| 937 | + if ($lazy) { |
|
| 938 | + $cache = $this->lazyCache; |
|
| 939 | + } else { |
|
| 940 | + $cache = $this->fastCache; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + if (!isset($cache[$app][$key])) { |
|
| 944 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * type returned by getValueType() is already cleaned from sensitive flag |
|
| 949 | + * we just need to update it based on $sensitive and store it in database |
|
| 950 | + */ |
|
| 951 | + $type = $this->getValueType($app, $key); |
|
| 952 | + $value = $cache[$app][$key]; |
|
| 953 | + if ($sensitive) { |
|
| 954 | + $type |= self::VALUE_SENSITIVE; |
|
| 955 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 956 | + } else { |
|
| 957 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + $update = $this->connection->getQueryBuilder(); |
|
| 961 | + $update->update('appconfig') |
|
| 962 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 963 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 964 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 965 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 966 | + $update->executeStatement(); |
|
| 967 | + |
|
| 968 | + $this->valueTypes[$app][$key] = $type; |
|
| 969 | + |
|
| 970 | + return true; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + /** |
|
| 974 | + * @inheritDoc |
|
| 975 | + * |
|
| 976 | + * @param string $app id of the app |
|
| 977 | + * @param string $key config key |
|
| 978 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 979 | + * |
|
| 980 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 981 | + * @since 29.0.0 |
|
| 982 | + */ |
|
| 983 | + public function updateLazy(string $app, string $key, bool $lazy): bool { |
|
| 984 | + $this->assertParams($app, $key); |
|
| 985 | + $this->loadConfigAll(); |
|
| 986 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 987 | + |
|
| 988 | + try { |
|
| 989 | + if ($lazy === $this->isLazy($app, $key)) { |
|
| 990 | + return false; |
|
| 991 | + } |
|
| 992 | + } catch (AppConfigUnknownKeyException $e) { |
|
| 993 | + return false; |
|
| 994 | + } |
|
| 995 | + |
|
| 996 | + $update = $this->connection->getQueryBuilder(); |
|
| 997 | + $update->update('appconfig') |
|
| 998 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 999 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1000 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1001 | + $update->executeStatement(); |
|
| 1002 | + |
|
| 1003 | + // At this point, it is a lot safer to clean cache |
|
| 1004 | + $this->clearCache(); |
|
| 1005 | + |
|
| 1006 | + return true; |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + /** |
|
| 1010 | + * @inheritDoc |
|
| 1011 | + * |
|
| 1012 | + * @param string $app id of the app |
|
| 1013 | + * @param string $key config key |
|
| 1014 | + * |
|
| 1015 | + * @return array |
|
| 1016 | + * @throws AppConfigUnknownKeyException if config key is not known in database |
|
| 1017 | + * @since 29.0.0 |
|
| 1018 | + */ |
|
| 1019 | + public function getDetails(string $app, string $key): array { |
|
| 1020 | + $this->assertParams($app, $key); |
|
| 1021 | + $this->loadConfigAll(); |
|
| 1022 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1023 | + $lazy = $this->isLazy($app, $key); |
|
| 1024 | + |
|
| 1025 | + if ($lazy) { |
|
| 1026 | + $cache = $this->lazyCache; |
|
| 1027 | + } else { |
|
| 1028 | + $cache = $this->fastCache; |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + $type = $this->getValueType($app, $key); |
|
| 1032 | + try { |
|
| 1033 | + $typeString = $this->convertTypeToString($type); |
|
| 1034 | + } catch (AppConfigIncorrectTypeException $e) { |
|
| 1035 | + $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1036 | + $typeString = (string)$type; |
|
| 1037 | + } |
|
| 1038 | + |
|
| 1039 | + if (!isset($cache[$app][$key])) { |
|
| 1040 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 1041 | + } |
|
| 1042 | + |
|
| 1043 | + $value = $cache[$app][$key]; |
|
| 1044 | + $sensitive = $this->isSensitive($app, $key, null); |
|
| 1045 | + if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1046 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + return [ |
|
| 1050 | + 'app' => $app, |
|
| 1051 | + 'key' => $key, |
|
| 1052 | + 'value' => $value, |
|
| 1053 | + 'type' => $type, |
|
| 1054 | + 'lazy' => $lazy, |
|
| 1055 | + 'typeString' => $typeString, |
|
| 1056 | + 'sensitive' => $sensitive |
|
| 1057 | + ]; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * @param string $type |
|
| 1062 | + * |
|
| 1063 | + * @return int |
|
| 1064 | + * @throws AppConfigIncorrectTypeException |
|
| 1065 | + * @since 29.0.0 |
|
| 1066 | + */ |
|
| 1067 | + public function convertTypeToInt(string $type): int { |
|
| 1068 | + return match (strtolower($type)) { |
|
| 1069 | + 'mixed' => IAppConfig::VALUE_MIXED, |
|
| 1070 | + 'string' => IAppConfig::VALUE_STRING, |
|
| 1071 | + 'integer' => IAppConfig::VALUE_INT, |
|
| 1072 | + 'float' => IAppConfig::VALUE_FLOAT, |
|
| 1073 | + 'boolean' => IAppConfig::VALUE_BOOL, |
|
| 1074 | + 'array' => IAppConfig::VALUE_ARRAY, |
|
| 1075 | + default => throw new AppConfigIncorrectTypeException('Unknown type ' . $type) |
|
| 1076 | + }; |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + /** |
|
| 1080 | + * @param int $type |
|
| 1081 | + * |
|
| 1082 | + * @return string |
|
| 1083 | + * @throws AppConfigIncorrectTypeException |
|
| 1084 | + * @since 29.0.0 |
|
| 1085 | + */ |
|
| 1086 | + public function convertTypeToString(int $type): string { |
|
| 1087 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 1088 | + |
|
| 1089 | + return match ($type) { |
|
| 1090 | + IAppConfig::VALUE_MIXED => 'mixed', |
|
| 1091 | + IAppConfig::VALUE_STRING => 'string', |
|
| 1092 | + IAppConfig::VALUE_INT => 'integer', |
|
| 1093 | + IAppConfig::VALUE_FLOAT => 'float', |
|
| 1094 | + IAppConfig::VALUE_BOOL => 'boolean', |
|
| 1095 | + IAppConfig::VALUE_ARRAY => 'array', |
|
| 1096 | + default => throw new AppConfigIncorrectTypeException('Unknown numeric type ' . $type) |
|
| 1097 | + }; |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + /** |
|
| 1101 | + * @inheritDoc |
|
| 1102 | + * |
|
| 1103 | + * @param string $app id of the app |
|
| 1104 | + * @param string $key config key |
|
| 1105 | + * |
|
| 1106 | + * @since 29.0.0 |
|
| 1107 | + */ |
|
| 1108 | + public function deleteKey(string $app, string $key): void { |
|
| 1109 | + $this->assertParams($app, $key); |
|
| 1110 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1111 | + |
|
| 1112 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1113 | + $qb->delete('appconfig') |
|
| 1114 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1115 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1116 | + $qb->executeStatement(); |
|
| 1117 | + |
|
| 1118 | + unset($this->lazyCache[$app][$key]); |
|
| 1119 | + unset($this->fastCache[$app][$key]); |
|
| 1120 | + unset($this->valueTypes[$app][$key]); |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + /** |
|
| 1124 | + * @inheritDoc |
|
| 1125 | + * |
|
| 1126 | + * @param string $app id of the app |
|
| 1127 | + * |
|
| 1128 | + * @since 29.0.0 |
|
| 1129 | + */ |
|
| 1130 | + public function deleteApp(string $app): void { |
|
| 1131 | + $this->assertParams($app); |
|
| 1132 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1133 | + $qb->delete('appconfig') |
|
| 1134 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1135 | + $qb->executeStatement(); |
|
| 1136 | + |
|
| 1137 | + $this->clearCache(); |
|
| 1138 | + } |
|
| 1139 | + |
|
| 1140 | + /** |
|
| 1141 | + * @inheritDoc |
|
| 1142 | + * |
|
| 1143 | + * @param bool $reload set to TRUE to refill cache instantly after clearing it |
|
| 1144 | + * |
|
| 1145 | + * @since 29.0.0 |
|
| 1146 | + */ |
|
| 1147 | + public function clearCache(bool $reload = false): void { |
|
| 1148 | + $this->lazyLoaded = $this->fastLoaded = false; |
|
| 1149 | + $this->lazyCache = $this->fastCache = $this->valueTypes = []; |
|
| 1150 | + |
|
| 1151 | + if (!$reload) { |
|
| 1152 | + return; |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + $this->loadConfigAll(); |
|
| 1156 | + } |
|
| 1157 | + |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * For debug purpose. |
|
| 1161 | + * Returns the cached data. |
|
| 1162 | + * |
|
| 1163 | + * @return array |
|
| 1164 | + * @since 29.0.0 |
|
| 1165 | + * @internal |
|
| 1166 | + */ |
|
| 1167 | + public function statusCache(): array { |
|
| 1168 | + return [ |
|
| 1169 | + 'fastLoaded' => $this->fastLoaded, |
|
| 1170 | + 'fastCache' => $this->fastCache, |
|
| 1171 | + 'lazyLoaded' => $this->lazyLoaded, |
|
| 1172 | + 'lazyCache' => $this->lazyCache, |
|
| 1173 | + ]; |
|
| 1174 | + } |
|
| 1175 | + |
|
| 1176 | + /** |
|
| 1177 | + * @param int $needle bitflag to search |
|
| 1178 | + * @param int $type known value |
|
| 1179 | + * |
|
| 1180 | + * @return bool TRUE if bitflag $needle is set in $type |
|
| 1181 | + */ |
|
| 1182 | + private function isTyped(int $needle, int $type): bool { |
|
| 1183 | + return (($needle & $type) !== 0); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Confirm the string set for app and key fit the database description |
|
| 1188 | + * |
|
| 1189 | + * @param string $app assert $app fit in database |
|
| 1190 | + * @param string $configKey assert config key fit in database |
|
| 1191 | + * @param bool $allowEmptyApp $app can be empty string |
|
| 1192 | + * @param int $valueType assert value type is only one type |
|
| 1193 | + * |
|
| 1194 | + * @throws InvalidArgumentException |
|
| 1195 | + */ |
|
| 1196 | + private function assertParams(string $app = '', string $configKey = '', bool $allowEmptyApp = false, int $valueType = -1): void { |
|
| 1197 | + if (!$allowEmptyApp && $app === '') { |
|
| 1198 | + throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1199 | + } |
|
| 1200 | + if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1201 | + throw new InvalidArgumentException( |
|
| 1202 | + 'Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')' |
|
| 1203 | + ); |
|
| 1204 | + } |
|
| 1205 | + if (strlen($configKey) > self::KEY_MAX_LENGTH) { |
|
| 1206 | + throw new InvalidArgumentException('Value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1207 | + } |
|
| 1208 | + if ($valueType > -1) { |
|
| 1209 | + $valueType &= ~self::VALUE_SENSITIVE; |
|
| 1210 | + if (!in_array($valueType, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 1211 | + throw new InvalidArgumentException('Unknown value type'); |
|
| 1212 | + } |
|
| 1213 | + } |
|
| 1214 | + } |
|
| 1215 | + |
|
| 1216 | + private function loadConfigAll(?string $app = null): void { |
|
| 1217 | + $this->loadConfig($app, null); |
|
| 1218 | + } |
|
| 1219 | + |
|
| 1220 | + /** |
|
| 1221 | + * Load normal config or config set as lazy loaded |
|
| 1222 | + * |
|
| 1223 | + * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1224 | + */ |
|
| 1225 | + private function loadConfig(?string $app = null, ?bool $lazy = false): void { |
|
| 1226 | + if ($this->isLoaded($lazy)) { |
|
| 1227 | + return; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + // if lazy is null or true, we debug log |
|
| 1231 | + if (($lazy ?? true) !== false && $app !== null) { |
|
| 1232 | + $exception = new \RuntimeException('The loading of lazy AppConfig values have been triggered by app "' . $app . '"'); |
|
| 1233 | + $this->logger->debug($exception->getMessage(), ['exception' => $exception, 'app' => $app]); |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1237 | + $qb->from('appconfig'); |
|
| 1238 | + |
|
| 1239 | + // we only need value from lazy when loadConfig does not specify it |
|
| 1240 | + $qb->select('appid', 'configkey', 'configvalue', 'type'); |
|
| 1241 | + |
|
| 1242 | + if ($lazy !== null) { |
|
| 1243 | + $qb->where($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1244 | + } else { |
|
| 1245 | + $qb->addSelect('lazy'); |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + $result = $qb->executeQuery(); |
|
| 1249 | + $rows = $result->fetchAll(); |
|
| 1250 | + foreach ($rows as $row) { |
|
| 1251 | + // most of the time, 'lazy' is not in the select because its value is already known |
|
| 1252 | + if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1253 | + $this->lazyCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1254 | + } else { |
|
| 1255 | + $this->fastCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1256 | + } |
|
| 1257 | + $this->valueTypes[$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); |
|
| 1258 | + } |
|
| 1259 | + $result->closeCursor(); |
|
| 1260 | + $this->setAsLoaded($lazy); |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + /** |
|
| 1264 | + * if $lazy is: |
|
| 1265 | + * - false: will returns true if fast config is loaded |
|
| 1266 | + * - true : will returns true if lazy config is loaded |
|
| 1267 | + * - null : will returns true if both config are loaded |
|
| 1268 | + * |
|
| 1269 | + * @param bool $lazy |
|
| 1270 | + * |
|
| 1271 | + * @return bool |
|
| 1272 | + */ |
|
| 1273 | + private function isLoaded(?bool $lazy): bool { |
|
| 1274 | + if ($lazy === null) { |
|
| 1275 | + return $this->lazyLoaded && $this->fastLoaded; |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + return $lazy ? $this->lazyLoaded : $this->fastLoaded; |
|
| 1279 | + } |
|
| 1280 | + |
|
| 1281 | + /** |
|
| 1282 | + * if $lazy is: |
|
| 1283 | + * - false: set fast config as loaded |
|
| 1284 | + * - true : set lazy config as loaded |
|
| 1285 | + * - null : set both config as loaded |
|
| 1286 | + * |
|
| 1287 | + * @param bool $lazy |
|
| 1288 | + */ |
|
| 1289 | + private function setAsLoaded(?bool $lazy): void { |
|
| 1290 | + if ($lazy === null) { |
|
| 1291 | + $this->fastLoaded = true; |
|
| 1292 | + $this->lazyLoaded = true; |
|
| 1293 | + |
|
| 1294 | + return; |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + if ($lazy) { |
|
| 1298 | + $this->lazyLoaded = true; |
|
| 1299 | + } else { |
|
| 1300 | + $this->fastLoaded = true; |
|
| 1301 | + } |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + /** |
|
| 1305 | + * Gets the config value |
|
| 1306 | + * |
|
| 1307 | + * @param string $app app |
|
| 1308 | + * @param string $key key |
|
| 1309 | + * @param string $default = null, default value if the key does not exist |
|
| 1310 | + * |
|
| 1311 | + * @return string the value or $default |
|
| 1312 | + * @deprecated 29.0.0 use getValue*() |
|
| 1313 | + * |
|
| 1314 | + * This function gets a value from the appconfig table. If the key does |
|
| 1315 | + * not exist the default value will be returned |
|
| 1316 | + */ |
|
| 1317 | + public function getValue($app, $key, $default = null) { |
|
| 1318 | + $this->loadConfig($app); |
|
| 1319 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1320 | + |
|
| 1321 | + return $this->fastCache[$app][$key] ?? $default; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + /** |
|
| 1325 | + * Sets a value. If the key did not exist before it will be created. |
|
| 1326 | + * |
|
| 1327 | + * @param string $app app |
|
| 1328 | + * @param string $key key |
|
| 1329 | + * @param string|float|int $value value |
|
| 1330 | + * |
|
| 1331 | + * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 1332 | + * @throws AppConfigTypeConflictException |
|
| 1333 | + * @throws AppConfigUnknownKeyException |
|
| 1334 | + * @deprecated 29.0.0 |
|
| 1335 | + */ |
|
| 1336 | + public function setValue($app, $key, $value) { |
|
| 1337 | + /** |
|
| 1338 | + * TODO: would it be overkill, or decently improve performance, to catch |
|
| 1339 | + * call to this method with $key='enabled' and 'hide' config value related |
|
| 1340 | + * to $app when the app is disabled (by modifying entry in database: lazy=lazy+2) |
|
| 1341 | + * or enabled (lazy=lazy-2) |
|
| 1342 | + * |
|
| 1343 | + * this solution would remove the loading of config values from disabled app |
|
| 1344 | + * unless calling the method {@see loadConfigAll()} |
|
| 1345 | + */ |
|
| 1346 | + return $this->setTypedValue($app, $key, (string)$value, false, self::VALUE_MIXED); |
|
| 1347 | + } |
|
| 1348 | + |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 1352 | + * |
|
| 1353 | + * @param string|false $app |
|
| 1354 | + * @param string|false $key |
|
| 1355 | + * |
|
| 1356 | + * @return array|false |
|
| 1357 | + * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1358 | + */ |
|
| 1359 | + public function getValues($app, $key) { |
|
| 1360 | + if (($app !== false) === ($key !== false)) { |
|
| 1361 | + return false; |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + $key = ($key === false) ? '' : $key; |
|
| 1365 | + if (!$app) { |
|
| 1366 | + return $this->searchValues($key, false, self::VALUE_MIXED); |
|
| 1367 | + } else { |
|
| 1368 | + return $this->getAllValues($app, $key); |
|
| 1369 | + } |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * get all values of the app or and filters out sensitive data |
|
| 1374 | + * |
|
| 1375 | + * @param string $app |
|
| 1376 | + * |
|
| 1377 | + * @return array |
|
| 1378 | + * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1379 | + */ |
|
| 1380 | + public function getFilteredValues($app) { |
|
| 1381 | + return $this->getAllValues($app, filtered: true); |
|
| 1382 | + } |
|
| 1383 | + |
|
| 1384 | + |
|
| 1385 | + /** |
|
| 1386 | + * **Warning:** avoid default NULL value for $lazy as this will |
|
| 1387 | + * load all lazy values from the database |
|
| 1388 | + * |
|
| 1389 | + * @param string $app |
|
| 1390 | + * @param array<string, string> $values ['key' => 'value'] |
|
| 1391 | + * @param bool|null $lazy |
|
| 1392 | + * |
|
| 1393 | + * @return array<string, string|int|float|bool|array> |
|
| 1394 | + */ |
|
| 1395 | + private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { |
|
| 1396 | + foreach ($values as $key => $value) { |
|
| 1397 | + try { |
|
| 1398 | + $type = $this->getValueType($app, $key, $lazy); |
|
| 1399 | + } catch (AppConfigUnknownKeyException) { |
|
| 1400 | + continue; |
|
| 1401 | + } |
|
| 1402 | + |
|
| 1403 | + $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + return $values; |
|
| 1407 | + } |
|
| 1408 | + |
|
| 1409 | + /** |
|
| 1410 | + * convert string value to the expected type |
|
| 1411 | + * |
|
| 1412 | + * @param string $value |
|
| 1413 | + * @param int $type |
|
| 1414 | + * |
|
| 1415 | + * @return string|int|float|bool|array |
|
| 1416 | + */ |
|
| 1417 | + private function convertTypedValue(string $value, int $type): string|int|float|bool|array { |
|
| 1418 | + switch ($type) { |
|
| 1419 | + case self::VALUE_INT: |
|
| 1420 | + return (int)$value; |
|
| 1421 | + case self::VALUE_FLOAT: |
|
| 1422 | + return (float)$value; |
|
| 1423 | + case self::VALUE_BOOL: |
|
| 1424 | + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1425 | + case self::VALUE_ARRAY: |
|
| 1426 | + try { |
|
| 1427 | + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1428 | + } catch (JsonException $e) { |
|
| 1429 | + // ignoreable |
|
| 1430 | + } |
|
| 1431 | + break; |
|
| 1432 | + } |
|
| 1433 | + return $value; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + /** |
|
| 1437 | + * @param string $app |
|
| 1438 | + * |
|
| 1439 | + * @return string[] |
|
| 1440 | + * @deprecated 29.0.0 data sensitivity should be set when calling setValue*() |
|
| 1441 | + */ |
|
| 1442 | + private function getSensitiveKeys(string $app): array { |
|
| 1443 | + $sensitiveValues = [ |
|
| 1444 | + 'circles' => [ |
|
| 1445 | + '/^key_pairs$/', |
|
| 1446 | + '/^local_gskey$/', |
|
| 1447 | + ], |
|
| 1448 | + 'call_summary_bot' => [ |
|
| 1449 | + '/^secret_(.*)$/', |
|
| 1450 | + ], |
|
| 1451 | + 'external' => [ |
|
| 1452 | + '/^sites$/', |
|
| 1453 | + '/^jwt_token_privkey_(.*)$/', |
|
| 1454 | + ], |
|
| 1455 | + 'globalsiteselector' => [ |
|
| 1456 | + '/^gss\.jwt\.key$/', |
|
| 1457 | + ], |
|
| 1458 | + 'gpgmailer' => [ |
|
| 1459 | + '/^GpgServerKey$/', |
|
| 1460 | + ], |
|
| 1461 | + 'integration_discourse' => [ |
|
| 1462 | + '/^private_key$/', |
|
| 1463 | + '/^public_key$/', |
|
| 1464 | + ], |
|
| 1465 | + 'integration_dropbox' => [ |
|
| 1466 | + '/^client_id$/', |
|
| 1467 | + '/^client_secret$/', |
|
| 1468 | + ], |
|
| 1469 | + 'integration_github' => [ |
|
| 1470 | + '/^client_id$/', |
|
| 1471 | + '/^client_secret$/', |
|
| 1472 | + ], |
|
| 1473 | + 'integration_gitlab' => [ |
|
| 1474 | + '/^client_id$/', |
|
| 1475 | + '/^client_secret$/', |
|
| 1476 | + '/^oauth_instance_url$/', |
|
| 1477 | + ], |
|
| 1478 | + 'integration_google' => [ |
|
| 1479 | + '/^client_id$/', |
|
| 1480 | + '/^client_secret$/', |
|
| 1481 | + ], |
|
| 1482 | + 'integration_jira' => [ |
|
| 1483 | + '/^client_id$/', |
|
| 1484 | + '/^client_secret$/', |
|
| 1485 | + '/^forced_instance_url$/', |
|
| 1486 | + ], |
|
| 1487 | + 'integration_onedrive' => [ |
|
| 1488 | + '/^client_id$/', |
|
| 1489 | + '/^client_secret$/', |
|
| 1490 | + ], |
|
| 1491 | + 'integration_openproject' => [ |
|
| 1492 | + '/^client_id$/', |
|
| 1493 | + '/^client_secret$/', |
|
| 1494 | + '/^oauth_instance_url$/', |
|
| 1495 | + ], |
|
| 1496 | + 'integration_reddit' => [ |
|
| 1497 | + '/^client_id$/', |
|
| 1498 | + '/^client_secret$/', |
|
| 1499 | + ], |
|
| 1500 | + 'integration_suitecrm' => [ |
|
| 1501 | + '/^client_id$/', |
|
| 1502 | + '/^client_secret$/', |
|
| 1503 | + '/^oauth_instance_url$/', |
|
| 1504 | + ], |
|
| 1505 | + 'integration_twitter' => [ |
|
| 1506 | + '/^consumer_key$/', |
|
| 1507 | + '/^consumer_secret$/', |
|
| 1508 | + '/^followed_user$/', |
|
| 1509 | + ], |
|
| 1510 | + 'integration_zammad' => [ |
|
| 1511 | + '/^client_id$/', |
|
| 1512 | + '/^client_secret$/', |
|
| 1513 | + '/^oauth_instance_url$/', |
|
| 1514 | + ], |
|
| 1515 | + 'maps' => [ |
|
| 1516 | + '/^mapboxAPIKEY$/', |
|
| 1517 | + ], |
|
| 1518 | + 'notify_push' => [ |
|
| 1519 | + '/^cookie$/', |
|
| 1520 | + ], |
|
| 1521 | + 'onlyoffice' => [ |
|
| 1522 | + '/^jwt_secret$/', |
|
| 1523 | + ], |
|
| 1524 | + 'passwords' => [ |
|
| 1525 | + '/^SSEv1ServerKey$/', |
|
| 1526 | + ], |
|
| 1527 | + 'serverinfo' => [ |
|
| 1528 | + '/^token$/', |
|
| 1529 | + ], |
|
| 1530 | + 'spreed' => [ |
|
| 1531 | + '/^bridge_bot_password$/', |
|
| 1532 | + '/^hosted-signaling-server-(.*)$/', |
|
| 1533 | + '/^recording_servers$/', |
|
| 1534 | + '/^signaling_servers$/', |
|
| 1535 | + '/^signaling_ticket_secret$/', |
|
| 1536 | + '/^signaling_token_privkey_(.*)$/', |
|
| 1537 | + '/^signaling_token_pubkey_(.*)$/', |
|
| 1538 | + '/^sip_bridge_dialin_info$/', |
|
| 1539 | + '/^sip_bridge_shared_secret$/', |
|
| 1540 | + '/^stun_servers$/', |
|
| 1541 | + '/^turn_servers$/', |
|
| 1542 | + '/^turn_server_secret$/', |
|
| 1543 | + ], |
|
| 1544 | + 'support' => [ |
|
| 1545 | + '/^last_response$/', |
|
| 1546 | + '/^potential_subscription_key$/', |
|
| 1547 | + '/^subscription_key$/', |
|
| 1548 | + ], |
|
| 1549 | + 'theming' => [ |
|
| 1550 | + '/^imprintUrl$/', |
|
| 1551 | + '/^privacyUrl$/', |
|
| 1552 | + '/^slogan$/', |
|
| 1553 | + '/^url$/', |
|
| 1554 | + ], |
|
| 1555 | + 'twofactor_gateway' => [ |
|
| 1556 | + '/^.*token$/', |
|
| 1557 | + ], |
|
| 1558 | + 'user_ldap' => [ |
|
| 1559 | + '/^(s..)?ldap_agent_password$/', |
|
| 1560 | + ], |
|
| 1561 | + 'user_saml' => [ |
|
| 1562 | + '/^idp-x509cert$/', |
|
| 1563 | + ], |
|
| 1564 | + 'whiteboard' => [ |
|
| 1565 | + '/^jwt_secret_key$/', |
|
| 1566 | + ], |
|
| 1567 | + ]; |
|
| 1568 | + |
|
| 1569 | + return $sensitiveValues[$app] ?? []; |
|
| 1570 | + } |
|
| 1571 | + |
|
| 1572 | + /** |
|
| 1573 | + * Clear all the cached app config values |
|
| 1574 | + * New cache will be generated next time a config value is retrieved |
|
| 1575 | + * |
|
| 1576 | + * @deprecated 29.0.0 use {@see clearCache()} |
|
| 1577 | + */ |
|
| 1578 | + public function clearCachedConfig(): void { |
|
| 1579 | + $this->clearCache(); |
|
| 1580 | + } |
|
| 1581 | + |
|
| 1582 | + /** |
|
| 1583 | + * Match and apply current use of config values with defined lexicon. |
|
| 1584 | + * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1585 | + * |
|
| 1586 | + * @throws AppConfigUnknownKeyException |
|
| 1587 | + * @throws AppConfigTypeConflictException |
|
| 1588 | + * @return bool TRUE if everything is fine compared to lexicon or lexicon does not exist |
|
| 1589 | + */ |
|
| 1590 | + private function matchAndApplyLexiconDefinition( |
|
| 1591 | + string $app, |
|
| 1592 | + string &$key, |
|
| 1593 | + ?bool &$lazy = null, |
|
| 1594 | + int &$type = self::VALUE_MIXED, |
|
| 1595 | + string &$default = '', |
|
| 1596 | + ): bool { |
|
| 1597 | + if (in_array($key, |
|
| 1598 | + [ |
|
| 1599 | + 'enabled', |
|
| 1600 | + 'installed_version', |
|
| 1601 | + 'types', |
|
| 1602 | + ])) { |
|
| 1603 | + return true; // we don't break stuff for this list of config keys. |
|
| 1604 | + } |
|
| 1605 | + $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1606 | + if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1607 | + // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1608 | + $key = $configDetails['aliases'][$key]; |
|
| 1609 | + } |
|
| 1610 | + |
|
| 1611 | + if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1612 | + return $this->applyLexiconStrictness($configDetails['strictness'], 'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1613 | + } |
|
| 1614 | + |
|
| 1615 | + // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1616 | + if ($lazy === null) { |
|
| 1617 | + return true; |
|
| 1618 | + } |
|
| 1619 | + |
|
| 1620 | + /** @var ConfigLexiconEntry $configValue */ |
|
| 1621 | + $configValue = $configDetails['entries'][$key]; |
|
| 1622 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 1623 | + |
|
| 1624 | + $appConfigValueType = $configValue->getValueType()->toAppConfigFlag(); |
|
| 1625 | + if ($type === self::VALUE_MIXED) { |
|
| 1626 | + $type = $appConfigValueType; // we overwrite if value was requested as mixed |
|
| 1627 | + } elseif ($appConfigValueType !== $type) { |
|
| 1628 | + throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1629 | + } |
|
| 1630 | + |
|
| 1631 | + $lazy = $configValue->isLazy(); |
|
| 1632 | + $default = $configValue->getDefault() ?? $default; // default from Lexicon got priority |
|
| 1633 | + if ($configValue->isFlagged(self::FLAG_SENSITIVE)) { |
|
| 1634 | + $type |= self::VALUE_SENSITIVE; |
|
| 1635 | + } |
|
| 1636 | + if ($configValue->isDeprecated()) { |
|
| 1637 | + $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1638 | + } |
|
| 1639 | + |
|
| 1640 | + return true; |
|
| 1641 | + } |
|
| 1642 | + |
|
| 1643 | + /** |
|
| 1644 | + * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1645 | + * |
|
| 1646 | + * @param ConfigLexiconStrictness|null $strictness |
|
| 1647 | + * @param string $line |
|
| 1648 | + * |
|
| 1649 | + * @return bool TRUE if conflict can be fully ignored, FALSE if action should be not performed |
|
| 1650 | + * @throws AppConfigUnknownKeyException if strictness implies exception |
|
| 1651 | + * @see IConfigLexicon::getStrictness() |
|
| 1652 | + */ |
|
| 1653 | + private function applyLexiconStrictness( |
|
| 1654 | + ?ConfigLexiconStrictness $strictness, |
|
| 1655 | + string $line = '', |
|
| 1656 | + ): bool { |
|
| 1657 | + if ($strictness === null) { |
|
| 1658 | + return true; |
|
| 1659 | + } |
|
| 1660 | + |
|
| 1661 | + switch ($strictness) { |
|
| 1662 | + case ConfigLexiconStrictness::IGNORE: |
|
| 1663 | + return true; |
|
| 1664 | + case ConfigLexiconStrictness::NOTICE: |
|
| 1665 | + $this->logger->notice($line); |
|
| 1666 | + return true; |
|
| 1667 | + case ConfigLexiconStrictness::WARNING: |
|
| 1668 | + $this->logger->warning($line); |
|
| 1669 | + return false; |
|
| 1670 | + } |
|
| 1671 | + |
|
| 1672 | + throw new AppConfigUnknownKeyException($line); |
|
| 1673 | + } |
|
| 1674 | + |
|
| 1675 | + /** |
|
| 1676 | + * extract details from registered $appId's config lexicon |
|
| 1677 | + * |
|
| 1678 | + * @param string $appId |
|
| 1679 | + * @internal |
|
| 1680 | + * |
|
| 1681 | + * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1682 | + */ |
|
| 1683 | + public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1684 | + if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1685 | + $entries = $aliases = []; |
|
| 1686 | + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1687 | + $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1688 | + foreach ($configLexicon?->getAppConfigs() ?? [] as $configEntry) { |
|
| 1689 | + $entries[$configEntry->getKey()] = $configEntry; |
|
| 1690 | + if ($configEntry->getRename() !== null) { |
|
| 1691 | + $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 1692 | + } |
|
| 1693 | + } |
|
| 1694 | + |
|
| 1695 | + $this->configLexiconDetails[$appId] = [ |
|
| 1696 | + 'entries' => $entries, |
|
| 1697 | + 'aliases' => $aliases, |
|
| 1698 | + 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 1699 | + ]; |
|
| 1700 | + } |
|
| 1701 | + |
|
| 1702 | + return $this->configLexiconDetails[$appId]; |
|
| 1703 | + } |
|
| 1704 | + |
|
| 1705 | + private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 1706 | + return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 1707 | + } |
|
| 1708 | + |
|
| 1709 | + /** |
|
| 1710 | + * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 1711 | + * |
|
| 1712 | + * @internal |
|
| 1713 | + */ |
|
| 1714 | + public function ignoreLexiconAliases(bool $ignore): void { |
|
| 1715 | + $this->ignoreLexiconAliases = $ignore; |
|
| 1716 | + } |
|
| 1717 | + |
|
| 1718 | + /** |
|
| 1719 | + * Returns the installed versions of all apps |
|
| 1720 | + * |
|
| 1721 | + * @return array<string, string> |
|
| 1722 | + */ |
|
| 1723 | + public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 1724 | + if ($this->appVersionsCache === null) { |
|
| 1725 | + /** @var array<string, string> */ |
|
| 1726 | + $this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING); |
|
| 1727 | + } |
|
| 1728 | + if ($onlyEnabled) { |
|
| 1729 | + return array_filter( |
|
| 1730 | + $this->appVersionsCache, |
|
| 1731 | + fn (string $app): bool => $this->getValueString($app, 'enabled', 'no') !== 'no', |
|
| 1732 | + ARRAY_FILTER_USE_KEY |
|
| 1733 | + ); |
|
| 1734 | + } |
|
| 1735 | + return $this->appVersionsCache; |
|
| 1736 | + } |
|
| 1737 | 1737 | } |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | use OCP\Migration\IRepairStep; |
| 15 | 15 | |
| 16 | 16 | class ConfigKeyMigration implements IRepairStep { |
| 17 | - public function __construct( |
|
| 18 | - private ConfigManager $configManager, |
|
| 19 | - ) { |
|
| 20 | - } |
|
| 17 | + public function __construct( |
|
| 18 | + private ConfigManager $configManager, |
|
| 19 | + ) { |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function getName(): string { |
|
| 23 | - return 'Migrate config keys'; |
|
| 24 | - } |
|
| 22 | + public function getName(): string { |
|
| 23 | + return 'Migrate config keys'; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function run(IOutput $output) { |
|
| 27 | - $this->configManager->migrateConfigLexiconKeys(); |
|
| 28 | - } |
|
| 26 | + public function run(IOutput $output) { |
|
| 27 | + $this->configManager->migrateConfigLexiconKeys(); |
|
| 28 | + } |
|
| 29 | 29 | } |