module.archive.gzip.php 10 KB

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