Completed
Branch master (610f90)
by Michael
07:23 queued 02:42
created

pcltar.lib.php ➔ PclTarHandleExtractByIndexList()   D

Complexity

Conditions 14
Paths 72

Size

Total Lines 88
Code Lines 48

Duplication

Lines 7
Ratio 7.95 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 48
c 2
b 0
f 0
nc 72
nop 6
dl 7
loc 88
rs 4.9516

How to fix   Long Method    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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 34.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// --------------------------------------------------------------------------------
3
// PhpConcept Library - Tar Module 1.3.1
4
// --------------------------------------------------------------------------------
5
// License GNU/GPL - Vincent Blavet - January 2003
6
// http://www.phpconcept.net
7
// --------------------------------------------------------------------------------
8
//
9
// Presentation :
10
//   PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
11
//   to add files or directories, to extract all the archive or a part of it.
12
//   So far tests show that the files generated by PclTar are readable by
13
//   gzip tools and WinZip application.
14
//
15
// Description :
16
//   See readme.txt (English & Français) and http://www.phpconcept.net
17
//
18
// Warning :
19
//   This library and the associated files are non commercial, non professional
20
//   work.
21
//   It should not have unexpected results. However if any damage is caused by
22
//   this software the author can not be responsible.
23
//   The use of this software is at the risk of the user.
24
//
25
// --------------------------------------------------------------------------------
26
27
// ----- Look for double include
28
if (!defined('PCL_TAR')) {
29
    define('PCL_TAR', 1);
30
31
    // ----- Configuration variable
32
    // Theses values may be changed by the user of PclTar library
33
    if (!isset($gPcltarLibDir)) {
34
        $gPcltarLibDir = 'lib';
35
    }
36
37
    // ----- Error codes
38
    //   -1 : Unable to open file in binary write mode
39
    //   -2 : Unable to open file in binary read mode
40
    //   -3 : Invalid parameters
41
    //   -4 : File does not exist
42
    //   -5 : Filename is too long (max. 99)
43
    //   -6 : Not a valid tar file
44
    //   -7 : Invalid extracted file size
45
    //   -8 : Unable to create directory
46
    //   -9 : Invalid archive extension
47
    //  -10 : Invalid archive format
48
    //  -11 : Unable to delete file (unlink)
49
    //  -12 : Unable to rename file (rename)
50
    //  -13 : Invalid header checksum
51
52
    // --------------------------------------------------------------------------------
53
    // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
54
    // --------------------------------------------------------------------------------
55
56
    // ----- Global variables
57
    $g_pcltar_version = '1.3.1';
58
59
    // ----- Include other libraries
60
    // This library should be called by each script before the include of PhpZip
61
    // Library in order to limit the potential 'lib' directory path problem.
62
    if (!defined('PCLERROR_LIB')) {
63
        include $gPcltarLibDir.'/pclerror.lib.php';
64
    }
65
    if (!defined('PCLTRACE_LIB')) {
66
        include $gPcltarLibDir.'/pcltrace.lib.php';
67
    }
68
69
    // --------------------------------------------------------------------------------
70
    // Function : PclTarCreate()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
    // Description :
72
    //   Creates a new archive with name $p_tarname containing the files and/or
73
    //   directories indicated in $p_list. If the tar filename extension is
74
    //   ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
75
    //   it will be a gzip compressed tar archive.
76
    //   If you want to use an other extension, you must indicate the mode in
77
    //   $p_mode ("tar" or "tgz").
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
    //   $p_add_dir and $p_remove_dir give you the ability to store a path
79
    //   which is not the real path of the files.
80
    // Parameters :
81
    //   $p_tarname : Name of an existing tar file
82
    //   $p_filelist : An array containing file or directory names, or
83
    //                 a string containing one filename or directory name, or
84
    //                 a string containing a list of filenames and/or directory
85
    //                 names separated by spaces.
86
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
87
    //             if $p_mode is not specified, it will be determined by the extension.
88
    //   $p_add_dir : Path to add in the filename path archived
89
    //   $p_remove_dir : Path to remove in the filename path archived
90
    // Return Values :
91
    //   1 on success, or an error code (see table at the beginning).
92
    // --------------------------------------------------------------------------------
93
    /**
94
     * @param        $p_tarname
95
     * @param string $p_filelist
96
     * @param string $p_mode
97
     * @param string $p_add_dir
98
     * @param string $p_remove_dir
99
     *
100
     * @return int
101
     */
102
    function PclTarCreate($p_tarname, $p_filelist = '', $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
103
    {
104
        TrFctStart(__FILE__, __LINE__, 'PclTarCreate', "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
105
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
107
        // ----- Look for default mode
108 View Code Duplication
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            // ----- Extract the tar format from the extension
110
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
111
                // ----- Return
112
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
113
114
                return PclErrorCode();
115
            }
116
117
            // ----- Trace
118
            TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
119
        }
120
121
        // ----- Look if the $p_filelist is really an array
122 View Code Duplication
        if (is_array($p_filelist)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
            // ----- Call the create fct
124
            $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
125
        } // ----- Look if the $p_filelist is a string
126
        else {
127
            if (is_string($p_filelist)) {
128
                // ----- Create a list with the elements from the string
129
                $v_list = explode(' ', $p_filelist);
130
131
                // ----- Call the create fct
132
                $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
133
            } // ----- Invalid variable
134
            else {
135
                // ----- Error log
136
                PclErrorLog(-3, 'Invalid variable type p_filelist');
137
                $v_result = -3;
138
            }
139
        }
140
141
        // ----- Return
142
        TrFctEnd(__FILE__, __LINE__, $v_result);
143
144
        return $v_result;
145
    }
146
147
    // --------------------------------------------------------------------------------
148
149
    // --------------------------------------------------------------------------------
150
    // Function : PclTarAdd()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
151
    // Description :
152
    //   PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
153
    //
154
    //   This function is maintained only for compatibility reason
155
    //
156
    // Parameters :
157
    //   $p_tarname : Name of an existing tar file
158
    //   $p_filelist : An array containing file or directory names, or
159
    //                 a string containing one filename or directory name, or
160
    //                 a string containing a list of filenames and/or directory
161
    //                 names separated by spaces.
162
    // Return Values :
163
    //   1 on success,
164
    //   Or an error code (see list on top).
165
    // --------------------------------------------------------------------------------
166
    /**
167
     * @param $p_tarname
168
     * @param $p_filelist
169
     *
170
     * @return int
171
     */
172
    function PclTarAdd($p_tarname, $p_filelist)
173
    {
174
        TrFctStart(__FILE__, __LINE__, 'PclTarAdd', "tar=$p_tarname, file=$p_filelist");
175
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
        $v_list_detail = array();
177
178
        // ----- Extract the tar format from the extension
179
        if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
180
            // ----- Return
181
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
182
183
            return PclErrorCode();
184
        }
185
186
        // ----- Look if the $p_filelist is really an array
187
        if (is_array($p_filelist)) {
188
            // ----- Call the add fct
189
            $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, '', '');
190
        } // ----- Look if the $p_filelist is a string
191
        else {
192
            if (is_string($p_filelist)) {
193
                // ----- Create a list with the elements from the string
194
                $v_list = explode(' ', $p_filelist);
195
196
                // ----- Call the add fct
197
                $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, '', '');
198
            } // ----- Invalid variable
199
            else {
200
                // ----- Error log
201
                PclErrorLog(-3, 'Invalid variable type p_filelist');
202
                $v_result = -3;
203
            }
204
        }
205
206
        // ----- Cleaning
207
        unset($v_list_detail);
208
209
        // ----- Return
210
        TrFctEnd(__FILE__, __LINE__, $v_result);
211
212
        return $v_result;
213
    }
214
215
    // --------------------------------------------------------------------------------
216
217
    // --------------------------------------------------------------------------------
218
    // Function : PclTarAddList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
219
    // Description :
220
    //   Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
221
    //   The list can be an array of file/directory names or a string with names
222
    //   separated by one space.
223
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
224
    //   different from the real path of the file. This is usefull if you want to have PclTar
225
    //   running in any directory, and memorize relative path from an other directory.
226
    //   If $p_mode is not set it will be automatically computed from the $p_tarname
227
    //   extension (.tar, .tar.gz or .tgz).
228
    // Parameters :
229
    //   $p_tarname : Name of an existing tar file
230
    //   $p_filelist : An array containing file or directory names, or
231
    //                 a string containing one filename or directory name, or
232
    //                 a string containing a list of filenames and/or directory
233
    //                 names separated by spaces.
234
    //   $p_add_dir : Path to add in the filename path archived
235
    //   $p_remove_dir : Path to remove in the filename path archived
236
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
237
    // Return Values :
238
    //   1 on success,
239
    //   Or an error code (see list on top).
240
    // --------------------------------------------------------------------------------
241
    /**
242
     * @param        $p_tarname
243
     * @param        $p_filelist
244
     * @param string $p_add_dir
245
     * @param string $p_remove_dir
246
     * @param string $p_mode
247
     *
248
     * @return array|int
249
     */
250
    function PclTarAddList($p_tarname, $p_filelist, $p_add_dir = '', $p_remove_dir = '', $p_mode = '')
251
    {
252
        TrFctStart(__FILE__, __LINE__, 'PclTarAddList', "tar=$p_tarname, file=$p_filelist, p_add_dir='$p_add_dir', p_remove_dir='$p_remove_dir', mode=$p_mode");
253
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
254
        $p_list_detail = array();
255
256
        // ----- Extract the tar format from the extension
257
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
258
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
259
                // ----- Return
260
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
261
262
                return PclErrorCode();
263
            }
264
        }
265
266
        // ----- Look if the $p_filelist is really an array
267 View Code Duplication
        if (is_array($p_filelist)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
268
            // ----- Call the add fct
269
            $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
270
        } // ----- Look if the $p_filelist is a string
271
        else {
272
            if (is_string($p_filelist)) {
273
                // ----- Create a list with the elements from the string
274
                $v_list = explode(' ', $p_filelist);
275
276
                // ----- Call the add fct
277
                $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
278
            } // ----- Invalid variable
279
            else {
280
                // ----- Error log
281
                PclErrorLog(-3, 'Invalid variable type p_filelist');
282
                $v_result = -3;
283
            }
284
        }
285
286
        // ----- Return
287
        if ($v_result != 1) {
288
            TrFctEnd(__FILE__, __LINE__, 0);
289
290
            return 0;
291
        }
292
        TrFctEnd(__FILE__, __LINE__, $p_list_detail);
293
294
        return $p_list_detail;
295
    }
296
297
    // --------------------------------------------------------------------------------
298
299
    // --------------------------------------------------------------------------------
300
    // Function : PclTarList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
301
    // Description :
302
    //   Gives the list of all the files present in the tar archive $p_tarname.
303
    //   The list is the function result, it will be 0 on error.
304
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
305
    //   function will determine the type of the archive.
306
    // Parameters :
307
    //   $p_tarname : Name of an existing tar file
308
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
309
    // Return Values :
310
    //  0 on error (Use PclErrorCode() and PclErrorString() for more info)
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
311
    //  or
312
    //  An array containing file properties. Each file properties is an array of
313
    //  properties.
314
    //  The properties (array field names) are :
315
    //    filename, size, mode, uid, gid, mtime, typeflag, status
316
    //  Exemple : $v_list = PclTarList("my.tar");
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
317
    //            for ($i=0; $i<count($v_list); ++$i)
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
318
    //              echo "Filename :'".$v_list[$i]['filename']."'<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
319
    // --------------------------------------------------------------------------------
320
    /**
321
     * @param        $p_tarname
322
     * @param string $p_mode
323
     *
324
     * @return array|int
325
     */
326 View Code Duplication
    function PclTarList($p_tarname, $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
    {
328
        TrFctStart(__FILE__, __LINE__, 'PclTarList', "tar=$p_tarname, mode='$p_mode'");
329
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
330
331
        // ----- Extract the tar format from the extension
332
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
333
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
334
                // ----- Return
335
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
336
337
                return 0;
338
            }
339
        }
340
341
        // ----- Call the extracting fct
342
        $p_list = array();
343
        if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'list', '', $p_mode, '')) != 1) {
344
            unset($p_list);
345
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
346
347
            return 0;
348
        }
349
350
        // ----- Return
351
        TrFctEnd(__FILE__, __LINE__, $p_list);
352
353
        return $p_list;
354
    }
355
356
    // --------------------------------------------------------------------------------
357
358
    // --------------------------------------------------------------------------------
359
    // Function : PclTarExtract()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
360
    // Description :
361
    //   Extract all the files present in the archive $p_tarname, in the directory
362
    //   $p_path. The relative path of the archived files are keep and become
363
    //   relative to $p_path.
364
    //   If a file with the same name already exists it will be replaced.
365
    //   If the path to the file does not exist, it will be created.
366
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
367
    //   function will determine the type of the archive.
368
    // Parameters :
369
    //   $p_tarname : Name of an existing tar file.
370
    //   $p_path : Path where the files will be extracted. The files will use
371
    //             their memorized path from $p_path.
372
    //             If $p_path is "", files will be extracted in "./".
373
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
374
    //                    extracted files. If the path does not match the file path,
375
    //                    the file is extracted with its memorized path.
376
    //                    $p_path and $p_remove_path are commulative.
377
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
378
    // Return Values :
379
    //   Same as PclTarList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
380
    // --------------------------------------------------------------------------------
381
    /**
382
     * @param        $p_tarname
383
     * @param string $p_path
384
     * @param string $p_remove_path
385
     * @param string $p_mode
386
     *
387
     * @return int
388
     */
389 View Code Duplication
    function PclTarExtract($p_tarname, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
390
    {
391
        TrFctStart(__FILE__, __LINE__, 'PclTarExtract', "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
392
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
393
394
        // ----- Extract the tar format from the extension
395
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
396
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
397
                // ----- Return
398
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
399
400
                return 0;
401
            }
402
        }
403
404
        // ----- Call the extracting fct
405
        if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'complete', $p_path, $p_mode, $p_remove_path)) != 1) {
406
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
407
408
            return 0;
409
        }
410
411
        // ----- Return
412
        TrFctEnd(__FILE__, __LINE__, $p_list);
413
414
        return $p_list;
415
    }
416
417
    // --------------------------------------------------------------------------------
418
419
    // --------------------------------------------------------------------------------
420
    // Function : PclTarExtractList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
421
    // Description :
422
    //   Extract the files present in the archive $p_tarname and specified in
423
    //   $p_filelist, in the directory
424
    //   $p_path. The relative path of the archived files are keep and become
425
    //   relative to $p_path.
426
    //   If a directory is spécified in the list, all the files from this directory
427
    //   will be extracted.
428
    //   If a file with the same name already exists it will be replaced.
429
    //   If the path to the file does not exist, it will be created.
430
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
431
    //   function will determine the type of the archive.
432
    // Parameters :
433
    //   $p_tarname : Name of an existing tar file
434
    //   $p_filelist : An array containing file or directory names, or
435
    //                 a string containing one filename or directory name, or
436
    //                 a string containing a list of filenames and/or directory
437
    //                 names separated by spaces.
438
    //   $p_path : Path where the files will be extracted. The files will use
439
    //             their memorized path from $p_path.
440
    //             If $p_path is "", files will be extracted in "./".
441
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
442
    //                    extracted files. If the path does not match the file path,
443
    //                    the file is extracted with its memorized path.
444
    //                    $p_path and $p_remove_path are commulative.
445
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
446
    // Return Values :
447
    //   Same as PclTarList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
448
    // --------------------------------------------------------------------------------
449
    /**
450
     * @param        $p_tarname
451
     * @param        $p_filelist
452
     * @param string $p_path
453
     * @param string $p_remove_path
454
     * @param string $p_mode
455
     *
456
     * @return int
457
     */
458 View Code Duplication
    function PclTarExtractList($p_tarname, $p_filelist, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
459
    {
460
        TrFctStart(__FILE__, __LINE__, 'PclTarExtractList', "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
461
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
462
463
        // ----- Extract the tar format from the extension
464
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
465
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
466
                // ----- Return
467
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
468
469
                return 0;
470
            }
471
        }
472
473
        // ----- Look if the $p_filelist is really an array
474
        if (is_array($p_filelist)) {
475
            // ----- Call the extracting fct
476
            if (($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path)) != 1) {
0 ignored issues
show
Bug introduced by
The variable $v_tar_mode does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
477
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
478
479
                return 0;
480
            }
481
        } // ----- Look if the $p_filelist is a string
482
        else {
483
            if (is_string($p_filelist)) {
484
                // ----- Create a list with the elements from the string
485
                $v_list = explode(' ', $p_filelist);
486
487
                // ----- Call the extracting fct
488
                if (($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path)) != 1) {
489
                    TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
490
491
                    return 0;
492
                }
493
            } // ----- Invalid variable
494
            else {
495
                // ----- Error log
496
                PclErrorLog(-3, 'Invalid variable type p_filelist');
497
498
                // ----- Return
499
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
500
501
                return 0;
502
            }
503
        }
504
505
        // ----- Return
506
        TrFctEnd(__FILE__, __LINE__, $p_list);
507
508
        return $p_list;
509
    }
510
511
    // --------------------------------------------------------------------------------
512
513
    // --------------------------------------------------------------------------------
514
    // Function : PclTarExtractIndex()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
515
    // Description :
516
    //   Extract the files present in the archive $p_tarname and specified at
517
    //   the indexes in $p_index, in the directory
518
    //   $p_path. The relative path of the archived files are keep and become
519
    //   relative to $p_path.
520
    //   If a directory is specified in the list, the directory only is created. All
521
    //   the file stored in this archive for this directory
522
    //   are not extracted.
523
    //   If a file with the same name already exists it will be replaced.
524
    //   If the path to the file does not exist, it will be created.
525
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
526
    //   function will determine the type of the archive.
527
    // Parameters :
528
    //   $p_tarname : Name of an existing tar file
529
    //   $p_index : A single index (integer) or a string of indexes of files to
530
    //              extract. The form of the string is "0,4-6,8-12" with only numbers
531
    //              and '-' for range or ',' to separate ranges. No spaces or ';'
532
    //              are allowed.
533
    //   $p_path : Path where the files will be extracted. The files will use
534
    //             their memorized path from $p_path.
535
    //             If $p_path is "", files will be extracted in "./".
536
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
537
    //                    extracted files. If the path does not match the file path,
538
    //                    the file is extracted with its memorized path.
539
    //                    $p_path and $p_remove_path are commulative.
540
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
541
    // Return Values :
542
    //   Same as PclTarList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
543
    // --------------------------------------------------------------------------------
544
    /**
545
     * @param        $p_tarname
546
     * @param        $p_index
547
     * @param string $p_path
548
     * @param string $p_remove_path
549
     * @param string $p_mode
550
     *
551
     * @return int
552
     */
553 View Code Duplication
    function PclTarExtractIndex($p_tarname, $p_index, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
554
    {
555
        TrFctStart(__FILE__, __LINE__, 'PclTarExtractIndex', "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
556
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
557
558
        // ----- Extract the tar format from the extension
559
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
560
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
561
                // ----- Return
562
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
563
564
                return 0;
565
            }
566
        }
567
568
        // ----- Look if the $p_index is really an integer
569
        if (is_int($p_index)) {
570
            // ----- Call the extracting fct
571
            if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1) {
0 ignored issues
show
Bug introduced by
The variable $v_tar_mode does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
572
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
573
574
                return 0;
575
            }
576
        } // ----- Look if the $p_filelist is a string
577
        else {
578
            if (is_string($p_index)) {
579
                // ----- Call the extracting fct
580
                if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1) {
581
                    TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
582
583
                    return 0;
584
                }
585
            } // ----- Invalid variable
586
            else {
587
                // ----- Error log
588
                PclErrorLog(-3, "Invalid variable type $p_index");
589
590
                // ----- Return
591
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
592
593
                return 0;
594
            }
595
        }
596
597
        // ----- Return
598
        TrFctEnd(__FILE__, __LINE__, $p_list);
599
600
        return $p_list;
601
    }
602
603
    // --------------------------------------------------------------------------------
604
605
    // --------------------------------------------------------------------------------
606
    // Function : PclTarDelete()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
607
    // Description :
608
    //   This function deletes from the archive $p_tarname the files which are listed
609
    //   in $p_filelist. $p_filelist can be a string with file names separated by
610
    //   spaces, or an array containing the file names.
611
    // Parameters :
612
    //   $p_tarname : Name of an existing tar file
613
    //   $p_filelist : An array or a string containing file names to remove from the
614
    //                 archive.
615
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
616
    // Return Values :
617
    //   List of the files which are kept in the archive (same format as PclTarList())
618
    // --------------------------------------------------------------------------------
619
    /**
620
     * @param        $p_tarname
621
     * @param        $p_filelist
622
     * @param string $p_mode
623
     *
624
     * @return int
625
     */
626
    function PclTarDelete($p_tarname, $p_filelist, $p_mode = '')
627
    {
628
        TrFctStart(__FILE__, __LINE__, 'PclTarDelete', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
629
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
630
631
        // ----- Extract the tar format from the extension
632
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
633
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
634
                // ----- Return
635
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
636
637
                return 0;
638
            }
639
        }
640
641
        // ----- Look if the $p_filelist is really an array
642
        if (is_array($p_filelist)) {
643
            // ----- Call the extracting fct
644
            if (($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode)) != 1) {
645
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
646
647
                return 0;
648
            }
649
        } // ----- Look if the $p_filelist is a string
650
        else {
651
            if (is_string($p_filelist)) {
652
                // ----- Create a list with the elements from the string
653
                $v_list = explode(' ', $p_filelist);
654
655
                // ----- Call the extracting fct
656
                if (($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode)) != 1) {
657
                    TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
658
659
                    return 0;
660
                }
661
            } // ----- Invalid variable
662
            else {
663
                // ----- Error log
664
                PclErrorLog(-3, 'Invalid variable type p_filelist');
665
666
                // ----- Return
667
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
668
669
                return 0;
670
            }
671
        }
672
673
        // ----- Return
674
        TrFctEnd(__FILE__, __LINE__, $p_list);
675
676
        return $p_list;
677
    }
678
679
    // --------------------------------------------------------------------------------
680
681
    // --------------------------------------------------------------------------------
682
    // Function : PclTarUpdate()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
683
    // Description :
684
    //   This function updates the files in $p_filelist which are already in the
685
    //   $p_tarname archive with an older last modified date. If the file does not
686
    //   exist, it is added at the end of the archive.
687
    // Parameters :
688
    //   $p_tarname : Name of an existing tar file
689
    //   $p_filelist : An array or a string containing file names to update from the
690
    //                 archive.
691
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
692
    // Return Values :
693
    //   List of the files contained in the archive. The field status contains
694
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
695
    // --------------------------------------------------------------------------------
696
    /**
697
     * @param        $p_tarname
698
     * @param        $p_filelist
699
     * @param string $p_mode
700
     * @param string $p_add_dir
701
     * @param string $p_remove_dir
702
     *
703
     * @return int
704
     */
705 View Code Duplication
    function PclTarUpdate($p_tarname, $p_filelist, $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
706
    {
707
        TrFctStart(__FILE__, __LINE__, 'PclTarUpdate', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
708
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
709
710
        // ----- Extract the tar format from the extension
711
        if (($p_mode == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
712
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
713
                // ----- Return
714
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
715
716
                return 0;
717
            }
718
        }
719
720
        // ----- Look if the $p_filelist is really an array
721
        if (is_array($p_filelist)) {
722
            // ----- Call the extracting fct
723
            if (($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1) {
724
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
725
726
                return 0;
727
            }
728
        } // ----- Look if the $p_filelist is a string
729
        else {
730
            if (is_string($p_filelist)) {
731
                // ----- Create a list with the elements from the string
732
                $v_list = explode(' ', $p_filelist);
733
734
                // ----- Call the extracting fct
735
                if (($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1) {
736
                    TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
737
738
                    return 0;
739
                }
740
            } // ----- Invalid variable
741
            else {
742
                // ----- Error log
743
                PclErrorLog(-3, 'Invalid variable type p_filelist');
744
745
                // ----- Return
746
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
747
748
                return 0;
749
            }
750
        }
751
752
        // ----- Return
753
        TrFctEnd(__FILE__, __LINE__, $p_list);
754
755
        return $p_list;
756
    }
757
758
    // --------------------------------------------------------------------------------
759
760
    // --------------------------------------------------------------------------------
761
    // Function : PclTarMerge()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
762
    // Description :
763
    //   This function add the content of $p_tarname_add at the end of $p_tarname.
764
    // Parameters :
765
    //   $p_tarname : Name of an existing tar file
766
    //   $p_tarname_add : Name of an existing tar file taht will be added at the end
767
    //                    of $p_tarname.
768
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
769
    //   $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
770
    //                 extension
771
    // Return Values :
772
    //   List of the files contained in the archive. The field status contains
773
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
774
    // --------------------------------------------------------------------------------
775
    /**
776
     * @param        $p_tarname
777
     * @param        $p_tarname_add
778
     * @param string $p_mode
779
     * @param string $p_mode_add
780
     *
781
     * @return int
782
     */
783
    function PclTarMerge($p_tarname, $p_tarname_add, $p_mode = '', $p_mode_add = '')
784
    {
785
        TrFctStart(__FILE__, __LINE__, 'PclTarMerge', "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
786
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
787
788
        // ----- Check the parameters
789
        if (($p_tarname == '') || ($p_tarname_add == '')) {
790
            // ----- Error log
791
            PclErrorLog(-3, 'Invalid empty archive name');
792
793
            // ----- Return
794
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
795
796
            return PclErrorCode();
797
        }
798
799
        // ----- Extract the tar format from the extension
800
        if (($p_mode === '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
801
            if (($p_mode = PclTarHandleExtension($p_tarname)) == '') {
802
                // ----- Return
803
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
804
805
                return 0;
806
            }
807
        }
808
        if (($p_mode_add === '') || (($p_mode_add !== 'tar') && ($p_mode_add !== 'tgz'))) {
809
            if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == '') {
810
                // ----- Return
811
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
812
813
                return 0;
814
            }
815
        }
816
817
        // ----- Clear filecache
818
        clearstatcache();
819
820
        // ----- Check the file size
821 View Code Duplication
        if ((!is_file($p_tarname)) || (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode === 'tar'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
822
            // ----- Error log
823
            if (!is_file($p_tarname)) {
824
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
825
            } else {
826
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname).'(not a 512 block multiple)');
827
            }
828
829
            // ----- Return
830
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
831
832
            return PclErrorCode();
833
        }
834 View Code Duplication
        if ((!is_file($p_tarname_add))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
835
            || (((($v_size_add = filesize($p_tarname_add)) % 512) != 0)
836
                && ($p_mode_add === 'tar'))
837
        ) {
838
            // ----- Error log
839
            if (!is_file($p_tarname_add)) {
840
                PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
841
            } else {
842
                PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size ".filesize($p_tarname_add).'(not a 512 block multiple)');
843
            }
844
845
            // ----- Return
846
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
847
848
            return PclErrorCode();
849
        }
850
851
        // ----- Look for compressed archive
852
        if ($p_mode === 'tgz') {
853
            // ----- Open the file in read mode
854
            if (($p_tar = @gzopen($p_tarname, 'rb')) == 0) {
855
                // ----- Error log
856
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
857
858
                // ----- Return
859
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
860
861
                return PclErrorCode();
862
            }
863
864
            // ----- Open a temporary file in write mode
865
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
866
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
867
            if (($v_temp_tar = @gzopen($v_temp_tarname, 'wb')) == 0) {
868
                // ----- Close tar file
869
                gzclose($p_tar);
870
871
                // ----- Error log
872
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
873
874
                // ----- Return
875
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
876
877
                return PclErrorCode();
878
            }
879
880
            // ----- Read the first 512 bytes block
881
            $v_buffer = gzread($p_tar, 512);
882
883
            // ----- Read the following blocks but not the last one
884 View Code Duplication
            if (!gzeof($p_tar)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
885
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
886
                $i = 1;
887
888
                // ----- Read new 512 block and write the already read
889
                do {
890
                    // ----- Write the already read block
891
                    $v_binary_data = pack('a512', "$v_buffer");
892
                    gzputs($v_temp_tar, $v_binary_data);
893
894
                    ++$i;
895
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
896
897
                    // ----- Read next block
898
                    $v_buffer = gzread($p_tar, 512);
899
                } while (!gzeof($p_tar));
900
901
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
902
            }
903
        } // ----- Look for uncompressed tar file
904
        else {
905
            if ($p_mode === 'tar') {
906
                // ----- Open the tar file
907
                if (($p_tar = fopen($p_tarname, 'r+b')) == 0) {
908
                    // ----- Error log
909
                    PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
910
911
                    // ----- Return
912
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
913
914
                    return PclErrorCode();
915
                }
916
917
                // ----- Go to the beginning of last block
918
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :'.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
919
                fseek($p_tar, $v_size - 512);
920
                TrFctMessage(__FILE__, __LINE__, 4, 'Position after :'.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
921
            } // ----- Look for unknown type
922
            else {
923
                // ----- Error log
924
                PclErrorLog(-3, "Invalid tar mode $p_mode");
925
926
                // ----- Return
927
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
928
929
                return PclErrorCode();
930
            }
931
        }
932
933
        // ----- Look for type of archive to add
934
        if ($p_mode_add === 'tgz') {
935
            TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
936
937
            // ----- Open the file in read mode
938
            if (($p_tar_add = @gzopen($p_tarname_add, 'rb')) == 0) {
939
                // ----- Error log
940
                PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
941
942
                // ----- Return
943
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
944
945
                return PclErrorCode();
946
            }
947
948
            // ----- Read the first 512 bytes block
949
            $v_buffer = gzread($p_tar_add, 512);
950
951
            // ----- Read the following blocks but not the last one
952
            if (!gzeof($p_tar_add)) {
953
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
954
                $i = 1;
955
956
                // ----- Read new 512 block and write the already read
957
                do {
958
                    // ----- Write the already read block
959
                    $v_binary_data = pack('a512', "$v_buffer");
960
                    if ($p_mode === 'tar') {
961
                        fwrite($p_tar, $v_binary_data);
962
                    } else {
963
                        gzputs($v_temp_tar, $v_binary_data);
0 ignored issues
show
Bug introduced by
The variable $v_temp_tar does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
964
                    }
965
966
                    ++$i;
967
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
968
969
                    // ----- Read next block
970
                    $v_buffer = gzread($p_tar_add, 512);
971
                } while (!gzeof($p_tar_add));
972
973
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
974
            }
975
976
            // ----- Close the files
977
            gzclose($p_tar_add);
978
        } // ----- Look for uncompressed tar file
979
        else {
980
            if ($p_mode === 'tar') {
981
                // ----- Open the file in read mode
982
                if (($p_tar_add = @fopen($p_tarname_add, 'rb')) == 0) {
983
                    // ----- Error log
984
                    PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
985
986
                    // ----- Return
987
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
988
989
                    return PclErrorCode();
990
                }
991
992
                // ----- Read the first 512 bytes block
993
                $v_buffer = fread($p_tar_add, 512);
994
995
                // ----- Read the following blocks but not the last one
996
                if (!feof($p_tar_add)) {
997
                    TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
998
                    $i = 1;
999
1000
                    // ----- Read new 512 block and write the already read
1001
                    do {
1002
                        // ----- Write the already read block
1003
                        $v_binary_data = pack('a512', "$v_buffer");
1004
                        if ($p_mode === 'tar') {
1005
                            fwrite($p_tar, $v_binary_data);
1006
                        } else {
1007
                            gzputs($v_temp_tar, $v_binary_data);
1008
                        }
1009
1010
                        ++$i;
1011
                        TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
1012
1013
                        // ----- Read next block
1014
                        $v_buffer = fread($p_tar_add, 512);
1015
                    } while (!feof($p_tar_add));
1016
1017
                    TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1018
                }
1019
1020
                // ----- Close the files
1021
                fclose($p_tar_add);
1022
            }
1023
        }
1024
1025
        // ----- Call the footer of the tar archive
1026
        $v_result = PclTarHandleFooter($p_tar, $p_mode);
1027
1028
        // ----- Look for closing compressed archive
1029
        if ($p_mode === 'tgz') {
1030
            // ----- Close the files
1031
            gzclose($p_tar);
1032
            gzclose($v_temp_tar);
1033
1034
            // ----- Unlink tar file
1035
            if (!@unlink($p_tarname)) {
1036
                // ----- Error log
1037
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1038
            }
1039
1040
            // ----- Rename tar file
1041
            if (!@rename($v_temp_tarname, $p_tarname)) {
1042
                // ----- Error log
1043
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
0 ignored issues
show
Bug introduced by
The variable $v_temp_tarname does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1044
1045
                // ----- Return
1046
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1047
1048
                return PclErrorCode();
1049
            }
1050
1051
            // ----- Return
1052
            TrFctEnd(__FILE__, __LINE__, $v_result);
1053
1054
            return $v_result;
1055
        } // ----- Look for closing uncompressed tar file
1056
        else {
1057
            if ($p_mode === 'tar') {
1058
                // ----- Close the tarfile
1059
                fclose($p_tar);
1060
            }
1061
        }
1062
1063
        // ----- Return
1064
        TrFctEnd(__FILE__, __LINE__, $v_result);
1065
1066
        return $v_result;
1067
    }
1068
1069
    // --------------------------------------------------------------------------------
1070
1071
    // --------------------------------------------------------------------------------
1072
    // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1073
    // *****                                                        *****
1074
    // *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1075
    // --------------------------------------------------------------------------------
1076
1077
    // --------------------------------------------------------------------------------
1078
    // Function : PclTarHandleCreate()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1079
    // Description :
1080
    // Parameters :
1081
    //   $p_tarname : Name of the tar file
1082
    //   $p_list : An array containing the file or directory names to add in the tar
1083
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1084
    // Return Values :
1085
    // --------------------------------------------------------------------------------
1086
    /**
1087
     * @param        $p_tarname
1088
     * @param        $p_list
1089
     * @param        $p_mode
1090
     * @param string $p_add_dir
1091
     * @param string $p_remove_dir
1092
     *
1093
     * @return int
1094
     */
1095
    function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir = '', $p_remove_dir = '')
1096
    {
1097
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleCreate', "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1098
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1099
        $v_list_detail = array();
1100
1101
        // ----- Check the parameters
1102 View Code Duplication
        if (($p_tarname == '') || (($p_mode !== 'tar') && ($p_mode !== 'tgz'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1103
            // ----- Error log
1104
            if ($p_tarname == '') {
1105
                PclErrorLog(-3, 'Invalid empty archive name');
1106
            } else {
1107
                PclErrorLog(-3, "Unknown mode '$p_mode'");
1108
            }
1109
1110
            // ----- Return
1111
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1112
1113
            return PclErrorCode();
1114
        }
1115
1116
        // ----- Look for tar file
1117
        if ($p_mode === 'tar') {
1118
            // ----- Open the tar file
1119
            if (($p_tar = fopen($p_tarname, 'wb')) == 0) {
1120
                // ----- Error log
1121
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1122
1123
                // ----- Return
1124
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1125
1126
                return PclErrorCode();
1127
            }
1128
1129
            // ----- Call the adding fct inside the tar
1130
            if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1) {
1131
                // ----- Call the footer of the tar archive
1132
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1133
            }
1134
1135
            // ----- Close the tarfile
1136
            fclose($p_tar);
1137
        } // ----- Look for tgz file
1138
        else {
1139
            // ----- Open the tar file
1140
            if (($p_tar = @gzopen($p_tarname, 'wb')) == 0) {
1141
                // ----- Error log
1142
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1143
1144
                // ----- Return
1145
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1146
1147
                return PclErrorCode();
1148
            }
1149
1150
            // ----- Call the adding fct inside the tar
1151
            if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1) {
1152
                // ----- Call the footer of the tar archive
1153
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1154
            }
1155
1156
            // ----- Close the tarfile
1157
            gzclose($p_tar);
1158
        }
1159
1160
        // ----- Return
1161
        TrFctEnd(__FILE__, __LINE__, $v_result);
1162
1163
        return $v_result;
1164
    }
1165
1166
    // --------------------------------------------------------------------------------
1167
1168
    // --------------------------------------------------------------------------------
1169
    // Function : PclTarHandleAppend()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1170
    // Description :
1171
    // Parameters :
1172
    //   $p_tarname : Name of the tar file
1173
    //   $p_list : An array containing the file or directory names to add in the tar
1174
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1175
    // Return Values :
1176
    // --------------------------------------------------------------------------------
1177
    /**
1178
     * @param $p_tarname
1179
     * @param $p_list
1180
     * @param $p_mode
1181
     * @param $p_list_detail
1182
     * @param $p_add_dir
1183
     * @param $p_remove_dir
1184
     *
1185
     * @return int
1186
     */
1187
    function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1188
    {
1189
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAppend', "tar=$p_tarname, list, mode=$p_mode");
1190
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1191
1192
        // ----- Check the parameters
1193
        if ($p_tarname == '') {
1194
            // ----- Error log
1195
            PclErrorLog(-3, 'Invalid empty archive name');
1196
1197
            // ----- Return
1198
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1199
1200
            return PclErrorCode();
1201
        }
1202
1203
        clearstatcache();
1204
1205
        // ----- Check the file size
1206 View Code Duplication
        if ((!is_file($p_tarname)) || (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode === 'tar'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1207
            // ----- Error log
1208
            if (!is_file($p_tarname)) {
1209
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
1210
            } else {
1211
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname).'(not a 512 block multiple)');
1212
            }
1213
1214
            // ----- Return
1215
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1216
1217
            return PclErrorCode();
1218
        }
1219
1220
        // ----- Look for compressed archive
1221
        if ($p_mode === 'tgz') {
1222
            // ----- Open the file in read mode
1223
            if (($p_tar = @gzopen($p_tarname, 'rb')) == 0) {
1224
                // ----- Error log
1225
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
1226
1227
                // ----- Return
1228
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1229
1230
                return PclErrorCode();
1231
            }
1232
1233
            // ----- Open a temporary file in write mode
1234
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
1235
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
1236
            if (($v_temp_tar = @gzopen($v_temp_tarname, 'wb')) == 0) {
1237
                // ----- Close tar file
1238
                gzclose($p_tar);
1239
1240
                // ----- Error log
1241
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
1242
1243
                // ----- Return
1244
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1245
1246
                return PclErrorCode();
1247
            }
1248
1249
            // ----- Read the first 512 bytes block
1250
            $v_buffer = gzread($p_tar, 512);
1251
1252
            // ----- Read the following blocks but not the last one
1253 View Code Duplication
            if (!gzeof($p_tar)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1254
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
1255
                $i = 1;
1256
1257
                // ----- Read new 512 block and write the already read
1258
                do {
1259
                    // ----- Write the already read block
1260
                    $v_binary_data = pack('a512', "$v_buffer");
1261
                    gzputs($v_temp_tar, $v_binary_data);
1262
1263
                    ++$i;
1264
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
1265
1266
                    // ----- Read next block
1267
                    $v_buffer = gzread($p_tar, 512);
1268
                } while (!gzeof($p_tar));
1269
1270
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1271
            }
1272
1273
            // ----- Call the adding fct inside the tar
1274 View Code Duplication
            if (($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1275
                // ----- Call the footer of the tar archive
1276
                $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
1277
            }
1278
1279
            // ----- Close the files
1280
            gzclose($p_tar);
1281
            gzclose($v_temp_tar);
1282
1283
            // ----- Unlink tar file
1284
            if (!@unlink($p_tarname)) {
1285
                // ----- Error log
1286
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1287
            }
1288
1289
            // ----- Rename tar file
1290
            if (!@rename($v_temp_tarname, $p_tarname)) {
1291
                // ----- Error log
1292
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
1293
1294
                // ----- Return
1295
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1296
1297
                return PclErrorCode();
1298
            }
1299
1300
            // ----- Return
1301
            TrFctEnd(__FILE__, __LINE__, $v_result);
1302
1303
            return $v_result;
1304
        } // ----- Look for uncompressed tar file
1305
        else {
1306
            if ($p_mode === 'tar') {
1307
                // ----- Open the tar file
1308
                if (($p_tar = fopen($p_tarname, 'r+b')) == 0) {
1309
                    // ----- Error log
1310
                    PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
1311
1312
                    // ----- Return
1313
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1314
1315
                    return PclErrorCode();
1316
                }
1317
1318
                // ----- Go to the beginning of last block
1319
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :'.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1320
                fseek($p_tar, $v_size - 512);
1321
                TrFctMessage(__FILE__, __LINE__, 4, 'Position after :'.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1322
1323
                // ----- Call the adding fct inside the tar
1324 View Code Duplication
                if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1325
                    // ----- Call the footer of the tar archive
1326
                    $v_result = PclTarHandleFooter($p_tar, $p_mode);
1327
                }
1328
1329
                // ----- Close the tarfile
1330
                fclose($p_tar);
1331
            } // ----- Look for unknown type
1332
            else {
1333
                // ----- Error log
1334
                PclErrorLog(-3, "Invalid tar mode $p_mode");
1335
1336
                // ----- Return
1337
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1338
1339
                return PclErrorCode();
1340
            }
1341
        }
1342
1343
        // ----- Return
1344
        TrFctEnd(__FILE__, __LINE__, $v_result);
1345
1346
        return $v_result;
1347
    }
1348
1349
    // --------------------------------------------------------------------------------
1350
1351
    // --------------------------------------------------------------------------------
1352
    // Function : PclTarHandleAddList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1353
    // Description :
1354
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
1355
    //   different from the real path of the file. This is usefull if you want to have PclTar
1356
    //   running in any directory, and memorize relative path from an other directory.
1357
    // Parameters :
1358
    //   $p_tar : File descriptor of the tar archive
1359
    //   $p_list : An array containing the file or directory names to add in the tar
1360
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1361
    //   $p_list_detail : list of added files with their properties (specially the status field)
1362
    //   $p_add_dir : Path to add in the filename path archived
1363
    //   $p_remove_dir : Path to remove in the filename path archived
1364
    // Return Values :
1365
    // --------------------------------------------------------------------------------
1366
    /**
1367
     * @param $p_tar
1368
     * @param $p_list
1369
     * @param $p_mode
1370
     * @param $p_list_detail
1371
     * @param $p_add_dir
1372
     * @param $p_remove_dir
1373
     *
1374
     * @return int
1375
     */
1376
    function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1377
    {
1378
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddList', "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1379
        $v_result = 1;
1380
        $v_header = array();
1381
1382
        // ----- Recuperate the current number of elt in list
1383
        $v_nb = count($p_list_detail);
1384
1385
        // ----- Check the parameters
1386 View Code Duplication
        if ($p_tar == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1387
            // ----- Error log
1388
            PclErrorLog(-3, 'Invalid file descriptor in file '.__FILE__.', line '.__LINE__);
1389
1390
            // ----- Return
1391
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1392
1393
            return PclErrorCode();
1394
        }
1395
1396
        // ----- Check the arguments
1397
        if (count($p_list) == 0) {
1398
            // ----- Error log
1399
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1400
1401
            // ----- Return
1402
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1403
1404
            return PclErrorCode();
1405
        }
1406
1407
        // ----- Loop on the files
1408
        for ($j = 0; ($j < count($p_list)) && ($v_result == 1); ++$j) {
1409
            // ----- Recuperate the filename
1410
            $p_filename = $p_list[$j];
1411
1412
            TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
1413
1414
            // ----- Skip empty file names
1415
            if ($p_filename == '') {
1416
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip empty filename');
1417
                continue;
1418
            }
1419
1420
            // ----- Check the filename
1421
            if (!file_exists($p_filename)) {
1422
                // ----- Error log
1423
                TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
1424
                PclErrorLog(-4, "File '$p_filename' does not exists");
1425
1426
                // ----- Return
1427
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1428
1429
                return PclErrorCode();
1430
            }
1431
1432
            // ----- Check the path length
1433
            if (strlen($p_filename) > 99) {
1434
                // ----- Error log
1435
                PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
1436
1437
                // ----- Return
1438
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1439
1440
                return PclErrorCode();
1441
            }
1442
1443
            TrFctMessage(__FILE__, __LINE__, 4, 'File position before header ='.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1444
1445
            // ----- Add the file
1446 View Code Duplication
            if (($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1447
                // ----- Return status
1448
                TrFctEnd(__FILE__, __LINE__, $v_result);
1449
1450
                return $v_result;
1451
            }
1452
1453
            // ----- Store the file infos
1454
            $p_list_detail[$v_nb++] = $v_header;
1455
1456
            // ----- Look for directory
1457
            if (is_dir($p_filename)) {
1458
                TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
1459
1460
                // ----- Look for path
1461
                $v_path = '';
1462
                if ($p_filename !== '.') {
1463
                    $v_path = $p_filename.'/';
1464
                }
1465
1466
                // ----- Read the directory for files and sub-directories
1467
                $p_hdir = opendir($p_filename);
1468
                $p_hitem = readdir($p_hdir); // '.' directory
0 ignored issues
show
Unused Code introduced by
$p_hitem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1469
                $p_hitem = readdir($p_hdir); // '..' directory
0 ignored issues
show
Unused Code introduced by
$p_hitem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1470
                while ($p_hitem = readdir($p_hdir)) {
1471
                    // ----- Look for a file
1472
                    if (is_file($v_path.$p_hitem)) {
1473
                        TrFctMessage(__FILE__, __LINE__, 4, "Add the file '".$v_path.$p_hitem."'");
1474
1475
                        // ----- Add the file
1476 View Code Duplication
                        if (($v_result = PclTarHandleAddFile($p_tar, $v_path.$p_hitem, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1477
                            // ----- Return status
1478
                            TrFctEnd(__FILE__, __LINE__, $v_result);
1479
1480
                            return $v_result;
1481
                        }
1482
1483
                        // ----- Store the file infos
1484
                        $p_list_detail[$v_nb++] = $v_header;
1485
                    } // ----- Recursive call to PclTarHandleAddFile()
1486
                    else {
1487
                        TrFctMessage(__FILE__, __LINE__, 4, "'".$v_path.$p_hitem."' is a directory");
1488
1489
                        // ----- Need an array as parameter
1490
                        $p_temp_list[0] = $v_path.$p_hitem;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$p_temp_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $p_temp_list = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1491
                        $v_result = PclTarHandleAddList($p_tar, $p_temp_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
0 ignored issues
show
Bug introduced by
The variable $p_temp_list does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1492
                    }
1493
                }
1494
1495
                // ----- Free memory for the recursive loop
1496
                unset($p_temp_list, $p_hdir, $p_hitem);
1497
            } else {
1498
                TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks ='.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1499
            }
1500
        }
1501
1502
        // ----- Return
1503
        TrFctEnd(__FILE__, __LINE__, $v_result);
1504
1505
        return $v_result;
1506
    }
1507
1508
    // --------------------------------------------------------------------------------
1509
1510
    // --------------------------------------------------------------------------------
1511
    // Function : PclTarHandleAddFile()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1512
    // Description :
1513
    // Parameters :
1514
    // Return Values :
1515
    // --------------------------------------------------------------------------------
1516
    /**
1517
     * @param $p_tar
1518
     * @param $p_filename
1519
     * @param $p_mode
1520
     * @param $p_header
1521
     * @param $p_add_dir
1522
     * @param $p_remove_dir
1523
     *
1524
     * @return int
1525
     */
1526
    function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
1527
    {
1528
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddFile', "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1529
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1530
1531
        // ----- Check the parameters
1532 View Code Duplication
        if ($p_tar == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1533
            // ----- Error log
1534
            PclErrorLog(-3, 'Invalid file descriptor in file '.__FILE__.', line '.__LINE__);
1535
1536
            // ----- Return
1537
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1538
1539
            return PclErrorCode();
1540
        }
1541
1542
        // ----- Skip empty file names
1543
        if ($p_filename == '') {
1544
            // ----- Error log
1545
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1546
1547
            // ----- Return
1548
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1549
1550
            return PclErrorCode();
1551
        }
1552
1553
        // ----- Calculate the stored filename
1554
        $v_stored_filename = $p_filename;
1555
        if ($p_remove_dir != '') {
1556
            if (substr($p_remove_dir, -1) !== '/') {
1557
                $p_remove_dir .= '/';
1558
            }
1559
1560
            if ((0 === strpos($p_filename, './')) || (0 === strpos($p_remove_dir, './'))) {
1561 View Code Duplication
                if ((0 === strpos($p_filename, './')) && (0 !== strpos($p_remove_dir, './'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1562
                    $p_remove_dir = './'.$p_remove_dir;
1563
                }
1564 View Code Duplication
                if ((0 !== strpos($p_filename, './')) && (0 === strpos($p_remove_dir, './'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1565
                    $p_remove_dir = substr($p_remove_dir, 2);
1566
                }
1567
            }
1568
1569
            if (0 === strpos($p_filename, $p_remove_dir)) {
1570
                $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
1571
                TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
1572
            }
1573
        }
1574
        if ($p_add_dir != '') {
1575
            if (substr($p_add_dir, -1) === '/') {
1576
                $v_stored_filename = $p_add_dir.$v_stored_filename;
1577
            } else {
1578
                $v_stored_filename = $p_add_dir.'/'.$v_stored_filename;
1579
            }
1580
            TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
1581
        }
1582
1583
        // ----- Check the path length
1584
        if (strlen($v_stored_filename) > 99) {
1585
            // ----- Error log
1586
            PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");
1587
1588
            // ----- Return
1589
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1590
1591
            return PclErrorCode();
1592
        }
1593
1594
        // ----- Look for a file
1595
        if (is_file($p_filename)) {
1596
            // ----- Open the source file
1597
            if (($v_file = fopen($p_filename, 'rb')) == 0) {
1598
                // ----- Error log
1599
                PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");
1600
1601
                // ----- Return
1602
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1603
1604
                return PclErrorCode();
1605
            }
1606
1607
            // ----- Call the header generation
1608 View Code Duplication
            if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1609
                // ----- Return status
1610
                TrFctEnd(__FILE__, __LINE__, $v_result);
1611
1612
                return $v_result;
1613
            }
1614
1615
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header ='.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1616
1617
            // ----- Read the file by 512 octets blocks
1618
            $i = 0;
1619
            while (($v_buffer = fread($v_file, 512)) != '') {
1620
                $v_binary_data = pack('a512', "$v_buffer");
1621
                if ($p_mode === 'tar') {
1622
                    fwrite($p_tar, $v_binary_data);
1623
                } else {
1624
                    gzputs($p_tar, $v_binary_data);
1625
                }
1626
                ++$i;
1627
            }
1628
            TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
1629
1630
            // ----- Close the file
1631
            fclose($v_file);
1632
1633
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks ='.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1634
        } // ----- Look for a directory
1635
        else {
1636
            // ----- Call the header generation
1637 View Code Duplication
            if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1638
                // ----- Return status
1639
                TrFctEnd(__FILE__, __LINE__, $v_result);
1640
1641
                return $v_result;
1642
            }
1643
1644
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header ='.($p_mode === 'tar' ? ftell($p_tar) : gztell($p_tar)));
1645
        }
1646
1647
        // ----- Return
1648
        TrFctEnd(__FILE__, __LINE__, $v_result);
1649
1650
        return $v_result;
1651
    }
1652
1653
    // --------------------------------------------------------------------------------
1654
1655
    // --------------------------------------------------------------------------------
1656
    // Function : PclTarHandleHeader()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1657
    // Description :
1658
    //   This function creates in the TAR $p_tar, the TAR header for the file
1659
    //   $p_filename.
1660
    //
1661
    //   1. The informations needed to compose the header are recuperated and formatted
1662
    //   2. Two binary strings are composed for the first part of the header, before
1663
    //      and after checksum field.
1664
    //   3. The checksum is calculated from the two binary strings
1665
    //   4. The header is write in the tar file (first binary string, binary string
1666
    //      for checksum and last binary string).
1667
    // Parameters :
1668
    //   $p_tar : a valid file descriptor, opened in write mode,
1669
    //   $p_filename : The name of the file the header is for,
1670
    //   $p_mode : The mode of the archive ("tar" or "tgz").
1671
    //   $p_header : A pointer to a array where will be set the file properties
1672
    // Return Values :
1673
    // --------------------------------------------------------------------------------
1674
    /**
1675
     * @param $p_tar
1676
     * @param $p_filename
1677
     * @param $p_mode
1678
     * @param $p_header
1679
     * @param $p_stored_filename
1680
     *
1681
     * @return int
1682
     */
1683
    function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename)
1684
    {
1685
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleHeader', "tar=$p_tar, file='$p_filename', mode='$p_mode', stored_filename='$p_stored_filename'");
1686
        $v_result = 1;
1687
1688
        // ----- Check the parameters
1689 View Code Duplication
        if (($p_tar == 0) || ($p_filename == '')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1690
            // ----- Error log
1691
            PclErrorLog(-3, 'Invalid file descriptor in file '.__FILE__.', line '.__LINE__);
1692
1693
            // ----- Return
1694
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1695
1696
            return PclErrorCode();
1697
        }
1698
1699
        // ----- Filename (reduce the path of stored name)
1700
        if ($p_stored_filename == '') {
1701
            $p_stored_filename = $p_filename;
1702
        }
1703
        $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename);
1704
        TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen ".strlen($v_reduce_filename));
1705
1706
        // ----- Get file info
1707
        $v_info = stat($p_filename);
1708
        $v_uid = sprintf('%6s ', decoct($v_info[4]));
1709
        $v_gid = sprintf('%6s ', decoct($v_info[5]));
1710
        TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid");
1711
        $v_perms = sprintf('%6s ', decoct(fileperms($p_filename)));
1712
        TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms");
1713
1714
        // ----- File mtime
1715
        $v_mtime_data = filemtime($p_filename);
1716
        TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data");
1717
        $v_mtime = sprintf('%11s', decoct($v_mtime_data));
1718
1719
        // ----- File typeflag
1720
        // '0' or '\0' is the code for regular file
1721
        // '5' is directory
1722
        if (is_dir($p_filename)) {
1723
            $v_typeflag = '5';
1724
            $v_size = 0;
1725
        } else {
1726
            $v_typeflag = '';
1727
1728
            // ----- Get the file size
1729
            clearstatcache();
1730
            $v_size = filesize($p_filename);
1731
        }
1732
1733
        TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size");
1734
        $v_size = sprintf('%11s ', decoct($v_size));
1735
1736
        TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag");
1737
1738
        // ----- Linkname
1739
        $v_linkname = '';
1740
1741
        // ----- Magic
1742
        $v_magic = '';
1743
1744
        // ----- Version
1745
        $v_version = '';
1746
1747
        // ----- uname
1748
        $v_uname = '';
1749
1750
        // ----- gname
1751
        $v_gname = '';
1752
1753
        // ----- devmajor
1754
        $v_devmajor = '';
1755
1756
        // ----- devminor
1757
        $v_devminor = '';
1758
1759
        // ----- prefix
1760
        $v_prefix = '';
1761
1762
        // ----- Compose the binary string of the header in two parts arround the checksum position
1763
        $v_binary_data_first = pack('a100a8a8a8a12A12', $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
1764
        $v_binary_data_last = pack('a1a100a6a2a32a32a8a8a155a12', $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
1765
1766
        // ----- Calculate the checksum
1767
        $v_checksum = 0;
1768
        // ..... First part of the header
1769
        for ($i = 0; $i < 148; ++$i) {
1770
            $v_checksum += ord(substr($v_binary_data_first, $i, 1));
1771
        }
1772
        // ..... Ignore the checksum value and replace it by ' ' (space)
1773
        for ($i = 148; $i < 156; ++$i) {
1774
            $v_checksum += ord(' ');
1775
        }
1776
        // ..... Last part of the header
1777
        for ($i = 156, $j = 0; $i < 512; ++$i, ++$j) {
1778
            $v_checksum += ord(substr($v_binary_data_last, $j, 1));
1779
        }
1780
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
1781
1782
        // ----- Write the first 148 bytes of the header in the archive
1783
        if ($p_mode === 'tar') {
1784
            fwrite($p_tar, $v_binary_data_first, 148);
1785
        } else {
1786
            gzputs($p_tar, $v_binary_data_first, 148);
1787
        }
1788
1789
        // ----- Write the calculated checksum
1790
        $v_checksum = sprintf('%6s ', decoct($v_checksum));
1791
        $v_binary_data = pack('a8', $v_checksum);
1792
        if ($p_mode === 'tar') {
1793
            fwrite($p_tar, $v_binary_data, 8);
1794
        } else {
1795
            gzputs($p_tar, $v_binary_data, 8);
1796
        }
1797
1798
        // ----- Write the last 356 bytes of the header in the archive
1799
        if ($p_mode === 'tar') {
1800
            fwrite($p_tar, $v_binary_data_last, 356);
1801
        } else {
1802
            gzputs($p_tar, $v_binary_data_last, 356);
1803
        }
1804
1805
        // ----- Set the properties in the header "structure"
1806
        $p_header['filename'] = $v_reduce_filename;
1807
        $p_header['mode'] = $v_perms;
1808
        $p_header['uid'] = $v_uid;
1809
        $p_header['gid'] = $v_gid;
1810
        $p_header['size'] = $v_size;
1811
        $p_header['mtime'] = $v_mtime;
1812
        $p_header['typeflag'] = $v_typeflag;
1813
        $p_header['status'] = 'added';
1814
1815
        // ----- Return
1816
        TrFctEnd(__FILE__, __LINE__, $v_result);
1817
1818
        return $v_result;
1819
    }
1820
1821
    // --------------------------------------------------------------------------------
1822
1823
    // --------------------------------------------------------------------------------
1824
    // Function : PclTarHandleFooter()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1825
    // Description :
1826
    // Parameters :
1827
    // Return Values :
1828
    // --------------------------------------------------------------------------------
1829
    /**
1830
     * @param $p_tar
1831
     * @param $p_mode
1832
     *
1833
     * @return int
1834
     */
1835
    function PclTarHandleFooter($p_tar, $p_mode)
1836
    {
1837
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleFooter', "tar='$p_tar', p_mode=$p_mode");
1838
        $v_result = 1;
1839
1840
        // ----- Write the last 0 filled block for end of archive
1841
        $v_binary_data = pack('a512', '');
1842
        if ($p_mode === 'tar') {
1843
            fwrite($p_tar, $v_binary_data);
1844
        } else {
1845
            gzputs($p_tar, $v_binary_data);
1846
        }
1847
1848
        // ----- Return
1849
        TrFctEnd(__FILE__, __LINE__, $v_result);
1850
1851
        return $v_result;
1852
    }
1853
1854
    // --------------------------------------------------------------------------------
1855
1856
    // --------------------------------------------------------------------------------
1857
    // Function : PclTarHandleExtract()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1858
    // Description :
1859
    // Parameters :
1860
    //   $p_tarname : Filename of the tar (or tgz) archive
1861
    //   $p_file_list : An array which contains the list of files to extract, this
1862
    //                  array may be empty when $p_mode is 'complete'
1863
    //   $p_list_detail : An array where will be placed the properties of  each extracted/listed file
1864
    //   $p_mode : 'complete' will extract all files from the archive,
1865
    //             'partial' will look for files in $p_file_list
1866
    //             'list' will only list the files from the archive without any extract
1867
    //   $p_path : Path to add while writing the extracted files
1868
    //   $p_tar_mode : 'tar' for GNU TAR archive, 'tgz' for compressed archive
1869
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
1870
    //                    extracted files. If the path does not match the file path,
1871
    //                    the file is extracted with its memorized path.
1872
    //                    $p_remove_path does not apply to 'list' mode.
1873
    //                    $p_path and $p_remove_path are commulative.
1874
    // Return Values :
1875
    // --------------------------------------------------------------------------------
1876
    /**
1877
     * @param $p_tarname
1878
     * @param $p_file_list
1879
     * @param $p_list_detail
1880
     * @param $p_mode
1881
     * @param $p_path
1882
     * @param $p_tar_mode
1883
     * @param $p_remove_path
1884
     *
1885
     * @return int
1886
     */
1887
    function PclTarHandleExtract(
1888
        $p_tarname,
1889
        $p_file_list,
1890
        &$p_list_detail,
1891
        $p_mode,
1892
        $p_path,
1893
        $p_tar_mode,
1894
        $p_remove_path
1895
    ) {
1896
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtract', "archive='$p_tarname', list, mode=$p_mode, path=$p_path, tar_mode=$p_tar_mode, remove_path='$p_remove_path'");
1897
        $v_result = 1;
1898
        $v_nb = 0;
1899
        $v_extract_all = true;
0 ignored issues
show
Unused Code introduced by
$v_extract_all is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1900
        $v_listing = false;
0 ignored issues
show
Unused Code introduced by
$v_listing is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1901
1902
        // ----- Check the path
1903
        //if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../")))
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1904
        if ($p_path == '') {
1905
            $p_path = './'.$p_path;
1906
        }
1907
1908
        // ----- Look for path to remove format (should end by /)
1909
        if (($p_remove_path != '') && (substr($p_remove_path, -1) !== '/')) {
1910
            $p_remove_path .= '/';
1911
        }
1912
        $p_remove_path_size = strlen($p_remove_path);
1913
1914
        // ----- Study the mode
1915
        switch ($p_mode) {
1916
            case 'complete' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1917
                // ----- Flag extract of all files
1918
                $v_extract_all = true;
1919
                $v_listing = false;
1920
                break;
1921
            case 'partial' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1922
                // ----- Flag extract of specific files
1923
                $v_extract_all = false;
1924
                $v_listing = false;
1925
                break;
1926
            case 'list' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1927
                // ----- Flag list of all files
1928
                $v_extract_all = false;
1929
                $v_listing = true;
1930
                break;
1931
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1932
                // ----- Error log
1933
                PclErrorLog(-3, "Invalid extract mode ($p_mode)");
1934
1935
                // ----- Return
1936
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1937
1938
                return PclErrorCode();
1939
        }
1940
1941
        // ----- Open the tar file
1942 View Code Duplication
        if ($p_tar_mode === 'tar') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1943
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
1944
            $v_tar = fopen($p_tarname, 'rb');
1945
        } else {
1946
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
1947
            $v_tar = @gzopen($p_tarname, 'rb');
1948
        }
1949
1950
        // ----- Check that the archive is open
1951
        if ($v_tar == 0) {
1952
            // ----- Error log
1953
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
1954
1955
            // ----- Return
1956
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1957
1958
            return PclErrorCode();
1959
        }
1960
1961
        // ----- Read the blocks
1962
        while (!($v_end_of_file = ($p_tar_mode === 'tar' ? feof($v_tar) : gzeof($v_tar)))) {
1963
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
1964
1965
            // ----- Clear cache of file infos
1966
            clearstatcache();
1967
1968
            // ----- Reset extract tag
1969
            $v_extract_file = false;
0 ignored issues
show
Unused Code introduced by
$v_extract_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1970
            $v_extraction_stopped = 0;
1971
1972
            // ----- Read the 512 bytes header
1973
            if ($p_tar_mode === 'tar') {
1974
                $v_binary_data = fread($v_tar, 512);
1975
            } else {
1976
                $v_binary_data = gzread($v_tar, 512);
1977
            }
1978
1979
            // ----- Read the header properties
1980
            if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1) {
1981
                // ----- Close the archive file
1982
                if ($p_tar_mode === 'tar') {
1983
                    fclose($v_tar);
1984
                } else {
1985
                    gzclose($v_tar);
1986
                }
1987
1988
                // ----- Return
1989
                TrFctEnd(__FILE__, __LINE__, $v_result);
1990
1991
                return $v_result;
1992
            }
1993
1994
            // ----- Look for empty blocks to skip
1995
            if ($v_header['filename'] == '') {
1996
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
1997
                continue;
1998
            }
1999
2000
            TrFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
2001
2002
            // ----- Look for partial extract
2003
            if ((!$v_extract_all) && is_array($p_file_list)) {
2004
                TrFctMessage(__FILE__, __LINE__, 2, 'Look if the file '.$v_header['filename'].' need to be extracted');
2005
2006
                // ----- By default no unzip if the file is not found
2007
                $v_extract_file = false;
2008
2009
                // ----- Look into the file list
2010
                for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
2011
                    TrFctMessage(__FILE__, __LINE__, 2, 'Compare archived file '.$v_header['filename']." from asked list file '".$p_file_list[$i]."'");
2012
2013
                    // ----- Look if it is a directory
2014
                    if (substr($p_file_list[$i], -1) === '/') {
2015
                        TrFctMessage(__FILE__, __LINE__, 3, 'Compare file '.$v_header['filename']." with directory '$p_file_list[$i]'");
2016
2017
                        // ----- Look if the directory is in the filename path
2018
                        if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
2019
                            && (0 === strpos($v_header['filename'], $p_file_list[$i]))
2020
                        ) {
2021
                            // ----- The file is in the directory, so extract it
2022
                            TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename']." is in directory '$p_file_list[$i]' : extract it");
2023
                            $v_extract_file = true;
2024
2025
                            // ----- End of loop
2026
                            break;
2027
                        }
2028
                    } // ----- It is a file, so compare the file names
2029
                    else {
2030
                        if ($p_file_list[$i] == $v_header['filename']) {
2031
                            // ----- File found
2032
                            TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' should be extracted');
2033
                            $v_extract_file = true;
2034
2035
                            // ----- End of loop
2036
                            break;
2037
                        }
2038
                    }
2039
                }
2040
2041
                // ----- Trace
2042
                if (!$v_extract_file) {
2043
                    TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' should not be extracted');
2044
                }
2045
            } else {
2046
                // ----- All files need to be extracted
2047
                $v_extract_file = true;
2048
            }
2049
2050
            // ----- Look if this file need to be extracted
2051
            if ($v_extract_file && (!$v_listing)) {
2052
                // ----- Look for path to remove
2053 View Code Duplication
                if (($p_remove_path != '') && (0 === strpos($v_header['filename'], $p_remove_path))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2054
                    TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file ".$v_header['filename'].'');
2055
                    // ----- Remove the path
2056
                    $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2057
                    TrFctMessage(__FILE__, __LINE__, 3, 'Reslting file is '.$v_header['filename'].'');
2058
                }
2059
2060
                // ----- Add the path to the file
2061 View Code Duplication
                if (($p_path !== './') && ($p_path !== '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2062
                    // ----- Look for the path end '/'
2063
                    while (substr($p_path, -1) === '/') {
2064
                        TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2065
                        $p_path = substr($p_path, 0, -1);
2066
                        TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2067
                    }
2068
2069
                    // ----- Add the path
2070
                    if (0 === strpos($v_header['filename'], '/')) {
2071
                        $v_header['filename'] = $p_path.$v_header['filename'];
2072
                    } else {
2073
                        $v_header['filename'] = $p_path.'/'.$v_header['filename'];
2074
                    }
2075
                }
2076
2077
                // ----- Trace
2078
                TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) '.$v_header['filename'].", size '".$v_header['size']."'");
2079
2080
                // ----- Check that the file does not exists
2081
                if (file_exists($v_header['filename'])) {
2082
                    TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' already exists');
2083
2084
                    // ----- Look if file is a directory
2085
                    if (is_dir($v_header['filename'])) {
2086
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file '.$v_header['filename'].' is a directory');
2087
2088
                        // ----- Change the file status
2089
                        $v_header['status'] = 'already_a_directory';
2090
2091
                        // ----- Skip the extract
2092
                        $v_extraction_stopped = 1;
2093
                        $v_extract_file = 0;
2094
                    } // ----- Look if file is write protected
2095
                    else {
2096
                        if (!s_writable($v_header['filename'])) {
2097
                            TrFctMessage(__FILE__, __LINE__, 2, 'Existing file '.$v_header['filename'].' is write protected');
2098
2099
                            // ----- Change the file status
2100
                            $v_header['status'] = 'write_protected';
2101
2102
                            // ----- Skip the extract
2103
                            $v_extraction_stopped = 1;
2104
                            $v_extract_file = 0;
2105
                        }
2106
                    }
2107
                    // ----- Look if the extracted file is older
2108
                    /*else if (filemtime($v_header['filename']) > $v_header['mtime']) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2109
            TrFctMessage(__FILE__, __LINE__, 2, "Existing file ".$v_header['filename']." is newer (".date("l dS of F Y h:i:s A", filemtime($v_header['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $v_header['mtime']).")");
2110
2111
            // ----- Change the file status
2112
            $v_header['status'] = "newer_exist";
2113
2114
            // ----- Skip the extract
2115
            $v_extraction_stopped = 1;
2116
            $v_extract_file = 0;
2117
          }*/
2118
                } // ----- Check the directory availability and create it if necessary
2119 View Code Duplication
                else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2120
                    if ($v_header['typeflag'] == '5') {
2121
                        $v_dir_to_check = $v_header['filename'];
2122
                    } else {
2123
                        if (false === strpos($v_header['filename'], '/')) {
2124
                            $v_dir_to_check = '';
2125
                        } else {
2126
                            $v_dir_to_check = dirname($v_header['filename']);
2127
                        }
2128
                    }
2129
2130
                    if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1) {
2131
                        TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for '.$v_header['filename'].'');
2132
2133
                        // ----- Change the file status
2134
                        $v_header['status'] = 'path_creation_fail';
2135
2136
                        // ----- Skip the extract
2137
                        $v_extraction_stopped = 1;
2138
                        $v_extract_file = 0;
2139
                    }
2140
                }
2141
2142
                // ----- Do the extraction
2143
                if ($v_extract_file && ($v_header['typeflag'] != '5')) {
2144
                    // ----- Open the destination file in write mode
2145 View Code Duplication
                    if (($v_dest_file = @fopen($v_header['filename'], 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2146
                        TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening '.$v_header['filename'].' in write binary mode');
2147
2148
                        // ----- Change the file status
2149
                        $v_header['status'] = 'write_error';
2150
2151
                        // ----- Jump to next file
2152
                        TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2153
                        if ($p_tar_mode === 'tar') {
2154
                            fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2155
                        } else {
2156
                            gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2157
                        }
2158
                    } else {
2159
                        TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of '.$v_header['filename'].'');
2160
2161
                        // ----- Read data
2162
                        $n = floor($v_header['size'] / 512);
2163
                        for ($i = 0; $i < $n; ++$i) {
2164
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number '.($i + 1));
2165
                            if ($p_tar_mode === 'tar') {
2166
                                $v_content = fread($v_tar, 512);
2167
                            } else {
2168
                                $v_content = gzread($v_tar, 512);
2169
                            }
2170
                            fwrite($v_dest_file, $v_content, 512);
2171
                        }
2172
                        if (($v_header['size'] % 512) != 0) {
2173
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read last '.($v_header['size'] % 512).' bytes in a 512 block');
2174
                            if ($p_tar_mode === 'tar') {
2175
                                $v_content = fread($v_tar, 512);
2176
                            } else {
2177
                                $v_content = gzread($v_tar, 512);
2178
                            }
2179
                            fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2180
                        }
2181
2182
                        // ----- Close the destination file
2183
                        fclose($v_dest_file);
2184
2185
                        // ----- Change the file mode, mtime
2186
                        touch($v_header['filename'], $v_header['mtime']);
2187
                        //chmod($v_header['filename'], decoct($v_header['mode']));
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2188
                    }
2189
2190
                    // ----- Check the file size
2191
                    clearstatcache();
2192
                    if (filesize($v_header['filename']) != $v_header['size']) {
2193
                        // ----- Close the archive file
2194
                        if ($p_tar_mode === 'tar') {
2195
                            fclose($v_tar);
2196
                        } else {
2197
                            gzclose($v_tar);
2198
                        }
2199
2200
                        // ----- Error log
2201
                        PclErrorLog(-7, 'Extracted file '.$v_header['filename']." does not have the correct file size '".filesize($v_filename)."' ('".$v_header['size']
0 ignored issues
show
Bug introduced by
The variable $v_filename does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
2202
                                        ."' expected). Archive may be corrupted.");
2203
2204
                        // ----- Return
2205
                        TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2206
2207
                        return PclErrorCode();
2208
                    }
2209
2210
                    // ----- Trace
2211
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2212 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2213
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file '.$v_header['filename'].' skipped.');
2214
2215
                    // ----- Jump to next file
2216
                    TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2217
                    if ($p_tar_mode === 'tar') {
2218
                        fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2219
                    } else {
2220
                        gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2221
                    }
2222
                }
2223
            } // ----- Look for file that is not to be unzipped
2224 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2225
                // ----- Trace
2226
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file '.$v_header['filename'].'');
2227
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2228
2229
                // ----- Jump to next file
2230
                if ($p_tar_mode === 'tar') {
2231
                    fseek($v_tar, ($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
2232
                } else {
2233
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2234
                }
2235
2236
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2237
            }
2238
2239
            if ($p_tar_mode === 'tar') {
2240
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2241
            } else {
2242
                $v_end_of_file = gzeof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2243
            }
2244
2245
            // ----- File name and properties are logged if listing mode or file is extracted
2246 View Code Duplication
            if ($v_listing || $v_extract_file || $v_extraction_stopped) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2247
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file '.$v_header['filename'].'');
2248
2249
                // ----- Log extracted files
2250
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2251
                    $v_file_dir = '';
2252
                }
2253
                if (($v_file_dir === '') && (0 === strpos($v_header['filename'], '/'))) {
2254
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
$v_file_dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2255
                }
2256
2257
                // ----- Add the array describing the file into the list
2258
                $p_list_detail[$v_nb] = $v_header;
2259
2260
                // ----- Increment
2261
                ++$v_nb;
2262
            }
2263
        }
2264
2265
        // ----- Close the tarfile
2266
        if ($p_tar_mode === 'tar') {
2267
            fclose($v_tar);
2268
        } else {
2269
            gzclose($v_tar);
2270
        }
2271
2272
        // ----- Return
2273
        TrFctEnd(__FILE__, __LINE__, $v_result);
2274
2275
        return $v_result;
2276
    }
2277
2278
    // --------------------------------------------------------------------------------
2279
2280
    // --------------------------------------------------------------------------------
2281
    // Function : PclTarHandleExtractByIndexList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2282
    // Description :
2283
    //   Extract the files which are at the indexes specified. If the 'file' at the
2284
    //   index is a directory, the directory only is created, not all the files stored
2285
    //   for that directory.
2286
    // Parameters :
2287
    //   $p_index_string : String of indexes of files to extract. The form of the
2288
    //                     string is "0,4-6,8-12" with only numbers and '-' for
2289
    //                     for range, and ',' to separate ranges. No spaces or ';'
2290
    //                     are allowed.
2291
    // Return Values :
2292
    // --------------------------------------------------------------------------------
2293
    /**
2294
     * @param $p_tarname
2295
     * @param $p_index_string
2296
     * @param $p_list_detail
2297
     * @param $p_path
2298
     * @param $p_remove_path
2299
     * @param $p_tar_mode
2300
     *
2301
     * @return int
2302
     */
2303
    function PclTarHandleExtractByIndexList(
2304
        $p_tarname,
2305
        $p_index_string,
2306
        &$p_list_detail,
2307
        $p_path,
2308
        $p_remove_path,
2309
        $p_tar_mode
2310
    ) {
2311
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractByIndexList',
2312
                   "archive='$p_tarname', index_string='$p_index_string', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2313
        $v_result = 1;
2314
        $v_nb = 0;
0 ignored issues
show
Unused Code introduced by
$v_nb is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2315
2316
        // ----- TBC : I should check the string by a regexp
2317
2318
        // ----- Check the path
2319
        if (($p_path === '')
2320
            || ((0 !== strpos($p_path, '/')) && (0 !== strpos($p_path, '../'))
2321
                && (0 !== strpos($p_path, './')))
2322
        ) {
2323
            $p_path = './'.$p_path;
2324
        }
2325
2326
        // ----- Look for path to remove format (should end by /)
2327
        if (($p_remove_path != '') && (substr($p_remove_path, -1) !== '/')) {
2328
            $p_remove_path .= '/';
2329
        }
2330
        $p_remove_path_size = strlen($p_remove_path);
0 ignored issues
show
Unused Code introduced by
$p_remove_path_size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2331
2332
        // ----- Open the tar file
2333 View Code Duplication
        if ($p_tar_mode === 'tar') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2334
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2335
            $v_tar = @fopen($p_tarname, 'rb');
2336
        } else {
2337
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2338
            $v_tar = @gzopen($p_tarname, 'rb');
2339
        }
2340
2341
        // ----- Check that the archive is open
2342
        if ($v_tar == 0) {
2343
            // ----- Error log
2344
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
2345
2346
            // ----- Return
2347
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2348
2349
            return PclErrorCode();
2350
        }
2351
2352
        // ----- Manipulate the index list
2353
        $v_list = explode(',', $p_index_string);
2354
        sort($v_list);
2355
2356
        // ----- Loop on the index list
2357
        $v_index = 0;
2358
        for ($i = 0; ($i < count($v_list)) && $v_result; ++$i) {
2359
            TrFctMessage(__FILE__, __LINE__, 3, "Looking for index part '$v_list[$i]'");
2360
2361
            // ----- Extract range
2362
            $v_index_list = explode('-', $v_list[$i]);
2363
            $v_size_index_list = count($v_index_list);
2364
            if ($v_size_index_list == 1) {
2365
                TrFctMessage(__FILE__, __LINE__, 3, "Only one index '$v_index_list[0]'");
2366
2367
                // ----- Do the extraction
2368
                $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[0], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
2369
            } else {
2370
                if ($v_size_index_list == 2) {
2371
                    TrFctMessage(__FILE__, __LINE__, 3, "Two indexes '$v_index_list[0]' and '$v_index_list[1]'");
2372
2373
                    // ----- Do the extraction
2374
                    $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[1], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
2375
                }
2376
            }
2377
        }
2378
2379
        // ----- Close the tarfile
2380
        if ($p_tar_mode === 'tar') {
2381
            fclose($v_tar);
2382
        } else {
2383
            gzclose($v_tar);
2384
        }
2385
2386
        // ----- Return
2387
        TrFctEnd(__FILE__, __LINE__, $v_result);
2388
2389
        return $v_result;
2390
    }
2391
2392
    // --------------------------------------------------------------------------------
2393
2394
    // --------------------------------------------------------------------------------
2395
    // Function : PclTarHandleExtractByIndex()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2396
    // Description :
2397
    // Parameters :
2398
    // Return Values :
2399
    // --------------------------------------------------------------------------------
2400
    /**
2401
     * @param $p_tar
2402
     * @param $p_index_current
2403
     * @param $p_index_start
2404
     * @param $p_index_stop
2405
     * @param $p_list_detail
2406
     * @param $p_path
2407
     * @param $p_remove_path
2408
     * @param $p_tar_mode
2409
     *
2410
     * @return int
2411
     */
2412
    function PclTarHandleExtractByIndex(
2413
        $p_tar,
2414
        &$p_index_current,
2415
        $p_index_start,
2416
        $p_index_stop,
2417
        &$p_list_detail,
2418
        $p_path,
2419
        $p_remove_path,
2420
        $p_tar_mode
2421
    ) {
2422
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractByIndex',
2423
                   "archive_descr='$p_tar', index_current=$p_index_current, index_start='$p_index_start', index_stop='$p_index_stop', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2424
        $v_result = 1;
2425
        $v_nb = 0;
0 ignored issues
show
Unused Code introduced by
$v_nb is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2426
2427
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2428
        $v_tar = $p_tar;
2429
2430
        // ----- Look the number of elements already in $p_list_detail
2431
        $v_nb = count($p_list_detail);
2432
2433
        // ----- Read the blocks
2434
        while (!($v_end_of_file = ($p_tar_mode === 'tar' ? feof($v_tar) : gzeof($v_tar)))) {
2435
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next file ...');
2436
            TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
2437
2438
            if ($p_index_current > $p_index_stop) {
2439
                TrFctMessage(__FILE__, __LINE__, 2, 'Stop extraction, past stop index');
2440
                break;
2441
            }
2442
2443
            // ----- Clear cache of file infos
2444
            clearstatcache();
2445
2446
            // ----- Reset extract tag
2447
            $v_extract_file = false;
0 ignored issues
show
Unused Code introduced by
$v_extract_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2448
            $v_extraction_stopped = 0;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2449
2450
            // ----- Read the 512 bytes header
2451
            if ($p_tar_mode === 'tar') {
2452
                $v_binary_data = fread($v_tar, 512);
2453
            } else {
2454
                $v_binary_data = gzread($v_tar, 512);
2455
            }
2456
2457
            // ----- Read the header properties
2458
            if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1) {
2459
                // ----- Return
2460
                TrFctEnd(__FILE__, __LINE__, $v_result);
2461
2462
                return $v_result;
2463
            }
2464
2465
            // ----- Look for empty blocks to skip
2466
            if ($v_header['filename'] == '') {
2467
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2468
                continue;
2469
            }
2470
2471
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file '.$v_header['filename'].", size '".$v_header['size']."'");
2472
2473
            // ----- Look if file is in the range to be extracted
2474
            if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop)) {
2475
                TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' is in the range to be extracted');
2476
                $v_extract_file = true;
2477
            } else {
2478
                TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' is out of the range');
2479
                $v_extract_file = false;
2480
            }
2481
2482
            // ----- Look if this file need to be extracted
2483
            if ($v_extract_file) {
2484
                if (($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode)) != 1) {
2485
                    // ----- Return
2486
                    TrFctEnd(__FILE__, __LINE__, $v_result);
2487
2488
                    return $v_result;
2489
                }
2490
            } // ----- Look for file that is not to be extracted
2491 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2492
                // ----- Trace
2493
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file '.$v_header['filename'].'');
2494
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2495
2496
                // ----- Jump to next file
2497
                if ($p_tar_mode === 'tar') {
2498
                    fseek($v_tar, ($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
2499
                } else {
2500
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2501
                }
2502
2503
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2504
            }
2505
2506
            if ($p_tar_mode === 'tar') {
2507
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2508
            } else {
2509
                $v_end_of_file = gzeof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2510
            }
2511
2512
            // ----- File name and properties are logged if listing mode or file is extracted
2513 View Code Duplication
            if ($v_extract_file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2514
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file '.$v_header['filename'].'');
2515
2516
                // ----- Log extracted files
2517
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2518
                    $v_file_dir = '';
2519
                }
2520
                if (($v_file_dir === '') && (0 === strpos($v_header['filename'], '/'))) {
2521
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
$v_file_dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2522
                }
2523
2524
                // ----- Add the array describing the file into the list
2525
                $p_list_detail[$v_nb] = $v_header;
2526
2527
                // ----- Increment
2528
                ++$v_nb;
2529
            }
2530
2531
            // ----- Increment the current file index
2532
            ++$p_index_current;
2533
        }
2534
2535
        // ----- Return
2536
        TrFctEnd(__FILE__, __LINE__, $v_result);
2537
2538
        return $v_result;
2539
    }
2540
2541
    // --------------------------------------------------------------------------------
2542
2543
    // --------------------------------------------------------------------------------
2544
    // Function : PclTarHandleExtractFile()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2545
    // Description :
2546
    // Parameters :
2547
    // Return Values :
2548
    // --------------------------------------------------------------------------------
2549
    /**
2550
     * @param $p_tar
2551
     * @param $v_header
2552
     * @param $p_path
2553
     * @param $p_remove_path
2554
     * @param $p_tar_mode
2555
     *
2556
     * @return int
2557
     */
2558
    function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
2559
    {
2560
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractFile', "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2561
        $v_result = 1;
2562
2563
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2564
        $v_tar = $p_tar;
2565
        $v_extract_file = 1;
2566
2567
        $p_remove_path_size = strlen($p_remove_path);
2568
2569
        // ----- Look for path to remove
2570 View Code Duplication
        if (($p_remove_path != '') && (0 === strpos($v_header['filename'], $p_remove_path))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2571
            TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file ".$v_header['filename'].'');
2572
            // ----- Remove the path
2573
            $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2574
            TrFctMessage(__FILE__, __LINE__, 3, 'Resulting file is '.$v_header['filename'].'');
2575
        }
2576
2577
        // ----- Add the path to the file
2578 View Code Duplication
        if (($p_path !== './') && ($p_path !== '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2579
            // ----- Look for the path end '/'
2580
            while (substr($p_path, -1) === '/') {
2581
                TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2582
                $p_path = substr($p_path, 0, -1);
2583
                TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2584
            }
2585
2586
            // ----- Add the path
2587
            if (0 === strpos($v_header['filename'], '/')) {
2588
                $v_header['filename'] = $p_path.$v_header['filename'];
2589
            } else {
2590
                $v_header['filename'] = $p_path.'/'.$v_header['filename'];
2591
            }
2592
        }
2593
2594
        // ----- Trace
2595
        TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) '.$v_header['filename'].", size '".$v_header['size']."'");
2596
2597
        // ----- Check that the file does not exists
2598
        if (file_exists($v_header['filename'])) {
2599
            TrFctMessage(__FILE__, __LINE__, 2, 'File '.$v_header['filename'].' already exists');
2600
2601
            // ----- Look if file is a directory
2602
            if (is_dir($v_header['filename'])) {
2603
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file '.$v_header['filename'].' is a directory');
2604
2605
                // ----- Change the file status
2606
                $v_header['status'] = 'already_a_directory';
2607
2608
                // ----- Skip the extract
2609
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2610
                $v_extract_file = 0;
2611
            } // ----- Look if file is write protected
2612
            else {
2613
                if (!s_writable($v_header['filename'])) {
2614
                    TrFctMessage(__FILE__, __LINE__, 2, 'Existing file '.$v_header['filename'].' is write protected');
2615
2616
                    // ----- Change the file status
2617
                    $v_header['status'] = 'write_protected';
2618
2619
                    // ----- Skip the extract
2620
                    $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2621
                    $v_extract_file = 0;
2622
                } // ----- Look if the extracted file is older
2623
                else {
2624
                    if (filemtime($v_header['filename']) > $v_header['mtime']) {
2625
                        TrFctMessage(__FILE__, __LINE__, 2,
2626
                                     'Existing file '.$v_header['filename'].' is newer ('.date('l dS of F Y h:i:s A', filemtime($v_header['filename'])).') than the extracted file ('
2627
                                     .date('l dS of F Y h:i:s A', $v_header['mtime']).')');
2628
2629
                        // ----- Change the file status
2630
                        $v_header['status'] = 'newer_exist';
2631
2632
                        // ----- Skip the extract
2633
                        $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2634
                        $v_extract_file = 0;
2635
                    }
2636
                }
2637
            }
2638
        } // ----- Check the directory availability and create it if necessary
2639 View Code Duplication
        else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2640
            if ($v_header['typeflag'] == '5') {
2641
                $v_dir_to_check = $v_header['filename'];
2642
            } else {
2643
                if (false === strpos($v_header['filename'], '/')) {
2644
                    $v_dir_to_check = '';
2645
                } else {
2646
                    $v_dir_to_check = dirname($v_header['filename']);
2647
                }
2648
            }
2649
2650
            if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1) {
2651
                TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for '.$v_header['filename'].'');
2652
2653
                // ----- Change the file status
2654
                $v_header['status'] = 'path_creation_fail';
2655
2656
                // ----- Skip the extract
2657
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2658
                $v_extract_file = 0;
2659
            }
2660
        }
2661
2662
        // ----- Do the real bytes extraction (if not a directory)
2663
        if ($v_extract_file && ($v_header['typeflag'] != '5')) {
2664
            // ----- Open the destination file in write mode
2665 View Code Duplication
            if (($v_dest_file = @fopen($v_header['filename'], 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2666
                TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening '.$v_header['filename'].' in write binary mode');
2667
2668
                // ----- Change the file status
2669
                $v_header['status'] = 'write_error';
2670
2671
                // ----- Jump to next file
2672
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2673
                if ($p_tar_mode === 'tar') {
2674
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2675
                } else {
2676
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2677
                }
2678
            } else {
2679
                TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of '.$v_header['filename'].'');
2680
2681
                // ----- Read data
2682
                $n = floor($v_header['size'] / 512);
2683
                for ($i = 0; $i < $n; ++$i) {
2684
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number '.($i + 1));
2685
                    if ($p_tar_mode === 'tar') {
2686
                        $v_content = fread($v_tar, 512);
2687
                    } else {
2688
                        $v_content = gzread($v_tar, 512);
2689
                    }
2690
                    fwrite($v_dest_file, $v_content, 512);
2691
                }
2692
                if (($v_header['size'] % 512) != 0) {
2693
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read last '.($v_header['size'] % 512).' bytes in a 512 block');
2694
                    if ($p_tar_mode === 'tar') {
2695
                        $v_content = fread($v_tar, 512);
2696
                    } else {
2697
                        $v_content = gzread($v_tar, 512);
2698
                    }
2699
                    fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2700
                }
2701
2702
                // ----- Close the destination file
2703
                fclose($v_dest_file);
2704
2705
                // ----- Change the file mode, mtime
2706
                touch($v_header['filename'], $v_header['mtime']);
2707
                //chmod($v_header['filename'], decoct($v_header['mode']));
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2708
            }
2709
2710
            // ----- Check the file size
2711
            clearstatcache();
2712
            if (filesize($v_header['filename']) != $v_header['size']) {
2713
                // ----- Error log
2714
                PclErrorLog(-7, 'Extracted file '.$v_header['filename']." does not have the correct file size '".filesize($v_filename)."' ('".$v_header['size']
0 ignored issues
show
Bug introduced by
The variable $v_filename does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
2715
                                ."' expected). Archive may be corrupted.");
2716
2717
                // ----- Return
2718
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2719
2720
                return PclErrorCode();
2721
            }
2722
2723
            // ----- Trace
2724
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2725 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2726
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file '.$v_header['filename'].' skipped.');
2727
2728
            // ----- Jump to next file
2729
            TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2730
            if ($p_tar_mode === 'tar') {
2731
                fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2732
            } else {
2733
                gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2734
            }
2735
        }
2736
2737
        // ----- Return
2738
        TrFctEnd(__FILE__, __LINE__, $v_result);
2739
2740
        return $v_result;
2741
    }
2742
2743
    // --------------------------------------------------------------------------------
2744
2745
    // --------------------------------------------------------------------------------
2746
    // Function : PclTarHandleDelete()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2747
    // Description :
2748
    // Parameters :
2749
    // Return Values :
2750
    // --------------------------------------------------------------------------------
2751
    /**
2752
     * @param $p_tarname
2753
     * @param $p_file_list
2754
     * @param $p_list_detail
2755
     * @param $p_tar_mode
2756
     *
2757
     * @return int
2758
     */
2759
    function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
2760
    {
2761
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleDelete', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2762
        $v_result = 1;
2763
        $v_nb = 0;
2764
2765
        // ----- Look for regular tar file
2766 View Code Duplication
        if ($p_tar_mode === 'tar') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2767
            // ----- Open file
2768
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2769
            if (($v_tar = @fopen($p_tarname, 'rb')) == 0) {
2770
                // ----- Error log
2771
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2772
2773
                // ----- Return
2774
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2775
2776
                return PclErrorCode();
2777
            }
2778
2779
            // ----- Open a temporary file in write mode
2780
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
2781
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2782
            if (($v_temp_tar = @fopen($v_temp_tarname, 'wb')) == 0) {
2783
                // ----- Close tar file
2784
                fclose($v_tar);
2785
2786
                // ----- Error log
2787
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2788
2789
                // ----- Return
2790
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2791
2792
                return PclErrorCode();
2793
            }
2794
        } // ----- Look for compressed tar file
2795
        else {
2796
            // ----- Open the file in read mode
2797
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2798
            if (($v_tar = @gzopen($p_tarname, 'rb')) == 0) {
2799
                // ----- Error log
2800
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2801
2802
                // ----- Return
2803
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2804
2805
                return PclErrorCode();
2806
            }
2807
2808
            // ----- Open a temporary file in write mode
2809
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
2810
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2811
            if (($v_temp_tar = @gzopen($v_temp_tarname, 'wb')) == 0) {
2812
                // ----- Close tar file
2813
                gzclose($v_tar);
2814
2815
                // ----- Error log
2816
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2817
2818
                // ----- Return
2819
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2820
2821
                return PclErrorCode();
2822
            }
2823
        }
2824
2825
        // ----- Read the blocks
2826
        while (!($v_end_of_file = ($p_tar_mode === 'tar' ? feof($v_tar) : gzeof($v_tar)))) {
2827
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
2828
2829
            // ----- Clear cache of file infos
2830
            clearstatcache();
2831
2832
            // ----- Reset delete tag
2833
            $v_delete_file = false;
0 ignored issues
show
Unused Code introduced by
$v_delete_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2834
2835
            // ----- Read the first 512 block header
2836
            if ($p_tar_mode === 'tar') {
2837
                $v_binary_data = fread($v_tar, 512);
2838
            } else {
2839
                $v_binary_data = gzread($v_tar, 512);
2840
            }
2841
2842
            // ----- Read the header properties
2843 View Code Duplication
            if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2844
                // ----- Close the archive file
2845
                if ($p_tar_mode === 'tar') {
2846
                    fclose($v_tar);
2847
                    fclose($v_temp_tar);
2848
                } else {
2849
                    gzclose($v_tar);
2850
                    gzclose($v_temp_tar);
2851
                }
2852
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
2853
2854
                // ----- Return
2855
                TrFctEnd(__FILE__, __LINE__, $v_result);
2856
2857
                return $v_result;
2858
            }
2859
2860
            // ----- Look for empty blocks to skip
2861
            if ($v_header['filename'] == '') {
2862
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2863
                continue;
2864
            }
2865
2866
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file '.$v_header['filename'].", size '".$v_header['size']."'");
2867
2868
            // ----- Look for filenames to delete
2869
            for ($i = 0, $v_delete_file = false; ($i < count($p_file_list)) && (!$v_delete_file); ++$i) {
2870
                // ----- Compare the file names
2871
                //        if ($p_file_list[$i] == $v_header['filename'])
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2872
                if (($v_len = strcmp($p_file_list[$i], $v_header['filename'])) <= 0) {
2873
                    if ($v_len == 0) {
2874
                        TrFctMessage(__FILE__, __LINE__, 3, 'Found that '.$v_header['filename'].' need to be deleted');
2875
                        $v_delete_file = true;
2876
                    } else {
2877
                        TrFctMessage(__FILE__, __LINE__, 3, 'Look if '.$v_header['filename']." is a file in $p_file_list[$i]");
2878
                        if (substr($v_header['filename'], strlen($p_file_list[$i]), 1) === '/') {
2879
                            TrFctMessage(__FILE__, __LINE__, 3, ''.$v_header['filename']." is a file in $p_file_list[$i]");
2880
                            $v_delete_file = true;
2881
                        }
2882
                    }
2883
                }
2884
            }
2885
2886
            // ----- Copy files that do not need to be deleted
2887
            if (!$v_delete_file) {
2888
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file '.$v_header['filename'].'');
2889
2890
                // ----- Write the file header
2891
                if ($p_tar_mode === 'tar') {
2892
                    fwrite($v_temp_tar, $v_binary_data, 512);
2893
                } else {
2894
                    gzputs($v_temp_tar, $v_binary_data, 512);
2895
                }
2896
2897
                // ----- Write the file data
2898
                $n = ceil($v_header['size'] / 512);
2899
                for ($i = 0; $i < $n; ++$i) {
2900
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number '.($i + 1));
2901
                    if ($p_tar_mode === 'tar') {
2902
                        $v_content = fread($v_tar, 512);
2903
                        fwrite($v_temp_tar, $v_content, 512);
2904
                    } else {
2905
                        $v_content = gzread($v_tar, 512);
2906
                        gzwrite($v_temp_tar, $v_content, 512);
2907
                    }
2908
                }
2909
2910
                // ----- File name and properties are logged if listing mode or file is extracted
2911
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file '.$v_header['filename'].'');
2912
2913
                // ----- Add the array describing the file into the list
2914
                $p_list_detail[$v_nb] = $v_header;
2915
                $p_list_detail[$v_nb]['status'] = 'ok';
2916
2917
                // ----- Increment
2918
                ++$v_nb;
2919
            } // ----- Look for file that is to be deleted
2920 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2921
                // ----- Trace
2922
                TrFctMessage(__FILE__, __LINE__, 2, 'Start deletion of '.$v_header['filename'].'');
2923
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2924
2925
                // ----- Jump to next file
2926
                if ($p_tar_mode === 'tar') {
2927
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2928
                } else {
2929
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2930
                }
2931
2932
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump ['.($p_tar_mode === 'tar' ? ftell($v_tar) : gztell($v_tar)).']');
2933
            }
2934
2935
            // ----- Look for end of file
2936
            if ($p_tar_mode === 'tar') {
2937
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2938
            } else {
2939
                $v_end_of_file = gzeof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2940
            }
2941
        }
2942
2943
        // ----- Write the last empty buffer
2944
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
2945
2946
        // ----- Close the tarfile
2947
        if ($p_tar_mode === 'tar') {
2948
            fclose($v_tar);
2949
            fclose($v_temp_tar);
2950
        } else {
2951
            gzclose($v_tar);
2952
            gzclose($v_temp_tar);
2953
        }
2954
2955
        // ----- Unlink tar file
2956
        if (!@unlink($p_tarname)) {
2957
            // ----- Error log
2958
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
2959
        }
2960
2961
        // ----- Rename tar file
2962
        if (!@rename($v_temp_tarname, $p_tarname)) {
2963
            // ----- Error log
2964
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
2965
2966
            // ----- Return
2967
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2968
2969
            return PclErrorCode();
2970
        }
2971
2972
        // ----- Return
2973
        TrFctEnd(__FILE__, __LINE__, $v_result);
2974
2975
        return $v_result;
2976
    }
2977
2978
    // --------------------------------------------------------------------------------
2979
2980
    // --------------------------------------------------------------------------------
2981
    // Function : PclTarHandleUpdate()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2982
    // Description :
2983
    // Parameters :
2984
    // Return Values :
2985
    // --------------------------------------------------------------------------------
2986
    /**
2987
     * @param $p_tarname
2988
     * @param $p_file_list
2989
     * @param $p_list_detail
2990
     * @param $p_tar_mode
2991
     * @param $p_add_dir
2992
     * @param $p_remove_dir
2993
     *
2994
     * @return int
2995
     */
2996
    function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
2997
    {
2998
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleUpdate', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2999
        $v_result = 1;
3000
        $v_nb = 0;
3001
        $v_found_list = array();
3002
3003
        // ----- Look for regular tar file
3004 View Code Duplication
        if ($p_tar_mode === 'tar') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3005
            // ----- Open file
3006
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
3007
            if (($v_tar = @fopen($p_tarname, 'rb')) == 0) {
3008
                // ----- Error log
3009
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
3010
3011
                // ----- Return
3012
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3013
3014
                return PclErrorCode();
3015
            }
3016
3017
            // ----- Open a temporary file in write mode
3018
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
3019
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3020
            if (($v_temp_tar = @fopen($v_temp_tarname, 'wb')) == 0) {
3021
                // ----- Close tar file
3022
                fclose($v_tar);
3023
3024
                // ----- Error log
3025
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3026
3027
                // ----- Return
3028
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3029
3030
                return PclErrorCode();
3031
            }
3032
        } // ----- Look for compressed tar file
3033
        else {
3034
            // ----- Open the file in read mode
3035
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
3036
            if (($v_tar = @gzopen($p_tarname, 'rb')) == 0) {
3037
                // ----- Error log
3038
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
3039
3040
                // ----- Return
3041
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3042
3043
                return PclErrorCode();
3044
            }
3045
3046
            // ----- Open a temporary file in write mode
3047
            $v_temp_tarname = uniqid('pcltar-').'.tmp';
3048
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3049
            if (($v_temp_tar = @gzopen($v_temp_tarname, 'wb')) == 0) {
3050
                // ----- Close tar file
3051
                gzclose($v_tar);
3052
3053
                // ----- Error log
3054
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3055
3056
                // ----- Return
3057
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3058
3059
                return PclErrorCode();
3060
            }
3061
        }
3062
3063
        // ----- Prepare the list of files
3064
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3065
            // ----- Reset the found list
3066
            $v_found_list[$i] = 0;
3067
3068
            // ----- Calculate the stored filename
3069
            $v_stored_list[$i] = $p_file_list[$i];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$v_stored_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $v_stored_list = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
3070
            if ($p_remove_dir != '') {
3071
                if (substr($p_file_list[$i], -1) !== '/') {
3072
                    $p_remove_dir .= '/';
3073
                }
3074
3075
                if (0 === strpos($p_file_list[$i], $p_remove_dir)) {
3076
                    $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
0 ignored issues
show
Bug introduced by
The variable $v_stored_list does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
3077
                    TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3078
                }
3079
            }
3080
            if ($p_add_dir != '') {
3081
                if (substr($p_add_dir, -1) === '/') {
3082
                    $v_stored_list[$i] = $p_add_dir.$v_stored_list[$i];
3083
                } else {
3084
                    $v_stored_list[$i] = $p_add_dir.'/'.$v_stored_list[$i];
3085
                }
3086
                TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3087
            }
3088
            $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
3089
            TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
3090
        }
3091
3092
        // ----- Update file cache
3093
        clearstatcache();
3094
3095
        // ----- Read the blocks
3096
        while (!($v_end_of_file = ($p_tar_mode === 'tar' ? feof($v_tar) : gzeof($v_tar)))) {
3097
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
3098
3099
            // ----- Clear cache of file infos
3100
            clearstatcache();
3101
3102
            // ----- Reset current found filename
3103
            $v_current_filename = '';
3104
3105
            // ----- Reset delete tag
3106
            $v_delete_file = false;
0 ignored issues
show
Unused Code introduced by
$v_delete_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
3107
3108
            // ----- Read the first 512 block header
3109
            if ($p_tar_mode === 'tar') {
3110
                $v_binary_data = fread($v_tar, 512);
3111
            } else {
3112
                $v_binary_data = gzread($v_tar, 512);
3113
            }
3114
3115
            // ----- Read the header properties
3116 View Code Duplication
            if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3117
                // ----- Close the archive file
3118
                if ($p_tar_mode === 'tar') {
3119
                    fclose($v_tar);
3120
                    fclose($v_temp_tar);
3121
                } else {
3122
                    gzclose($v_tar);
3123
                    gzclose($v_temp_tar);
3124
                }
3125
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3126
3127
                // ----- Return
3128
                TrFctEnd(__FILE__, __LINE__, $v_result);
3129
3130
                return $v_result;
3131
            }
3132
3133
            // ----- Look for empty blocks to skip
3134
            if ($v_header['filename'] == '') {
3135
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
3136
                continue;
3137
            }
3138
3139
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file '.$v_header['filename'].", size '".$v_header['size']."'");
3140
3141
            // ----- Look for filenames to update
3142
            for ($i = 0, $v_update_file = false, $v_found_file = false; ($i < count($v_stored_list)) && (!$v_update_file); ++$i) {
3143
                TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
3144
3145
                // ----- Compare the file names
3146
                if ($v_stored_list[$i] == $v_header['filename']) {
3147
                    TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
3148
                    TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' mtime=".filemtime($p_file_list[$i]).' '.date('l dS of F Y h:i:s A', filemtime($p_file_list[$i])));
3149
                    TrFctMessage(__FILE__, __LINE__, 3, 'Archived mtime='.$v_header['mtime'].' '.date('l dS of F Y h:i:s A', $v_header['mtime']));
3150
3151
                    // ----- Store found informations
3152
                    $v_found_file = true;
3153
                    $v_current_filename = $p_file_list[$i];
3154
3155
                    // ----- Look if the file need to be updated
3156
                    if (filemtime($p_file_list[$i]) > $v_header['mtime']) {
3157
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
3158
                        $v_update_file = true;
3159
                    } else {
3160
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
3161
                        $v_update_file = false;
3162
                    }
3163
3164
                    // ----- Flag the name in order not to add the file at the end
3165
                    $v_found_list[$i] = 1;
3166
                } else {
3167
                    TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not ".$v_header['filename'].'');
3168
                }
3169
            }
3170
3171
            // ----- Copy files that do not need to be updated
3172
            if (!$v_update_file) {
3173
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file '.$v_header['filename'].'');
3174
3175
                // ----- Write the file header
3176
                if ($p_tar_mode === 'tar') {
3177
                    fwrite($v_temp_tar, $v_binary_data, 512);
3178
                } else {
3179
                    gzputs($v_temp_tar, $v_binary_data, 512);
3180
                }
3181
3182
                // ----- Write the file data
3183
                $n = ceil($v_header['size'] / 512);
3184
                for ($j = 0; $j < $n; ++$j) {
3185
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number '.($j + 1));
3186
                    if ($p_tar_mode === 'tar') {
3187
                        $v_content = fread($v_tar, 512);
3188
                        fwrite($v_temp_tar, $v_content, 512);
3189
                    } else {
3190
                        $v_content = gzread($v_tar, 512);
3191
                        gzwrite($v_temp_tar, $v_content, 512);
3192
                    }
3193
                }
3194
3195
                // ----- File name and properties are logged if listing mode or file is extracted
3196
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file '.$v_header['filename'].'');
3197
3198
                // ----- Add the array describing the file into the list
3199
                $p_list_detail[$v_nb] = $v_header;
3200
                $p_list_detail[$v_nb]['status'] = ($v_found_file ? 'not_updated' : 'ok');
3201
3202
                // ----- Increment
3203
                ++$v_nb;
3204
            } // ----- Look for file that need to be updated
3205
            else {
3206
                // ----- Trace
3207
                TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
3208
3209
                // ----- Store the old file size
3210
                $v_old_size = $v_header['size'];
3211
3212
                // ----- Add the file
3213 View Code Duplication
                if (($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3214
                    // ----- Close the tarfile
3215
                    if ($p_tar_mode === 'tar') {
3216
                        fclose($v_tar);
3217
                        fclose($v_temp_tar);
3218
                    } else {
3219
                        gzclose($v_tar);
3220
                        gzclose($v_temp_tar);
3221
                    }
3222
                    @unlink($p_temp_tarname);
0 ignored issues
show
Bug introduced by
The variable $p_temp_tarname does not exist. Did you mean $p_tarname?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3223
3224
                    // ----- Return status
3225
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3226
3227
                    return $v_result;
3228
                }
3229
3230
                // ----- Trace
3231
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip old file '.$v_header['filename'].'');
3232
3233
                // ----- Jump to next file
3234
                if ($p_tar_mode === 'tar') {
3235
                    fseek($v_tar, ftell($v_tar) + (ceil($v_old_size / 512) * 512));
3236
                } else {
3237
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_old_size / 512) * 512));
3238
                }
3239
3240
                // ----- Add the array describing the file into the list
3241
                $p_list_detail[$v_nb] = $v_header;
3242
                $p_list_detail[$v_nb]['status'] = 'updated';
3243
3244
                // ----- Increment
3245
                ++$v_nb;
3246
            }
3247
3248
            // ----- Look for end of file
3249
            if ($p_tar_mode === 'tar') {
3250
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
3251
            } else {
3252
                $v_end_of_file = gzeof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
3253
            }
3254
        }
3255
3256
        // ----- Look for files that does not exists in the archive and need to be added
3257
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3258
            // ----- Look if file not found in the archive
3259
            if (!$v_found_list[$i]) {
3260
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be added");
3261
3262
                // ----- Add the file
3263 View Code Duplication
                if (($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3264
                    // ----- Close the tarfile
3265
                    if ($p_tar_mode === 'tar') {
3266
                        fclose($v_tar);
3267
                        fclose($v_temp_tar);
3268
                    } else {
3269
                        gzclose($v_tar);
3270
                        gzclose($v_temp_tar);
3271
                    }
3272
                    @unlink($p_temp_tarname);
0 ignored issues
show
Bug introduced by
The variable $p_temp_tarname does not exist. Did you mean $p_tarname?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3273
3274
                    // ----- Return status
3275
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3276
3277
                    return $v_result;
3278
                }
3279
3280
                // ----- Add the array describing the file into the list
3281
                $p_list_detail[$v_nb] = $v_header;
3282
                $p_list_detail[$v_nb]['status'] = 'added';
3283
3284
                // ----- Increment
3285
                ++$v_nb;
3286
            } else {
3287
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' was already updated if needed");
3288
            }
3289
        }
3290
3291
        // ----- Write the last empty buffer
3292
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
3293
3294
        // ----- Close the tarfile
3295
        if ($p_tar_mode === 'tar') {
3296
            fclose($v_tar);
3297
            fclose($v_temp_tar);
3298
        } else {
3299
            gzclose($v_tar);
3300
            gzclose($v_temp_tar);
3301
        }
3302
3303
        // ----- Unlink tar file
3304
        if (!@unlink($p_tarname)) {
3305
            // ----- Error log
3306
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
3307
        }
3308
3309
        // ----- Rename tar file
3310
        if (!@rename($v_temp_tarname, $p_tarname)) {
3311
            // ----- Error log
3312
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
3313
3314
            // ----- Return
3315
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3316
3317
            return PclErrorCode();
3318
        }
3319
3320
        // ----- Return
3321
        TrFctEnd(__FILE__, __LINE__, $v_result);
3322
3323
        return $v_result;
3324
    }
3325
3326
    // --------------------------------------------------------------------------------
3327
3328
    // --------------------------------------------------------------------------------
3329
    // Function : PclTarHandleReadHeader()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3330
    // Description :
3331
    // Parameters :
3332
    // Return Values :
3333
    // --------------------------------------------------------------------------------
3334
    /**
3335
     * @param $v_binary_data
3336
     * @param $v_header
3337
     *
3338
     * @return int
3339
     */
3340
    function PclTarHandleReadHeader($v_binary_data, &$v_header)
3341
    {
3342
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleReadHeader', '');
3343
        $v_result = 1;
3344
3345
        // ----- Read the 512 bytes header
3346
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3347
    if ($p_tar_mode == "tar")
3348
      $v_binary_data = fread($p_tar, 512);
3349
    else
3350
      $v_binary_data = gzread($p_tar, 512);
3351
    */
3352
3353
        // ----- Look for no more block
3354 View Code Duplication
        if ('' === $v_binary_data) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3355
            $v_header['filename'] = '';
3356
            $v_header['status'] = 'empty';
3357
3358
            // ----- Return
3359
            TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3360
3361
            return $v_result;
3362
        }
3363
3364
        // ----- Look for invalid block size
3365
        if (strlen($v_binary_data) != 512) {
3366
            $v_header['filename'] = '';
3367
            $v_header['status'] = 'invalid_header';
3368
            TrFctMessage(__FILE__, __LINE__, 2, 'Invalid block size : '.strlen($v_binary_data));
3369
3370
            // ----- Error log
3371
            PclErrorLog(-10, 'Invalid block size : '.strlen($v_binary_data));
3372
3373
            // ----- Return
3374
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3375
3376
            return PclErrorCode();
3377
        }
3378
3379
        // ----- Calculate the checksum
3380
        $v_checksum = 0;
3381
        // ..... First part of the header
3382 View Code Duplication
        for ($i = 0; $i < 148; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3383
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3384
        }
3385
        // ..... Ignore the checksum value and replace it by ' ' (space)
3386
        for ($i = 148; $i < 156; ++$i) {
3387
            $v_checksum += ord(' ');
3388
        }
3389
        // ..... Last part of the header
3390 View Code Duplication
        for ($i = 156; $i < 512; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3391
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3392
        }
3393
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
3394
3395
        // ----- Extract the values
3396
        TrFctMessage(__FILE__, __LINE__, 2, "Header : '$v_binary_data'");
3397
        $v_data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $v_binary_data);
3398
3399
        // ----- Extract the checksum for check
3400
        $v_header['checksum'] = octdec(trim($v_data['checksum']));
3401
        TrFctMessage(__FILE__, __LINE__, 3, 'File checksum : '.$v_header['checksum'].'');
3402
        if ($v_header['checksum'] != $v_checksum) {
3403
            TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : $v_checksum calculated, ".$v_header['checksum'].' expected');
3404
3405
            $v_header['filename'] = '';
3406
            $v_header['status'] = 'invalid_header';
3407
3408
            // ----- Look for last block (empty block)
3409 View Code Duplication
            if (($v_checksum == 256) && ($v_header['checksum'] == 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3410
                $v_header['status'] = 'empty';
3411
                // ----- Return
3412
                TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3413
3414
                return $v_result;
3415
            }
3416
3417
            // ----- Error log
3418
            PclErrorLog(-13, "Invalid checksum : $v_checksum calculated, ".$v_header['checksum'].' expected');
3419
3420
            // ----- Return
3421
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3422
3423
            return PclErrorCode();
3424
        }
3425
        TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ($v_checksum)");
3426
3427
        // ----- Extract the properties
3428
        $v_header['filename'] = trim($v_data['filename']);
3429
        TrFctMessage(__FILE__, __LINE__, 2, 'Name : '.$v_header['filename'].'');
3430
        $v_header['mode'] = octdec(trim($v_data['mode']));
3431
        TrFctMessage(__FILE__, __LINE__, 2, "Mode : '".decoct($v_header['mode'])."'");
3432
        $v_header['uid'] = octdec(trim($v_data['uid']));
3433
        TrFctMessage(__FILE__, __LINE__, 2, "Uid : '".$v_header['uid']."'");
3434
        $v_header['gid'] = octdec(trim($v_data['gid']));
3435
        TrFctMessage(__FILE__, __LINE__, 2, "Gid : '".$v_header['gid']."'");
3436
        $v_header['size'] = octdec(trim($v_data['size']));
3437
        TrFctMessage(__FILE__, __LINE__, 2, "Size : '".$v_header['size']."'");
3438
        $v_header['mtime'] = octdec(trim($v_data['mtime']));
3439
        TrFctMessage(__FILE__, __LINE__, 2, 'Date : '.date('l dS of F Y h:i:s A', $v_header['mtime']));
3440
        if (($v_header['typeflag'] = $v_data['typeflag']) == '5') {
3441
            $v_header['size'] = 0;
3442
            TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '".$v_header['size']."'");
3443
        }
3444
        TrFctMessage(__FILE__, __LINE__, 2, 'File typeflag : '.$v_header['typeflag'].'');
3445
        /* ----- All these fields are removed form the header because they do not carry interesting info
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3446
    $v_header[link] = trim($v_data[link]);
3447
    TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]");
3448
    $v_header[magic] = trim($v_data[magic]);
3449
    TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]");
3450
    $v_header[version] = trim($v_data[version]);
3451
    TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]");
3452
    $v_header[uname] = trim($v_data[uname]);
3453
    TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]");
3454
    $v_header[gname] = trim($v_data[gname]);
3455
    TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]");
3456
    $v_header[devmajor] = trim($v_data[devmajor]);
3457
    TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]");
3458
    $v_header[devminor] = trim($v_data[devminor]);
3459
    TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]");
3460
    */
3461
3462
        // ----- Set the status field
3463
        $v_header['status'] = 'ok';
3464
3465
        // ----- Return
3466
        TrFctEnd(__FILE__, __LINE__, $v_result);
3467
3468
        return $v_result;
3469
    }
3470
3471
    // --------------------------------------------------------------------------------
3472
3473
    // --------------------------------------------------------------------------------
3474
    // Function : PclTarHandlerDirCheck()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3475
    // Description :
3476
    //   Check if a directory exists, if not it creates it and all the parents directory
3477
    //   which may be useful.
3478
    // Parameters :
3479
    //   $p_dir : Directory path to check (without / at the end).
3480
    // Return Values :
3481
    //    1 : OK
3482
    //   -1 : Unable to create directory
3483
    // --------------------------------------------------------------------------------
3484
    /**
3485
     * @param $p_dir
3486
     *
3487
     * @return int
3488
     */
3489
    function PclTarHandlerDirCheck($p_dir)
3490
    {
3491
        $v_result = 1;
3492
3493
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlerDirCheck', "$p_dir");
3494
3495
        // ----- Check the directory availability
3496
        if (('' == $p_dir) || is_dir($p_dir)) {
3497
            TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
3498
3499
            return 1;
3500
        }
3501
3502
        // ----- Look for file alone
3503
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3504
    if (!strstr("$p_dir", "/")) {
3505
      TrFctEnd(__FILE__, __LINE__,  "'$p_dir' is a file with no directory");
3506
3507
      return 1;
3508
    }
3509
    */
3510
3511
        // ----- Extract parent directory
3512
        $p_parent_dir = dirname($p_dir);
3513
        TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
3514
3515
        // ----- Just a check
3516
        if ($p_parent_dir != $p_dir) {
3517
            // ----- Look for parent directory
3518
            if ($p_parent_dir != '') {
3519
                if (($v_result = PclTarHandlerDirCheck($p_parent_dir)) != 1) {
3520
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3521
3522
                    return $v_result;
3523
                }
3524
            }
3525
        }
3526
3527
        // ----- Create the directory
3528
        TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
3529
        if (!@mkdir($p_dir, 0777)) {
3530
            // ----- Error log
3531
            PclErrorLog(-8, "Unable to create directory '$p_dir'");
3532
3533
            // ----- Return
3534
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3535
3536
            return PclErrorCode();
3537
        }
3538
3539
        // ----- Return
3540
        TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
3541
3542
        return $v_result;
3543
    }
3544
3545
    // --------------------------------------------------------------------------------
3546
3547
    // --------------------------------------------------------------------------------
3548
    // Function : PclTarHandleExtension()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3549
    // Description :
3550
    // Parameters :
3551
    // Return Values :
3552
    // --------------------------------------------------------------------------------
3553
    /**
3554
     * @param $p_tarname
3555
     *
3556
     * @return string
3557
     */
3558
    function PclTarHandleExtension($p_tarname)
3559
    {
3560
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtension', "tar=$p_tarname");
3561
3562
        // ----- Look for file extension
3563
        if ((substr($p_tarname, -7) === '.tar.gz') || (substr($p_tarname, -4) === '.tgz')) {
3564
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a gzip tar');
3565
            $v_tar_mode = 'tgz';
3566
        } else {
3567
            if (substr($p_tarname, -4) === '.tar') {
3568
                TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a tar');
3569
                $v_tar_mode = 'tar';
3570
            } else {
3571
                // ----- Error log
3572
                PclErrorLog(-9, 'Invalid archive extension');
3573
3574
                TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3575
3576
                $v_tar_mode = '';
3577
            }
3578
        }
3579
3580
        // ----- Return
3581
        TrFctEnd(__FILE__, __LINE__, $v_tar_mode);
3582
3583
        return $v_tar_mode;
3584
    }
3585
3586
    // --------------------------------------------------------------------------------
3587
3588
    // --------------------------------------------------------------------------------
3589
    // Function : PclTarHandlePathReduction()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3590
    // Description :
3591
    // Parameters :
3592
    // Return Values :
3593
    // --------------------------------------------------------------------------------
3594
    /**
3595
     * @param $p_dir
3596
     *
3597
     * @return string
3598
     */
3599
    function PclTarHandlePathReduction($p_dir)
3600
    {
3601
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlePathReduction', "dir='$p_dir'");
3602
        $v_result = '';
3603
3604
        // ----- Look for not empty path
3605
        if ($p_dir != '') {
3606
            // ----- Explode path by directory names
3607
            $v_list = explode('/', $p_dir);
3608
3609
            // ----- Study directories from last to first
3610
            for ($i = count($v_list) - 1; $i >= 0; --$i) {
3611
                // ----- Look for current path
3612
                if ($v_list[$i] === '.') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
3613
                    // ----- Ignore this directory
3614
                    // Should be the first $i=0, but no check is done
3615
                } else {
3616
                    if ($v_list[$i] === '..') {
3617
                        // ----- Ignore it and ignore the $i-1
3618
                        --$i;
3619
                    } else {
3620
                        if ((0 != $i) && ('' == $v_list[$i]) && ($i != (count($v_list) - 1))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
3621
                            // ----- Ignore only the double '//' in path,
3622
                            // but not the first and last '/'
3623
                        } else {
3624
                            $v_result = $v_list[$i].($i != (count($v_list) - 1) ? '/'.$v_result : '');
3625
                        }
3626
                    }
3627
                }
3628
            }
3629
        }
3630
3631
        // ----- Return
3632
        TrFctEnd(__FILE__, __LINE__, $v_result);
3633
3634
        return $v_result;
3635
    }
3636
3637
    // --------------------------------------------------------------------------------
3638
3639
    // ----- End of double include look
3640
}
3641