From c419de9d626725ae93a6dd6d4a54f370a174dee8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 11 Aug 2021 14:17:49 +0200 Subject: [PATCH] Add LanguageEditor::validateItemName() --- .../data/language/LanguageEditor.class.php | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php index 5184ef7a12..e11db2e4e5 100644 --- a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php +++ b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php @@ -361,24 +361,7 @@ class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObje foreach ($elements as $element) { $itemName = $element->getAttribute('name'); - // Safeguard against malformed phrases, an empty name has a strange side effect. - if (empty($itemName)) { - throw new \InvalidArgumentException("The name attribute is missing or empty."); - } - - if ($itemName !== $categoryName && \strpos($itemName, $categoryName . '.') !== 0) { - throw new \InvalidArgumentException(WCF::getLanguage()->getDynamicVariable( - 'wcf.acp.language.import.error.categoryMismatch', - [ - 'categoryName' => $categoryName, - 'languageItem' => $itemName, - ] - )); - } - - if (StringUtil::trim($itemName) !== $itemName) { - throw new \InvalidArgumentException("The name '{$itemName}' contains leading or trailing whitespaces."); - } + self::validateItemName($itemName, $categoryName); $itemValue = $element->nodeValue; @@ -610,6 +593,34 @@ class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObje $this->deleteCompiledTemplates(); } + /** + * Verifies that the given variable is a valid variable within the given category. + * Throws an exception otherwise. + * + * @since 5.4 + */ + final public static function validateItemName(string $itemName, string $categoryName): void + { + // Safeguard against malformed phrases, an empty name has a strange side effect. + if (empty($itemName)) { + throw new \InvalidArgumentException("The name attribute is missing or empty."); + } + + if ($itemName !== $categoryName && \strpos($itemName, $categoryName . '.') !== 0) { + throw new \InvalidArgumentException(WCF::getLanguage()->getDynamicVariable( + 'wcf.acp.language.import.error.categoryMismatch', + [ + 'categoryName' => $categoryName, + 'languageItem' => $itemName, + ] + )); + } + + if (StringUtil::trim($itemName) !== $itemName) { + throw new \InvalidArgumentException("The name '{$itemName}' contains leading or trailing whitespaces."); + } + } + /** * Deletes the language cache. * -- 2.20.1