module.archive.tar.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.tar.php //
  12. // module for analyzing TAR files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. // //
  17. // Module originally written by //
  18. // Mike Mozolin <teddybearØmail*ru> //
  19. // //
  20. /////////////////////////////////////////////////////////////////
  21. class getid3_tar extends getid3_handler
  22. {
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $info['fileformat'] = 'tar';
  26. $info['tar']['files'] = array();
  27. $unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155prefix';
  28. $null_512k = str_repeat("\x00", 512); // end-of-file marker
  29. $this->fseek(0);
  30. while (!feof($this->getid3->fp)) {
  31. $buffer = $this->fread(512);
  32. if (strlen($buffer) < 512) {
  33. break;
  34. }
  35. // check the block
  36. $checksum = 0;
  37. for ($i = 0; $i < 148; $i++) {
  38. $checksum += ord($buffer{$i});
  39. }
  40. for ($i = 148; $i < 156; $i++) {
  41. $checksum += ord(' ');
  42. }
  43. for ($i = 156; $i < 512; $i++) {
  44. $checksum += ord($buffer{$i});
  45. }
  46. $attr = unpack($unpack_header, $buffer);
  47. $name = (isset($attr['fname'] ) ? trim($attr['fname'] ) : '');
  48. $mode = octdec(isset($attr['mode'] ) ? trim($attr['mode'] ) : '');
  49. $uid = octdec(isset($attr['uid'] ) ? trim($attr['uid'] ) : '');
  50. $gid = octdec(isset($attr['gid'] ) ? trim($attr['gid'] ) : '');
  51. $size = octdec(isset($attr['size'] ) ? trim($attr['size'] ) : '');
  52. $mtime = octdec(isset($attr['mtime'] ) ? trim($attr['mtime'] ) : '');
  53. $chksum = octdec(isset($attr['chksum'] ) ? trim($attr['chksum'] ) : '');
  54. $typflag = (isset($attr['typflag']) ? trim($attr['typflag']) : '');
  55. $lnkname = (isset($attr['lnkname']) ? trim($attr['lnkname']) : '');
  56. $magic = (isset($attr['magic'] ) ? trim($attr['magic'] ) : '');
  57. $ver = (isset($attr['ver'] ) ? trim($attr['ver'] ) : '');
  58. $uname = (isset($attr['uname'] ) ? trim($attr['uname'] ) : '');
  59. $gname = (isset($attr['gname'] ) ? trim($attr['gname'] ) : '');
  60. $devmaj = octdec(isset($attr['devmaj'] ) ? trim($attr['devmaj'] ) : '');
  61. $devmin = octdec(isset($attr['devmin'] ) ? trim($attr['devmin'] ) : '');
  62. $prefix = (isset($attr['prefix'] ) ? trim($attr['prefix'] ) : '');
  63. if (($checksum == 256) && ($chksum == 0)) {
  64. // EOF Found
  65. break;
  66. }
  67. if ($prefix) {
  68. $name = $prefix.'/'.$name;
  69. }
  70. if ((preg_match('#/$#', $name)) && !$name) {
  71. $typeflag = 5;
  72. }
  73. if ($buffer == $null_512k) {
  74. // it's the end of the tar-file...
  75. break;
  76. }
  77. // Read to the next chunk
  78. $this->fseek($size, SEEK_CUR);
  79. $diff = $size % 512;
  80. if ($diff != 0) {
  81. // Padding, throw away
  82. $this->fseek((512 - $diff), SEEK_CUR);
  83. }
  84. // Protect against tar-files with garbage at the end
  85. if ($name == '') {
  86. break;
  87. }
  88. $info['tar']['file_details'][$name] = array (
  89. 'name' => $name,
  90. 'mode_raw' => $mode,
  91. 'mode' => self::display_perms($mode),
  92. 'uid' => $uid,
  93. 'gid' => $gid,
  94. 'size' => $size,
  95. 'mtime' => $mtime,
  96. 'chksum' => $chksum,
  97. 'typeflag' => self::get_flag_type($typflag),
  98. 'linkname' => $lnkname,
  99. 'magic' => $magic,
  100. 'version' => $ver,
  101. 'uname' => $uname,
  102. 'gname' => $gname,
  103. 'devmajor' => $devmaj,
  104. 'devminor' => $devmin
  105. );
  106. $info['tar']['files'] = getid3_lib::array_merge_clobber($info['tar']['files'], getid3_lib::CreateDeepArray($info['tar']['file_details'][$name]['name'], '/', $size));
  107. }
  108. return true;
  109. }
  110. // Parses the file mode to file permissions
  111. public function display_perms($mode) {
  112. // Determine Type
  113. if ($mode & 0x1000) $type='p'; // FIFO pipe
  114. elseif ($mode & 0x2000) $type='c'; // Character special
  115. elseif ($mode & 0x4000) $type='d'; // Directory
  116. elseif ($mode & 0x6000) $type='b'; // Block special
  117. elseif ($mode & 0x8000) $type='-'; // Regular
  118. elseif ($mode & 0xA000) $type='l'; // Symbolic Link
  119. elseif ($mode & 0xC000) $type='s'; // Socket
  120. else $type='u'; // UNKNOWN
  121. // Determine permissions
  122. $owner['read'] = (($mode & 00400) ? 'r' : '-');
  123. $owner['write'] = (($mode & 00200) ? 'w' : '-');
  124. $owner['execute'] = (($mode & 00100) ? 'x' : '-');
  125. $group['read'] = (($mode & 00040) ? 'r' : '-');
  126. $group['write'] = (($mode & 00020) ? 'w' : '-');
  127. $group['execute'] = (($mode & 00010) ? 'x' : '-');
  128. $world['read'] = (($mode & 00004) ? 'r' : '-');
  129. $world['write'] = (($mode & 00002) ? 'w' : '-');
  130. $world['execute'] = (($mode & 00001) ? 'x' : '-');
  131. // Adjust for SUID, SGID and sticky bit
  132. if ($mode & 0x800) $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
  133. if ($mode & 0x400) $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
  134. if ($mode & 0x200) $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
  135. $s = sprintf('%1s', $type);
  136. $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
  137. $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
  138. $s .= sprintf('%1s%1s%1s'."\n", $world['read'], $world['write'], $world['execute']);
  139. return $s;
  140. }
  141. // Converts the file type
  142. public function get_flag_type($typflag) {
  143. static $flag_types = array(
  144. '0' => 'LF_NORMAL',
  145. '1' => 'LF_LINK',
  146. '2' => 'LF_SYNLINK',
  147. '3' => 'LF_CHR',
  148. '4' => 'LF_BLK',
  149. '5' => 'LF_DIR',
  150. '6' => 'LF_FIFO',
  151. '7' => 'LF_CONFIG',
  152. 'D' => 'LF_DUMPDIR',
  153. 'K' => 'LF_LONGLINK',
  154. 'L' => 'LF_LONGNAME',
  155. 'M' => 'LF_MULTIVOL',
  156. 'N' => 'LF_NAMES',
  157. 'S' => 'LF_SPARSE',
  158. 'V' => 'LF_VOLHDR'
  159. );
  160. return (isset($flag_types[$typflag]) ? $flag_types[$typflag] : '');
  161. }
  162. }