module.archive.gzip.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.archive.gzip.php //
  11. // module for analyzing GZIP files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. // //
  16. // Module originally written by //
  17. // Mike Mozolin <teddybearØmail*ru> //
  18. // //
  19. /////////////////////////////////////////////////////////////////
  20. class getid3_gzip {
  21. // public: Optional file list - disable for speed.
  22. var $option_gzip_parse_contents = false; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
  23. function getid3_gzip(&$fd, &$ThisFileInfo) {
  24. $ThisFileInfo['fileformat'] = 'gzip';
  25. $start_length = 10;
  26. $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
  27. //+---+---+---+---+---+---+---+---+---+---+
  28. //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
  29. //+---+---+---+---+---+---+---+---+---+---+
  30. @fseek($fd, 0);
  31. $buffer = @fread($fd, $ThisFileInfo['filesize']);
  32. $arr_members = explode("\x1F\x8B\x08", $buffer);
  33. while (true) {
  34. $is_wrong_members = false;
  35. $num_members = intval(count($arr_members));
  36. for ($i = 0; $i < $num_members; $i++) {
  37. if (strlen($arr_members[$i]) == 0) {
  38. continue;
  39. }
  40. $buf = "\x1F\x8B\x08".$arr_members[$i];
  41. $attr = unpack($unpack_header, substr($buf, 0, $start_length));
  42. if (!$this->get_os_type(ord($attr['os']))) {
  43. // Merge member with previous if wrong OS type
  44. $arr_members[$i - 1] .= $buf;
  45. $arr_members[$i] = '';
  46. $is_wrong_members = true;
  47. continue;
  48. }
  49. }
  50. if (!$is_wrong_members) {
  51. break;
  52. }
  53. }
  54. $ThisFileInfo['gzip']['files'] = array();
  55. $fpointer = 0;
  56. $idx = 0;
  57. for ($i = 0; $i < $num_members; $i++) {
  58. if (strlen($arr_members[$i]) == 0) {
  59. continue;
  60. }
  61. $thisThisFileInfo = &$ThisFileInfo['gzip']['member_header'][++$idx];
  62. $buff = "\x1F\x8B\x08".$arr_members[$i];
  63. $attr = unpack($unpack_header, substr($buff, 0, $start_length));
  64. $thisThisFileInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
  65. $thisThisFileInfo['raw']['id1'] = ord($attr['cmethod']);
  66. $thisThisFileInfo['raw']['id2'] = ord($attr['cmethod']);
  67. $thisThisFileInfo['raw']['cmethod'] = ord($attr['cmethod']);
  68. $thisThisFileInfo['raw']['os'] = ord($attr['os']);
  69. $thisThisFileInfo['raw']['xflags'] = ord($attr['xflags']);
  70. $thisThisFileInfo['raw']['flags'] = ord($attr['flags']);
  71. $thisThisFileInfo['flags']['crc16'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x02);
  72. $thisThisFileInfo['flags']['extra'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x04);
  73. $thisThisFileInfo['flags']['filename'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x08);
  74. $thisThisFileInfo['flags']['comment'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x10);
  75. $thisThisFileInfo['compression'] = $this->get_xflag_type($thisThisFileInfo['raw']['xflags']);
  76. $thisThisFileInfo['os'] = $this->get_os_type($thisThisFileInfo['raw']['os']);
  77. if (!$thisThisFileInfo['os']) {
  78. $ThisFileInfo['error'][] = 'Read error on gzip file';
  79. return false;
  80. }
  81. $fpointer = 10;
  82. $arr_xsubfield = array();
  83. // bit 2 - FLG.FEXTRA
  84. //+---+---+=================================+
  85. //| XLEN |...XLEN bytes of "extra field"...|
  86. //+---+---+=================================+
  87. if ($thisThisFileInfo['flags']['extra']) {
  88. $w_xlen = substr($buff, $fpointer, 2);
  89. $xlen = getid3_lib::LittleEndian2Int($w_xlen);
  90. $fpointer += 2;
  91. $thisThisFileInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
  92. // Extra SubFields
  93. //+---+---+---+---+==================================+
  94. //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
  95. //+---+---+---+---+==================================+
  96. $idx = 0;
  97. while (true) {
  98. if ($idx >= $xlen) {
  99. break;
  100. }
  101. $si1 = ord(substr($buff, $fpointer + $idx++, 1));
  102. $si2 = ord(substr($buff, $fpointer + $idx++, 1));
  103. if (($si1 == 0x41) && ($si2 == 0x70)) {
  104. $w_xsublen = substr($buff, $fpointer + $idx, 2);
  105. $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
  106. $idx += 2;
  107. $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen);
  108. $idx += $xsublen;
  109. } else {
  110. break;
  111. }
  112. }
  113. $fpointer += $xlen;
  114. }
  115. // bit 3 - FLG.FNAME
  116. //+=========================================+
  117. //|...original file name, zero-terminated...|
  118. //+=========================================+
  119. // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
  120. $thisThisFileInfo['filename'] = eregi_replace('.gz$', '', $ThisFileInfo['filename']);
  121. if ($thisThisFileInfo['flags']['filename']) {
  122. while (true) {
  123. if (ord($buff[$fpointer]) == 0) {
  124. $fpointer++;
  125. break;
  126. }
  127. $thisThisFileInfo['filename'] .= $buff[$fpointer];
  128. $fpointer++;
  129. }
  130. }
  131. // bit 4 - FLG.FCOMMENT
  132. //+===================================+
  133. //|...file comment, zero-terminated...|
  134. //+===================================+
  135. if ($thisThisFileInfo['flags']['comment']) {
  136. while (true) {
  137. if (ord($buff[$fpointer]) == 0) {
  138. $fpointer++;
  139. break;
  140. }
  141. $thisThisFileInfo['comment'] .= $buff[$fpointer];
  142. $fpointer++;
  143. }
  144. }
  145. // bit 1 - FLG.FHCRC
  146. //+---+---+
  147. //| CRC16 |
  148. //+---+---+
  149. if ($thisThisFileInfo['flags']['crc16']) {
  150. $w_crc = substr($buff, $fpointer, 2);
  151. $thisThisFileInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
  152. $fpointer += 2;
  153. }
  154. // bit 0 - FLG.FTEXT
  155. //if ($thisThisFileInfo['raw']['flags'] & 0x01) {
  156. // Ignored...
  157. //}
  158. // bits 5, 6, 7 - reserved
  159. $thisThisFileInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
  160. $thisThisFileInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
  161. $ThisFileInfo['gzip']['files'] = getid3_lib::array_merge_clobber($ThisFileInfo['gzip']['files'], getid3_lib::CreateDeepArray($thisThisFileInfo['filename'], '/', $thisThisFileInfo['filesize']));
  162. if ($this->option_gzip_parse_contents) {
  163. // Try to inflate GZip
  164. $csize = 0;
  165. $inflated = '';
  166. $chkcrc32 = '';
  167. if (function_exists('gzinflate')) {
  168. $cdata = substr($buff, $fpointer);
  169. $cdata = substr($cdata, 0, strlen($cdata) - 8);
  170. $csize = strlen($cdata);
  171. $inflated = gzinflate($cdata);
  172. // Calculate CRC32 for inflated content
  173. $thisThisFileInfo['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $thisThisFileInfo['crc32']);
  174. // determine format
  175. $formattest = substr($inflated, 0, 32774);
  176. $newgetID3 = new getID3();
  177. $determined_format = $newgetID3->GetFileFormat($formattest);
  178. unset($newgetID3);
  179. // file format is determined
  180. switch (@$determined_format['module']) {
  181. case 'tar':
  182. // view TAR-file info
  183. if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && @include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
  184. if (($temp_tar_filename = tempnam('*', 'getID3')) === false) {
  185. // can't find anywhere to create a temp file, abort
  186. $ThisFileInfo['error'][] = 'Unable to create temp file to parse TAR inside GZIP file';
  187. break;
  188. }
  189. if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
  190. fwrite($fp_temp_tar, $inflated);
  191. rewind($fp_temp_tar);
  192. $getid3_tar = new getid3_tar($fp_temp_tar, $dummy);
  193. $ThisFileInfo['gzip']['member_header'][$idx]['tar'] = $dummy['tar'];
  194. unset($dummy);
  195. unset($getid3_tar);
  196. fclose($fp_temp_tar);
  197. unlink($temp_tar_filename);
  198. } else {
  199. $ThisFileInfo['error'][] = 'Unable to fopen() temp file to parse TAR inside GZIP file';
  200. break;
  201. }
  202. }
  203. break;
  204. case '':
  205. default:
  206. // unknown or unhandled format
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. return true;
  213. }
  214. // Converts the OS type
  215. function get_os_type($key) {
  216. static $os_type = array(
  217. '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
  218. '1' => 'Amiga',
  219. '2' => 'VMS (or OpenVMS)',
  220. '3' => 'Unix',
  221. '4' => 'VM/CMS',
  222. '5' => 'Atari TOS',
  223. '6' => 'HPFS filesystem (OS/2, NT)',
  224. '7' => 'Macintosh',
  225. '8' => 'Z-System',
  226. '9' => 'CP/M',
  227. '10' => 'TOPS-20',
  228. '11' => 'NTFS filesystem (NT)',
  229. '12' => 'QDOS',
  230. '13' => 'Acorn RISCOS',
  231. '255' => 'unknown'
  232. );
  233. return @$os_type[$key];
  234. }
  235. // Converts the eXtra FLags
  236. function get_xflag_type($key) {
  237. static $xflag_type = array(
  238. '0' => 'unknown',
  239. '2' => 'maximum compression',
  240. '4' => 'fastest algorithm'
  241. );
  242. return @$xflag_type[$key];
  243. }
  244. }
  245. ?>