diff options
Diffstat (limited to 'lib/private/OCS')
-rw-r--r-- | lib/private/OCS/ApiHelper.php | 80 | ||||
-rw-r--r-- | lib/private/OCS/DiscoveryService.php | 2 | ||||
-rw-r--r-- | lib/private/OCS/Exception.php | 19 | ||||
-rw-r--r-- | lib/private/OCS/Provider.php | 6 | ||||
-rw-r--r-- | lib/private/OCS/Result.php | 137 |
5 files changed, 85 insertions, 159 deletions
diff --git a/lib/private/OCS/ApiHelper.php b/lib/private/OCS/ApiHelper.php new file mode 100644 index 00000000000..f69b540eafa --- /dev/null +++ b/lib/private/OCS/ApiHelper.php @@ -0,0 +1,80 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace OC\OCS; + +use OC\AppFramework\OCS\V1Response; +use OC\AppFramework\OCS\V2Response; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCSController; +use OCP\IRequest; +use OCP\Server; + +class ApiHelper { + /** + * Respond to a call + * @psalm-taint-escape html + * @param int $overrideHttpStatusCode force the HTTP status code, only used for the special case of maintenance mode which return 503 even for v1 + */ + public static function respond(int $statusCode, string $statusMessage, array $headers = [], ?int $overrideHttpStatusCode = null): void { + $request = Server::get(IRequest::class); + $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); + } + + // 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'); + } +} diff --git a/lib/private/OCS/DiscoveryService.php b/lib/private/OCS/DiscoveryService.php index fc9c1bd4796..85686e59b02 100644 --- a/lib/private/OCS/DiscoveryService.php +++ b/lib/private/OCS/DiscoveryService.php @@ -27,7 +27,7 @@ class DiscoveryService implements IDiscoveryService { * @param IClientService $clientService */ public function __construct(ICacheFactory $cacheFactory, - IClientService $clientService + IClientService $clientService, ) { $this->cache = $cacheFactory->createDistributed('ocs-discovery'); $this->client = $clientService->newClient(); diff --git a/lib/private/OCS/Exception.php b/lib/private/OCS/Exception.php deleted file mode 100644 index eca8ec26df0..00000000000 --- a/lib/private/OCS/Exception.php +++ /dev/null @@ -1,19 +0,0 @@ -<?php -/** - * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. - * SPDX-License-Identifier: AGPL-3.0-only - */ -namespace OC\OCS; - -class Exception extends \Exception { - public function __construct( - private Result $result, - ) { - parent::__construct(); - } - - public function getResult(): Result { - return $this->result; - } -} diff --git a/lib/private/OCS/Provider.php b/lib/private/OCS/Provider.php index 23547da9433..402b6b7059d 100644 --- a/lib/private/OCS/Provider.php +++ b/lib/private/OCS/Provider.php @@ -18,9 +18,11 @@ class Provider extends Controller { * @param IRequest $request * @param IAppManager $appManager */ - public function __construct($appName, + public function __construct( + $appName, \OCP\IRequest $request, - private \OCP\App\IAppManager $appManager) { + private \OCP\App\IAppManager $appManager, + ) { parent::__construct($appName, $request); } diff --git a/lib/private/OCS/Result.php b/lib/private/OCS/Result.php deleted file mode 100644 index 5460a8b275c..00000000000 --- a/lib/private/OCS/Result.php +++ /dev/null @@ -1,137 +0,0 @@ -<?php - -/** - * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. - * SPDX-License-Identifier: AGPL-3.0-only - */ -namespace OC\OCS; - -class Result { - protected array $data; - - /** @var null|string */ - protected ?string $message; - - /** @var int */ - protected int $statusCode; - - /** @var integer */ - protected $items; - - /** @var integer */ - protected $perPage; - - /** @var array */ - private array $headers = []; - - /** - * create the OCS_Result object - * - * @param mixed|null $data the data to return - * @param int $code - * @param string|null $message - * @param array $headers - */ - public function __construct(mixed $data = null, int $code = 100, ?string $message = null, array $headers = []) { - if ($data === null) { - $this->data = []; - } elseif (!is_array($data)) { - $this->data = [$this->data]; - } else { - $this->data = $data; - } - $this->statusCode = $code; - $this->message = $message; - $this->headers = $headers; - } - - /** - * optionally set the total number of items available - * - * @param int $items - */ - public function setTotalItems(int $items): void { - $this->items = $items; - } - - /** - * optionally set the number of items per page - * - * @param int $items - */ - public function setItemsPerPage(int $items): void { - $this->perPage = $items; - } - - /** - * get the status code - * @return int - */ - public function getStatusCode(): int { - return $this->statusCode; - } - - /** - * get the meta data for the result - * @return array - */ - public function getMeta(): array { - $meta = []; - $meta['status'] = $this->succeeded() ? 'ok' : 'failure'; - $meta['statuscode'] = $this->statusCode; - $meta['message'] = $this->message; - if ($this->items !== null) { - $meta['totalitems'] = $this->items; - } - if ($this->perPage !== null) { - $meta['itemsperpage'] = $this->perPage; - } - return $meta; - } - - /** - * get the result data - * @return array - */ - public function getData(): array { - return $this->data; - } - - /** - * return bool Whether the method succeeded - * @return bool - */ - public function succeeded(): bool { - return ($this->statusCode == 100); - } - - /** - * Adds a new header to the response - * - * @param string $name The name of the HTTP header - * @param string $value The value, null will delete it - * @return $this - */ - public function addHeader(string $name, ?string $value): static { - $name = trim($name); // always remove leading and trailing whitespace - // to be able to reliably check for security - // headers - - if (is_null($value)) { - unset($this->headers[$name]); - } else { - $this->headers[$name] = $value; - } - - return $this; - } - - /** - * Returns the set headers - * @return array the headers - */ - public function getHeaders(): array { - return $this->headers; - } -} |