diff options
Diffstat (limited to 'lib/private/OCS')
-rw-r--r-- | lib/private/OCS/ApiHelper.php | 80 | ||||
-rw-r--r-- | lib/private/OCS/CoreCapabilities.php | 23 | ||||
-rw-r--r-- | lib/private/OCS/DiscoveryService.php | 26 | ||||
-rw-r--r-- | lib/private/OCS/Exception.php | 35 | ||||
-rw-r--r-- | lib/private/OCS/Provider.php | 31 | ||||
-rw-r--r-- | lib/private/OCS/Result.php | 160 |
6 files changed, 95 insertions, 260 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/CoreCapabilities.php b/lib/private/OCS/CoreCapabilities.php index 81b6c021a7c..3d988a8662e 100644 --- a/lib/private/OCS/CoreCapabilities.php +++ b/lib/private/OCS/CoreCapabilities.php @@ -1,24 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\OCS; diff --git a/lib/private/OCS/DiscoveryService.php b/lib/private/OCS/DiscoveryService.php index 9bc83d42b1a..85686e59b02 100644 --- a/lib/private/OCS/DiscoveryService.php +++ b/lib/private/OCS/DiscoveryService.php @@ -3,28 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org> - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\OCS; @@ -47,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 a4e80d12dcc..00000000000 --- a/lib/private/OCS/Exception.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -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 b9bcb389c57..402b6b7059d 100644 --- a/lib/private/OCS/Provider.php +++ b/lib/private/OCS/Provider.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\OCS; @@ -35,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 a09d0c89bf2..00000000000 --- a/lib/private/OCS/Result.php +++ /dev/null @@ -1,160 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bart Visscher <bartv@thisnet.nl> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christopher Schäpers <kondou@ts.unde.re> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Tom Needham <tom@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -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; - } -} |