Drupal investigation

MoFileLoader.php 4.7KB

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