From ea32d17d88b52a07e04bbc2f8ea0e8d81e84b884 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Mon, 2 Sep 2024 17:06:20 +0200 Subject: fix: Move OC_API into \OC\ApiHelper in standard namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s only used by ocs/v1.php Signed-off-by: Côme Chilliet --- lib/private/legacy/OC_API.php | 162 ------------------------------------------ 1 file changed, 162 deletions(-) delete mode 100644 lib/private/legacy/OC_API.php (limited to 'lib/private/legacy/OC_API.php') diff --git a/lib/private/legacy/OC_API.php b/lib/private/legacy/OC_API.php deleted file mode 100644 index bb2376d4ea0..00000000000 --- a/lib/private/legacy/OC_API.php +++ /dev/null @@ -1,162 +0,0 @@ -getRequest(); - - // Send 401 headers if unauthorised - if ($result->getStatusCode() === \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED) { - // If request comes from JS return dummy auth request - if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { - header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); - } else { - header('WWW-Authenticate: Basic realm="Authorisation Required"'); - } - http_response_code(401); - } - - foreach ($result->getHeaders() as $name => $value) { - header($name . ': ' . $value); - } - - $meta = $result->getMeta(); - $data = $result->getData(); - if (self::isV2($request)) { - $statusCode = self::mapStatusCodes($result->getStatusCode()); - if (!is_null($statusCode)) { - $meta['statuscode'] = $statusCode; - http_response_code($statusCode); - } - } - - self::setContentType($format); - $body = self::renderResult($format, $meta, $data); - echo $body; - } - - /** - * @param XMLWriter $writer - */ - private static function toXML($array, $writer) { - foreach ($array as $k => $v) { - if ($k[0] === '@') { - $writer->writeAttribute(substr($k, 1), $v); - continue; - } elseif (is_numeric($k)) { - $k = 'element'; - } - if (is_array($v)) { - $writer->startElement($k); - self::toXML($v, $writer); - $writer->endElement(); - } else { - $writer->writeElement($k, $v); - } - } - } - - public static function requestedFormat(): string { - $formats = ['json', 'xml']; - - $format = (isset($_GET['format']) && is_string($_GET['format']) && in_array($_GET['format'], $formats)) ? $_GET['format'] : 'xml'; - return $format; - } - - /** - * Based on the requested format the response content type is set - * @param string $format - */ - public static function setContentType($format = null) { - $format = is_null($format) ? self::requestedFormat() : $format; - if ($format === 'xml') { - header('Content-type: text/xml; charset=UTF-8'); - return; - } - - if ($format === 'json') { - header('Content-Type: application/json; charset=utf-8'); - return; - } - - header('Content-Type: application/octet-stream; charset=utf-8'); - } - - /** - * @param \OCP\IRequest $request - * @return bool - */ - protected static function isV2(\OCP\IRequest $request) { - $script = $request->getScriptName(); - - return str_ends_with($script, '/ocs/v2.php'); - } - - /** - * @param integer $sc - * @return int - */ - public static function mapStatusCodes($sc) { - switch ($sc) { - case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND: - return Http::STATUS_NOT_FOUND; - case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR: - return Http::STATUS_INTERNAL_SERVER_ERROR; - case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR: - return Http::STATUS_INTERNAL_SERVER_ERROR; - case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED: - // already handled for v1 - return null; - case 100: - return Http::STATUS_OK; - } - // any 2xx, 4xx and 5xx will be used as is - if ($sc >= 200 && $sc < 600) { - return $sc; - } - - return Http::STATUS_BAD_REQUEST; - } - - /** - * @param string $format - * @return string - */ - public static function renderResult($format, $meta, $data) { - $response = [ - 'ocs' => [ - 'meta' => $meta, - 'data' => $data, - ], - ]; - if ($format == 'json') { - return json_encode($response, JSON_HEX_TAG); - } - - $writer = new XMLWriter(); - $writer->openMemory(); - $writer->setIndent(true); - $writer->startDocument(); - self::toXML($response, $writer); - $writer->endDocument(); - return $writer->outputMemory(true); - } -} -- cgit v1.2.3