console.php 3.7 KB

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