diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2024-05-30 14:19:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-30 14:19:51 +0200 |
commit | 99af78cd66cb489b52f985e9a5b7d165b60b0e82 (patch) | |
tree | e0ea396f5eefcd8a5a4b6f7ec35769d42cb32a7f /lib/public/Util.php | |
parent | f80282a5ed1891224e3fa71c69ca2d60412c4c63 (diff) | |
parent | 31b0a44cf65b6625636ea0fa15fb1a1122b525e1 (diff) | |
download | nextcloud-server-99af78cd66cb489b52f985e9a5b7d165b60b0e82.tar.gz nextcloud-server-99af78cd66cb489b52f985e9a5b7d165b60b0e82.zip |
Merge branch 'master' into refactor/OC-Server-getL10NFactory
Signed-off-by: John Molakvoæ <skjnldsv@users.noreply.github.com>
Diffstat (limited to 'lib/public/Util.php')
-rw-r--r-- | lib/public/Util.php | 151 |
1 files changed, 74 insertions, 77 deletions
diff --git a/lib/public/Util.php b/lib/public/Util.php index d6642daf864..7e9ddb6788b 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -1,56 +1,22 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bart Visscher <bartv@thisnet.nl> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Frank Karlitschek <frank@karlitschek.de> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Individual IT Services <info@individual-it.net> - * @author J0WI <J0WI@users.noreply.github.com> - * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch> - * @author Joas Schilling <coding@schilljs.com> - * @author Jonas Meurer <jonas@freesources.org> - * @author Julius Härtl <jus@bitgrid.net> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Pellaeon Lin <nfsmwlin@gmail.com> - * @author Randolph Carter <RandolphCarter@fantasymail.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Victor Dubiniuk <dubiniuk@owncloud.com> - * @author Vincent Petry <vincent@nextcloud.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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ // use OCP namespace for all classes that are considered public. -// This means that they should be used by apps instead of the internal ownCloud classes +// This means that they should be used by apps instead of the internal Nextcloud classes namespace OCP; +use bantu\IniGetWrapper\IniGetWrapper; use OC\AppScriptDependency; use OC\AppScriptSort; use OCP\L10N\IFactory; -use bantu\IniGetWrapper\IniGetWrapper; +use OCP\Share\IManager; use Psr\Container\ContainerExceptionInterface; +use Psr\Log\LoggerInterface /** * This class provides different helper functions to make the life of a developer easier @@ -58,17 +24,11 @@ use Psr\Container\ContainerExceptionInterface; * @since 4.0.0 */ class Util { - /** @var \OCP\Share\IManager */ - private static $shareManager; - - /** @var array */ - private static $scripts = []; - - /** @var array */ - private static $scriptDeps = []; + private static ?IManager $shareManager = null; - /** @var array */ - private static $sortedScriptDeps = []; + private static array $scriptsInit = []; + private static array $scripts = []; + private static array $scriptDeps = []; /** * get the current installed version of Nextcloud @@ -111,28 +71,15 @@ class Util { } /** - * write a message in the log - * @param string $app - * @param string $message - * @param int $level - * @since 4.0.0 - * @deprecated 13.0.0 use log of \OCP\ILogger - */ - public static function writeLog($app, $message, $level) { - $context = ['app' => $app]; - \OC::$server->getLogger()->log($level, $message, $context); - } - - /** * check if sharing is disabled for the current user * * @return boolean * @since 7.0.0 - * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser + * @deprecated 9.1.0 Use \OC::$server->get(\OCP\Share\IManager::class)->sharingDisabledForUser */ public static function isSharingDisabledForUser() { if (self::$shareManager === null) { - self::$shareManager = \OC::$server->getShareManager(); + self::$shareManager = \OC::$server->get(IManager::class); } $user = \OC::$server->getUserSession()->getUser(); @@ -145,13 +92,10 @@ class Util { /** * get l10n object - * @param string $application - * @param string|null $language - * @return \OCP\IL10N * @since 6.0.0 - parameter $language was added in 8.0.0 */ - public static function getL10N($application, $language = null) { - return \OC::$server->getL10N($application, $language); + public static function getL10N(string $application, ?string $language = null): IL10N { + return Server::get(\OCP\L10N\IFactory::class)->get($application, $language); } /** @@ -165,6 +109,31 @@ class Util { } /** + * Add a standalone init js file that is loaded for initialization + * + * Be careful loading scripts using this method as they are loaded early + * and block the initial page rendering. They should not have dependencies + * on any other scripts than core-common and core-main. + * + * @since 28.0.0 + */ + public static function addInitScript(string $application, string $file): void { + if (!empty($application)) { + $path = "$application/js/$file"; + } else { + $path = "js/$file"; + } + + // We need to handle the translation BEFORE the init script + // is loaded, as the init script might use translations + if ($application !== 'core' && !str_contains($file, 'l10n')) { + self::addTranslations($application, null, true); + } + + self::$scriptsInit[] = $path; + } + + /** * add a javascript file * * @param string $application @@ -173,7 +142,7 @@ class Util { * @param bool $prepend * @since 4.0.0 */ - public static function addScript(string $application, string $file = null, string $afterAppId = 'core', bool $prepend = false): void { + public static function addScript(string $application, ?string $file = null, string $afterAppId = 'core', bool $prepend = false): void { if (!empty($application)) { $path = "$application/js/$file"; } else { @@ -215,7 +184,8 @@ class Util { $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); // Flatten array and remove duplicates - $sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : []; + $sortedScripts = array_merge([self::$scriptsInit], $sortedScripts); + $sortedScripts = array_merge(...array_values($sortedScripts)); // Override core-common and core-main order if (in_array('core/js/main', $sortedScripts)) { @@ -232,9 +202,10 @@ class Util { * Add a translation JS file * @param string $application application id * @param string $languageCode language code, defaults to the current locale + * @param bool $init whether the translations should be loaded early or not * @since 8.0.0 */ - public static function addTranslations($application, $languageCode = null) { + public static function addTranslations($application, $languageCode = null, $init = false) { if (is_null($languageCode)) { $languageCode = \OC::$server->get(IFactory::class)->findLanguage($application); } @@ -243,7 +214,12 @@ class Util { } else { $path = "l10n/$languageCode"; } - self::$scripts[$application][] = $path; + + if ($init) { + self::$scriptsInit[] = $path; + } else { + self::$scripts[$application][] = $path; + } } /** @@ -511,10 +487,31 @@ class Util { } /** + * Get a list of characters forbidden in file names + * @return string[] + * @since 29.0.0 + */ + public static function getForbiddenFileNameChars(): array { + // Get always forbidden characters + $invalidChars = str_split(\OCP\Constants::FILENAME_INVALID_CHARS); + if ($invalidChars === false) { + $invalidChars = []; + } + + // Get admin defined invalid characters + $additionalChars = \OCP\Server::get(IConfig::class)->getSystemValue('forbidden_chars', []); + if (!is_array($additionalChars)) { + \OCP\Server::get(LoggerInterface::class)->error('Invalid system config value for "forbidden_chars" is ignored.'); + $additionalChars = []; + } + return array_merge($invalidChars, $additionalChars); + } + + /** * Returns whether the given file name is valid * @param string $file file name to check * @return bool true if the file name is valid, false otherwise - * @deprecated 8.1.0 use \OC\Files\View::verifyPath() + * @deprecated 8.1.0 use OCP\Files\Storage\IStorage::verifyPath() * @since 7.0.0 * @suppress PhanDeprecatedFunction */ |