PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / translation / Util / XliffUtils.php
independent-analytics / vendor / symfony / translation / Util Last commit date
ArrayConverter.php 1 month ago XliffUtils.php 1 month ago
XliffUtils.php
156 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace IAWPSCOPED\Symfony\Component\Translation\Util;
12
13 use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidArgumentException;
14 use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidResourceException;
15 /**
16 * Provides some utility methods for XLIFF translation files, such as validating
17 * their contents according to the XSD schema.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 * @internal
21 */
22 class XliffUtils
23 {
24 /**
25 * Gets xliff file version based on the root "version" attribute.
26 *
27 * Defaults to 1.2 for backwards compatibility.
28 *
29 * @throws InvalidArgumentException
30 */
31 public static function getVersionNumber(\DOMDocument $dom) : string
32 {
33 /** @var \DOMNode $xliff */
34 foreach ($dom->getElementsByTagName('xliff') as $xliff) {
35 $version = $xliff->attributes->getNamedItem('version');
36 if ($version) {
37 return $version->nodeValue;
38 }
39 $namespace = $xliff->attributes->getNamedItem('xmlns');
40 if ($namespace) {
41 if (0 !== \substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) {
42 throw new InvalidArgumentException(\sprintf('Not a valid XLIFF namespace "%s".', $namespace));
43 }
44 return \substr($namespace, 34);
45 }
46 }
47 // Falls back to v1.2
48 return '1.2';
49 }
50 /**
51 * Validates and parses the given file into a DOMDocument.
52 *
53 * @throws InvalidResourceException
54 */
55 public static function validateSchema(\DOMDocument $dom) : array
56 {
57 $xliffVersion = static::getVersionNumber($dom);
58 $internalErrors = \libxml_use_internal_errors(\true);
59 if ($shouldEnable = self::shouldEnableEntityLoader()) {
60 $disableEntities = \libxml_disable_entity_loader(\false);
61 }
62 try {
63 $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion));
64 if (!$isValid) {
65 return self::getXmlErrors($internalErrors);
66 }
67 } finally {
68 if ($shouldEnable) {
69 \libxml_disable_entity_loader($disableEntities);
70 }
71 }
72 $dom->normalizeDocument();
73 \libxml_clear_errors();
74 \libxml_use_internal_errors($internalErrors);
75 return [];
76 }
77 private static function shouldEnableEntityLoader() : bool
78 {
79 static $dom, $schema;
80 if (null === $dom) {
81 $dom = new \DOMDocument();
82 $dom->loadXML('<?xml version="1.0"?><test/>');
83 $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony');
84 \register_shutdown_function(static function () use($tmpfile) {
85 @\unlink($tmpfile);
86 });
87 $schema = '<?xml version="1.0" encoding="utf-8"?>
88 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
89 <xsd:include schemaLocation="file:///' . \str_replace('\\', '/', $tmpfile) . '" />
90 </xsd:schema>';
91 \file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?>
92 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
93 <xsd:element name="test" type="testType" />
94 <xsd:complexType name="testType"/>
95 </xsd:schema>');
96 }
97 return !@$dom->schemaValidateSource($schema);
98 }
99 public static function getErrorsAsString(array $xmlErrors) : string
100 {
101 $errorsAsString = '';
102 foreach ($xmlErrors as $error) {
103 $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']);
104 }
105 return $errorsAsString;
106 }
107 private static function getSchema(string $xliffVersion) : string
108 {
109 if ('1.2' === $xliffVersion) {
110 $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-1.2-strict.xsd');
111 $xmlUri = 'http://www.w3.org/2001/xml.xsd';
112 } elseif ('2.0' === $xliffVersion) {
113 $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-2.0.xsd');
114 $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
115 } else {
116 throw new InvalidArgumentException(\sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
117 }
118 return self::fixXmlLocation($schemaSource, $xmlUri);
119 }
120 /**
121 * Internally changes the URI of a dependent xsd to be loaded locally.
122 */
123 private static function fixXmlLocation(string $schemaSource, string $xmlUri) : string
124 {
125 $newPath = \str_replace('\\', '/', __DIR__) . '/../Resources/schemas/xml.xsd';
126 $parts = \explode('/', $newPath);
127 $locationstart = 'file:///';
128 if (0 === \stripos($newPath, 'phar://')) {
129 $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony');
130 if ($tmpfile) {
131 \copy($newPath, $tmpfile);
132 $parts = \explode('/', \str_replace('\\', '/', $tmpfile));
133 } else {
134 \array_shift($parts);
135 $locationstart = 'phar:///';
136 }
137 }
138 $drive = '\\' === \DIRECTORY_SEPARATOR ? \array_shift($parts) . '/' : '';
139 $newPath = $locationstart . $drive . \implode('/', \array_map('rawurlencode', $parts));
140 return \str_replace($xmlUri, $newPath, $schemaSource);
141 }
142 /**
143 * Returns the XML errors of the internal XML parser.
144 */
145 private static function getXmlErrors(bool $internalErrors) : array
146 {
147 $errors = [];
148 foreach (\libxml_get_errors() as $error) {
149 $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];
150 }
151 \libxml_clear_errors();
152 \libxml_use_internal_errors($internalErrors);
153 return $errors;
154 }
155 }
156