fix symbols in map name

This commit is contained in:
Beu 2025-02-07 20:43:08 +01:00
parent 43cc088fa6
commit cfa42acd24
2 changed files with 37 additions and 23 deletions

View File

@ -59,6 +59,7 @@ abstract class FileUtil {
$fileName = Formatter::utf8($fileName); $fileName = Formatter::utf8($fileName);
$fileName = preg_replace('/[^0-9A-Za-z\-\+\.\_\ ]/', '', $fileName); $fileName = preg_replace('/[^0-9A-Za-z\-\+\.\_\ ]/', '', $fileName);
$fileName = preg_replace('/ /', '_', $fileName); $fileName = preg_replace('/ /', '_', $fileName);
$fileName = str_replace(["'", "+"], '', $fileName);
return $fileName; return $fileName;
} }

View File

@ -322,7 +322,7 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
$nameLabel->setAction(self::ACTION_OPEN_FOLDER . substr($shortFilePath, 0, -1))->addTooltipLabelFeature($tooltipLabel, 'Open folder ' . $fileName); $nameLabel->setAction(self::ACTION_OPEN_FOLDER . substr($shortFilePath, 0, -1))->addTooltipLabelFeature($tooltipLabel, 'Open folder ' . $fileName);
} else { } else {
// File // File
$nameLabel->setAction(self::ACTION_INSPECT_FILE . $fileName)->addTooltipLabelFeature($tooltipLabel, 'Inspect file ' . $fileName); $nameLabel->setAction(self::ACTION_INSPECT_FILE . base64_encode($fileName))->addTooltipLabelFeature($tooltipLabel, 'Inspect file ' . $fileName);
if ($canAddMaps) { if ($canAddMaps) {
// 'Add' button // 'Add' button
@ -331,7 +331,7 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
$addButton->setX($width - 5); $addButton->setX($width - 5);
$addButton->setSize(4, 4); $addButton->setSize(4, 4);
$addButton->setSubStyle($addButton::SUBSTYLE_NewBullet); $addButton->setSubStyle($addButton::SUBSTYLE_NewBullet);
$addButton->setAction(self::ACTION_ADD_FILE . $fileName); $addButton->setAction(self::ACTION_ADD_FILE . base64_encode($fileName));
$addButton->addTooltipLabelFeature($tooltipLabel, 'Add map ' . $fileName); $addButton->addTooltipLabelFeature($tooltipLabel, 'Add map ' . $fileName);
} }
@ -342,7 +342,7 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
$eraseButton->setX($width - 10); $eraseButton->setX($width - 10);
$eraseButton->setSize(4, 4); $eraseButton->setSize(4, 4);
$eraseButton->setSubStyle($eraseButton::SUBSTYLE_Erase); $eraseButton->setSubStyle($eraseButton::SUBSTYLE_Erase);
$eraseButton->setAction(self::ACTION_ERASE_FILE . $fileName); $eraseButton->setAction(self::ACTION_ERASE_FILE . base64_encode($fileName));
$eraseButton->addTooltipLabelFeature($tooltipLabel, 'Erase file ' . $fileName); $eraseButton->addTooltipLabelFeature($tooltipLabel, 'Erase file ' . $fileName);
} }
} }
@ -457,9 +457,23 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
*/ */
public function handleInspectFile(array $actionCallback, Player $player) { public function handleInspectFile(array $actionCallback, Player $player) {
$actionName = $actionCallback[1][2]; $actionName = $actionCallback[1][2];
$fileName = substr($actionName, strlen(self::ACTION_INSPECT_FILE)); $fileName = base64_decode(substr($actionName, strlen(self::ACTION_INSPECT_FILE)));
// TODO: show inspect file view $folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH);
var_dump($fileName); $filePath = $folderPath . $fileName;
$mapsFolder = $this->maniaControl->getServer()->getDirectory()->getMapsFolder();
$relativeFilePath = substr($filePath, strlen($mapsFolder));
try {
$message = '';
$infos = $this->maniaControl->getClient()->getMapInfo($relativeFilePath);
foreach ($infos as $key => $value) {
$message .= '$<$0c0' . $key .':$> '. $value . PHP_EOL;
}
$this->maniaControl->getChat()->sendChat($message, $player->login, false);
} catch (\Throwable $th) {
$this->maniaControl->getChat()->sendError("can't fetch map info: ". $th->getMessage(), $player->login);
}
} }
/** /**
@ -470,7 +484,7 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
*/ */
public function handleAddFile(array $actionCallback, Player $player) { public function handleAddFile(array $actionCallback, Player $player) {
$actionName = $actionCallback[1][2]; $actionName = $actionCallback[1][2];
$fileName = substr($actionName, strlen(self::ACTION_ADD_FILE)); $fileName = base64_decode(substr($actionName, strlen(self::ACTION_ADD_FILE)));
$folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH); $folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH);
$filePath = $folderPath . $fileName; $filePath = $folderPath . $fileName;
@ -529,7 +543,7 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
*/ */
public function handleEraseFile(array $actionCallback, Player $player) { public function handleEraseFile(array $actionCallback, Player $player) {
$actionName = $actionCallback[1][2]; $actionName = $actionCallback[1][2];
$fileName = substr($actionName, strlen(self::ACTION_ERASE_FILE)); $fileName = base64_decode(substr($actionName, strlen(self::ACTION_ERASE_FILE)));
$folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH); $folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH);
$filePath = $folderPath . $fileName; $filePath = $folderPath . $fileName;
if (@unlink($filePath)) { if (@unlink($filePath)) {
@ -652,26 +666,25 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
$attributes[trim($key)] = trim($value); $attributes[trim($key)] = trim($value);
} }
$attrNames = ['filename*' => true, 'filename' => false]; $fileName = null;
$filename = null;
$isUtf8 = false; if (array_key_exists('filename*', $attributes)) {
foreach ($attrNames as $attrName => $utf8) { $fileName = trim($attributes['filename*']);
if (!empty($attributes[$attrName])) {
$fileName = trim($attributes[$attrName]); // remove prefix if needed
$isUtf8 = $utf8; if (strpos(strtolower($fileName), "utf-8''") === 0) {
break; $fileName = substr($fileName, strlen("utf-8''"));
} }
} else if (array_key_exists('filename', $attributes)) {
$fileName = trim($attributes['filename']);
} }
if ($fileName !== null) { if ($fileName !== null) {
if ($isUtf8 && strpos(strtolower($fileName), "utf-8''") === 0 && $fileName = substr($fileName, strlen("utf-8''"))) {
$filePath = $folderPath . FileUtil::getClearedFileName(rawurldecode($fileName));
}
if (substr($fileName, 0, 1) === '"' && substr($fileName, -1, 1) === '"') { if (substr($fileName, 0, 1) === '"' && substr($fileName, -1, 1) === '"') {
$filePath = $folderPath . substr($fileName, 1, -1); $fileName = substr($fileName, 1, -1);
} else {
$filePath = $folderPath . $fileName;
} }
$filePath = $folderPath . FileUtil::getClearedFileName($fileName);
} }
} }