Skip to content

Commit

Permalink
Merge pull request #47685 from nextcloud/fix/move-apihelper-to-oc-nam…
Browse files Browse the repository at this point in the history
…espace

fix: Move OC_API into \OC\ApiHelper in standard namespace
  • Loading branch information
come-nc authored Sep 12, 2024
2 parents a2ac1d8 + 90a9485 commit b6c2866
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 460 deletions.
4 changes: 1 addition & 3 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1714,11 +1714,10 @@
'OC\\OCM\\Model\\OCMProvider' => $baseDir . '/lib/private/OCM/Model/OCMProvider.php',
'OC\\OCM\\Model\\OCMResource' => $baseDir . '/lib/private/OCM/Model/OCMResource.php',
'OC\\OCM\\OCMDiscoveryService' => $baseDir . '/lib/private/OCM/OCMDiscoveryService.php',
'OC\\OCS\\ApiHelper' => $baseDir . '/lib/private/OCS/ApiHelper.php',
'OC\\OCS\\CoreCapabilities' => $baseDir . '/lib/private/OCS/CoreCapabilities.php',
'OC\\OCS\\DiscoveryService' => $baseDir . '/lib/private/OCS/DiscoveryService.php',
'OC\\OCS\\Exception' => $baseDir . '/lib/private/OCS/Exception.php',
'OC\\OCS\\Provider' => $baseDir . '/lib/private/OCS/Provider.php',
'OC\\OCS\\Result' => $baseDir . '/lib/private/OCS/Result.php',
'OC\\PhoneNumberUtil' => $baseDir . '/lib/private/PhoneNumberUtil.php',
'OC\\PreviewManager' => $baseDir . '/lib/private/PreviewManager.php',
'OC\\PreviewNotAvailableException' => $baseDir . '/lib/private/PreviewNotAvailableException.php',
Expand Down Expand Up @@ -2000,7 +1999,6 @@
'OC\\User\\OutOfOfficeData' => $baseDir . '/lib/private/User/OutOfOfficeData.php',
'OC\\User\\Session' => $baseDir . '/lib/private/User/Session.php',
'OC\\User\\User' => $baseDir . '/lib/private/User/User.php',
'OC_API' => $baseDir . '/lib/private/legacy/OC_API.php',
'OC_App' => $baseDir . '/lib/private/legacy/OC_App.php',
'OC_Defaults' => $baseDir . '/lib/private/legacy/OC_Defaults.php',
'OC_Files' => $baseDir . '/lib/private/legacy/OC_Files.php',
Expand Down
4 changes: 1 addition & 3 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1747,11 +1747,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\OCM\\Model\\OCMProvider' => __DIR__ . '/../../..' . '/lib/private/OCM/Model/OCMProvider.php',
'OC\\OCM\\Model\\OCMResource' => __DIR__ . '/../../..' . '/lib/private/OCM/Model/OCMResource.php',
'OC\\OCM\\OCMDiscoveryService' => __DIR__ . '/../../..' . '/lib/private/OCM/OCMDiscoveryService.php',
'OC\\OCS\\ApiHelper' => __DIR__ . '/../../..' . '/lib/private/OCS/ApiHelper.php',
'OC\\OCS\\CoreCapabilities' => __DIR__ . '/../../..' . '/lib/private/OCS/CoreCapabilities.php',
'OC\\OCS\\DiscoveryService' => __DIR__ . '/../../..' . '/lib/private/OCS/DiscoveryService.php',
'OC\\OCS\\Exception' => __DIR__ . '/../../..' . '/lib/private/OCS/Exception.php',
'OC\\OCS\\Provider' => __DIR__ . '/../../..' . '/lib/private/OCS/Provider.php',
'OC\\OCS\\Result' => __DIR__ . '/../../..' . '/lib/private/OCS/Result.php',
'OC\\PhoneNumberUtil' => __DIR__ . '/../../..' . '/lib/private/PhoneNumberUtil.php',
'OC\\PreviewManager' => __DIR__ . '/../../..' . '/lib/private/PreviewManager.php',
'OC\\PreviewNotAvailableException' => __DIR__ . '/../../..' . '/lib/private/PreviewNotAvailableException.php',
Expand Down Expand Up @@ -2033,7 +2032,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\User\\OutOfOfficeData' => __DIR__ . '/../../..' . '/lib/private/User/OutOfOfficeData.php',
'OC\\User\\Session' => __DIR__ . '/../../..' . '/lib/private/User/Session.php',
'OC\\User\\User' => __DIR__ . '/../../..' . '/lib/private/User/User.php',
'OC_API' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_API.php',
'OC_App' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_App.php',
'OC_Defaults' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_Defaults.php',
'OC_Files' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_Files.php',
Expand Down
80 changes: 80 additions & 0 deletions lib/private/OCS/ApiHelper.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
19 changes: 0 additions & 19 deletions lib/private/OCS/Exception.php

This file was deleted.

137 changes: 0 additions & 137 deletions lib/private/OCS/Result.php

This file was deleted.

Loading

0 comments on commit b6c2866

Please sign in to comment.