ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Tar.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Module\Archive;
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // See readme.txt for more details //
15 // //
16 // module.archive.tar.php //
17 // module for analyzing TAR files //
18 // dependencies: NONE //
19 // ///
21 // //
22 // Module originally written by //
23 // Mike Mozolin <teddybearØmail*ru> //
24 // //
26 
35 class Tar extends BaseHandler
36 {
37 
42  public function analyze()
43  {
44  $info = &$this->getid3->info;
45 
46  $info['fileformat'] = 'tar';
47  $info['tar']['files'] = array();
48 
49  $unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155prefix';
50  $null_512k = str_repeat("\x00", 512); // end-of-file marker
51 
52  fseek($this->getid3->fp, 0);
53  while (!feof($this->getid3->fp)) {
54  $buffer = fread($this->getid3->fp, 512);
55  if (strlen($buffer) < 512) {
56  break;
57  }
58 
59  // check the block
60  $checksum = 0;
61  for ($i = 0; $i < 148; $i++) {
62  $checksum += ord($buffer{$i});
63  }
64  for ($i = 148; $i < 156; $i++) {
65  $checksum += ord(' ');
66  }
67  for ($i = 156; $i < 512; $i++) {
68  $checksum += ord($buffer{$i});
69  }
70  $attr = unpack($unpack_header, $buffer);
71  $name = (isset($attr['fname']) ? trim($attr['fname']) : '');
72  $mode = octdec(isset($attr['mode']) ? trim($attr['mode']) : '');
73  $uid = octdec(isset($attr['uid']) ? trim($attr['uid']) : '');
74  $gid = octdec(isset($attr['gid']) ? trim($attr['gid']) : '');
75  $size = octdec(isset($attr['size']) ? trim($attr['size']) : '');
76  $mtime = octdec(isset($attr['mtime']) ? trim($attr['mtime']) : '');
77  $chksum = octdec(isset($attr['chksum']) ? trim($attr['chksum']) : '');
78  $typflag = (isset($attr['typflag']) ? trim($attr['typflag']) : '');
79  $lnkname = (isset($attr['lnkname']) ? trim($attr['lnkname']) : '');
80  $magic = (isset($attr['magic']) ? trim($attr['magic']) : '');
81  $ver = (isset($attr['ver']) ? trim($attr['ver']) : '');
82  $uname = (isset($attr['uname']) ? trim($attr['uname']) : '');
83  $gname = (isset($attr['gname']) ? trim($attr['gname']) : '');
84  $devmaj = octdec(isset($attr['devmaj']) ? trim($attr['devmaj']) : '');
85  $devmin = octdec(isset($attr['devmin']) ? trim($attr['devmin']) : '');
86  $prefix = (isset($attr['prefix']) ? trim($attr['prefix']) : '');
87  if (($checksum == 256) && ($chksum == 0)) {
88  // EOF Found
89  break;
90  }
91  if ($prefix) {
92  $name = $prefix . '/' . $name;
93  }
94  if ((preg_match('#/$#', $name)) && !$name) {
95  $typeflag = 5;
96  }
97  if ($buffer == $null_512k) {
98  // it's the end of the tar-file...
99  break;
100  }
101 
102  // Read to the next chunk
103  fseek($this->getid3->fp, $size, SEEK_CUR);
104 
105  $diff = $size % 512;
106  if ($diff != 0) {
107  // Padding, throw away
108  fseek($this->getid3->fp, (512 - $diff), SEEK_CUR);
109  }
110  // Protect against tar-files with garbage at the end
111  if ($name == '') {
112  break;
113  }
114  $info['tar']['file_details'][$name] = array(
115  'name' => $name,
116  'mode_raw' => $mode,
117  'mode' => self::display_perms($mode),
118  'uid' => $uid,
119  'gid' => $gid,
120  'size' => $size,
121  'mtime' => $mtime,
122  'chksum' => $chksum,
123  'typeflag' => self::get_flag_type($typflag),
124  'linkname' => $lnkname,
125  'magic' => $magic,
126  'version' => $ver,
127  'uname' => $uname,
128  'gname' => $gname,
129  'devmajor' => $devmaj,
130  'devminor' => $devmin
131  );
132  $info['tar']['files'] = Helper::array_merge_clobber($info['tar']['files'],
133  Helper::CreateDeepArray($info['tar']['file_details'][$name]['name'],
134  '/',
135  $size));
136  }
137 
138  return true;
139  }
140 
147  public function display_perms($mode)
148  {
149  // Determine Type
150  if ($mode & 0x1000) $type = 'p'; // FIFO pipe
151  elseif ($mode & 0x2000) $type = 'c'; // Character special
152  elseif ($mode & 0x4000) $type = 'd'; // Directory
153  elseif ($mode & 0x6000) $type = 'b'; // Block special
154  elseif ($mode & 0x8000) $type = '-'; // Regular
155  elseif ($mode & 0xA000) $type = 'l'; // Symbolic Link
156  elseif ($mode & 0xC000) $type = 's'; // Socket
157  else $type = 'u'; // UNKNOWN
158 
159 
160 // Determine permissions
161  $owner['read'] = (($mode & 00400) ? 'r' : '-');
162  $owner['write'] = (($mode & 00200) ? 'w' : '-');
163  $owner['execute'] = (($mode & 00100) ? 'x' : '-');
164  $group['read'] = (($mode & 00040) ? 'r' : '-');
165  $group['write'] = (($mode & 00020) ? 'w' : '-');
166  $group['execute'] = (($mode & 00010) ? 'x' : '-');
167  $world['read'] = (($mode & 00004) ? 'r' : '-');
168  $world['write'] = (($mode & 00002) ? 'w' : '-');
169  $world['execute'] = (($mode & 00001) ? 'x' : '-');
170 
171  // Adjust for SUID, SGID and sticky bit
172  if ($mode & 0x800)
173  $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
174  if ($mode & 0x400)
175  $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
176  if ($mode & 0x200)
177  $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
178 
179  $s = sprintf('%1s', $type);
180  $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'],
181  $owner['execute']);
182  $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'],
183  $group['execute']);
184  $s .= sprintf('%1s%1s%1s' . "\n", $world['read'], $world['write'],
185  $world['execute']);
186 
187  return $s;
188  }
189 
197  public function get_flag_type($typflag)
198  {
199  static $flag_types = array(
200  '0' => 'LF_NORMAL',
201  '1' => 'LF_LINK',
202  '2' => 'LF_SYNLINK',
203  '3' => 'LF_CHR',
204  '4' => 'LF_BLK',
205  '5' => 'LF_DIR',
206  '6' => 'LF_FIFO',
207  '7' => 'LF_CONFIG',
208  'D' => 'LF_DUMPDIR',
209  'K' => 'LF_LONGLINK',
210  'L' => 'LF_LONGNAME',
211  'M' => 'LF_MULTIVOL',
212  'N' => 'LF_NAMES',
213  'S' => 'LF_SPARSE',
214  'V' => 'LF_VOLHDR'
215  );
216 
217  return (isset($flag_types[$typflag]) ? $flag_types[$typflag] : '');
218  }
219 }
static CreateDeepArray($ArrayPath, $Separator, $Value)
Definition: Helper.php:726
$size
Definition: RandomTest.php:79
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
GetId3() by James Heinrich info@getid3.org //.
Definition: Tar.php:35
display_perms($mode)
Parses the file mode to file permissions.
Definition: Tar.php:147
fseek($bytes, $whence=SEEK_SET)
$info
Definition: example_052.php:80
Create styles array
The data for the language used.
static array_merge_clobber($array1, $array2)
Definition: Helper.php:561
get_flag_type($typflag)
Converts the file type.
Definition: Tar.php:197