PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / vendor / _scoped / symfony / translation / Loader / MoFileLoader.php

MoFileLoader.php in Metricool – Social media and site statistics 2.1.1, at vendor/_scoped/symfony/translation/Loader/MoFileLoader.php

115 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 Metricool\Vendor\Symfony\Component\Translation\Loader;
12
13 use Metricool\Vendor\Symfony\Component\Translation\Exception\InvalidResourceException;
14 /**
15 * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
16 */
17 class MoFileLoader extends FileLoader
18 {
19 /**
20 * Magic used for validating the format of an MO file as well as
21 * detecting if the machine used to create that file was little endian.
22 */
23 public const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
24 /**
25 * Magic used for validating the format of an MO file as well as
26 * detecting if the machine used to create that file was big endian.
27 */
28 public const MO_BIG_ENDIAN_MAGIC = 0xde120495;
29 /**
30 * The size of the header of an MO file in bytes.
31 */
32 public const MO_HEADER_SIZE = 28;
33 /**
34 * Parses machine object (MO) format, independent of the machine's endian it
35 * was created on. Both 32bit and 64bit systems are supported.
36 *
37 * {@inheritdoc}
38 */
39 protected function loadResource(string $resource)
40 {
41 $stream = fopen($resource, 'r');
42 $stat = fstat($stream);
43 if ($stat['size'] < self::MO_HEADER_SIZE) {
44 throw new InvalidResourceException('MO stream content has an invalid format.');
45 }
46 $magic = unpack('V1', fread($stream, 4));
47 $magic = hexdec(substr(dechex(current($magic)), -8));
48 if (self::MO_LITTLE_ENDIAN_MAGIC == $magic) {
49 $isBigEndian = \false;
50 } elseif (self::MO_BIG_ENDIAN_MAGIC == $magic) {
51 $isBigEndian = \true;
52 } else {
53 throw new InvalidResourceException('MO stream content has an invalid format.');
54 }
55 // formatRevision
56 $this->readLong($stream, $isBigEndian);
57 $count = $this->readLong($stream, $isBigEndian);
58 $offsetId = $this->readLong($stream, $isBigEndian);
59 $offsetTranslated = $this->readLong($stream, $isBigEndian);
60 // sizeHashes
61 $this->readLong($stream, $isBigEndian);
62 // offsetHashes
63 $this->readLong($stream, $isBigEndian);
64 $messages = [];
65 for ($i = 0; $i < $count; ++$i) {
66 $pluralId = null;
67 $translated = null;
68 fseek($stream, $offsetId + $i * 8);
69 $length = $this->readLong($stream, $isBigEndian);
70 $offset = $this->readLong($stream, $isBigEndian);
71 if ($length < 1) {
72 continue;
73 }
74 fseek($stream, $offset);
75 $singularId = fread($stream, $length);
76 if (str_contains($singularId, "\x00")) {
77 [$singularId, $pluralId] = explode("\x00", $singularId);
78 }
79 fseek($stream, $offsetTranslated + $i * 8);
80 $length = $this->readLong($stream, $isBigEndian);
81 $offset = $this->readLong($stream, $isBigEndian);
82 if ($length < 1) {
83 continue;
84 }
85 fseek($stream, $offset);
86 $translated = fread($stream, $length);
87 if (str_contains($translated, "\x00")) {
88 $translated = explode("\x00", $translated);
89 }
90 $ids = ['singular' => $singularId, 'plural' => $pluralId];
91 $item = compact('ids', 'translated');
92 if (!empty($item['ids']['singular'])) {
93 $id = $item['ids']['singular'];
94 if (isset($item['ids']['plural'])) {
95 $id .= '|' . $item['ids']['plural'];
96 }
97 $messages[$id] = stripcslashes(implode('|', (array) $item['translated']));
98 }
99 }
100 fclose($stream);
101 return array_filter($messages);
102 }
103 /**
104 * Reads an unsigned long from stream respecting endianness.
105 *
106 * @param resource $stream
107 */
108 private function readLong($stream, bool $isBigEndian): int
109 {
110 $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
111 $result = current($result);
112 return (int) substr($result, -8);
113 }
114 }
115