2019-01-19 14:40:52 +00:00
|
|
|
<?php
|
|
|
|
|
2019-01-19 14:54:24 +00:00
|
|
|
namespace OCA\OCCWeb\Controller;
|
2019-01-19 14:40:52 +00:00
|
|
|
|
2020-01-26 14:37:16 +00:00
|
|
|
use Exception;
|
|
|
|
use OC;
|
|
|
|
use OC\Console\Application;
|
|
|
|
use OC\MemoryInfo;
|
2019-01-19 14:40:52 +00:00
|
|
|
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
|
2019-01-19 14:40:52 +00:00
|
|
|
{
|
|
|
|
private $logger;
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
private $application;
|
|
|
|
private $output;
|
2020-03-02 09:12:51 +00:00
|
|
|
private $blacklist;
|
2020-01-26 14:37:16 +00:00
|
|
|
public function __construct(ILogger $logger, $AppName, IRequest $request, $userId)
|
2019-01-19 14:40:52 +00:00
|
|
|
{
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->logger = $logger;
|
2020-01-26 14:37:16 +00:00
|
|
|
$this->userId = $userId;
|
2020-03-02 09:12:51 +00:00
|
|
|
$this->blacklist = array("maintenance:mode on");
|
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)
|
2019-01-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
$this->application->setAutoExit(false);
|
2019-01-19 16:20:07 +00:00
|
|
|
$this->output = new OccOutput(OutputInterface::VERBOSITY_NORMAL, true);
|
2019-01-19 14:40:52 +00:00
|
|
|
$this->application->loadCommands(new StringInput(""), $this->output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-01-26 14:37:16 +00:00
|
|
|
return new TemplateResponse('occweb', 'index');
|
2019-01-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 14:37:16 +00:00
|
|
|
/**
|
|
|
|
* @param $input
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-19 14:40:52 +00:00
|
|
|
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) {
|
2020-03-02 09:12:51 +00:00
|
|
|
exceptionHandler($ex);
|
|
|
|
} catch (Error $ex) {
|
|
|
|
exceptionHandler($ex);
|
2019-01-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $command
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function cmd($command)
|
|
|
|
{
|
2020-01-26 14:37:16 +00:00
|
|
|
$this->logger->debug($command);
|
2020-03-02 09:12:51 +00:00
|
|
|
if (in_array($command, $this->blacklist)) {
|
|
|
|
$this->logger->debug("Command blacklist");
|
|
|
|
return new DataResponse(array("error" => "\n".'\u001b[37;41m \u001b[39;49m'."\n".'\u001b[37;41m Command blacklisted. \u001b[39;49m'."\n".'\u001b[37;41m \u001b[39;49m'."\n\n"), 403);
|
|
|
|
}
|
2019-01-19 14:40:52 +00:00
|
|
|
$input = new StringInput($command);
|
|
|
|
$response = $this->run($input);
|
2020-01-26 14:37:16 +00:00
|
|
|
$this->logger->debug($response);
|
2019-01-19 14:40:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 09:12:51 +00:00
|
|
|
function exceptionHandler($exception)
|
|
|
|
{
|
|
|
|
echo "An unhandled exception has been thrown:" . PHP_EOL;
|
|
|
|
echo $exception;
|
|
|
|
exit(1);
|
|
|
|
}
|