ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.archive.tar.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
7 // See readme.txt for more details //
9 // //
10 // module.archive.tar.php //
11 // written by Mike Mozolin <teddybearØmail*ru> //
12 // module for analyzing TAR files //
13 // dependencies: NONE //
14 // ///
16 
17 class getid3_tar {
18 
19  function getid3_tar(&$fd, &$ThisFileInfo) {
20  $ThisFileInfo['fileformat'] = 'tar';
21  $ThisFileInfo['tar']['files'] = array();
22 
23  $unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155/prefix';
24  $null_512k = str_repeat("\0", 512); // end-of-file marker
25 
26  @fseek($fd, 0);
27  while (!feof($fd)) {
28  $buffer = fread($fd, 512);
29 
30  // check the block
31  $checksum = 0;
32  for ($i = 0; $i < 148; $i++) {
33  $checksum += ord($buffer{$i});
34  }
35  for ($i = 148; $i < 156; $i++) {
36  $checksum += ord(' ');
37  }
38  for ($i = 156; $i < 512; $i++) {
39  $checksum += ord($buffer{$i});
40  }
41  $attr = unpack($unpack_header, $buffer);
42  $name = trim(@$attr['fname']);
43  $mode = octdec(trim(@$attr['mode']));
44  $uid = octdec(trim(@$attr['uid']));
45  $gid = octdec(trim(@$attr['gid']));
46  $size = octdec(trim(@$attr['size']));
47  $mtime = octdec(trim(@$attr['mtime']));
48  $chksum = octdec(trim(@$attr['chksum']));
49  $typflag = trim(@$attr['typflag']);
50  $lnkname = trim(@$attr['lnkname']);
51  $magic = trim(@$attr['magic']);
52  $ver = trim(@$attr['ver']);
53  $uname = trim(@$attr['uname']);
54  $gname = trim(@$attr['gname']);
55  $devmaj = octdec(trim(@$attr['devmaj']));
56  $devmin = octdec(trim(@$attr['devmin']));
57  $prefix = trim(@$attr['prefix']);
58  if (($checksum == 256) && ($chksum == 0)) {
59  // EOF Found
60  break;
61  }
62  if ($prefix) {
63  $name = $prefix.'/'.$name;
64  }
65  if ((preg_match('#/$#', $name)) && !$name) {
66  $typeflag = 5;
67  }
68  if ($buffer == $null_512k) {
69  // it's the end of the tar-file...
70  break;
71  }
72 
73  // Read to the next chunk
74  fseek($fd, $size, SEEK_CUR);
75 
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 
108  // Parses the file mode to file permissions
109  function display_perms($mode) {
110  // Determine Type
111  if ($mode & 0x1000) $type='p'; // FIFO pipe
112  elseif ($mode & 0x2000) $type='c'; // Character special
113  elseif ($mode & 0x4000) $type='d'; // Directory
114  elseif ($mode & 0x6000) $type='b'; // Block special
115  elseif ($mode & 0x8000) $type='-'; // Regular
116  elseif ($mode & 0xA000) $type='l'; // Symbolic Link
117  elseif ($mode & 0xC000) $type='s'; // Socket
118  else $type='u'; // UNKNOWN
119 
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 
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 
136  $s = sprintf('%1s', $type);
137  $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
138  $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
139  $s .= sprintf('%1s%1s%1s'."\n", $world['read'], $world['write'], $world['execute']);
140  return $s;
141  }
142 
143  // Converts the file type
144  function get_flag_type($typflag) {
145  static $flag_types = array(
146  '0' => 'LF_NORMAL',
147  '1' => 'LF_LINK',
148  '2' => 'LF_SYNLINK',
149  '3' => 'LF_CHR',
150  '4' => 'LF_BLK',
151  '5' => 'LF_DIR',
152  '6' => 'LF_FIFO',
153  '7' => 'LF_CONFIG',
154  'D' => 'LF_DUMPDIR',
155  'K' => 'LF_LONGLINK',
156  'L' => 'LF_LONGNAME',
157  'M' => 'LF_MULTIVOL',
158  'N' => 'LF_NAMES',
159  'S' => 'LF_SPARSE',
160  'V' => 'LF_VOLHDR'
161  );
162  return @$flag_types[$typflag];
163  }
164 
165 }
166 
167 ?>