console.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author hoellen <dev@hoellen.eu>
  8. * @author J0WI <J0WI@users.noreply.github.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Ko- <k.stoffelen@cs.ru.nl>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Weimann <mail@michael-weimann.eu>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Patrick Paysant <patrick.paysant@linagora.com>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. require_once __DIR__ . '/lib/versioncheck.php';
  35. use OC\Console\Application;
  36. use Symfony\Component\Console\Input\ArgvInput;
  37. use Symfony\Component\Console\Output\ConsoleOutput;
  38. define('OC_CONSOLE', 1);
  39. function exceptionHandler($exception) {
  40. echo "An unhandled exception has been thrown:" . PHP_EOL;
  41. echo $exception;
  42. exit(1);
  43. }
  44. try {
  45. require_once __DIR__ . '/lib/base.php';
  46. // set to run indefinitely if needed
  47. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  48. @set_time_limit(0);
  49. }
  50. if (!OC::$CLI) {
  51. echo "This script can be run from the command line only" . PHP_EOL;
  52. exit(1);
  53. }
  54. $config = \OC::$server->getConfig();
  55. set_exception_handler('exceptionHandler');
  56. if (!function_exists('posix_getuid')) {
  57. echo "The posix extensions are required - see https://www.php.net/manual/en/book.posix.php" . PHP_EOL;
  58. exit(1);
  59. }
  60. // Check if the data directory is available and the server is installed
  61. $dataDirectory = $config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data');
  62. if ($config->getSystemValueBool('installed', false) && !is_dir($dataDirectory)) {
  63. echo "Data directory (" . $dataDirectory . ") not found" . PHP_EOL;
  64. exit(1);
  65. }
  66. // Check if the user running the console is the same as the user that owns the data directory
  67. // If the data directory does not exist, the server is not setup yet and we can skip.
  68. if (is_dir($dataDirectory)) {
  69. $user = posix_getuid();
  70. $dataDirectoryUser = fileowner($dataDirectory);
  71. if ($user !== $dataDirectoryUser) {
  72. echo "Console has to be executed with the user that owns the data directory" . PHP_EOL;
  73. echo "Current user id: " . $user . PHP_EOL;
  74. echo "Owner id of the data directory: " . $dataDirectoryUser . PHP_EOL;
  75. echo "Try adding 'sudo -u #" . $dataDirectoryUser . "' to the beginning of the command (without the single quotes)" . PHP_EOL;
  76. echo "If running with 'docker exec' try adding the option '-u " . $dataDirectoryUser . "' to the docker command (without the single quotes)" . PHP_EOL;
  77. exit(1);
  78. }
  79. }
  80. $oldWorkingDir = getcwd();
  81. if ($oldWorkingDir === false) {
  82. echo "This script can be run from the Nextcloud root directory only." . PHP_EOL;
  83. echo "Can't determine current working dir - the script will continue to work but be aware of the above fact." . PHP_EOL;
  84. } elseif ($oldWorkingDir !== __DIR__ && !chdir(__DIR__)) {
  85. echo "This script can be run from the Nextcloud root directory only." . PHP_EOL;
  86. echo "Can't change to Nextcloud root directory." . PHP_EOL;
  87. exit(1);
  88. }
  89. if (!(function_exists('pcntl_signal') && function_exists('pcntl_signal_dispatch')) && !in_array('--no-warnings', $argv)) {
  90. 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;
  91. echo "Additionally the function 'pcntl_signal' and 'pcntl_signal_dispatch' need to be enabled in your php.ini." . PHP_EOL;
  92. }
  93. $application = new Application(
  94. $config,
  95. \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class),
  96. \OC::$server->getRequest(),
  97. \OC::$server->get(\Psr\Log\LoggerInterface::class),
  98. \OC::$server->query(\OC\MemoryInfo::class)
  99. );
  100. $application->loadCommands(new ArgvInput(), new ConsoleOutput());
  101. $application->run();
  102. } catch (Exception $ex) {
  103. exceptionHandler($ex);
  104. } catch (Error $ex) {
  105. exceptionHandler($ex);
  106. }