2020-06-20 20:13:51 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\Context;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
abstract class LanguageAPI extends Request {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
|
|
|
|
parent::__construct($context, $externalCall, $params);
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API\Language {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\API\LanguageAPI;
|
2022-11-30 16:42:24 +01:00
|
|
|
use Core\API\Parameter\ArrayType;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\API\Parameter\Parameter;
|
|
|
|
use Core\API\Parameter\StringType;
|
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
|
|
|
use Core\Driver\SQL\Condition\CondOr;
|
|
|
|
use Core\Objects\Context;
|
|
|
|
use Core\Objects\DatabaseEntity\Language;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
|
|
|
class Get extends LanguageAPI {
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array());
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$languages = Language::findAll($sql);
|
|
|
|
$this->success = ($languages !== null);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
if ($this->success) {
|
|
|
|
$this->result['languages'] = [];
|
|
|
|
if (count($languages) === 0) {
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = L("No languages found");
|
|
|
|
} else {
|
2022-06-20 19:52:31 +02:00
|
|
|
foreach ($languages as $language) {
|
|
|
|
$this->result['languages'][$language->getId()] = $language->jsonSerialize();
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Set extends LanguageAPI {
|
|
|
|
|
|
|
|
private Language $language;
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array(
|
2022-11-29 14:17:11 +01:00
|
|
|
'id' => new Parameter('id', Parameter::TYPE_INT, true, NULL),
|
|
|
|
'code' => new StringType('code', 5, true, NULL),
|
2020-06-20 20:13:51 +02:00
|
|
|
));
|
2020-06-23 15:31:09 +02:00
|
|
|
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
private function checkLanguage(): bool {
|
2022-11-29 14:17:11 +01:00
|
|
|
$langId = $this->getParam("id");
|
|
|
|
$langCode = $this->getParam("code");
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
if (is_null($langId) && is_null($langCode)) {
|
2022-11-29 14:17:11 +01:00
|
|
|
return $this->createError(L("Either 'id' or 'code' must be given"));
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$languages = Language::findAll($sql,
|
|
|
|
new CondOr(new Compare("id", $langId), new Compare("code", $langCode))
|
|
|
|
);
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->success = ($languages !== null);
|
|
|
|
$this->lastError = $sql->getLastError();
|
2020-06-20 20:13:51 +02:00
|
|
|
|
|
|
|
if ($this->success) {
|
2022-06-20 19:52:31 +02:00
|
|
|
if (count($languages) === 0) {
|
2020-06-20 20:13:51 +02:00
|
|
|
return $this->createError(L("This Language does not exist"));
|
|
|
|
} else {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->language = array_shift($languages);
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:54:05 +02:00
|
|
|
private function updateLanguage(): void {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
2022-11-29 14:17:11 +01:00
|
|
|
$currentUser = $this->context->getUser();
|
|
|
|
$currentUser->language = $this->language;
|
2023-01-09 14:21:11 +01:00
|
|
|
$this->success = $currentUser->save($sql, ["language"]);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-06-20 19:52:31 +02:00
|
|
|
if (!$this->checkLanguage())
|
2020-06-20 20:13:51 +02:00
|
|
|
return false;
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
if ($this->context->getSession()) {
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->updateLanguage();
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->context->setLanguage($this->language);
|
2022-11-30 16:42:24 +01:00
|
|
|
$this->result["language"] = $this->language->jsonSerialize();
|
2020-06-20 20:13:51 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 16:42:24 +01:00
|
|
|
|
|
|
|
class GetEntries extends LanguageAPI {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, [
|
|
|
|
"code" => new StringType("code", 5, true, NULL),
|
2024-03-29 15:20:45 +01:00
|
|
|
"modules" => new ArrayType("modules", Parameter::TYPE_STRING, true, false),
|
|
|
|
"compression" => new StringType("compression", -1, true, NULL, ["gzip", "zlib"])
|
2022-11-30 16:42:24 +01:00
|
|
|
]);
|
|
|
|
$this->loginRequired = false;
|
|
|
|
$this->csrfTokenRequired = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$code = $this->getParam("code");
|
|
|
|
if ($code === null) {
|
|
|
|
$code = $this->context->getLanguage()->getCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match(Language::LANG_CODE_PATTERN, $code)) {
|
|
|
|
return $this->createError("Invalid lang code format: $code");
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = [];
|
|
|
|
$modulePaths = [];
|
|
|
|
$requestedModules = $this->getParam("modules");
|
|
|
|
foreach ($requestedModules as $module) {
|
|
|
|
if (!preg_match(Language::LANG_MODULE_PATTERN, $module)) {
|
|
|
|
return $this->createError("Invalid module name: $module");
|
|
|
|
}
|
|
|
|
|
|
|
|
$moduleFound = false;
|
2024-04-02 12:54:05 +02:00
|
|
|
foreach (["Core", "Site"] as $baseDir) {
|
|
|
|
$filePath = realpath(implode("/", [WEBROOT, $baseDir, "Localization", $code, "$module.php"]));
|
2022-11-30 16:42:24 +01:00
|
|
|
if ($filePath && is_file($filePath)) {
|
|
|
|
$moduleFound = true;
|
|
|
|
$moduleEntries = @include_once $filePath;
|
2024-04-02 12:54:05 +02:00
|
|
|
$entries[$module] = array_merge($entries[$module] ?? [], $moduleEntries);
|
2022-11-30 16:42:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$moduleFound) {
|
|
|
|
return $this->createError("Module not found: $module");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->result["code"] = $code;
|
2024-03-29 15:20:45 +01:00
|
|
|
|
|
|
|
$compression = $this->getParam("compression");
|
|
|
|
if ($compression) {
|
|
|
|
switch ($compression) {
|
|
|
|
case "gzip":
|
|
|
|
$this->result["compressed"] = base64_encode(gzencode(json_encode($entries), 9));
|
|
|
|
break;
|
|
|
|
case "zlib":
|
|
|
|
$this->result["compressed"] = base64_encode(gzcompress(json_encode($entries), 9, ZLIB_ENCODING_DEFLATE));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(400);
|
|
|
|
return $this->createError("Invalid compression method: $compression");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->result["entries"] = $entries;
|
|
|
|
}
|
2022-11-30 16:42:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|