OCSMiddleware.php 4.2 KB

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