console.php 3.8 KB

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