module.archive.tar.php 6.6 KB

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