1
0

OC_API.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. use OCP\API;
  32. use OCP\AppFramework\Http;
  33. class OC_API {
  34. /**
  35. * api actions
  36. */
  37. protected static $actions = [];
  38. /**
  39. * respond to a call
  40. * @param \OC\OCS\Result $result
  41. * @param string $format the format xml|json
  42. * @psalm-taint-escape html
  43. */
  44. public static function respond($result, $format = 'xml') {
  45. $request = \OC::$server->getRequest();
  46. // Send 401 headers if unauthorised
  47. if ($result->getStatusCode() === \OCP\AppFramework\OCSController::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. public static function requestedFormat(): string {
  93. $formats = ['json', 'xml'];
  94. $format = (isset($_GET['format']) && is_string($_GET['format']) && in_array($_GET['format'], $formats)) ? $_GET['format'] : 'xml';
  95. return $format;
  96. }
  97. /**
  98. * Based on the requested format the response content type is set
  99. * @param string $format
  100. */
  101. public static function setContentType($format = null) {
  102. $format = is_null($format) ? self::requestedFormat() : $format;
  103. if ($format === 'xml') {
  104. header('Content-type: text/xml; charset=UTF-8');
  105. return;
  106. }
  107. if ($format === 'json') {
  108. header('Content-Type: application/json; charset=utf-8');
  109. return;
  110. }
  111. header('Content-Type: application/octet-stream; charset=utf-8');
  112. }
  113. /**
  114. * @param \OCP\IRequest $request
  115. * @return bool
  116. */
  117. protected static function isV2(\OCP\IRequest $request) {
  118. $script = $request->getScriptName();
  119. return substr($script, -11) === '/ocs/v2.php';
  120. }
  121. /**
  122. * @param integer $sc
  123. * @return int
  124. */
  125. public static function mapStatusCodes($sc) {
  126. switch ($sc) {
  127. case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND:
  128. return Http::STATUS_NOT_FOUND;
  129. case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR:
  130. return Http::STATUS_INTERNAL_SERVER_ERROR;
  131. case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR:
  132. return Http::STATUS_INTERNAL_SERVER_ERROR;
  133. case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED:
  134. // already handled for v1
  135. return null;
  136. case 100:
  137. return Http::STATUS_OK;
  138. }
  139. // any 2xx, 4xx and 5xx will be used as is
  140. if ($sc >= 200 && $sc < 600) {
  141. return $sc;
  142. }
  143. return Http::STATUS_BAD_REQUEST;
  144. }
  145. /**
  146. * @param string $format
  147. * @return string
  148. */
  149. public static function renderResult($format, $meta, $data) {
  150. $response = [
  151. 'ocs' => [
  152. 'meta' => $meta,
  153. 'data' => $data,
  154. ],
  155. ];
  156. if ($format == 'json') {
  157. return OC_JSON::encode($response);
  158. }
  159. $writer = new XMLWriter();
  160. $writer->openMemory();
  161. $writer->setIndent(true);
  162. $writer->startDocument();
  163. self::toXML($response, $writer);
  164. $writer->endDocument();
  165. return $writer->outputMemory(true);
  166. }
  167. }