module.archive.zip.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.archive.zip.php //
  11. // module for analyzing pkZip files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_zip extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $info['fileformat'] = 'zip';
  26. $info['zip']['encoding'] = 'ISO-8859-1';
  27. $info['zip']['files'] = array();
  28. $info['zip']['compressed_size'] = 0;
  29. $info['zip']['uncompressed_size'] = 0;
  30. $info['zip']['entries_count'] = 0;
  31. if (!getid3_lib::intValueSupported($info['filesize'])) {
  32. $this->error('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB, not supported by PHP');
  33. return false;
  34. } else {
  35. $EOCDsearchData = '';
  36. $EOCDsearchCounter = 0;
  37. while ($EOCDsearchCounter++ < 512) {
  38. $this->fseek(-128 * $EOCDsearchCounter, SEEK_END);
  39. $EOCDsearchData = $this->fread(128).$EOCDsearchData;
  40. if (strstr($EOCDsearchData, 'PK'."\x05\x06")) {
  41. $EOCDposition = strpos($EOCDsearchData, 'PK'."\x05\x06");
  42. $this->fseek((-128 * $EOCDsearchCounter) + $EOCDposition, SEEK_END);
  43. $info['zip']['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory();
  44. $this->fseek($info['zip']['end_central_directory']['directory_offset']);
  45. $info['zip']['entries_count'] = 0;
  46. while ($centraldirectoryentry = $this->ZIPparseCentralDirectory()) {
  47. $info['zip']['central_directory'][] = $centraldirectoryentry;
  48. $info['zip']['entries_count']++;
  49. $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
  50. $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
  51. //if ($centraldirectoryentry['uncompressed_size'] > 0) { zero-byte files are valid
  52. if (!empty($centraldirectoryentry['filename'])) {
  53. $info['zip']['files'] = getid3_lib::array_merge_clobber($info['zip']['files'], getid3_lib::CreateDeepArray($centraldirectoryentry['filename'], '/', $centraldirectoryentry['uncompressed_size']));
  54. }
  55. }
  56. if ($info['zip']['entries_count'] == 0) {
  57. $this->error('No Central Directory entries found (truncated file?)');
  58. return false;
  59. }
  60. if (!empty($info['zip']['end_central_directory']['comment'])) {
  61. $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
  62. }
  63. if (isset($info['zip']['central_directory'][0]['compression_method'])) {
  64. $info['zip']['compression_method'] = $info['zip']['central_directory'][0]['compression_method'];
  65. }
  66. if (isset($info['zip']['central_directory'][0]['flags']['compression_speed'])) {
  67. $info['zip']['compression_speed'] = $info['zip']['central_directory'][0]['flags']['compression_speed'];
  68. }
  69. if (isset($info['zip']['compression_method']) && ($info['zip']['compression_method'] == 'store') && !isset($info['zip']['compression_speed'])) {
  70. $info['zip']['compression_speed'] = 'store';
  71. }
  72. // secondary check - we (should) already have all the info we NEED from the Central Directory above, but scanning each
  73. // Local File Header entry will
  74. foreach ($info['zip']['central_directory'] as $central_directory_entry) {
  75. $this->fseek($central_directory_entry['entry_offset']);
  76. if ($fileentry = $this->ZIPparseLocalFileHeader()) {
  77. $info['zip']['entries'][] = $fileentry;
  78. } else {
  79. $this->warning('Error parsing Local File Header at offset '.$central_directory_entry['entry_offset']);
  80. }
  81. }
  82. // check for EPUB files
  83. if (!empty($info['zip']['entries'][0]['filename']) &&
  84. ($info['zip']['entries'][0]['filename'] == 'mimetype') &&
  85. ($info['zip']['entries'][0]['compression_method'] == 'store') &&
  86. ($info['zip']['entries'][0]['uncompressed_size'] == 20) &&
  87. isset($info['zip']['entries'][0]['data_offset'])) {
  88. // http://idpf.org/epub/30/spec/epub30-ocf.html
  89. // "3.3 OCF ZIP Container Media Type Identification
  90. // OCF ZIP Containers must include a mimetype file as the first file in the Container, and the contents of this file must be the MIME type string application/epub+zip.
  91. // The contents of the mimetype file must not contain any leading padding or whitespace, must not begin with the Unicode signature (or Byte Order Mark),
  92. // and the case of the MIME type string must be exactly as presented above. The mimetype file additionally must be neither compressed nor encrypted,
  93. // and there must not be an extra field in its ZIP header."
  94. $this->fseek($info['zip']['entries'][0]['data_offset']);
  95. if ($this->fread(20) == 'application/epub+zip') {
  96. $info['fileformat'] = 'zip.epub';
  97. $info['mime_type'] = 'application/epub+zip';
  98. }
  99. }
  100. // check for Office Open XML files (e.g. .docx, .xlsx)
  101. if (!empty($info['zip']['files']['[Content_Types].xml']) &&
  102. !empty($info['zip']['files']['_rels']['.rels']) &&
  103. !empty($info['zip']['files']['docProps']['app.xml']) &&
  104. !empty($info['zip']['files']['docProps']['core.xml'])) {
  105. // http://technet.microsoft.com/en-us/library/cc179224.aspx
  106. $info['fileformat'] = 'zip.msoffice';
  107. if (!empty($info['zip']['files']['ppt'])) {
  108. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  109. } elseif (!empty($info['zip']['files']['xl'])) {
  110. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  111. } elseif (!empty($info['zip']['files']['word'])) {
  112. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  113. }
  114. }
  115. return true;
  116. }
  117. }
  118. }
  119. if (!$this->getZIPentriesFilepointer()) {
  120. unset($info['zip']);
  121. $info['fileformat'] = '';
  122. $this->error('Cannot find End Of Central Directory (truncated file?)');
  123. return false;
  124. }
  125. // central directory couldn't be found and/or parsed
  126. // scan through actual file data entries, recover as much as possible from probable trucated file
  127. if ($info['zip']['compressed_size'] > ($info['filesize'] - 46 - 22)) {
  128. $this->error('Warning: Truncated file! - Total compressed file sizes ('.$info['zip']['compressed_size'].' bytes) is greater than filesize minus Central Directory and End Of Central Directory structures ('.($info['filesize'] - 46 - 22).' bytes)');
  129. }
  130. $this->error('Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete');
  131. foreach ($info['zip']['entries'] as $key => $valuearray) {
  132. $info['zip']['files'][$valuearray['filename']] = $valuearray['uncompressed_size'];
  133. }
  134. return true;
  135. }
  136. /**
  137. * @return bool
  138. */
  139. public function getZIPHeaderFilepointerTopDown() {
  140. $info = &$this->getid3->info;
  141. $info['fileformat'] = 'zip';
  142. $info['zip']['compressed_size'] = 0;
  143. $info['zip']['uncompressed_size'] = 0;
  144. $info['zip']['entries_count'] = 0;
  145. rewind($this->getid3->fp);
  146. while ($fileentry = $this->ZIPparseLocalFileHeader()) {
  147. $info['zip']['entries'][] = $fileentry;
  148. $info['zip']['entries_count']++;
  149. }
  150. if ($info['zip']['entries_count'] == 0) {
  151. $this->error('No Local File Header entries found');
  152. return false;
  153. }
  154. $info['zip']['entries_count'] = 0;
  155. while ($centraldirectoryentry = $this->ZIPparseCentralDirectory()) {
  156. $info['zip']['central_directory'][] = $centraldirectoryentry;
  157. $info['zip']['entries_count']++;
  158. $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
  159. $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
  160. }
  161. if ($info['zip']['entries_count'] == 0) {
  162. $this->error('No Central Directory entries found (truncated file?)');
  163. return false;
  164. }
  165. if ($EOCD = $this->ZIPparseEndOfCentralDirectory()) {
  166. $info['zip']['end_central_directory'] = $EOCD;
  167. } else {
  168. $this->error('No End Of Central Directory entry found (truncated file?)');
  169. return false;
  170. }
  171. if (!empty($info['zip']['end_central_directory']['comment'])) {
  172. $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
  173. }
  174. return true;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function getZIPentriesFilepointer() {
  180. $info = &$this->getid3->info;
  181. $info['zip']['compressed_size'] = 0;
  182. $info['zip']['uncompressed_size'] = 0;
  183. $info['zip']['entries_count'] = 0;
  184. rewind($this->getid3->fp);
  185. while ($fileentry = $this->ZIPparseLocalFileHeader()) {
  186. $info['zip']['entries'][] = $fileentry;
  187. $info['zip']['entries_count']++;
  188. $info['zip']['compressed_size'] += $fileentry['compressed_size'];
  189. $info['zip']['uncompressed_size'] += $fileentry['uncompressed_size'];
  190. }
  191. if ($info['zip']['entries_count'] == 0) {
  192. $this->error('No Local File Header entries found');
  193. return false;
  194. }
  195. return true;
  196. }
  197. /**
  198. * @return array|false
  199. */
  200. public function ZIPparseLocalFileHeader() {
  201. $LocalFileHeader = array();
  202. $LocalFileHeader['offset'] = $this->ftell();
  203. $ZIPlocalFileHeader = $this->fread(30);
  204. $LocalFileHeader['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 0, 4));
  205. if ($LocalFileHeader['raw']['signature'] != 0x04034B50) { // "PK\x03\x04"
  206. // invalid Local File Header Signature
  207. $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
  208. return false;
  209. }
  210. $LocalFileHeader['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 4, 2));
  211. $LocalFileHeader['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 6, 2));
  212. $LocalFileHeader['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 8, 2));
  213. $LocalFileHeader['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 10, 2));
  214. $LocalFileHeader['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 12, 2));
  215. $LocalFileHeader['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 14, 4));
  216. $LocalFileHeader['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 18, 4));
  217. $LocalFileHeader['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 22, 4));
  218. $LocalFileHeader['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 26, 2));
  219. $LocalFileHeader['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 28, 2));
  220. $LocalFileHeader['extract_version'] = sprintf('%1.1f', $LocalFileHeader['raw']['extract_version'] / 10);
  221. $LocalFileHeader['host_os'] = $this->ZIPversionOSLookup(($LocalFileHeader['raw']['extract_version'] & 0xFF00) >> 8);
  222. $LocalFileHeader['compression_method'] = $this->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']);
  223. $LocalFileHeader['compressed_size'] = $LocalFileHeader['raw']['compressed_size'];
  224. $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['raw']['uncompressed_size'];
  225. $LocalFileHeader['flags'] = $this->ZIPparseGeneralPurposeFlags($LocalFileHeader['raw']['general_flags'], $LocalFileHeader['raw']['compression_method']);
  226. $LocalFileHeader['last_modified_timestamp'] = $this->DOStime2UNIXtime($LocalFileHeader['raw']['last_mod_file_date'], $LocalFileHeader['raw']['last_mod_file_time']);
  227. $FilenameExtrafieldLength = $LocalFileHeader['raw']['filename_length'] + $LocalFileHeader['raw']['extra_field_length'];
  228. if ($FilenameExtrafieldLength > 0) {
  229. $ZIPlocalFileHeader .= $this->fread($FilenameExtrafieldLength);
  230. if ($LocalFileHeader['raw']['filename_length'] > 0) {
  231. $LocalFileHeader['filename'] = substr($ZIPlocalFileHeader, 30, $LocalFileHeader['raw']['filename_length']);
  232. }
  233. if ($LocalFileHeader['raw']['extra_field_length'] > 0) {
  234. $LocalFileHeader['raw']['extra_field_data'] = substr($ZIPlocalFileHeader, 30 + $LocalFileHeader['raw']['filename_length'], $LocalFileHeader['raw']['extra_field_length']);
  235. }
  236. }
  237. if ($LocalFileHeader['compressed_size'] == 0) {
  238. // *Could* be a zero-byte file
  239. // But could also be a file written on the fly that didn't know compressed filesize beforehand.
  240. // Correct compressed filesize should be in the data_descriptor located after this file data, and also in Central Directory (at end of zip file)
  241. if (!empty($this->getid3->info['zip']['central_directory'])) {
  242. foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
  243. if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
  244. if ($central_directory_entry['compressed_size'] > 0) {
  245. // overwrite local zero value (but not ['raw']'compressed_size']) so that seeking for data_descriptor (and next file entry) works correctly
  246. $LocalFileHeader['compressed_size'] = $central_directory_entry['compressed_size'];
  247. }
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. $LocalFileHeader['data_offset'] = $this->ftell();
  254. $this->fseek($LocalFileHeader['compressed_size'], SEEK_CUR); // this should (but may not) match value in $LocalFileHeader['raw']['compressed_size'] -- $LocalFileHeader['compressed_size'] could have been overwritten above with value from Central Directory
  255. if ($LocalFileHeader['flags']['data_descriptor_used']) {
  256. $DataDescriptor = $this->fread(16);
  257. $LocalFileHeader['data_descriptor']['signature'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 0, 4));
  258. if ($LocalFileHeader['data_descriptor']['signature'] != 0x08074B50) { // "PK\x07\x08"
  259. $this->getid3->warning('invalid Local File Header Data Descriptor Signature at offset '.($this->ftell() - 16).' - expecting 08 07 4B 50, found '.getid3_lib::PrintHexBytes(substr($DataDescriptor, 0, 4)));
  260. $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
  261. return false;
  262. }
  263. $LocalFileHeader['data_descriptor']['crc_32'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 4, 4));
  264. $LocalFileHeader['data_descriptor']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 8, 4));
  265. $LocalFileHeader['data_descriptor']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 12, 4));
  266. if (!$LocalFileHeader['raw']['compressed_size'] && $LocalFileHeader['data_descriptor']['compressed_size']) {
  267. foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
  268. if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
  269. if ($LocalFileHeader['data_descriptor']['compressed_size'] == $central_directory_entry['compressed_size']) {
  270. // $LocalFileHeader['compressed_size'] already set from Central Directory
  271. } else {
  272. $this->warning('conflicting compressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['compressed_size'].') vs Central Directory ('.$central_directory_entry['compressed_size'].') for file at offset '.$LocalFileHeader['offset']);
  273. }
  274. if ($LocalFileHeader['data_descriptor']['uncompressed_size'] == $central_directory_entry['uncompressed_size']) {
  275. $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['data_descriptor']['uncompressed_size'];
  276. } else {
  277. $this->warning('conflicting uncompressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['uncompressed_size'].') vs Central Directory ('.$central_directory_entry['uncompressed_size'].') for file at offset '.$LocalFileHeader['offset']);
  278. }
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. return $LocalFileHeader;
  285. }
  286. /**
  287. * @return array|false
  288. */
  289. public function ZIPparseCentralDirectory() {
  290. $CentralDirectory = array();
  291. $CentralDirectory['offset'] = $this->ftell();
  292. $ZIPcentralDirectory = $this->fread(46);
  293. $CentralDirectory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 0, 4));
  294. if ($CentralDirectory['raw']['signature'] != 0x02014B50) {
  295. // invalid Central Directory Signature
  296. $this->fseek($CentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
  297. return false;
  298. }
  299. $CentralDirectory['raw']['create_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 4, 2));
  300. $CentralDirectory['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 6, 2));
  301. $CentralDirectory['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 8, 2));
  302. $CentralDirectory['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 10, 2));
  303. $CentralDirectory['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 12, 2));
  304. $CentralDirectory['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 14, 2));
  305. $CentralDirectory['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 16, 4));
  306. $CentralDirectory['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 20, 4));
  307. $CentralDirectory['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 24, 4));
  308. $CentralDirectory['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 28, 2));
  309. $CentralDirectory['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 30, 2));
  310. $CentralDirectory['raw']['file_comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 32, 2));
  311. $CentralDirectory['raw']['disk_number_start'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 34, 2));
  312. $CentralDirectory['raw']['internal_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 36, 2));
  313. $CentralDirectory['raw']['external_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 38, 4));
  314. $CentralDirectory['raw']['local_header_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 42, 4));
  315. $CentralDirectory['entry_offset'] = $CentralDirectory['raw']['local_header_offset'];
  316. $CentralDirectory['create_version'] = sprintf('%1.1f', $CentralDirectory['raw']['create_version'] / 10);
  317. $CentralDirectory['extract_version'] = sprintf('%1.1f', $CentralDirectory['raw']['extract_version'] / 10);
  318. $CentralDirectory['host_os'] = $this->ZIPversionOSLookup(($CentralDirectory['raw']['extract_version'] & 0xFF00) >> 8);
  319. $CentralDirectory['compression_method'] = $this->ZIPcompressionMethodLookup($CentralDirectory['raw']['compression_method']);
  320. $CentralDirectory['compressed_size'] = $CentralDirectory['raw']['compressed_size'];
  321. $CentralDirectory['uncompressed_size'] = $CentralDirectory['raw']['uncompressed_size'];
  322. $CentralDirectory['flags'] = $this->ZIPparseGeneralPurposeFlags($CentralDirectory['raw']['general_flags'], $CentralDirectory['raw']['compression_method']);
  323. $CentralDirectory['last_modified_timestamp'] = $this->DOStime2UNIXtime($CentralDirectory['raw']['last_mod_file_date'], $CentralDirectory['raw']['last_mod_file_time']);
  324. $FilenameExtrafieldCommentLength = $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'] + $CentralDirectory['raw']['file_comment_length'];
  325. if ($FilenameExtrafieldCommentLength > 0) {
  326. $FilenameExtrafieldComment = $this->fread($FilenameExtrafieldCommentLength);
  327. if ($CentralDirectory['raw']['filename_length'] > 0) {
  328. $CentralDirectory['filename'] = substr($FilenameExtrafieldComment, 0, $CentralDirectory['raw']['filename_length']);
  329. }
  330. if ($CentralDirectory['raw']['extra_field_length'] > 0) {
  331. $CentralDirectory['raw']['extra_field_data'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'], $CentralDirectory['raw']['extra_field_length']);
  332. }
  333. if ($CentralDirectory['raw']['file_comment_length'] > 0) {
  334. $CentralDirectory['file_comment'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'], $CentralDirectory['raw']['file_comment_length']);
  335. }
  336. }
  337. return $CentralDirectory;
  338. }
  339. /**
  340. * @return array|false
  341. */
  342. public function ZIPparseEndOfCentralDirectory() {
  343. $EndOfCentralDirectory = array();
  344. $EndOfCentralDirectory['offset'] = $this->ftell();
  345. $ZIPendOfCentralDirectory = $this->fread(22);
  346. $EndOfCentralDirectory['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 0, 4));
  347. if ($EndOfCentralDirectory['signature'] != 0x06054B50) {
  348. // invalid End Of Central Directory Signature
  349. $this->fseek($EndOfCentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
  350. return false;
  351. }
  352. $EndOfCentralDirectory['disk_number_current'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 4, 2));
  353. $EndOfCentralDirectory['disk_number_start_directory'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 6, 2));
  354. $EndOfCentralDirectory['directory_entries_this_disk'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 8, 2));
  355. $EndOfCentralDirectory['directory_entries_total'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 10, 2));
  356. $EndOfCentralDirectory['directory_size'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 12, 4));
  357. $EndOfCentralDirectory['directory_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 16, 4));
  358. $EndOfCentralDirectory['comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 20, 2));
  359. if ($EndOfCentralDirectory['comment_length'] > 0) {
  360. $EndOfCentralDirectory['comment'] = $this->fread($EndOfCentralDirectory['comment_length']);
  361. }
  362. return $EndOfCentralDirectory;
  363. }
  364. /**
  365. * @param int $flagbytes
  366. * @param int $compressionmethod
  367. *
  368. * @return array
  369. */
  370. public static function ZIPparseGeneralPurposeFlags($flagbytes, $compressionmethod) {
  371. // https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip-printable.html
  372. $ParsedFlags = array();
  373. $ParsedFlags['encrypted'] = (bool) ($flagbytes & 0x0001);
  374. // 0x0002 -- see below
  375. // 0x0004 -- see below
  376. $ParsedFlags['data_descriptor_used'] = (bool) ($flagbytes & 0x0008);
  377. $ParsedFlags['enhanced_deflation'] = (bool) ($flagbytes & 0x0010);
  378. $ParsedFlags['compressed_patched_data'] = (bool) ($flagbytes & 0x0020);
  379. $ParsedFlags['strong_encryption'] = (bool) ($flagbytes & 0x0040);
  380. // 0x0080 - unused
  381. // 0x0100 - unused
  382. // 0x0200 - unused
  383. // 0x0400 - unused
  384. $ParsedFlags['language_encoding'] = (bool) ($flagbytes & 0x0800);
  385. // 0x1000 - reserved
  386. $ParsedFlags['mask_header_values'] = (bool) ($flagbytes & 0x2000);
  387. // 0x4000 - reserved
  388. // 0x8000 - reserved
  389. switch ($compressionmethod) {
  390. case 6:
  391. $ParsedFlags['dictionary_size'] = (($flagbytes & 0x0002) ? 8192 : 4096);
  392. $ParsedFlags['shannon_fano_trees'] = (($flagbytes & 0x0004) ? 3 : 2);
  393. break;
  394. case 8:
  395. case 9:
  396. switch (($flagbytes & 0x0006) >> 1) {
  397. case 0:
  398. $ParsedFlags['compression_speed'] = 'normal';
  399. break;
  400. case 1:
  401. $ParsedFlags['compression_speed'] = 'maximum';
  402. break;
  403. case 2:
  404. $ParsedFlags['compression_speed'] = 'fast';
  405. break;
  406. case 3:
  407. $ParsedFlags['compression_speed'] = 'superfast';
  408. break;
  409. }
  410. break;
  411. }
  412. return $ParsedFlags;
  413. }
  414. /**
  415. * @param int $index
  416. *
  417. * @return string
  418. */
  419. public static function ZIPversionOSLookup($index) {
  420. static $ZIPversionOSLookup = array(
  421. 0 => 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
  422. 1 => 'Amiga',
  423. 2 => 'OpenVMS',
  424. 3 => 'Unix',
  425. 4 => 'VM/CMS',
  426. 5 => 'Atari ST',
  427. 6 => 'OS/2 H.P.F.S.',
  428. 7 => 'Macintosh',
  429. 8 => 'Z-System',
  430. 9 => 'CP/M',
  431. 10 => 'Windows NTFS',
  432. 11 => 'MVS',
  433. 12 => 'VSE',
  434. 13 => 'Acorn Risc',
  435. 14 => 'VFAT',
  436. 15 => 'Alternate MVS',
  437. 16 => 'BeOS',
  438. 17 => 'Tandem',
  439. 18 => 'OS/400',
  440. 19 => 'OS/X (Darwin)',
  441. );
  442. return (isset($ZIPversionOSLookup[$index]) ? $ZIPversionOSLookup[$index] : '[unknown]');
  443. }
  444. /**
  445. * @param int $index
  446. *
  447. * @return string
  448. */
  449. public static function ZIPcompressionMethodLookup($index) {
  450. // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/ZIP.html
  451. static $ZIPcompressionMethodLookup = array(
  452. 0 => 'store',
  453. 1 => 'shrink',
  454. 2 => 'reduce-1',
  455. 3 => 'reduce-2',
  456. 4 => 'reduce-3',
  457. 5 => 'reduce-4',
  458. 6 => 'implode',
  459. 7 => 'tokenize',
  460. 8 => 'deflate',
  461. 9 => 'deflate64',
  462. 10 => 'Imploded (old IBM TERSE)',
  463. 11 => 'RESERVED[11]',
  464. 12 => 'BZIP2',
  465. 13 => 'RESERVED[13]',
  466. 14 => 'LZMA (EFS)',
  467. 15 => 'RESERVED[15]',
  468. 16 => 'RESERVED[16]',
  469. 17 => 'RESERVED[17]',
  470. 18 => 'IBM TERSE (new)',
  471. 19 => 'IBM LZ77 z Architecture (PFS)',
  472. 96 => 'JPEG recompressed',
  473. 97 => 'WavPack compressed',
  474. 98 => 'PPMd version I, Rev 1',
  475. );
  476. return (isset($ZIPcompressionMethodLookup[$index]) ? $ZIPcompressionMethodLookup[$index] : '[unknown]');
  477. }
  478. /**
  479. * @param int $DOSdate
  480. * @param int $DOStime
  481. *
  482. * @return int
  483. */
  484. public static function DOStime2UNIXtime($DOSdate, $DOStime) {
  485. // wFatDate
  486. // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
  487. // Bits Contents
  488. // 0-4 Day of the month (1-31)
  489. // 5-8 Month (1 = January, 2 = February, and so on)
  490. // 9-15 Year offset from 1980 (add 1980 to get actual year)
  491. $UNIXday = ($DOSdate & 0x001F);
  492. $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
  493. $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
  494. // wFatTime
  495. // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
  496. // Bits Contents
  497. // 0-4 Second divided by 2
  498. // 5-10 Minute (0-59)
  499. // 11-15 Hour (0-23 on a 24-hour clock)
  500. $UNIXsecond = ($DOStime & 0x001F) * 2;
  501. $UNIXminute = (($DOStime & 0x07E0) >> 5);
  502. $UNIXhour = (($DOStime & 0xF800) >> 11);
  503. return gmmktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
  504. }
  505. }