Skip to content

EspoCRM – Open Source CRM Application

Language manager validation

Yurii31540a04ca8e
diff --git a/application/Espo/Controllers/LabelManager.php b/application/Espo/Controllers/LabelManager.php
index 0378edf185..fd7020be77 100644
--- a/application/Espo/Controllers/LabelManager.php
+++ b/application/Espo/Controllers/LabelManager.php
@@ -32,15 +32,18 @@ namespace Espo\Controllers;
use Espo\Core\Api\Request;
use Espo\Core\DataManager;
use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Entities\User;
use Espo\Tools\LabelManager\LabelManager as LabelManagerTool;
use stdClass;
+/**
+ * @noinspection PhpUnused
+ */
class LabelManager
{
-
/**
* @throws Forbidden
*/
@@ -62,28 +65,57 @@ class LabelManager
return $this->labelManagerTool->getScopeList();
}
+ /**
+ * @throws BadRequest
+ */
public function postActionGetScopeData(Request $request): stdClass
{
$data = $request->getParsedBody();
- if (empty($data->scope) || empty($data->language)) {
+ $language = $data->language ?? null;
+ $scope = $data->scope ?? null;
+
+ if (!$scope || !$language) {
+ throw new BadRequest();
+ }
+
+ if (!is_string($scope) || !is_string($language)) {
+ throw new BadRequest();
+ }
+
+ if (basename($scope) !== $scope || basename($language) !== $language) {
throw new BadRequest();
}
- return $this->labelManagerTool->getScopeData($data->language, $data->scope);
+ return $this->labelManagerTool->getScopeData($language, $scope);
}
+ /**
+ * @throws BadRequest
+ * @throws Error
+ */
public function postActionSaveLabels(Request $request): stdClass
{
$data = $request->getParsedBody();
- if (empty($data->scope) || empty($data->language) || !isset($data->labels)) {
+ $language = $data->language ?? null;
+ $scope = $data->scope ?? null;
+
+ if (!$scope || !$language || !isset($data->labels)) {
+ throw new BadRequest();
+ }
+
+ if (!is_string($scope) || !is_string($language)) {
+ throw new BadRequest();
+ }
+
+ if (basename($scope) !== $scope || basename($language) !== $language) {
throw new BadRequest();
}
$labels = get_object_vars($data->labels);
- $returnData = $this->labelManagerTool->saveLabels($data->language, $data->scope, $labels);
+ $returnData = $this->labelManagerTool->saveLabels($language, $scope, $labels);
$this->dataManager->clearCache();
diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php
index 27a7b0ca4e..17752e8ff5 100644
--- a/application/Espo/Core/Utils/Language.php
+++ b/application/Espo/Core/Utils/Language.php
@@ -65,7 +65,6 @@ class Language
private bool $noFallback = false,
) {
$this->currentLanguage = $language ?? $this->defaultLanguage;
-
}
public function getLanguage(): string
@@ -99,7 +98,9 @@ class Language
private function getCacheKey(?string $language = null): string
{
- return 'languages/' . ($language ?? $this->currentLanguage);
+ $language ??= $this->currentLanguage;
+
+ return 'languages/' . $language;
}
/**
@@ -226,7 +227,10 @@ class Language
*/
public function getScopeCustom(string $scope): ?stdClass
{
- $path = str_replace('{language}', $this->currentLanguage, $this->customPath) . "/$scope.json";
+ $language = basename($this->currentLanguage);
+ $scope = basename($scope);
+
+ $path = str_replace('{language}', $language, $this->customPath) . "/$scope.json";
if (!$this->fileManager->isFile($path)) {
return null;
@@ -242,7 +246,10 @@ class Language
*/
public function saveScopeCustom(string $scope, stdClass $data): void
{
- $path = str_replace('{language}', $this->currentLanguage, $this->customPath) . "/$scope.json";
+ $language = basename($this->currentLanguage);
+ $scope = basename($scope);
+
+ $path = str_replace('{language}', $language, $this->customPath) . "/$scope.json";
foreach (get_object_vars($data) as $key => $item) {
if (
@@ -265,7 +272,9 @@ class Language
*/
public function save(): bool
{
- $path = str_replace('{language}', $this->currentLanguage, $this->customPath);
+ $language = basename($this->currentLanguage);
+
+ $path = str_replace('{language}', $language, $this->customPath);
$result = true;
@@ -275,6 +284,8 @@ class Language
continue;
}
+ $scope = basename($scope);
+
$result &= $this->fileManager->mergeJsonContents($path . "/$scope.json", $data);
}
}
@@ -285,6 +296,8 @@ class Language
continue;
}
+ $scope = basename($scope);
+
$result &= $this->fileManager->unsetJsonContents($path . "/$scope.json", $unsetData);
}
}