module.archive.gzip.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.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. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  21. exit;
  22. }
  23. class getid3_gzip extends getid3_handler
  24. {
  25. /**
  26. * Optional file list - disable for speed.
  27. * Decode gzipped files, if possible, and parse recursively (.tar.gz for example).
  28. *
  29. * @var bool
  30. */
  31. public $parse_contents = false;
  32. /**
  33. * @return bool
  34. */
  35. public function Analyze() {
  36. $info = &$this->getid3->info;
  37. $info['fileformat'] = 'gzip';
  38. $start_length = 10;
  39. $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
  40. //+---+---+---+---+---+---+---+---+---+---+
  41. //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
  42. //+---+---+---+---+---+---+---+---+---+---+
  43. if ($info['php_memory_limit'] && ($info['filesize'] > $info['php_memory_limit'])) {
  44. $this->error('File is too large ('.number_format($info['filesize']).' bytes) to read into memory (limit: '.number_format($info['php_memory_limit'] / 1048576).'MB)');
  45. return false;
  46. }
  47. $this->fseek(0);
  48. $buffer = $this->fread($info['filesize']);
  49. $arr_members = explode("\x1F\x8B\x08", $buffer);
  50. $num_members = 0;
  51. while (true) {
  52. $is_wrong_members = false;
  53. $num_members = count($arr_members);
  54. for ($i = 0; $i < $num_members; $i++) {
  55. if (strlen($arr_members[$i]) == 0) {
  56. continue;
  57. }
  58. $buf = "\x1F\x8B\x08".$arr_members[$i];
  59. $attr = unpack($unpack_header, substr($buf, 0, $start_length));
  60. if (!$this->get_os_type(ord($attr['os']))) {
  61. // Merge member with previous if wrong OS type
  62. $arr_members[($i - 1)] .= $buf;
  63. $arr_members[$i] = '';
  64. $is_wrong_members = true;
  65. continue;
  66. }
  67. }
  68. if (!$is_wrong_members) {
  69. break;
  70. }
  71. }
  72. $info['gzip']['files'] = array();
  73. $fpointer = 0;
  74. $idx = 0;
  75. foreach ($arr_members as $member) {
  76. if (strlen($member) == 0) {
  77. continue;
  78. }
  79. $thisInfo = &$info['gzip']['member_header'][++$idx];
  80. $buff = "\x1F\x8B\x08". $member;
  81. $attr = unpack($unpack_header, substr($buff, 0, $start_length));
  82. $thisInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
  83. $thisInfo['raw']['id1'] = ord($attr['cmethod']);
  84. $thisInfo['raw']['id2'] = ord($attr['cmethod']);
  85. $thisInfo['raw']['cmethod'] = ord($attr['cmethod']);
  86. $thisInfo['raw']['os'] = ord($attr['os']);
  87. $thisInfo['raw']['xflags'] = ord($attr['xflags']);
  88. $thisInfo['raw']['flags'] = ord($attr['flags']);
  89. $thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x02);
  90. $thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x04);
  91. $thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x08);
  92. $thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10);
  93. $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
  94. $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
  95. if (!$thisInfo['os']) {
  96. $this->error('Read error on gzip file');
  97. return false;
  98. }
  99. $fpointer = 10;
  100. $arr_xsubfield = array();
  101. // bit 2 - FLG.FEXTRA
  102. //+---+---+=================================+
  103. //| XLEN |...XLEN bytes of "extra field"...|
  104. //+---+---+=================================+
  105. if ($thisInfo['flags']['extra']) {
  106. $w_xlen = substr($buff, $fpointer, 2);
  107. $xlen = getid3_lib::LittleEndian2Int($w_xlen);
  108. $fpointer += 2;
  109. $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
  110. // Extra SubFields
  111. //+---+---+---+---+==================================+
  112. //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
  113. //+---+---+---+---+==================================+
  114. $idx = 0;
  115. while (true) {
  116. if ($idx >= $xlen) {
  117. break;
  118. }
  119. $si1 = ord(substr($buff, $fpointer + $idx++, 1));
  120. $si2 = ord(substr($buff, $fpointer + $idx++, 1));
  121. if (($si1 == 0x41) && ($si2 == 0x70)) {
  122. $w_xsublen = substr($buff, $fpointer + $idx, 2);
  123. $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
  124. $idx += 2;
  125. $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen);
  126. $idx += $xsublen;
  127. } else {
  128. break;
  129. }
  130. }
  131. $fpointer += $xlen;
  132. }
  133. // bit 3 - FLG.FNAME
  134. //+=========================================+
  135. //|...original file name, zero-terminated...|
  136. //+=========================================+
  137. // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
  138. $thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']);
  139. if ($thisInfo['flags']['filename']) {
  140. $thisInfo['filename'] = '';
  141. while (true) {
  142. if (ord($buff[$fpointer]) == 0) {
  143. $fpointer++;
  144. break;
  145. }
  146. $thisInfo['filename'] .= $buff[$fpointer];
  147. $fpointer++;
  148. }
  149. }
  150. // bit 4 - FLG.FCOMMENT
  151. //+===================================+
  152. //|...file comment, zero-terminated...|
  153. //+===================================+
  154. if ($thisInfo['flags']['comment']) {
  155. while (true) {
  156. if (ord($buff[$fpointer]) == 0) {
  157. $fpointer++;
  158. break;
  159. }
  160. $thisInfo['comment'] .= $buff[$fpointer];
  161. $fpointer++;
  162. }
  163. }
  164. // bit 1 - FLG.FHCRC
  165. //+---+---+
  166. //| CRC16 |
  167. //+---+---+
  168. if ($thisInfo['flags']['crc16']) {
  169. $w_crc = substr($buff, $fpointer, 2);
  170. $thisInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
  171. $fpointer += 2;
  172. }
  173. // bit 0 - FLG.FTEXT
  174. //if ($thisInfo['raw']['flags'] & 0x01) {
  175. // Ignored...
  176. //}
  177. // bits 5, 6, 7 - reserved
  178. $thisInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
  179. $thisInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
  180. $info['gzip']['files'] = getid3_lib::array_merge_clobber($info['gzip']['files'], getid3_lib::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
  181. if ($this->parse_contents) {
  182. // Try to inflate GZip
  183. $csize = 0;
  184. $inflated = '';
  185. $chkcrc32 = '';
  186. if (function_exists('gzinflate')) {
  187. $cdata = substr($buff, $fpointer);
  188. $cdata = substr($cdata, 0, strlen($cdata) - 8);
  189. $csize = strlen($cdata);
  190. $inflated = gzinflate($cdata);
  191. // Calculate CRC32 for inflated content
  192. $thisInfo['crc32_valid'] = sprintf('%u', crc32($inflated)) == $thisInfo['crc32'];
  193. // determine format
  194. $formattest = substr($inflated, 0, 32774);
  195. $getid3_temp = new getID3();
  196. $determined_format = $getid3_temp->GetFileFormat($formattest);
  197. unset($getid3_temp);
  198. // file format is determined
  199. $determined_format['module'] = (isset($determined_format['module']) ? $determined_format['module'] : '');
  200. switch ($determined_format['module']) {
  201. case 'tar':
  202. // view TAR-file info
  203. if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
  204. if (($temp_tar_filename = tempnam(GETID3_TEMP_DIR, 'getID3')) === false) {
  205. // can't find anywhere to create a temp file, abort
  206. $this->error('Unable to create temp file to parse TAR inside GZIP file');
  207. break;
  208. }
  209. if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
  210. fwrite($fp_temp_tar, $inflated);
  211. fclose($fp_temp_tar);
  212. $getid3_temp = new getID3();
  213. $getid3_temp->openfile($temp_tar_filename);
  214. $getid3_tar = new getid3_tar($getid3_temp);
  215. $getid3_tar->Analyze();
  216. $info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info['tar'];
  217. unset($getid3_temp, $getid3_tar);
  218. unlink($temp_tar_filename);
  219. } else {
  220. $this->error('Unable to fopen() temp file to parse TAR inside GZIP file');
  221. break;
  222. }
  223. }
  224. break;
  225. case '':
  226. default:
  227. // unknown or unhandled format
  228. break;
  229. }
  230. } else {
  231. $this->warning('PHP is not compiled with gzinflate() support. Please enable PHP Zlib extension or recompile with the --with-zlib switch');
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * Converts the OS type.
  239. *
  240. * @param string $key
  241. *
  242. * @return string
  243. */
  244. public function get_os_type($key) {
  245. static $os_type = array(
  246. '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
  247. '1' => 'Amiga',
  248. '2' => 'VMS (or OpenVMS)',
  249. '3' => 'Unix',
  250. '4' => 'VM/CMS',
  251. '5' => 'Atari TOS',
  252. '6' => 'HPFS filesystem (OS/2, NT)',
  253. '7' => 'Macintosh',
  254. '8' => 'Z-System',
  255. '9' => 'CP/M',
  256. '10' => 'TOPS-20',
  257. '11' => 'NTFS filesystem (NT)',
  258. '12' => 'QDOS',
  259. '13' => 'Acorn RISCOS',
  260. '255' => 'unknown'
  261. );
  262. return (isset($os_type[$key]) ? $os_type[$key] : '');
  263. }
  264. /**
  265. * Converts the eXtra FLags.
  266. *
  267. * @param string $key
  268. *
  269. * @return string
  270. */
  271. public function get_xflag_type($key) {
  272. static $xflag_type = array(
  273. '0' => 'unknown',
  274. '2' => 'maximum compression',
  275. '4' => 'fastest algorithm'
  276. );
  277. return (isset($xflag_type[$key]) ? $xflag_type[$key] : '');
  278. }
  279. }