diff options
Diffstat (limited to 'core/Command/Maintenance')
-rw-r--r-- | core/Command/Maintenance/DataFingerprint.php | 41 | ||||
-rw-r--r-- | core/Command/Maintenance/Install.php | 115 | ||||
-rw-r--r-- | core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php | 57 | ||||
-rw-r--r-- | core/Command/Maintenance/Mimetype/UpdateDB.php | 49 | ||||
-rw-r--r-- | core/Command/Maintenance/Mimetype/UpdateJS.php | 35 | ||||
-rw-r--r-- | core/Command/Maintenance/Mode.php | 38 | ||||
-rw-r--r-- | core/Command/Maintenance/Repair.php | 142 | ||||
-rw-r--r-- | core/Command/Maintenance/RepairShareOwnership.php | 176 | ||||
-rw-r--r-- | core/Command/Maintenance/UpdateHtaccess.php | 29 | ||||
-rw-r--r-- | core/Command/Maintenance/UpdateTheme.php | 35 |
10 files changed, 353 insertions, 364 deletions
diff --git a/core/Command/Maintenance/DataFingerprint.php b/core/Command/Maintenance/DataFingerprint.php index 2bd1b9270bb..014d6c411a4 100644 --- a/core/Command/Maintenance/DataFingerprint.php +++ b/core/Command/Maintenance/DataFingerprint.php @@ -1,24 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance; @@ -29,16 +14,10 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class DataFingerprint extends Command { - - /** @var IConfig */ - protected $config; - /** @var ITimeFactory */ - protected $timeFactory; - - public function __construct(IConfig $config, - ITimeFactory $timeFactory) { - $this->config = $config; - $this->timeFactory = $timeFactory; + public function __construct( + protected IConfig $config, + protected ITimeFactory $timeFactory, + ) { parent::__construct(); } @@ -49,7 +28,9 @@ class DataFingerprint extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - $this->config->setSystemValue('data-fingerprint', md5($this->timeFactory->getTime())); + $fingerPrint = md5($this->timeFactory->getTime()); + $this->config->setSystemValue('data-fingerprint', $fingerPrint); + $output->writeln('<info>Updated data-fingerprint to ' . $fingerPrint . '</info>'); return 0; } } diff --git a/core/Command/Maintenance/Install.php b/core/Command/Maintenance/Install.php index d98ec870e37..6170c5a2638 100644 --- a/core/Command/Maintenance/Install.php +++ b/core/Command/Maintenance/Install.php @@ -1,42 +1,21 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bernhard Posselt <dev@bernhard-posselt.com> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Hansson <daniel@techandme.se> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Thomas Pulzer <t.pulzer@kniel.de> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance; use bantu\IniGetWrapper\IniGetWrapper; use InvalidArgumentException; -use OC\Installer; +use OC\Console\TimestampFormatter; +use OC\Migration\ConsoleOutput; use OC\Setup; use OC\SystemConfig; -use OCP\Defaults; -use Psr\Log\LoggerInterface; +use OCP\Server; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; @@ -47,19 +26,14 @@ use Throwable; use function get_class; class Install extends Command { - - /** @var SystemConfig */ - private $config; - /** @var IniGetWrapper */ - private $iniGetWrapper; - - public function __construct(SystemConfig $config, IniGetWrapper $iniGetWrapper) { + public function __construct( + private SystemConfig $config, + private IniGetWrapper $iniGetWrapper, + ) { parent::__construct(); - $this->config = $config; - $this->iniGetWrapper = $iniGetWrapper; } - protected function configure() { + protected function configure(): void { $this ->setName('maintenance:install') ->setDescription('install Nextcloud') @@ -67,36 +41,27 @@ class Install extends Command { ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database') ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost') ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on') - ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database') + ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'Login to connect to the database') ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null) ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null) - ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin') + ->addOption('disable-admin-user', null, InputOption::VALUE_NONE, 'Disable the creation of an admin user') + ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'Login of the admin account', 'admin') ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account') ->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account') - ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data"); + ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT . '/data'); } protected function execute(InputInterface $input, OutputInterface $output): int { - // validate the environment - $server = \OC::$server; - $setupHelper = new Setup( - $this->config, - $this->iniGetWrapper, - $server->getL10N('lib'), - $server->query(Defaults::class), - $server->get(LoggerInterface::class), - $server->getSecureRandom(), - \OC::$server->query(Installer::class) - ); + $setupHelper = Server::get(Setup::class); $sysInfo = $setupHelper->getSystemInfo(true); $errors = $sysInfo['errors']; if (count($errors) > 0) { $this->printErrors($output, $errors); // ignore the OS X setup warning - if (count($errors) !== 1 || - (string)$errors[0]['error'] !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') { + if (count($errors) !== 1 + || (string)$errors[0]['error'] !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk!') { return 1; } } @@ -104,13 +69,25 @@ class Install extends Command { // validate user input $options = $this->validateInput($input, $output, array_keys($sysInfo['databases'])); + if ($output->isVerbose()) { + // Prepend each line with a little timestamp + $timestampFormatter = new TimestampFormatter(null, $output->getFormatter()); + $output->setFormatter($timestampFormatter); + $migrationOutput = new ConsoleOutput($output); + } else { + $migrationOutput = null; + } + // perform installation - $errors = $setupHelper->install($options); + $errors = $setupHelper->install($options, $migrationOutput); if (count($errors) > 0) { $this->printErrors($output, $errors); return 1; } - $output->writeln("Nextcloud was successfully installed"); + if ($setupHelper->shouldRemoveCanInstallFile()) { + $output->writeln('<warn>Could not remove CAN_INSTALL from the config folder. Please remove this file manually.</warn>'); + } + $output->writeln('Nextcloud was successfully installed'); return 0; } @@ -124,7 +101,7 @@ class Install extends Command { $db = strtolower($input->getOption('database')); if (!in_array($db, $supportedDatabases)) { - throw new InvalidArgumentException("Database <$db> is not supported."); + throw new InvalidArgumentException("Database <$db> is not supported. " . implode(', ', $supportedDatabases) . ' are supported.'); } $dbUser = $input->getOption('database-user'); @@ -142,8 +119,9 @@ class Install extends Command { $dbHost .= ':' . $dbPort; } if ($input->hasParameterOption('--database-pass')) { - $dbPass = (string) $input->getOption('database-pass'); + $dbPass = (string)$input->getOption('database-pass'); } + $disableAdminUser = (bool)$input->getOption('disable-admin-user'); $adminLogin = $input->getOption('admin-user'); $adminPassword = $input->getOption('admin-pass'); $adminEmail = $input->getOption('admin-email'); @@ -151,31 +129,31 @@ class Install extends Command { if ($db !== 'sqlite') { if (is_null($dbUser)) { - throw new InvalidArgumentException("Database user not provided."); + throw new InvalidArgumentException('Database account not provided.'); } if (is_null($dbName)) { - throw new InvalidArgumentException("Database name not provided."); + throw new InvalidArgumentException('Database name not provided.'); } if (is_null($dbPass)) { /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); - $question = new Question('What is the password to access the database with user <'.$dbUser.'>?'); + $question = new Question('What is the password to access the database with user <' . $dbUser . '>?'); $question->setHidden(true); $question->setHiddenFallback(false); $dbPass = $helper->ask($input, $output, $question); } } - if (is_null($adminPassword)) { + if (!$disableAdminUser && $adminPassword === null) { /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); - $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?'); + $question = new Question('What is the password you like to use for the admin account <' . $adminLogin . '>?'); $question->setHidden(true); $question->setHiddenFallback(false); $adminPassword = $helper->ask($input, $output, $question); } - if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) { + if (!$disableAdminUser && $adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) { throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.'); } @@ -185,6 +163,7 @@ class Install extends Command { 'dbpass' => $dbPass, 'dbname' => $dbName, 'dbhost' => $dbHost, + 'admindisable' => $disableAdminUser, 'adminlogin' => $adminLogin, 'adminpass' => $adminPassword, 'adminemail' => $adminEmail, @@ -198,9 +177,9 @@ class Install extends Command { /** * @param OutputInterface $output - * @param $errors + * @param array<string|array> $errors */ - protected function printErrors(OutputInterface $output, $errors) { + protected function printErrors(OutputInterface $output, array $errors): void { foreach ($errors as $error) { if (is_array($error)) { $output->writeln('<error>' . $error['error'] . '</error>'); diff --git a/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php b/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php index 97432473722..f8f19a61993 100644 --- a/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php +++ b/core/Command/Maintenance/Mimetype/GenerateMimetypeFileBuilder.php @@ -3,47 +3,30 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019 Xheni Myrtaj <xheni@protonmail.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Xheni Myrtaj <myrtajxheni@gmail.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Command\Maintenance\Mimetype; class GenerateMimetypeFileBuilder { /** * Generate mime type list file - * @param $aliases + * + * @param array<string,string> $aliases * @return string */ - public function generateFile(array $aliases): string { + public function generateFile(array $aliases, array $names): string { // Remove comments - $keys = array_filter(array_keys($aliases), function ($k) { - return $k[0] === '_'; - }); - foreach ($keys as $key) { - unset($aliases[$key]); - } + $aliases = array_filter($aliases, static function ($key) { + // Single digit extensions will be treated as integers + // Let's make sure they are strings + // https://github.com/nextcloud/server/issues/42902 + $key = (string)$key; + return !($key === '' || $key[0] === '_'); + }, ARRAY_FILTER_USE_KEY); // Fetch all files - $dir = new \DirectoryIterator(\OC::$SERVERROOT.'/core/img/filetypes'); + $dir = new \DirectoryIterator(\OC::$SERVERROOT . '/core/img/filetypes'); $files = []; foreach ($dir as $fileInfo) { @@ -59,7 +42,7 @@ class GenerateMimetypeFileBuilder { // Fetch all themes! $themes = []; - $dirs = new \DirectoryIterator(\OC::$SERVERROOT.'/themes/'); + $dirs = new \DirectoryIterator(\OC::$SERVERROOT . '/themes/'); foreach ($dirs as $dir) { //Valid theme dir if ($dir->isFile() || $dir->isDot()) { @@ -88,6 +71,15 @@ class GenerateMimetypeFileBuilder { sort($themes[$theme]); } + $namesOutput = ''; + foreach ($names as $key => $name) { + if (str_starts_with($key, '_') || trim($name) === '') { + // Skip internal names + continue; + } + $namesOutput .= "'$key': t('core', " . json_encode($name) . "),\n"; + } + //Generate the JS return '/** * This file is automatically generated @@ -100,7 +92,8 @@ class GenerateMimetypeFileBuilder { OC.MimeTypeList={ aliases: ' . json_encode($aliases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ', files: ' . json_encode($files, JSON_PRETTY_PRINT) . ', - themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . ' + themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . ', + names: {' . $namesOutput . '}, }; '; } diff --git a/core/Command/Maintenance/Mimetype/UpdateDB.php b/core/Command/Maintenance/Mimetype/UpdateDB.php index e2e3137927f..4467e89eb32 100644 --- a/core/Command/Maintenance/Mimetype/UpdateDB.php +++ b/core/Command/Maintenance/Mimetype/UpdateDB.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance\Mimetype; @@ -35,19 +18,11 @@ use Symfony\Component\Console\Output\OutputInterface; class UpdateDB extends Command { public const DEFAULT_MIMETYPE = 'application/octet-stream'; - /** @var IMimeTypeDetector */ - protected $mimetypeDetector; - - /** @var IMimeTypeLoader */ - protected $mimetypeLoader; - public function __construct( - IMimeTypeDetector $mimetypeDetector, - IMimeTypeLoader $mimetypeLoader + protected IMimeTypeDetector $mimetypeDetector, + protected IMimeTypeLoader $mimetypeLoader, ) { parent::__construct(); - $this->mimetypeDetector = $mimetypeDetector; - $this->mimetypeLoader = $mimetypeLoader; } protected function configure() { @@ -70,6 +45,10 @@ class UpdateDB extends Command { $totalNewMimetypes = 0; foreach ($mappings as $ext => $mimetypes) { + // Single digit extensions will be treated as integers + // Let's make sure they are strings + // https://github.com/nextcloud/server/issues/42902 + $ext = (string)$ext; if ($ext[0] === '_') { // comment continue; @@ -80,21 +59,21 @@ class UpdateDB extends Command { $mimetypeId = $this->mimetypeLoader->getId($mimetype); if (!$existing) { - $output->writeln('Added mimetype "'.$mimetype.'" to database'); + $output->writeln('Added mimetype "' . $mimetype . '" to database'); $totalNewMimetypes++; } if (!$existing || $input->getOption('repair-filecache')) { $touchedFilecacheRows = $this->mimetypeLoader->updateFilecache($ext, $mimetypeId); if ($touchedFilecacheRows > 0) { - $output->writeln('Updated '.$touchedFilecacheRows.' filecache rows for mimetype "'.$mimetype.'"'); + $output->writeln('Updated ' . $touchedFilecacheRows . ' filecache rows for mimetype "' . $mimetype . '"'); } $totalFilecacheUpdates += $touchedFilecacheRows; } } - $output->writeln('Added '.$totalNewMimetypes.' new mimetypes'); - $output->writeln('Updated '.$totalFilecacheUpdates.' filecache rows'); + $output->writeln('Added ' . $totalNewMimetypes . ' new mimetypes'); + $output->writeln('Updated ' . $totalFilecacheUpdates . ' filecache rows'); return 0; } } diff --git a/core/Command/Maintenance/Mimetype/UpdateJS.php b/core/Command/Maintenance/Mimetype/UpdateJS.php index 042b4d5d62f..2132ff54c6d 100644 --- a/core/Command/Maintenance/Mimetype/UpdateJS.php +++ b/core/Command/Maintenance/Mimetype/UpdateJS.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Xheni Myrtaj <myrtajxheni@gmail.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance\Mimetype; @@ -31,15 +14,10 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class UpdateJS extends Command { - - /** @var IMimeTypeDetector */ - protected $mimetypeDetector; - public function __construct( - IMimeTypeDetector $mimetypeDetector + protected IMimeTypeDetector $mimetypeDetector, ) { parent::__construct(); - $this->mimetypeDetector = $mimetypeDetector; } protected function configure() { @@ -54,7 +32,8 @@ class UpdateJS extends Command { // Output the JS $generatedMimetypeFile = new GenerateMimetypeFileBuilder(); - file_put_contents(\OC::$SERVERROOT.'/core/js/mimetypelist.js', $generatedMimetypeFile->generateFile($aliases)); + $namings = $this->mimetypeDetector->getAllNamings(); + file_put_contents(\OC::$SERVERROOT . '/core/js/mimetypelist.js', $generatedMimetypeFile->generateFile($aliases, $namings)); $output->writeln('<info>mimetypelist.js is updated'); return 0; diff --git a/core/Command/Maintenance/Mode.php b/core/Command/Maintenance/Mode.php index 17580b8b331..853e843f57b 100644 --- a/core/Command/Maintenance/Mode.php +++ b/core/Command/Maintenance/Mode.php @@ -1,27 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Michael Weimann <mail@michael-weimann.eu> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author scolebrook <scolebrook@mac.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance; @@ -33,19 +15,17 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Mode extends Command { - - /** @var IConfig */ - protected $config; - - public function __construct(IConfig $config) { - $this->config = $config; + public function __construct( + protected IConfig $config, + ) { parent::__construct(); } protected function configure() { $this ->setName('maintenance:mode') - ->setDescription('set maintenance mode') + ->setDescription('Show or toggle maintenance mode status') + ->setHelp('Maintenance mode prevents new logins, locks existing sessions, and disables background jobs.') ->addOption( 'on', null, diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 2a3d7a908e2..f0c88f6811b 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -1,69 +1,41 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Temtaime <temtaime@gmail.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance; use Exception; +use OC\Repair\Events\RepairAdvanceEvent; +use OC\Repair\Events\RepairErrorEvent; +use OC\Repair\Events\RepairFinishEvent; +use OC\Repair\Events\RepairInfoEvent; +use OC\Repair\Events\RepairStartEvent; +use OC\Repair\Events\RepairStepEvent; +use OC\Repair\Events\RepairWarningEvent; use OCP\App\IAppManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; class Repair extends Command { - /** @var \OC\Repair $repair */ - protected $repair; - /** @var IConfig */ - protected $config; - /** @var EventDispatcherInterface */ - private $dispatcher; - /** @var ProgressBar */ - private $progress; - /** @var OutputInterface */ - private $output; - /** @var IAppManager */ - private $appManager; - - /** - * @param \OC\Repair $repair - * @param IConfig $config - * @param EventDispatcherInterface $dispatcher - * @param IAppManager $appManager - */ - public function __construct(\OC\Repair $repair, IConfig $config, EventDispatcherInterface $dispatcher, IAppManager $appManager) { - $this->repair = $repair; - $this->config = $config; - $this->dispatcher = $dispatcher; - $this->appManager = $appManager; + private ProgressBar $progress; + private OutputInterface $output; + protected bool $errored = false; + + public function __construct( + protected \OC\Repair $repair, + protected IConfig $config, + private IEventDispatcher $dispatcher, + private IAppManager $appManager, + ) { parent::__construct(); } @@ -89,16 +61,16 @@ class Repair extends Command { $this->repair->addStep($step); } - $apps = $this->appManager->getInstalledApps(); + $apps = $this->appManager->getEnabledApps(); foreach ($apps as $app) { if (!$this->appManager->isEnabledForUser($app)) { continue; } - $info = \OC_App::getAppInfo($app); + $info = $this->appManager->getAppInfo($app); if (!is_array($info)) { continue; } - \OC_App::loadApp($app); + $this->appManager->loadApp($app); $steps = $info['repair-steps']['post-migration']; foreach ($steps as $step) { try { @@ -109,52 +81,44 @@ class Repair extends Command { } } + + $maintenanceMode = $this->config->getSystemValueBool('maintenance'); $this->config->setSystemValue('maintenance', true); $this->progress = new ProgressBar($output); $this->output = $output; - $this->dispatcher->addListener('\OC\Repair::startProgress', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::advance', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::finishProgress', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::step', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::info', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::warning', [$this, 'handleRepairFeedBack']); - $this->dispatcher->addListener('\OC\Repair::error', [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairStartEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairAdvanceEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairFinishEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairStepEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairInfoEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairWarningEvent::class, [$this, 'handleRepairFeedBack']); + $this->dispatcher->addListener(RepairErrorEvent::class, [$this, 'handleRepairFeedBack']); $this->repair->run(); $this->config->setSystemValue('maintenance', $maintenanceMode); - return 0; + return $this->errored ? 1 : 0; } - public function handleRepairFeedBack($event) { - if (!$event instanceof GenericEvent) { - return; - } - switch ($event->getSubject()) { - case '\OC\Repair::startProgress': - $this->progress->start($event->getArgument(0)); - break; - case '\OC\Repair::advance': - $this->progress->advance($event->getArgument(0)); - break; - case '\OC\Repair::finishProgress': - $this->progress->finish(); - $this->output->writeln(''); - break; - case '\OC\Repair::step': - $this->output->writeln(' - ' . $event->getArgument(0)); - break; - case '\OC\Repair::info': - $this->output->writeln(' - ' . $event->getArgument(0)); - break; - case '\OC\Repair::warning': - $this->output->writeln(' - WARNING: ' . $event->getArgument(0)); - break; - case '\OC\Repair::error': - $this->output->writeln('<error> - ERROR: ' . $event->getArgument(0) . '</error>'); - break; + public function handleRepairFeedBack(Event $event): void { + if ($event instanceof RepairStartEvent) { + $this->progress->start($event->getMaxStep()); + } elseif ($event instanceof RepairAdvanceEvent) { + $this->progress->advance($event->getIncrement()); + } elseif ($event instanceof RepairFinishEvent) { + $this->progress->finish(); + $this->output->writeln(''); + } elseif ($event instanceof RepairStepEvent) { + $this->output->writeln('<info> - ' . $event->getStepName() . '</info>'); + } elseif ($event instanceof RepairInfoEvent) { + $this->output->writeln('<info> - ' . $event->getMessage() . '</info>'); + } elseif ($event instanceof RepairWarningEvent) { + $this->output->writeln('<comment> - WARNING: ' . $event->getMessage() . '</comment>'); + } elseif ($event instanceof RepairErrorEvent) { + $this->output->writeln('<error> - ERROR: ' . $event->getMessage() . '</error>'); + $this->errored = true; } } } diff --git a/core/Command/Maintenance/RepairShareOwnership.php b/core/Command/Maintenance/RepairShareOwnership.php new file mode 100644 index 00000000000..16675545afe --- /dev/null +++ b/core/Command/Maintenance/RepairShareOwnership.php @@ -0,0 +1,176 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Command\Maintenance; + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; + +class RepairShareOwnership extends Command { + public function __construct( + private IDBConnection $dbConnection, + private IUserManager $userManager, + ) { + parent::__construct(); + } + + protected function configure() { + $this + ->setName('maintenance:repair-share-owner') + ->setDescription('repair invalid share-owner entries in the database') + ->addOption('no-confirm', 'y', InputOption::VALUE_NONE, "Don't ask for confirmation before repairing the shares") + ->addArgument('user', InputArgument::OPTIONAL, 'User to fix incoming shares for, if omitted all users will be fixed'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $noConfirm = $input->getOption('no-confirm'); + $userId = $input->getArgument('user'); + if ($userId) { + $user = $this->userManager->get($userId); + if (!$user) { + $output->writeln("<error>user $userId not found</error>"); + return 1; + } + $shares = $this->getWrongShareOwnershipForUser($user); + } else { + $shares = $this->getWrongShareOwnership(); + } + + if ($shares) { + $output->writeln(''); + $output->writeln('Found ' . count($shares) . ' shares with invalid share owner'); + foreach ($shares as $share) { + /** @var array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string} $share */ + $output->writeln(" - share {$share['shareId']} from \"{$share['initiator']}\" to \"{$share['receiver']}\" at \"{$share['fileTarget']}\", owned by \"{$share['owner']}\", that should be owned by \"{$share['mountOwner']}\""); + } + $output->writeln(''); + + if (!$noConfirm) { + /** @var QuestionHelper $helper */ + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Repair these shares? [y/N]', false); + + if (!$helper->ask($input, $output, $question)) { + return 0; + } + } + $output->writeln('Repairing ' . count($shares) . ' shares'); + $this->repairShares($shares); + } else { + $output->writeln('Found no shares with invalid share owner'); + } + + return 0; + } + + /** + * @return array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[] + * @throws \OCP\DB\Exception + */ + protected function getWrongShareOwnership(): array { + $qb = $this->dbConnection->getQueryBuilder(); + $brokenShares = $qb + ->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.file_target') + ->from('share', 's') + ->join('s', 'filecache', 'f', $qb->expr()->eq($qb->expr()->castColumn('s.item_source', IQueryBuilder::PARAM_INT), 'f.fileid')) + ->join('s', 'mounts', 'm', $qb->expr()->eq('f.storage', 'm.storage_id')) + ->where($qb->expr()->neq('m.user_id', 's.uid_owner')) + ->andWhere($qb->expr()->eq($qb->func()->concat($qb->expr()->literal('/'), 'm.user_id', $qb->expr()->literal('/')), 'm.mount_point')) + ->executeQuery() + ->fetchAll(); + + $found = []; + + foreach ($brokenShares as $share) { + $found[] = [ + 'shareId' => (int)$share['id'], + 'fileTarget' => $share['file_target'], + 'initiator' => $share['uid_initiator'], + 'receiver' => $share['share_with'], + 'owner' => $share['uid_owner'], + 'mountOwner' => $share['user_id'], + ]; + } + + return $found; + } + + /** + * @param IUser $user + * @return array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[] + * @throws \OCP\DB\Exception + */ + protected function getWrongShareOwnershipForUser(IUser $user): array { + $qb = $this->dbConnection->getQueryBuilder(); + $brokenShares = $qb + ->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.file_target') + ->from('share', 's') + ->join('s', 'filecache', 'f', $qb->expr()->eq('s.item_source', $qb->expr()->castColumn('f.fileid', IQueryBuilder::PARAM_STR))) + ->join('s', 'mounts', 'm', $qb->expr()->eq('f.storage', 'm.storage_id')) + ->where($qb->expr()->neq('m.user_id', 's.uid_owner')) + ->andWhere($qb->expr()->eq($qb->func()->concat($qb->expr()->literal('/'), 'm.user_id', $qb->expr()->literal('/')), 'm.mount_point')) + ->andWhere($qb->expr()->eq('s.share_with', $qb->createNamedParameter($user->getUID()))) + ->executeQuery() + ->fetchAll(); + + $found = []; + + foreach ($brokenShares as $share) { + $found[] = [ + 'shareId' => (int)$share['id'], + 'fileTarget' => $share['file_target'], + 'initiator' => $share['uid_initiator'], + 'receiver' => $share['share_with'], + 'owner' => $share['uid_owner'], + 'mountOwner' => $share['user_id'], + ]; + } + + return $found; + } + + /** + * @param array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[] $shares + * @return void + */ + protected function repairShares(array $shares) { + $this->dbConnection->beginTransaction(); + + $update = $this->dbConnection->getQueryBuilder(); + $update->update('share') + ->set('uid_owner', $update->createParameter('share_owner')) + ->set('uid_initiator', $update->createParameter('share_initiator')) + ->where($update->expr()->eq('id', $update->createParameter('share_id'))); + + foreach ($shares as $share) { + /** @var array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string} $share */ + $update->setParameter('share_id', $share['shareId'], IQueryBuilder::PARAM_INT); + $update->setParameter('share_owner', $share['mountOwner']); + + // if the broken owner is also the initiator it's safe to update them both, otherwise we don't touch the initiator + if ($share['initiator'] === $share['owner']) { + $update->setParameter('share_initiator', $share['mountOwner']); + } else { + $update->setParameter('share_initiator', $share['initiator']); + } + $update->executeStatement(); + } + + $this->dbConnection->commit(); + } +} diff --git a/core/Command/Maintenance/UpdateHtaccess.php b/core/Command/Maintenance/UpdateHtaccess.php index 67c6db22b21..eeff3bf8c62 100644 --- a/core/Command/Maintenance/UpdateHtaccess.php +++ b/core/Command/Maintenance/UpdateHtaccess.php @@ -1,28 +1,13 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Core\Command\Maintenance; +use OC\Setup; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -35,11 +20,11 @@ class UpdateHtaccess extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - if (\OC\Setup::updateHtaccess()) { + if (Setup::updateHtaccess()) { $output->writeln('.htaccess has been updated'); return 0; } else { - $output->writeln('<error>Error updating .htaccess file, not enough permissions or "overwrite.cli.url" set to an invalid URL?</error>'); + $output->writeln('<error>Error updating .htaccess file, not enough permissions, not enough free space or "overwrite.cli.url" set to an invalid URL?</error>'); return 1; } } diff --git a/core/Command/Maintenance/UpdateTheme.php b/core/Command/Maintenance/UpdateTheme.php index 10526fd54e6..3fbcb546cca 100644 --- a/core/Command/Maintenance/UpdateTheme.php +++ b/core/Command/Maintenance/UpdateTheme.php @@ -1,27 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net> - * - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Command\Maintenance; @@ -33,19 +14,11 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class UpdateTheme extends UpdateJS { - - /** @var IMimeTypeDetector */ - protected $mimetypeDetector; - - /** @var ICacheFactory */ - protected $cacheFactory; - public function __construct( IMimeTypeDetector $mimetypeDetector, - ICacheFactory $cacheFactory + protected ICacheFactory $cacheFactory, ) { parent::__construct($mimetypeDetector); - $this->cacheFactory = $cacheFactory; } protected function configure() { |