OccController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace OCA\OCCWeb\Controller;
  3. use Exception;
  4. use OC;
  5. use OC\Console\Application;
  6. use OC\MemoryInfo;
  7. use OCP\IRequest;
  8. use OCP\AppFramework\Http\TemplateResponse;
  9. use OCP\AppFramework\Http\DataResponse;
  10. use OCP\AppFramework\Controller;
  11. use OCP\ILogger;
  12. use Symfony\Component\Console\Input\StringInput;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class OccController extends Controller
  15. {
  16. private $logger;
  17. private $userId;
  18. private $application;
  19. private $output;
  20. public function __construct(ILogger $logger, $AppName, IRequest $request, $userId)
  21. {
  22. parent::__construct($AppName, $request);
  23. $this->logger = $logger;
  24. $this->userId = $userId;
  25. $this->application = new Application(
  26. OC::$server->getConfig(),
  27. OC::$server->getEventDispatcher(),
  28. new FakeRequest(),
  29. OC::$server->getLogger(),
  30. OC::$server->query(MemoryInfo::class)
  31. );
  32. $this->application->setAutoExit(false);
  33. $this->output = new OccOutput(OutputInterface::VERBOSITY_NORMAL, true);
  34. $this->application->loadCommands(new StringInput(""), $this->output);
  35. }
  36. /**
  37. * @NoCSRFRequired
  38. */
  39. public function index()
  40. {
  41. return new TemplateResponse('occweb', 'index');
  42. }
  43. /**
  44. * @param $input
  45. * @return string
  46. */
  47. private function run($input)
  48. {
  49. try {
  50. $this->application->run($input, $this->output);
  51. return $this->output->fetch();
  52. } catch (Exception $ex) {
  53. $this->logger->logException($ex);
  54. return "error: " . $ex->getMessage();
  55. }
  56. }
  57. /**
  58. * @param string $command
  59. * @return DataResponse
  60. */
  61. public function cmd($command)
  62. {
  63. $this->logger->debug($command);
  64. $input = new StringInput($command);
  65. $response = $this->run($input);
  66. $this->logger->debug($response);
  67. return new DataResponse($response);
  68. }
  69. public function list() {
  70. $defs = $this->application->application->all();
  71. $cmds = array();
  72. foreach ($defs as $d) {
  73. array_push($cmds, $d->getName());
  74. }
  75. return new DataResponse($cmds);
  76. }
  77. }