console.php 4.5 KB

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