Commit 007789c9 authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[BUGFIX] fix problems with CDATA-blocks

parent 079fbdf9
...@@ -83,7 +83,17 @@ class FileBaseXLFService extends FileBaseService { ...@@ -83,7 +83,17 @@ class FileBaseXLFService extends FileBaseService {
} }
// read xml into array // read xml into array
$xmlContent = simplexml_load_file($file); // $xmlContent = simplexml_load_file($file);
// $xmlContent = json_decode(json_encode($xmlContent), TRUE);
$contents = file_get_contents($file);
// Remove CDATA tags and encode html entities
$contents = preg_replace_callback ("#<\!\[CDATA\[(.*)\]\]>#sU", function($matches) {
return htmlspecialchars($matches[1]);
}, $contents);
$xmlContent = simplexml_load_string($contents,'SimpleXMLElement', LIBXML_NOCDATA);
$xmlContent = json_decode(json_encode($xmlContent), TRUE); $xmlContent = json_decode(json_encode($xmlContent), TRUE);
// check data // check data
...@@ -265,8 +275,28 @@ class FileBaseXLFService extends FileBaseService { ...@@ -265,8 +275,28 @@ class FileBaseXLFService extends FileBaseService {
$body = '<body>' . "\n"; $body = '<body>' . "\n";
foreach ($phpArray['data'] as $constant => $value) { foreach ($phpArray['data'] as $constant => $value) {
$approved = ($targetLanguage !== 'default' ? ' approved="yes"' : ''); $approved = ($targetLanguage !== 'default' ? ' approved="yes"' : '');
$enValue = SgLib::htmlSpecialCharsIgnoringCdata($enLanguage[$constant]); $enValue = $enLanguage[$constant];
$value = SgLib::htmlSpecialCharsIgnoringCdata($value);
$startCDATA = "<![CDATA[";
$endCDATA = "]]>";
//Detects if html is in the value and add CDATA tags
if(preg_match("#<(\w+).*>.*</(\w)>#isU", $value) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$value = $startCDATA.$value.$endCDATA;
} else {
$value = htmlspecialchars($value);
}
//Detects if html is in the enValue and add CDATA tags
if(preg_match("#<(\w+).*>.*</(\w)>#isU", $enValue) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$enValue = $startCDATA.$enValue.$endCDATA;
} else {
$enValue = htmlspecialchars($enLanguage[$constant]);
}
$body .= "\t\t" . '<trans-unit id="' . htmlspecialchars( $body .= "\t\t" . '<trans-unit id="' . htmlspecialchars(
$constant $constant
) . '"' . $approved . $this->addPreserveSpaceAttribute( ) . '"' . $approved . $this->addPreserveSpaceAttribute(
...@@ -402,4 +432,4 @@ class FileBaseXLFService extends FileBaseService { ...@@ -402,4 +432,4 @@ class FileBaseXLFService extends FileBaseService {
} }
} }
?> ?>
\ No newline at end of file
...@@ -201,7 +201,19 @@ class FileBaseXMLService extends FileBaseService { ...@@ -201,7 +201,19 @@ class FileBaseXMLService extends FileBaseService {
if (SgLib::checkForCdataInString($value)) { if (SgLib::checkForCdataInString($value)) {
$simpleSubNode = $xmlElement->addChildCData($tagName, $value); $simpleSubNode = $xmlElement->addChildCData($tagName, $value);
} else { } else {
$simpleSubNode = $xmlElement->addChild($tagName, htmlspecialchars($value)); $startCDATA = "<![CDATA[";
$endCDATA = "]]>";
//Detects if html is in the value and add CDATA tags
if(preg_match("#<(\w+).*>.*<\/(\w+)>#isU", $value) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$value = $startCDATA.$value.$endCDATA;
} else {
$value = htmlspecialchars($value);
}
$simpleSubNode = $xmlElement->addChild($tagName, $value);
} }
if ($indexAttributeValue !== NULL) { if ($indexAttributeValue !== NULL) {
$simpleSubNode->addAttribute('index', $indexAttributeValue); $simpleSubNode->addAttribute('index', $indexAttributeValue);
...@@ -351,10 +363,18 @@ class FileBaseXMLService extends FileBaseService { ...@@ -351,10 +363,18 @@ class FileBaseXMLService extends FileBaseService {
$dom = new DOMDocument('1.0'); $dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = FALSE; $dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE; $dom->formatOutput = TRUE;
$dom->loadXML($xmlElement->asXML());
$xmlRaw = $xmlElement->asXML();
//Search CDATA tags to decode the content inside
//Because $xmlElement->asXML() encode html entities
$xmlRaw = preg_replace_callback ("#(&lt;\!\[CDATA\[.*\]\]&gt;)#sU", function($matches) {
return htmlspecialchars_decode($matches[1]);}, $xmlRaw);
$dom->loadXML($xmlRaw);
$formattedXml = $dom->saveXML(); $formattedXml = $dom->saveXML();
return $formattedXml; return $formattedXml;
} }
} }
?> ?>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment