2012-07-28 17:40:11 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2024-09-03 08:56:26 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2015-10-05 14:54:56 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-10-05 14:54:56 -04:00
|
|
|
*/
|
2024-09-02 11:06:20 -04:00
|
|
|
|
2024-09-02 11:44:46 -04:00
|
|
|
namespace OC\OCS;
|
2024-09-02 11:06:20 -04:00
|
|
|
|
2024-09-03 08:56:26 -04:00
|
|
|
use OC\AppFramework\OCS\V1Response;
|
|
|
|
|
use OC\AppFramework\OCS\V2Response;
|
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2024-09-02 11:28:06 -04:00
|
|
|
use OCP\AppFramework\OCSController;
|
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
use OCP\Server;
|
2015-08-03 10:05:50 -04:00
|
|
|
|
2024-09-02 11:06:20 -04:00
|
|
|
class ApiHelper {
|
2012-07-28 17:50:40 -04:00
|
|
|
/**
|
2024-09-03 08:56:26 -04:00
|
|
|
* Respond to a call
|
2020-12-11 16:23:11 -05:00
|
|
|
* @psalm-taint-escape html
|
2024-09-09 04:45:41 -04:00
|
|
|
* @param int $overrideHttpStatusCode force the HTTP status code, only used for the special case of maintenance mode which return 503 even for v1
|
2012-12-31 10:47:15 -05:00
|
|
|
*/
|
2024-09-09 04:45:41 -04:00
|
|
|
public static function respond(int $statusCode, string $statusMessage, array $headers = [], ?int $overrideHttpStatusCode = null): void {
|
2024-09-02 11:28:06 -04:00
|
|
|
$request = Server::get(IRequest::class);
|
2024-09-03 08:56:26 -04:00
|
|
|
$format = $request->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);
|
|
|
|
|
}
|
2016-02-15 09:38:37 -05:00
|
|
|
|
2013-01-25 07:48:59 -05:00
|
|
|
// Send 401 headers if unauthorised
|
2024-09-03 08:56:26 -04:00
|
|
|
if ($response->getOCSStatus() === OCSController::RESPOND_UNAUTHORISED) {
|
2016-02-15 09:38:37 -05:00
|
|
|
// 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"');
|
|
|
|
|
}
|
2018-06-26 04:32:50 -04:00
|
|
|
http_response_code(401);
|
2013-01-25 07:48:59 -05:00
|
|
|
}
|
2015-08-03 10:05:50 -04:00
|
|
|
|
2024-09-03 08:56:26 -04:00
|
|
|
foreach ($response->getHeaders() as $name => $value) {
|
2015-08-07 07:12:43 -04:00
|
|
|
header($name . ': ' . $value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 04:45:41 -04:00
|
|
|
http_response_code($overrideHttpStatusCode ?? $response->getStatus());
|
2015-08-03 10:05:50 -04:00
|
|
|
|
2015-08-03 15:03:11 -04:00
|
|
|
self::setContentType($format);
|
2024-09-03 08:56:26 -04:00
|
|
|
$body = $response->render();
|
2015-08-03 15:03:11 -04:00
|
|
|
echo $body;
|
2012-07-28 17:50:40 -04:00
|
|
|
}
|
2012-08-01 13:48:51 -04:00
|
|
|
|
2014-03-11 19:35:19 -04:00
|
|
|
/**
|
|
|
|
|
* Based on the requested format the response content type is set
|
|
|
|
|
*/
|
2024-09-02 11:28:06 -04:00
|
|
|
public static function setContentType(?string $format = null): void {
|
2024-09-03 08:56:26 -04:00
|
|
|
$format ??= Server::get(IRequest::class)->getParam('format', 'xml');
|
2014-03-11 19:35:19 -04:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 11:28:06 -04:00
|
|
|
protected static function isV2(IRequest $request): bool {
|
2015-08-03 10:05:50 -04:00
|
|
|
$script = $request->getScriptName();
|
2014-03-11 19:35:19 -04:00
|
|
|
|
2023-07-06 21:24:20 -04:00
|
|
|
return str_ends_with($script, '/ocs/v2.php');
|
2015-08-03 10:05:50 -04:00
|
|
|
}
|
2012-07-30 15:03:41 -04:00
|
|
|
}
|