XliffUtils.php
125 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Translation\Util; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 5 | use MailPoetVendor\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 6 | class XliffUtils |
| 7 | { |
| 8 | public static function getVersionNumber(\DOMDocument $dom) : string |
| 9 | { |
| 10 | foreach ($dom->getElementsByTagName('xliff') as $xliff) { |
| 11 | $version = $xliff->attributes->getNamedItem('version'); |
| 12 | if ($version) { |
| 13 | return $version->nodeValue; |
| 14 | } |
| 15 | $namespace = $xliff->attributes->getNamedItem('xmlns'); |
| 16 | if ($namespace) { |
| 17 | if (0 !== \substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) { |
| 18 | throw new InvalidArgumentException(\sprintf('Not a valid XLIFF namespace "%s".', $namespace)); |
| 19 | } |
| 20 | return \substr($namespace, 34); |
| 21 | } |
| 22 | } |
| 23 | // Falls back to v1.2 |
| 24 | return '1.2'; |
| 25 | } |
| 26 | public static function validateSchema(\DOMDocument $dom) : array |
| 27 | { |
| 28 | $xliffVersion = static::getVersionNumber($dom); |
| 29 | $internalErrors = \libxml_use_internal_errors(\true); |
| 30 | if ($shouldEnable = self::shouldEnableEntityLoader()) { |
| 31 | $disableEntities = \libxml_disable_entity_loader(\false); |
| 32 | } |
| 33 | try { |
| 34 | $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion)); |
| 35 | if (!$isValid) { |
| 36 | return self::getXmlErrors($internalErrors); |
| 37 | } |
| 38 | } finally { |
| 39 | if ($shouldEnable) { |
| 40 | \libxml_disable_entity_loader($disableEntities); |
| 41 | } |
| 42 | } |
| 43 | $dom->normalizeDocument(); |
| 44 | \libxml_clear_errors(); |
| 45 | \libxml_use_internal_errors($internalErrors); |
| 46 | return []; |
| 47 | } |
| 48 | private static function shouldEnableEntityLoader() : bool |
| 49 | { |
| 50 | // Version prior to 8.0 can be enabled without deprecation |
| 51 | if (\PHP_VERSION_ID < 80000) { |
| 52 | return \true; |
| 53 | } |
| 54 | static $dom, $schema; |
| 55 | if (null === $dom) { |
| 56 | $dom = new \DOMDocument(); |
| 57 | $dom->loadXML('<?xml version="1.0"?><test/>'); |
| 58 | $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony'); |
| 59 | \register_shutdown_function(static function () use($tmpfile) { |
| 60 | @\unlink($tmpfile); |
| 61 | }); |
| 62 | $schema = '<?xml version="1.0" encoding="utf-8"?> |
| 63 | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 64 | <xsd:include schemaLocation="file:///' . \str_replace('\\', '/', $tmpfile) . '" /> |
| 65 | </xsd:schema>'; |
| 66 | \file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?> |
| 67 | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 68 | <xsd:element name="test" type="testType" /> |
| 69 | <xsd:complexType name="testType"/> |
| 70 | </xsd:schema>'); |
| 71 | } |
| 72 | return !@$dom->schemaValidateSource($schema); |
| 73 | } |
| 74 | public static function getErrorsAsString(array $xmlErrors) : string |
| 75 | { |
| 76 | $errorsAsString = ''; |
| 77 | foreach ($xmlErrors as $error) { |
| 78 | $errorsAsString .= \sprintf("[%s %s] %s (in %s - line %d, column %d)\n", \LIBXML_ERR_WARNING === $error['level'] ? 'WARNING' : 'ERROR', $error['code'], $error['message'], $error['file'], $error['line'], $error['column']); |
| 79 | } |
| 80 | return $errorsAsString; |
| 81 | } |
| 82 | private static function getSchema(string $xliffVersion) : string |
| 83 | { |
| 84 | if ('1.2' === $xliffVersion) { |
| 85 | $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-1.2-strict.xsd'); |
| 86 | $xmlUri = 'http://www.w3.org/2001/xml.xsd'; |
| 87 | } elseif ('2.0' === $xliffVersion) { |
| 88 | $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-2.0.xsd'); |
| 89 | $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd'; |
| 90 | } else { |
| 91 | throw new InvalidArgumentException(\sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion)); |
| 92 | } |
| 93 | return self::fixXmlLocation($schemaSource, $xmlUri); |
| 94 | } |
| 95 | private static function fixXmlLocation(string $schemaSource, string $xmlUri) : string |
| 96 | { |
| 97 | $newPath = \str_replace('\\', '/', __DIR__) . '/../Resources/schemas/xml.xsd'; |
| 98 | $parts = \explode('/', $newPath); |
| 99 | $locationstart = 'file:///'; |
| 100 | if (0 === \stripos($newPath, 'phar://')) { |
| 101 | $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony'); |
| 102 | if ($tmpfile) { |
| 103 | \copy($newPath, $tmpfile); |
| 104 | $parts = \explode('/', \str_replace('\\', '/', $tmpfile)); |
| 105 | } else { |
| 106 | \array_shift($parts); |
| 107 | $locationstart = 'phar:///'; |
| 108 | } |
| 109 | } |
| 110 | $drive = '\\' === \DIRECTORY_SEPARATOR ? \array_shift($parts) . '/' : ''; |
| 111 | $newPath = $locationstart . $drive . \implode('/', \array_map('rawurlencode', $parts)); |
| 112 | return \str_replace($xmlUri, $newPath, $schemaSource); |
| 113 | } |
| 114 | private static function getXmlErrors(bool $internalErrors) : array |
| 115 | { |
| 116 | $errors = []; |
| 117 | foreach (\libxml_get_errors() as $error) { |
| 118 | $errors[] = ['level' => \LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', 'code' => $error->code, 'message' => \trim($error->message), 'file' => $error->file ?: 'n/a', 'line' => $error->line, 'column' => $error->column]; |
| 119 | } |
| 120 | \libxml_clear_errors(); |
| 121 | \libxml_use_internal_errors($internalErrors); |
| 122 | return $errors; |
| 123 | } |
| 124 | } |
| 125 |