Skip to content

Commit 38d8eb5

Browse files
authored
Merge pull request #58806 from nextcloud/carl/utils-cleanup
Multiple cleanups in OCP/Utils and OC_Utils
2 parents 4ffcb2a + e21b7d1 commit 38d8eb5

15 files changed

Lines changed: 88 additions & 119 deletions

File tree

apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,10 @@ private function getFile(IUser $user, int $fileSource): array {
569569
$file = null;
570570
}
571571
$args = Filesystem::is_dir($file) ? ['dir' => $file] : ['dir' => dirname($file), 'scrollto' => $file];
572-
$link = Util::linkToAbsolute('files', 'index.php', $args);
572+
$urlGenerator = Server::get(IURLGenerator::class);
573+
$link = $urlGenerator->getAbsoluteURL(
574+
$urlGenerator->linkTo('files', 'index.php', $args)
575+
);
573576

574577
return [$file, $link];
575578
}

apps/settings/lib/Settings/Admin/Sharing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getForm() {
5959
'restrictUserEnumerationFullMatchDisplayname' => $this->shareManager->matchDisplayName(),
6060
'restrictUserEnumerationFullMatchEmail' => $this->shareManager->matchEmail(),
6161
'restrictUserEnumerationFullMatchIgnoreSecondDN' => $this->shareManager->ignoreSecondDisplayName(),
62-
'enforceLinksPassword' => Util::isPublicLinkPasswordRequired(false),
62+
'enforceLinksPassword' => $this->shareManager->shareApiLinkEnforcePassword(false),
6363
'enforceLinksPasswordExcludedGroups' => json_decode($excludedPasswordGroups) ?? [],
6464
'enforceLinksPasswordExcludedGroupsEnabled' => $this->config->getSystemValueBool('sharing.allow_disabled_password_enforcement_groups', false),
6565
'onlyShareWithGroupMembers' => $this->shareManager->shareWithGroupMembersOnly(),

apps/updatenotification/lib/Controller/AdminController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use OCP\IL10N;
2121
use OCP\IRequest;
2222
use OCP\Security\ISecureRandom;
23-
use OCP\Util;
23+
use OCP\ServerVersion;
2424
use Psr\Log\LoggerInterface;
2525

2626
class AdminController extends Controller {
@@ -35,16 +35,16 @@ public function __construct(
3535
private ITimeFactory $timeFactory,
3636
private IL10N $l10n,
3737
private LoggerInterface $logger,
38+
private ServerVersion $serverVersion,
3839
) {
3940
parent::__construct($appName, $request);
4041
}
4142

4243
/**
43-
* @param string $channel
44-
* @return DataResponse
44+
* @param 'beta'|'stable'|'enterprise'|'git' $channel
4545
*/
4646
public function setChannel(string $channel): DataResponse {
47-
Util::setChannel($channel);
47+
$this->serverVersion->setChannel($channel);
4848
$this->appConfig->setValueInt('core', 'lastupdatedat', 0);
4949
return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
5050
}

apps/updatenotification/tests/Controller/AdminControllerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\IL10N;
1919
use OCP\IRequest;
2020
use OCP\Security\ISecureRandom;
21+
use OCP\ServerVersion;
2122
use PHPUnit\Framework\MockObject\MockObject;
2223
use Psr\Log\LoggerInterface;
2324
use Test\TestCase;
@@ -56,6 +57,7 @@ protected function setUp(): void {
5657
$this->timeFactory,
5758
$this->l10n,
5859
$this->logger,
60+
$this->createMock(ServerVersion::class),
5961
);
6062
}
6163

lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
1919
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
2020
use OC\AppFramework\Middleware\Security\Exceptions\StrictCookieMissingException;
21+
use OC\Security\CSRF\CsrfTokenManager;
2122
use OC\Settings\AuthorizedGroupMapper;
2223
use OC\User\Session;
2324
use OCA\Talk\Controller\PageController as TalkPageController;
@@ -46,7 +47,7 @@
4647
use OCP\IURLGenerator;
4748
use OCP\IUserSession;
4849
use OCP\Security\Ip\IRemoteAddress;
49-
use OCP\Util;
50+
use OCP\Server;
5051
use Psr\Log\LoggerInterface;
5152
use ReflectionMethod;
5253

@@ -195,7 +196,7 @@ public function beforeController($controller, $methodName) {
195196
}
196197
}
197198
// CSRF check - also registers the CSRF token since the session may be closed later
198-
Util::callRegister();
199+
Server::get(CsrfTokenManager::class)->generateSessionToken();
199200
if ($this->isInvalidCSRFRequired($reflectionMethod)) {
200201
/*
201202
* Only allow the CSRF check to fail on OCS Requests. This kind of

lib/private/Security/CSRF/CsrfTokenManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ public function isTokenValid(CsrfToken $token): bool {
7474
$token->getDecryptedValue()
7575
);
7676
}
77+
78+
public function generateSessionToken(): void {
79+
$this->getToken();
80+
}
7781
}

lib/private/Template/Template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace OC\Template;
1212

1313
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
14+
use OC\Security\CSRF\CsrfTokenManager;
1415
use OC\TemplateLayout;
1516
use OCP\App\AppPathNotFoundException;
1617
use OCP\App\IAppManager;
@@ -40,7 +41,7 @@ public function __construct(
4041
) {
4142
$theme = \OC_Util::getTheme();
4243

43-
$requestToken = ($registerCall ? Util::callRegister() : '');
44+
$requestToken = ($registerCall ? Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue() : '');
4445
$cspNonce = Server::get(ContentSecurityPolicyNonceManager::class)->getNonce();
4546

4647
// fix translation when app is something like core/lostpassword

lib/private/URLGenerator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\IURLGenerator;
2020
use OCP\IUserSession;
2121
use OCP\Server;
22+
use Override;
2223
use RuntimeException;
2324

2425
class URLGenerator implements IURLGenerator {
@@ -326,4 +327,12 @@ public function getBaseUrl(): string {
326327
public function getWebroot(): string {
327328
return \OC::$WEBROOT;
328329
}
330+
331+
#[Override]
332+
public function linkToRemote(string $service): string {
333+
$remoteBase = $this->linkTo('', 'remote.php') . '/' . $service;
334+
return $this->getAbsoluteURL(
335+
$remoteBase . (($service[strlen($service) - 1] !== '/') ? '/' : '')
336+
);
337+
}
329338
}

lib/private/legacy/OC_User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
88
use OC\Authentication\Token\IProvider;
9+
use OC\Security\CSRF\CsrfTokenManager;
910
use OC\SystemConfig;
1011
use OC\User\Database;
1112
use OC\User\DisabledUserException;
@@ -29,7 +30,6 @@
2930
use OCP\User\Events\BeforeUserLoggedInEvent;
3031
use OCP\User\Events\UserLoggedInEvent;
3132
use OCP\UserInterface;
32-
use OCP\Util;
3333
use Psr\Log\LoggerInterface;
3434

3535
/**
@@ -291,7 +291,7 @@ public static function getLogoutUrl(IURLGenerator $urlGenerator): string {
291291
}
292292

293293
$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
294-
$logoutUrl .= '?requesttoken=' . urlencode(Util::callRegister());
294+
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
295295

296296
return $logoutUrl;
297297
}

lib/private/legacy/OC_Util.php

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -525,44 +525,6 @@ public static function checkLoggedIn(): void {
525525
}
526526
}
527527

528-
/**
529-
* Check if the user is a admin, redirects to home if not
530-
*
531-
* @deprecated 32.0.0
532-
*/
533-
public static function checkAdminUser(): void {
534-
self::checkLoggedIn();
535-
if (!OC_User::isAdminUser(OC_User::getUser())) {
536-
header('Location: ' . Util::linkToAbsolute('', 'index.php'));
537-
exit();
538-
}
539-
}
540-
541-
/**
542-
* Returns the URL of the default page
543-
* based on the system configuration and
544-
* the apps visible for the current user
545-
*
546-
* @return string URL
547-
* @deprecated 32.0.0 use IURLGenerator's linkToDefaultPageUrl directly
548-
*/
549-
public static function getDefaultPageUrl() {
550-
/** @var IURLGenerator $urlGenerator */
551-
$urlGenerator = Server::get(IURLGenerator::class);
552-
return $urlGenerator->linkToDefaultPageUrl();
553-
}
554-
555-
/**
556-
* Redirect to the user default page
557-
*
558-
* @deprecated 32.0.0
559-
*/
560-
public static function redirectToDefaultPage(): void {
561-
$location = self::getDefaultPageUrl();
562-
header('Location: ' . $location);
563-
exit();
564-
}
565-
566528
/**
567529
* get an id unique for this instance
568530
*
@@ -578,45 +540,6 @@ public static function getInstanceId(): string {
578540
return $id;
579541
}
580542

581-
/**
582-
* Public function to sanitize HTML
583-
*
584-
* This function is used to sanitize HTML and should be applied on any
585-
* string or array of strings before displaying it on a web page.
586-
*
587-
* @param string|string[] $value
588-
* @return ($value is array ? string[] : string)
589-
* @deprecated 32.0.0 use \OCP\Util::sanitizeHTML instead
590-
*/
591-
public static function sanitizeHTML($value) {
592-
if (is_array($value)) {
593-
$value = array_map(function ($value) {
594-
return self::sanitizeHTML($value);
595-
}, $value);
596-
} else {
597-
// Specify encoding for PHP<5.4
598-
$value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
599-
}
600-
return $value;
601-
}
602-
603-
/**
604-
* Public function to encode url parameters
605-
*
606-
* This function is used to encode path to file before output.
607-
* Encoding is done according to RFC 3986 with one exception:
608-
* Character '/' is preserved as is.
609-
*
610-
* @param string $component part of URI to encode
611-
* @return string
612-
* @deprecated 32.0.0 use \OCP\Util::encodePath instead
613-
*/
614-
public static function encodePath($component) {
615-
$encoded = rawurlencode($component);
616-
$encoded = str_replace('%2F', '/', $encoded);
617-
return $encoded;
618-
}
619-
620543
/**
621544
* Check if current locale is non-UTF8
622545
*

0 commit comments

Comments
 (0)