|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OC\Config\Model; |
| 4 | + |
| 5 | +use OCP\Server; |
| 6 | +use Psr\Log\LoggerInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * @since 32.0.0 |
| 10 | + */ |
| 11 | +class PresetDefault { |
| 12 | + private array $preset = []; |
| 13 | + public function __construct( |
| 14 | + private readonly string $presetFile, |
| 15 | + private readonly bool $invalidate = false, |
| 16 | + ) { |
| 17 | + $this->loadEntries(); |
| 18 | + } |
| 19 | + |
| 20 | + public function getKeys(string $appId): array { |
| 21 | + return array_keys($this->preset[$appId] ?? []); |
| 22 | + } |
| 23 | + |
| 24 | + public function isAppConfigEnforced(string $appId, string $key): bool { |
| 25 | + return (($this->preset[$appId]['appConfig']['!' . $key] ?? null) !== null); |
| 26 | + } |
| 27 | + |
| 28 | + public function getAppConfigDefault(string $appId, string $key): ?string { |
| 29 | + return $this->preset[$appId]['appConfig']['!' . $key] ?? $this->preset[$appId]['appConfig'][$key] ?? null; |
| 30 | + } |
| 31 | + |
| 32 | + public function isUserConfigEnforced(string $appId, string $key): bool { |
| 33 | + return (($this->preset[$appId]['userConfig']['!' . $key] ?? null) !== null); |
| 34 | + } |
| 35 | + |
| 36 | + public function getUserConfigDefault(string $appId, string $key): ?string { |
| 37 | + return $this->preset[$appId]['userConfig'][$key] ?? null; |
| 38 | + } |
| 39 | + |
| 40 | + private function loadEntries(): void { |
| 41 | + if ($this->invalidate && function_exists('opcache_invalidate')) { |
| 42 | + @opcache_invalidate($this->presetFile, false); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + $fp = (file_exists($this->presetFile) ? fopen($this->presetFile, 'r') : false); |
| 47 | + if (!$fp) { |
| 48 | + Server::get(LoggerInterface::class)->warning(sprintf('Preset file %s does not exist', $this->presetFile)); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!flock($fp, LOCK_SH)) { |
| 53 | + Server::get(LoggerInterface::class)->warning(sprintf('Could not acquire a shared lock on preset file %s', $this->presetFile)); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + $alreadySent = (!defined('PHPUNIT_RUN') && headers_sent()); |
| 59 | + $preset = include $this->presetFile; |
| 60 | + } finally { |
| 61 | + flock($fp, LOCK_UN); |
| 62 | + fclose($fp); |
| 63 | + } |
| 64 | + |
| 65 | + if (!$alreadySent && !defined('PHPUNIT_RUN') && headers_sent()) { |
| 66 | + Server::get(LoggerInterface::class)->warning(sprintf('Preset file has leading content, please remove everything before "<?php" in %s', $this->presetFile)); |
| 67 | + throw new \Exception('preset file has leading content.'); |
| 68 | + } |
| 69 | + |
| 70 | + if ($preset && is_array($preset)) { |
| 71 | + $this->preset = $preset; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments