Passed
Push — master ( d2520f...3f8ec2 )
by Michael
02:50
created

PclTarHandleExtractByIndex()   D

Complexity

Conditions 20
Paths 8

Size

Total Lines 126
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 4.7294
c 0
b 0
f 0
cc 20
eloc 58
nc 8
nop 8

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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|array $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;
106
107
        // ----- Look for default mode
108
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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
        if (is_array($p_filelist)) {
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)) {
0 ignored issues
show
introduced by
The condition is_string($p_filelist) is always true.
Loading history...
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;
176
        $v_list_detail = [];
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;
254
        $p_list_detail = [];
255
256
        // ----- Extract the tar format from the extension
257
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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
        if (is_array($p_filelist)) {
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 (1 != $v_result) {
288
            TrFctEnd(__FILE__, __LINE__, 0);
289
290
            return 0;
291
        }
292
        TrFctEnd(__FILE__, __LINE__, $p_list_detail);
0 ignored issues
show
Bug introduced by
$p_list_detail of type array is incompatible with the type integer expected by parameter $p_return of TrFctEnd(). ( Ignorable by Annotation )

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

292
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $p_list_detail);
Loading history...
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
    function PclTarList($p_tarname, $p_mode = '')
327
    {
328
        TrFctStart(__FILE__, __LINE__, 'PclTarList', "tar=$p_tarname, mode='$p_mode'");
329
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
330
331
        // ----- Extract the tar format from the extension
332
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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 = [];
343
        if (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'list', '', $p_mode, ''))) {
344
            unset($p_list);
345
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
346
347
            return 0;
348
        }
349
350
        // ----- Return
351
        TrFctEnd(__FILE__, __LINE__, $p_list);
0 ignored issues
show
Bug introduced by
$p_list of type array is incompatible with the type integer expected by parameter $p_return of TrFctEnd(). ( Ignorable by Annotation )

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

351
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $p_list);
Loading history...
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
    function PclTarExtract($p_tarname, $p_path = './', $p_remove_path = '', $p_mode = '')
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
The assignment to $v_result is dead and can be removed.
Loading history...
393
394
        // ----- Extract the tar format from the extension
395
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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 (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'complete', $p_path, $p_mode, $p_remove_path))) {
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
    function PclTarExtractList($p_tarname, $p_filelist, $p_path = './', $p_remove_path = '', $p_mode = '')
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;
462
463
        // ----- Extract the tar format from the extension
464
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
465
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $p_mode is dead and can be removed.
Loading history...
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 (1 != ($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_tar_mode seems to be never defined.
Loading history...
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
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 (1 != ($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path))) {
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
    function PclTarExtractIndex($p_tarname, $p_index, $p_path = './', $p_remove_path = '', $p_mode = '')
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;
557
558
        // ----- Extract the tar format from the extension
559
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
560
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $p_mode is dead and can be removed.
Loading history...
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 (1 != ($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_tar_mode seems to be never defined.
Loading history...
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
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 (1 != ($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode))) {
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;
630
631
        // ----- Extract the tar format from the extension
632
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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 (1 != ($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
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 (1 != ($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode))) {
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
    function PclTarUpdate($p_tarname, $p_filelist, $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
706
    {
707
        TrFctStart(__FILE__, __LINE__, 'PclTarUpdate', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
708
        $v_result = 1;
709
710
        // ----- Extract the tar format from the extension
711
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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 (1 != ($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
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 (1 != ($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir))) {
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
The assignment to $v_result is dead and can be removed.
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) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
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) || (('tar' !== $p_mode_add) && ('tgz' !== $p_mode_add))) {
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
        if ((!is_file($p_tarname)) || ((0 != (($v_size = filesize($p_tarname)) % 512)) && ('tar' === $p_mode))) {
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
        if ((!is_file($p_tarname_add))
835
            || ((0 != (($v_size_add = filesize($p_tarname_add)) % 512))
0 ignored issues
show
Unused Code introduced by
The assignment to $v_size_add is dead and can be removed.
Loading history...
836
                && ('tar' === $p_mode_add))) {
837
            // ----- Error log
838
            if (!is_file($p_tarname_add)) {
839
                PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
840
            } else {
841
                PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size " . filesize($p_tarname_add) . '(not a 512 block multiple)');
842
            }
843
844
            // ----- Return
845
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
846
847
            return PclErrorCode();
848
        }
849
850
        // ----- Look for compressed archive
851
        if ('tgz' === $p_mode) {
852
            // ----- Open the file in read mode
853
            if (0 == ($p_tar = @gzopen($p_tarname, 'rb'))) {
0 ignored issues
show
introduced by
The condition 0 == $p_tar = @gzopen($p_tarname, 'rb') is always false.
Loading history...
854
                // ----- Error log
855
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
856
857
                // ----- Return
858
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
859
860
                return PclErrorCode();
861
            }
862
863
            // ----- Open a temporary file in write mode
864
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
865
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
866
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_temp_tar = @gzopen($v_temp_tarname, 'wb') is always false.
Loading history...
867
                // ----- Close tar file
868
                gzclose($p_tar);
869
870
                // ----- Error log
871
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
872
873
                // ----- Return
874
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
875
876
                return PclErrorCode();
877
            }
878
879
            // ----- Read the first 512 bytes block
880
            $v_buffer = gzread($p_tar, 512);
881
882
            // ----- Read the following blocks but not the last one
883
            if (!gzeof($p_tar)) {
884
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
885
                $i = 1;
886
887
                // ----- Read new 512 block and write the already read
888
                do {
889
                    // ----- Write the already read block
890
                    $v_binary_data = pack('a512', "$v_buffer");
891
                    gzputs($v_temp_tar, $v_binary_data);
892
893
                    ++$i;
894
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
895
896
                    // ----- Read next block
897
                    $v_buffer = gzread($p_tar, 512);
898
                } while (!gzeof($p_tar));
899
900
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
901
            }
902
        } // ----- Look for uncompressed tar file
903
        else {
904
            if ('tar' === $p_mode) {
905
                // ----- Open the tar file
906
                if (0 == ($p_tar = fopen($p_tarname, 'r+b'))) {
907
                    // ----- Error log
908
                    PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
909
910
                    // ----- Return
911
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
912
913
                    return PclErrorCode();
914
                }
915
916
                // ----- Go to the beginning of last block
917
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

917
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell(/** @scrutinizer ignore-type */ $p_tar) : gztell($p_tar)));
Loading history...
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $zp of gztell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

917
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell(/** @scrutinizer ignore-type */ $p_tar)));
Loading history...
918
                fseek($p_tar, $v_size - 512);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

918
                fseek(/** @scrutinizer ignore-type */ $p_tar, $v_size - 512);
Loading history...
919
                TrFctMessage(__FILE__, __LINE__, 4, 'Position after :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
920
            } // ----- Look for unknown type
921
            else {
922
                // ----- Error log
923
                PclErrorLog(-3, "Invalid tar mode $p_mode");
924
925
                // ----- Return
926
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
927
928
                return PclErrorCode();
929
            }
930
        }
931
932
        // ----- Look for type of archive to add
933
        if ('tgz' === $p_mode_add) {
934
            TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
935
936
            // ----- Open the file in read mode
937
            if (0 == ($p_tar_add = @gzopen($p_tarname_add, 'rb'))) {
0 ignored issues
show
introduced by
The condition 0 == $p_tar_add = @gzopen($p_tarname_add, 'rb') is always false.
Loading history...
938
                // ----- Error log
939
                PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
940
941
                // ----- Return
942
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
943
944
                return PclErrorCode();
945
            }
946
947
            // ----- Read the first 512 bytes block
948
            $v_buffer = gzread($p_tar_add, 512);
949
950
            // ----- Read the following blocks but not the last one
951
            if (!gzeof($p_tar_add)) {
952
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
953
                $i = 1;
954
955
                // ----- Read new 512 block and write the already read
956
                do {
957
                    // ----- Write the already read block
958
                    $v_binary_data = pack('a512', "$v_buffer");
959
                    if ('tar' === $p_mode) {
960
                        fwrite($p_tar, $v_binary_data);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

960
                        fwrite(/** @scrutinizer ignore-type */ $p_tar, $v_binary_data);
Loading history...
961
                    } else {
962
                        gzputs($v_temp_tar, $v_binary_data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_temp_tar does not seem to be defined for all execution paths leading up to this point.
Loading history...
963
                    }
964
965
                    ++$i;
966
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
967
968
                    // ----- Read next block
969
                    $v_buffer = gzread($p_tar_add, 512);
970
                } while (!gzeof($p_tar_add));
971
972
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
973
            }
974
975
            // ----- Close the files
976
            gzclose($p_tar_add);
977
        } // ----- Look for uncompressed tar file
978
        else {
979
            if ('tar' === $p_mode) {
980
                // ----- Open the file in read mode
981
                if (0 == ($p_tar_add = @fopen($p_tarname_add, 'rb'))) {
982
                    // ----- Error log
983
                    PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
984
985
                    // ----- Return
986
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
987
988
                    return PclErrorCode();
989
                }
990
991
                // ----- Read the first 512 bytes block
992
                $v_buffer = fread($p_tar_add, 512);
0 ignored issues
show
Bug introduced by
It seems like $p_tar_add can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

992
                $v_buffer = fread(/** @scrutinizer ignore-type */ $p_tar_add, 512);
Loading history...
993
994
                // ----- Read the following blocks but not the last one
995
                if (!feof($p_tar_add)) {
0 ignored issues
show
Bug introduced by
It seems like $p_tar_add can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

995
                if (!feof(/** @scrutinizer ignore-type */ $p_tar_add)) {
Loading history...
996
                    TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
997
                    $i = 1;
998
999
                    // ----- Read new 512 block and write the already read
1000
                    do {
1001
                        // ----- Write the already read block
1002
                        $v_binary_data = pack('a512', "$v_buffer");
1003
                        if ('tar' === $p_mode) {
1004
                            fwrite($p_tar, $v_binary_data);
1005
                        } else {
1006
                            gzputs($v_temp_tar, $v_binary_data);
1007
                        }
1008
1009
                        ++$i;
1010
                        TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
1011
1012
                        // ----- Read next block
1013
                        $v_buffer = fread($p_tar_add, 512);
1014
                    } while (!feof($p_tar_add));
1015
1016
                    TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1017
                }
1018
1019
                // ----- Close the files
1020
                fclose($p_tar_add);
0 ignored issues
show
Bug introduced by
It seems like $p_tar_add can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1020
                fclose(/** @scrutinizer ignore-type */ $p_tar_add);
Loading history...
1021
            }
1022
        }
1023
1024
        // ----- Call the footer of the tar archive
1025
        $v_result = PclTarHandleFooter($p_tar, $p_mode);
1026
1027
        // ----- Look for closing compressed archive
1028
        if ('tgz' === $p_mode) {
1029
            // ----- Close the files
1030
            gzclose($p_tar);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $zp of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1030
            gzclose(/** @scrutinizer ignore-type */ $p_tar);
Loading history...
1031
            gzclose($v_temp_tar);
1032
1033
            // ----- Unlink tar file
1034
            if (!@unlink($p_tarname)) {
1035
                // ----- Error log
1036
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1037
            }
1038
1039
            // ----- Rename tar file
1040
            if (!@rename($v_temp_tarname, $p_tarname)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_temp_tarname does not seem to be defined for all execution paths leading up to this point.
Loading history...
1041
                // ----- Error log
1042
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
1043
1044
                // ----- Return
1045
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1046
1047
                return PclErrorCode();
1048
            }
1049
1050
            // ----- Return
1051
            TrFctEnd(__FILE__, __LINE__, $v_result);
1052
1053
            return $v_result;
1054
        } // ----- Look for closing uncompressed tar file
1055
        else {
1056
            if ('tar' === $p_mode) {
1057
                // ----- Close the tarfile
1058
                fclose($p_tar);
1059
            }
1060
        }
1061
1062
        // ----- Return
1063
        TrFctEnd(__FILE__, __LINE__, $v_result);
1064
1065
        return $v_result;
1066
    }
1067
1068
    // --------------------------------------------------------------------------------
1069
1070
    // --------------------------------------------------------------------------------
1071
    // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1072
    // *****                                                        *****
1073
    // *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1074
    // --------------------------------------------------------------------------------
1075
1076
    // --------------------------------------------------------------------------------
1077
    // 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...
1078
    // Description :
1079
    // Parameters :
1080
    //   $p_tarname : Name of the tar file
1081
    //   $p_list : An array containing the file or directory names to add in the tar
1082
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1083
    // Return Values :
1084
    // --------------------------------------------------------------------------------
1085
    /**
1086
     * @param        $p_tarname
1087
     * @param        $p_list
1088
     * @param        $p_mode
1089
     * @param string $p_add_dir
1090
     * @param string $p_remove_dir
1091
     *
1092
     * @return int
1093
     */
1094
    function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir = '', $p_remove_dir = '')
1095
    {
1096
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleCreate', "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1097
        $v_result      = 1;
1098
        $v_list_detail = [];
1099
1100
        // ----- Check the parameters
1101
        if (('' == $p_tarname) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
1102
            // ----- Error log
1103
            if ('' == $p_tarname) {
1104
                PclErrorLog(-3, 'Invalid empty archive name');
1105
            } else {
1106
                PclErrorLog(-3, "Unknown mode '$p_mode'");
1107
            }
1108
1109
            // ----- Return
1110
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1111
1112
            return PclErrorCode();
1113
        }
1114
1115
        // ----- Look for tar file
1116
        if ('tar' === $p_mode) {
1117
            // ----- Open the tar file
1118
            if (0 == ($p_tar = fopen($p_tarname, 'wb'))) {
1119
                // ----- Error log
1120
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1121
1122
                // ----- Return
1123
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1124
1125
                return PclErrorCode();
1126
            }
1127
1128
            // ----- Call the adding fct inside the tar
1129
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir))) {
1130
                // ----- Call the footer of the tar archive
1131
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1132
            }
1133
1134
            // ----- Close the tarfile
1135
            fclose($p_tar);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1135
            fclose(/** @scrutinizer ignore-type */ $p_tar);
Loading history...
1136
        } // ----- Look for tgz file
1137
        else {
1138
            // ----- Open the tar file
1139
            if (0 == ($p_tar = @gzopen($p_tarname, 'wb'))) {
0 ignored issues
show
introduced by
The condition 0 == $p_tar = @gzopen($p_tarname, 'wb') is always false.
Loading history...
1140
                // ----- Error log
1141
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1142
1143
                // ----- Return
1144
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1145
1146
                return PclErrorCode();
1147
            }
1148
1149
            // ----- Call the adding fct inside the tar
1150
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir))) {
1151
                // ----- Call the footer of the tar archive
1152
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1153
            }
1154
1155
            // ----- Close the tarfile
1156
            gzclose($p_tar);
1157
        }
1158
1159
        // ----- Return
1160
        TrFctEnd(__FILE__, __LINE__, $v_result);
1161
1162
        return $v_result;
1163
    }
1164
1165
    // --------------------------------------------------------------------------------
1166
1167
    // --------------------------------------------------------------------------------
1168
    // 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...
1169
    // Description :
1170
    // Parameters :
1171
    //   $p_tarname : Name of the tar file
1172
    //   $p_list : An array containing the file or directory names to add in the tar
1173
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1174
    // Return Values :
1175
    // --------------------------------------------------------------------------------
1176
    /**
1177
     * @param $p_tarname
1178
     * @param $p_list
1179
     * @param $p_mode
1180
     * @param $p_list_detail
1181
     * @param $p_add_dir
1182
     * @param $p_remove_dir
1183
     *
1184
     * @return int
1185
     */
1186
    function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1187
    {
1188
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAppend', "tar=$p_tarname, list, mode=$p_mode");
1189
        $v_result = 1;
1190
1191
        // ----- Check the parameters
1192
        if ('' == $p_tarname) {
1193
            // ----- Error log
1194
            PclErrorLog(-3, 'Invalid empty archive name');
1195
1196
            // ----- Return
1197
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1198
1199
            return PclErrorCode();
1200
        }
1201
1202
        clearstatcache();
1203
1204
        // ----- Check the file size
1205
        if ((!is_file($p_tarname)) || ((0 != (($v_size = filesize($p_tarname)) % 512)) && ('tar' === $p_mode))) {
1206
            // ----- Error log
1207
            if (!is_file($p_tarname)) {
1208
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
1209
            } else {
1210
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size " . filesize($p_tarname) . '(not a 512 block multiple)');
1211
            }
1212
1213
            // ----- Return
1214
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1215
1216
            return PclErrorCode();
1217
        }
1218
1219
        // ----- Look for compressed archive
1220
        if ('tgz' === $p_mode) {
1221
            // ----- Open the file in read mode
1222
            if (0 == ($p_tar = @gzopen($p_tarname, 'rb'))) {
0 ignored issues
show
introduced by
The condition 0 == $p_tar = @gzopen($p_tarname, 'rb') is always false.
Loading history...
1223
                // ----- Error log
1224
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
1225
1226
                // ----- Return
1227
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1228
1229
                return PclErrorCode();
1230
            }
1231
1232
            // ----- Open a temporary file in write mode
1233
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
1234
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
1235
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_temp_tar = @gzopen($v_temp_tarname, 'wb') is always false.
Loading history...
1236
                // ----- Close tar file
1237
                gzclose($p_tar);
1238
1239
                // ----- Error log
1240
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
1241
1242
                // ----- Return
1243
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1244
1245
                return PclErrorCode();
1246
            }
1247
1248
            // ----- Read the first 512 bytes block
1249
            $v_buffer = gzread($p_tar, 512);
1250
1251
            // ----- Read the following blocks but not the last one
1252
            if (!gzeof($p_tar)) {
1253
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
1254
                $i = 1;
1255
1256
                // ----- Read new 512 block and write the already read
1257
                do {
1258
                    // ----- Write the already read block
1259
                    $v_binary_data = pack('a512', "$v_buffer");
1260
                    gzputs($v_temp_tar, $v_binary_data);
1261
1262
                    ++$i;
1263
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
1264
1265
                    // ----- Read next block
1266
                    $v_buffer = gzread($p_tar, 512);
1267
                } while (!gzeof($p_tar));
1268
1269
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1270
            }
1271
1272
            // ----- Call the adding fct inside the tar
1273
            if (1 == ($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir))) {
1274
                // ----- Call the footer of the tar archive
1275
                $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
1276
            }
1277
1278
            // ----- Close the files
1279
            gzclose($p_tar);
1280
            gzclose($v_temp_tar);
1281
1282
            // ----- Unlink tar file
1283
            if (!@unlink($p_tarname)) {
1284
                // ----- Error log
1285
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1286
            }
1287
1288
            // ----- Rename tar file
1289
            if (!@rename($v_temp_tarname, $p_tarname)) {
1290
                // ----- Error log
1291
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
1292
1293
                // ----- Return
1294
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1295
1296
                return PclErrorCode();
1297
            }
1298
1299
            // ----- Return
1300
            TrFctEnd(__FILE__, __LINE__, $v_result);
1301
1302
            return $v_result;
1303
        } // ----- Look for uncompressed tar file
1304
        else {
1305
            if ('tar' === $p_mode) {
1306
                // ----- Open the tar file
1307
                if (0 == ($p_tar = fopen($p_tarname, 'r+b'))) {
1308
                    // ----- Error log
1309
                    PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
1310
1311
                    // ----- Return
1312
                    TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1313
1314
                    return PclErrorCode();
1315
                }
1316
1317
                // ----- Go to the beginning of last block
1318
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1318
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell(/** @scrutinizer ignore-type */ $p_tar) : gztell($p_tar)));
Loading history...
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $zp of gztell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1318
                TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell(/** @scrutinizer ignore-type */ $p_tar)));
Loading history...
1319
                fseek($p_tar, $v_size - 512);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1319
                fseek(/** @scrutinizer ignore-type */ $p_tar, $v_size - 512);
Loading history...
1320
                TrFctMessage(__FILE__, __LINE__, 4, 'Position after :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1321
1322
                // ----- Call the adding fct inside the tar
1323
                if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir))) {
1324
                    // ----- Call the footer of the tar archive
1325
                    $v_result = PclTarHandleFooter($p_tar, $p_mode);
1326
                }
1327
1328
                // ----- Close the tarfile
1329
                fclose($p_tar);
0 ignored issues
show
Bug introduced by
It seems like $p_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1329
                fclose(/** @scrutinizer ignore-type */ $p_tar);
Loading history...
1330
            } // ----- Look for unknown type
1331
            else {
1332
                // ----- Error log
1333
                PclErrorLog(-3, "Invalid tar mode $p_mode");
1334
1335
                // ----- Return
1336
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1337
1338
                return PclErrorCode();
1339
            }
1340
        }
1341
1342
        // ----- Return
1343
        TrFctEnd(__FILE__, __LINE__, $v_result);
1344
1345
        return $v_result;
1346
    }
1347
1348
    // --------------------------------------------------------------------------------
1349
1350
    // --------------------------------------------------------------------------------
1351
    // 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...
1352
    // Description :
1353
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
1354
    //   different from the real path of the file. This is usefull if you want to have PclTar
1355
    //   running in any directory, and memorize relative path from an other directory.
1356
    // Parameters :
1357
    //   $p_tar : File descriptor of the tar archive
1358
    //   $p_list : An array containing the file or directory names to add in the tar
1359
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1360
    //   $p_list_detail : list of added files with their properties (specially the status field)
1361
    //   $p_add_dir : Path to add in the filename path archived
1362
    //   $p_remove_dir : Path to remove in the filename path archived
1363
    // Return Values :
1364
    // --------------------------------------------------------------------------------
1365
    /**
1366
     * @param $p_tar
1367
     * @param $p_list
1368
     * @param $p_mode
1369
     * @param $p_list_detail
1370
     * @param $p_add_dir
1371
     * @param $p_remove_dir
1372
     *
1373
     * @return int
1374
     */
1375
    function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1376
    {
1377
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddList', "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1378
        $v_result = 1;
1379
        $v_header = [];
1380
1381
        // ----- Recuperate the current number of elt in list
1382
        $v_nb = count($p_list_detail);
1383
1384
        // ----- Check the parameters
1385
        if (0 == $p_tar) {
1386
            // ----- Error log
1387
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1388
1389
            // ----- Return
1390
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1391
1392
            return PclErrorCode();
1393
        }
1394
1395
        // ----- Check the arguments
1396
        if (0 == count($p_list)) {
1397
            // ----- Error log
1398
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1399
1400
            // ----- Return
1401
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1402
1403
            return PclErrorCode();
1404
        }
1405
1406
        // ----- Loop on the files
1407
        for ($j = 0; ($j < count($p_list)) && (1 == $v_result); ++$j) {
1408
            // ----- Recuperate the filename
1409
            $p_filename = $p_list[$j];
1410
1411
            TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
1412
1413
            // ----- Skip empty file names
1414
            if ('' == $p_filename) {
1415
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip empty filename');
1416
                continue;
1417
            }
1418
1419
            // ----- Check the filename
1420
            if (!file_exists($p_filename)) {
1421
                // ----- Error log
1422
                TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
1423
                PclErrorLog(-4, "File '$p_filename' does not exists");
1424
1425
                // ----- Return
1426
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1427
1428
                return PclErrorCode();
1429
            }
1430
1431
            // ----- Check the path length
1432
            if (strlen($p_filename) > 99) {
1433
                // ----- Error log
1434
                PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
1435
1436
                // ----- Return
1437
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1438
1439
                return PclErrorCode();
1440
            }
1441
1442
            TrFctMessage(__FILE__, __LINE__, 4, 'File position before header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1443
1444
            // ----- Add the file
1445
            if (1 != ($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir))) {
1446
                // ----- Return status
1447
                TrFctEnd(__FILE__, __LINE__, $v_result);
1448
1449
                return $v_result;
1450
            }
1451
1452
            // ----- Store the file infos
1453
            $p_list_detail[$v_nb++] = $v_header;
1454
1455
            // ----- Look for directory
1456
            if (is_dir($p_filename)) {
1457
                TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
1458
1459
                // ----- Look for path
1460
                $v_path = '';
1461
                if ('.' !== $p_filename) {
1462
                    $v_path = $p_filename . '/';
1463
                }
1464
1465
                // ----- Read the directory for files and sub-directories
1466
                $p_hdir  = opendir($p_filename);
1467
                $p_hitem = readdir($p_hdir); // '.' directory
0 ignored issues
show
Unused Code introduced by
The assignment to $p_hitem is dead and can be removed.
Loading history...
Bug introduced by
It seems like $p_hdir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1467
                $p_hitem = readdir(/** @scrutinizer ignore-type */ $p_hdir); // '.' directory
Loading history...
1468
                $p_hitem = readdir($p_hdir); // '..' directory
1469
                while ($p_hitem = readdir($p_hdir)) {
1470
                    // ----- Look for a file
1471
                    if (is_file($v_path . $p_hitem)) {
1472
                        TrFctMessage(__FILE__, __LINE__, 4, "Add the file '" . $v_path . $p_hitem . "'");
1473
1474
                        // ----- Add the file
1475
                        if (1 != ($v_result = PclTarHandleAddFile($p_tar, $v_path . $p_hitem, $p_mode, $v_header, $p_add_dir, $p_remove_dir))) {
1476
                            // ----- Return status
1477
                            TrFctEnd(__FILE__, __LINE__, $v_result);
1478
1479
                            return $v_result;
1480
                        }
1481
1482
                        // ----- Store the file infos
1483
                        $p_list_detail[$v_nb++] = $v_header;
1484
                    } // ----- Recursive call to PclTarHandleAddFile()
1485
                    else {
1486
                        TrFctMessage(__FILE__, __LINE__, 4, "'" . $v_path . $p_hitem . "' is a directory");
1487
1488
                        // ----- Need an array as parameter
1489
                        $p_temp_list[0] = $v_path . $p_hitem;
1490
                        $v_result       = PclTarHandleAddList($p_tar, $p_temp_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
1491
                    }
1492
                }
1493
1494
                // ----- Free memory for the recursive loop
1495
                unset($p_temp_list, $p_hdir, $p_hitem);
1496
            } else {
1497
                TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1498
            }
1499
        }
1500
1501
        // ----- Return
1502
        TrFctEnd(__FILE__, __LINE__, $v_result);
1503
1504
        return $v_result;
1505
    }
1506
1507
    // --------------------------------------------------------------------------------
1508
1509
    // --------------------------------------------------------------------------------
1510
    // 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...
1511
    // Description :
1512
    // Parameters :
1513
    // Return Values :
1514
    // --------------------------------------------------------------------------------
1515
    /**
1516
     * @param $p_tar
1517
     * @param $p_filename
1518
     * @param $p_mode
1519
     * @param $p_header
1520
     * @param $p_add_dir
1521
     * @param $p_remove_dir
1522
     *
1523
     * @return int
1524
     */
1525
    function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
1526
    {
1527
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddFile', "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1528
        $v_result = 1;
1529
1530
        // ----- Check the parameters
1531
        if (0 == $p_tar) {
1532
            // ----- Error log
1533
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1534
1535
            // ----- Return
1536
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1537
1538
            return PclErrorCode();
1539
        }
1540
1541
        // ----- Skip empty file names
1542
        if ('' == $p_filename) {
1543
            // ----- Error log
1544
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1545
1546
            // ----- Return
1547
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1548
1549
            return PclErrorCode();
1550
        }
1551
1552
        // ----- Calculate the stored filename
1553
        $v_stored_filename = $p_filename;
1554
        if ('' != $p_remove_dir) {
1555
            if ('/' !== substr($p_remove_dir, -1)) {
1556
                $p_remove_dir .= '/';
1557
            }
1558
1559
            if ((0 === strpos($p_filename, './')) || (0 === strpos($p_remove_dir, './'))) {
1560
                if ((0 === strpos($p_filename, './')) && (0 !== strpos($p_remove_dir, './'))) {
1561
                    $p_remove_dir = './' . $p_remove_dir;
1562
                }
1563
                if ((0 !== strpos($p_filename, './')) && (0 === strpos($p_remove_dir, './'))) {
1564
                    $p_remove_dir = substr($p_remove_dir, 2);
1565
                }
1566
            }
1567
1568
            if (0 === strpos($p_filename, $p_remove_dir)) {
1569
                $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
1570
                TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
1571
            }
1572
        }
1573
        if ('' != $p_add_dir) {
1574
            if ('/' === substr($p_add_dir, -1)) {
1575
                $v_stored_filename = $p_add_dir . $v_stored_filename;
1576
            } else {
1577
                $v_stored_filename = $p_add_dir . '/' . $v_stored_filename;
1578
            }
1579
            TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
1580
        }
1581
1582
        // ----- Check the path length
1583
        if (strlen($v_stored_filename) > 99) {
1584
            // ----- Error log
1585
            PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");
1586
1587
            // ----- Return
1588
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1589
1590
            return PclErrorCode();
1591
        }
1592
1593
        // ----- Look for a file
1594
        if (is_file($p_filename)) {
1595
            // ----- Open the source file
1596
            if (0 == ($v_file = fopen($p_filename, 'rb'))) {
1597
                // ----- Error log
1598
                PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");
1599
1600
                // ----- Return
1601
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1602
1603
                return PclErrorCode();
1604
            }
1605
1606
            // ----- Call the header generation
1607
            if (1 != ($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename))) {
1608
                // ----- Return status
1609
                TrFctEnd(__FILE__, __LINE__, $v_result);
1610
1611
                return $v_result;
1612
            }
1613
1614
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1615
1616
            // ----- Read the file by 512 octets blocks
1617
            $i = 0;
1618
            while ('' != ($v_buffer = fread($v_file, 512))) {
0 ignored issues
show
Bug introduced by
It seems like $v_file can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1618
            while ('' != ($v_buffer = fread(/** @scrutinizer ignore-type */ $v_file, 512))) {
Loading history...
1619
                $v_binary_data = pack('a512', "$v_buffer");
1620
                if ('tar' === $p_mode) {
1621
                    fwrite($p_tar, $v_binary_data);
1622
                } else {
1623
                    gzputs($p_tar, $v_binary_data);
1624
                }
1625
                ++$i;
1626
            }
1627
            TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
1628
1629
            // ----- Close the file
1630
            fclose($v_file);
0 ignored issues
show
Bug introduced by
It seems like $v_file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1630
            fclose(/** @scrutinizer ignore-type */ $v_file);
Loading history...
1631
1632
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1633
        } // ----- Look for a directory
1634
        else {
1635
            // ----- Call the header generation
1636
            if (1 != ($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename))) {
1637
                // ----- Return status
1638
                TrFctEnd(__FILE__, __LINE__, $v_result);
1639
1640
                return $v_result;
1641
            }
1642
1643
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1644
        }
1645
1646
        // ----- Return
1647
        TrFctEnd(__FILE__, __LINE__, $v_result);
1648
1649
        return $v_result;
1650
    }
1651
1652
    // --------------------------------------------------------------------------------
1653
1654
    // --------------------------------------------------------------------------------
1655
    // 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...
1656
    // Description :
1657
    //   This function creates in the TAR $p_tar, the TAR header for the file
1658
    //   $p_filename.
1659
    //
1660
    //   1. The informations needed to compose the header are recuperated and formatted
1661
    //   2. Two binary strings are composed for the first part of the header, before
1662
    //      and after checksum field.
1663
    //   3. The checksum is calculated from the two binary strings
1664
    //   4. The header is write in the tar file (first binary string, binary string
1665
    //      for checksum and last binary string).
1666
    // Parameters :
1667
    //   $p_tar : a valid file descriptor, opened in write mode,
1668
    //   $p_filename : The name of the file the header is for,
1669
    //   $p_mode : The mode of the archive ("tar" or "tgz").
1670
    //   $p_header : A pointer to a array where will be set the file properties
1671
    // Return Values :
1672
    // --------------------------------------------------------------------------------
1673
    /**
1674
     * @param $p_tar
1675
     * @param $p_filename
1676
     * @param $p_mode
1677
     * @param $p_header
1678
     * @param $p_stored_filename
1679
     *
1680
     * @return int
1681
     */
1682
    function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename)
1683
    {
1684
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleHeader', "tar=$p_tar, file='$p_filename', mode='$p_mode', stored_filename='$p_stored_filename'");
1685
        $v_result = 1;
1686
1687
        // ----- Check the parameters
1688
        if ((0 == $p_tar) || ('' == $p_filename)) {
1689
            // ----- Error log
1690
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1691
1692
            // ----- Return
1693
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1694
1695
            return PclErrorCode();
1696
        }
1697
1698
        // ----- Filename (reduce the path of stored name)
1699
        if ('' == $p_stored_filename) {
1700
            $p_stored_filename = $p_filename;
1701
        }
1702
        $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename);
1703
        TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen " . strlen($v_reduce_filename));
1704
1705
        // ----- Get file info
1706
        $v_info = stat($p_filename);
1707
        $v_uid  = sprintf('%6s ', decoct($v_info[4]));
1708
        $v_gid  = sprintf('%6s ', decoct($v_info[5]));
1709
        TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid");
1710
        $v_perms = sprintf('%6s ', decoct(fileperms($p_filename)));
1711
        TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms");
1712
1713
        // ----- File mtime
1714
        $v_mtime_data = filemtime($p_filename);
1715
        TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data");
1716
        $v_mtime = sprintf('%11s', decoct($v_mtime_data));
1717
1718
        // ----- File typeflag
1719
        // '0' or '\0' is the code for regular file
1720
        // '5' is directory
1721
        if (is_dir($p_filename)) {
1722
            $v_typeflag = '5';
1723
            $v_size     = 0;
1724
        } else {
1725
            $v_typeflag = '';
1726
1727
            // ----- Get the file size
1728
            clearstatcache();
1729
            $v_size = filesize($p_filename);
1730
        }
1731
1732
        TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size");
1733
        $v_size = sprintf('%11s ', decoct($v_size));
1734
1735
        TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag");
1736
1737
        // ----- Linkname
1738
        $v_linkname = '';
1739
1740
        // ----- Magic
1741
        $v_magic = '';
1742
1743
        // ----- Version
1744
        $v_version = '';
1745
1746
        // ----- uname
1747
        $v_uname = '';
1748
1749
        // ----- gname
1750
        $v_gname = '';
1751
1752
        // ----- devmajor
1753
        $v_devmajor = '';
1754
1755
        // ----- devminor
1756
        $v_devminor = '';
1757
1758
        // ----- prefix
1759
        $v_prefix = '';
1760
1761
        // ----- Compose the binary string of the header in two parts arround the checksum position
1762
        $v_binary_data_first = pack('a100a8a8a8a12A12', $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
1763
        $v_binary_data_last  = pack('a1a100a6a2a32a32a8a8a155a12', $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
1764
1765
        // ----- Calculate the checksum
1766
        $v_checksum = 0;
1767
        // ..... First part of the header
1768
        for ($i = 0; $i < 148; ++$i) {
1769
            $v_checksum += ord(substr($v_binary_data_first, $i, 1));
1770
        }
1771
        // ..... Ignore the checksum value and replace it by ' ' (space)
1772
        for ($i = 148; $i < 156; ++$i) {
1773
            $v_checksum += ord(' ');
1774
        }
1775
        // ..... Last part of the header
1776
        for ($i = 156, $j = 0; $i < 512; ++$i, ++$j) {
1777
            $v_checksum += ord(substr($v_binary_data_last, $j, 1));
1778
        }
1779
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
1780
1781
        // ----- Write the first 148 bytes of the header in the archive
1782
        if ('tar' === $p_mode) {
1783
            fwrite($p_tar, $v_binary_data_first, 148);
1784
        } else {
1785
            gzputs($p_tar, $v_binary_data_first, 148);
1786
        }
1787
1788
        // ----- Write the calculated checksum
1789
        $v_checksum    = sprintf('%6s ', decoct($v_checksum));
1790
        $v_binary_data = pack('a8', $v_checksum);
1791
        if ('tar' === $p_mode) {
1792
            fwrite($p_tar, $v_binary_data, 8);
1793
        } else {
1794
            gzputs($p_tar, $v_binary_data, 8);
1795
        }
1796
1797
        // ----- Write the last 356 bytes of the header in the archive
1798
        if ('tar' === $p_mode) {
1799
            fwrite($p_tar, $v_binary_data_last, 356);
1800
        } else {
1801
            gzputs($p_tar, $v_binary_data_last, 356);
1802
        }
1803
1804
        // ----- Set the properties in the header "structure"
1805
        $p_header['filename'] = $v_reduce_filename;
1806
        $p_header['mode']     = $v_perms;
1807
        $p_header['uid']      = $v_uid;
1808
        $p_header['gid']      = $v_gid;
1809
        $p_header['size']     = $v_size;
1810
        $p_header['mtime']    = $v_mtime;
1811
        $p_header['typeflag'] = $v_typeflag;
1812
        $p_header['status']   = 'added';
1813
1814
        // ----- Return
1815
        TrFctEnd(__FILE__, __LINE__, $v_result);
1816
1817
        return $v_result;
1818
    }
1819
1820
    // --------------------------------------------------------------------------------
1821
1822
    // --------------------------------------------------------------------------------
1823
    // 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...
1824
    // Description :
1825
    // Parameters :
1826
    // Return Values :
1827
    // --------------------------------------------------------------------------------
1828
    /**
1829
     * @param $p_tar
1830
     * @param $p_mode
1831
     *
1832
     * @return int
1833
     */
1834
    function PclTarHandleFooter($p_tar, $p_mode)
1835
    {
1836
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleFooter', "tar='$p_tar', p_mode=$p_mode");
1837
        $v_result = 1;
1838
1839
        // ----- Write the last 0 filled block for end of archive
1840
        $v_binary_data = pack('a512', '');
1841
        if ('tar' === $p_mode) {
1842
            fwrite($p_tar, $v_binary_data);
1843
        } else {
1844
            gzputs($p_tar, $v_binary_data);
1845
        }
1846
1847
        // ----- Return
1848
        TrFctEnd(__FILE__, __LINE__, $v_result);
1849
1850
        return $v_result;
1851
    }
1852
1853
    // --------------------------------------------------------------------------------
1854
1855
    // --------------------------------------------------------------------------------
1856
    // 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...
1857
    // Description :
1858
    // Parameters :
1859
    //   $p_tarname : Filename of the tar (or tgz) archive
1860
    //   $p_file_list : An array which contains the list of files to extract, this
1861
    //                  array may be empty when $p_mode is 'complete'
1862
    //   $p_list_detail : An array where will be placed the properties of  each extracted/listed file
1863
    //   $p_mode : 'complete' will extract all files from the archive,
1864
    //             'partial' will look for files in $p_file_list
1865
    //             'list' will only list the files from the archive without any extract
1866
    //   $p_path : Path to add while writing the extracted files
1867
    //   $p_tar_mode : 'tar' for GNU TAR archive, 'tgz' for compressed archive
1868
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
1869
    //                    extracted files. If the path does not match the file path,
1870
    //                    the file is extracted with its memorized path.
1871
    //                    $p_remove_path does not apply to 'list' mode.
1872
    //                    $p_path and $p_remove_path are commulative.
1873
    // Return Values :
1874
    // --------------------------------------------------------------------------------
1875
    /**
1876
     * @param $p_tarname
1877
     * @param $p_file_list
1878
     * @param $p_list_detail
1879
     * @param $p_mode
1880
     * @param $p_path
1881
     * @param $p_tar_mode
1882
     * @param $p_remove_path
1883
     *
1884
     * @return int
1885
     */
1886
    function PclTarHandleExtract(
1887
        $p_tarname,
1888
        $p_file_list,
1889
        &$p_list_detail,
1890
        $p_mode,
1891
        $p_path,
1892
        $p_tar_mode,
1893
        $p_remove_path)
1894
    {
1895
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtract', "archive='$p_tarname', list, mode=$p_mode, path=$p_path, tar_mode=$p_tar_mode, remove_path='$p_remove_path'");
1896
        $v_result      = 1;
1897
        $v_nb          = 0;
1898
        $v_extract_all = true;
1899
        $v_listing     = false;
1900
1901
        // ----- Check the path
1902
        //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...
1903
        if ('' == $p_path) {
1904
            $p_path = './' . $p_path;
1905
        }
1906
1907
        // ----- Look for path to remove format (should end by /)
1908
        if (('' != $p_remove_path) && ('/' !== substr($p_remove_path, -1))) {
1909
            $p_remove_path .= '/';
1910
        }
1911
        $p_remove_path_size = strlen($p_remove_path);
1912
1913
        // ----- Study the mode
1914
        switch ($p_mode) {
1915
            case 'complete':
1916
                // ----- Flag extract of all files
1917
                $v_extract_all = true;
1918
                $v_listing     = false;
1919
                break;
1920
            case 'partial':
1921
                // ----- Flag extract of specific files
1922
                $v_extract_all = false;
1923
                $v_listing     = false;
1924
                break;
1925
            case 'list':
1926
                // ----- Flag list of all files
1927
                $v_extract_all = false;
1928
                $v_listing     = true;
1929
                break;
1930
            default:
1931
                // ----- Error log
1932
                PclErrorLog(-3, "Invalid extract mode ($p_mode)");
1933
1934
                // ----- Return
1935
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1936
1937
                return PclErrorCode();
1938
        }
1939
1940
        // ----- Open the tar file
1941
        if ('tar' === $p_tar_mode) {
1942
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
1943
            $v_tar = fopen($p_tarname, 'rb');
1944
        } else {
1945
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
1946
            $v_tar = @gzopen($p_tarname, 'rb');
1947
        }
1948
1949
        // ----- Check that the archive is open
1950
        if (0 == $v_tar) {
1951
            // ----- Error log
1952
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
1953
1954
            // ----- Return
1955
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1956
1957
            return PclErrorCode();
1958
        }
1959
1960
        // ----- Read the blocks
1961
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1961
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof(/** @scrutinizer ignore-type */ $v_tar) : gzeof($v_tar)))) {
Loading history...
Unused Code introduced by
The assignment to $v_end_of_file is dead and can be removed.
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzeof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1961
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof(/** @scrutinizer ignore-type */ $v_tar)))) {
Loading history...
1962
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
1963
1964
            // ----- Clear cache of file infos
1965
            clearstatcache();
1966
1967
            // ----- Reset extract tag
1968
            $v_extract_file       = false;
1969
            $v_extraction_stopped = 0;
1970
1971
            // ----- Read the 512 bytes header
1972
            if ('tar' === $p_tar_mode) {
1973
                $v_binary_data = fread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1973
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
1974
            } else {
1975
                $v_binary_data = gzread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1975
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
1976
            }
1977
1978
            // ----- Read the header properties
1979
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
1980
                // ----- Close the archive file
1981
                if ('tar' === $p_tar_mode) {
1982
                    fclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1982
                    fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
1983
                } else {
1984
                    gzclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1984
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
1985
                }
1986
1987
                // ----- Return
1988
                TrFctEnd(__FILE__, __LINE__, $v_result);
1989
1990
                return $v_result;
1991
            }
1992
1993
            // ----- Look for empty blocks to skip
1994
            if ('' == $v_header['filename']) {
1995
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
1996
                continue;
1997
            }
1998
1999
            TrFctMessage(__FILE__, __LINE__, 2, "Found file '" . $v_header['filename'] . "', size '" . $v_header['size'] . "'");
2000
2001
            // ----- Look for partial extract
2002
            if ((!$v_extract_all) && is_array($p_file_list)) {
2003
                TrFctMessage(__FILE__, __LINE__, 2, 'Look if the file ' . $v_header['filename'] . ' need to be extracted');
2004
2005
                // ----- By default no unzip if the file is not found
2006
                $v_extract_file = false;
2007
2008
                // ----- Look into the file list
2009
                for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
2010
                    TrFctMessage(__FILE__, __LINE__, 2, 'Compare archived file ' . $v_header['filename'] . " from asked list file '" . $p_file_list[$i] . "'");
2011
2012
                    // ----- Look if it is a directory
2013
                    if ('/' === substr($p_file_list[$i], -1)) {
2014
                        TrFctMessage(__FILE__, __LINE__, 3, 'Compare file ' . $v_header['filename'] . " with directory '$p_file_list[$i]'");
2015
2016
                        // ----- Look if the directory is in the filename path
2017
                        if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
2018
                            && (0 === strpos($v_header['filename'], $p_file_list[$i]))) {
2019
                            // ----- The file is in the directory, so extract it
2020
                            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . " is in directory '$p_file_list[$i]' : extract it");
2021
                            $v_extract_file = true;
2022
2023
                            // ----- End of loop
2024
                            break;
2025
                        }
2026
                    } // ----- It is a file, so compare the file names
2027
                    else {
2028
                        if ($p_file_list[$i] == $v_header['filename']) {
2029
                            // ----- File found
2030
                            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' should be extracted');
2031
                            $v_extract_file = true;
2032
2033
                            // ----- End of loop
2034
                            break;
2035
                        }
2036
                    }
2037
                }
2038
2039
                // ----- Trace
2040
                if (!$v_extract_file) {
2041
                    TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' should not be extracted');
2042
                }
2043
            } else {
2044
                // ----- All files need to be extracted
2045
                $v_extract_file = true;
2046
            }
2047
2048
            // ----- Look if this file need to be extracted
2049
            if ($v_extract_file && (!$v_listing)) {
2050
                // ----- Look for path to remove
2051
                if (('' != $p_remove_path) && (0 === strpos($v_header['filename'], $p_remove_path))) {
2052
                    TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2053
                    // ----- Remove the path
2054
                    $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2055
                    TrFctMessage(__FILE__, __LINE__, 3, 'Reslting file is ' . $v_header['filename'] . '');
2056
                }
2057
2058
                // ----- Add the path to the file
2059
                if (('./' !== $p_path) && ('/' !== $p_path)) {
2060
                    // ----- Look for the path end '/'
2061
                    while ('/' === substr($p_path, -1)) {
2062
                        TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2063
                        $p_path = substr($p_path, 0, -1);
2064
                        TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2065
                    }
2066
2067
                    // ----- Add the path
2068
                    if (0 === strpos($v_header['filename'], '/')) {
2069
                        $v_header['filename'] = $p_path . $v_header['filename'];
2070
                    } else {
2071
                        $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2072
                    }
2073
                }
2074
2075
                // ----- Trace
2076
                TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2077
2078
                // ----- Check that the file does not exists
2079
                if (file_exists($v_header['filename'])) {
2080
                    TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2081
2082
                    // ----- Look if file is a directory
2083
                    if (is_dir($v_header['filename'])) {
2084
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2085
2086
                        // ----- Change the file status
2087
                        $v_header['status'] = 'already_a_directory';
2088
2089
                        // ----- Skip the extract
2090
                        $v_extraction_stopped = 1;
2091
                        $v_extract_file       = 0;
2092
                    } // ----- Look if file is write protected
2093
                    else {
2094
                        if (!s_writable($v_header['filename'])) {
0 ignored issues
show
Bug introduced by
The function s_writable was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2094
                        if (!/** @scrutinizer ignore-call */ s_writable($v_header['filename'])) {
Loading history...
2095
                            TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2096
2097
                            // ----- Change the file status
2098
                            $v_header['status'] = 'write_protected';
2099
2100
                            // ----- Skip the extract
2101
                            $v_extraction_stopped = 1;
2102
                            $v_extract_file       = 0;
2103
                        }
2104
                    }
2105
                    // ----- Look if the extracted file is older
2106
                    /*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...
2107
            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']).")");
2108
2109
            // ----- Change the file status
2110
            $v_header['status'] = "newer_exist";
2111
2112
            // ----- Skip the extract
2113
            $v_extraction_stopped = 1;
2114
            $v_extract_file = 0;
2115
          }*/
2116
                } // ----- Check the directory availability and create it if necessary
2117
                else {
2118
                    if ('5' == $v_header['typeflag']) {
2119
                        $v_dir_to_check = $v_header['filename'];
2120
                    } else {
2121
                        if (false === strpos($v_header['filename'], '/')) {
2122
                            $v_dir_to_check = '';
2123
                        } else {
2124
                            $v_dir_to_check = dirname($v_header['filename']);
2125
                        }
2126
                    }
2127
2128
                    if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2129
                        TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2130
2131
                        // ----- Change the file status
2132
                        $v_header['status'] = 'path_creation_fail';
2133
2134
                        // ----- Skip the extract
2135
                        $v_extraction_stopped = 1;
2136
                        $v_extract_file       = 0;
2137
                    }
2138
                }
2139
2140
                // ----- Do the extraction
2141
                if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2142
                    // ----- Open the destination file in write mode
2143
                    if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
2144
                        TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2145
2146
                        // ----- Change the file status
2147
                        $v_header['status'] = 'write_error';
2148
2149
                        // ----- Jump to next file
2150
                        TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2151
                        if ('tar' === $p_tar_mode) {
2152
                            fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
ftell($v_tar) + ceil($v_...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of fseek(). ( Ignorable by Annotation )

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

2152
                            fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2152
                            fseek(/** @scrutinizer ignore-type */ $v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2152
                            fseek($v_tar, ftell(/** @scrutinizer ignore-type */ $v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2153
                        } else {
2154
                            gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
gztell($v_tar) + ceil($v...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of gzseek(). ( Ignorable by Annotation )

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

2154
                            gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2154
                            gzseek(/** @scrutinizer ignore-type */ $v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gztell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2154
                            gzseek($v_tar, gztell(/** @scrutinizer ignore-type */ $v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2155
                        }
2156
                    } else {
2157
                        TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2158
2159
                        // ----- Read data
2160
                        $n = floor($v_header['size'] / 512);
2161
                        for ($i = 0; $i < $n; ++$i) {
2162
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2163
                            if ('tar' === $p_tar_mode) {
2164
                                $v_content = fread($v_tar, 512);
2165
                            } else {
2166
                                $v_content = gzread($v_tar, 512);
2167
                            }
2168
                            fwrite($v_dest_file, $v_content, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_dest_file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2168
                            fwrite(/** @scrutinizer ignore-type */ $v_dest_file, $v_content, 512);
Loading history...
2169
                        }
2170
                        if (0 != ($v_header['size'] % 512)) {
2171
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read last ' . ($v_header['size'] % 512) . ' bytes in a 512 block');
2172
                            if ('tar' === $p_tar_mode) {
2173
                                $v_content = fread($v_tar, 512);
2174
                            } else {
2175
                                $v_content = gzread($v_tar, 512);
2176
                            }
2177
                            fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2178
                        }
2179
2180
                        // ----- Close the destination file
2181
                        fclose($v_dest_file);
2182
2183
                        // ----- Change the file mode, mtime
2184
                        touch($v_header['filename'], $v_header['mtime']);
2185
                        //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...
2186
                    }
2187
2188
                    // ----- Check the file size
2189
                    clearstatcache();
2190
                    if (filesize($v_header['filename']) != $v_header['size']) {
2191
                        // ----- Close the archive file
2192
                        if ('tar' === $p_tar_mode) {
2193
                            fclose($v_tar);
2194
                        } else {
2195
                            gzclose($v_tar);
2196
                        }
2197
2198
                        // ----- Error log
2199
                        PclErrorLog(-7, 'Extracted file ' . $v_header['filename'] . " does not have the correct file size '" . filesize($v_filename) . "' ('" . $v_header['size'] . "' expected). Archive may be corrupted.");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_filename seems to be never defined.
Loading history...
2200
2201
                        // ----- Return
2202
                        TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2203
2204
                        return PclErrorCode();
2205
                    }
2206
2207
                    // ----- Trace
2208
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2209
                } else {
2210
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file ' . $v_header['filename'] . ' skipped.');
2211
2212
                    // ----- Jump to next file
2213
                    TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2214
                    if ('tar' === $p_tar_mode) {
2215
                        fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2216
                    } else {
2217
                        gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2218
                    }
2219
                }
2220
            } // ----- Look for file that is not to be unzipped
2221
            else {
2222
                // ----- Trace
2223
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file ' . $v_header['filename'] . '');
2224
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2225
2226
                // ----- Jump to next file
2227
                if ('tar' === $p_tar_mode) {
2228
                    fseek($v_tar, ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
2229
                } else {
2230
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2231
                }
2232
2233
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2234
            }
2235
2236
            if ('tar' === $p_tar_mode) {
2237
                $v_end_of_file = feof($v_tar);
2238
            } else {
2239
                $v_end_of_file = gzeof($v_tar);
2240
            }
2241
2242
            // ----- File name and properties are logged if listing mode or file is extracted
2243
            if ($v_listing || $v_extract_file || $v_extraction_stopped) {
2244
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2245
2246
                // ----- Log extracted files
2247
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2248
                    $v_file_dir = '';
2249
                }
2250
                if (('' === $v_file_dir) && (0 === strpos($v_header['filename'], '/'))) {
2251
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
The assignment to $v_file_dir is dead and can be removed.
Loading history...
2252
                }
2253
2254
                // ----- Add the array describing the file into the list
2255
                $p_list_detail[$v_nb] = $v_header;
2256
2257
                // ----- Increment
2258
                ++$v_nb;
2259
            }
2260
        }
2261
2262
        // ----- Close the tarfile
2263
        if ('tar' === $p_tar_mode) {
2264
            fclose($v_tar);
2265
        } else {
2266
            gzclose($v_tar);
2267
        }
2268
2269
        // ----- Return
2270
        TrFctEnd(__FILE__, __LINE__, $v_result);
2271
2272
        return $v_result;
2273
    }
2274
2275
    // --------------------------------------------------------------------------------
2276
2277
    // --------------------------------------------------------------------------------
2278
    // 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...
2279
    // Description :
2280
    //   Extract the files which are at the indexes specified. If the 'file' at the
2281
    //   index is a directory, the directory only is created, not all the files stored
2282
    //   for that directory.
2283
    // Parameters :
2284
    //   $p_index_string : String of indexes of files to extract. The form of the
2285
    //                     string is "0,4-6,8-12" with only numbers and '-' for
2286
    //                     for range, and ',' to separate ranges. No spaces or ';'
2287
    //                     are allowed.
2288
    // Return Values :
2289
    // --------------------------------------------------------------------------------
2290
    /**
2291
     * @param $p_tarname
2292
     * @param $p_index_string
2293
     * @param $p_list_detail
2294
     * @param $p_path
2295
     * @param $p_remove_path
2296
     * @param $p_tar_mode
2297
     *
2298
     * @return int
2299
     */
2300
    function PclTarHandleExtractByIndexList(
2301
        $p_tarname,
2302
        $p_index_string,
2303
        &$p_list_detail,
2304
        $p_path,
2305
        $p_remove_path,
2306
        $p_tar_mode)
2307
    {
2308
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractByIndexList', "archive='$p_tarname', index_string='$p_index_string', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2309
        $v_result = 1;
2310
        $v_nb     = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_nb is dead and can be removed.
Loading history...
2311
2312
        // ----- TBC : I should check the string by a regexp
2313
2314
        // ----- Check the path
2315
        if (('' === $p_path)
2316
            || ((0 !== strpos($p_path, '/')) && (0 !== strpos($p_path, '../'))
2317
                && (0 !== strpos($p_path, './')))) {
2318
            $p_path = './' . $p_path;
2319
        }
2320
2321
        // ----- Look for path to remove format (should end by /)
2322
        if (('' != $p_remove_path) && ('/' !== substr($p_remove_path, -1))) {
2323
            $p_remove_path .= '/';
2324
        }
2325
        $p_remove_path_size = strlen($p_remove_path);
0 ignored issues
show
Unused Code introduced by
The assignment to $p_remove_path_size is dead and can be removed.
Loading history...
2326
2327
        // ----- Open the tar file
2328
        if ('tar' === $p_tar_mode) {
2329
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2330
            $v_tar = @fopen($p_tarname, 'rb');
2331
        } else {
2332
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2333
            $v_tar = @gzopen($p_tarname, 'rb');
2334
        }
2335
2336
        // ----- Check that the archive is open
2337
        if (0 == $v_tar) {
2338
            // ----- Error log
2339
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
2340
2341
            // ----- Return
2342
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2343
2344
            return PclErrorCode();
2345
        }
2346
2347
        // ----- Manipulate the index list
2348
        $v_list = explode(',', $p_index_string);
2349
        sort($v_list);
2350
2351
        // ----- Loop on the index list
2352
        $v_index = 0;
2353
        for ($i = 0; ($i < count($v_list)) && $v_result; ++$i) {
2354
            TrFctMessage(__FILE__, __LINE__, 3, "Looking for index part '$v_list[$i]'");
2355
2356
            // ----- Extract range
2357
            $v_index_list      = explode('-', $v_list[$i]);
2358
            $v_size_index_list = count($v_index_list);
2359
            if (1 == $v_size_index_list) {
2360
                TrFctMessage(__FILE__, __LINE__, 3, "Only one index '$v_index_list[0]'");
2361
2362
                // ----- Do the extraction
2363
                $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);
2364
            } else {
2365
                if (2 == $v_size_index_list) {
2366
                    TrFctMessage(__FILE__, __LINE__, 3, "Two indexes '$v_index_list[0]' and '$v_index_list[1]'");
2367
2368
                    // ----- Do the extraction
2369
                    $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);
2370
                }
2371
            }
2372
        }
2373
2374
        // ----- Close the tarfile
2375
        if ('tar' === $p_tar_mode) {
2376
            fclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2376
            fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2377
        } else {
2378
            gzclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2378
            gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2379
        }
2380
2381
        // ----- Return
2382
        TrFctEnd(__FILE__, __LINE__, $v_result);
2383
2384
        return $v_result;
2385
    }
2386
2387
    // --------------------------------------------------------------------------------
2388
2389
    // --------------------------------------------------------------------------------
2390
    // 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...
2391
    // Description :
2392
    // Parameters :
2393
    // Return Values :
2394
    // --------------------------------------------------------------------------------
2395
    /**
2396
     * @param $p_tar
2397
     * @param $p_index_current
2398
     * @param $p_index_start
2399
     * @param $p_index_stop
2400
     * @param $p_list_detail
2401
     * @param $p_path
2402
     * @param $p_remove_path
2403
     * @param $p_tar_mode
2404
     *
2405
     * @return int
2406
     */
2407
    function PclTarHandleExtractByIndex(
2408
        $p_tar,
2409
        &$p_index_current,
2410
        $p_index_start,
2411
        $p_index_stop,
2412
        &$p_list_detail,
2413
        $p_path,
2414
        $p_remove_path,
2415
        $p_tar_mode)
2416
    {
2417
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractByIndex', "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");
2418
        $v_result = 1;
2419
        $v_nb     = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_nb is dead and can be removed.
Loading history...
2420
2421
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2422
        $v_tar = $p_tar;
2423
2424
        // ----- Look the number of elements already in $p_list_detail
2425
        $v_nb = count($p_list_detail);
2426
2427
        // ----- Read the blocks
2428
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_end_of_file is dead and can be removed.
Loading history...
2429
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next file ...');
2430
            TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
2431
2432
            if ($p_index_current > $p_index_stop) {
2433
                TrFctMessage(__FILE__, __LINE__, 2, 'Stop extraction, past stop index');
2434
                break;
2435
            }
2436
2437
            // ----- Clear cache of file infos
2438
            clearstatcache();
2439
2440
            // ----- Reset extract tag
2441
            $v_extract_file       = false;
2442
            $v_extraction_stopped = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_extraction_stopped is dead and can be removed.
Loading history...
2443
2444
            // ----- Read the 512 bytes header
2445
            if ('tar' === $p_tar_mode) {
2446
                $v_binary_data = fread($v_tar, 512);
2447
            } else {
2448
                $v_binary_data = gzread($v_tar, 512);
2449
            }
2450
2451
            // ----- Read the header properties
2452
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
2453
                // ----- Return
2454
                TrFctEnd(__FILE__, __LINE__, $v_result);
2455
2456
                return $v_result;
2457
            }
2458
2459
            // ----- Look for empty blocks to skip
2460
            if ('' == $v_header['filename']) {
2461
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2462
                continue;
2463
            }
2464
2465
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2466
2467
            // ----- Look if file is in the range to be extracted
2468
            if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop)) {
2469
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is in the range to be extracted');
2470
                $v_extract_file = true;
2471
            } else {
2472
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is out of the range');
2473
                $v_extract_file = false;
2474
            }
2475
2476
            // ----- Look if this file need to be extracted
2477
            if ($v_extract_file) {
2478
                if (1 != ($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode))) {
2479
                    // ----- Return
2480
                    TrFctEnd(__FILE__, __LINE__, $v_result);
2481
2482
                    return $v_result;
2483
                }
2484
            } // ----- Look for file that is not to be extracted
2485
            else {
2486
                // ----- Trace
2487
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file ' . $v_header['filename'] . '');
2488
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2489
2490
                // ----- Jump to next file
2491
                if ('tar' === $p_tar_mode) {
2492
                    fseek($v_tar, ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
'tar' === $p_tar_mode ? ...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of fseek(). ( Ignorable by Annotation )

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

2492
                    fseek($v_tar, /** @scrutinizer ignore-type */ ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2493
                } else {
2494
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
gztell($v_tar) + ceil($v...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of gzseek(). ( Ignorable by Annotation )

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

2494
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2495
                }
2496
2497
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2498
            }
2499
2500
            if ('tar' === $p_tar_mode) {
2501
                $v_end_of_file = feof($v_tar);
2502
            } else {
2503
                $v_end_of_file = gzeof($v_tar);
2504
            }
2505
2506
            // ----- File name and properties are logged if listing mode or file is extracted
2507
            if ($v_extract_file) {
2508
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2509
2510
                // ----- Log extracted files
2511
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2512
                    $v_file_dir = '';
2513
                }
2514
                if (('' === $v_file_dir) && (0 === strpos($v_header['filename'], '/'))) {
2515
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
The assignment to $v_file_dir is dead and can be removed.
Loading history...
2516
                }
2517
2518
                // ----- Add the array describing the file into the list
2519
                $p_list_detail[$v_nb] = $v_header;
2520
2521
                // ----- Increment
2522
                ++$v_nb;
2523
            }
2524
2525
            // ----- Increment the current file index
2526
            ++$p_index_current;
2527
        }
2528
2529
        // ----- Return
2530
        TrFctEnd(__FILE__, __LINE__, $v_result);
2531
2532
        return $v_result;
2533
    }
2534
2535
    // --------------------------------------------------------------------------------
2536
2537
    // --------------------------------------------------------------------------------
2538
    // 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...
2539
    // Description :
2540
    // Parameters :
2541
    // Return Values :
2542
    // --------------------------------------------------------------------------------
2543
    /**
2544
     * @param $p_tar
2545
     * @param $v_header
2546
     * @param $p_path
2547
     * @param $p_remove_path
2548
     * @param $p_tar_mode
2549
     *
2550
     * @return int
2551
     */
2552
    function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
2553
    {
2554
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractFile', "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2555
        $v_result = 1;
2556
2557
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2558
        $v_tar          = $p_tar;
2559
        $v_extract_file = 1;
2560
2561
        $p_remove_path_size = strlen($p_remove_path);
2562
2563
        // ----- Look for path to remove
2564
        if (('' != $p_remove_path) && (0 === strpos($v_header['filename'], $p_remove_path))) {
2565
            TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2566
            // ----- Remove the path
2567
            $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2568
            TrFctMessage(__FILE__, __LINE__, 3, 'Resulting file is ' . $v_header['filename'] . '');
2569
        }
2570
2571
        // ----- Add the path to the file
2572
        if (('./' !== $p_path) && ('/' !== $p_path)) {
2573
            // ----- Look for the path end '/'
2574
            while ('/' === substr($p_path, -1)) {
2575
                TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2576
                $p_path = substr($p_path, 0, -1);
2577
                TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2578
            }
2579
2580
            // ----- Add the path
2581
            if (0 === strpos($v_header['filename'], '/')) {
2582
                $v_header['filename'] = $p_path . $v_header['filename'];
2583
            } else {
2584
                $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2585
            }
2586
        }
2587
2588
        // ----- Trace
2589
        TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2590
2591
        // ----- Check that the file does not exists
2592
        if (file_exists($v_header['filename'])) {
2593
            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2594
2595
            // ----- Look if file is a directory
2596
            if (is_dir($v_header['filename'])) {
2597
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2598
2599
                // ----- Change the file status
2600
                $v_header['status'] = 'already_a_directory';
2601
2602
                // ----- Skip the extract
2603
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_extraction_stopped is dead and can be removed.
Loading history...
2604
                $v_extract_file       = 0;
2605
            } // ----- Look if file is write protected
2606
            else {
2607
                if (!s_writable($v_header['filename'])) {
0 ignored issues
show
Bug introduced by
The function s_writable was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2607
                if (!/** @scrutinizer ignore-call */ s_writable($v_header['filename'])) {
Loading history...
2608
                    TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2609
2610
                    // ----- Change the file status
2611
                    $v_header['status'] = 'write_protected';
2612
2613
                    // ----- Skip the extract
2614
                    $v_extraction_stopped = 1;
2615
                    $v_extract_file       = 0;
2616
                } // ----- Look if the extracted file is older
2617
                else {
2618
                    if (filemtime($v_header['filename']) > $v_header['mtime']) {
2619
                        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']) . ')');
2620
2621
                        // ----- Change the file status
2622
                        $v_header['status'] = 'newer_exist';
2623
2624
                        // ----- Skip the extract
2625
                        $v_extraction_stopped = 1;
2626
                        $v_extract_file       = 0;
2627
                    }
2628
                }
2629
            }
2630
        } // ----- Check the directory availability and create it if necessary
2631
        else {
2632
            if ('5' == $v_header['typeflag']) {
2633
                $v_dir_to_check = $v_header['filename'];
2634
            } else {
2635
                if (false === strpos($v_header['filename'], '/')) {
2636
                    $v_dir_to_check = '';
2637
                } else {
2638
                    $v_dir_to_check = dirname($v_header['filename']);
2639
                }
2640
            }
2641
2642
            if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2643
                TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2644
2645
                // ----- Change the file status
2646
                $v_header['status'] = 'path_creation_fail';
2647
2648
                // ----- Skip the extract
2649
                $v_extraction_stopped = 1;
2650
                $v_extract_file       = 0;
2651
            }
2652
        }
2653
2654
        // ----- Do the real bytes extraction (if not a directory)
2655
        if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2656
            // ----- Open the destination file in write mode
2657
            if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
2658
                TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2659
2660
                // ----- Change the file status
2661
                $v_header['status'] = 'write_error';
2662
2663
                // ----- Jump to next file
2664
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2665
                if ('tar' === $p_tar_mode) {
2666
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
ftell($v_tar) + ceil($v_...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of fseek(). ( Ignorable by Annotation )

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

2666
                    fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2667
                } else {
2668
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
gztell($v_tar) + ceil($v...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of gzseek(). ( Ignorable by Annotation )

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

2668
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2669
                }
2670
            } else {
2671
                TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2672
2673
                // ----- Read data
2674
                $n = floor($v_header['size'] / 512);
2675
                for ($i = 0; $i < $n; ++$i) {
2676
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2677
                    if ('tar' === $p_tar_mode) {
2678
                        $v_content = fread($v_tar, 512);
2679
                    } else {
2680
                        $v_content = gzread($v_tar, 512);
2681
                    }
2682
                    fwrite($v_dest_file, $v_content, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_dest_file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2682
                    fwrite(/** @scrutinizer ignore-type */ $v_dest_file, $v_content, 512);
Loading history...
2683
                }
2684
                if (0 != ($v_header['size'] % 512)) {
2685
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read last ' . ($v_header['size'] % 512) . ' bytes in a 512 block');
2686
                    if ('tar' === $p_tar_mode) {
2687
                        $v_content = fread($v_tar, 512);
2688
                    } else {
2689
                        $v_content = gzread($v_tar, 512);
2690
                    }
2691
                    fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2692
                }
2693
2694
                // ----- Close the destination file
2695
                fclose($v_dest_file);
0 ignored issues
show
Bug introduced by
It seems like $v_dest_file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2695
                fclose(/** @scrutinizer ignore-type */ $v_dest_file);
Loading history...
2696
2697
                // ----- Change the file mode, mtime
2698
                touch($v_header['filename'], $v_header['mtime']);
2699
                //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...
2700
            }
2701
2702
            // ----- Check the file size
2703
            clearstatcache();
2704
            if (filesize($v_header['filename']) != $v_header['size']) {
2705
                // ----- Error log
2706
                PclErrorLog(-7, 'Extracted file ' . $v_header['filename'] . " does not have the correct file size '" . filesize($v_filename) . "' ('" . $v_header['size'] . "' expected). Archive may be corrupted.");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_filename seems to be never defined.
Loading history...
2707
2708
                // ----- Return
2709
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2710
2711
                return PclErrorCode();
2712
            }
2713
2714
            // ----- Trace
2715
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2716
        } else {
2717
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file ' . $v_header['filename'] . ' skipped.');
2718
2719
            // ----- Jump to next file
2720
            TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2721
            if ('tar' === $p_tar_mode) {
2722
                fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2723
            } else {
2724
                gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2725
            }
2726
        }
2727
2728
        // ----- Return
2729
        TrFctEnd(__FILE__, __LINE__, $v_result);
2730
2731
        return $v_result;
2732
    }
2733
2734
    // --------------------------------------------------------------------------------
2735
2736
    // --------------------------------------------------------------------------------
2737
    // 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...
2738
    // Description :
2739
    // Parameters :
2740
    // Return Values :
2741
    // --------------------------------------------------------------------------------
2742
    /**
2743
     * @param $p_tarname
2744
     * @param $p_file_list
2745
     * @param $p_list_detail
2746
     * @param $p_tar_mode
2747
     *
2748
     * @return int
2749
     */
2750
    function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
2751
    {
2752
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleDelete', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2753
        $v_result = 1;
2754
        $v_nb     = 0;
2755
2756
        // ----- Look for regular tar file
2757
        if ('tar' === $p_tar_mode) {
2758
            // ----- Open file
2759
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2760
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2761
                // ----- Error log
2762
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2763
2764
                // ----- Return
2765
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2766
2767
                return PclErrorCode();
2768
            }
2769
2770
            // ----- Open a temporary file in write mode
2771
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2772
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2773
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
2774
                // ----- Close tar file
2775
                fclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2775
                fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2776
2777
                // ----- Error log
2778
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2779
2780
                // ----- Return
2781
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2782
2783
                return PclErrorCode();
2784
            }
2785
        } // ----- Look for compressed tar file
2786
        else {
2787
            // ----- Open the file in read mode
2788
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2789
            if (0 == ($v_tar = @gzopen($p_tarname, 'rb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_tar = @gzopen($p_tarname, 'rb') is always false.
Loading history...
2790
                // ----- Error log
2791
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2792
2793
                // ----- Return
2794
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2795
2796
                return PclErrorCode();
2797
            }
2798
2799
            // ----- Open a temporary file in write mode
2800
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2801
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2802
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_temp_tar = @gzopen($v_temp_tarname, 'wb') is always false.
Loading history...
2803
                // ----- Close tar file
2804
                gzclose($v_tar);
2805
2806
                // ----- Error log
2807
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2808
2809
                // ----- Return
2810
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2811
2812
                return PclErrorCode();
2813
            }
2814
        }
2815
2816
        // ----- Read the blocks
2817
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_end_of_file is dead and can be removed.
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzeof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2817
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof(/** @scrutinizer ignore-type */ $v_tar)))) {
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2817
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof(/** @scrutinizer ignore-type */ $v_tar) : gzeof($v_tar)))) {
Loading history...
2818
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
2819
2820
            // ----- Clear cache of file infos
2821
            clearstatcache();
2822
2823
            // ----- Reset delete tag
2824
            $v_delete_file = false;
2825
2826
            // ----- Read the first 512 block header
2827
            if ('tar' === $p_tar_mode) {
2828
                $v_binary_data = fread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2828
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
2829
            } else {
2830
                $v_binary_data = gzread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2830
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
2831
            }
2832
2833
            // ----- Read the header properties
2834
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
2835
                // ----- Close the archive file
2836
                if ('tar' === $p_tar_mode) {
2837
                    fclose($v_tar);
2838
                    fclose($v_temp_tar);
2839
                } else {
2840
                    gzclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2840
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2841
                    gzclose($v_temp_tar);
2842
                }
2843
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

2843
                /** @scrutinizer ignore-unhandled */ @unlink($v_temp_tarname);

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...
2844
2845
                // ----- Return
2846
                TrFctEnd(__FILE__, __LINE__, $v_result);
2847
2848
                return $v_result;
2849
            }
2850
2851
            // ----- Look for empty blocks to skip
2852
            if ('' == $v_header['filename']) {
2853
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2854
                continue;
2855
            }
2856
2857
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2858
2859
            // ----- Look for filenames to delete
2860
            for ($i = 0, $v_delete_file = false; ($i < count($p_file_list)) && (!$v_delete_file); ++$i) {
2861
                // ----- Compare the file names
2862
                //        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...
2863
                if (($v_len = strcmp($p_file_list[$i], $v_header['filename'])) <= 0) {
2864
                    if (0 == $v_len) {
2865
                        TrFctMessage(__FILE__, __LINE__, 3, 'Found that ' . $v_header['filename'] . ' need to be deleted');
2866
                        $v_delete_file = true;
2867
                    } else {
2868
                        TrFctMessage(__FILE__, __LINE__, 3, 'Look if ' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2869
                        if ('/' === substr($v_header['filename'], strlen($p_file_list[$i]), 1)) {
2870
                            TrFctMessage(__FILE__, __LINE__, 3, '' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2871
                            $v_delete_file = true;
2872
                        }
2873
                    }
2874
                }
2875
            }
2876
2877
            // ----- Copy files that do not need to be deleted
2878
            if (!$v_delete_file) {
2879
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
2880
2881
                // ----- Write the file header
2882
                if ('tar' === $p_tar_mode) {
2883
                    fwrite($v_temp_tar, $v_binary_data, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_temp_tar can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2883
                    fwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_binary_data, 512);
Loading history...
2884
                } else {
2885
                    gzputs($v_temp_tar, $v_binary_data, 512);
2886
                }
2887
2888
                // ----- Write the file data
2889
                $n = ceil($v_header['size'] / 512);
2890
                for ($i = 0; $i < $n; ++$i) {
2891
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2892
                    if ('tar' === $p_tar_mode) {
2893
                        $v_content = fread($v_tar, 512);
2894
                        fwrite($v_temp_tar, $v_content, 512);
2895
                    } else {
2896
                        $v_content = gzread($v_tar, 512);
2897
                        gzwrite($v_temp_tar, $v_content, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_temp_tar can also be of type false; however, parameter $zp of gzwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2897
                        gzwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_content, 512);
Loading history...
2898
                    }
2899
                }
2900
2901
                // ----- File name and properties are logged if listing mode or file is extracted
2902
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2903
2904
                // ----- Add the array describing the file into the list
2905
                $p_list_detail[$v_nb]           = $v_header;
2906
                $p_list_detail[$v_nb]['status'] = 'ok';
2907
2908
                // ----- Increment
2909
                ++$v_nb;
2910
            } // ----- Look for file that is to be deleted
2911
            else {
2912
                // ----- Trace
2913
                TrFctMessage(__FILE__, __LINE__, 2, 'Start deletion of ' . $v_header['filename'] . '');
2914
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2914
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell(/** @scrutinizer ignore-type */ $v_tar) : gztell($v_tar)) . ']');
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gztell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2914
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell(/** @scrutinizer ignore-type */ $v_tar)) . ']');
Loading history...
2915
2916
                // ----- Jump to next file
2917
                if ('tar' === $p_tar_mode) {
2918
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2918
                    fseek(/** @scrutinizer ignore-type */ $v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
ftell($v_tar) + ceil($v_...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of fseek(). ( Ignorable by Annotation )

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

2918
                    fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2919
                } else {
2920
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
0 ignored issues
show
Bug introduced by
gztell($v_tar) + ceil($v...er['size'] / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of gzseek(). ( Ignorable by Annotation )

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

2920
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

2920
                    gzseek(/** @scrutinizer ignore-type */ $v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2921
                }
2922
2923
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2924
            }
2925
2926
            // ----- Look for end of file
2927
            if ('tar' === $p_tar_mode) {
2928
                $v_end_of_file = feof($v_tar);
2929
            } else {
2930
                $v_end_of_file = gzeof($v_tar);
2931
            }
2932
        }
2933
2934
        // ----- Write the last empty buffer
2935
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
2936
2937
        // ----- Close the tarfile
2938
        if ('tar' === $p_tar_mode) {
2939
            fclose($v_tar);
2940
            fclose($v_temp_tar);
2941
        } else {
2942
            gzclose($v_tar);
2943
            gzclose($v_temp_tar);
2944
        }
2945
2946
        // ----- Unlink tar file
2947
        if (!@unlink($p_tarname)) {
2948
            // ----- Error log
2949
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
2950
        }
2951
2952
        // ----- Rename tar file
2953
        if (!@rename($v_temp_tarname, $p_tarname)) {
2954
            // ----- Error log
2955
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
2956
2957
            // ----- Return
2958
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2959
2960
            return PclErrorCode();
2961
        }
2962
2963
        // ----- Return
2964
        TrFctEnd(__FILE__, __LINE__, $v_result);
2965
2966
        return $v_result;
2967
    }
2968
2969
    // --------------------------------------------------------------------------------
2970
2971
    // --------------------------------------------------------------------------------
2972
    // 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...
2973
    // Description :
2974
    // Parameters :
2975
    // Return Values :
2976
    // --------------------------------------------------------------------------------
2977
    /**
2978
     * @param $p_tarname
2979
     * @param $p_file_list
2980
     * @param $p_list_detail
2981
     * @param $p_tar_mode
2982
     * @param $p_add_dir
2983
     * @param $p_remove_dir
2984
     *
2985
     * @return int
2986
     */
2987
    function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
2988
    {
2989
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleUpdate', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2990
        $v_result     = 1;
2991
        $v_nb         = 0;
2992
        $v_found_list = [];
2993
2994
        // ----- Look for regular tar file
2995
        if ('tar' === $p_tar_mode) {
2996
            // ----- Open file
2997
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2998
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2999
                // ----- Error log
3000
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
3001
3002
                // ----- Return
3003
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3004
3005
                return PclErrorCode();
3006
            }
3007
3008
            // ----- Open a temporary file in write mode
3009
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
3010
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3011
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
3012
                // ----- Close tar file
3013
                fclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3013
                fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
3014
3015
                // ----- Error log
3016
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3017
3018
                // ----- Return
3019
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3020
3021
                return PclErrorCode();
3022
            }
3023
        } // ----- Look for compressed tar file
3024
        else {
3025
            // ----- Open the file in read mode
3026
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
3027
            if (0 == ($v_tar = @gzopen($p_tarname, 'rb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_tar = @gzopen($p_tarname, 'rb') is always false.
Loading history...
3028
                // ----- Error log
3029
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
3030
3031
                // ----- Return
3032
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3033
3034
                return PclErrorCode();
3035
            }
3036
3037
            // ----- Open a temporary file in write mode
3038
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
3039
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3040
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
0 ignored issues
show
introduced by
The condition 0 == $v_temp_tar = @gzopen($v_temp_tarname, 'wb') is always false.
Loading history...
3041
                // ----- Close tar file
3042
                gzclose($v_tar);
3043
3044
                // ----- Error log
3045
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3046
3047
                // ----- Return
3048
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3049
3050
                return PclErrorCode();
3051
            }
3052
        }
3053
3054
        // ----- Prepare the list of files
3055
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3056
            // ----- Reset the found list
3057
            $v_found_list[$i] = 0;
3058
3059
            // ----- Calculate the stored filename
3060
            $v_stored_list[$i] = $p_file_list[$i];
3061
            if ('' != $p_remove_dir) {
3062
                if ('/' !== substr($p_file_list[$i], -1)) {
3063
                    $p_remove_dir .= '/';
3064
                }
3065
3066
                if (0 === strpos($p_file_list[$i], $p_remove_dir)) {
3067
                    $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
3068
                    TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_stored_list does not seem to be defined for all execution paths leading up to this point.
Loading history...
3069
                }
3070
            }
3071
            if ('' != $p_add_dir) {
3072
                if ('/' === substr($p_add_dir, -1)) {
3073
                    $v_stored_list[$i] = $p_add_dir . $v_stored_list[$i];
3074
                } else {
3075
                    $v_stored_list[$i] = $p_add_dir . '/' . $v_stored_list[$i];
3076
                }
3077
                TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3078
            }
3079
            $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
3080
            TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
3081
        }
3082
3083
        // ----- Update file cache
3084
        clearstatcache();
3085
3086
        // ----- Read the blocks
3087
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_end_of_file is dead and can be removed.
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3087
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof(/** @scrutinizer ignore-type */ $v_tar) : gzeof($v_tar)))) {
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzeof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3087
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof(/** @scrutinizer ignore-type */ $v_tar)))) {
Loading history...
3088
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
3089
3090
            // ----- Clear cache of file infos
3091
            clearstatcache();
3092
3093
            // ----- Reset current found filename
3094
            $v_current_filename = '';
3095
3096
            // ----- Reset delete tag
3097
            $v_delete_file = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_delete_file is dead and can be removed.
Loading history...
3098
3099
            // ----- Read the first 512 block header
3100
            if ('tar' === $p_tar_mode) {
3101
                $v_binary_data = fread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3101
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
3102
            } else {
3103
                $v_binary_data = gzread($v_tar, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3103
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
3104
            }
3105
3106
            // ----- Read the header properties
3107
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
3108
                // ----- Close the archive file
3109
                if ('tar' === $p_tar_mode) {
3110
                    fclose($v_tar);
3111
                    fclose($v_temp_tar);
3112
                } else {
3113
                    gzclose($v_tar);
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3113
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
3114
                    gzclose($v_temp_tar);
3115
                }
3116
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

3116
                /** @scrutinizer ignore-unhandled */ @unlink($v_temp_tarname);

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...
3117
3118
                // ----- Return
3119
                TrFctEnd(__FILE__, __LINE__, $v_result);
3120
3121
                return $v_result;
3122
            }
3123
3124
            // ----- Look for empty blocks to skip
3125
            if ('' == $v_header['filename']) {
3126
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
3127
                continue;
3128
            }
3129
3130
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
3131
3132
            // ----- Look for filenames to update
3133
            for ($i = 0, $v_update_file = false, $v_found_file = false; ($i < count($v_stored_list)) && (!$v_update_file); ++$i) {
3134
                TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
3135
3136
                // ----- Compare the file names
3137
                if ($v_stored_list[$i] == $v_header['filename']) {
3138
                    TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
3139
                    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])));
3140
                    TrFctMessage(__FILE__, __LINE__, 3, 'Archived mtime=' . $v_header['mtime'] . ' ' . date('l dS of F Y h:i:s A', $v_header['mtime']));
3141
3142
                    // ----- Store found informations
3143
                    $v_found_file       = true;
3144
                    $v_current_filename = $p_file_list[$i];
3145
3146
                    // ----- Look if the file need to be updated
3147
                    if (filemtime($p_file_list[$i]) > $v_header['mtime']) {
3148
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
3149
                        $v_update_file = true;
3150
                    } else {
3151
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
3152
                        $v_update_file = false;
3153
                    }
3154
3155
                    // ----- Flag the name in order not to add the file at the end
3156
                    $v_found_list[$i] = 1;
3157
                } else {
3158
                    TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not " . $v_header['filename'] . '');
3159
                }
3160
            }
3161
3162
            // ----- Copy files that do not need to be updated
3163
            if (!$v_update_file) {
3164
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
3165
3166
                // ----- Write the file header
3167
                if ('tar' === $p_tar_mode) {
3168
                    fwrite($v_temp_tar, $v_binary_data, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_temp_tar can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3168
                    fwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_binary_data, 512);
Loading history...
3169
                } else {
3170
                    gzputs($v_temp_tar, $v_binary_data, 512);
3171
                }
3172
3173
                // ----- Write the file data
3174
                $n = ceil($v_header['size'] / 512);
3175
                for ($j = 0; $j < $n; ++$j) {
3176
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($j + 1));
3177
                    if ('tar' === $p_tar_mode) {
3178
                        $v_content = fread($v_tar, 512);
3179
                        fwrite($v_temp_tar, $v_content, 512);
3180
                    } else {
3181
                        $v_content = gzread($v_tar, 512);
3182
                        gzwrite($v_temp_tar, $v_content, 512);
0 ignored issues
show
Bug introduced by
It seems like $v_temp_tar can also be of type false; however, parameter $zp of gzwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3182
                        gzwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_content, 512);
Loading history...
3183
                    }
3184
                }
3185
3186
                // ----- File name and properties are logged if listing mode or file is extracted
3187
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
3188
3189
                // ----- Add the array describing the file into the list
3190
                $p_list_detail[$v_nb]           = $v_header;
3191
                $p_list_detail[$v_nb]['status'] = ($v_found_file ? 'not_updated' : 'ok');
3192
3193
                // ----- Increment
3194
                ++$v_nb;
3195
            } // ----- Look for file that need to be updated
3196
            else {
3197
                // ----- Trace
3198
                TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
3199
3200
                // ----- Store the old file size
3201
                $v_old_size = $v_header['size'];
3202
3203
                // ----- Add the file
3204
                if (1 != ($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir))) {
3205
                    // ----- Close the tarfile
3206
                    if ('tar' === $p_tar_mode) {
3207
                        fclose($v_tar);
3208
                        fclose($v_temp_tar);
3209
                    } else {
3210
                        gzclose($v_tar);
3211
                        gzclose($v_temp_tar);
3212
                    }
3213
                    @unlink($p_temp_tarname);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $p_temp_tarname does not exist. Did you maybe mean $p_tarname?
Loading history...
3214
3215
                    // ----- Return status
3216
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3217
3218
                    return $v_result;
3219
                }
3220
3221
                // ----- Trace
3222
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip old file ' . $v_header['filename'] . '');
3223
3224
                // ----- Jump to next file
3225
                if ('tar' === $p_tar_mode) {
3226
                    fseek($v_tar, ftell($v_tar) + (ceil($v_old_size / 512) * 512));
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3226
                    fseek($v_tar, ftell(/** @scrutinizer ignore-type */ $v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
Bug introduced by
ftell($v_tar) + ceil($v_old_size / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of fseek(). ( Ignorable by Annotation )

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

3226
                    fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3226
                    fseek(/** @scrutinizer ignore-type */ $v_tar, ftell($v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
3227
                } else {
3228
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_old_size / 512) * 512));
0 ignored issues
show
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gztell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3228
                    gzseek($v_tar, gztell(/** @scrutinizer ignore-type */ $v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
Bug introduced by
It seems like $v_tar can also be of type false; however, parameter $zp of gzseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

3228
                    gzseek(/** @scrutinizer ignore-type */ $v_tar, gztell($v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
Bug introduced by
gztell($v_tar) + ceil($v_old_size / 512) * 512 of type double is incompatible with the type integer expected by parameter $offset of gzseek(). ( Ignorable by Annotation )

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

3228
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
3229
                }
3230
3231
                // ----- Add the array describing the file into the list
3232
                $p_list_detail[$v_nb]           = $v_header;
3233
                $p_list_detail[$v_nb]['status'] = 'updated';
3234
3235
                // ----- Increment
3236
                ++$v_nb;
3237
            }
3238
3239
            // ----- Look for end of file
3240
            if ('tar' === $p_tar_mode) {
3241
                $v_end_of_file = feof($v_tar);
3242
            } else {
3243
                $v_end_of_file = gzeof($v_tar);
3244
            }
3245
        }
3246
3247
        // ----- Look for files that does not exists in the archive and need to be added
3248
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3249
            // ----- Look if file not found in the archive
3250
            if (!$v_found_list[$i]) {
3251
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be added");
3252
3253
                // ----- Add the file
3254
                if (1 != ($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir))) {
3255
                    // ----- Close the tarfile
3256
                    if ('tar' === $p_tar_mode) {
3257
                        fclose($v_tar);
3258
                        fclose($v_temp_tar);
3259
                    } else {
3260
                        gzclose($v_tar);
3261
                        gzclose($v_temp_tar);
3262
                    }
3263
                    @unlink($p_temp_tarname);
3264
3265
                    // ----- Return status
3266
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3267
3268
                    return $v_result;
3269
                }
3270
3271
                // ----- Add the array describing the file into the list
3272
                $p_list_detail[$v_nb]           = $v_header;
3273
                $p_list_detail[$v_nb]['status'] = 'added';
3274
3275
                // ----- Increment
3276
                ++$v_nb;
3277
            } else {
3278
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' was already updated if needed");
3279
            }
3280
        }
3281
3282
        // ----- Write the last empty buffer
3283
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
3284
3285
        // ----- Close the tarfile
3286
        if ('tar' === $p_tar_mode) {
3287
            fclose($v_tar);
3288
            fclose($v_temp_tar);
3289
        } else {
3290
            gzclose($v_tar);
3291
            gzclose($v_temp_tar);
3292
        }
3293
3294
        // ----- Unlink tar file
3295
        if (!@unlink($p_tarname)) {
3296
            // ----- Error log
3297
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
3298
        }
3299
3300
        // ----- Rename tar file
3301
        if (!@rename($v_temp_tarname, $p_tarname)) {
3302
            // ----- Error log
3303
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
3304
3305
            // ----- Return
3306
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3307
3308
            return PclErrorCode();
3309
        }
3310
3311
        // ----- Return
3312
        TrFctEnd(__FILE__, __LINE__, $v_result);
3313
3314
        return $v_result;
3315
    }
3316
3317
    // --------------------------------------------------------------------------------
3318
3319
    // --------------------------------------------------------------------------------
3320
    // 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...
3321
    // Description :
3322
    // Parameters :
3323
    // Return Values :
3324
    // --------------------------------------------------------------------------------
3325
    /**
3326
     * @param $v_binary_data
3327
     * @param $v_header
3328
     *
3329
     * @return int
3330
     */
3331
    function PclTarHandleReadHeader($v_binary_data, &$v_header)
3332
    {
3333
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleReadHeader', '');
3334
        $v_result = 1;
3335
3336
        // ----- Read the 512 bytes header
3337
        /*
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...
3338
    if ($p_tar_mode == "tar")
3339
      $v_binary_data = fread($p_tar, 512);
3340
    else
3341
      $v_binary_data = gzread($p_tar, 512);
3342
    */
3343
3344
        // ----- Look for no more block
3345
        if ('' === $v_binary_data) {
3346
            $v_header['filename'] = '';
3347
            $v_header['status']   = 'empty';
3348
3349
            // ----- Return
3350
            TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3351
3352
            return $v_result;
3353
        }
3354
3355
        // ----- Look for invalid block size
3356
        if (512 != strlen($v_binary_data)) {
3357
            $v_header['filename'] = '';
3358
            $v_header['status']   = 'invalid_header';
3359
            TrFctMessage(__FILE__, __LINE__, 2, 'Invalid block size : ' . strlen($v_binary_data));
3360
3361
            // ----- Error log
3362
            PclErrorLog(-10, 'Invalid block size : ' . strlen($v_binary_data));
3363
3364
            // ----- Return
3365
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3366
3367
            return PclErrorCode();
3368
        }
3369
3370
        // ----- Calculate the checksum
3371
        $v_checksum = 0;
3372
        // ..... First part of the header
3373
        for ($i = 0; $i < 148; ++$i) {
3374
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3375
        }
3376
        // ..... Ignore the checksum value and replace it by ' ' (space)
3377
        for ($i = 148; $i < 156; ++$i) {
3378
            $v_checksum += ord(' ');
3379
        }
3380
        // ..... Last part of the header
3381
        for ($i = 156; $i < 512; ++$i) {
3382
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3383
        }
3384
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
3385
3386
        // ----- Extract the values
3387
        TrFctMessage(__FILE__, __LINE__, 2, "Header : '$v_binary_data'");
3388
        $v_data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $v_binary_data);
3389
3390
        // ----- Extract the checksum for check
3391
        $v_header['checksum'] = octdec(trim($v_data['checksum']));
3392
        TrFctMessage(__FILE__, __LINE__, 3, 'File checksum : ' . $v_header['checksum'] . '');
3393
        if ($v_header['checksum'] != $v_checksum) {
3394
            TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : $v_checksum calculated, " . $v_header['checksum'] . ' expected');
3395
3396
            $v_header['filename'] = '';
3397
            $v_header['status']   = 'invalid_header';
3398
3399
            // ----- Look for last block (empty block)
3400
            if ((256 == $v_checksum) && (0 == $v_header['checksum'])) {
0 ignored issues
show
introduced by
The condition 256 == $v_checksum is always false.
Loading history...
3401
                $v_header['status'] = 'empty';
3402
                // ----- Return
3403
                TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3404
3405
                return $v_result;
3406
            }
3407
3408
            // ----- Error log
3409
            PclErrorLog(-13, "Invalid checksum : $v_checksum calculated, " . $v_header['checksum'] . ' expected');
3410
3411
            // ----- Return
3412
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3413
3414
            return PclErrorCode();
3415
        }
3416
        TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ($v_checksum)");
3417
3418
        // ----- Extract the properties
3419
        $v_header['filename'] = trim($v_data['filename']);
3420
        TrFctMessage(__FILE__, __LINE__, 2, 'Name : ' . $v_header['filename'] . '');
3421
        $v_header['mode'] = octdec(trim($v_data['mode']));
3422
        TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . decoct($v_header['mode']) . "'");
3423
        $v_header['uid'] = octdec(trim($v_data['uid']));
3424
        TrFctMessage(__FILE__, __LINE__, 2, "Uid : '" . $v_header['uid'] . "'");
3425
        $v_header['gid'] = octdec(trim($v_data['gid']));
3426
        TrFctMessage(__FILE__, __LINE__, 2, "Gid : '" . $v_header['gid'] . "'");
3427
        $v_header['size'] = octdec(trim($v_data['size']));
3428
        TrFctMessage(__FILE__, __LINE__, 2, "Size : '" . $v_header['size'] . "'");
3429
        $v_header['mtime'] = octdec(trim($v_data['mtime']));
3430
        TrFctMessage(__FILE__, __LINE__, 2, 'Date : ' . date('l dS of F Y h:i:s A', $v_header['mtime']));
3431
        if ('5' == ($v_header['typeflag'] = $v_data['typeflag'])) {
3432
            $v_header['size'] = 0;
3433
            TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '" . $v_header['size'] . "'");
3434
        }
3435
        TrFctMessage(__FILE__, __LINE__, 2, 'File typeflag : ' . $v_header['typeflag'] . '');
3436
        /* ----- 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...
3437
    $v_header[link] = trim($v_data[link]);
3438
    TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]");
3439
    $v_header[magic] = trim($v_data[magic]);
3440
    TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]");
3441
    $v_header[version] = trim($v_data[version]);
3442
    TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]");
3443
    $v_header[uname] = trim($v_data[uname]);
3444
    TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]");
3445
    $v_header[gname] = trim($v_data[gname]);
3446
    TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]");
3447
    $v_header[devmajor] = trim($v_data[devmajor]);
3448
    TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]");
3449
    $v_header[devminor] = trim($v_data[devminor]);
3450
    TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]");
3451
    */
3452
3453
        // ----- Set the status field
3454
        $v_header['status'] = 'ok';
3455
3456
        // ----- Return
3457
        TrFctEnd(__FILE__, __LINE__, $v_result);
3458
3459
        return $v_result;
3460
    }
3461
3462
    // --------------------------------------------------------------------------------
3463
3464
    // --------------------------------------------------------------------------------
3465
    // 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...
3466
    // Description :
3467
    //   Check if a directory exists, if not it creates it and all the parents directory
3468
    //   which may be useful.
3469
    // Parameters :
3470
    //   $p_dir : Directory path to check (without / at the end).
3471
    // Return Values :
3472
    //    1 : OK
3473
    //   -1 : Unable to create directory
3474
    // --------------------------------------------------------------------------------
3475
    /**
3476
     * @param $p_dir
3477
     *
3478
     * @return int
3479
     */
3480
    function PclTarHandlerDirCheck($p_dir)
3481
    {
3482
        $v_result = 1;
3483
3484
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlerDirCheck', "$p_dir");
3485
3486
        // ----- Check the directory availability
3487
        if (('' == $p_dir) || is_dir($p_dir)) {
3488
            TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
0 ignored issues
show
Bug introduced by
'''.$p_dir.'' is a directory' of type string is incompatible with the type integer expected by parameter $p_return of TrFctEnd(). ( Ignorable by Annotation )

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

3488
            TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ "'$p_dir' is a directory");
Loading history...
3489
3490
            return 1;
3491
        }
3492
3493
        // ----- Look for file alone
3494
        /*
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...
3495
    if (!strstr("$p_dir", "/")) {
3496
      TrFctEnd(__FILE__, __LINE__,  "'$p_dir' is a file with no directory");
3497
3498
      return 1;
3499
    }
3500
    */
3501
3502
        // ----- Extract parent directory
3503
        $p_parent_dir = dirname($p_dir);
3504
        TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
3505
3506
        // ----- Just a check
3507
        if ($p_parent_dir != $p_dir) {
3508
            // ----- Look for parent directory
3509
            if ('' != $p_parent_dir) {
3510
                if (1 != ($v_result = PclTarHandlerDirCheck($p_parent_dir))) {
3511
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3512
3513
                    return $v_result;
3514
                }
3515
            }
3516
        }
3517
3518
        // ----- Create the directory
3519
        TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
3520
        if (!@mkdir($p_dir, 0777)) {
3521
            // ----- Error log
3522
            PclErrorLog(-8, "Unable to create directory '$p_dir'");
3523
3524
            // ----- Return
3525
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3526
3527
            return PclErrorCode();
3528
        }
3529
3530
        // ----- Return
3531
        TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
3532
3533
        return $v_result;
3534
    }
3535
3536
    // --------------------------------------------------------------------------------
3537
3538
    // --------------------------------------------------------------------------------
3539
    // 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...
3540
    // Description :
3541
    // Parameters :
3542
    // Return Values :
3543
    // --------------------------------------------------------------------------------
3544
    /**
3545
     * @param $p_tarname
3546
     *
3547
     * @return string
3548
     */
3549
    function PclTarHandleExtension($p_tarname)
3550
    {
3551
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtension', "tar=$p_tarname");
3552
3553
        // ----- Look for file extension
3554
        if (('.tar.gz' === substr($p_tarname, -7)) || ('.tgz' === substr($p_tarname, -4))) {
3555
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a gzip tar');
3556
            $v_tar_mode = 'tgz';
3557
        } else {
3558
            if ('.tar' === substr($p_tarname, -4)) {
3559
                TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a tar');
3560
                $v_tar_mode = 'tar';
3561
            } else {
3562
                // ----- Error log
3563
                PclErrorLog(-9, 'Invalid archive extension');
3564
3565
                TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3566
3567
                $v_tar_mode = '';
3568
            }
3569
        }
3570
3571
        // ----- Return
3572
        TrFctEnd(__FILE__, __LINE__, $v_tar_mode);
0 ignored issues
show
Bug introduced by
$v_tar_mode of type string is incompatible with the type integer expected by parameter $p_return of TrFctEnd(). ( Ignorable by Annotation )

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

3572
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $v_tar_mode);
Loading history...
3573
3574
        return $v_tar_mode;
3575
    }
3576
3577
    // --------------------------------------------------------------------------------
3578
3579
    // --------------------------------------------------------------------------------
3580
    // 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...
3581
    // Description :
3582
    // Parameters :
3583
    // Return Values :
3584
    // --------------------------------------------------------------------------------
3585
    /**
3586
     * @param $p_dir
3587
     *
3588
     * @return string
3589
     */
3590
    function PclTarHandlePathReduction($p_dir)
3591
    {
3592
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlePathReduction', "dir='$p_dir'");
3593
        $v_result = '';
3594
3595
        // ----- Look for not empty path
3596
        if ('' != $p_dir) {
3597
            // ----- Explode path by directory names
3598
            $v_list = explode('/', $p_dir);
3599
3600
            // ----- Study directories from last to first
3601
            for ($i = count($v_list) - 1; $i >= 0; --$i) {
3602
                // ----- Look for current path
3603
                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...
3604
                    // ----- Ignore this directory
3605
                    // Should be the first $i=0, but no check is done
3606
                } else {
3607
                    if ('..' === $v_list[$i]) {
3608
                        // ----- Ignore it and ignore the $i-1
3609
                        --$i;
3610
                    } else {
3611
                        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...
3612
                            // ----- Ignore only the double '//' in path,
3613
                            // but not the first and last '/'
3614
                        } else {
3615
                            $v_result = $v_list[$i] . ($i != (count($v_list) - 1) ? '/' . $v_result : '');
3616
                        }
3617
                    }
3618
                }
3619
            }
3620
        }
3621
3622
        // ----- Return
3623
        TrFctEnd(__FILE__, __LINE__, $v_result);
0 ignored issues
show
Bug introduced by
$v_result of type string is incompatible with the type integer expected by parameter $p_return of TrFctEnd(). ( Ignorable by Annotation )

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

3623
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $v_result);
Loading history...
3624
3625
        return $v_result;
3626
    }
3627
3628
    // --------------------------------------------------------------------------------
3629
3630
    // ----- End of double include look
3631
}
3632