UpdateConfig.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Command;
  7. use OCA\Theming\ImageManager;
  8. use OCA\Theming\ThemingDefaults;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class UpdateConfig extends Command {
  16. public const SUPPORTED_KEYS = [
  17. 'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color', 'primary_color', 'disable-user-theming'
  18. ];
  19. private $themingDefaults;
  20. private $imageManager;
  21. private $config;
  22. public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) {
  23. parent::__construct();
  24. $this->themingDefaults = $themingDefaults;
  25. $this->imageManager = $imageManager;
  26. $this->config = $config;
  27. }
  28. protected function configure() {
  29. $this
  30. ->setName('theming:config')
  31. ->setDescription('Set theming app config values')
  32. ->addArgument(
  33. 'key',
  34. InputArgument::OPTIONAL,
  35. 'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
  36. 'One of: ' . implode(', ', self::SUPPORTED_KEYS)
  37. )
  38. ->addArgument(
  39. 'value',
  40. InputArgument::OPTIONAL,
  41. 'Value to set (leave empty to obtain the current value)'
  42. )
  43. ->addOption(
  44. 'reset',
  45. 'r',
  46. InputOption::VALUE_NONE,
  47. 'Reset the given config key to default'
  48. );
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $key = $input->getArgument('key');
  52. $value = $input->getArgument('value');
  53. assert(is_string($value) || $value === null, 'At most one value should be provided.');
  54. if ($key === null) {
  55. $output->writeln('Current theming config:');
  56. foreach (self::SUPPORTED_KEYS as $key) {
  57. $value = $this->config->getAppValue('theming', $key, '');
  58. $output->writeln('- ' . $key . ': ' . $value . '');
  59. }
  60. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $key) {
  61. $value = $this->config->getAppValue('theming', $key . 'Mime', '');
  62. $output->writeln('- ' . $key . ': ' . $value . '');
  63. }
  64. return 0;
  65. }
  66. if (!in_array($key, self::SUPPORTED_KEYS, true) && !in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  67. $output->writeln('<error>Invalid config key provided</error>');
  68. return 1;
  69. }
  70. if ($input->getOption('reset')) {
  71. $defaultValue = $this->themingDefaults->undo($key);
  72. $output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
  73. return 0;
  74. }
  75. if ($value === null) {
  76. $value = $this->config->getAppValue('theming', $key, '');
  77. if ($value !== '') {
  78. $output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
  79. } else {
  80. $output->writeln('<info>' . $key . ' is currently not set</info>');
  81. }
  82. return 0;
  83. }
  84. if ($key === 'background' && $value === 'backgroundColor') {
  85. $this->themingDefaults->undo($key);
  86. $key = $key . 'Mime';
  87. }
  88. if (in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  89. if (!str_starts_with($value, '/')) {
  90. $output->writeln('<error>The image file needs to be provided as an absolute path: ' . $value . '.</error>');
  91. return 1;
  92. }
  93. if (!file_exists($value)) {
  94. $output->writeln('<error>File could not be found: ' . $value . '.</error>');
  95. return 1;
  96. }
  97. $value = $this->imageManager->updateImage($key, $value);
  98. $key = $key . 'Mime';
  99. }
  100. if ($key === 'color') {
  101. $output->writeln('<comment>Using "color" is deprecated, use "primary_color" instead</comment>');
  102. $key = 'primary_color';
  103. }
  104. if ($key === 'primary_color' && !preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  105. $output->writeln('<error>The given color is invalid: ' . $value . '</error>');
  106. return 1;
  107. }
  108. $this->themingDefaults->set($key, $value);
  109. $output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
  110. return 0;
  111. }
  112. }