OCSMiddleware.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\AppFramework\Middleware;
  27. use OC\AppFramework\Http;
  28. use OC\AppFramework\OCS\BaseResponse;
  29. use OC\AppFramework\OCS\V1Response;
  30. use OC\AppFramework\OCS\V2Response;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCP\AppFramework\Http\Response;
  35. use OCP\AppFramework\Middleware;
  36. use OCP\AppFramework\OCS\OCSException;
  37. use OCP\AppFramework\OCSController;
  38. use OCP\IRequest;
  39. class OCSMiddleware extends Middleware {
  40. /** @var IRequest */
  41. private $request;
  42. /** @var int */
  43. private $ocsVersion;
  44. /**
  45. * @param IRequest $request
  46. */
  47. public function __construct(IRequest $request) {
  48. $this->request = $request;
  49. }
  50. /**
  51. * @param Controller $controller
  52. * @param string $methodName
  53. */
  54. public function beforeController($controller, $methodName) {
  55. if ($controller instanceof OCSController) {
  56. if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) {
  57. $this->ocsVersion = 2;
  58. } else {
  59. $this->ocsVersion = 1;
  60. }
  61. $controller->setOCSVersion($this->ocsVersion);
  62. }
  63. }
  64. /**
  65. * @param Controller $controller
  66. * @param string $methodName
  67. * @param \Exception $exception
  68. * @throws \Exception
  69. * @return BaseResponse
  70. */
  71. public function afterException($controller, $methodName, \Exception $exception) {
  72. if ($controller instanceof OCSController && $exception instanceof OCSException) {
  73. $code = $exception->getCode();
  74. if ($code === 0) {
  75. $code = \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR;
  76. }
  77. return $this->buildNewResponse($controller, $code, $exception->getMessage());
  78. }
  79. throw $exception;
  80. }
  81. /**
  82. * @param Controller $controller
  83. * @param string $methodName
  84. * @param Response $response
  85. * @return \OCP\AppFramework\Http\Response
  86. */
  87. public function afterController($controller, $methodName, Response $response) {
  88. /*
  89. * If a different middleware has detected that a request unauthorized or forbidden
  90. * we need to catch the response and convert it to a proper OCS response.
  91. */
  92. if ($controller instanceof OCSController && !($response instanceof BaseResponse)) {
  93. if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) {
  94. $message = '';
  95. if ($response instanceof JSONResponse) {
  96. /** @var DataResponse $response */
  97. $message = $response->getData()['message'];
  98. }
  99. return $this->buildNewResponse($controller, OCSController::RESPOND_UNAUTHORISED, $message);
  100. }
  101. if ($response->getStatus() === Http::STATUS_FORBIDDEN) {
  102. $message = '';
  103. if ($response instanceof JSONResponse) {
  104. /** @var DataResponse $response */
  105. $message = $response->getData()['message'];
  106. }
  107. return $this->buildNewResponse($controller, Http::STATUS_FORBIDDEN, $message);
  108. }
  109. }
  110. return $response;
  111. }
  112. /**
  113. * @param Controller $controller
  114. * @param int $code
  115. * @param string $message
  116. * @return V1Response|V2Response
  117. */
  118. private function buildNewResponse(Controller $controller, $code, $message) {
  119. $format = $this->getFormat($controller);
  120. $data = new DataResponse();
  121. $data->setStatus($code);
  122. if ($this->ocsVersion === 1) {
  123. $response = new V1Response($data, $format, $message);
  124. } else {
  125. $response = new V2Response($data, $format, $message);
  126. }
  127. return $response;
  128. }
  129. /**
  130. * @param Controller $controller
  131. * @return string
  132. */
  133. private function getFormat(Controller $controller) {
  134. // get format from the url format or request format parameter
  135. $format = $this->request->getParam('format');
  136. // if none is given try the first Accept header
  137. if ($format === null) {
  138. $headers = $this->request->getHeader('Accept');
  139. $format = $controller->getResponderByHTTPHeader($headers, 'xml');
  140. }
  141. return $format;
  142. }
  143. }