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.

112 lines
3.7 KiB

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