UpdateConfig.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming\Command;
  24. use OCA\Theming\ImageManager;
  25. use OCA\Theming\ThemingDefaults;
  26. use OCP\IConfig;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class UpdateConfig extends Command {
  33. public const SUPPORTED_KEYS = [
  34. 'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color', 'disable-user-theming'
  35. ];
  36. private $themingDefaults;
  37. private $imageManager;
  38. private $config;
  39. public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) {
  40. parent::__construct();
  41. $this->themingDefaults = $themingDefaults;
  42. $this->imageManager = $imageManager;
  43. $this->config = $config;
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('theming:config')
  48. ->setDescription('Set theming app config values')
  49. ->addArgument(
  50. 'key',
  51. InputArgument::OPTIONAL,
  52. 'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
  53. 'One of: ' . implode(', ', self::SUPPORTED_KEYS)
  54. )
  55. ->addArgument(
  56. 'value',
  57. InputArgument::OPTIONAL,
  58. 'Value to set (leave empty to obtain the current value)'
  59. )
  60. ->addOption(
  61. 'reset',
  62. 'r',
  63. InputOption::VALUE_NONE,
  64. 'Reset the given config key to default'
  65. );
  66. }
  67. protected function execute(InputInterface $input, OutputInterface $output): int {
  68. $key = $input->getArgument('key');
  69. $value = $input->getArgument('value');
  70. assert(is_string($value) || $value === null, 'At most one value should be provided.');
  71. if ($key === null) {
  72. $output->writeln('Current theming config:');
  73. foreach (self::SUPPORTED_KEYS as $key) {
  74. $value = $this->config->getAppValue('theming', $key, '');
  75. $output->writeln('- ' . $key . ': ' . $value . '');
  76. }
  77. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $key) {
  78. $value = $this->config->getAppValue('theming', $key . 'Mime', '');
  79. $output->writeln('- ' . $key . ': ' . $value . '');
  80. }
  81. return 0;
  82. }
  83. if (!in_array($key, self::SUPPORTED_KEYS, true) && !in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  84. $output->writeln('<error>Invalid config key provided</error>');
  85. return 1;
  86. }
  87. if ($input->getOption('reset')) {
  88. $defaultValue = $this->themingDefaults->undo($key);
  89. $output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
  90. return 0;
  91. }
  92. if ($value === null) {
  93. $value = $this->config->getAppValue('theming', $key, '');
  94. if ($value !== '') {
  95. $output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
  96. } else {
  97. $output->writeln('<info>' . $key . ' is currently not set</info>');
  98. }
  99. return 0;
  100. }
  101. if ($key === 'background' && $value === 'backgroundColor') {
  102. $this->themingDefaults->undo($key);
  103. $key = $key . 'Mime';
  104. }
  105. if (in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) {
  106. if (!str_starts_with($value, '/')) {
  107. $output->writeln('<error>The image file needs to be provided as an absolute path: ' . $value . '.</error>');
  108. return 1;
  109. }
  110. if (!file_exists($value)) {
  111. $output->writeln('<error>File could not be found: ' . $value . '.</error>');
  112. return 1;
  113. }
  114. $value = $this->imageManager->updateImage($key, $value);
  115. $key = $key . 'Mime';
  116. }
  117. if ($key === 'color' && !preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  118. $output->writeln('<error>The given color is invalid: ' . $value . '</error>');
  119. return 1;
  120. }
  121. $this->themingDefaults->set($key, $value);
  122. $output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
  123. return 0;
  124. }
  125. }