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 | 55 | ||||
-rw-r--r-- | lib/private/OCS/DiscoveryService.php | 34 | ||||
-rw-r--r-- | lib/private/OCS/Exception.php | 39 | ||||
-rw-r--r-- | lib/private/OCS/Provider.php | 53 | ||||
-rw-r--r-- | lib/private/OCS/Result.php | 159 |
6 files changed, 129 insertions, 291 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 81c7bf93da5..3d988a8662e 100644 --- a/lib/private/OCS/CoreCapabilities.php +++ b/lib/private/OCS/CoreCapabilities.php @@ -1,30 +1,15 @@ <?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; use OCP\Capabilities\ICapability; use OCP\IConfig; +use OCP\IURLGenerator; /** * Class Capabilities @@ -32,28 +17,36 @@ use OCP\IConfig; * @package OC\OCS */ class CoreCapabilities implements ICapability { - - /** @var IConfig */ - private $config; - /** * @param IConfig $config */ - public function __construct(IConfig $config) { - $this->config = $config; + public function __construct( + private IConfig $config, + ) { } /** * Return this classes capabilities * - * @return array + * @return array{ + * core: array{ + * pollinterval: int, + * webdav-root: string, + * reference-api: boolean, + * reference-regex: string, + * mod-rewrite-working: boolean, + * }, + * } */ - public function getCapabilities() { + public function getCapabilities(): array { return [ 'core' => [ - 'pollinterval' => $this->config->getSystemValue('pollinterval', 60), - 'webdav-root' => $this->config->getSystemValue('webdav-root', 'remote.php/webdav'), - ] + 'pollinterval' => $this->config->getSystemValueInt('pollinterval', 60), + 'webdav-root' => $this->config->getSystemValueString('webdav-root', 'remote.php/webdav'), + 'reference-api' => true, + 'reference-regex' => IURLGenerator::URL_REGEX_NO_MODIFIERS, + 'mod-rewrite-working' => $this->config->getSystemValueBool('htaccess.IgnoreFrontController') || getenv('front_controller_active') === 'true', + ], ]; } } diff --git a/lib/private/OCS/DiscoveryService.php b/lib/private/OCS/DiscoveryService.php index 0a28b09fda1..85686e59b02 100644 --- a/lib/private/OCS/DiscoveryService.php +++ b/lib/private/OCS/DiscoveryService.php @@ -3,30 +3,9 @@ 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; use OCP\AppFramework\Http; @@ -37,19 +16,18 @@ use OCP\ICacheFactory; use OCP\OCS\IDiscoveryService; class DiscoveryService implements IDiscoveryService { - /** @var ICache */ - private $cache; + private ICache $cache; /** @var IClient */ - private $client; + private IClient $client; /** * @param ICacheFactory $cacheFactory * @param IClientService $clientService */ public function __construct(ICacheFactory $cacheFactory, - IClientService $clientService + IClientService $clientService, ) { $this->cache = $cacheFactory->createDistributed('ocs-discovery'); $this->client = $clientService->newClient(); @@ -63,7 +41,7 @@ class DiscoveryService implements IDiscoveryService { * * @param string $remote * @param string $service the service you want to discover - * @param bool $skipCache We won't check if the data is in the cache. This is usefull if a background job is updating the status + * @param bool $skipCache We won't check if the data is in the cache. This is useful if a background job is updating the status * @return array */ public function discover(string $remote, string $service, bool $skipCache = false): array { diff --git a/lib/private/OCS/Exception.php b/lib/private/OCS/Exception.php deleted file mode 100644 index 63a7bdb54ea..00000000000 --- a/lib/private/OCS/Exception.php +++ /dev/null @@ -1,39 +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 { - - /** @var Result */ - private $result; - - public function __construct(Result $result) { - parent::__construct(); - $this->result = $result; - } - - public function getResult() { - return $this->result; - } -} diff --git a/lib/private/OCS/Provider.php b/lib/private/OCS/Provider.php index 68100c64819..402b6b7059d 100644 --- a/lib/private/OCS/Provider.php +++ b/lib/private/OCS/Provider.php @@ -1,50 +1,35 @@ <?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; -class Provider extends \OCP\AppFramework\Controller { - /** @var \OCP\App\IAppManager */ - private $appManager; +use OCP\App\IAppManager; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IRequest; +class Provider extends Controller { /** * @param string $appName - * @param \OCP\IRequest $request - * @param \OCP\App\IAppManager $appManager + * @param IRequest $request + * @param IAppManager $appManager */ - public function __construct($appName, - \OCP\IRequest $request, - \OCP\App\IAppManager $appManager) { + public function __construct( + $appName, + \OCP\IRequest $request, + private \OCP\App\IAppManager $appManager, + ) { parent::__construct($appName, $request); - $this->appManager = $appManager; } /** - * @return \OCP\AppFramework\Http\JSONResponse + * @return JSONResponse */ - public function buildProviderList() { + public function buildProviderList(): JSONResponse { $services = [ 'PRIVATE_DATA' => [ 'version' => 1, @@ -109,7 +94,7 @@ class Provider extends \OCP\AppFramework\Controller { ]; } - return new \OCP\AppFramework\Http\JSONResponse([ + return new JSONResponse([ 'version' => 2, 'services' => $services, ]); diff --git a/lib/private/OCS/Result.php b/lib/private/OCS/Result.php deleted file mode 100644 index be2e6259309..00000000000 --- a/lib/private/OCS/Result.php +++ /dev/null @@ -1,159 +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 { - - /** @var array */ - protected $data; - - /** @var null|string */ - protected $message; - - /** @var int */ - protected $statusCode; - - /** @var integer */ - protected $items; - - /** @var integer */ - protected $perPage; - - /** @var array */ - private $headers = []; - - /** - * create the OCS_Result object - * @param mixed $data the data to return - * @param int $code - * @param null|string $message - * @param array $headers - */ - public function __construct($data = null, $code = 100, $message = null, $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($items) { - $this->items = $items; - } - - /** - * optionally set the the number of items per page - * @param int $items - */ - public function setItemsPerPage($items) { - $this->perPage = $items; - } - - /** - * get the status code - * @return int - */ - public function getStatusCode() { - return $this->statusCode; - } - - /** - * get the meta data for the result - * @return array - */ - public function getMeta() { - $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() { - return $this->data; - } - - /** - * return bool Whether the method succeeded - * @return bool - */ - public function succeeded() { - 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($name, $value) { - $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() { - return $this->headers; - } -} |