You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.8 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Tom Needham <tom@owncloud.com>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. use OCP\API;
  33. use OCP\AppFramework\Http;
  34. class OC_API {
  35. /**
  36. * api actions
  37. */
  38. protected static $actions = [];
  39. /**
  40. * respond to a call
  41. * @param \OC\OCS\Result $result
  42. * @param string $format the format xml|json
  43. */
  44. public static function respond($result, $format='xml') {
  45. $request = \OC::$server->getRequest();
  46. // Send 401 headers if unauthorised
  47. if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
  48. // If request comes from JS return dummy auth request
  49. if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
  50. header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
  51. } else {
  52. header('WWW-Authenticate: Basic realm="Authorisation Required"');
  53. }
  54. http_response_code(401);
  55. }
  56. foreach ($result->getHeaders() as $name => $value) {
  57. header($name . ': ' . $value);
  58. }
  59. $meta = $result->getMeta();
  60. $data = $result->getData();
  61. if (self::isV2($request)) {
  62. $statusCode = self::mapStatusCodes($result->getStatusCode());
  63. if (!is_null($statusCode)) {
  64. $meta['statuscode'] = $statusCode;
  65. http_response_code($statusCode);
  66. }
  67. }
  68. self::setContentType($format);
  69. $body = self::renderResult($format, $meta, $data);
  70. echo $body;
  71. }
  72. /**
  73. * @param XMLWriter $writer
  74. */
  75. private static function toXML($array, $writer) {
  76. foreach ($array as $k => $v) {
  77. if ($k[0] === '@') {
  78. $writer->writeAttribute(substr($k, 1), $v);
  79. continue;
  80. } elseif (is_numeric($k)) {
  81. $k = 'element';
  82. }
  83. if (is_array($v)) {
  84. $writer->startElement($k);
  85. self::toXML($v, $writer);
  86. $writer->endElement();
  87. } else {
  88. $writer->writeElement($k, $v);
  89. }
  90. }
  91. }
  92. /**
  93. * @return string
  94. */
  95. public static function requestedFormat() {
  96. $formats = ['json', 'xml'];
  97. $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
  98. return $format;
  99. }
  100. /**
  101. * Based on the requested format the response content type is set
  102. * @param string $format
  103. */
  104. public static function setContentType($format = null) {
  105. $format = is_null($format) ? self::requestedFormat() : $format;
  106. if ($format === 'xml') {
  107. header('Content-type: text/xml; charset=UTF-8');
  108. return;
  109. }
  110. if ($format === 'json') {
  111. header('Content-Type: application/json; charset=utf-8');
  112. return;
  113. }
  114. header('Content-Type: application/octet-stream; charset=utf-8');
  115. }
  116. /**
  117. * @param \OCP\IRequest $request
  118. * @return bool
  119. */
  120. protected static function isV2(\OCP\IRequest $request) {
  121. $script = $request->getScriptName();
  122. return substr($script, -11) === '/ocs/v2.php';
  123. }
  124. /**
  125. * @param integer $sc
  126. * @return int
  127. */
  128. public static function mapStatusCodes($sc) {
  129. switch ($sc) {
  130. case API::RESPOND_NOT_FOUND:
  131. return Http::STATUS_NOT_FOUND;
  132. case API::RESPOND_SERVER_ERROR:
  133. return Http::STATUS_INTERNAL_SERVER_ERROR;
  134. case API::RESPOND_UNKNOWN_ERROR:
  135. return Http::STATUS_INTERNAL_SERVER_ERROR;
  136. case API::RESPOND_UNAUTHORISED:
  137. // already handled for v1
  138. return null;
  139. case 100:
  140. return Http::STATUS_OK;
  141. }
  142. // any 2xx, 4xx and 5xx will be used as is
  143. if ($sc >= 200 && $sc < 600) {
  144. return $sc;
  145. }
  146. return Http::STATUS_BAD_REQUEST;
  147. }
  148. /**
  149. * @param string $format
  150. * @return string
  151. */
  152. public static function renderResult($format, $meta, $data) {
  153. $response = [
  154. 'ocs' => [
  155. 'meta' => $meta,
  156. 'data' => $data,
  157. ],
  158. ];
  159. if ($format == 'json') {
  160. return OC_JSON::encode($response);
  161. }
  162. $writer = new XMLWriter();
  163. $writer->openMemory();
  164. $writer->setIndent(true);
  165. $writer->startDocument();
  166. self::toXML($response, $writer);
  167. $writer->endDocument();
  168. return $writer->outputMemory(true);
  169. }
  170. }