getParam('format', 'xml'); if (self::isV2($request)) { $response = new V2Response(new DataResponse([], $statusCode, $headers), $format, $statusMessage); } else { $response = new V1Response(new DataResponse([], $statusCode, $headers), $format, $statusMessage); } // Send 401 headers if unauthorised if ($response->getOCSStatus() === 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 ($response->getHeaders() as $name => $value) { header($name . ': ' . $value); } http_response_code($overrideHttpStatusCode ?? $response->getStatus()); self::setContentType($format); $body = $response->render(); echo $body; } /** * Based on the requested format the response content type is set */ public static function setContentType(?string $format = null): void { $format ??= Server::get(IRequest::class)->getParam('format', 'xml'); 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'); } protected static function isV2(IRequest $request): bool { $script = $request->getScriptName(); return str_ends_with($script, '/ocs/v2.php'); } }