Passed
Push — master ( 8d8e58...047d50 )
by Michael
02:21
created

PclTarHandleDelete()   F

Complexity

Conditions 28
Paths 1340

Size

Total Lines 217
Code Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 107
nc 1340
nop 4
dl 0
loc 217
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php namespace XoopsModules\Extgallery;
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 31 and the first side effect is on line 36.

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
use XoopsModules\Extgallery;
28
29
// ----- Look for double include
30
if (!defined('PCL_TAR')) {
31
    define('PCL_TAR', 1);
32
33
    // ----- Configuration variable
34
    // Theses values may be changed by the user of PclTar library
35
    if (!isset($g_pcltar_lib_dir)) {
36
        $g_pcltar_lib_dir = 'lib';
37
    }
38
39
    // ----- Error codes
40
    //   -1 : Unable to open file in binary write mode
41
    //   -2 : Unable to open file in binary read mode
42
    //   -3 : Invalid parameters
43
    //   -4 : File does not exist
44
    //   -5 : Filename is too long (max. 99)
45
    //   -6 : Not a valid tar file
46
    //   -7 : Invalid extracted file size
47
    //   -8 : Unable to create directory
48
    //   -9 : Invalid archive extension
49
    //  -10 : Invalid archive format
50
    //  -11 : Unable to delete file (unlink)
51
    //  -12 : Unable to rename file (rename)
52
    //  -13 : Invalid header checksum
53
54
    // --------------------------------------------------------------------------------
55
    // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
56
    // --------------------------------------------------------------------------------
57
58
    // ----- Global variables
59
    $g_pcltar_version = '1.3.1';
60
61
    // ----- Include other libraries
62
    // This library should be called by each script before the include of PhpZip
63
    // Library in order to limit the potential 'lib' directory path problem.
64
    if (!defined('PCLERROR_LIB')) {
65
        include $g_pcltar_lib_dir . '/pclerror.lib.php';
66
    }
67
    if (!defined('PCLTRACE_LIB')) {
68
        include $g_pcltar_lib_dir . '/pcltrace.lib.php';
69
    }
70
71
    // --------------------------------------------------------------------------------
72
    // 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...
73
    // Description :
74
    //   Creates a new archive with name $p_tarname containing the files and/or
75
    //   directories indicated in $p_list. If the tar filename extension is
76
    //   ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
77
    //   it will be a gzip compressed tar archive.
78
    //   If you want to use an other extension, you must indicate the mode in
79
    //   $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...
80
    //   $p_add_dir and $p_remove_dir give you the ability to store a path
81
    //   which is not the real path of the files.
82
    // Parameters :
83
    //   $p_tarname : Name of an existing tar file
84
    //   $p_filelist : An array containing file or directory names, or
85
    //                 a string containing one filename or directory name, or
86
    //                 a string containing a list of filenames and/or directory
87
    //                 names separated by spaces.
88
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
89
    //             if $p_mode is not specified, it will be determined by the extension.
90
    //   $p_add_dir : Path to add in the filename path archived
91
    //   $p_remove_dir : Path to remove in the filename path archived
92
    // Return Values :
93
    //   1 on success, or an error code (see table at the beginning).
94
    // --------------------------------------------------------------------------------
95
    /**
96
     * @param        $p_tarname
97
     * @param string $p_filelist
98
     * @param string $p_mode
99
     * @param string $p_add_dir
100
     * @param string $p_remove_dir
101
     *
102
     * @return int
103
     */
104
    function PclTarCreate($p_tarname, $p_filelist = '', $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
105
    {
106
        TrFctStart(__FILE__, __LINE__, 'PclTarCreate', "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
107
        $v_result = 1;
108
109
        // ----- Look for default mode
110
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
111
            // ----- Extract the tar format from the extension
112
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
113
                // ----- Return
114
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
115
116
                return PclErrorCode();
117
            }
118
119
            // ----- Trace
120
            TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
121
        }
122
123
        // ----- Look if the $p_filelist is really an array
124
        if (is_array($p_filelist)) {
0 ignored issues
show
introduced by
The condition is_array($p_filelist) is always false.
Loading history...
125
            // ----- Call the create fct
126
            $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
127
        } // ----- Look if the $p_filelist is a string
128
        elseif (is_string($p_filelist)) {
0 ignored issues
show
introduced by
The condition is_string($p_filelist) is always true.
Loading history...
129
            // ----- Create a list with the elements from the string
130
            $v_list = explode(' ', $p_filelist);
131
132
            // ----- Call the create fct
133
            $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
134
        } // ----- Invalid variable
135
        else {
136
            // ----- Error log
137
            PclErrorLog(-3, 'Invalid variable type p_filelist');
138
            $v_result = -3;
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
        elseif (is_string($p_filelist)) {
192
            // ----- Create a list with the elements from the string
193
            $v_list = explode(' ', $p_filelist);
194
195
            // ----- Call the add fct
196
            $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, '', '');
197
        } // ----- Invalid variable
198
        else {
199
            // ----- Error log
200
            PclErrorLog(-3, 'Invalid variable type p_filelist');
201
            $v_result = -3;
202
        }
203
204
        // ----- Cleaning
205
        unset($v_list_detail);
206
207
        // ----- Return
208
        TrFctEnd(__FILE__, __LINE__, $v_result);
209
210
        return $v_result;
211
    }
212
213
    // --------------------------------------------------------------------------------
214
215
    // --------------------------------------------------------------------------------
216
    // 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...
217
    // Description :
218
    //   Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
219
    //   The list can be an array of file/directory names or a string with names
220
    //   separated by one space.
221
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
222
    //   different from the real path of the file. This is usefull if you want to have PclTar
223
    //   running in any directory, and memorize relative path from an other directory.
224
    //   If $p_mode is not set it will be automatically computed from the $p_tarname
225
    //   extension (.tar, .tar.gz or .tgz).
226
    // Parameters :
227
    //   $p_tarname : Name of an existing tar file
228
    //   $p_filelist : An array containing file or directory names, or
229
    //                 a string containing one filename or directory name, or
230
    //                 a string containing a list of filenames and/or directory
231
    //                 names separated by spaces.
232
    //   $p_add_dir : Path to add in the filename path archived
233
    //   $p_remove_dir : Path to remove in the filename path archived
234
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
235
    // Return Values :
236
    //   1 on success,
237
    //   Or an error code (see list on top).
238
    // --------------------------------------------------------------------------------
239
    /**
240
     * @param        $p_tarname
241
     * @param        $p_filelist
242
     * @param string $p_add_dir
243
     * @param string $p_remove_dir
244
     * @param string $p_mode
245
     *
246
     * @return array|int
247
     */
248
    function PclTarAddList($p_tarname, $p_filelist, $p_add_dir = '', $p_remove_dir = '', $p_mode = '')
249
    {
250
        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");
251
        $v_result      = 1;
252
        $p_list_detail = [];
253
254
        // ----- Extract the tar format from the extension
255
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
256
            if ('' === ($p_mode = PclTarHandleExtension($p_tarname))) {
257
                // ----- Return
258
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
259
260
                return PclErrorCode();
261
            }
262
        }
263
264
        // ----- Look if the $p_filelist is really an array
265
        if (is_array($p_filelist)) {
266
            // ----- Call the add fct
267
            $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
268
        } // ----- Look if the $p_filelist is a string
269
        elseif (is_string($p_filelist)) {
270
            // ----- Create a list with the elements from the string
271
            $v_list = explode(' ', $p_filelist);
272
273
            // ----- Call the add fct
274
            $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
275
        } // ----- Invalid variable
276
        else {
277
            // ----- Error log
278
            PclErrorLog(-3, 'Invalid variable type p_filelist');
279
            $v_result = -3;
280
        }
281
282
        // ----- Return
283
        if (1 != $v_result) {
284
            TrFctEnd(__FILE__, __LINE__, 0);
285
286
            return 0;
287
        }
288
        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 XoopsModules\Extgallery\TrFctEnd(). ( Ignorable by Annotation )

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

288
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $p_list_detail);
Loading history...
289
290
        return $p_list_detail;
291
    }
292
293
    // --------------------------------------------------------------------------------
294
295
    // --------------------------------------------------------------------------------
296
    // 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...
297
    // Description :
298
    //   Gives the list of all the files present in the tar archive $p_tarname.
299
    //   The list is the function result, it will be 0 on error.
300
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
301
    //   function will determine the type of the archive.
302
    // Parameters :
303
    //   $p_tarname : Name of an existing tar file
304
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
305
    // Return Values :
306
    //  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...
307
    //  or
308
    //  An array containing file properties. Each file properties is an array of
309
    //  properties.
310
    //  The properties (array field names) are :
311
    //    filename, size, mode, uid, gid, mtime, typeflag, status
312
    //  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...
313
    //            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...
314
    //              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...
315
    // --------------------------------------------------------------------------------
316
    /**
317
     * @param        $p_tarname
318
     * @param string $p_mode
319
     *
320
     * @return array|int
321
     */
322
    function PclTarList($p_tarname, $p_mode = '')
323
    {
324
        TrFctStart(__FILE__, __LINE__, 'PclTarList', "tar=$p_tarname, mode='$p_mode'");
325
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
326
327
        // ----- Extract the tar format from the extension
328
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
329
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
330
                // ----- Return
331
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
332
333
                return 0;
334
            }
335
        }
336
337
        // ----- Call the extracting fct
338
        $p_list = [];
339
        if (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'list', '', $p_mode, ''))) {
340
            unset($p_list);
341
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
342
343
            return 0;
344
        }
345
346
        // ----- Return
347
        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 XoopsModules\Extgallery\TrFctEnd(). ( Ignorable by Annotation )

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

347
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $p_list);
Loading history...
348
349
        return $p_list;
350
    }
351
352
    // --------------------------------------------------------------------------------
353
354
    // --------------------------------------------------------------------------------
355
    // 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...
356
    // Description :
357
    //   Extract all the files present in the archive $p_tarname, in the directory
358
    //   $p_path. The relative path of the archived files are keep and become
359
    //   relative to $p_path.
360
    //   If a file with the same name already exists it will be replaced.
361
    //   If the path to the file does not exist, it will be created.
362
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
363
    //   function will determine the type of the archive.
364
    // Parameters :
365
    //   $p_tarname : Name of an existing tar file.
366
    //   $p_path : Path where the files will be extracted. The files will use
367
    //             their memorized path from $p_path.
368
    //             If $p_path is "", files will be extracted in "./".
369
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
370
    //                    extracted files. If the path does not match the file path,
371
    //                    the file is extracted with its memorized path.
372
    //                    $p_path and $p_remove_path are commulative.
373
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
374
    // Return Values :
375
    //   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...
376
    // --------------------------------------------------------------------------------
377
    /**
378
     * @param        $p_tarname
379
     * @param string $p_path
380
     * @param string $p_remove_path
381
     * @param string $p_mode
382
     *
383
     * @return int
384
     */
385
    function PclTarExtract($p_tarname, $p_path = './', $p_remove_path = '', $p_mode = '')
386
    {
387
        TrFctStart(__FILE__, __LINE__, 'PclTarExtract', "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
388
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
389
390
        // ----- Extract the tar format from the extension
391
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
392
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
393
                // ----- Return
394
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
395
396
                return 0;
397
            }
398
        }
399
400
        // ----- Call the extracting fct
401
        if (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'complete', $p_path, $p_mode, $p_remove_path))) {
402
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
403
404
            return 0;
405
        }
406
407
        // ----- Return
408
        TrFctEnd(__FILE__, __LINE__, $p_list);
409
410
        return $p_list;
411
    }
412
413
    // --------------------------------------------------------------------------------
414
415
    // --------------------------------------------------------------------------------
416
    // 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...
417
    // Description :
418
    //   Extract the files present in the archive $p_tarname and specified in
419
    //   $p_filelist, in the directory
420
    //   $p_path. The relative path of the archived files are keep and become
421
    //   relative to $p_path.
422
    //   If a directory is specified in the list, all the files from this directory
423
    //   will be extracted.
424
    //   If a file with the same name already exists it will be replaced.
425
    //   If the path to the file does not exist, it will be created.
426
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
427
    //   function will determine the type of the archive.
428
    // Parameters :
429
    //   $p_tarname : Name of an existing tar file
430
    //   $p_filelist : An array containing file or directory names, or
431
    //                 a string containing one filename or directory name, or
432
    //                 a string containing a list of filenames and/or directory
433
    //                 names separated by spaces.
434
    //   $p_path : Path where the files will be extracted. The files will use
435
    //             their memorized path from $p_path.
436
    //             If $p_path is "", files will be extracted in "./".
437
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
438
    //                    extracted files. If the path does not match the file path,
439
    //                    the file is extracted with its memorized path.
440
    //                    $p_path and $p_remove_path are commulative.
441
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
442
    // Return Values :
443
    //   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...
444
    // --------------------------------------------------------------------------------
445
    /**
446
     * @param        $p_tarname
447
     * @param        $p_filelist
448
     * @param string $p_path
449
     * @param string $p_remove_path
450
     * @param string $p_mode
451
     *
452
     * @return int
453
     */
454
    function PclTarExtractList($p_tarname, $p_filelist, $p_path = './', $p_remove_path = '', $p_mode = '')
455
    {
456
        TrFctStart(__FILE__, __LINE__, 'PclTarExtractList', "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
457
        $v_result = 1;
458
459
        // ----- Extract the tar format from the extension
460
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
461
            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...
462
                // ----- Return
463
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
464
465
                return 0;
466
            }
467
        }
468
469
        // ----- Look if the $p_filelist is really an array
470
        if (is_array($p_filelist)) {
471
            // ----- Call the extracting fct
472
            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...
473
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
474
475
                return 0;
476
            }
477
        } // ----- Look if the $p_filelist is a string
478
        elseif (is_string($p_filelist)) {
479
            // ----- Create a list with the elements from the string
480
            $v_list = explode(' ', $p_filelist);
481
482
            // ----- Call the extracting fct
483
            if (1 != ($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path))) {
484
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
485
486
                return 0;
487
            }
488
        } // ----- Invalid variable
489
        else {
490
            // ----- Error log
491
            PclErrorLog(-3, 'Invalid variable type p_filelist');
492
493
            // ----- Return
494
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
495
496
            return 0;
497
        }
498
499
        // ----- Return
500
        TrFctEnd(__FILE__, __LINE__, $p_list);
501
502
        return $p_list;
503
    }
504
505
    // --------------------------------------------------------------------------------
506
507
    // --------------------------------------------------------------------------------
508
    // 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...
509
    // Description :
510
    //   Extract the files present in the archive $p_tarname and specified at
511
    //   the indexes in $p_index, in the directory
512
    //   $p_path. The relative path of the archived files are keep and become
513
    //   relative to $p_path.
514
    //   If a directory is specified in the list, the directory only is created. All
515
    //   the file stored in this archive for this directory
516
    //   are not extracted.
517
    //   If a file with the same name already exists it will be replaced.
518
    //   If the path to the file does not exist, it will be created.
519
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
520
    //   function will determine the type of the archive.
521
    // Parameters :
522
    //   $p_tarname : Name of an existing tar file
523
    //   $p_index : A single index (integer) or a string of indexes of files to
524
    //              extract. The form of the string is "0,4-6,8-12" with only numbers
525
    //              and '-' for range or ',' to separate ranges. No spaces or ';'
526
    //              are allowed.
527
    //   $p_path : Path where the files will be extracted. The files will use
528
    //             their memorized path from $p_path.
529
    //             If $p_path is "", files will be extracted in "./".
530
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
531
    //                    extracted files. If the path does not match the file path,
532
    //                    the file is extracted with its memorized path.
533
    //                    $p_path and $p_remove_path are commulative.
534
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
535
    // Return Values :
536
    //   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...
537
    // --------------------------------------------------------------------------------
538
    /**
539
     * @param        $p_tarname
540
     * @param        $p_index
541
     * @param string $p_path
542
     * @param string $p_remove_path
543
     * @param string $p_mode
544
     *
545
     * @return int
546
     */
547
    function PclTarExtractIndex($p_tarname, $p_index, $p_path = './', $p_remove_path = '', $p_mode = '')
548
    {
549
        TrFctStart(__FILE__, __LINE__, 'PclTarExtractIndex', "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
550
        $v_result = 1;
551
552
        // ----- Extract the tar format from the extension
553
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
554
            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...
555
                // ----- Return
556
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
557
558
                return 0;
559
            }
560
        }
561
562
        // ----- Look if the $p_index is really an integer
563
        if (is_int($p_index)) {
564
            // ----- Call the extracting fct
565
            if (1 != ($v_result = PclTarHandleExtractByIndexList($p_tarname, (string)$p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $v_tar_mode seems to be never defined.
Loading history...
566
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
567
568
                return 0;
569
            }
570
        } // ----- Look if the $p_filelist is a string
571
        elseif (is_string($p_index)) {
572
            // ----- Call the extracting fct
573
            if (1 != ($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode))) {
574
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
575
576
                return 0;
577
            }
578
        } // ----- Invalid variable
579
        else {
580
            // ----- Error log
581
            PclErrorLog(-3, "Invalid variable type $p_index");
582
583
            // ----- Return
584
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
585
586
            return 0;
587
        }
588
589
        // ----- Return
590
        TrFctEnd(__FILE__, __LINE__, $p_list);
591
592
        return $p_list;
593
    }
594
595
    // --------------------------------------------------------------------------------
596
597
    // --------------------------------------------------------------------------------
598
    // 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...
599
    // Description :
600
    //   This function deletes from the archive $p_tarname the files which are listed
601
    //   in $p_filelist. $p_filelist can be a string with file names separated by
602
    //   spaces, or an array containing the file names.
603
    // Parameters :
604
    //   $p_tarname : Name of an existing tar file
605
    //   $p_filelist : An array or a string containing file names to remove from the
606
    //                 archive.
607
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
608
    // Return Values :
609
    //   List of the files which are kept in the archive (same format as PclTarList())
610
    // --------------------------------------------------------------------------------
611
    /**
612
     * @param        $p_tarname
613
     * @param        $p_filelist
614
     * @param string $p_mode
615
     *
616
     * @return int
617
     */
618
    function PclTarDelete($p_tarname, $p_filelist, $p_mode = '')
619
    {
620
        TrFctStart(__FILE__, __LINE__, 'PclTarDelete', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
621
        $v_result = 1;
622
623
        // ----- Extract the tar format from the extension
624
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
625
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
626
                // ----- Return
627
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
628
629
                return 0;
630
            }
631
        }
632
633
        // ----- Look if the $p_filelist is really an array
634
        if (is_array($p_filelist)) {
635
            // ----- Call the extracting fct
636
            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...
637
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
638
639
                return 0;
640
            }
641
        } // ----- Look if the $p_filelist is a string
642
        elseif (is_string($p_filelist)) {
643
            // ----- Create a list with the elements from the string
644
            $v_list = explode(' ', $p_filelist);
645
646
            // ----- Call the extracting fct
647
            if (1 != ($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode))) {
648
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
649
650
                return 0;
651
            }
652
        } // ----- Invalid variable
653
        else {
654
            // ----- Error log
655
            PclErrorLog(-3, 'Invalid variable type p_filelist');
656
657
            // ----- Return
658
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
659
660
            return 0;
661
        }
662
663
        // ----- Return
664
        TrFctEnd(__FILE__, __LINE__, $p_list);
665
666
        return $p_list;
667
    }
668
669
    // --------------------------------------------------------------------------------
670
671
    // --------------------------------------------------------------------------------
672
    // 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...
673
    // Description :
674
    //   This function updates the files in $p_filelist which are already in the
675
    //   $p_tarname archive with an older last modified date. If the file does not
676
    //   exist, it is added at the end of the archive.
677
    // Parameters :
678
    //   $p_tarname : Name of an existing tar file
679
    //   $p_filelist : An array or a string containing file names to update from the
680
    //                 archive.
681
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
682
    // Return Values :
683
    //   List of the files contained in the archive. The field status contains
684
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
685
    // --------------------------------------------------------------------------------
686
    /**
687
     * @param        $p_tarname
688
     * @param        $p_filelist
689
     * @param string $p_mode
690
     * @param string $p_add_dir
691
     * @param string $p_remove_dir
692
     *
693
     * @return int
694
     */
695
    function PclTarUpdate($p_tarname, $p_filelist, $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
696
    {
697
        TrFctStart(__FILE__, __LINE__, 'PclTarUpdate', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
698
        $v_result = 1;
699
700
        // ----- Extract the tar format from the extension
701
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
702
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
703
                // ----- Return
704
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
705
706
                return 0;
707
            }
708
        }
709
710
        // ----- Look if the $p_filelist is really an array
711
        if (is_array($p_filelist)) {
712
            // ----- Call the extracting fct
713
            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...
714
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
715
716
                return 0;
717
            }
718
        } // ----- Look if the $p_filelist is a string
719
        elseif (is_string($p_filelist)) {
720
            // ----- Create a list with the elements from the string
721
            $v_list = explode(' ', $p_filelist);
722
723
            // ----- Call the extracting fct
724
            if (1 != ($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir))) {
725
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
726
727
                return 0;
728
            }
729
        } // ----- Invalid variable
730
        else {
731
            // ----- Error log
732
            PclErrorLog(-3, 'Invalid variable type p_filelist');
733
734
            // ----- Return
735
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
736
737
            return 0;
738
        }
739
740
        // ----- Return
741
        TrFctEnd(__FILE__, __LINE__, $p_list);
742
743
        return $p_list;
744
    }
745
746
    // --------------------------------------------------------------------------------
747
748
    // --------------------------------------------------------------------------------
749
    // 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...
750
    // Description :
751
    //   This function add the content of $p_tarname_add at the end of $p_tarname.
752
    // Parameters :
753
    //   $p_tarname : Name of an existing tar file
754
    //   $p_tarname_add : Name of an existing tar file taht will be added at the end
755
    //                    of $p_tarname.
756
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
757
    //   $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
758
    //                 extension
759
    // Return Values :
760
    //   List of the files contained in the archive. The field status contains
761
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
762
    // --------------------------------------------------------------------------------
763
    /**
764
     * @param        $p_tarname
765
     * @param        $p_tarname_add
766
     * @param string $p_mode
767
     * @param string $p_mode_add
768
     *
769
     * @return int
770
     */
771
    function PclTarMerge($p_tarname, $p_tarname_add, $p_mode = '', $p_mode_add = '')
772
    {
773
        TrFctStart(__FILE__, __LINE__, 'PclTarMerge', "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
774
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_result is dead and can be removed.
Loading history...
775
776
        // ----- Check the parameters
777
        if (('' == $p_tarname) || ('' == $p_tarname_add)) {
778
            // ----- Error log
779
            PclErrorLog(-3, 'Invalid empty archive name');
780
781
            // ----- Return
782
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
783
784
            return PclErrorCode();
785
        }
786
787
        // ----- Extract the tar format from the extension
788
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
789
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
790
                // ----- Return
791
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
792
793
                return 0;
794
            }
795
        }
796
        if (('' === $p_mode_add) || (('tar' !== $p_mode_add) && ('tgz' !== $p_mode_add))) {
797
            if ('' == ($p_mode_add = PclTarHandleExtension($p_tarname_add))) {
798
                // ----- Return
799
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
800
801
                return 0;
802
            }
803
        }
804
805
        // ----- Clear filecache
806
        clearstatcache();
807
808
        // ----- Check the file size
809
        if ((!is_file($p_tarname)) || ((0 != (($v_size = filesize($p_tarname)) % 512)) && ('tar' === $p_mode))) {
810
            // ----- Error log
811
            if (!is_file($p_tarname)) {
812
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
813
            } else {
814
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size " . filesize($p_tarname) . '(not a 512 block multiple)');
815
            }
816
817
            // ----- Return
818
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
819
820
            return PclErrorCode();
821
        }
822
        if ((!is_file($p_tarname_add))
823
            || ((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...
824
                && ('tar' === $p_mode_add))) {
825
            // ----- Error log
826
            if (!is_file($p_tarname_add)) {
827
                PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
828
            } else {
829
                PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size " . filesize($p_tarname_add) . '(not a 512 block multiple)');
830
            }
831
832
            // ----- Return
833
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
834
835
            return PclErrorCode();
836
        }
837
838
        // ----- Look for compressed archive
839
        if ('tgz' === $p_mode) {
840
            // ----- Open the file in read mode
841
            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...
842
                // ----- Error log
843
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
844
845
                // ----- Return
846
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
847
848
                return PclErrorCode();
849
            }
850
851
            // ----- Open a temporary file in write mode
852
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
853
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
854
            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...
855
                // ----- Close tar file
856
                gzclose($p_tar);
857
858
                // ----- Error log
859
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
860
861
                // ----- Return
862
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
863
864
                return PclErrorCode();
865
            }
866
867
            // ----- Read the first 512 bytes block
868
            $v_buffer = gzread($p_tar, 512);
869
870
            // ----- Read the following blocks but not the last one
871
            if (!gzeof($p_tar)) {
872
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
873
                $i = 1;
874
875
                // ----- Read new 512 block and write the already read
876
                do {
877
                    // ----- Write the already read block
878
                    $v_binary_data = pack('a512', $v_buffer);
879
                    gzputs($v_temp_tar, $v_binary_data);
880
881
                    ++$i;
882
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
883
884
                    // ----- Read next block
885
                    $v_buffer = gzread($p_tar, 512);
886
                } while (!gzeof($p_tar));
887
888
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
889
            }
890
        } // ----- Look for uncompressed tar file
891
        elseif ('tar' === $p_mode) {
892
            // ----- Open the tar file
893
            if (0 == ($p_tar = fopen($p_tarname, 'r+b'))) {
894
                // ----- Error log
895
                PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
896
897
                // ----- Return
898
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
899
900
                return PclErrorCode();
901
            }
902
903
            // ----- Go to the beginning of last block
904
            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

904
            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

904
            TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell(/** @scrutinizer ignore-type */ $p_tar)));
Loading history...
905
            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

905
            fseek(/** @scrutinizer ignore-type */ $p_tar, $v_size - 512);
Loading history...
906
            TrFctMessage(__FILE__, __LINE__, 4, 'Position after :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
907
        } // ----- Look for unknown type
908
        else {
909
            // ----- Error log
910
            PclErrorLog(-3, "Invalid tar mode $p_mode");
911
912
            // ----- Return
913
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
914
915
            return PclErrorCode();
916
        }
917
918
        // ----- Look for type of archive to add
919
        if ('tgz' === $p_mode_add) {
920
            TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
921
922
            // ----- Open the file in read mode
923
            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...
924
                // ----- Error log
925
                PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
926
927
                // ----- Return
928
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
929
930
                return PclErrorCode();
931
            }
932
933
            // ----- Read the first 512 bytes block
934
            $v_buffer = gzread($p_tar_add, 512);
935
936
            // ----- Read the following blocks but not the last one
937
            if (!gzeof($p_tar_add)) {
938
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
939
                $i = 1;
940
941
                // ----- Read new 512 block and write the already read
942
                do {
943
                    // ----- Write the already read block
944
                    $v_binary_data = pack('a512', $v_buffer);
945
                    if ('tar' === $p_mode) {
946
                        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

946
                        fwrite(/** @scrutinizer ignore-type */ $p_tar, $v_binary_data);
Loading history...
947
                    } else {
948
                        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...
949
                    }
950
951
                    ++$i;
952
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
953
954
                    // ----- Read next block
955
                    $v_buffer = gzread($p_tar_add, 512);
956
                } while (!gzeof($p_tar_add));
957
958
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
959
            }
960
961
            // ----- Close the files
962
            gzclose($p_tar_add);
963
        } // ----- Look for uncompressed tar file
964
        elseif ('tar' === $p_mode) {
965
            // ----- Open the file in read mode
966
            if (0 == ($p_tar_add = @fopen($p_tarname_add, 'rb'))) {
967
                // ----- Error log
968
                PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
969
970
                // ----- Return
971
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
972
973
                return PclErrorCode();
974
            }
975
976
            // ----- Read the first 512 bytes block
977
            $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

977
            $v_buffer = fread(/** @scrutinizer ignore-type */ $p_tar_add, 512);
Loading history...
978
979
            // ----- Read the following blocks but not the last one
980
            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

980
            if (!feof(/** @scrutinizer ignore-type */ $p_tar_add)) {
Loading history...
981
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
982
                $i = 1;
983
984
                // ----- Read new 512 block and write the already read
985
                do {
986
                    // ----- Write the already read block
987
                    $v_binary_data = pack('a512', (string)$v_buffer);
988
                    if ('tar' === $p_mode) {
989
                        fwrite($p_tar, $v_binary_data);
990
                    } else {
991
                        gzputs($v_temp_tar, $v_binary_data);
992
                    }
993
994
                    ++$i;
995
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
996
997
                    // ----- Read next block
998
                    $v_buffer = fread($p_tar_add, 512);
999
                } while (!feof($p_tar_add));
1000
1001
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1002
            }
1003
1004
            // ----- Close the files
1005
            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

1005
            fclose(/** @scrutinizer ignore-type */ $p_tar_add);
Loading history...
1006
        }
1007
1008
        // ----- Call the footer of the tar archive
1009
        $v_result = PclTarHandleFooter($p_tar, $p_mode);
1010
1011
        // ----- Look for closing compressed archive
1012
        if ('tgz' === $p_mode) {
1013
            // ----- Close the files
1014
            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

1014
            gzclose(/** @scrutinizer ignore-type */ $p_tar);
Loading history...
1015
            gzclose($v_temp_tar);
1016
1017
            // ----- Unlink tar file
1018
            if (!@unlink($p_tarname)) {
1019
                // ----- Error log
1020
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1021
            }
1022
1023
            // ----- Rename tar file
1024
            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...
1025
                // ----- Error log
1026
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
1027
1028
                // ----- Return
1029
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1030
1031
                return PclErrorCode();
1032
            }
1033
1034
            // ----- Return
1035
            TrFctEnd(__FILE__, __LINE__, $v_result);
1036
1037
            return $v_result;
1038
        } // ----- Look for closing uncompressed tar file
1039
        elseif ('tar' === $p_mode) {
1040
            // ----- Close the tarfile
1041
            fclose($p_tar);
1042
        }
1043
1044
        // ----- Return
1045
        TrFctEnd(__FILE__, __LINE__, $v_result);
1046
1047
        return $v_result;
1048
    }
1049
1050
    // --------------------------------------------------------------------------------
1051
1052
    // --------------------------------------------------------------------------------
1053
    // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1054
    // *****                                                        *****
1055
    // *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1056
    // --------------------------------------------------------------------------------
1057
1058
    // --------------------------------------------------------------------------------
1059
    // 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...
1060
    // Description :
1061
    // Parameters :
1062
    //   $p_tarname : Name of the tar file
1063
    //   $p_list : An array containing the file or directory names to add in the tar
1064
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1065
    // Return Values :
1066
    // --------------------------------------------------------------------------------
1067
    /**
1068
     * @param        $p_tarname
1069
     * @param        $p_list
1070
     * @param        $p_mode
1071
     * @param string $p_add_dir
1072
     * @param string $p_remove_dir
1073
     *
1074
     * @return int
1075
     */
1076
    function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir = '', $p_remove_dir = '')
1077
    {
1078
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleCreate', "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1079
        $v_result      = 1;
1080
        $v_list_detail = [];
1081
1082
        // ----- Check the parameters
1083
        if (('' === $p_tarname) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
1084
            // ----- Error log
1085
            if ('' == $p_tarname) {
1086
                PclErrorLog(-3, 'Invalid empty archive name');
1087
            } else {
1088
                PclErrorLog(-3, "Unknown mode '$p_mode'");
1089
            }
1090
1091
            // ----- Return
1092
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1093
1094
            return PclErrorCode();
1095
        }
1096
1097
        // ----- Look for tar file
1098
        if ('tar' === $p_mode) {
1099
            // ----- Open the tar file
1100
            if (0 == ($p_tar = fopen($p_tarname, 'wb'))) {
1101
                // ----- Error log
1102
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1103
1104
                // ----- Return
1105
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1106
1107
                return PclErrorCode();
1108
            }
1109
1110
            // ----- Call the adding fct inside the tar
1111
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir))) {
1112
                // ----- Call the footer of the tar archive
1113
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1114
            }
1115
1116
            // ----- Close the tarfile
1117
            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

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

1299
            TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell(/** @scrutinizer ignore-type */ $p_tar)));
Loading history...
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

1299
            TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell(/** @scrutinizer ignore-type */ $p_tar) : gztell($p_tar)));
Loading history...
1300
            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

1300
            fseek(/** @scrutinizer ignore-type */ $p_tar, $v_size - 512);
Loading history...
1301
            TrFctMessage(__FILE__, __LINE__, 4, 'Position after :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1302
1303
            // ----- Call the adding fct inside the tar
1304
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir))) {
1305
                // ----- Call the footer of the tar archive
1306
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1307
            }
1308
1309
            // ----- Close the tarfile
1310
            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

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

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

1599
            while ('' != ($v_buffer = fread(/** @scrutinizer ignore-type */ $v_file, 512))) {
Loading history...
1600
                $v_binary_data = pack('a512', (string)$v_buffer);
1601
                if ('tar' === $p_mode) {
1602
                    fwrite($p_tar, $v_binary_data);
1603
                } else {
1604
                    gzputs($p_tar, $v_binary_data);
1605
                }
1606
                ++$i;
1607
            }
1608
            TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
1609
1610
            // ----- Close the file
1611
            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

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

1942
        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

1942
        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...
1943
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
1944
1945
            // ----- Clear cache of file infos
1946
            clearstatcache();
1947
1948
            // ----- Reset extract tag
1949
            $v_extract_file       = false;
1950
            $v_extraction_stopped = 0;
1951
1952
            // ----- Read the 512 bytes header
1953
            if ('tar' === $p_tar_mode) {
1954
                $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

1954
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
1955
            } else {
1956
                $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

1956
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
1957
            }
1958
1959
            // ----- Read the header properties
1960
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
1961
                // ----- Close the archive file
1962
                if ('tar' === $p_tar_mode) {
1963
                    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

1963
                    fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
1964
                } else {
1965
                    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

1965
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
1966
                }
1967
1968
                // ----- Return
1969
                TrFctEnd(__FILE__, __LINE__, $v_result);
1970
1971
                return $v_result;
1972
            }
1973
1974
            // ----- Look for empty blocks to skip
1975
            if ('' == $v_header['filename']) {
1976
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
1977
                continue;
1978
            }
1979
1980
            TrFctMessage(__FILE__, __LINE__, 2, "Found file '" . $v_header['filename'] . "', size '" . $v_header['size'] . "'");
1981
1982
            // ----- Look for partial extract
1983
            if ((!$v_extract_all) && is_array($p_file_list)) {
1984
                TrFctMessage(__FILE__, __LINE__, 2, 'Look if the file ' . $v_header['filename'] . ' need to be extracted');
1985
1986
                // ----- By default no unzip if the file is not found
1987
                $v_extract_file = false;
1988
1989
                // ----- Look into the file list
1990
                for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
1991
                    TrFctMessage(__FILE__, __LINE__, 2, 'Compare archived file ' . $v_header['filename'] . " from asked list file '" . $p_file_list[$i] . "'");
1992
1993
                    // ----- Look if it is a directory
1994
                    if ('/' === substr($p_file_list[$i], -1)) {
1995
                        TrFctMessage(__FILE__, __LINE__, 3, 'Compare file ' . $v_header['filename'] . " with directory '$p_file_list[$i]'");
1996
1997
                        // ----- Look if the directory is in the filename path
1998
                        if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
1999
                            && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i])) {
2000
                            // ----- The file is in the directory, so extract it
2001
                            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . " is in directory '$p_file_list[$i]' : extract it");
2002
                            $v_extract_file = true;
2003
2004
                            // ----- End of loop
2005
                            break;
2006
                        }
2007
                    } // ----- It is a file, so compare the file names
2008
                    elseif ($p_file_list[$i] == $v_header['filename']) {
2009
                        // ----- File found
2010
                        TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' should be extracted');
2011
                        $v_extract_file = true;
2012
2013
                        // ----- End of loop
2014
                        break;
2015
                    }
2016
                }
2017
2018
                // ----- Trace
2019
                if (!$v_extract_file) {
2020
                    TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' should not be extracted');
2021
                }
2022
            } else {
2023
                // ----- All files need to be extracted
2024
                $v_extract_file = true;
2025
            }
2026
2027
            // ----- Look if this file need to be extracted
2028
            if ($v_extract_file && (!$v_listing)) {
2029
                // ----- Look for path to remove
2030
                if (('' != $p_remove_path)
2031
                    && (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path)) {
2032
                    TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2033
                    // ----- Remove the path
2034
                    $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2035
                    TrFctMessage(__FILE__, __LINE__, 3, 'Reslting file is ' . $v_header['filename'] . '');
2036
                }
2037
2038
                // ----- Add the path to the file
2039
                if (('./' !== $p_path) && ('/' !== $p_path)) {
2040
                    // ----- Look for the path end '/'
2041
                    while ('/' === substr($p_path, -1)) {
2042
                        TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2043
                        $p_path = substr($p_path, 0, -1);
2044
                        TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2045
                    }
2046
2047
                    // ----- Add the path
2048
                    if ('/' === substr($v_header['filename'], 0, 1)) {
2049
                        $v_header['filename'] = $p_path . $v_header['filename'];
2050
                    } else {
2051
                        $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2052
                    }
2053
                }
2054
2055
                // ----- Trace
2056
                TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2057
2058
                // ----- Check that the file does not exists
2059
                if (file_exists($v_header['filename'])) {
2060
                    TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2061
2062
                    // ----- Look if file is a directory
2063
                    if (is_dir($v_header['filename'])) {
2064
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2065
2066
                        // ----- Change the file status
2067
                        $v_header['status'] = 'already_a_directory';
2068
2069
                        // ----- Skip the extract
2070
                        $v_extraction_stopped = 1;
2071
                        $v_extract_file       = 0;
2072
                    } // ----- Look if file is write protected
2073
                    elseif (!is_writable($v_header['filename'])) {
2074
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2075
2076
                        // ----- Change the file status
2077
                        $v_header['status'] = 'write_protected';
2078
2079
                        // ----- Skip the extract
2080
                        $v_extraction_stopped = 1;
2081
                        $v_extract_file       = 0;
2082
                    }
2083
                    // ----- Look if the extracted file is older
2084
                    /*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...
2085
            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']).")");
2086
2087
            // ----- Change the file status
2088
            $v_header['status'] = "newer_exist";
2089
2090
            // ----- Skip the extract
2091
            $v_extraction_stopped = 1;
2092
            $v_extract_file = 0;
2093
          }*/
2094
                } // ----- Check the directory availability and create it if necessary
2095
                else {
2096
                    if ('5' == $v_header['typeflag']) {
2097
                        $v_dir_to_check = $v_header['filename'];
2098
                    } elseif (false === strpos($v_header['filename'], '/')) {
2099
                        $v_dir_to_check = '';
2100
                    } else {
2101
                        $v_dir_to_check = dirname($v_header['filename']);
2102
                    }
2103
2104
                    if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2105
                        TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2106
2107
                        // ----- Change the file status
2108
                        $v_header['status'] = 'path_creation_fail';
2109
2110
                        // ----- Skip the extract
2111
                        $v_extraction_stopped = 1;
2112
                        $v_extract_file       = 0;
2113
                    }
2114
                }
2115
2116
                // ----- Do the extraction
2117
                if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2118
                    // ----- Open the destination file in write mode
2119
                    if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
2120
                        TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2121
2122
                        // ----- Change the file status
2123
                        $v_header['status'] = 'write_error';
2124
2125
                        // ----- Jump to next file
2126
                        TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2127
                        if ('tar' === $p_tar_mode) {
2128
                            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

2128
                            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 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

2128
                            fseek($v_tar, ftell(/** @scrutinizer ignore-type */ $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

2128
                            fseek(/** @scrutinizer ignore-type */ $v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2129
                        } else {
2130
                            gzseek($v_tar, gztell($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 $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

2130
                            gzseek($v_tar, gztell(/** @scrutinizer ignore-type */ $v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
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

2130
                            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

2130
                            gzseek(/** @scrutinizer ignore-type */ $v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2131
                        }
2132
                    } else {
2133
                        TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2134
2135
                        // ----- Read data
2136
                        $n = floor($v_header['size'] / 512);
2137
                        for ($i = 0; $i < $n; ++$i) {
2138
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2139
                            if ('tar' === $p_tar_mode) {
2140
                                $v_content = fread($v_tar, 512);
2141
                            } else {
2142
                                $v_content = gzread($v_tar, 512);
2143
                            }
2144
                            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

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

2350
            fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2351
        } else {
2352
            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

2352
            gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2353
        }
2354
2355
        // ----- Return
2356
        TrFctEnd(__FILE__, __LINE__, $v_result);
2357
2358
        return $v_result;
2359
    }
2360
2361
    // --------------------------------------------------------------------------------
2362
2363
    // --------------------------------------------------------------------------------
2364
    // 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...
2365
    // Description :
2366
    // Parameters :
2367
    // Return Values :
2368
    // --------------------------------------------------------------------------------
2369
    /**
2370
     * @param $p_tar
2371
     * @param $p_index_current
2372
     * @param $p_index_start
2373
     * @param $p_index_stop
2374
     * @param $p_list_detail
2375
     * @param $p_path
2376
     * @param $p_remove_path
2377
     * @param $p_tar_mode
2378
     *
2379
     * @return int
2380
     */
2381
    function PclTarHandleExtractByIndex(
2382
        $p_tar,
2383
        &$p_index_current,
2384
        $p_index_start,
2385
        $p_index_stop,
2386
        &$p_list_detail,
2387
        $p_path,
2388
        $p_remove_path,
2389
        $p_tar_mode
2390
    ) {
2391
        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");
2392
        $v_result = 1;
2393
        $v_nb     = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $v_nb is dead and can be removed.
Loading history...
2394
2395
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2396
        $v_tar = $p_tar;
2397
2398
        // ----- Look the number of elements already in $p_list_detail
2399
        $v_nb = count($p_list_detail);
2400
2401
        // ----- Read the blocks
2402
        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...
2403
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next file ...');
2404
            TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
2405
2406
            if ($p_index_current > $p_index_stop) {
2407
                TrFctMessage(__FILE__, __LINE__, 2, 'Stop extraction, past stop index');
2408
                break;
2409
            }
2410
2411
            // ----- Clear cache of file infos
2412
            clearstatcache();
2413
2414
            // ----- Reset extract tag
2415
            $v_extract_file       = false;
2416
            $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...
2417
2418
            // ----- Read the 512 bytes header
2419
            if ('tar' === $p_tar_mode) {
2420
                $v_binary_data = fread($v_tar, 512);
2421
            } else {
2422
                $v_binary_data = gzread($v_tar, 512);
2423
            }
2424
2425
            // ----- Read the header properties
2426
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
2427
                // ----- Return
2428
                TrFctEnd(__FILE__, __LINE__, $v_result);
2429
2430
                return $v_result;
2431
            }
2432
2433
            // ----- Look for empty blocks to skip
2434
            if ('' == $v_header['filename']) {
2435
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2436
                continue;
2437
            }
2438
2439
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2440
2441
            // ----- Look if file is in the range to be extracted
2442
            if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop)) {
2443
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is in the range to be extracted');
2444
                $v_extract_file = true;
2445
            } else {
2446
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is out of the range');
2447
                $v_extract_file = false;
2448
            }
2449
2450
            // ----- Look if this file need to be extracted
2451
            if ($v_extract_file) {
2452
                if (1 != ($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode))) {
2453
                    // ----- Return
2454
                    TrFctEnd(__FILE__, __LINE__, $v_result);
2455
2456
                    return $v_result;
2457
                }
2458
            } // ----- Look for file that is not to be extracted
2459
            else {
2460
                // ----- Trace
2461
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file ' . $v_header['filename'] . '');
2462
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2463
2464
                // ----- Jump to next file
2465
                if ('tar' === $p_tar_mode) {
2466
                    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

2466
                    fseek($v_tar, /** @scrutinizer ignore-type */ ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2467
                } else {
2468
                    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

2468
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2469
                }
2470
2471
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2472
            }
2473
2474
            if ('tar' === $p_tar_mode) {
2475
                $v_end_of_file = feof($v_tar);
2476
            } else {
2477
                $v_end_of_file = gzeof($v_tar);
2478
            }
2479
2480
            // ----- File name and properties are logged if listing mode or file is extracted
2481
            if ($v_extract_file) {
2482
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2483
2484
                // ----- Log extracted files
2485
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2486
                    $v_file_dir = '';
2487
                }
2488
                if (('/' === substr($v_header['filename'], 0, 1)) && ('' === $v_file_dir)) {
2489
                    $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...
2490
                }
2491
2492
                // ----- Add the array describing the file into the list
2493
                $p_list_detail[$v_nb] = $v_header;
2494
2495
                // ----- Increment
2496
                ++$v_nb;
2497
            }
2498
2499
            // ----- Increment the current file index
2500
            ++$p_index_current;
2501
        }
2502
2503
        // ----- Return
2504
        TrFctEnd(__FILE__, __LINE__, $v_result);
2505
2506
        return $v_result;
2507
    }
2508
2509
    // --------------------------------------------------------------------------------
2510
2511
    // --------------------------------------------------------------------------------
2512
    // 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...
2513
    // Description :
2514
    // Parameters :
2515
    // Return Values :
2516
    // --------------------------------------------------------------------------------
2517
    /**
2518
     * @param $p_tar
2519
     * @param $v_header
2520
     * @param $p_path
2521
     * @param $p_remove_path
2522
     * @param $p_tar_mode
2523
     *
2524
     * @return int
2525
     */
2526
    function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
2527
    {
2528
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractFile', "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2529
        $v_result = 1;
2530
2531
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2532
        $v_tar          = $p_tar;
2533
        $v_extract_file = 1;
2534
2535
        $p_remove_path_size = strlen($p_remove_path);
2536
2537
        // ----- Look for path to remove
2538
        if (('' != $p_remove_path) && (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path)) {
2539
            TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2540
            // ----- Remove the path
2541
            $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2542
            TrFctMessage(__FILE__, __LINE__, 3, 'Resulting file is ' . $v_header['filename'] . '');
2543
        }
2544
2545
        // ----- Add the path to the file
2546
        if (('./' !== $p_path) && ('/' !== $p_path)) {
2547
            // ----- Look for the path end '/'
2548
            while ('/' === substr($p_path, -1)) {
2549
                TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2550
                $p_path = substr($p_path, 0, -1);
2551
                TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2552
            }
2553
2554
            // ----- Add the path
2555
            if ('/' === substr($v_header['filename'], 0, 1)) {
2556
                $v_header['filename'] = $p_path . $v_header['filename'];
2557
            } else {
2558
                $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2559
            }
2560
        }
2561
2562
        // ----- Trace
2563
        TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2564
2565
        // ----- Check that the file does not exists
2566
        if (file_exists($v_header['filename'])) {
2567
            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2568
2569
            // ----- Look if file is a directory
2570
            if (is_dir($v_header['filename'])) {
2571
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2572
2573
                // ----- Change the file status
2574
                $v_header['status'] = 'already_a_directory';
2575
2576
                // ----- Skip the extract
2577
                $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...
2578
                $v_extract_file       = 0;
2579
            } // ----- Look if file is write protected
2580
            elseif (!is_writable($v_header['filename'])) {
2581
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2582
2583
                // ----- Change the file status
2584
                $v_header['status'] = 'write_protected';
2585
2586
                // ----- Skip the extract
2587
                $v_extraction_stopped = 1;
2588
                $v_extract_file       = 0;
2589
            } // ----- Look if the extracted file is older
2590
            elseif (filemtime($v_header['filename']) > $v_header['mtime']) {
2591
                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']) . ')');
2592
2593
                // ----- Change the file status
2594
                $v_header['status'] = 'newer_exist';
2595
2596
                // ----- Skip the extract
2597
                $v_extraction_stopped = 1;
2598
                $v_extract_file       = 0;
2599
            }
2600
        } // ----- Check the directory availability and create it if necessary
2601
        else {
2602
            if ('5' == $v_header['typeflag']) {
2603
                $v_dir_to_check = $v_header['filename'];
2604
            } elseif (false === strpos($v_header['filename'], '/')) {
2605
                $v_dir_to_check = '';
2606
            } else {
2607
                $v_dir_to_check = dirname($v_header['filename']);
2608
            }
2609
2610
            if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2611
                TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2612
2613
                // ----- Change the file status
2614
                $v_header['status'] = 'path_creation_fail';
2615
2616
                // ----- Skip the extract
2617
                $v_extraction_stopped = 1;
2618
                $v_extract_file       = 0;
2619
            }
2620
        }
2621
2622
        // ----- Do the real bytes extraction (if not a directory)
2623
        if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2624
            // ----- Open the destination file in write mode
2625
            if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
2626
                TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2627
2628
                // ----- Change the file status
2629
                $v_header['status'] = 'write_error';
2630
2631
                // ----- Jump to next file
2632
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2633
                if ('tar' === $p_tar_mode) {
2634
                    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

2634
                    fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2635
                } else {
2636
                    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

2636
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2637
                }
2638
            } else {
2639
                TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2640
2641
                // ----- Read data
2642
                $n = floor($v_header['size'] / 512);
2643
                for ($i = 0; $i < $n; ++$i) {
2644
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2645
                    if ('tar' === $p_tar_mode) {
2646
                        $v_content = fread($v_tar, 512);
2647
                    } else {
2648
                        $v_content = gzread($v_tar, 512);
2649
                    }
2650
                    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

2650
                    fwrite(/** @scrutinizer ignore-type */ $v_dest_file, $v_content, 512);
Loading history...
2651
                }
2652
                if (0 != ($v_header['size'] % 512)) {
2653
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read last ' . ($v_header['size'] % 512) . ' bytes in a 512 block');
2654
                    if ('tar' === $p_tar_mode) {
2655
                        $v_content = fread($v_tar, 512);
2656
                    } else {
2657
                        $v_content = gzread($v_tar, 512);
2658
                    }
2659
                    fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2660
                }
2661
2662
                // ----- Close the destination file
2663
                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

2663
                fclose(/** @scrutinizer ignore-type */ $v_dest_file);
Loading history...
2664
2665
                // ----- Change the file mode, mtime
2666
                touch($v_header['filename'], $v_header['mtime']);
2667
                //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...
2668
            }
2669
2670
            // ----- Check the file size
2671
            clearstatcache();
2672
            if (filesize($v_header['filename']) != $v_header['size']) {
2673
                // ----- Error log
2674
                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...
2675
2676
                // ----- Return
2677
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2678
2679
                return PclErrorCode();
2680
            }
2681
2682
            // ----- Trace
2683
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2684
        } else {
2685
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file ' . $v_header['filename'] . ' skipped.');
2686
2687
            // ----- Jump to next file
2688
            TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2689
            if ('tar' === $p_tar_mode) {
2690
                fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2691
            } else {
2692
                gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2693
            }
2694
        }
2695
2696
        // ----- Return
2697
        TrFctEnd(__FILE__, __LINE__, $v_result);
2698
2699
        return $v_result;
2700
    }
2701
2702
    // --------------------------------------------------------------------------------
2703
2704
    // --------------------------------------------------------------------------------
2705
    // 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...
2706
    // Description :
2707
    // Parameters :
2708
    // Return Values :
2709
    // --------------------------------------------------------------------------------
2710
    /**
2711
     * @param $p_tarname
2712
     * @param $p_file_list
2713
     * @param $p_list_detail
2714
     * @param $p_tar_mode
2715
     *
2716
     * @return int
2717
     */
2718
    function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
2719
    {
2720
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleDelete', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2721
        $v_result = 1;
2722
        $v_nb     = 0;
2723
2724
        // ----- Look for regular tar file
2725
        if ('tar' === $p_tar_mode) {
2726
            // ----- Open file
2727
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2728
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2729
                // ----- Error log
2730
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2731
2732
                // ----- Return
2733
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2734
2735
                return PclErrorCode();
2736
            }
2737
2738
            // ----- Open a temporary file in write mode
2739
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2740
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2741
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
2742
                // ----- Close tar file
2743
                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

2743
                fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2744
2745
                // ----- Error log
2746
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2747
2748
                // ----- Return
2749
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2750
2751
                return PclErrorCode();
2752
            }
2753
        } // ----- Look for compressed tar file
2754
        else {
2755
            // ----- Open the file in read mode
2756
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2757
            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...
2758
                // ----- Error log
2759
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2760
2761
                // ----- Return
2762
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2763
2764
                return PclErrorCode();
2765
            }
2766
2767
            // ----- Open a temporary file in write mode
2768
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2769
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2770
            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...
2771
                // ----- Close tar file
2772
                gzclose($v_tar);
2773
2774
                // ----- Error log
2775
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2776
2777
                // ----- Return
2778
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2779
2780
                return PclErrorCode();
2781
            }
2782
        }
2783
2784
        // ----- Read the blocks
2785
        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

2785
        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

2785
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof(/** @scrutinizer ignore-type */ $v_tar) : gzeof($v_tar)))) {
Loading history...
2786
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
2787
2788
            // ----- Clear cache of file infos
2789
            clearstatcache();
2790
2791
            // ----- Reset delete tag
2792
            $v_delete_file = false;
2793
2794
            // ----- Read the first 512 block header
2795
            if ('tar' === $p_tar_mode) {
2796
                $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

2796
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
2797
            } else {
2798
                $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

2798
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
2799
            }
2800
2801
            // ----- Read the header properties
2802
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
2803
                // ----- Close the archive file
2804
                if ('tar' === $p_tar_mode) {
2805
                    fclose($v_tar);
2806
                    fclose($v_temp_tar);
2807
                } else {
2808
                    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

2808
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2809
                    gzclose($v_temp_tar);
2810
                }
2811
                @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

2811
                /** @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...
2812
2813
                // ----- Return
2814
                TrFctEnd(__FILE__, __LINE__, $v_result);
2815
2816
                return $v_result;
2817
            }
2818
2819
            // ----- Look for empty blocks to skip
2820
            if ('' == $v_header['filename']) {
2821
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2822
                continue;
2823
            }
2824
2825
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2826
2827
            // ----- Look for filenames to delete
2828
            for ($i = 0, $v_delete_file = false; ($i < count($p_file_list)) && (!$v_delete_file); ++$i) {
2829
                // ----- Compare the file names
2830
                //        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...
2831
                if (($v_len = strcmp($p_file_list[$i], $v_header['filename'])) <= 0) {
2832
                    if (0 == $v_len) {
2833
                        TrFctMessage(__FILE__, __LINE__, 3, 'Found that ' . $v_header['filename'] . ' need to be deleted');
2834
                        $v_delete_file = true;
2835
                    } else {
2836
                        TrFctMessage(__FILE__, __LINE__, 3, 'Look if ' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2837
                        if ('/' === substr($v_header['filename'], strlen($p_file_list[$i]), 1)) {
2838
                            TrFctMessage(__FILE__, __LINE__, 3, '' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2839
                            $v_delete_file = true;
2840
                        }
2841
                    }
2842
                }
2843
            }
2844
2845
            // ----- Copy files that do not need to be deleted
2846
            if (!$v_delete_file) {
2847
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
2848
2849
                // ----- Write the file header
2850
                if ('tar' === $p_tar_mode) {
2851
                    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

2851
                    fwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_binary_data, 512);
Loading history...
2852
                } else {
2853
                    gzputs($v_temp_tar, $v_binary_data, 512);
2854
                }
2855
2856
                // ----- Write the file data
2857
                $n = ceil($v_header['size'] / 512);
2858
                for ($i = 0; $i < $n; ++$i) {
2859
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2860
                    if ('tar' === $p_tar_mode) {
2861
                        $v_content = fread($v_tar, 512);
2862
                        fwrite($v_temp_tar, $v_content, 512);
2863
                    } else {
2864
                        $v_content = gzread($v_tar, 512);
2865
                        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

2865
                        gzwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_content, 512);
Loading history...
2866
                    }
2867
                }
2868
2869
                // ----- File name and properties are logged if listing mode or file is extracted
2870
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2871
2872
                // ----- Add the array describing the file into the list
2873
                $p_list_detail[$v_nb]           = $v_header;
2874
                $p_list_detail[$v_nb]['status'] = 'ok';
2875
2876
                // ----- Increment
2877
                ++$v_nb;
2878
            } // ----- Look for file that is to be deleted
2879
            else {
2880
                // ----- Trace
2881
                TrFctMessage(__FILE__, __LINE__, 2, 'Start deletion of ' . $v_header['filename'] . '');
2882
                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 $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

2882
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell(/** @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 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

2882
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell(/** @scrutinizer ignore-type */ $v_tar) : gztell($v_tar)) . ']');
Loading history...
2883
2884
                // ----- Jump to next file
2885
                if ('tar' === $p_tar_mode) {
2886
                    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

2886
                    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

2886
                    fseek($v_tar, /** @scrutinizer ignore-type */ ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2887
                } else {
2888
                    gzseek($v_tar, gztell($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 $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

2888
                    gzseek(/** @scrutinizer ignore-type */ $v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
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

2888
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
Loading history...
2889
                }
2890
2891
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2892
            }
2893
2894
            // ----- Look for end of file
2895
            if ('tar' === $p_tar_mode) {
2896
                $v_end_of_file = feof($v_tar);
2897
            } else {
2898
                $v_end_of_file = gzeof($v_tar);
2899
            }
2900
        }
2901
2902
        // ----- Write the last empty buffer
2903
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
2904
2905
        // ----- Close the tarfile
2906
        if ('tar' === $p_tar_mode) {
2907
            fclose($v_tar);
2908
            fclose($v_temp_tar);
2909
        } else {
2910
            gzclose($v_tar);
2911
            gzclose($v_temp_tar);
2912
        }
2913
2914
        // ----- Unlink tar file
2915
        if (!@unlink($p_tarname)) {
2916
            // ----- Error log
2917
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
2918
        }
2919
2920
        // ----- Rename tar file
2921
        if (!@rename($v_temp_tarname, $p_tarname)) {
2922
            // ----- Error log
2923
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
2924
2925
            // ----- Return
2926
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2927
2928
            return PclErrorCode();
2929
        }
2930
2931
        // ----- Return
2932
        TrFctEnd(__FILE__, __LINE__, $v_result);
2933
2934
        return $v_result;
2935
    }
2936
2937
    // --------------------------------------------------------------------------------
2938
2939
    // --------------------------------------------------------------------------------
2940
    // 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...
2941
    // Description :
2942
    // Parameters :
2943
    // Return Values :
2944
    // --------------------------------------------------------------------------------
2945
    /**
2946
     * @param $p_tarname
2947
     * @param $p_file_list
2948
     * @param $p_list_detail
2949
     * @param $p_tar_mode
2950
     * @param $p_add_dir
2951
     * @param $p_remove_dir
2952
     *
2953
     * @return int
2954
     */
2955
    function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
2956
    {
2957
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleUpdate', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2958
        $v_result     = 1;
2959
        $v_nb         = 0;
2960
        $v_found_list = [];
2961
2962
        // ----- Look for regular tar file
2963
        if ('tar' === $p_tar_mode) {
2964
            // ----- Open file
2965
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2966
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2967
                // ----- Error log
2968
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2969
2970
                // ----- Return
2971
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2972
2973
                return PclErrorCode();
2974
            }
2975
2976
            // ----- Open a temporary file in write mode
2977
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2978
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2979
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
2980
                // ----- Close tar file
2981
                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

2981
                fclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
2982
2983
                // ----- Error log
2984
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2985
2986
                // ----- Return
2987
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2988
2989
                return PclErrorCode();
2990
            }
2991
        } // ----- Look for compressed tar file
2992
        else {
2993
            // ----- Open the file in read mode
2994
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2995
            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...
2996
                // ----- Error log
2997
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2998
2999
                // ----- Return
3000
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3001
3002
                return PclErrorCode();
3003
            }
3004
3005
            // ----- Open a temporary file in write mode
3006
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
3007
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3008
            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...
3009
                // ----- Close tar file
3010
                gzclose($v_tar);
3011
3012
                // ----- Error log
3013
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3014
3015
                // ----- Return
3016
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3017
3018
                return PclErrorCode();
3019
            }
3020
        }
3021
3022
        // ----- Prepare the list of files
3023
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3024
            // ----- Reset the found list
3025
            $v_found_list[$i] = 0;
3026
3027
            // ----- Calculate the stored filename
3028
            $v_stored_list[$i] = $p_file_list[$i];
3029
            if ('' != $p_remove_dir) {
3030
                if ('/' !== substr($p_file_list[$i], -1)) {
3031
                    $p_remove_dir .= '/';
3032
                }
3033
3034
                if (substr($p_file_list[$i], 0, strlen($p_remove_dir)) == $p_remove_dir) {
3035
                    $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
3036
                    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...
3037
                }
3038
            }
3039
            if ('' != $p_add_dir) {
3040
                if ('/' === substr($p_add_dir, -1)) {
3041
                    $v_stored_list[$i] = $p_add_dir . $v_stored_list[$i];
3042
                } else {
3043
                    $v_stored_list[$i] = $p_add_dir . '/' . $v_stored_list[$i];
3044
                }
3045
                TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3046
            }
3047
            $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
3048
            TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
3049
        }
3050
3051
        // ----- Update file cache
3052
        clearstatcache();
3053
3054
        // ----- Read the blocks
3055
        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

3055
        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

3055
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof(/** @scrutinizer ignore-type */ $v_tar)))) {
Loading history...
Unused Code introduced by
The assignment to $v_end_of_file is dead and can be removed.
Loading history...
3056
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
3057
3058
            // ----- Clear cache of file infos
3059
            clearstatcache();
3060
3061
            // ----- Reset current found filename
3062
            $v_current_filename = '';
3063
3064
            // ----- Reset delete tag
3065
            $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...
3066
3067
            // ----- Read the first 512 block header
3068
            if ('tar' === $p_tar_mode) {
3069
                $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

3069
                $v_binary_data = fread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
3070
            } else {
3071
                $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

3071
                $v_binary_data = gzread(/** @scrutinizer ignore-type */ $v_tar, 512);
Loading history...
3072
            }
3073
3074
            // ----- Read the header properties
3075
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
3076
                // ----- Close the archive file
3077
                if ('tar' === $p_tar_mode) {
3078
                    fclose($v_tar);
3079
                    fclose($v_temp_tar);
3080
                } else {
3081
                    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

3081
                    gzclose(/** @scrutinizer ignore-type */ $v_tar);
Loading history...
3082
                    gzclose($v_temp_tar);
3083
                }
3084
                @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

3084
                /** @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...
3085
3086
                // ----- Return
3087
                TrFctEnd(__FILE__, __LINE__, $v_result);
3088
3089
                return $v_result;
3090
            }
3091
3092
            // ----- Look for empty blocks to skip
3093
            if ('' == $v_header['filename']) {
3094
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
3095
                continue;
3096
            }
3097
3098
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
3099
3100
            // ----- Look for filenames to update
3101
            for ($i = 0, $v_update_file = false, $v_found_file = false; ($i < count($v_stored_list)) && (!$v_update_file); ++$i) {
3102
                TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
3103
3104
                // ----- Compare the file names
3105
                if ($v_stored_list[$i] == $v_header['filename']) {
3106
                    TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
3107
                    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])));
3108
                    TrFctMessage(__FILE__, __LINE__, 3, 'Archived mtime=' . $v_header['mtime'] . ' ' . date('l dS of F Y h:i:s A', $v_header['mtime']));
3109
3110
                    // ----- Store found informations
3111
                    $v_found_file       = true;
3112
                    $v_current_filename = $p_file_list[$i];
3113
3114
                    // ----- Look if the file need to be updated
3115
                    if (filemtime($p_file_list[$i]) > $v_header['mtime']) {
3116
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
3117
                        $v_update_file = true;
3118
                    } else {
3119
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
3120
                        $v_update_file = false;
3121
                    }
3122
3123
                    // ----- Flag the name in order not to add the file at the end
3124
                    $v_found_list[$i] = 1;
3125
                } else {
3126
                    TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not " . $v_header['filename'] . '');
3127
                }
3128
            }
3129
3130
            // ----- Copy files that do not need to be updated
3131
            if (!$v_update_file) {
3132
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
3133
3134
                // ----- Write the file header
3135
                if ('tar' === $p_tar_mode) {
3136
                    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

3136
                    fwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_binary_data, 512);
Loading history...
3137
                } else {
3138
                    gzputs($v_temp_tar, $v_binary_data, 512);
3139
                }
3140
3141
                // ----- Write the file data
3142
                $n = ceil($v_header['size'] / 512);
3143
                for ($j = 0; $j < $n; ++$j) {
3144
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($j + 1));
3145
                    if ('tar' === $p_tar_mode) {
3146
                        $v_content = fread($v_tar, 512);
3147
                        fwrite($v_temp_tar, $v_content, 512);
3148
                    } else {
3149
                        $v_content = gzread($v_tar, 512);
3150
                        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

3150
                        gzwrite(/** @scrutinizer ignore-type */ $v_temp_tar, $v_content, 512);
Loading history...
3151
                    }
3152
                }
3153
3154
                // ----- File name and properties are logged if listing mode or file is extracted
3155
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
3156
3157
                // ----- Add the array describing the file into the list
3158
                $p_list_detail[$v_nb]           = $v_header;
3159
                $p_list_detail[$v_nb]['status'] = ($v_found_file ? 'not_updated' : 'ok');
3160
3161
                // ----- Increment
3162
                ++$v_nb;
3163
            } // ----- Look for file that need to be updated
3164
            else {
3165
                // ----- Trace
3166
                TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
3167
3168
                // ----- Store the old file size
3169
                $v_old_size = $v_header['size'];
3170
3171
                // ----- Add the file
3172
                if (1 != ($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir))) {
3173
                    // ----- Close the tarfile
3174
                    if ('tar' === $p_tar_mode) {
3175
                        fclose($v_tar);
3176
                        fclose($v_temp_tar);
3177
                    } else {
3178
                        gzclose($v_tar);
3179
                        gzclose($v_temp_tar);
3180
                    }
3181
                    @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...
3182
3183
                    // ----- Return status
3184
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3185
3186
                    return $v_result;
3187
                }
3188
3189
                // ----- Trace
3190
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip old file ' . $v_header['filename'] . '');
3191
3192
                // ----- Jump to next file
3193
                if ('tar' === $p_tar_mode) {
3194
                    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

3194
                    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

3194
                    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

3194
                    fseek(/** @scrutinizer ignore-type */ $v_tar, ftell($v_tar) + (ceil($v_old_size / 512) * 512));
Loading history...
3195
                } else {
3196
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_old_size / 512) * 512));
0 ignored issues
show
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

3196
                    gzseek($v_tar, /** @scrutinizer ignore-type */ gztell($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 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

3196
                    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

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

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

3456
            TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ "'$p_dir' is a directory");
Loading history...
3457
3458
            return 1;
3459
        }
3460
3461
        // ----- Look for file alone
3462
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
3463
    if (false === strpos("$p_dir", "/")) {
3464
      TrFctEnd(__FILE__, __LINE__,  "'$p_dir' is a file with no directory");
3465
3466
      return 1;
3467
    }
3468
    */
3469
3470
        // ----- Extract parent directory
3471
        $p_parent_dir = dirname($p_dir);
3472
        TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
3473
3474
        // ----- Just a check
3475
        if ($p_parent_dir != $p_dir) {
3476
            // ----- Look for parent directory
3477
            if ('' != $p_parent_dir) {
3478
                if (1 != ($v_result = PclTarHandlerDirCheck($p_parent_dir))) {
3479
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3480
3481
                    return $v_result;
3482
                }
3483
            }
3484
        }
3485
3486
        // ----- Create the directory
3487
        TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
3488
        if (!@mkdir($p_dir, 0777)) {
3489
            // ----- Error log
3490
            PclErrorLog(-8, "Unable to create directory '$p_dir'");
3491
3492
            // ----- Return
3493
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3494
3495
            return PclErrorCode();
3496
        }
3497
3498
        // ----- Return
3499
        TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
3500
3501
        return $v_result;
3502
    }
3503
3504
    // --------------------------------------------------------------------------------
3505
3506
    // --------------------------------------------------------------------------------
3507
    // 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...
3508
    // Description :
3509
    // Parameters :
3510
    // Return Values :
3511
    // --------------------------------------------------------------------------------
3512
    /**
3513
     * @param $p_tarname
3514
     *
3515
     * @return string
3516
     */
3517
    function PclTarHandleExtension($p_tarname)
3518
    {
3519
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtension', "tar=$p_tarname");
3520
3521
        // ----- Look for file extension
3522
        if (('.tar.gz' === substr($p_tarname, -7)) || ('.tgz' === substr($p_tarname, -4))) {
3523
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a gzip tar');
3524
            $v_tar_mode = 'tgz';
3525
        } elseif ('.tar' === substr($p_tarname, -4)) {
3526
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a tar');
3527
            $v_tar_mode = 'tar';
3528
        } else {
3529
            // ----- Error log
3530
            PclErrorLog(-9, 'Invalid archive extension');
3531
3532
            TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3533
3534
            $v_tar_mode = '';
3535
        }
3536
3537
        // ----- Return
3538
        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 XoopsModules\Extgallery\TrFctEnd(). ( Ignorable by Annotation )

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

3538
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $v_tar_mode);
Loading history...
3539
3540
        return $v_tar_mode;
3541
    }
3542
3543
    // --------------------------------------------------------------------------------
3544
3545
    // --------------------------------------------------------------------------------
3546
    // 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...
3547
    // Description :
3548
    // Parameters :
3549
    // Return Values :
3550
    // --------------------------------------------------------------------------------
3551
    /**
3552
     * @param $p_dir
3553
     *
3554
     * @return string
3555
     */
3556
    function PclTarHandlePathReduction($p_dir)
3557
    {
3558
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlePathReduction', "dir='$p_dir'");
3559
        $v_result = '';
3560
3561
        // ----- Look for not empty path
3562
        if ('' != $p_dir) {
3563
            // ----- Explode path by directory names
3564
            $v_list = explode('/', $p_dir);
3565
3566
            // ----- Study directories from last to first
3567
            for ($i = count($v_list) - 1; $i >= 0; $i--) {
3568
                // ----- Look for current path
3569
                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...
3570
                    // ----- Ignore this directory
3571
                    // Should be the first $i=0, but no check is done
3572
                } elseif ('..' === $v_list[$i]) {
3573
                    // ----- Ignore it and ignore the $i-1
3574
                    $i--;
3575
                } elseif (('' == $v_list[$i]) && ($i != (count($v_list) - 1)) && (0 != $i)) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif 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 elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
3576
                    // ----- Ignore only the double '//' in path,
3577
                    // but not the first and last '/'
3578
                } else {
3579
                    $v_result = $v_list[$i] . ($i != (count($v_list) - 1) ? '/' . $v_result : '');
3580
                }
3581
            }
3582
        }
3583
3584
        // ----- Return
3585
        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 XoopsModules\Extgallery\TrFctEnd(). ( Ignorable by Annotation )

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

3585
        TrFctEnd(__FILE__, __LINE__, /** @scrutinizer ignore-type */ $v_result);
Loading history...
3586
3587
        return $v_result;
3588
    }
3589
3590
    // --------------------------------------------------------------------------------
3591
3592
    // ----- End of double include look
3593
}
3594