encryptall.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Encryption;
  23. use OCP\App\IAppManager;
  24. use OCP\Encryption\IManager;
  25. use OCP\IConfig;
  26. use Symfony\Component\Console\Command\Command;
  27. use Symfony\Component\Console\Helper\QuestionHelper;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Symfony\Component\Console\Question\ConfirmationQuestion;
  31. class EncryptAll extends Command {
  32. /** @var IManager */
  33. protected $encryptionManager;
  34. /** @var IAppManager */
  35. protected $appManager;
  36. /** @var IConfig */
  37. protected $config;
  38. /** @var QuestionHelper */
  39. protected $questionHelper;
  40. /** @var bool */
  41. protected $wasTrashbinEnabled;
  42. /** @var bool */
  43. protected $wasSingleUserModeEnabled;
  44. /**
  45. * @param IManager $encryptionManager
  46. * @param IAppManager $appManager
  47. * @param IConfig $config
  48. * @param QuestionHelper $questionHelper
  49. */
  50. public function __construct(
  51. IManager $encryptionManager,
  52. IAppManager $appManager,
  53. IConfig $config,
  54. QuestionHelper $questionHelper
  55. ) {
  56. parent::__construct();
  57. $this->appManager = $appManager;
  58. $this->encryptionManager = $encryptionManager;
  59. $this->config = $config;
  60. $this->questionHelper = $questionHelper;
  61. }
  62. /**
  63. * Set single user mode and disable the trashbin app
  64. */
  65. protected function forceSingleUserAndTrashbin() {
  66. $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
  67. $this->wasSingleUserModeEnabled = $this->config->getSystemValue('singleuser', false);
  68. $this->config->setSystemValue('singleuser', true);
  69. $this->appManager->disableApp('files_trashbin');
  70. }
  71. /**
  72. * Reset the single user mode and re-enable the trashbin app
  73. */
  74. protected function resetSingleUserAndTrashbin() {
  75. $this->config->setSystemValue('singleuser', $this->wasSingleUserModeEnabled);
  76. if ($this->wasTrashbinEnabled) {
  77. $this->appManager->enableApp('files_trashbin');
  78. }
  79. }
  80. protected function configure() {
  81. parent::configure();
  82. $this->setName('encryption:encrypt-all');
  83. $this->setDescription('Encrypt all files for all users');
  84. $this->setHelp(
  85. 'This will encrypt all files for all users. '
  86. . 'Please make sure that no user access his files during this process!'
  87. );
  88. }
  89. protected function execute(InputInterface $input, OutputInterface $output) {
  90. if ($this->encryptionManager->isEnabled() === false) {
  91. throw new \Exception('Server side encryption is not enabled');
  92. }
  93. $output->writeln("\n");
  94. $output->writeln('You are about to start to encrypt all files stored in your ownCloud.');
  95. $output->writeln('It will depend on the encryption module you use which files get encrypted.');
  96. $output->writeln('Depending on the number and size of your files this can take some time');
  97. $output->writeln('Please make sure that no user access his files during this process!');
  98. $output->writeln('');
  99. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  100. if ($this->questionHelper->ask($input, $output, $question)) {
  101. $this->forceSingleUserAndTrashbin();
  102. try {
  103. $defaultModule = $this->encryptionManager->getEncryptionModule();
  104. $defaultModule->encryptAll($input, $output);
  105. } catch (\Exception $ex) {
  106. $this->resetSingleUserAndTrashbin();
  107. throw $ex;
  108. }
  109. $this->resetSingleUserAndTrashbin();
  110. } else {
  111. $output->writeln('aborted');
  112. }
  113. }
  114. }