OCCController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace OCA\TestNextcloudApp\Controller;
  3. use OC\Console\Application;
  4. use OCP\IRequest;
  5. use OCP\AppFramework\Http\TemplateResponse;
  6. use OCP\AppFramework\Http\DataResponse;
  7. use OCP\AppFramework\Controller;
  8. use OCP\ILogger;
  9. use Symfony\Component\Console\Input\StringInput;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class OCCController extends Controller implements IRequest
  13. {
  14. private $logger;
  15. private $userId;
  16. private $application;
  17. private $output;
  18. public $server;
  19. public function __construct(ILogger $logger, $AppName, IRequest $request, $UserId)
  20. {
  21. parent::__construct($AppName, $request);
  22. $this->logger = $logger;
  23. $this->userId = $UserId;
  24. $this->server = array(
  25. 'argv' => ["occ"],
  26. );
  27. $this->application = new Application(
  28. \OC::$server->getConfig(),
  29. \OC::$server->getEventDispatcher(),
  30. $this,
  31. \OC::$server->getLogger(),
  32. \OC::$server->query(\OC\MemoryInfo::class)
  33. );
  34. $this->application->setAutoExit(false);
  35. // $this->output = new OCCOutput();
  36. $this->output = new OCCOutput(OutputInterface::VERBOSITY_NORMAL, true);
  37. $this->application->loadCommands(new StringInput(""), $this->output);
  38. }
  39. /**
  40. * CAUTION: the @Stuff turns off security checks; for this page no admin is
  41. * required and no CSRF check. If you don't know what CSRF is, read
  42. * it up in the docs or you might create a security hole. This is
  43. * basically the only required method to add this exemption, don't
  44. * add it to any other method if you don't exactly know what it does
  45. *
  46. * @NoAdminRequired
  47. * @NoCSRFRequired
  48. */
  49. public function index()
  50. {
  51. return new TemplateResponse('testnextcloudapp', 'index'); // templates/index.php
  52. }
  53. private function run($input)
  54. {
  55. try {
  56. $this->application->run($input, $this->output);
  57. return $this->output->fetch();
  58. } catch (\Exception $ex) {
  59. exceptionHandler($ex);
  60. } catch (Error $ex) {
  61. exceptionHandler($ex);
  62. }
  63. }
  64. /**
  65. * @NoAdminRequired
  66. * @param string $command
  67. * @return DataResponse
  68. */
  69. public function cmd($command)
  70. {
  71. $this->logger->info($command);
  72. $input = new StringInput($command);
  73. // TODO : Interactive ?
  74. $response = $this->run($input);
  75. // $this->logger->info($response);
  76. // $converter = new AnsiToHtmlConverter();
  77. // return new DataResponse($converter->convert($response));
  78. return new DataResponse($response);
  79. }
  80. public function list() {
  81. $defs = $this->application->application->all();
  82. $cmds = array();
  83. foreach ($defs as $d) {
  84. array_push($cmds, $d->getName());
  85. }
  86. return new DataResponse($cmds);
  87. }
  88. /**
  89. * Lets you access post and get parameters by the index
  90. * In case of json requests the encoded json body is accessed
  91. *
  92. * @param string $key the key which you want to access in the URL Parameter
  93. * placeholder, $_POST or $_GET array.
  94. * The priority how they're returned is the following:
  95. * 1. URL parameters
  96. * 2. POST parameters
  97. * 3. GET parameters
  98. * @param mixed $default If the key is not found, this value will be returned
  99. * @return mixed the content of the array
  100. * @since 6.0.0
  101. */
  102. public function getParam(string $key, $default = null)
  103. {
  104. // TODO: Implement getParam() method.
  105. }
  106. /**
  107. * Returns all params that were received, be it from the request
  108. *
  109. * (as GET or POST) or through the URL by the route
  110. *
  111. * @return array the array with all parameters
  112. * @since 6.0.0
  113. */
  114. public function getParams(): array
  115. {
  116. // TODO: Implement getParams() method.
  117. }
  118. /**
  119. * Returns the method of the request
  120. *
  121. * @return string the method of the request (POST, GET, etc)
  122. * @since 6.0.0
  123. */
  124. public function getMethod(): string
  125. {
  126. // TODO: Implement getMethod() method.
  127. }
  128. /**
  129. * Shortcut for accessing an uploaded file through the $_FILES array
  130. *
  131. * @param string $key the key that will be taken from the $_FILES array
  132. * @return array the file in the $_FILES element
  133. * @since 6.0.0
  134. */
  135. public function getUploadedFile(string $key)
  136. {
  137. // TODO: Implement getUploadedFile() method.
  138. }
  139. /**
  140. * Shortcut for getting env variables
  141. *
  142. * @param string $key the key that will be taken from the $_ENV array
  143. * @return array the value in the $_ENV element
  144. * @since 6.0.0
  145. */
  146. public function getEnv(string $key)
  147. {
  148. // TODO: Implement getEnv() method.
  149. }
  150. /**
  151. * Shortcut for getting cookie variables
  152. *
  153. * @param string $key the key that will be taken from the $_COOKIE array
  154. * @return string|null the value in the $_COOKIE element
  155. * @since 6.0.0
  156. */
  157. public function getCookie(string $key)
  158. {
  159. // TODO: Implement getCookie() method.
  160. }
  161. /**
  162. * Checks if the CSRF check was correct
  163. *
  164. * @return bool true if CSRF check passed
  165. * @since 6.0.0
  166. */
  167. public function passesCSRFCheck(): bool
  168. {
  169. // TODO: Implement passesCSRFCheck() method.
  170. }
  171. /**
  172. * Checks if the strict cookie has been sent with the request if the request
  173. * is including any cookies.
  174. *
  175. * @return bool
  176. * @since 9.0.0
  177. */
  178. public function passesStrictCookieCheck(): bool
  179. {
  180. // TODO: Implement passesStrictCookieCheck() method.
  181. }
  182. /**
  183. * Checks if the lax cookie has been sent with the request if the request
  184. * is including any cookies.
  185. *
  186. * @return bool
  187. * @since 9.0.0
  188. */
  189. public function passesLaxCookieCheck(): bool
  190. {
  191. // TODO: Implement passesLaxCookieCheck() method.
  192. }
  193. /**
  194. * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
  195. * If `mod_unique_id` is installed this value will be taken.
  196. *
  197. * @return string
  198. * @since 8.1.0
  199. */
  200. public function getId(): string
  201. {
  202. // TODO: Implement getId() method.
  203. }
  204. /**
  205. * Returns the remote address, if the connection came from a trusted proxy
  206. * and `forwarded_for_headers` has been configured then the IP address
  207. * specified in this header will be returned instead.
  208. * Do always use this instead of $_SERVER['REMOTE_ADDR']
  209. *
  210. * @return string IP address
  211. * @since 8.1.0
  212. */
  213. public function getRemoteAddress(): string
  214. {
  215. // TODO: Implement getRemoteAddress() method.
  216. }
  217. /**
  218. * Returns the server protocol. It respects reverse proxy servers and load
  219. * balancers.
  220. *
  221. * @return string Server protocol (http or https)
  222. * @since 8.1.0
  223. */
  224. public function getServerProtocol(): string
  225. {
  226. // TODO: Implement getServerProtocol() method.
  227. }
  228. /**
  229. * Returns the used HTTP protocol.
  230. *
  231. * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
  232. * @since 8.2.0
  233. */
  234. public function getHttpProtocol(): string
  235. {
  236. // TODO: Implement getHttpProtocol() method.
  237. }
  238. /**
  239. * Returns the request uri, even if the website uses one or more
  240. * reverse proxies
  241. *
  242. * @return string
  243. * @since 8.1.0
  244. */
  245. public function getRequestUri(): string
  246. {
  247. // TODO: Implement getRequestUri() method.
  248. }
  249. /**
  250. * Get raw PathInfo from request (not urldecoded)
  251. *
  252. * @throws \Exception
  253. * @return string Path info
  254. * @since 8.1.0
  255. */
  256. public function getRawPathInfo(): string
  257. {
  258. // TODO: Implement getRawPathInfo() method.
  259. }
  260. /**
  261. * Get PathInfo from request
  262. *
  263. * @throws \Exception
  264. * @return string|false Path info or false when not found
  265. * @since 8.1.0
  266. */
  267. public function getPathInfo()
  268. {
  269. // TODO: Implement getPathInfo() method.
  270. }
  271. /**
  272. * Returns the script name, even if the website uses one or more
  273. * reverse proxies
  274. *
  275. * @return string the script name
  276. * @since 8.1.0
  277. */
  278. public function getScriptName(): string
  279. {
  280. // TODO: Implement getScriptName() method.
  281. }
  282. /**
  283. * Checks whether the user agent matches a given regex
  284. *
  285. * @param array $agent array of agent names
  286. * @return bool true if at least one of the given agent matches, false otherwise
  287. * @since 8.1.0
  288. */
  289. public function isUserAgent(array $agent): bool
  290. {
  291. // TODO: Implement isUserAgent() method.
  292. }
  293. /**
  294. * Returns the unverified server host from the headers without checking
  295. * whether it is a trusted domain
  296. *
  297. * @return string Server host
  298. * @since 8.1.0
  299. */
  300. public function getInsecureServerHost(): string
  301. {
  302. // TODO: Implement getInsecureServerHost() method.
  303. }
  304. /**
  305. * Returns the server host from the headers, or the first configured
  306. * trusted domain if the host isn't in the trusted list
  307. *
  308. * @return string Server host
  309. * @since 8.1.0
  310. */
  311. public function getServerHost(): string
  312. {
  313. // TODO: Implement getServerHost() method.
  314. }
  315. /**
  316. * @param string $name
  317. *
  318. * @return string
  319. * @since 6.0.0
  320. */
  321. public function getHeader(string $name): string
  322. {
  323. // TODO: Implement getHeader() method.
  324. }
  325. }
  326. function exceptionHandler($exception)
  327. {
  328. echo "An unhandled exception has been thrown:" . PHP_EOL;
  329. echo $exception;
  330. exit(1);
  331. }