chore: Refactor XML text encoding

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-02-10 18:02:05 +01:00
parent b7284cb358
commit 735cb29ea3
4 changed files with 21 additions and 61 deletions

View File

@@ -77,18 +77,18 @@ final class XmlCategoryExport extends Service implements XmlCategoryExportServic
foreach ($categories as $category) {
$nodeCategory = $document->createElement('Category');
$nodeCategories->appendChild($nodeCategory);
$nodeCategory->setAttribute('id', $category->getId());
$nodeCategory->appendChild(
$document->createElement('name', XmlUtil::escapeChars($category->getName()))
$document->createElement('name', $document->createTextNode($category->getName())->nodeValue)
);
$nodeCategory->appendChild(
$document->createElement(
'description',
XmlUtil::escapeChars($category->getDescription())
$document->createTextNode($category->getDescription())->nodeValue
)
);
$nodeCategories->appendChild($nodeCategory);
}
return $nodeCategories;

View File

@@ -78,13 +78,18 @@ final class XmlClientExport extends Service implements XmlClientExportService
foreach ($clients as $client) {
$nodeClient = $document->createElement('Client');
$nodeClient->setAttribute('id', $client->getId());
$nodeClient->appendChild($document->createElement('name', XmlUtil::escapeChars($client->getName())));
$nodeClient->appendChild(
$document->createElement('description', XmlUtil::escapeChars($client->getDescription()))
);
$nodeClients->appendChild($nodeClient);
$nodeClient->setAttribute('id', $client->getId());
$nodeClient->appendChild(
$document->createElement('name', $document->createTextNode($client->getName())->nodeValue)
);
$nodeClient->appendChild(
$document->createElement(
'description',
$document->createTextNode($client->getDescription())->nodeValue
)
);
}
return $nodeClients;

View File

@@ -32,7 +32,6 @@ use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Export\Services\XmlUtil;
use SP\Domain\Tag\Services\TagService;
use function SP\__u;
@@ -43,7 +42,7 @@ use function SP\__u;
final class XmlTagExport extends Service implements XmlTagExportService
{
public function __construct(
Application $application,
Application $application,
private readonly TagService $tagService,
) {
@@ -77,10 +76,12 @@ final class XmlTagExport extends Service implements XmlTagExportService
foreach ($tags as $tag) {
$nodeTag = $document->createElement('Tag');
$nodeTag->setAttribute('id', $tag->getId());
$nodeTag->appendChild($document->createElement('name', XmlUtil::escapeChars($tag->getName())));
$nodeTags->appendChild($nodeTag);
$nodeTag->setAttribute('id', $tag->getId());
$nodeTag->appendChild(
$document->createElement('name', $document->createTextNode($tag->getName())->nodeValue)
);
}
return $nodeTags;

View File

@@ -1,46 +0,0 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Domain\Export\Services;
/**
* Class XmlUtil
*/
final class XmlUtil
{
/**
* Escapar carácteres no válidos en XML
*
* @param $data string Los datos a escapar
*
* @return string
*/
public static function escapeChars(string $data): string
{
$arrStrFrom = ['&', '<', '>', '"', '\''];
$arrStrTo = ['&#38;', '&#60;', '&#62;', '&#34;', '&#39;'];
return str_replace($arrStrFrom, $arrStrTo, $data);
}
}