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