You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.0 KiB

  1. <?php
  2. declare(strict_types=1);
  3. use OCP\IConfig;
  4. use OCP\IURLGenerator;
  5. use OCP\Server;
  6. /**
  7. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  8. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  9. * SPDX-License-Identifier: AGPL-3.0-only
  10. */
  11. require_once __DIR__ . '/lib/versioncheck.php';
  12. use OC\Console\Application;
  13. use OCP\AppFramework\Http\Response;
  14. use OCP\Diagnostics\IEventLogger;
  15. use OCP\IRequest;
  16. use OCP\Profiler\IProfiler;
  17. use Symfony\Component\Console\Input\ArgvInput;
  18. use Symfony\Component\Console\Output\ConsoleOutput;
  19. define('OC_CONSOLE', 1);
  20. function exceptionHandler($exception) {
  21. echo 'An unhandled exception has been thrown:' . PHP_EOL;
  22. echo $exception;
  23. exit(1);
  24. }
  25. try {
  26. require_once __DIR__ . '/lib/base.php';
  27. // set to run indefinitely if needed
  28. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  29. @set_time_limit(0);
  30. }
  31. if (!OC::$CLI) {
  32. echo 'This script can be run from the command line only' . PHP_EOL;
  33. exit(1);
  34. }
  35. $config = Server::get(IConfig::class);
  36. set_exception_handler('exceptionHandler');
  37. if (!function_exists('posix_getuid')) {
  38. echo 'The posix extensions are required - see https://www.php.net/manual/en/book.posix.php' . PHP_EOL;
  39. exit(1);
  40. }
  41. $user = posix_getuid();
  42. $configUser = fileowner(OC::$configDir . 'config.php');
  43. if ($user !== $configUser) {
  44. echo 'Console has to be executed with the user that owns the file config/config.php' . PHP_EOL;
  45. echo 'Current user id: ' . $user . PHP_EOL;
  46. echo 'Owner id of config.php: ' . $configUser . PHP_EOL;
  47. echo "Try adding 'sudo -u #" . $configUser . "' to the beginning of the command (without the single quotes)" . PHP_EOL;
  48. echo "If running with 'docker exec' try adding the option '-u " . $configUser . "' to the docker command (without the single quotes)" . PHP_EOL;
  49. exit(1);
  50. }
  51. $oldWorkingDir = getcwd();
  52. if ($oldWorkingDir === false) {
  53. echo 'This script can be run from the Nextcloud root directory only.' . PHP_EOL;
  54. echo "Can't determine current working dir - the script will continue to work but be aware of the above fact." . PHP_EOL;
  55. } elseif ($oldWorkingDir !== __DIR__ && !chdir(__DIR__)) {
  56. echo 'This script can be run from the Nextcloud root directory only.' . PHP_EOL;
  57. echo "Can't change to Nextcloud root directory." . PHP_EOL;
  58. exit(1);
  59. }
  60. if (!(function_exists('pcntl_signal') && function_exists('pcntl_signal_dispatch')) && !in_array('--no-warnings', $argv)) {
  61. 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;
  62. echo "Additionally the function 'pcntl_signal' and 'pcntl_signal_dispatch' need to be enabled in your php.ini." . PHP_EOL;
  63. }
  64. $eventLogger = Server::get(IEventLogger::class);
  65. $eventLogger->start('console:build_application', 'Build Application instance and load commands');
  66. $application = Server::get(Application::class);
  67. /* base.php will have removed eventual debug options from argv in $_SERVER */
  68. $argv = $_SERVER['argv'];
  69. $input = new ArgvInput($argv);
  70. $output = new ConsoleOutput();
  71. $application->loadCommands($input, $output);
  72. $eventLogger->end('console:build_application');
  73. $eventLogger->start('console:run', 'Run the command');
  74. $application->setAutoExit(false);
  75. $exitCode = $application->run($input);
  76. $eventLogger->end('console:run');
  77. $profiler = Server::get(IProfiler::class);
  78. if ($profiler->isEnabled()) {
  79. $eventLogger->end('runtime');
  80. $profile = $profiler->collect(Server::get(IRequest::class), new Response());
  81. $profile->setMethod('occ');
  82. $profile->setUrl(implode(' ', $argv));
  83. $profiler->saveProfile($profile);
  84. $urlGenerator = Server::get(IURLGenerator::class);
  85. $url = $urlGenerator->linkToRouteAbsolute('profiler.main.profiler', [
  86. 'profiler' => 'db',
  87. 'token' => $profile->getToken(),
  88. ]);
  89. $output->getErrorOutput()->writeln('Profiler output available at ' . $url);
  90. }
  91. if ($exitCode > 255) {
  92. $exitCode = 255;
  93. }
  94. exit($exitCode);
  95. } catch (Exception $ex) {
  96. exceptionHandler($ex);
  97. } catch (Error $ex) {
  98. exceptionHandler($ex);
  99. }