From fd59e02a24ebd547d30319e4ee09c8c83bc7b7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Apr 2025 15:36:38 +0200 Subject: [PATCH 1/9] chore(legacy): Officially deprecate OC_Util and remove non-static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/legacy/OC_Util.php | 87 ++-------------------------------- 1 file changed, 3 insertions(+), 84 deletions(-) diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index b2950275e41c0..4e6e17732913e 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -9,7 +9,6 @@ use OC\Authentication\TwoFactorAuth\Manager as TwoFactorAuthManager; use OC\Files\SetupManager; use OCP\Files\Template\ITemplateManager; -use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IGroupManager; use OCP\IURLGenerator; @@ -19,13 +18,13 @@ use OCP\Share\IManager; use Psr\Log\LoggerInterface; +/** + * @deprecated 32.0.0 Use \OCP\Util or any appropriate official API instead + */ class OC_Util { public static $styles = []; public static $headers = []; - /** @var array Local cache of version.php */ - private static $versionCache = null; - protected static function getAppManager() { return \OC::$server->getAppManager(); } @@ -692,86 +691,6 @@ public static function encodePath($component) { return $encoded; } - - public function createHtaccessTestFile(\OCP\IConfig $config) { - // php dev server does not support htaccess - if (php_sapi_name() === 'cli-server') { - return false; - } - - // testdata - $fileName = '/htaccesstest.txt'; - $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; - - // creating a test file - $testFile = $config->getSystemValueString('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; - - if (file_exists($testFile)) {// already running this test, possible recursive call - return false; - } - - $fp = @fopen($testFile, 'w'); - if (!$fp) { - throw new \OCP\HintException('Can\'t create test file to check for working .htaccess file.', - 'Make sure it is possible for the web server to write to ' . $testFile); - } - fwrite($fp, $testContent); - fclose($fp); - - return $testContent; - } - - /** - * Check if the .htaccess file is working - * - * @param \OCP\IConfig $config - * @return bool - * @throws Exception - * @throws \OCP\HintException If the test file can't get written. - */ - public function isHtaccessWorking(\OCP\IConfig $config) { - if (\OC::$CLI || !$config->getSystemValueBool('check_for_working_htaccess', true)) { - return true; - } - - $testContent = $this->createHtaccessTestFile($config); - if ($testContent === false) { - return false; - } - - $fileName = '/htaccesstest.txt'; - $testFile = $config->getSystemValueString('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; - - // accessing the file via http - $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName); - try { - $content = \OC::$server->get(IClientService::class)->newClient()->get($url)->getBody(); - } catch (\Exception $e) { - $content = false; - } - - if (str_starts_with($url, 'https:')) { - $url = 'http:' . substr($url, 6); - } else { - $url = 'https:' . substr($url, 5); - } - - try { - $fallbackContent = \OC::$server->get(IClientService::class)->newClient()->get($url)->getBody(); - } catch (\Exception $e) { - $fallbackContent = false; - } - - // cleanup - @unlink($testFile); - - /* - * If the content is not equal to test content our .htaccess - * is working as required - */ - return $content !== $testContent && $fallbackContent !== $testContent; - } - /** * Check if current locale is non-UTF8 * From 09dcc87f051d4b808fac645c399101c2e1cdecab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Apr 2025 15:38:40 +0200 Subject: [PATCH 2/9] chore(legacy): Remove unused protected method in OC_Util MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/legacy/OC_Util.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index 4e6e17732913e..d23b4f1313027 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -25,10 +25,6 @@ class OC_Util { public static $styles = []; public static $headers = []; - protected static function getAppManager() { - return \OC::$server->getAppManager(); - } - /** * Setup the file system * From b20f74a95b16af7d81c53182253ef6b6cba8ecaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Apr 2025 16:07:27 +0200 Subject: [PATCH 3/9] chore(legacy): Flag methods as deprecated, with appropriate replacement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when possible. Signed-off-by: Côme Chilliet --- lib/private/legacy/OC_Util.php | 70 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index d23b4f1313027..f6413d57907ef 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -57,8 +57,8 @@ public static function setupFS(?string $user = '') { * Check if a password is required for each public link * * @param bool $checkGroupMembership Check group membership exclusion - * @return boolean - * @suppress PhanDeprecatedFunction + * @return bool + * @deprecated 32.0.0 use shareApiLinkEnforcePassword directly */ public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { /** @var IManager $shareManager */ @@ -72,6 +72,7 @@ public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = * @param IGroupManager $groupManager * @param IUser|null $user * @return bool + * @deprecated 32.0.0 use sharingDisabledForUser directly */ public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { /** @var IManager $shareManager */ @@ -84,7 +85,7 @@ public static function isSharingDisabledForUser(IConfig $config, IGroupManager $ * check if share API enforces a default expire date * * @return bool - * @suppress PhanDeprecatedFunction + * @deprecated 32.0.0 use shareApiLinkDefaultExpireDateEnforced directly */ public static function isDefaultExpireDateEnforced() { /** @var IManager $shareManager */ @@ -200,12 +201,10 @@ public static function copyr($source, \OCP\Files\Folder $target) { } /** - * @return void - * @suppress PhanUndeclaredMethod + * @deprecated 32.0.0 Call tearDown directly on SetupManager */ - public static function tearDownFS() { - /** @var SetupManager $setupManager */ - $setupManager = \OC::$server->get(SetupManager::class); + public static function tearDownFS(): void { + $setupManager = \OCP\Server::get(SetupManager::class); $setupManager->tearDown(); } @@ -214,10 +213,9 @@ public static function tearDownFS() { * * @param string $application application to get the files from * @param string $directory directory within this application (css, js, vendor, etc) - * @param string $file the file inside of the above folder - * @return string the path + * @param ?string $file the file inside of the above folder */ - private static function generatePath($application, $directory, $file) { + private static function generatePath($application, $directory, $file): string { if (is_null($file)) { $file = $application; $application = ''; @@ -235,9 +233,9 @@ private static function generatePath($application, $directory, $file) { * @param string $application application id * @param string|null $file filename * @param bool $prepend prepend the Style to the beginning of the list - * @return void + * @deprecated 32.0.0 Use \OCP\Util::addStyle */ - public static function addStyle($application, $file = null, $prepend = false) { + public static function addStyle($application, $file = null, $prepend = false): void { $path = OC_Util::generatePath($application, 'css', $file); self::addExternalResource($application, $prepend, $path, 'style'); } @@ -248,9 +246,9 @@ public static function addStyle($application, $file = null, $prepend = false) { * @param string $application application id * @param string|null $file filename * @param bool $prepend prepend the Style to the beginning of the list - * @return void + * @deprecated 32.0.0 */ - public static function addVendorStyle($application, $file = null, $prepend = false) { + public static function addVendorStyle($application, $file = null, $prepend = false): void { $path = OC_Util::generatePath($application, 'vendor', $file); self::addExternalResource($application, $prepend, $path, 'style'); } @@ -262,9 +260,8 @@ public static function addVendorStyle($application, $file = null, $prepend = fal * @param bool $prepend prepend the file to the beginning of the list * @param string $path * @param string $type (script or style) - * @return void */ - private static function addExternalResource($application, $prepend, $path, $type = 'script') { + private static function addExternalResource($application, $prepend, $path, $type = 'script'): void { if ($type === 'style') { if (!in_array($path, self::$styles)) { if ($prepend === true) { @@ -284,8 +281,9 @@ private static function addExternalResource($application, $prepend, $path, $type * @param array $attributes array of attributes for the element * @param string $text the text content for the element * @param bool $prepend prepend the header to the beginning of the list + * @deprecated 32.0.0 Use \OCP\Util::addHeader instead */ - public static function addHeader($tag, $attributes, $text = null, $prepend = false) { + public static function addHeader($tag, $attributes, $text = null, $prepend = false): void { $header = [ 'tag' => $tag, 'attributes' => $attributes, @@ -301,7 +299,6 @@ public static function addHeader($tag, $attributes, $text = null, $prepend = fal /** * check if the current server configuration is suitable for ownCloud * - * @param \OC\SystemConfig $config * @return array arrays with error messages and hints */ public static function checkServer(\OC\SystemConfig $config) { @@ -524,6 +521,7 @@ public static function checkServer(\OC\SystemConfig $config) { * * @param string $dataDirectory * @return array arrays with error messages and hints + * @internal */ public static function checkDataDirectoryPermissions($dataDirectory) { if (!\OC::$server->getConfig()->getSystemValueBool('check_data_directory_permissions', true)) { @@ -552,6 +550,7 @@ public static function checkDataDirectoryPermissions($dataDirectory) { * * @param string $dataDirectory data directory path * @return array errors found + * @internal */ public static function checkDataDirectoryValidity($dataDirectory) { $l = \OC::$server->getL10N('lib'); @@ -576,9 +575,9 @@ public static function checkDataDirectoryValidity($dataDirectory) { * Check if the user is logged in, redirects to home if not. With * redirect URL parameter to the request URI. * - * @return void + * @deprecated 32.0.0 */ - public static function checkLoggedIn() { + public static function checkLoggedIn(): void { // Check if we are a user if (!\OC::$server->getUserSession()->isLoggedIn()) { header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute( @@ -600,10 +599,10 @@ public static function checkLoggedIn() { /** * Check if the user is a admin, redirects to home if not * - * @return void + * @deprecated 32.0.0 */ - public static function checkAdminUser() { - OC_Util::checkLoggedIn(); + public static function checkAdminUser(): void { + self::checkLoggedIn(); if (!OC_User::isAdminUser(OC_User::getUser())) { header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); exit(); @@ -616,7 +615,7 @@ public static function checkAdminUser() { * the apps visible for the current user * * @return string URL - * @suppress PhanDeprecatedFunction + * @deprecated 32.0.0 use linkToDefaultPageUrl directly */ public static function getDefaultPageUrl() { /** @var IURLGenerator $urlGenerator */ @@ -627,9 +626,9 @@ public static function getDefaultPageUrl() { /** * Redirect to the user default page * - * @return void + * @deprecated 32.0.0 */ - public static function redirectToDefaultPage() { + public static function redirectToDefaultPage(): void { $location = self::getDefaultPageUrl(); header('Location: ' . $location); exit(); @@ -640,7 +639,7 @@ public static function redirectToDefaultPage() { * * @return string */ - public static function getInstanceId() { + public static function getInstanceId(): string { $id = \OC::$server->getSystemConfig()->getValue('instanceid', null); if (is_null($id)) { // We need to guarantee at least one letter in instanceid so it can be used as the session_name @@ -658,6 +657,7 @@ public static function getInstanceId() { * * @param string|string[] $value * @return ($value is array ? string[] : string) + * @deprecated 32.0.0 use \OCP\Util::sanitizeHTML instead */ public static function sanitizeHTML($value) { if (is_array($value)) { @@ -680,6 +680,7 @@ public static function sanitizeHTML($value) { * * @param string $component part of URI to encode * @return string + * @deprecated 32.0.0 use \OCP\Util::encodePath instead */ public static function encodePath($component) { $encoded = rawurlencode($component); @@ -706,9 +707,9 @@ private static function isNonUTF8Locale() { * Check if the setlocale call does not work. This can happen if the right * local packages are not available on the server. * - * @return bool + * @internal */ - public static function isSetLocaleWorking() { + public static function isSetLocaleWorking(): bool { if (self::isNonUTF8Locale()) { // Borrowed from \Patchwork\Utf8\Bootup::initLocale setlocale(LC_ALL, 'C.UTF-8', 'C'); @@ -726,9 +727,9 @@ public static function isSetLocaleWorking() { /** * Check if it's possible to get the inline annotations * - * @return bool + * @internal */ - public static function isAnnotationsWorking() { + public static function isAnnotationsWorking(): bool { if (PHP_VERSION_ID >= 80300) { /** @psalm-suppress UndefinedMethod */ $reflection = \ReflectionMethod::createFromMethodName(__METHOD__); @@ -743,9 +744,9 @@ public static function isAnnotationsWorking() { /** * Check if the PHP module fileinfo is loaded. * - * @return bool + * @internal */ - public static function fileInfoLoaded() { + public static function fileInfoLoaded(): bool { return function_exists('finfo_open'); } @@ -815,6 +816,7 @@ public static function normalizeUnicode(string $value): string { * @param \OC\SystemConfig $config * @return bool whether the core or any app needs an upgrade * @throws \OCP\HintException When the upgrade from the given version is not allowed + * @deprecated 32.0.0 Use \OCP\Util::needUpgrade instead */ public static function needUpgrade(\OC\SystemConfig $config) { if ($config->getValue('installed', false)) { From b2100484c00d3ff42a77be8c8e2a0d0371415c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Apr 2025 16:25:35 +0200 Subject: [PATCH 4/9] fix: Remove some call and references to deprecated OC_Util class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/settings/templates/help.php | 2 +- .../BeforeTemplateRenderedListener.php | 2 +- core/js/tests/specs/coreSpec.js | 2 +- lib/base.php | 2 +- lib/private/Image.php | 2 +- lib/private/Share20/Manager.php | 9 ++------- lib/private/Template/functions.php | 5 +++-- lib/public/Share/IManager.php | 3 +-- lib/public/Util.php | 12 ++++++----- tests/lib/UtilTest.php | 20 +++++++++---------- 10 files changed, 28 insertions(+), 31 deletions(-) diff --git a/apps/settings/templates/help.php b/apps/settings/templates/help.php index 1488399679122..17356e6ccdf8a 100644 --- a/apps/settings/templates/help.php +++ b/apps/settings/templates/help.php @@ -3,7 +3,7 @@ * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -\OC_Util::addStyle('settings', 'help'); +\OCP\Util::addStyle('settings', 'help'); ?>