Merge pull request #47685 from nextcloud/fix/move-apihelper-to-oc-namespace
fix: Move OC_API into \OC\ApiHelper in standard namespacepull/47924/head
commit
b6c286640f
@ -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');
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -1,162 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
use OCP\API;
|
||||
use OCP\AppFramework\Http;
|
||||
|
||||
class OC_API {
|
||||
/**
|
||||
* api actions
|
||||
*/
|
||||
protected static $actions = [];
|
||||
|
||||
/**
|
||||
* respond to a call
|
||||
* @param \OC\OCS\Result $result
|
||||
* @param string $format the format xml|json
|
||||
* @psalm-taint-escape html
|
||||
*/
|
||||
public static function respond($result, $format = 'xml') {
|
||||
$request = \OC::$server->getRequest();
|
||||
|
||||
// Send 401 headers if unauthorised
|
||||
if ($result->getStatusCode() === \OCP\AppFramework\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 ($result->getHeaders() as $name => $value) {
|
||||
header($name . ': ' . $value);
|
||||
}
|
||||
|
||||
$meta = $result->getMeta();
|
||||
$data = $result->getData();
|
||||
if (self::isV2($request)) {
|
||||
$statusCode = self::mapStatusCodes($result->getStatusCode());
|
||||
if (!is_null($statusCode)) {
|
||||
$meta['statuscode'] = $statusCode;
|
||||
http_response_code($statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
self::setContentType($format);
|
||||
$body = self::renderResult($format, $meta, $data);
|
||||
echo $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param XMLWriter $writer
|
||||
*/
|
||||
private static function toXML($array, $writer) {
|
||||
foreach ($array as $k => $v) {
|
||||
if ($k[0] === '@') {
|
||||
$writer->writeAttribute(substr($k, 1), $v);
|
||||
continue;
|
||||
} elseif (is_numeric($k)) {
|
||||
$k = 'element';
|
||||
}
|
||||
if (is_array($v)) {
|
||||
$writer->startElement($k);
|
||||
self::toXML($v, $writer);
|
||||
$writer->endElement();
|
||||
} else {
|
||||
$writer->writeElement($k, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function requestedFormat(): string {
|
||||
$formats = ['json', 'xml'];
|
||||
|
||||
$format = (isset($_GET['format']) && is_string($_GET['format']) && in_array($_GET['format'], $formats)) ? $_GET['format'] : 'xml';
|
||||
return $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on the requested format the response content type is set
|
||||
* @param string $format
|
||||
*/
|
||||
public static function setContentType($format = null) {
|
||||
$format = is_null($format) ? self::requestedFormat() : $format;
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \OCP\IRequest $request
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isV2(\OCP\IRequest $request) {
|
||||
$script = $request->getScriptName();
|
||||
|
||||
return str_ends_with($script, '/ocs/v2.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $sc
|
||||
* @return int
|
||||
*/
|
||||
public static function mapStatusCodes($sc) {
|
||||
switch ($sc) {
|
||||
case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND:
|
||||
return Http::STATUS_NOT_FOUND;
|
||||
case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR:
|
||||
return Http::STATUS_INTERNAL_SERVER_ERROR;
|
||||
case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR:
|
||||
return Http::STATUS_INTERNAL_SERVER_ERROR;
|
||||
case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED:
|
||||
// already handled for v1
|
||||
return null;
|
||||
case 100:
|
||||
return Http::STATUS_OK;
|
||||
}
|
||||
// any 2xx, 4xx and 5xx will be used as is
|
||||
if ($sc >= 200 && $sc < 600) {
|
||||
return $sc;
|
||||
}
|
||||
|
||||
return Http::STATUS_BAD_REQUEST;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
public static function renderResult($format, $meta, $data) {
|
||||
$response = [
|
||||
'ocs' => [
|
||||
'meta' => $meta,
|
||||
'data' => $data,
|
||||
],
|
||||
];
|
||||
if ($format == 'json') {
|
||||
return json_encode($response, JSON_HEX_TAG);
|
||||
}
|
||||
|
||||
$writer = new XMLWriter();
|
||||
$writer->openMemory();
|
||||
$writer->setIndent(true);
|
||||
$writer->startDocument();
|
||||
self::toXML($response, $writer);
|
||||
$writer->endDocument();
|
||||
return $writer->outputMemory(true);
|
||||
}
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace Test;
|
||||
|
||||
use OCP\IRequest;
|
||||
|
||||
class APITest extends \Test\TestCase {
|
||||
// Helps build a response variable
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function buildResponse($shipped, $data, $code, $message = null) {
|
||||
$resp = new \OC\OCS\Result($data, $code, $message);
|
||||
$resp->addHeader('KEY', 'VALUE');
|
||||
return [
|
||||
'shipped' => $shipped,
|
||||
'response' => $resp,
|
||||
'app' => $this->getUniqueID('testapp_'),
|
||||
];
|
||||
}
|
||||
|
||||
// Validate details of the result
|
||||
|
||||
/**
|
||||
* @param \OC\OCS\Result $result
|
||||
*/
|
||||
public function checkResult($result, $success) {
|
||||
// Check response is of correct type
|
||||
$this->assertInstanceOf(\OC\OCS\Result::class, $result);
|
||||
// Check if it succeeded
|
||||
/** @var \OC\OCS\Result $result */
|
||||
$this->assertEquals($success, $result->succeeded());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function versionDataScriptNameProvider() {
|
||||
return [
|
||||
// Valid script name
|
||||
[
|
||||
'/master/ocs/v2.php',
|
||||
true,
|
||||
],
|
||||
|
||||
// Invalid script names
|
||||
[
|
||||
'/master/ocs/v2.php/someInvalidPathName',
|
||||
false,
|
||||
],
|
||||
[
|
||||
'/master/ocs/v1.php',
|
||||
false,
|
||||
],
|
||||
[
|
||||
'',
|
||||
false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider versionDataScriptNameProvider
|
||||
* @param string $scriptName
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testIsV2($scriptName, $expected) {
|
||||
$request = $this->getMockBuilder(IRequest::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$request
|
||||
->expects($this->once())
|
||||
->method('getScriptName')
|
||||
->willReturn($scriptName);
|
||||
|
||||
$this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace Test\AppFramework\OCS;
|
||||
|
||||
use OC\AppFramework\OCS\V2Response;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\OCSController;
|
||||
|
||||
class V2ResponseTest extends \Test\TestCase {
|
||||
/**
|
||||
* @dataProvider providesStatusCodes
|
||||
*/
|
||||
public function testStatusCodeMapper(int $expected, int $sc): void {
|
||||
$response = new V2Response(new DataResponse([], $sc));
|
||||
$this->assertEquals($expected, $response->getStatus());
|
||||
}
|
||||
|
||||
public function providesStatusCodes(): array {
|
||||
return [
|
||||
[Http::STATUS_OK, 200],
|
||||
[Http::STATUS_BAD_REQUEST, 104],
|
||||
[Http::STATUS_BAD_REQUEST, 1000],
|
||||
[201, 201],
|
||||
[Http::STATUS_UNAUTHORIZED, OCSController::RESPOND_UNAUTHORISED],
|
||||
[Http::STATUS_INTERNAL_SERVER_ERROR, OCSController::RESPOND_SERVER_ERROR],
|
||||
[Http::STATUS_NOT_FOUND, OCSController::RESPOND_NOT_FOUND],
|
||||
[Http::STATUS_INTERNAL_SERVER_ERROR, OCSController::RESPOND_UNKNOWN_ERROR],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
<?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-or-later
|
||||
*/
|
||||
|
||||
namespace Test\OCS;
|
||||
|
||||
use OC\OCS\ApiHelper;
|
||||
use OCP\IRequest;
|
||||
|
||||
class ApiHelperTest extends \Test\TestCase {
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function versionDataScriptNameProvider(): array {
|
||||
return [
|
||||
// Valid script name
|
||||
[
|
||||
'/master/ocs/v2.php', true,
|
||||
],
|
||||
|
||||
// Invalid script names
|
||||
[
|
||||
'/master/ocs/v2.php/someInvalidPathName', false,
|
||||
],
|
||||
[
|
||||
'/master/ocs/v1.php', false,
|
||||
],
|
||||
[
|
||||
'', false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider versionDataScriptNameProvider
|
||||
*/
|
||||
public function testIsV2(string $scriptName, bool $expected): void {
|
||||
$request = $this->getMockBuilder(IRequest::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$request
|
||||
->expects($this->once())
|
||||
->method('getScriptName')
|
||||
->willReturn($scriptName);
|
||||
|
||||
$this->assertEquals($expected, $this->invokePrivate(new ApiHelper, 'isV2', [$request]));
|
||||
}
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
namespace Test\OCS;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
|
||||
class MapStatusCodeTest extends \Test\TestCase {
|
||||
/**
|
||||
* @dataProvider providesStatusCodes
|
||||
*/
|
||||
public function testStatusCodeMapper($expected, $sc) {
|
||||
$result = \OC_API::mapStatusCodes($sc);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function providesStatusCodes() {
|
||||
return [
|
||||
[Http::STATUS_OK, 100],
|
||||
[Http::STATUS_BAD_REQUEST, 104],
|
||||
[Http::STATUS_BAD_REQUEST, 1000],
|
||||
[201, 201],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue