Manifest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace TYPO3\PharStreamWrapper\Phar;
  3. /*
  4. * This file is part of the TYPO3 project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms
  7. * of the MIT License (MIT). For the full copyright and license information,
  8. * please read the LICENSE file that was distributed with this source code.
  9. *
  10. * The TYPO3 project - inspiring people to share!
  11. */
  12. use Brumann\Polyfill\Unserialize;
  13. class Manifest
  14. {
  15. /**
  16. * @param string $content
  17. * @return self
  18. * @see http://php.net/manual/en/phar.fileformat.phar.php
  19. */
  20. public static function fromContent($content)
  21. {
  22. $target = new static();
  23. $target->manifestLength = Reader::resolveFourByteLittleEndian($content, 0);
  24. $target->amountOfFiles = Reader::resolveFourByteLittleEndian($content, 4);
  25. $target->flags = Reader::resolveFourByteLittleEndian($content, 10);
  26. $target->aliasLength = Reader::resolveFourByteLittleEndian($content, 14);
  27. $target->alias = substr($content, 18, $target->aliasLength);
  28. $target->metaDataLength = Reader::resolveFourByteLittleEndian($content, 18 + $target->aliasLength);
  29. $target->metaData = substr($content, 22 + $target->aliasLength, $target->metaDataLength);
  30. $apiVersionNibbles = Reader::resolveTwoByteBigEndian($content, 8);
  31. $target->apiVersion = implode('.', array(
  32. ($apiVersionNibbles & 0xf000) >> 12,
  33. ($apiVersionNibbles & 0x0f00) >> 8,
  34. ($apiVersionNibbles & 0x00f0) >> 4,
  35. ));
  36. return $target;
  37. }
  38. /**
  39. * @var int
  40. */
  41. private $manifestLength;
  42. /**
  43. * @var int
  44. */
  45. private $amountOfFiles;
  46. /**
  47. * @var string
  48. */
  49. private $apiVersion;
  50. /**
  51. * @var int
  52. */
  53. private $flags;
  54. /**
  55. * @var int
  56. */
  57. private $aliasLength;
  58. /**
  59. * @var string
  60. */
  61. private $alias;
  62. /**
  63. * @var int
  64. */
  65. private $metaDataLength;
  66. /**
  67. * @var string
  68. */
  69. private $metaData;
  70. /**
  71. * Avoid direct instantiation.
  72. */
  73. private function __construct()
  74. {
  75. }
  76. /**
  77. * @return int
  78. */
  79. public function getManifestLength()
  80. {
  81. return $this->manifestLength;
  82. }
  83. /**
  84. * @return int
  85. */
  86. public function getAmountOfFiles()
  87. {
  88. return $this->amountOfFiles;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getApiVersion()
  94. {
  95. return $this->apiVersion;
  96. }
  97. /**
  98. * @return int
  99. */
  100. public function getFlags()
  101. {
  102. return $this->flags;
  103. }
  104. /**
  105. * @return int
  106. */
  107. public function getAliasLength()
  108. {
  109. return $this->aliasLength;
  110. }
  111. /**
  112. * @return string
  113. */
  114. public function getAlias()
  115. {
  116. return $this->alias;
  117. }
  118. /**
  119. * @return int
  120. */
  121. public function getMetaDataLength()
  122. {
  123. return $this->metaDataLength;
  124. }
  125. /**
  126. * @return string
  127. */
  128. public function getMetaData()
  129. {
  130. return $this->metaData;
  131. }
  132. /**
  133. * @return mixed|null
  134. */
  135. public function deserializeMetaData()
  136. {
  137. if (empty($this->metaData)) {
  138. return null;
  139. }
  140. $result = Unserialize::unserialize($this->metaData, array('allowed_classes' => false));
  141. $serialized = json_encode($result);
  142. if (strpos($serialized, '__PHP_Incomplete_Class_Name') !== false) {
  143. throw new DeserializationException(
  144. 'Meta-data contains serialized object',
  145. 1539623382
  146. );
  147. }
  148. return $result;
  149. }
  150. }