PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/getid3/module.archive.gzip.php

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