ZipEntry.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. <?php
  2. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  3. namespace PhpZip\Model;
  4. use PhpZip\Constants\DosAttrs;
  5. use PhpZip\Constants\DosCodePage;
  6. use PhpZip\Constants\GeneralPurposeBitFlag;
  7. use PhpZip\Constants\UnixStat;
  8. use PhpZip\Constants\ZipCompressionLevel;
  9. use PhpZip\Constants\ZipCompressionMethod;
  10. use PhpZip\Constants\ZipConstants;
  11. use PhpZip\Constants\ZipEncryptionMethod;
  12. use PhpZip\Constants\ZipPlatform;
  13. use PhpZip\Constants\ZipVersion;
  14. use PhpZip\Exception\InvalidArgumentException;
  15. use PhpZip\Exception\RuntimeException;
  16. use PhpZip\Exception\ZipUnsupportMethodException;
  17. use PhpZip\Model\Extra\ExtraFieldsCollection;
  18. use PhpZip\Model\Extra\Fields\AsiExtraField;
  19. use PhpZip\Model\Extra\Fields\ExtendedTimestampExtraField;
  20. use PhpZip\Model\Extra\Fields\NtfsExtraField;
  21. use PhpZip\Model\Extra\Fields\OldUnixExtraField;
  22. use PhpZip\Model\Extra\Fields\UnicodePathExtraField;
  23. use PhpZip\Model\Extra\Fields\WinZipAesExtraField;
  24. use PhpZip\Model\Extra\ZipExtraField;
  25. use PhpZip\Util\DateTimeConverter;
  26. use PhpZip\Util\StringUtil;
  27. /**
  28. * ZIP file entry.
  29. *
  30. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  31. *
  32. * @author Ne-Lexa alexey@nelexa.ru
  33. * @license MIT
  34. */
  35. class ZipEntry
  36. {
  37. /** @var int the unknown value for numeric properties */
  38. const UNKNOWN = -1;
  39. /**
  40. * @var int DOS platform
  41. *
  42. * @deprecated Use {@see ZipPlatform::OS_DOS}
  43. */
  44. const PLATFORM_FAT = ZipPlatform::OS_DOS;
  45. /**
  46. * @var int Unix platform
  47. *
  48. * @deprecated Use {@see ZipPlatform::OS_UNIX}
  49. */
  50. const PLATFORM_UNIX = ZipPlatform::OS_UNIX;
  51. /**
  52. * @var int MacOS platform
  53. *
  54. * @deprecated Use {@see ZipPlatform::OS_MAC_OSX}
  55. */
  56. const PLATFORM_OS_X = ZipPlatform::OS_MAC_OSX;
  57. /**
  58. * Pseudo compression method for WinZip AES encrypted entries.
  59. * Require php extension openssl or mcrypt.
  60. *
  61. * @deprecated Use {@see ZipCompressionMethod::WINZIP_AES}
  62. */
  63. const METHOD_WINZIP_AES = ZipCompressionMethod::WINZIP_AES;
  64. /** @var string Entry name (filename in archive) */
  65. private $name;
  66. /** @var bool Is directory */
  67. private $isDirectory;
  68. /** @var ZipData|null Zip entry contents */
  69. private $data;
  70. /** @var int Made by platform */
  71. private $createdOS = self::UNKNOWN;
  72. /** @var int Extracted by platform */
  73. private $extractedOS = self::UNKNOWN;
  74. /** @var int Software version */
  75. private $softwareVersion = self::UNKNOWN;
  76. /** @var int Version needed to extract */
  77. private $extractVersion = self::UNKNOWN;
  78. /** @var int Compression method */
  79. private $compressionMethod = self::UNKNOWN;
  80. /** @var int General purpose bit flags */
  81. private $generalPurposeBitFlags = 0;
  82. /** @var int Dos time */
  83. private $dosTime = self::UNKNOWN;
  84. /** @var int Crc32 */
  85. private $crc = self::UNKNOWN;
  86. /** @var int Compressed size */
  87. private $compressedSize = self::UNKNOWN;
  88. /** @var int Uncompressed size */
  89. private $uncompressedSize = self::UNKNOWN;
  90. /** @var int Internal attributes */
  91. private $internalAttributes = 0;
  92. /** @var int External attributes */
  93. private $externalAttributes = 0;
  94. /** @var int relative Offset Of Local File Header */
  95. private $localHeaderOffset = 0;
  96. /**
  97. * Collections of Extra Fields in Central Directory.
  98. * Keys from Header ID [int] and value Extra Field [ExtraField].
  99. *
  100. * @var ExtraFieldsCollection
  101. */
  102. protected $cdExtraFields;
  103. /**
  104. * Collections of Extra Fields int local header.
  105. * Keys from Header ID [int] and value Extra Field [ExtraField].
  106. *
  107. * @var ExtraFieldsCollection
  108. */
  109. protected $localExtraFields;
  110. /** @var string|null comment field */
  111. private $comment;
  112. /** @var string|null entry password for read or write encryption data */
  113. private $password;
  114. /** @var int encryption method */
  115. private $encryptionMethod = ZipEncryptionMethod::NONE;
  116. /** @var int */
  117. private $compressionLevel = ZipCompressionLevel::NORMAL;
  118. /** @var string|null */
  119. private $charset;
  120. /**
  121. * ZipEntry constructor.
  122. *
  123. * @param string $name Entry name
  124. * @param string|null $charset DOS charset
  125. */
  126. public function __construct($name, $charset = null)
  127. {
  128. $this->setName($name, $charset);
  129. $this->cdExtraFields = new ExtraFieldsCollection();
  130. $this->localExtraFields = new ExtraFieldsCollection();
  131. }
  132. /**
  133. * This method only internal use.
  134. *
  135. * @param string $name
  136. * @param int $createdOS
  137. * @param int $extractedOS
  138. * @param int $softwareVersion
  139. * @param int $extractVersion
  140. * @param int $compressionMethod
  141. * @param int $gpbf
  142. * @param int $dosTime
  143. * @param int $crc
  144. * @param int $compressedSize
  145. * @param int $uncompressedSize
  146. * @param int $internalAttributes
  147. * @param int $externalAttributes
  148. * @param int $offsetLocalHeader
  149. * @param string|null $comment
  150. * @param string|null $charset
  151. *
  152. * @return ZipEntry
  153. *
  154. * @internal
  155. *
  156. * @noinspection PhpTooManyParametersInspection
  157. */
  158. public static function create(
  159. $name,
  160. $createdOS,
  161. $extractedOS,
  162. $softwareVersion,
  163. $extractVersion,
  164. $compressionMethod,
  165. $gpbf,
  166. $dosTime,
  167. $crc,
  168. $compressedSize,
  169. $uncompressedSize,
  170. $internalAttributes,
  171. $externalAttributes,
  172. $offsetLocalHeader,
  173. $comment,
  174. $charset
  175. ) {
  176. $entry = new self($name);
  177. $entry->createdOS = (int) $createdOS;
  178. $entry->extractedOS = (int) $extractedOS;
  179. $entry->softwareVersion = (int) $softwareVersion;
  180. $entry->extractVersion = (int) $extractVersion;
  181. $entry->compressionMethod = (int) $compressionMethod;
  182. $entry->generalPurposeBitFlags = (int) $gpbf;
  183. $entry->dosTime = (int) $dosTime;
  184. $entry->crc = (int) $crc;
  185. $entry->compressedSize = (int) $compressedSize;
  186. $entry->uncompressedSize = (int) $uncompressedSize;
  187. $entry->internalAttributes = (int) $internalAttributes;
  188. $entry->externalAttributes = (int) $externalAttributes;
  189. $entry->localHeaderOffset = (int) $offsetLocalHeader;
  190. $entry->setComment($comment);
  191. $entry->setCharset($charset);
  192. $entry->updateCompressionLevel();
  193. return $entry;
  194. }
  195. /**
  196. * Set entry name.
  197. *
  198. * @param string $name New entry name
  199. * @param string|null $charset
  200. *
  201. * @return ZipEntry
  202. */
  203. private function setName($name, $charset = null)
  204. {
  205. if ($name === null) {
  206. throw new InvalidArgumentException('zip entry name is null');
  207. }
  208. $name = ltrim((string) $name, '\\/');
  209. if ($name === '') {
  210. throw new InvalidArgumentException('Empty zip entry name');
  211. }
  212. $name = (string) $name;
  213. $length = \strlen($name);
  214. if ($length > 0xffff) {
  215. throw new InvalidArgumentException('Illegal zip entry name parameter');
  216. }
  217. $this->setCharset($charset);
  218. if ($this->charset === null && !StringUtil::isASCII($name)) {
  219. $this->enableUtf8Name(true);
  220. }
  221. $this->name = $name;
  222. $this->isDirectory = ($length = \strlen($name)) >= 1 && $name[$length - 1] === '/';
  223. $this->externalAttributes = $this->isDirectory ? DosAttrs::DOS_DIRECTORY : DosAttrs::DOS_ARCHIVE;
  224. if ($this->extractVersion !== self::UNKNOWN) {
  225. $this->extractVersion = max(
  226. $this->extractVersion,
  227. $this->isDirectory ?
  228. ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO :
  229. ZipVersion::v10_DEFAULT_MIN
  230. );
  231. }
  232. return $this;
  233. }
  234. /**
  235. * @param string|null $charset
  236. *
  237. * @return ZipEntry
  238. *
  239. * @see DosCodePage::getCodePages()
  240. */
  241. public function setCharset($charset = null)
  242. {
  243. if ($charset !== null && $charset === '') {
  244. throw new InvalidArgumentException('Empty charset');
  245. }
  246. $this->charset = $charset;
  247. return $this;
  248. }
  249. /**
  250. * @return string|null
  251. */
  252. public function getCharset()
  253. {
  254. return $this->charset;
  255. }
  256. /**
  257. * @param string $newName New entry name
  258. *
  259. * @return ZipEntry new {@see ZipEntry} object with new name
  260. *
  261. * @internal
  262. */
  263. public function rename($newName)
  264. {
  265. $newEntry = clone $this;
  266. $newEntry->setName($newName);
  267. $newEntry->removeExtraField(UnicodePathExtraField::HEADER_ID);
  268. return $newEntry;
  269. }
  270. /**
  271. * Returns the ZIP entry name.
  272. *
  273. * @return string
  274. */
  275. public function getName()
  276. {
  277. return $this->name;
  278. }
  279. /**
  280. * @return ZipData|null
  281. *
  282. * @internal
  283. */
  284. public function getData()
  285. {
  286. return $this->data;
  287. }
  288. /**
  289. * @param ZipData|null $data
  290. *
  291. * @internal
  292. */
  293. public function setData($data)
  294. {
  295. $this->data = $data;
  296. }
  297. /**
  298. * @return int Get platform
  299. *
  300. * @deprecated Use {@see ZipEntry::getCreatedOS()}
  301. */
  302. public function getPlatform()
  303. {
  304. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::getCreatedOS()', \E_USER_DEPRECATED);
  305. return $this->getCreatedOS();
  306. }
  307. /**
  308. * @param int $platform
  309. *
  310. * @return ZipEntry
  311. *
  312. * @deprecated Use {@see ZipEntry::setCreatedOS()}
  313. */
  314. public function setPlatform($platform)
  315. {
  316. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::setCreatedOS()', \E_USER_DEPRECATED);
  317. return $this->setCreatedOS($platform);
  318. }
  319. /**
  320. * @return int platform
  321. */
  322. public function getCreatedOS()
  323. {
  324. return $this->createdOS;
  325. }
  326. /**
  327. * Set platform.
  328. *
  329. * @param int $platform
  330. *
  331. * @return ZipEntry
  332. */
  333. public function setCreatedOS($platform)
  334. {
  335. $platform = (int) $platform;
  336. if ($platform < 0x00 || $platform > 0xff) {
  337. throw new InvalidArgumentException('Platform out of range');
  338. }
  339. $this->createdOS = $platform;
  340. return $this;
  341. }
  342. /**
  343. * @return int
  344. */
  345. public function getExtractedOS()
  346. {
  347. return $this->extractedOS;
  348. }
  349. /**
  350. * Set extracted OS.
  351. *
  352. * @param int $platform
  353. *
  354. * @return ZipEntry
  355. */
  356. public function setExtractedOS($platform)
  357. {
  358. $platform = (int) $platform;
  359. if ($platform < 0x00 || $platform > 0xff) {
  360. throw new InvalidArgumentException('Platform out of range');
  361. }
  362. $this->extractedOS = $platform;
  363. return $this;
  364. }
  365. /**
  366. * @return int
  367. */
  368. public function getSoftwareVersion()
  369. {
  370. if ($this->softwareVersion === self::UNKNOWN) {
  371. return $this->getExtractVersion();
  372. }
  373. return $this->softwareVersion;
  374. }
  375. /**
  376. * @param int $softwareVersion
  377. *
  378. * @return ZipEntry
  379. */
  380. public function setSoftwareVersion($softwareVersion)
  381. {
  382. $this->softwareVersion = (int) $softwareVersion;
  383. return $this;
  384. }
  385. /**
  386. * Version needed to extract.
  387. *
  388. * @return int
  389. *
  390. * @deprecated Use {@see ZipEntry::getExtractVersion()}
  391. */
  392. public function getVersionNeededToExtract()
  393. {
  394. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::getExtractVersion()', \E_USER_DEPRECATED);
  395. return $this->getExtractVersion();
  396. }
  397. /**
  398. * Version needed to extract.
  399. *
  400. * @return int
  401. */
  402. public function getExtractVersion()
  403. {
  404. if ($this->extractVersion === self::UNKNOWN) {
  405. if (ZipEncryptionMethod::isWinZipAesMethod($this->encryptionMethod)) {
  406. return ZipVersion::v51_ENCR_AES_RC2_CORRECT;
  407. }
  408. if ($this->compressionMethod === ZipCompressionMethod::BZIP2) {
  409. return ZipVersion::v46_BZIP2;
  410. }
  411. if ($this->isZip64ExtensionsRequired()) {
  412. return ZipVersion::v45_ZIP64_EXT;
  413. }
  414. if (
  415. $this->compressionMethod === ZipCompressionMethod::DEFLATED ||
  416. $this->isDirectory ||
  417. $this->encryptionMethod === ZipEncryptionMethod::PKWARE
  418. ) {
  419. return ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO;
  420. }
  421. return ZipVersion::v10_DEFAULT_MIN;
  422. }
  423. return $this->extractVersion;
  424. }
  425. /**
  426. * Set version needed to extract.
  427. *
  428. * @param int $version
  429. *
  430. * @return ZipEntry
  431. *
  432. * @deprecated Use {@see ZipEntry::setExtractVersion()}
  433. */
  434. public function setVersionNeededToExtract($version)
  435. {
  436. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::setExtractVersion()', \E_USER_DEPRECATED);
  437. return $this->setExtractVersion($version);
  438. }
  439. /**
  440. * Set version needed to extract.
  441. *
  442. * @param int $version
  443. *
  444. * @return ZipEntry
  445. */
  446. public function setExtractVersion($version)
  447. {
  448. $this->extractVersion = max(ZipVersion::v10_DEFAULT_MIN, (int) $version);
  449. return $this;
  450. }
  451. /**
  452. * Returns the compressed size of this entry.
  453. *
  454. * @return int
  455. */
  456. public function getCompressedSize()
  457. {
  458. return $this->compressedSize;
  459. }
  460. /**
  461. * Sets the compressed size of this entry.
  462. *
  463. * @param int $compressedSize the Compressed Size
  464. *
  465. * @return ZipEntry
  466. *
  467. * @internal
  468. */
  469. public function setCompressedSize($compressedSize)
  470. {
  471. $compressedSize = (int) $compressedSize;
  472. if ($compressedSize < self::UNKNOWN) {
  473. throw new InvalidArgumentException('Compressed size < ' . self::UNKNOWN);
  474. }
  475. $this->compressedSize = $compressedSize;
  476. return $this;
  477. }
  478. /**
  479. * Returns the uncompressed size of this entry.
  480. *
  481. * @return int
  482. *
  483. * @deprecated Use {@see ZipEntry::getUncompressedSize()}
  484. */
  485. public function getSize()
  486. {
  487. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::getUncompressedSize()', \E_USER_DEPRECATED);
  488. return $this->getUncompressedSize();
  489. }
  490. /**
  491. * Sets the uncompressed size of this entry.
  492. *
  493. * @param int $size the (Uncompressed) Size
  494. *
  495. * @return ZipEntry
  496. *
  497. * @deprecated Use {@see ZipEntry::setUncompressedSize()}
  498. *
  499. * @internal
  500. */
  501. public function setSize($size)
  502. {
  503. @trigger_error(__METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::setUncompressedSize()', \E_USER_DEPRECATED);
  504. return $this->setUncompressedSize($size);
  505. }
  506. /**
  507. * Returns the uncompressed size of this entry.
  508. *
  509. * @return int
  510. */
  511. public function getUncompressedSize()
  512. {
  513. return $this->uncompressedSize;
  514. }
  515. /**
  516. * Sets the uncompressed size of this entry.
  517. *
  518. * @param int $uncompressedSize the (Uncompressed) Size
  519. *
  520. * @return ZipEntry
  521. *
  522. * @internal
  523. */
  524. public function setUncompressedSize($uncompressedSize)
  525. {
  526. $uncompressedSize = (int) $uncompressedSize;
  527. if ($uncompressedSize < self::UNKNOWN) {
  528. throw new InvalidArgumentException('Uncompressed size < ' . self::UNKNOWN);
  529. }
  530. $this->uncompressedSize = $uncompressedSize;
  531. return $this;
  532. }
  533. /**
  534. * Return relative Offset Of Local File Header.
  535. *
  536. * @return int
  537. */
  538. public function getLocalHeaderOffset()
  539. {
  540. return $this->localHeaderOffset;
  541. }
  542. /**
  543. * @param int $localHeaderOffset
  544. *
  545. * @return ZipEntry
  546. *
  547. * @internal
  548. */
  549. public function setLocalHeaderOffset($localHeaderOffset)
  550. {
  551. $localHeaderOffset = (int) $localHeaderOffset;
  552. if ($localHeaderOffset < 0) {
  553. throw new InvalidArgumentException('Negative $localHeaderOffset');
  554. }
  555. $this->localHeaderOffset = $localHeaderOffset;
  556. return $this;
  557. }
  558. /**
  559. * Return relative Offset Of Local File Header.
  560. *
  561. * @return int
  562. *
  563. * @deprecated Use {@see ZipEntry::getLocalHeaderOffset()}
  564. */
  565. public function getOffset()
  566. {
  567. @trigger_error(
  568. __METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::getLocalHeaderOffset()',
  569. \E_USER_DEPRECATED
  570. );
  571. return $this->getLocalHeaderOffset();
  572. }
  573. /**
  574. * @param int $offset
  575. *
  576. * @return ZipEntry
  577. *
  578. * @deprecated Use {@see ZipEntry::setLocalHeaderOffset()}
  579. *
  580. * @internal
  581. */
  582. public function setOffset($offset)
  583. {
  584. @trigger_error(
  585. __METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::setLocalHeaderOffset()',
  586. \E_USER_DEPRECATED
  587. );
  588. return $this->setLocalHeaderOffset($offset);
  589. }
  590. /**
  591. * Returns the General Purpose Bit Flags.
  592. *
  593. * @return int
  594. */
  595. public function getGeneralPurposeBitFlags()
  596. {
  597. return $this->generalPurposeBitFlags;
  598. }
  599. /**
  600. * Sets the General Purpose Bit Flags.
  601. *
  602. * @param int $gpbf general purpose bit flags
  603. *
  604. * @return ZipEntry
  605. *
  606. * @internal
  607. */
  608. public function setGeneralPurposeBitFlags($gpbf)
  609. {
  610. $gpbf = (int) $gpbf;
  611. if ($gpbf < 0x0000 || $gpbf > 0xffff) {
  612. throw new InvalidArgumentException('general purpose bit flags out of range');
  613. }
  614. $this->generalPurposeBitFlags = $gpbf;
  615. $this->updateCompressionLevel();
  616. return $this;
  617. }
  618. private function updateCompressionLevel()
  619. {
  620. if ($this->compressionMethod === ZipCompressionMethod::DEFLATED) {
  621. $bit1 = $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::COMPRESSION_FLAG1);
  622. $bit2 = $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::COMPRESSION_FLAG2);
  623. if ($bit1 && !$bit2) {
  624. $this->compressionLevel = ZipCompressionLevel::MAXIMUM;
  625. } elseif (!$bit1 && $bit2) {
  626. $this->compressionLevel = ZipCompressionLevel::FAST;
  627. } elseif ($bit1 && $bit2) {
  628. $this->compressionLevel = ZipCompressionLevel::SUPER_FAST;
  629. } else {
  630. $this->compressionLevel = ZipCompressionLevel::NORMAL;
  631. }
  632. }
  633. }
  634. /**
  635. * @param int $mask
  636. * @param bool $enable
  637. *
  638. * @return ZipEntry
  639. */
  640. private function setGeneralBitFlag($mask, $enable)
  641. {
  642. if ($enable) {
  643. $this->generalPurposeBitFlags |= $mask;
  644. } else {
  645. $this->generalPurposeBitFlags &= ~$mask;
  646. }
  647. return $this;
  648. }
  649. /**
  650. * @param int $mask
  651. *
  652. * @return bool
  653. */
  654. private function isSetGeneralBitFlag($mask)
  655. {
  656. return ($this->generalPurposeBitFlags & $mask) === $mask;
  657. }
  658. /**
  659. * @return bool
  660. */
  661. public function isDataDescriptorEnabled()
  662. {
  663. return $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::DATA_DESCRIPTOR);
  664. }
  665. /**
  666. * Enabling or disabling the use of the Data Descriptor block.
  667. *
  668. * @param bool $enabled
  669. */
  670. public function enableDataDescriptor($enabled = true)
  671. {
  672. $this->setGeneralBitFlag(GeneralPurposeBitFlag::DATA_DESCRIPTOR, (bool) $enabled);
  673. }
  674. /**
  675. * @param bool $enabled
  676. */
  677. public function enableUtf8Name($enabled)
  678. {
  679. $this->setGeneralBitFlag(GeneralPurposeBitFlag::UTF8, (bool) $enabled);
  680. }
  681. /**
  682. * @return bool
  683. */
  684. public function isUtf8Flag()
  685. {
  686. return $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::UTF8);
  687. }
  688. /**
  689. * Returns true if and only if this ZIP entry is encrypted.
  690. *
  691. * @return bool
  692. */
  693. public function isEncrypted()
  694. {
  695. return $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::ENCRYPTION);
  696. }
  697. /**
  698. * @return bool
  699. */
  700. public function isStrongEncryption()
  701. {
  702. return $this->isSetGeneralBitFlag(GeneralPurposeBitFlag::STRONG_ENCRYPTION);
  703. }
  704. /**
  705. * Sets the encryption property to false and removes any other
  706. * encryption artifacts.
  707. *
  708. * @return ZipEntry
  709. */
  710. public function disableEncryption()
  711. {
  712. $this->setEncrypted(false);
  713. $this->removeExtraField(WinZipAesExtraField::HEADER_ID);
  714. $this->encryptionMethod = ZipEncryptionMethod::NONE;
  715. $this->password = null;
  716. $this->extractVersion = self::UNKNOWN;
  717. return $this;
  718. }
  719. /**
  720. * Sets the encryption flag for this ZIP entry.
  721. *
  722. * @param bool $encrypted
  723. *
  724. * @return ZipEntry
  725. */
  726. private function setEncrypted($encrypted)
  727. {
  728. $encrypted = (bool) $encrypted;
  729. $this->setGeneralBitFlag(GeneralPurposeBitFlag::ENCRYPTION, $encrypted);
  730. return $this;
  731. }
  732. /**
  733. * Returns the compression method for this entry.
  734. *
  735. * @return int
  736. *
  737. * @deprecated Use {@see ZipEntry::getCompressionMethod()}
  738. */
  739. public function getMethod()
  740. {
  741. @trigger_error(
  742. __METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::getCompressionMethod()',
  743. \E_USER_DEPRECATED
  744. );
  745. return $this->getCompressionMethod();
  746. }
  747. /**
  748. * Returns the compression method for this entry.
  749. *
  750. * @return int
  751. */
  752. public function getCompressionMethod()
  753. {
  754. return $this->compressionMethod;
  755. }
  756. /**
  757. * Sets the compression method for this entry.
  758. *
  759. * @param int $method
  760. *
  761. * @throws ZipUnsupportMethodException
  762. *
  763. * @return ZipEntry
  764. *
  765. * @deprecated Use {@see ZipEntry::setCompressionMethod()}
  766. */
  767. public function setMethod($method)
  768. {
  769. @trigger_error(
  770. __METHOD__ . ' is deprecated. Use ' . __CLASS__ . '::setCompressionMethod()',
  771. \E_USER_DEPRECATED
  772. );
  773. return $this->setCompressionMethod($method);
  774. }
  775. /**
  776. * Sets the compression method for this entry.
  777. *
  778. * @param int $compressionMethod
  779. *
  780. * @throws ZipUnsupportMethodException
  781. *
  782. * @return ZipEntry
  783. *
  784. * @see ZipCompressionMethod::STORED
  785. * @see ZipCompressionMethod::DEFLATED
  786. * @see ZipCompressionMethod::BZIP2
  787. */
  788. public function setCompressionMethod($compressionMethod)
  789. {
  790. $compressionMethod = (int) $compressionMethod;
  791. if ($compressionMethod < 0x0000 || $compressionMethod > 0xffff) {
  792. throw new InvalidArgumentException('method out of range: ' . $compressionMethod);
  793. }
  794. ZipCompressionMethod::checkSupport($compressionMethod);
  795. $this->compressionMethod = $compressionMethod;
  796. $this->updateCompressionLevel();
  797. $this->extractVersion = self::UNKNOWN;
  798. return $this;
  799. }
  800. /**
  801. * Get Unix Timestamp.
  802. *
  803. * @return int
  804. */
  805. public function getTime()
  806. {
  807. if ($this->getDosTime() === self::UNKNOWN) {
  808. return self::UNKNOWN;
  809. }
  810. return DateTimeConverter::msDosToUnix($this->getDosTime());
  811. }
  812. /**
  813. * Get Dos Time.
  814. *
  815. * @return int
  816. */
  817. public function getDosTime()
  818. {
  819. return $this->dosTime;
  820. }
  821. /**
  822. * Set Dos Time.
  823. *
  824. * @param int $dosTime
  825. *
  826. * @return ZipEntry
  827. */
  828. public function setDosTime($dosTime)
  829. {
  830. $dosTime = (int) $dosTime;
  831. if (\PHP_INT_SIZE === 8) {
  832. if ($dosTime < 0x00000000 || $dosTime > 0xffffffff) {
  833. throw new InvalidArgumentException('DosTime out of range');
  834. }
  835. }
  836. $this->dosTime = $dosTime;
  837. return $this;
  838. }
  839. /**
  840. * Set time from unix timestamp.
  841. *
  842. * @param int $unixTimestamp
  843. *
  844. * @return ZipEntry
  845. */
  846. public function setTime($unixTimestamp)
  847. {
  848. if ($unixTimestamp !== self::UNKNOWN) {
  849. $this->setDosTime(DateTimeConverter::unixToMsDos($unixTimestamp));
  850. } else {
  851. $this->dosTime = 0;
  852. }
  853. return $this;
  854. }
  855. /**
  856. * Returns the external file attributes.
  857. *
  858. * @return int the external file attributes
  859. */
  860. public function getExternalAttributes()
  861. {
  862. return $this->externalAttributes;
  863. }
  864. /**
  865. * Sets the external file attributes.
  866. *
  867. * @param int $externalAttributes the external file attributes
  868. *
  869. * @return ZipEntry
  870. */
  871. public function setExternalAttributes($externalAttributes)
  872. {
  873. $this->externalAttributes = (int) $externalAttributes;
  874. if (\PHP_INT_SIZE === 8) {
  875. if ($externalAttributes < 0x00000000 || $externalAttributes > 0xffffffff) {
  876. throw new InvalidArgumentException('external attributes out of range: ' . $externalAttributes);
  877. }
  878. }
  879. $this->externalAttributes = $externalAttributes;
  880. return $this;
  881. }
  882. /**
  883. * Returns the internal file attributes.
  884. *
  885. * @return int the internal file attributes
  886. */
  887. public function getInternalAttributes()
  888. {
  889. return $this->internalAttributes;
  890. }
  891. /**
  892. * Sets the internal file attributes.
  893. *
  894. * @param int $internalAttributes the internal file attributes
  895. *
  896. * @return ZipEntry
  897. */
  898. public function setInternalAttributes($internalAttributes)
  899. {
  900. $internalAttributes = (int) $internalAttributes;
  901. if ($internalAttributes < 0x0000 || $internalAttributes > 0xffff) {
  902. throw new InvalidArgumentException('internal attributes out of range');
  903. }
  904. $this->internalAttributes = $internalAttributes;
  905. return $this;
  906. }
  907. /**
  908. * Returns true if and only if this ZIP entry represents a directory entry
  909. * (i.e. end with '/').
  910. *
  911. * @return bool
  912. */
  913. final public function isDirectory()
  914. {
  915. return $this->isDirectory;
  916. }
  917. /**
  918. * @return ExtraFieldsCollection
  919. */
  920. public function getCdExtraFields()
  921. {
  922. return $this->cdExtraFields;
  923. }
  924. /**
  925. * @param int $headerId
  926. *
  927. * @return ZipExtraField|null
  928. */
  929. public function getCdExtraField($headerId)
  930. {
  931. return $this->cdExtraFields->get((int) $headerId);
  932. }
  933. /**
  934. * @param ExtraFieldsCollection $cdExtraFields
  935. *
  936. * @return ZipEntry
  937. */
  938. public function setCdExtraFields(ExtraFieldsCollection $cdExtraFields)
  939. {
  940. $this->cdExtraFields = $cdExtraFields;
  941. return $this;
  942. }
  943. /**
  944. * @return ExtraFieldsCollection
  945. */
  946. public function getLocalExtraFields()
  947. {
  948. return $this->localExtraFields;
  949. }
  950. /**
  951. * @param int $headerId
  952. *
  953. * @return ZipExtraField|null
  954. */
  955. public function getLocalExtraField($headerId)
  956. {
  957. return $this->localExtraFields[(int) $headerId];
  958. }
  959. /**
  960. * @param ExtraFieldsCollection $localExtraFields
  961. *
  962. * @return ZipEntry
  963. */
  964. public function setLocalExtraFields(ExtraFieldsCollection $localExtraFields)
  965. {
  966. $this->localExtraFields = $localExtraFields;
  967. return $this;
  968. }
  969. /**
  970. * @param int $headerId
  971. *
  972. * @return ZipExtraField|null
  973. */
  974. public function getExtraField($headerId)
  975. {
  976. $headerId = (int) $headerId;
  977. $local = $this->getLocalExtraField($headerId);
  978. if ($local === null) {
  979. return $this->getCdExtraField($headerId);
  980. }
  981. return $local;
  982. }
  983. /**
  984. * @param int $headerId
  985. *
  986. * @return bool
  987. */
  988. public function hasExtraField($headerId)
  989. {
  990. $headerId = (int) $headerId;
  991. return
  992. isset($this->localExtraFields[$headerId]) ||
  993. isset($this->cdExtraFields[$headerId]);
  994. }
  995. /**
  996. * @param int $headerId
  997. */
  998. public function removeExtraField($headerId)
  999. {
  1000. $headerId = (int) $headerId;
  1001. $this->cdExtraFields->remove($headerId);
  1002. $this->localExtraFields->remove($headerId);
  1003. }
  1004. /**
  1005. * @param ZipExtraField $zipExtraField
  1006. */
  1007. public function addExtraField(ZipExtraField $zipExtraField)
  1008. {
  1009. $this->addLocalExtraField($zipExtraField);
  1010. $this->addCdExtraField($zipExtraField);
  1011. }
  1012. /**
  1013. * @param ZipExtraField $zipExtraField
  1014. */
  1015. public function addLocalExtraField(ZipExtraField $zipExtraField)
  1016. {
  1017. $this->localExtraFields->add($zipExtraField);
  1018. }
  1019. /**
  1020. * @param ZipExtraField $zipExtraField
  1021. */
  1022. public function addCdExtraField(ZipExtraField $zipExtraField)
  1023. {
  1024. $this->cdExtraFields->add($zipExtraField);
  1025. }
  1026. /**
  1027. * Returns comment entry.
  1028. *
  1029. * @return string
  1030. */
  1031. public function getComment()
  1032. {
  1033. return $this->comment !== null ? $this->comment : '';
  1034. }
  1035. /**
  1036. * Set entry comment.
  1037. *
  1038. * @param string|null $comment
  1039. *
  1040. * @return ZipEntry
  1041. */
  1042. public function setComment($comment)
  1043. {
  1044. if ($comment !== null) {
  1045. $commentLength = \strlen($comment);
  1046. if ($commentLength > 0xffff) {
  1047. throw new InvalidArgumentException('Comment too long');
  1048. }
  1049. if ($this->charset === null && !StringUtil::isASCII($comment)) {
  1050. $this->enableUtf8Name(true);
  1051. }
  1052. }
  1053. $this->comment = $comment;
  1054. return $this;
  1055. }
  1056. /**
  1057. * @return bool
  1058. */
  1059. public function isDataDescriptorRequired()
  1060. {
  1061. return ($this->getCrc() | $this->getCompressedSize() | $this->getUncompressedSize()) === self::UNKNOWN;
  1062. }
  1063. /**
  1064. * Return crc32 content or 0 for WinZip AES v2.
  1065. *
  1066. * @return int
  1067. */
  1068. public function getCrc()
  1069. {
  1070. return $this->crc;
  1071. }
  1072. /**
  1073. * Set crc32 content.
  1074. *
  1075. * @param int $crc
  1076. *
  1077. * @return ZipEntry
  1078. *
  1079. * @internal
  1080. */
  1081. public function setCrc($crc)
  1082. {
  1083. $this->crc = (int) $crc;
  1084. return $this;
  1085. }
  1086. /**
  1087. * @return string|null
  1088. */
  1089. public function getPassword()
  1090. {
  1091. return $this->password;
  1092. }
  1093. /**
  1094. * Set password and encryption method from entry.
  1095. *
  1096. * @param string|null $password
  1097. * @param int|null $encryptionMethod
  1098. *
  1099. * @return ZipEntry
  1100. */
  1101. public function setPassword($password, $encryptionMethod = null)
  1102. {
  1103. if (!$this->isDirectory) {
  1104. if ($password === null || $password === '') {
  1105. $this->password = null;
  1106. $this->disableEncryption();
  1107. } else {
  1108. $this->password = (string) $password;
  1109. if ($encryptionMethod === null && $this->encryptionMethod === ZipEncryptionMethod::NONE) {
  1110. $encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256;
  1111. }
  1112. if ($encryptionMethod !== null) {
  1113. $this->setEncryptionMethod($encryptionMethod);
  1114. }
  1115. $this->setEncrypted(true);
  1116. }
  1117. }
  1118. return $this;
  1119. }
  1120. /**
  1121. * @return int
  1122. */
  1123. public function getEncryptionMethod()
  1124. {
  1125. return $this->encryptionMethod;
  1126. }
  1127. /**
  1128. * Set encryption method.
  1129. *
  1130. * @param int|null $encryptionMethod
  1131. *
  1132. * @return ZipEntry
  1133. *
  1134. * @see ZipEncryptionMethod::NONE
  1135. * @see ZipEncryptionMethod::PKWARE
  1136. * @see ZipEncryptionMethod::WINZIP_AES_256
  1137. * @see ZipEncryptionMethod::WINZIP_AES_192
  1138. * @see ZipEncryptionMethod::WINZIP_AES_128
  1139. */
  1140. public function setEncryptionMethod($encryptionMethod)
  1141. {
  1142. if ($encryptionMethod === null) {
  1143. $encryptionMethod = ZipEncryptionMethod::NONE;
  1144. }
  1145. $encryptionMethod = (int) $encryptionMethod;
  1146. ZipEncryptionMethod::checkSupport($encryptionMethod);
  1147. $this->encryptionMethod = $encryptionMethod;
  1148. $this->setEncrypted($this->encryptionMethod !== ZipEncryptionMethod::NONE);
  1149. $this->extractVersion = self::UNKNOWN;
  1150. return $this;
  1151. }
  1152. /**
  1153. * @return int
  1154. */
  1155. public function getCompressionLevel()
  1156. {
  1157. return $this->compressionLevel;
  1158. }
  1159. /**
  1160. * @param int $compressionLevel
  1161. *
  1162. * @return ZipEntry
  1163. */
  1164. public function setCompressionLevel($compressionLevel)
  1165. {
  1166. $compressionLevel = (int) $compressionLevel;
  1167. if ($compressionLevel === self::UNKNOWN) {
  1168. $compressionLevel = ZipCompressionLevel::NORMAL;
  1169. }
  1170. if (
  1171. $compressionLevel < ZipCompressionLevel::LEVEL_MIN ||
  1172. $compressionLevel > ZipCompressionLevel::LEVEL_MAX
  1173. ) {
  1174. throw new InvalidArgumentException(
  1175. 'Invalid compression level. Minimum level ' .
  1176. ZipCompressionLevel::LEVEL_MIN . '. Maximum level ' . ZipCompressionLevel::LEVEL_MAX
  1177. );
  1178. }
  1179. $this->compressionLevel = $compressionLevel;
  1180. $this->updateGbpfCompLevel();
  1181. return $this;
  1182. }
  1183. /**
  1184. * Update general purpose bit flogs.
  1185. */
  1186. private function updateGbpfCompLevel()
  1187. {
  1188. if ($this->compressionMethod === ZipCompressionMethod::DEFLATED) {
  1189. $bit1 = false;
  1190. $bit2 = false;
  1191. switch ($this->compressionLevel) {
  1192. case ZipCompressionLevel::MAXIMUM:
  1193. $bit1 = true;
  1194. break;
  1195. case ZipCompressionLevel::FAST:
  1196. $bit2 = true;
  1197. break;
  1198. case ZipCompressionLevel::SUPER_FAST:
  1199. $bit1 = true;
  1200. $bit2 = true;
  1201. break;
  1202. // default is ZipCompressionLevel::NORMAL
  1203. }
  1204. $this->generalPurposeBitFlags |= ($bit1 ? GeneralPurposeBitFlag::COMPRESSION_FLAG1 : 0);
  1205. $this->generalPurposeBitFlags |= ($bit2 ? GeneralPurposeBitFlag::COMPRESSION_FLAG2 : 0);
  1206. }
  1207. }
  1208. /**
  1209. * Sets Unix permissions in a way that is understood by Info-Zip's
  1210. * unzip command.
  1211. *
  1212. * @param int $mode mode an int value
  1213. *
  1214. * @return ZipEntry
  1215. */
  1216. public function setUnixMode($mode)
  1217. {
  1218. $mode = (int) $mode;
  1219. $this->setExternalAttributes(
  1220. ($mode << 16)
  1221. // MS-DOS read-only attribute
  1222. | (($mode & UnixStat::UNX_IWUSR) === 0 ? DosAttrs::DOS_HIDDEN : 0)
  1223. // MS-DOS directory flag
  1224. | ($this->isDirectory() ? DosAttrs::DOS_DIRECTORY : DosAttrs::DOS_ARCHIVE)
  1225. );
  1226. $this->createdOS = ZipPlatform::OS_UNIX;
  1227. return $this;
  1228. }
  1229. /**
  1230. * Unix permission.
  1231. *
  1232. * @return int the unix permissions
  1233. */
  1234. public function getUnixMode()
  1235. {
  1236. $mode = 0;
  1237. if ($this->createdOS === ZipPlatform::OS_UNIX) {
  1238. $mode = ($this->externalAttributes >> 16) & 0xFFFF;
  1239. } elseif ($this->hasExtraField(AsiExtraField::HEADER_ID)) {
  1240. /** @var AsiExtraField $asiExtraField */
  1241. $asiExtraField = $this->getExtraField(AsiExtraField::HEADER_ID);
  1242. $mode = $asiExtraField->getMode();
  1243. }
  1244. if ($mode > 0) {
  1245. return $mode;
  1246. }
  1247. return $this->isDirectory ? 040755 : 0100644;
  1248. }
  1249. /**
  1250. * Offset MUST be considered in decision about ZIP64 format - see
  1251. * description of Data Descriptor in ZIP File Format Specification.
  1252. *
  1253. * @return bool
  1254. */
  1255. public function isZip64ExtensionsRequired()
  1256. {
  1257. return $this->compressedSize > ZipConstants::ZIP64_MAGIC
  1258. || $this->uncompressedSize > ZipConstants::ZIP64_MAGIC;
  1259. }
  1260. /**
  1261. * Returns true if this entry represents a unix symlink,
  1262. * in which case the entry's content contains the target path
  1263. * for the symlink.
  1264. *
  1265. * @return bool true if the entry represents a unix symlink,
  1266. * false otherwise
  1267. */
  1268. public function isUnixSymlink()
  1269. {
  1270. return ($this->getUnixMode() & UnixStat::UNX_IFMT) === UnixStat::UNX_IFLNK;
  1271. }
  1272. /**
  1273. * @return \DateTimeInterface
  1274. */
  1275. public function getMTime()
  1276. {
  1277. /** @var NtfsExtraField|null $ntfsExtra */
  1278. $ntfsExtra = $this->getExtraField(NtfsExtraField::HEADER_ID);
  1279. if ($ntfsExtra !== null) {
  1280. return $ntfsExtra->getModifyDateTime();
  1281. }
  1282. /** @var ExtendedTimestampExtraField|null $extendedExtra */
  1283. $extendedExtra = $this->getExtraField(ExtendedTimestampExtraField::HEADER_ID);
  1284. if ($extendedExtra !== null && ($mtime = $extendedExtra->getModifyDateTime()) !== null) {
  1285. return $mtime;
  1286. }
  1287. /** @var OldUnixExtraField|null $oldUnixExtra */
  1288. $oldUnixExtra = $this->getExtraField(OldUnixExtraField::HEADER_ID);
  1289. if ($oldUnixExtra !== null && ($mtime = $oldUnixExtra->getModifyDateTime()) !== null) {
  1290. return $mtime;
  1291. }
  1292. $timestamp = $this->getTime();
  1293. try {
  1294. return new \DateTimeImmutable('@' . $timestamp);
  1295. } catch (\Exception $e) {
  1296. throw new RuntimeException('Error create DateTime object with timestamp ' . $timestamp, 1, $e);
  1297. }
  1298. }
  1299. /**
  1300. * @return \DateTimeInterface|null
  1301. */
  1302. public function getATime()
  1303. {
  1304. /** @var NtfsExtraField|null $ntfsExtra */
  1305. $ntfsExtra = $this->getExtraField(NtfsExtraField::HEADER_ID);
  1306. if ($ntfsExtra !== null) {
  1307. return $ntfsExtra->getAccessDateTime();
  1308. }
  1309. /** @var ExtendedTimestampExtraField|null $extendedExtra */
  1310. $extendedExtra = $this->getExtraField(ExtendedTimestampExtraField::HEADER_ID);
  1311. if ($extendedExtra !== null && ($atime = $extendedExtra->getAccessDateTime()) !== null) {
  1312. return $atime;
  1313. }
  1314. /** @var OldUnixExtraField|null $oldUnixExtra */
  1315. $oldUnixExtra = $this->getExtraField(OldUnixExtraField::HEADER_ID);
  1316. if ($oldUnixExtra !== null) {
  1317. return $oldUnixExtra->getAccessDateTime();
  1318. }
  1319. return null;
  1320. }
  1321. /**
  1322. * @return \DateTimeInterface|null
  1323. */
  1324. public function getCTime()
  1325. {
  1326. /** @var NtfsExtraField|null $ntfsExtra */
  1327. $ntfsExtra = $this->getExtraField(NtfsExtraField::HEADER_ID);
  1328. if ($ntfsExtra !== null) {
  1329. return $ntfsExtra->getCreateDateTime();
  1330. }
  1331. /** @var ExtendedTimestampExtraField|null $extendedExtra */
  1332. $extendedExtra = $this->getExtraField(ExtendedTimestampExtraField::HEADER_ID);
  1333. if ($extendedExtra !== null) {
  1334. return $extendedExtra->getCreateDateTime();
  1335. }
  1336. return null;
  1337. }
  1338. public function __clone()
  1339. {
  1340. $this->cdExtraFields = clone $this->cdExtraFields;
  1341. $this->localExtraFields = clone $this->localExtraFields;
  1342. if ($this->data !== null) {
  1343. $this->data = clone $this->data;
  1344. }
  1345. }
  1346. }