occweb/lib/Controller/OCCController.php

96 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2019-01-19 14:54:24 +00:00
namespace OCA\OCCWeb\Controller;
2020-01-26 14:37:16 +00:00
use Exception;
use OC;
use OC\Console\Application;
use OC\MemoryInfo;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\ILogger;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
2019-01-24 18:56:20 +00:00
class OccController extends Controller
{
private $logger;
private $userId;
private $application;
private $output;
2020-01-26 14:37:16 +00:00
public function __construct(ILogger $logger, $AppName, IRequest $request, $userId)
{
parent::__construct($AppName, $request);
$this->logger = $logger;
2020-01-26 14:37:16 +00:00
$this->userId = $userId;
2020-01-26 14:37:16 +00:00
$this->application = new Application(
OC::$server->getConfig(),
OC::$server->getEventDispatcher(),
new FakeRequest(),
OC::$server->getLogger(),
OC::$server->query(MemoryInfo::class)
);
$this->application->setAutoExit(false);
2019-01-19 16:20:07 +00:00
$this->output = new OccOutput(OutputInterface::VERBOSITY_NORMAL, true);
$this->application->loadCommands(new StringInput(""), $this->output);
}
/**
* @NoCSRFRequired
*/
public function index()
{
2020-01-26 14:37:16 +00:00
return new TemplateResponse('occweb', 'index');
}
2020-01-26 14:37:16 +00:00
/**
* @param $input
* @return string
*/
private function run($input)
{
try {
$this->application->run($input, $this->output);
return $this->output->fetch();
2020-01-26 14:37:16 +00:00
} catch (Exception $ex) {
exceptionHandler($ex);
} catch (Error $ex) {
exceptionHandler($ex);
}
}
/**
* @param string $command
* @return DataResponse
*/
public function cmd($command)
{
2020-01-26 14:37:16 +00:00
$this->logger->debug($command);
$input = new StringInput($command);
$response = $this->run($input);
2020-01-26 14:37:16 +00:00
$this->logger->debug($response);
return new DataResponse($response);
}
public function list() {
$defs = $this->application->application->all();
$cmds = array();
foreach ($defs as $d) {
array_push($cmds, $d->getName());
}
return new DataResponse($cmds);
}
}
function exceptionHandler($exception)
{
echo "An unhandled exception has been thrown:" . PHP_EOL;
echo $exception;
exit(1);
}