/** * @PublicPage * @NoCSRFRequired * * Execute occ command * Sample request * POST http://domain.tld/index.php/occ/status', * { * 'params': { * '--no-warnings':'1', * '--output':'json' * }, * 'token': 'someToken' * } * * @param string $command * @param string $token * @param array $params * * @return JSONResponse * @throws \Exception */ public function execute($command, $token, $params = []) { try { $this->validateRequest($command, $token); $output = new BufferedOutput(); $formatter = $output->getFormatter(); $formatter->setDecorated(false); $this->console->setAutoExit(false); $this->console->loadCommands(new ArrayInput([]), $output); $inputArray = array_merge(['command' => $command], $params); $input = new ArrayInput($inputArray); $exitCode = $this->console->run($input, $output); $response = $output->fetch(); $json = ['exitCode' => $exitCode, 'response' => $response]; } catch (\UnexpectedValueException $e) { $json = ['exitCode' => 126, 'response' => 'Not allowed', 'details' => $e->getMessage()]; } return new JSONResponse($json); }
$configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); if ($user['name'] !== $configUser['name']) { echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL; echo "Current user: "******"Owner of config.php: " . $configUser['name'] . PHP_EOL; echo "Try adding 'sudo -u " . $configUser['name'] . " ' to the beginning of the command (without the single quotes)" . PHP_EOL; exit(0); } } $oldWorkingDir = getcwd(); if ($oldWorkingDir === false) { echo "This script can be run from the ownCloud root directory only." . PHP_EOL; echo "Can't determine current working dir - the script will continue to work but be aware of the above fact." . PHP_EOL; } else { if ($oldWorkingDir !== __DIR__ && !chdir(__DIR__)) { echo "This script can be run from the ownCloud root directory only." . PHP_EOL; echo "Can't change to ownCloud root diretory." . PHP_EOL; exit(1); } } if (!function_exists('pcntl_signal')) { echo "The process control (PCNTL) extensions are required in case you want to interrupt long running commands - see http://php.net/manual/en/book.pcntl.php" . PHP_EOL; } $application = new Application(\OC::$server->getConfig(), \OC::$server->getEventDispatcher(), \OC::$server->getRequest()); $application->loadCommands(new ConsoleOutput()); $application->run(); } catch (Exception $ex) { echo "An unhandled exception has been thrown:" . PHP_EOL; echo $ex; exit(1); }