OC_API.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /**
  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 \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND:
  131. return Http::STATUS_NOT_FOUND;
  132. case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR:
  133. return Http::STATUS_INTERNAL_SERVER_ERROR;
  134. case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR:
  135. return Http::STATUS_INTERNAL_SERVER_ERROR;
  136. case \OCP\AppFramework\OCSController::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. }