UpdateConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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'
  35. ];
  36. public const SUPPORTED_IMAGE_KEYS = [
  37. 'background', 'logo', 'favicon', 'logoheader'
  38. ];
  39. private $themingDefaults;
  40. private $imageManager;
  41. private $config;
  42. public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) {
  43. parent::__construct();
  44. $this->themingDefaults = $themingDefaults;
  45. $this->imageManager = $imageManager;
  46. $this->config = $config;
  47. }
  48. protected function configure() {
  49. $this
  50. ->setName('theming:config')
  51. ->setDescription('Set theming app config values')
  52. ->addArgument(
  53. 'key',
  54. InputArgument::OPTIONAL,
  55. 'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
  56. 'One of: ' . implode(', ', self::SUPPORTED_KEYS)
  57. )
  58. ->addArgument(
  59. 'value',
  60. InputArgument::OPTIONAL,
  61. 'Value to set (leave empty to obtain the current value)'
  62. )
  63. ->addOption(
  64. 'reset',
  65. 'r',
  66. InputOption::VALUE_NONE,
  67. 'Reset the given config key to default'
  68. );
  69. }
  70. protected function execute(InputInterface $input, OutputInterface $output): int {
  71. $key = $input->getArgument('key');
  72. $value = $input->getArgument('value');
  73. assert(is_string($value) || $value === null, 'At most one value should be provided.');
  74. if ($key === null) {
  75. $output->writeln('Current theming config:');
  76. foreach (self::SUPPORTED_KEYS as $key) {
  77. $value = $this->config->getAppValue('theming', $key, '');
  78. $output->writeln('- ' . $key . ': ' . $value . '');
  79. }
  80. foreach (self::SUPPORTED_IMAGE_KEYS as $key) {
  81. $value = $this->config->getAppValue('theming', $key . 'Mime', '');
  82. $output->writeln('- ' . $key . ': ' . $value . '');
  83. }
  84. return 0;
  85. }
  86. if (!in_array($key, self::SUPPORTED_KEYS, true) && !in_array($key, self::SUPPORTED_IMAGE_KEYS, true)) {
  87. $output->writeln('<error>Invalid config key provided</error>');
  88. return 1;
  89. }
  90. if ($input->getOption('reset')) {
  91. $defaultValue = $this->themingDefaults->undo($key);
  92. $output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
  93. return 0;
  94. }
  95. if ($value === null) {
  96. $value = $this->config->getAppValue('theming', $key, '');
  97. if ($value !== '') {
  98. $output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
  99. } else {
  100. $output->writeln('<info>' . $key . ' is currently not set</info>');
  101. }
  102. return 0;
  103. }
  104. if (in_array($key, self::SUPPORTED_IMAGE_KEYS, true)) {
  105. if (strpos($value, '/') !== 0) {
  106. $output->writeln('<error>The image file needs to be provided as an absolute path: ' . $value . '.</error>');
  107. return 1;
  108. }
  109. if (!file_exists($value)) {
  110. $output->writeln('<error>File could not be found: ' . $value . '.</error>');
  111. return 1;
  112. }
  113. $value = $this->imageManager->updateImage($key, $value);
  114. $key = $key . 'Mime';
  115. }
  116. if ($key === 'color' && !preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  117. $output->writeln('<error>The given color is invalid: ' . $value . '</error>');
  118. return 1;
  119. }
  120. $this->themingDefaults->set($key, $value);
  121. $output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
  122. return 0;
  123. }
  124. }