console.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. require_once __DIR__ . '/lib/versioncheck.php';
  9. use OC\Console\Application;
  10. use Symfony\Component\Console\Input\ArgvInput;
  11. use Symfony\Component\Console\Output\ConsoleOutput;
  12. define('OC_CONSOLE', 1);
  13. function exceptionHandler($exception) {
  14. echo "An unhandled exception has been thrown:" . PHP_EOL;
  15. echo $exception;
  16. exit(1);
  17. }
  18. try {
  19. require_once __DIR__ . '/lib/base.php';
  20. // set to run indefinitely if needed
  21. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  22. @set_time_limit(0);
  23. }
  24. if (!OC::$CLI) {
  25. echo "This script can be run from the command line only" . PHP_EOL;
  26. exit(1);
  27. }
  28. $config = \OCP\Server::get(\OCP\IConfig::class);
  29. set_exception_handler('exceptionHandler');
  30. if (!function_exists('posix_getuid')) {
  31. echo "The posix extensions are required - see https://www.php.net/manual/en/book.posix.php" . PHP_EOL;
  32. exit(1);
  33. }
  34. $user = posix_getuid();
  35. $configUser = fileowner(OC::$configDir . 'config.php');
  36. if ($user !== $configUser) {
  37. echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
  38. echo "Current user id: " . $user . PHP_EOL;
  39. echo "Owner id of config.php: " . $configUser . PHP_EOL;
  40. echo "Try adding 'sudo -u #" . $configUser . "' to the beginning of the command (without the single quotes)" . PHP_EOL;
  41. echo "If running with 'docker exec' try adding the option '-u " . $configUser . "' to the docker command (without the single quotes)" . PHP_EOL;
  42. exit(1);
  43. }
  44. $oldWorkingDir = getcwd();
  45. if ($oldWorkingDir === false) {
  46. echo "This script can be run from the Nextcloud root directory only." . PHP_EOL;
  47. echo "Can't determine current working dir - the script will continue to work but be aware of the above fact." . PHP_EOL;
  48. } elseif ($oldWorkingDir !== __DIR__ && !chdir(__DIR__)) {
  49. echo "This script can be run from the Nextcloud root directory only." . PHP_EOL;
  50. echo "Can't change to Nextcloud root directory." . PHP_EOL;
  51. exit(1);
  52. }
  53. if (!(function_exists('pcntl_signal') && function_exists('pcntl_signal_dispatch')) && !in_array('--no-warnings', $argv)) {
  54. echo "The process control (PCNTL) extensions are required in case you want to interrupt long running commands - see https://www.php.net/manual/en/book.pcntl.php" . PHP_EOL;
  55. echo "Additionally the function 'pcntl_signal' and 'pcntl_signal_dispatch' need to be enabled in your php.ini." . PHP_EOL;
  56. }
  57. $application = \OCP\Server::get(Application::class);
  58. $application->loadCommands(new ArgvInput(), new ConsoleOutput());
  59. $application->run();
  60. } catch (Exception $ex) {
  61. exceptionHandler($ex);
  62. } catch (Error $ex) {
  63. exceptionHandler($ex);
  64. }