getid3_tar::display_perms()   F
last analyzed

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 36
c 1
b 0
f 0
nc 110592
nop 1
dl 0
loc 49
rs 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP version 5                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6
// +----------------------------------------------------------------------+
7
// | This source file is subject to version 2 of the GPL license,         |
8
// | that is bundled with this package in the file license.txt and is     |
9
// | available through the world-wide-web at the following url:           |
10
// | http://www.gnu.org/copyleft/gpl.html                                 |
11
// +----------------------------------------------------------------------+
12
// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13
// +----------------------------------------------------------------------+
14
// | Authors: James Heinrich <info�getid3*org>                            |
15
// |          Allan Hansen <ah�artemis*dk>                                |
16
// +----------------------------------------------------------------------+
17
// | module.archive.tar.php                                               |
18
// | module for analyzing TAR files                                       |
19
// | dependencies: NONE                                                   |
20
// +----------------------------------------------------------------------+
21
// | Module originally written by Mike Mozolin <teddybear�mail*ru>        |
22
// +----------------------------------------------------------------------+
23
//
24
// $Id: module.archive.tar.php,v 1.2 2006/11/02 10:48:00 ah Exp $
25
26
class getid3_tar extends getid3_handler
27
{
28
29
    public function Analyze()
30
    {
31
        $info = &$this->getid3->info;
32
33
        $info['fileformat'] = 'tar';
34
35
        $fp = $this->getid3->fp;
36
37
        fseek($fp, 0);
38
39
        $unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155/prefix';
40
41
        $null_512k = str_repeat("\0", 512); // end-of-file marker
42
43
        $already_warned = false;
44
45
        while (!feof($fp)) {
46
            $buffer = fread($fp, 512);
47
48
            // check the block
49
            $checksum = 0;
50
            for ($i = 0; $i < 148; $i++) {
51
                $checksum += ord(substr($buffer, $i, 1));
52
            }
53
            for ($i = 148; $i < 156; $i++) {
54
                $checksum += ord(' ');
55
            }
56
            for ($i = 156; $i < 512; $i++) {
57
                $checksum += ord(substr($buffer, $i, 1));
58
            }
59
            $attr    = unpack($unpack_header, $buffer);
60
            $name    = trim(@$attr['fname']);
61
            $mode    = octdec(trim(@$attr['mode']));
62
            $uid     = octdec(trim(@$attr['uid']));
63
            $gid     = octdec(trim(@$attr['gid']));
64
            $size    = octdec(trim(@$attr['size']));
65
            $mtime   = octdec(trim(@$attr['mtime']));
66
            $chksum  = octdec(trim(@$attr['chksum']));
67
            $typflag = trim(@$attr['typflag']);
68
            $lnkname = trim(@$attr['lnkname']);
69
            $magic   = trim(@$attr['magic']);
70
            $ver     = trim(@$attr['ver']);
71
            $uname   = trim(@$attr['uname']);
72
            $gname   = trim(@$attr['gname']);
73
            $devmaj  = octdec(trim(@$attr['devmaj']));
74
            $devmin  = octdec(trim(@$attr['devmin']));
75
            $prefix  = trim(@$attr['prefix']);
76
77
            // EOF Found
78
            if ((256 == $checksum) && (0 == $chksum)) {
79
                break;
80
            }
81
82
            // Check if filename if 7bit as spec requires
83
            if (!$already_warned) {
84
                for ($i = 0; $i < strlen($name); $i++) {
85
                    if ($name{$i} < chr(32) || $name{$i} > chr(127)) {
86
                        $this->getid3->warning('Some filenames contains extended characters, which breaks the tar specifation. This is not uncommon, but you will have to handle the character encoding for filenames yourself.');
87
                        $already_warned = true;
88
                        break;
89
                    }
90
                }
91
            }
92
93
            if ($prefix) {
94
                $name = $prefix . '/' . $name;
95
            }
96
            if ((preg_match('#/$#', $name)) && !$name) {
97
                $typeflag = 5;
0 ignored issues
show
Unused Code introduced by
The assignment to $typeflag is dead and can be removed.
Loading history...
98
            }
99
100
            // If it's the end of the tar-file...
101
            if ($buffer == $null_512k) {
102
                break;
103
            }
104
105
            // Protect against tar-files with garbage at the end
106
            if ('' == $name) {
107
                break;
108
            }
109
110
            $info['tar']['file_details'][$name] = [
111
                'name'     => $name,
112
                'mode_raw' => $mode,
113
                'mode'     => getid3_tar::display_perms($mode),
114
                'uid'      => $uid,
115
                'gid'      => $gid,
116
                'size'     => $size,
117
                'mtime'    => $mtime,
118
                'chksum'   => $chksum,
119
                'typeflag' => getid3_tar::get_flag_type($typflag),
120
                'linkname' => $lnkname,
121
                'magic'    => $magic,
122
                'version'  => $ver,
123
                'uname'    => $uname,
124
                'gname'    => $gname,
125
                'devmajor' => $devmaj,
126
                'devminor' => $devmin
127
            ];
128
129
            // Skip the next chunk
130
            fseek($fp, $size, SEEK_CUR);
0 ignored issues
show
Bug introduced by
It seems like $size can also be of type double; however, parameter $offset of fseek() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
            fseek($fp, /** @scrutinizer ignore-type */ $size, SEEK_CUR);
Loading history...
131
132
            // Throw away padding
133
            if ($size % 512) {
134
                fseek($fp, 512 - $diff, SEEK_CUR);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $diff seems to be never defined.
Loading history...
135
            }
136
        }
137
        return true;
138
    }
139
140
    // Parses the file mode to file permissions
141
    public static function display_perms($mode)
142
    {
143
        // Determine Type
144
        if ($mode & 0x1000) {
145
            $type = 'p'; // FIFO pipe
146
        } elseif ($mode & 0x2000) {
147
            $type = 'c'; // Character special
148
        } elseif ($mode & 0x4000) {
149
            $type = 'd'; // Directory
150
        } elseif ($mode & 0x6000) {
151
            $type = 'b'; // Block special
152
        } elseif ($mode & 0x8000) {
153
            $type = '-'; // Regular
154
        } elseif ($mode & 0xA000) {
155
            $type = 'l'; // Symbolic Link
156
        } elseif ($mode & 0xC000) {
157
            $type = 's'; // Socket
158
        } else {
159
            $type = 'u'; // UNKNOWN
160
        }
161
162
        // Determine permissions
163
        $owner['read']    = (($mode & 00400) ? 'r' : '-');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$owner was never initialized. Although not strictly required by PHP, it is generally a good practice to add $owner = array(); before regardless.
Loading history...
164
        $owner['write']   = (($mode & 00200) ? 'w' : '-');
165
        $owner['execute'] = (($mode & 00100) ? 'x' : '-');
166
        $group['read']    = (($mode & 00040) ? 'r' : '-');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$group was never initialized. Although not strictly required by PHP, it is generally a good practice to add $group = array(); before regardless.
Loading history...
167
        $group['write']   = (($mode & 00020) ? 'w' : '-');
168
        $group['execute'] = (($mode & 00010) ? 'x' : '-');
169
        $world['read']    = (($mode & 00004) ? 'r' : '-');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$world was never initialized. Although not strictly required by PHP, it is generally a good practice to add $world = array(); before regardless.
Loading history...
170
        $world['write']   = (($mode & 00002) ? 'w' : '-');
171
        $world['execute'] = (($mode & 00001) ? 'x' : '-');
172
173
        // Adjust for SUID, SGID and sticky bit
174
        if ($mode & 0x800) {
175
            $owner['execute'] = ('x' == $owner['execute']) ? 's' : 'S';
176
        }
177
        if ($mode & 0x400) {
178
            $group['execute'] = ('x' == $group['execute']) ? 's' : 'S';
179
        }
180
        if ($mode & 0x200) {
181
            $world['execute'] = ('x' == $world['execute']) ? 't' : 'T';
182
        }
183
184
        $s = sprintf('%1s', $type);
185
        $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
186
        $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
187
        $s .= sprintf('%1s%1s%1s' . "\n", $world['read'], $world['write'], $world['execute']);
188
189
        return $s;
190
    }
191
192
    // Converts the file type
193
    public static function get_flag_type($typflag)
194
    {
195
        static $flag_types = [
196
            '0' => 'LF_NORMAL',
197
            '1' => 'LF_LINK',
198
            '2' => 'LF_SYNLINK',
199
            '3' => 'LF_CHR',
200
            '4' => 'LF_BLK',
201
            '5' => 'LF_DIR',
202
            '6' => 'LF_FIFO',
203
            '7' => 'LF_CONFIG',
204
            'D' => 'LF_DUMPDIR',
205
            'K' => 'LF_LONGLINK',
206
            'L' => 'LF_LONGNAME',
207
            'M' => 'LF_MULTIVOL',
208
            'N' => 'LF_NAMES',
209
            'S' => 'LF_SPARSE',
210
            'V' => 'LF_VOLHDR'
211
        ];
212
213
        return @$flag_types[$typflag];
214
    }
215
216
}
217
218
219