1
0

SettingsService.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Service;
  8. use OC\Files\FilenameValidator;
  9. use OCP\IConfig;
  10. use Psr\Log\LoggerInterface;
  11. class SettingsService {
  12. protected const WINDOWS_EXTENSION = [
  13. ' ',
  14. '.',
  15. ];
  16. protected const WINDOWS_BASENAMES = [
  17. 'con', 'prn', 'aux', 'nul', 'com0', 'com1', 'com2', 'com3', 'com4', 'com5',
  18. 'com6', 'com7', 'com8', 'com9', 'com¹', 'com²', 'com³', 'lpt0', 'lpt1', 'lpt2',
  19. 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9', 'lpt¹', 'lpt²', 'lpt³',
  20. ];
  21. protected const WINDOWS_CHARACTERS = [
  22. '<', '>', ':',
  23. '"', '|', '?',
  24. '*',
  25. ];
  26. public function __construct(
  27. private IConfig $config,
  28. private FilenameValidator $filenameValidator,
  29. private LoggerInterface $logger,
  30. ) {
  31. }
  32. public function hasFilesWindowsSupport(): bool {
  33. return empty(array_diff(self::WINDOWS_BASENAMES, $this->filenameValidator->getForbiddenBasenames()))
  34. && empty(array_diff(self::WINDOWS_CHARACTERS, $this->filenameValidator->getForbiddenCharacters()))
  35. && empty(array_diff(self::WINDOWS_EXTENSION, $this->filenameValidator->getForbiddenExtensions()));
  36. }
  37. public function setFilesWindowsSupport(bool $enabled = true): void {
  38. if ($enabled) {
  39. $basenames = array_unique(array_merge(self::WINDOWS_BASENAMES, $this->filenameValidator->getForbiddenBasenames()));
  40. $characters = array_unique(array_merge(self::WINDOWS_CHARACTERS, $this->filenameValidator->getForbiddenCharacters()));
  41. $extensions = array_unique(array_merge(self::WINDOWS_EXTENSION, $this->filenameValidator->getForbiddenExtensions()));
  42. } else {
  43. $basenames = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenBasenames(), self::WINDOWS_BASENAMES)));
  44. $characters = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenCharacters(), self::WINDOWS_CHARACTERS)));
  45. $extensions = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenExtensions(), self::WINDOWS_EXTENSION)));
  46. }
  47. $values = [
  48. 'forbidden_filename_basenames' => empty($basenames) ? null : $basenames,
  49. 'forbidden_filename_characters' => empty($characters) ? null : $characters,
  50. 'forbidden_filename_extensions' => empty($extensions) ? null : $extensions,
  51. ];
  52. $this->config->setSystemValues($values);
  53. }
  54. }