module.archive.tar.php 6.0 KB

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