Completed
Push — master ( 72613d...069c91 )
by Michael
02:16
created

pcltar.lib.php ➔ PclTarExtractIndex()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 47
Code Lines 21

Duplication

Lines 47
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 11
nop 5
dl 47
loc 47
rs 5.2941
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 34.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
123
            // ----- Call the create fct
124
            $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
125
        } // ----- Look if the $p_filelist is a string
126
        elseif (is_string($p_filelist)) {
127
            // ----- Create a list with the elements from the string
128
            $v_list = explode(' ', $p_filelist);
129
130
            // ----- Call the create fct
131
            $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
132
        } // ----- Invalid variable
133
        else {
134
            // ----- Error log
135
            PclErrorLog(-3, 'Invalid variable type p_filelist');
136
            $v_result = -3;
137
        }
138
139
        // ----- Return
140
        TrFctEnd(__FILE__, __LINE__, $v_result);
141
142
        return $v_result;
143
    }
144
145
    // --------------------------------------------------------------------------------
146
147
    // --------------------------------------------------------------------------------
148
    // 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...
149
    // Description :
150
    //   PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
151
    //
152
    //   This function is maintained only for compatibility reason
153
    //
154
    // Parameters :
155
    //   $p_tarname : Name of an existing tar file
156
    //   $p_filelist : An array containing file or directory names, or
157
    //                 a string containing one filename or directory name, or
158
    //                 a string containing a list of filenames and/or directory
159
    //                 names separated by spaces.
160
    // Return Values :
161
    //   1 on success,
162
    //   Or an error code (see list on top).
163
    // --------------------------------------------------------------------------------
164
    /**
165
     * @param $p_tarname
166
     * @param $p_filelist
167
     *
168
     * @return int
169
     */
170
    function PclTarAdd($p_tarname, $p_filelist)
171
    {
172
        TrFctStart(__FILE__, __LINE__, 'PclTarAdd', "tar=$p_tarname, file=$p_filelist");
173
        $v_result      = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

Loading history...
174
        $v_list_detail = [];
175
176
        // ----- Extract the tar format from the extension
177
        if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
178
            // ----- Return
179
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
180
181
            return PclErrorCode();
182
        }
183
184
        // ----- Look if the $p_filelist is really an array
185
        if (is_array($p_filelist)) {
186
            // ----- Call the add fct
187
            $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, '', '');
188
        } // ----- Look if the $p_filelist is a string
189
        elseif (is_string($p_filelist)) {
190
            // ----- Create a list with the elements from the string
191
            $v_list = explode(' ', $p_filelist);
192
193
            // ----- Call the add fct
194
            $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, '', '');
195
        } // ----- Invalid variable
196
        else {
197
            // ----- Error log
198
            PclErrorLog(-3, 'Invalid variable type p_filelist');
199
            $v_result = -3;
200
        }
201
202
        // ----- Cleaning
203
        unset($v_list_detail);
204
205
        // ----- Return
206
        TrFctEnd(__FILE__, __LINE__, $v_result);
207
208
        return $v_result;
209
    }
210
211
    // --------------------------------------------------------------------------------
212
213
    // --------------------------------------------------------------------------------
214
    // 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...
215
    // Description :
216
    //   Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
217
    //   The list can be an array of file/directory names or a string with names
218
    //   separated by one space.
219
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
220
    //   different from the real path of the file. This is usefull if you want to have PclTar
221
    //   running in any directory, and memorize relative path from an other directory.
222
    //   If $p_mode is not set it will be automatically computed from the $p_tarname
223
    //   extension (.tar, .tar.gz or .tgz).
224
    // Parameters :
225
    //   $p_tarname : Name of an existing tar file
226
    //   $p_filelist : An array containing file or directory names, or
227
    //                 a string containing one filename or directory name, or
228
    //                 a string containing a list of filenames and/or directory
229
    //                 names separated by spaces.
230
    //   $p_add_dir : Path to add in the filename path archived
231
    //   $p_remove_dir : Path to remove in the filename path archived
232
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
233
    // Return Values :
234
    //   1 on success,
235
    //   Or an error code (see list on top).
236
    // --------------------------------------------------------------------------------
237
    /**
238
     * @param        $p_tarname
239
     * @param        $p_filelist
240
     * @param string $p_add_dir
241
     * @param string $p_remove_dir
242
     * @param string $p_mode
243
     *
244
     * @return array|int
245
     */
246
    function PclTarAddList($p_tarname, $p_filelist, $p_add_dir = '', $p_remove_dir = '', $p_mode = '')
247
    {
248
        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");
249
        $v_result      = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

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

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

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

Loading history...
264
            // ----- Call the add fct
265
            $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
266
        } // ----- Look if the $p_filelist is a string
267
        elseif (is_string($p_filelist)) {
268
            // ----- Create a list with the elements from the string
269
            $v_list = explode(' ', $p_filelist);
270
271
            // ----- Call the add fct
272
            $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
273
        } // ----- Invalid variable
274
        else {
275
            // ----- Error log
276
            PclErrorLog(-3, 'Invalid variable type p_filelist');
277
            $v_result = -3;
278
        }
279
280
        // ----- Return
281
        if (1 != $v_result) {
282
            TrFctEnd(__FILE__, __LINE__, 0);
283
284
            return 0;
285
        }
286
        TrFctEnd(__FILE__, __LINE__, $p_list_detail);
287
288
        return $p_list_detail;
289
    }
290
291
    // --------------------------------------------------------------------------------
292
293
    // --------------------------------------------------------------------------------
294
    // 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...
295
    // Description :
296
    //   Gives the list of all the files present in the tar archive $p_tarname.
297
    //   The list is the function result, it will be 0 on error.
298
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
299
    //   function will determine the type of the archive.
300
    // Parameters :
301
    //   $p_tarname : Name of an existing tar file
302
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
303
    // Return Values :
304
    //  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...
305
    //  or
306
    //  An array containing file properties. Each file properties is an array of
307
    //  properties.
308
    //  The properties (array field names) are :
309
    //    filename, size, mode, uid, gid, mtime, typeflag, status
310
    //  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...
311
    //            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...
312
    //              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...
313
    // --------------------------------------------------------------------------------
314
    /**
315
     * @param        $p_tarname
316
     * @param string $p_mode
317
     *
318
     * @return array|int
319
     */
320 View Code Duplication
    function PclTarList($p_tarname, $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

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

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

Loading history...
324
325
        // ----- Extract the tar format from the extension
326
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
327
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
328
                // ----- Return
329
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
330
331
                return 0;
332
            }
333
        }
334
335
        // ----- Call the extracting fct
336
        $p_list = [];
337
        if (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'list', '', $p_mode, ''))) {
338
            unset($p_list);
339
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
340
341
            return 0;
342
        }
343
344
        // ----- Return
345
        TrFctEnd(__FILE__, __LINE__, $p_list);
346
347
        return $p_list;
348
    }
349
350
    // --------------------------------------------------------------------------------
351
352
    // --------------------------------------------------------------------------------
353
    // 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...
354
    // Description :
355
    //   Extract all the files present in the archive $p_tarname, in the directory
356
    //   $p_path. The relative path of the archived files are keep and become
357
    //   relative to $p_path.
358
    //   If a file with the same name already exists it will be replaced.
359
    //   If the path to the file does not exist, it will be created.
360
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
361
    //   function will determine the type of the archive.
362
    // Parameters :
363
    //   $p_tarname : Name of an existing tar file.
364
    //   $p_path : Path where the files will be extracted. The files will use
365
    //             their memorized path from $p_path.
366
    //             If $p_path is "", files will be extracted in "./".
367
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
368
    //                    extracted files. If the path does not match the file path,
369
    //                    the file is extracted with its memorized path.
370
    //                    $p_path and $p_remove_path are commulative.
371
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
372
    // Return Values :
373
    //   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...
374
    // --------------------------------------------------------------------------------
375
    /**
376
     * @param        $p_tarname
377
     * @param string $p_path
378
     * @param string $p_remove_path
379
     * @param string $p_mode
380
     *
381
     * @return int
382
     */
383 View Code Duplication
    function PclTarExtract($p_tarname, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

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

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

Loading history...
387
388
        // ----- Extract the tar format from the extension
389
        if (('' == $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
390
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
391
                // ----- Return
392
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
393
394
                return 0;
395
            }
396
        }
397
398
        // ----- Call the extracting fct
399
        if (1 != ($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, 'complete', $p_path, $p_mode, $p_remove_path))) {
400
            TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
401
402
            return 0;
403
        }
404
405
        // ----- Return
406
        TrFctEnd(__FILE__, __LINE__, $p_list);
407
408
        return $p_list;
409
    }
410
411
    // --------------------------------------------------------------------------------
412
413
    // --------------------------------------------------------------------------------
414
    // 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...
415
    // Description :
416
    //   Extract the files present in the archive $p_tarname and specified in
417
    //   $p_filelist, in the directory
418
    //   $p_path. The relative path of the archived files are keep and become
419
    //   relative to $p_path.
420
    //   If a directory is specified in the list, all the files from this directory
421
    //   will be extracted.
422
    //   If a file with the same name already exists it will be replaced.
423
    //   If the path to the file does not exist, it will be created.
424
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
425
    //   function will determine the type of the archive.
426
    // Parameters :
427
    //   $p_tarname : Name of an existing tar file
428
    //   $p_filelist : An array containing file or directory names, or
429
    //                 a string containing one filename or directory name, or
430
    //                 a string containing a list of filenames and/or directory
431
    //                 names separated by spaces.
432
    //   $p_path : Path where the files will be extracted. The files will use
433
    //             their memorized path from $p_path.
434
    //             If $p_path is "", files will be extracted in "./".
435
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
436
    //                    extracted files. If the path does not match the file path,
437
    //                    the file is extracted with its memorized path.
438
    //                    $p_path and $p_remove_path are commulative.
439
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
440
    // Return Values :
441
    //   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...
442
    // --------------------------------------------------------------------------------
443
    /**
444
     * @param        $p_tarname
445
     * @param        $p_filelist
446
     * @param string $p_path
447
     * @param string $p_remove_path
448
     * @param string $p_mode
449
     *
450
     * @return int
451
     */
452 View Code Duplication
    function PclTarExtractList($p_tarname, $p_filelist, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

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

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

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

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

Loading history...
471
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
472
473
                return 0;
474
            }
475
        } // ----- Look if the $p_filelist is a string
476
        elseif (is_string($p_filelist)) {
477
            // ----- Create a list with the elements from the string
478
            $v_list = explode(' ', $p_filelist);
479
480
            // ----- Call the extracting fct
481
            if (1 != ($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, 'partial', $p_path, $v_tar_mode, $p_remove_path))) {
482
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
483
484
                return 0;
485
            }
486
        } // ----- Invalid variable
487
        else {
488
            // ----- Error log
489
            PclErrorLog(-3, 'Invalid variable type p_filelist');
490
491
            // ----- Return
492
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
493
494
            return 0;
495
        }
496
497
        // ----- Return
498
        TrFctEnd(__FILE__, __LINE__, $p_list);
499
500
        return $p_list;
501
    }
502
503
    // --------------------------------------------------------------------------------
504
505
    // --------------------------------------------------------------------------------
506
    // 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...
507
    // Description :
508
    //   Extract the files present in the archive $p_tarname and specified at
509
    //   the indexes in $p_index, in the directory
510
    //   $p_path. The relative path of the archived files are keep and become
511
    //   relative to $p_path.
512
    //   If a directory is specified in the list, the directory only is created. All
513
    //   the file stored in this archive for this directory
514
    //   are not extracted.
515
    //   If a file with the same name already exists it will be replaced.
516
    //   If the path to the file does not exist, it will be created.
517
    //   Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
518
    //   function will determine the type of the archive.
519
    // Parameters :
520
    //   $p_tarname : Name of an existing tar file
521
    //   $p_index : A single index (integer) or a string of indexes of files to
522
    //              extract. The form of the string is "0,4-6,8-12" with only numbers
523
    //              and '-' for range or ',' to separate ranges. No spaces or ';'
524
    //              are allowed.
525
    //   $p_path : Path where the files will be extracted. The files will use
526
    //             their memorized path from $p_path.
527
    //             If $p_path is "", files will be extracted in "./".
528
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
529
    //                    extracted files. If the path does not match the file path,
530
    //                    the file is extracted with its memorized path.
531
    //                    $p_path and $p_remove_path are commulative.
532
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
533
    // Return Values :
534
    //   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...
535
    // --------------------------------------------------------------------------------
536
    /**
537
     * @param        $p_tarname
538
     * @param        $p_index
539
     * @param string $p_path
540
     * @param string $p_remove_path
541
     * @param string $p_mode
542
     *
543
     * @return int
544
     */
545 View Code Duplication
    function PclTarExtractIndex($p_tarname, $p_index, $p_path = './', $p_remove_path = '', $p_mode = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

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

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

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

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

Loading history...
564
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
565
566
                return 0;
567
            }
568
        } // ----- Look if the $p_filelist is a string
569
        elseif (is_string($p_index)) {
570
            // ----- Call the extracting fct
571
            if (1 != ($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode))) {
572
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
573
574
                return 0;
575
            }
576
        } // ----- Invalid variable
577
        else {
578
            // ----- Error log
579
            PclErrorLog(-3, "Invalid variable type $p_index");
580
581
            // ----- Return
582
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
583
584
            return 0;
585
        }
586
587
        // ----- Return
588
        TrFctEnd(__FILE__, __LINE__, $p_list);
589
590
        return $p_list;
591
    }
592
593
    // --------------------------------------------------------------------------------
594
595
    // --------------------------------------------------------------------------------
596
    // 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...
597
    // Description :
598
    //   This function deletes from the archive $p_tarname the files which are listed
599
    //   in $p_filelist. $p_filelist can be a string with file names separated by
600
    //   spaces, or an array containing the file names.
601
    // Parameters :
602
    //   $p_tarname : Name of an existing tar file
603
    //   $p_filelist : An array or a string containing file names to remove from the
604
    //                 archive.
605
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
606
    // Return Values :
607
    //   List of the files which are kept in the archive (same format as PclTarList())
608
    // --------------------------------------------------------------------------------
609
    /**
610
     * @param        $p_tarname
611
     * @param        $p_filelist
612
     * @param string $p_mode
613
     *
614
     * @return int
615
     */
616
    function PclTarDelete($p_tarname, $p_filelist, $p_mode = '')
617
    {
618
        TrFctStart(__FILE__, __LINE__, 'PclTarDelete', "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
619
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

Loading history...
620
621
        // ----- Extract the tar format from the extension
622
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
623
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
624
                // ----- Return
625
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
626
627
                return 0;
628
            }
629
        }
630
631
        // ----- Look if the $p_filelist is really an array
632
        if (is_array($p_filelist)) {
633
            // ----- Call the extracting fct
634
            if (1 != ($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode))) {
635
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
636
637
                return 0;
638
            }
639
        } // ----- Look if the $p_filelist is a string
640
        elseif (is_string($p_filelist)) {
641
            // ----- Create a list with the elements from the string
642
            $v_list = explode(' ', $p_filelist);
643
644
            // ----- Call the extracting fct
645
            if (1 != ($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode))) {
646
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
647
648
                return 0;
649
            }
650
        } // ----- Invalid variable
651
        else {
652
            // ----- Error log
653
            PclErrorLog(-3, 'Invalid variable type p_filelist');
654
655
            // ----- Return
656
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
657
658
            return 0;
659
        }
660
661
        // ----- Return
662
        TrFctEnd(__FILE__, __LINE__, $p_list);
663
664
        return $p_list;
665
    }
666
667
    // --------------------------------------------------------------------------------
668
669
    // --------------------------------------------------------------------------------
670
    // 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...
671
    // Description :
672
    //   This function updates the files in $p_filelist which are already in the
673
    //   $p_tarname archive with an older last modified date. If the file does not
674
    //   exist, it is added at the end of the archive.
675
    // Parameters :
676
    //   $p_tarname : Name of an existing tar file
677
    //   $p_filelist : An array or a string containing file names to update from the
678
    //                 archive.
679
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
680
    // Return Values :
681
    //   List of the files contained in the archive. The field status contains
682
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
683
    // --------------------------------------------------------------------------------
684
    /**
685
     * @param        $p_tarname
686
     * @param        $p_filelist
687
     * @param string $p_mode
688
     * @param string $p_add_dir
689
     * @param string $p_remove_dir
690
     *
691
     * @return int
692
     */
693 View Code Duplication
    function PclTarUpdate($p_tarname, $p_filelist, $p_mode = '', $p_add_dir = '', $p_remove_dir = '')
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

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

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

Loading history...
697
698
        // ----- Extract the tar format from the extension
699
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
700
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
701
                // ----- Return
702
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
703
704
                return 0;
705
            }
706
        }
707
708
        // ----- Look if the $p_filelist is really an array
709
        if (is_array($p_filelist)) {
710
            // ----- Call the extracting fct
711
            if (1 != ($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir))) {
712
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
713
714
                return 0;
715
            }
716
        } // ----- Look if the $p_filelist is a string
717
        elseif (is_string($p_filelist)) {
718
            // ----- Create a list with the elements from the string
719
            $v_list = explode(' ', $p_filelist);
720
721
            // ----- Call the extracting fct
722
            if (1 != ($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir))) {
723
                TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
724
725
                return 0;
726
            }
727
        } // ----- Invalid variable
728
        else {
729
            // ----- Error log
730
            PclErrorLog(-3, 'Invalid variable type p_filelist');
731
732
            // ----- Return
733
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
734
735
            return 0;
736
        }
737
738
        // ----- Return
739
        TrFctEnd(__FILE__, __LINE__, $p_list);
740
741
        return $p_list;
742
    }
743
744
    // --------------------------------------------------------------------------------
745
746
    // --------------------------------------------------------------------------------
747
    // 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...
748
    // Description :
749
    //   This function add the content of $p_tarname_add at the end of $p_tarname.
750
    // Parameters :
751
    //   $p_tarname : Name of an existing tar file
752
    //   $p_tarname_add : Name of an existing tar file taht will be added at the end
753
    //                    of $p_tarname.
754
    //   $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
755
    //   $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
756
    //                 extension
757
    // Return Values :
758
    //   List of the files contained in the archive. The field status contains
759
    //   "updated", "not_updated", "added" or "ok" for the files not concerned.
760
    // --------------------------------------------------------------------------------
761
    /**
762
     * @param        $p_tarname
763
     * @param        $p_tarname_add
764
     * @param string $p_mode
765
     * @param string $p_mode_add
766
     *
767
     * @return int
768
     */
769
    function PclTarMerge($p_tarname, $p_tarname_add, $p_mode = '', $p_mode_add = '')
770
    {
771
        TrFctStart(__FILE__, __LINE__, 'PclTarMerge', "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
772
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

Loading history...
773
774
        // ----- Check the parameters
775
        if (('' == $p_tarname) || ('' == $p_tarname_add)) {
776
            // ----- Error log
777
            PclErrorLog(-3, 'Invalid empty archive name');
778
779
            // ----- Return
780
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
781
782
            return PclErrorCode();
783
        }
784
785
        // ----- Extract the tar format from the extension
786
        if (('' === $p_mode) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
787
            if ('' == ($p_mode = PclTarHandleExtension($p_tarname))) {
788
                // ----- Return
789
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
790
791
                return 0;
792
            }
793
        }
794
        if (('' === $p_mode_add) || (('tar' !== $p_mode_add) && ('tgz' !== $p_mode_add))) {
795
            if ('' == ($p_mode_add = PclTarHandleExtension($p_tarname_add))) {
796
                // ----- Return
797
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
798
799
                return 0;
800
            }
801
        }
802
803
        // ----- Clear filecache
804
        clearstatcache();
805
806
        // ----- Check the file size
807 View Code Duplication
        if ((!is_file($p_tarname)) || ((0 != (($v_size = filesize($p_tarname)) % 512)) && ('tar' === $p_mode))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
808
            // ----- Error log
809
            if (!is_file($p_tarname)) {
810
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
811
            } else {
812
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size " . filesize($p_tarname) . '(not a 512 block multiple)');
813
            }
814
815
            // ----- Return
816
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
817
818
            return PclErrorCode();
819
        }
820 View Code Duplication
        if ((!is_file($p_tarname_add))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

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

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

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

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1025
1026
                // ----- Return
1027
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1028
1029
                return PclErrorCode();
1030
            }
1031
1032
            // ----- Return
1033
            TrFctEnd(__FILE__, __LINE__, $v_result);
1034
1035
            return $v_result;
1036
        } // ----- Look for closing uncompressed tar file
1037
        elseif ('tar' === $p_mode) {
1038
            // ----- Close the tarfile
1039
            fclose($p_tar);
1040
        }
1041
1042
        // ----- Return
1043
        TrFctEnd(__FILE__, __LINE__, $v_result);
1044
1045
        return $v_result;
1046
    }
1047
1048
    // --------------------------------------------------------------------------------
1049
1050
    // --------------------------------------------------------------------------------
1051
    // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1052
    // *****                                                        *****
1053
    // *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1054
    // --------------------------------------------------------------------------------
1055
1056
    // --------------------------------------------------------------------------------
1057
    // 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...
1058
    // Description :
1059
    // Parameters :
1060
    //   $p_tarname : Name of the tar file
1061
    //   $p_list : An array containing the file or directory names to add in the tar
1062
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1063
    // Return Values :
1064
    // --------------------------------------------------------------------------------
1065
    /**
1066
     * @param        $p_tarname
1067
     * @param        $p_list
1068
     * @param        $p_mode
1069
     * @param string $p_add_dir
1070
     * @param string $p_remove_dir
1071
     *
1072
     * @return int
1073
     */
1074
    function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir = '', $p_remove_dir = '')
1075
    {
1076
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleCreate', "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1077
        $v_result      = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

Loading history...
1078
        $v_list_detail = [];
1079
1080
        // ----- Check the parameters
1081 View Code Duplication
        if (('' === $p_tarname) || (('tar' !== $p_mode) && ('tgz' !== $p_mode))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1082
            // ----- Error log
1083
            if ('' == $p_tarname) {
1084
                PclErrorLog(-3, 'Invalid empty archive name');
1085
            } else {
1086
                PclErrorLog(-3, "Unknown mode '$p_mode'");
1087
            }
1088
1089
            // ----- Return
1090
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1091
1092
            return PclErrorCode();
1093
        }
1094
1095
        // ----- Look for tar file
1096
        if ('tar' === $p_mode) {
1097
            // ----- Open the tar file
1098
            if (0 == ($p_tar = fopen($p_tarname, 'wb'))) {
1099
                // ----- Error log
1100
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1101
1102
                // ----- Return
1103
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1104
1105
                return PclErrorCode();
1106
            }
1107
1108
            // ----- Call the adding fct inside the tar
1109
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir))) {
1110
                // ----- Call the footer of the tar archive
1111
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1112
            }
1113
1114
            // ----- Close the tarfile
1115
            fclose($p_tar);
1116
        } // ----- Look for tgz file
1117
        else {
1118
            // ----- Open the tar file
1119
            if (0 == ($p_tar = @gzopen($p_tarname, 'wb'))) {
1120
                // ----- Error log
1121
                PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
1122
1123
                // ----- Return
1124
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1125
1126
                return PclErrorCode();
1127
            }
1128
1129
            // ----- Call the adding fct inside the tar
1130
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir))) {
1131
                // ----- Call the footer of the tar archive
1132
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1133
            }
1134
1135
            // ----- Close the tarfile
1136
            gzclose($p_tar);
1137
        }
1138
1139
        // ----- Return
1140
        TrFctEnd(__FILE__, __LINE__, $v_result);
1141
1142
        return $v_result;
1143
    }
1144
1145
    // --------------------------------------------------------------------------------
1146
1147
    // --------------------------------------------------------------------------------
1148
    // 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...
1149
    // Description :
1150
    // Parameters :
1151
    //   $p_tarname : Name of the tar file
1152
    //   $p_list : An array containing the file or directory names to add in the tar
1153
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1154
    // Return Values :
1155
    // --------------------------------------------------------------------------------
1156
    /**
1157
     * @param $p_tarname
1158
     * @param $p_list
1159
     * @param $p_mode
1160
     * @param $p_list_detail
1161
     * @param $p_add_dir
1162
     * @param $p_remove_dir
1163
     *
1164
     * @return int
1165
     */
1166
    function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1167
    {
1168
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAppend', "tar=$p_tarname, list, mode=$p_mode");
1169
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

Loading history...
1170
1171
        // ----- Check the parameters
1172
        if ('' == $p_tarname) {
1173
            // ----- Error log
1174
            PclErrorLog(-3, 'Invalid empty archive name');
1175
1176
            // ----- Return
1177
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1178
1179
            return PclErrorCode();
1180
        }
1181
1182
        clearstatcache();
1183
1184
        // ----- Check the file size
1185 View Code Duplication
        if ((!is_file($p_tarname)) || ((0 != (($v_size = filesize($p_tarname)) % 512)) && ('tar' === $p_mode))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1186
            // ----- Error log
1187
            if (!is_file($p_tarname)) {
1188
                PclErrorLog(-4, "Archive '$p_tarname' does not exist");
1189
            } else {
1190
                PclErrorLog(-6, "Archive '$p_tarname' has invalid size " . filesize($p_tarname) . '(not a 512 block multiple)');
1191
            }
1192
1193
            // ----- Return
1194
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1195
1196
            return PclErrorCode();
1197
        }
1198
1199
        // ----- Look for compressed archive
1200
        if ('tgz' === $p_mode) {
1201
            // ----- Open the file in read mode
1202
            if (0 == ($p_tar = @gzopen($p_tarname, 'rb'))) {
1203
                // ----- Error log
1204
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
1205
1206
                // ----- Return
1207
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1208
1209
                return PclErrorCode();
1210
            }
1211
1212
            // ----- Open a temporary file in write mode
1213
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
1214
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
1215
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
1216
                // ----- Close tar file
1217
                gzclose($p_tar);
1218
1219
                // ----- Error log
1220
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
1221
1222
                // ----- Return
1223
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1224
1225
                return PclErrorCode();
1226
            }
1227
1228
            // ----- Read the first 512 bytes block
1229
            $v_buffer = gzread($p_tar, 512);
1230
1231
            // ----- Read the following blocks but not the last one
1232 View Code Duplication
            if (!gzeof($p_tar)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1233
                TrFctMessage(__FILE__, __LINE__, 3, 'More than one 512 block file');
1234
                $i = 1;
1235
1236
                // ----- Read new 512 block and write the already read
1237
                do {
1238
                    // ----- Write the already read block
1239
                    $v_binary_data = pack('a512', "$v_buffer");
1240
                    gzputs($v_temp_tar, $v_binary_data);
1241
1242
                    ++$i;
1243
                    TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
1244
1245
                    // ----- Read next block
1246
                    $v_buffer = gzread($p_tar, 512);
1247
                } while (!gzeof($p_tar));
1248
1249
                TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
1250
            }
1251
1252
            // ----- Call the adding fct inside the tar
1253 View Code Duplication
            if (1 == ($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1254
                // ----- Call the footer of the tar archive
1255
                $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
1256
            }
1257
1258
            // ----- Close the files
1259
            gzclose($p_tar);
1260
            gzclose($v_temp_tar);
1261
1262
            // ----- Unlink tar file
1263
            if (!@unlink($p_tarname)) {
1264
                // ----- Error log
1265
                PclErrorLog(-11, "Error while deleting archive name $p_tarname");
1266
            }
1267
1268
            // ----- Rename tar file
1269
            if (!@rename($v_temp_tarname, $p_tarname)) {
1270
                // ----- Error log
1271
                PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
1272
1273
                // ----- Return
1274
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1275
1276
                return PclErrorCode();
1277
            }
1278
1279
            // ----- Return
1280
            TrFctEnd(__FILE__, __LINE__, $v_result);
1281
1282
            return $v_result;
1283
        } // ----- Look for uncompressed tar file
1284
        elseif ('tar' === $p_mode) {
1285
            // ----- Open the tar file
1286
            if (0 == ($p_tar = fopen($p_tarname, 'r+b'))) {
1287
                // ----- Error log
1288
                PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
1289
1290
                // ----- Return
1291
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1292
1293
                return PclErrorCode();
1294
            }
1295
1296
            // ----- Go to the beginning of last block
1297
            TrFctMessage(__FILE__, __LINE__, 4, 'Position before :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1298
            fseek($p_tar, $v_size - 512);
1299
            TrFctMessage(__FILE__, __LINE__, 4, 'Position after :' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1300
1301
            // ----- Call the adding fct inside the tar
1302 View Code Duplication
            if (1 == ($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1303
                // ----- Call the footer of the tar archive
1304
                $v_result = PclTarHandleFooter($p_tar, $p_mode);
1305
            }
1306
1307
            // ----- Close the tarfile
1308
            fclose($p_tar);
1309
        } // ----- Look for unknown type
1310
        else {
1311
            // ----- Error log
1312
            PclErrorLog(-3, "Invalid tar mode $p_mode");
1313
1314
            // ----- Return
1315
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1316
1317
            return PclErrorCode();
1318
        }
1319
1320
        // ----- Return
1321
        TrFctEnd(__FILE__, __LINE__, $v_result);
1322
1323
        return $v_result;
1324
    }
1325
1326
    // --------------------------------------------------------------------------------
1327
1328
    // --------------------------------------------------------------------------------
1329
    // 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...
1330
    // Description :
1331
    //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
1332
    //   different from the real path of the file. This is usefull if you want to have PclTar
1333
    //   running in any directory, and memorize relative path from an other directory.
1334
    // Parameters :
1335
    //   $p_tar : File descriptor of the tar archive
1336
    //   $p_list : An array containing the file or directory names to add in the tar
1337
    //   $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
1338
    //   $p_list_detail : list of added files with their properties (specially the status field)
1339
    //   $p_add_dir : Path to add in the filename path archived
1340
    //   $p_remove_dir : Path to remove in the filename path archived
1341
    // Return Values :
1342
    // --------------------------------------------------------------------------------
1343
    /**
1344
     * @param $p_tar
1345
     * @param $p_list
1346
     * @param $p_mode
1347
     * @param $p_list_detail
1348
     * @param $p_add_dir
1349
     * @param $p_remove_dir
1350
     *
1351
     * @return int
1352
     */
1353
    function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
1354
    {
1355
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddList', "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1356
        $v_result = 1;
1357
        $v_header = [];
1358
1359
        // ----- Recuperate the current number of elt in list
1360
        $v_nb = count($p_list_detail);
1361
1362
        // ----- Check the parameters
1363 View Code Duplication
        if (0 == $p_tar) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1364
            // ----- Error log
1365
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1366
1367
            // ----- Return
1368
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1369
1370
            return PclErrorCode();
1371
        }
1372
1373
        // ----- Check the arguments
1374
        if (0 == count($p_list)) {
1375
            // ----- Error log
1376
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1377
1378
            // ----- Return
1379
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1380
1381
            return PclErrorCode();
1382
        }
1383
1384
        // ----- Loop on the files
1385
        for ($j = 0; ($j < count($p_list)) && (1 == $v_result); ++$j) {
1386
            // ----- Recuperate the filename
1387
            $p_filename = $p_list[$j];
1388
1389
            TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
1390
1391
            // ----- Skip empty file names
1392
            if ('' == $p_filename) {
1393
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip empty filename');
1394
                continue;
1395
            }
1396
1397
            // ----- Check the filename
1398
            if (!file_exists($p_filename)) {
1399
                // ----- Error log
1400
                TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
1401
                PclErrorLog(-4, "File '$p_filename' does not exists");
1402
1403
                // ----- Return
1404
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1405
1406
                return PclErrorCode();
1407
            }
1408
1409
            // ----- Check the path length
1410
            if (strlen($p_filename) > 99) {
1411
                // ----- Error log
1412
                PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
1413
1414
                // ----- Return
1415
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1416
1417
                return PclErrorCode();
1418
            }
1419
1420
            TrFctMessage(__FILE__, __LINE__, 4, 'File position before header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1421
1422
            // ----- Add the file
1423 View Code Duplication
            if (1 != ($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1424
                // ----- Return status
1425
                TrFctEnd(__FILE__, __LINE__, $v_result);
1426
1427
                return $v_result;
1428
            }
1429
1430
            // ----- Store the file infos
1431
            $p_list_detail[$v_nb++] = $v_header;
1432
1433
            // ----- Look for directory
1434
            if (is_dir($p_filename)) {
1435
                TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
1436
1437
                // ----- Look for path
1438
                if ('.' !== $p_filename) {
1439
                    $v_path = $p_filename . '/';
1440
                } else {
1441
                    $v_path = '';
1442
                }
1443
1444
                // ----- Read the directory for files and sub-directories
1445
                $p_hdir  = opendir($p_filename);
1446
                $p_hitem = readdir($p_hdir); // '.' directory
0 ignored issues
show
Unused Code introduced by
$p_hitem is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1455
                            // ----- Return status
1456
                            TrFctEnd(__FILE__, __LINE__, $v_result);
1457
1458
                            return $v_result;
1459
                        }
1460
1461
                        // ----- Store the file infos
1462
                        $p_list_detail[$v_nb++] = $v_header;
1463
                    } // ----- Recursive call to PclTarHandleAddFile()
1464
                    else {
1465
                        TrFctMessage(__FILE__, __LINE__, 4, "'" . $v_path . $p_hitem . "' is a directory");
1466
1467
                        // ----- Need an array as parameter
1468
                        $p_temp_list[0] = $v_path . $p_hitem;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$p_temp_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $p_temp_list = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

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

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1470
                    }
1471
                }
1472
1473
                // ----- Free memory for the recursive loop
1474
                unset($p_temp_list, $p_hdir, $p_hitem);
1475
            } else {
1476
                TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1477
            }
1478
        }
1479
1480
        // ----- Return
1481
        TrFctEnd(__FILE__, __LINE__, $v_result);
1482
1483
        return $v_result;
1484
    }
1485
1486
    // --------------------------------------------------------------------------------
1487
1488
    // --------------------------------------------------------------------------------
1489
    // 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...
1490
    // Description :
1491
    // Parameters :
1492
    // Return Values :
1493
    // --------------------------------------------------------------------------------
1494
    /**
1495
     * @param $p_tar
1496
     * @param $p_filename
1497
     * @param $p_mode
1498
     * @param $p_header
1499
     * @param $p_add_dir
1500
     * @param $p_remove_dir
1501
     *
1502
     * @return int
1503
     */
1504
    function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
1505
    {
1506
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleAddFile', "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1507
        $v_result = 1;
0 ignored issues
show
Unused Code introduced by
$v_result is not used, you could remove the assignment.

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

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

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

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

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

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

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

Loading history...
1511
            // ----- Error log
1512
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1513
1514
            // ----- Return
1515
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1516
1517
            return PclErrorCode();
1518
        }
1519
1520
        // ----- Skip empty file names
1521
        if ('' == $p_filename) {
1522
            // ----- Error log
1523
            PclErrorLog(-3, 'Invalid file list parameter (invalid or empty list)');
1524
1525
            // ----- Return
1526
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1527
1528
            return PclErrorCode();
1529
        }
1530
1531
        // ----- Calculate the stored filename
1532
        $v_stored_filename = $p_filename;
1533
        if ('' != $p_remove_dir) {
1534
            if ('/' !== substr($p_remove_dir, -1)) {
1535
                $p_remove_dir .= '/';
1536
            }
1537
1538
            if (('./' === substr($p_filename, 0, 2)) || ('./' === substr($p_remove_dir, 0, 2))) {
1539 View Code Duplication
                if (('./' === substr($p_filename, 0, 2)) && ('./' !== substr($p_remove_dir, 0, 2))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

Loading history...
1543
                    $p_remove_dir = substr($p_remove_dir, 2);
1544
                }
1545
            }
1546
1547
            if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir) {
1548
                $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
1549
                TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
1550
            }
1551
        }
1552
        if ('' != $p_add_dir) {
1553
            if ('/' === substr($p_add_dir, -1)) {
1554
                $v_stored_filename = $p_add_dir . $v_stored_filename;
1555
            } else {
1556
                $v_stored_filename = $p_add_dir . '/' . $v_stored_filename;
1557
            }
1558
            TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
1559
        }
1560
1561
        // ----- Check the path length
1562
        if (strlen($v_stored_filename) > 99) {
1563
            // ----- Error log
1564
            PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");
1565
1566
            // ----- Return
1567
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1568
1569
            return PclErrorCode();
1570
        }
1571
1572
        // ----- Look for a file
1573
        if (is_file($p_filename)) {
1574
            // ----- Open the source file
1575
            if (0 == ($v_file = fopen($p_filename, 'rb'))) {
1576
                // ----- Error log
1577
                PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");
1578
1579
                // ----- Return
1580
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1581
1582
                return PclErrorCode();
1583
            }
1584
1585
            // ----- Call the header generation
1586 View Code Duplication
            if (1 != ($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1587
                // ----- Return status
1588
                TrFctEnd(__FILE__, __LINE__, $v_result);
1589
1590
                return $v_result;
1591
            }
1592
1593
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1594
1595
            // ----- Read the file by 512 octets blocks
1596
            $i = 0;
1597
            while ('' != ($v_buffer = fread($v_file, 512))) {
1598
                $v_binary_data = pack('a512', "$v_buffer");
1599
                if ('tar' === $p_mode) {
1600
                    fwrite($p_tar, $v_binary_data);
1601
                } else {
1602
                    gzputs($p_tar, $v_binary_data);
1603
                }
1604
                ++$i;
1605
            }
1606
            TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
1607
1608
            // ----- Close the file
1609
            fclose($v_file);
1610
1611
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after blocks =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1612
        } // ----- Look for a directory
1613
        else {
1614
            // ----- Call the header generation
1615 View Code Duplication
            if (1 != ($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1616
                // ----- Return status
1617
                TrFctEnd(__FILE__, __LINE__, $v_result);
1618
1619
                return $v_result;
1620
            }
1621
1622
            TrFctMessage(__FILE__, __LINE__, 4, 'File position after header =' . ('tar' === $p_mode ? ftell($p_tar) : gztell($p_tar)));
1623
        }
1624
1625
        // ----- Return
1626
        TrFctEnd(__FILE__, __LINE__, $v_result);
1627
1628
        return $v_result;
1629
    }
1630
1631
    // --------------------------------------------------------------------------------
1632
1633
    // --------------------------------------------------------------------------------
1634
    // 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...
1635
    // Description :
1636
    //   This function creates in the TAR $p_tar, the TAR header for the file
1637
    //   $p_filename.
1638
    //
1639
    //   1. The informations needed to compose the header are recuperated and formatted
1640
    //   2. Two binary strings are composed for the first part of the header, before
1641
    //      and after checksum field.
1642
    //   3. The checksum is calculated from the two binary strings
1643
    //   4. The header is write in the tar file (first binary string, binary string
1644
    //      for checksum and last binary string).
1645
    // Parameters :
1646
    //   $p_tar : a valid file descriptor, opened in write mode,
1647
    //   $p_filename : The name of the file the header is for,
1648
    //   $p_mode : The mode of the archive ("tar" or "tgz").
1649
    //   $p_header : A pointer to a array where will be set the file properties
1650
    // Return Values :
1651
    // --------------------------------------------------------------------------------
1652
    /**
1653
     * @param $p_tar
1654
     * @param $p_filename
1655
     * @param $p_mode
1656
     * @param $p_header
1657
     * @param $p_stored_filename
1658
     *
1659
     * @return int
1660
     */
1661
    function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename)
1662
    {
1663
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleHeader', "tar=$p_tar, file='$p_filename', mode='$p_mode', stored_filename='$p_stored_filename'");
1664
        $v_result = 1;
1665
1666
        // ----- Check the parameters
1667 View Code Duplication
        if ((0 == $p_tar) || ('' == $p_filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1668
            // ----- Error log
1669
            PclErrorLog(-3, 'Invalid file descriptor in file ' . __FILE__ . ', line ' . __LINE__);
1670
1671
            // ----- Return
1672
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1673
1674
            return PclErrorCode();
1675
        }
1676
1677
        // ----- Filename (reduce the path of stored name)
1678
        if ('' == $p_stored_filename) {
1679
            $p_stored_filename = $p_filename;
1680
        }
1681
        $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename);
1682
        TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen " . strlen($v_reduce_filename));
1683
1684
        // ----- Get file info
1685
        $v_info = stat($p_filename);
1686
        $v_uid  = sprintf('%6s ', decoct($v_info[4]));
1687
        $v_gid  = sprintf('%6s ', decoct($v_info[5]));
1688
        TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid");
1689
        $v_perms = sprintf('%6s ', decoct(fileperms($p_filename)));
1690
        TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms");
1691
1692
        // ----- File mtime
1693
        $v_mtime_data = filemtime($p_filename);
1694
        TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data");
1695
        $v_mtime = sprintf('%11s', decoct($v_mtime_data));
1696
1697
        // ----- File typeflag
1698
        // '0' or '\0' is the code for regular file
1699
        // '5' is directory
1700
        if (is_dir($p_filename)) {
1701
            $v_typeflag = '5';
1702
            $v_size     = 0;
1703
        } else {
1704
            $v_typeflag = '';
1705
1706
            // ----- Get the file size
1707
            clearstatcache();
1708
            $v_size = filesize($p_filename);
1709
        }
1710
1711
        TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size");
1712
        $v_size = sprintf('%11s ', decoct($v_size));
1713
1714
        TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag");
1715
1716
        // ----- Linkname
1717
        $v_linkname = '';
1718
1719
        // ----- Magic
1720
        $v_magic = '';
1721
1722
        // ----- Version
1723
        $v_version = '';
1724
1725
        // ----- uname
1726
        $v_uname = '';
1727
1728
        // ----- gname
1729
        $v_gname = '';
1730
1731
        // ----- devmajor
1732
        $v_devmajor = '';
1733
1734
        // ----- devminor
1735
        $v_devminor = '';
1736
1737
        // ----- prefix
1738
        $v_prefix = '';
1739
1740
        // ----- Compose the binary string of the header in two parts arround the checksum position
1741
        $v_binary_data_first = pack('a100a8a8a8a12A12', $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
1742
        $v_binary_data_last  = pack('a1a100a6a2a32a32a8a8a155a12', $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
1743
1744
        // ----- Calculate the checksum
1745
        $v_checksum = 0;
1746
        // ..... First part of the header
1747
        for ($i = 0; $i < 148; ++$i) {
1748
            $v_checksum += ord(substr($v_binary_data_first, $i, 1));
1749
        }
1750
        // ..... Ignore the checksum value and replace it by ' ' (space)
1751
        for ($i = 148; $i < 156; ++$i) {
1752
            $v_checksum += ord(' ');
1753
        }
1754
        // ..... Last part of the header
1755
        for ($i = 156, $j = 0; $i < 512; ++$i, ++$j) {
1756
            $v_checksum += ord(substr($v_binary_data_last, $j, 1));
1757
        }
1758
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
1759
1760
        // ----- Write the first 148 bytes of the header in the archive
1761
        if ('tar' === $p_mode) {
1762
            fwrite($p_tar, $v_binary_data_first, 148);
1763
        } else {
1764
            gzputs($p_tar, $v_binary_data_first, 148);
1765
        }
1766
1767
        // ----- Write the calculated checksum
1768
        $v_checksum    = sprintf('%6s ', decoct($v_checksum));
1769
        $v_binary_data = pack('a8', $v_checksum);
1770
        if ('tar' === $p_mode) {
1771
            fwrite($p_tar, $v_binary_data, 8);
1772
        } else {
1773
            gzputs($p_tar, $v_binary_data, 8);
1774
        }
1775
1776
        // ----- Write the last 356 bytes of the header in the archive
1777
        if ('tar' === $p_mode) {
1778
            fwrite($p_tar, $v_binary_data_last, 356);
1779
        } else {
1780
            gzputs($p_tar, $v_binary_data_last, 356);
1781
        }
1782
1783
        // ----- Set the properties in the header "structure"
1784
        $p_header['filename'] = $v_reduce_filename;
1785
        $p_header['mode']     = $v_perms;
1786
        $p_header['uid']      = $v_uid;
1787
        $p_header['gid']      = $v_gid;
1788
        $p_header['size']     = $v_size;
1789
        $p_header['mtime']    = $v_mtime;
1790
        $p_header['typeflag'] = $v_typeflag;
1791
        $p_header['status']   = 'added';
1792
1793
        // ----- Return
1794
        TrFctEnd(__FILE__, __LINE__, $v_result);
1795
1796
        return $v_result;
1797
    }
1798
1799
    // --------------------------------------------------------------------------------
1800
1801
    // --------------------------------------------------------------------------------
1802
    // 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...
1803
    // Description :
1804
    // Parameters :
1805
    // Return Values :
1806
    // --------------------------------------------------------------------------------
1807
    /**
1808
     * @param $p_tar
1809
     * @param $p_mode
1810
     *
1811
     * @return int
1812
     */
1813
    function PclTarHandleFooter($p_tar, $p_mode)
1814
    {
1815
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleFooter', "tar='$p_tar', p_mode=$p_mode");
1816
        $v_result = 1;
1817
1818
        // ----- Write the last 0 filled block for end of archive
1819
        $v_binary_data = pack('a512', '');
1820
        if ('tar' === $p_mode) {
1821
            fwrite($p_tar, $v_binary_data);
1822
        } else {
1823
            gzputs($p_tar, $v_binary_data);
1824
        }
1825
1826
        // ----- Return
1827
        TrFctEnd(__FILE__, __LINE__, $v_result);
1828
1829
        return $v_result;
1830
    }
1831
1832
    // --------------------------------------------------------------------------------
1833
1834
    // --------------------------------------------------------------------------------
1835
    // 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...
1836
    // Description :
1837
    // Parameters :
1838
    //   $p_tarname : Filename of the tar (or tgz) archive
1839
    //   $p_file_list : An array which contains the list of files to extract, this
1840
    //                  array may be empty when $p_mode is 'complete'
1841
    //   $p_list_detail : An array where will be placed the properties of  each extracted/listed file
1842
    //   $p_mode : 'complete' will extract all files from the archive,
1843
    //             'partial' will look for files in $p_file_list
1844
    //             'list' will only list the files from the archive without any extract
1845
    //   $p_path : Path to add while writing the extracted files
1846
    //   $p_tar_mode : 'tar' for GNU TAR archive, 'tgz' for compressed archive
1847
    //   $p_remove_path : Path to remove (from the file memorized path) while writing the
1848
    //                    extracted files. If the path does not match the file path,
1849
    //                    the file is extracted with its memorized path.
1850
    //                    $p_remove_path does not apply to 'list' mode.
1851
    //                    $p_path and $p_remove_path are commulative.
1852
    // Return Values :
1853
    // --------------------------------------------------------------------------------
1854
    /**
1855
     * @param $p_tarname
1856
     * @param $p_file_list
1857
     * @param $p_list_detail
1858
     * @param $p_mode
1859
     * @param $p_path
1860
     * @param $p_tar_mode
1861
     * @param $p_remove_path
1862
     *
1863
     * @return int
1864
     */
1865
    function PclTarHandleExtract(
1866
        $p_tarname,
1867
        $p_file_list,
1868
        &$p_list_detail,
1869
        $p_mode,
1870
        $p_path,
1871
        $p_tar_mode,
1872
        $p_remove_path
1873
    ) {
1874
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtract', "archive='$p_tarname', list, mode=$p_mode, path=$p_path, tar_mode=$p_tar_mode, remove_path='$p_remove_path'");
1875
        $v_result      = 1;
1876
        $v_nb          = 0;
1877
        $v_extract_all = true;
0 ignored issues
show
Unused Code introduced by
$v_extract_all is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
1879
1880
        // ----- Check the path
1881
        //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...
1882
        if ('' == $p_path) {
1883
            $p_path = './' . $p_path;
1884
        }
1885
1886
        // ----- Look for path to remove format (should end by /)
1887
        if (('' != $p_remove_path) && ('/' !== substr($p_remove_path, -1))) {
1888
            $p_remove_path .= '/';
1889
        }
1890
        $p_remove_path_size = strlen($p_remove_path);
1891
1892
        // ----- Study the mode
1893
        switch ($p_mode) {
1894
            case 'complete':
1895
                // ----- Flag extract of all files
1896
                $v_extract_all = true;
1897
                $v_listing     = false;
1898
                break;
1899
            case 'partial':
1900
                // ----- Flag extract of specific files
1901
                $v_extract_all = false;
1902
                $v_listing     = false;
1903
                break;
1904
            case 'list':
1905
                // ----- Flag list of all files
1906
                $v_extract_all = false;
1907
                $v_listing     = true;
1908
                break;
1909
            default:
1910
                // ----- Error log
1911
                PclErrorLog(-3, "Invalid extract mode ($p_mode)");
1912
1913
                // ----- Return
1914
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1915
1916
                return PclErrorCode();
1917
        }
1918
1919
        // ----- Open the tar file
1920 View Code Duplication
        if ('tar' === $p_tar_mode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1921
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
1922
            $v_tar = fopen($p_tarname, 'rb');
1923
        } else {
1924
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
1925
            $v_tar = @gzopen($p_tarname, 'rb');
1926
        }
1927
1928
        // ----- Check that the archive is open
1929
        if (0 == $v_tar) {
1930
            // ----- Error log
1931
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
1932
1933
            // ----- Return
1934
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
1935
1936
            return PclErrorCode();
1937
        }
1938
1939
        // ----- Read the blocks
1940
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
1941
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
1942
1943
            // ----- Clear cache of file infos
1944
            clearstatcache();
1945
1946
            // ----- Reset extract tag
1947
            $v_extract_file       = false;
0 ignored issues
show
Unused Code introduced by
$v_extract_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

Loading history...
2029
                    && (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path)) {
2030
                    TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2031
                    // ----- Remove the path
2032
                    $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2033
                    TrFctMessage(__FILE__, __LINE__, 3, 'Reslting file is ' . $v_header['filename'] . '');
2034
                }
2035
2036
                // ----- Add the path to the file
2037 View Code Duplication
                if (('./' !== $p_path) && ('/' !== $p_path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2038
                    // ----- Look for the path end '/'
2039
                    while ('/' === substr($p_path, -1)) {
2040
                        TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2041
                        $p_path = substr($p_path, 0, -1);
2042
                        TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2043
                    }
2044
2045
                    // ----- Add the path
2046
                    if ('/' === substr($v_header['filename'], 0, 1)) {
2047
                        $v_header['filename'] = $p_path . $v_header['filename'];
2048
                    } else {
2049
                        $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2050
                    }
2051
                }
2052
2053
                // ----- Trace
2054
                TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2055
2056
                // ----- Check that the file does not exists
2057
                if (file_exists($v_header['filename'])) {
2058
                    TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2059
2060
                    // ----- Look if file is a directory
2061
                    if (is_dir($v_header['filename'])) {
2062
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2063
2064
                        // ----- Change the file status
2065
                        $v_header['status'] = 'already_a_directory';
2066
2067
                        // ----- Skip the extract
2068
                        $v_extraction_stopped = 1;
2069
                        $v_extract_file       = 0;
2070
                    } // ----- Look if file is write protected
2071
                    elseif (!is_writable($v_header['filename'])) {
2072
                        TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2073
2074
                        // ----- Change the file status
2075
                        $v_header['status'] = 'write_protected';
2076
2077
                        // ----- Skip the extract
2078
                        $v_extraction_stopped = 1;
2079
                        $v_extract_file       = 0;
2080
                    }
2081
                    // ----- Look if the extracted file is older
2082
                    /*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...
2083
            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']).")");
2084
2085
            // ----- Change the file status
2086
            $v_header['status'] = "newer_exist";
2087
2088
            // ----- Skip the extract
2089
            $v_extraction_stopped = 1;
2090
            $v_extract_file = 0;
2091
          }*/
2092
                } // ----- Check the directory availability and create it if necessary
2093 View Code Duplication
                else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2094
                    if ('5' == $v_header['typeflag']) {
2095
                        $v_dir_to_check = $v_header['filename'];
2096
                    } elseif (false === strpos($v_header['filename'], '/')) {
2097
                        $v_dir_to_check = '';
2098
                    } else {
2099
                        $v_dir_to_check = dirname($v_header['filename']);
2100
                    }
2101
2102
                    if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2103
                        TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2104
2105
                        // ----- Change the file status
2106
                        $v_header['status'] = 'path_creation_fail';
2107
2108
                        // ----- Skip the extract
2109
                        $v_extraction_stopped = 1;
2110
                        $v_extract_file       = 0;
2111
                    }
2112
                }
2113
2114
                // ----- Do the extraction
2115
                if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2116
                    // ----- Open the destination file in write mode
2117 View Code Duplication
                    if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2118
                        TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2119
2120
                        // ----- Change the file status
2121
                        $v_header['status'] = 'write_error';
2122
2123
                        // ----- Jump to next file
2124
                        TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2125
                        if ('tar' === $p_tar_mode) {
2126
                            fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2127
                        } else {
2128
                            gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2129
                        }
2130
                    } else {
2131
                        TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2132
2133
                        // ----- Read data
2134
                        $n = floor($v_header['size'] / 512);
2135
                        for ($i = 0; $i < $n; ++$i) {
2136
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2137
                            if ('tar' === $p_tar_mode) {
2138
                                $v_content = fread($v_tar, 512);
2139
                            } else {
2140
                                $v_content = gzread($v_tar, 512);
2141
                            }
2142
                            fwrite($v_dest_file, $v_content, 512);
2143
                        }
2144
                        if (0 != ($v_header['size'] % 512)) {
2145
                            TrFctMessage(__FILE__, __LINE__, 3, 'Read last ' . ($v_header['size'] % 512) . ' bytes in a 512 block');
2146
                            if ('tar' === $p_tar_mode) {
2147
                                $v_content = fread($v_tar, 512);
2148
                            } else {
2149
                                $v_content = gzread($v_tar, 512);
2150
                            }
2151
                            fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2152
                        }
2153
2154
                        // ----- Close the destination file
2155
                        fclose($v_dest_file);
2156
2157
                        // ----- Change the file mode, mtime
2158
                        touch($v_header['filename'], $v_header['mtime']);
2159
                        //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...
2160
                    }
2161
2162
                    // ----- Check the file size
2163
                    clearstatcache();
2164
                    if (filesize($v_header['filename']) != $v_header['size']) {
2165
                        // ----- Close the archive file
2166
                        if ('tar' === $p_tar_mode) {
2167
                            fclose($v_tar);
2168
                        } else {
2169
                            gzclose($v_tar);
2170
                        }
2171
2172
                        // ----- Error log
2173
                        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
Bug introduced by
The variable $v_filename does not exist. Did you forget to declare it?

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

Loading history...
2174
2175
                        // ----- Return
2176
                        TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2177
2178
                        return PclErrorCode();
2179
                    }
2180
2181
                    // ----- Trace
2182
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2183 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2184
                    TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file ' . $v_header['filename'] . ' skipped.');
2185
2186
                    // ----- Jump to next file
2187
                    TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2188
                    if ('tar' === $p_tar_mode) {
2189
                        fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2190
                    } else {
2191
                        gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2192
                    }
2193
                }
2194
            } // ----- Look for file that is not to be unzipped
2195 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2196
                // ----- Trace
2197
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file ' . $v_header['filename'] . '');
2198
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2199
2200
                // ----- Jump to next file
2201
                if ('tar' === $p_tar_mode) {
2202
                    fseek($v_tar, ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
2203
                } else {
2204
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2205
                }
2206
2207
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2208
            }
2209
2210
            if ('tar' === $p_tar_mode) {
2211
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
2214
            }
2215
2216
            // ----- File name and properties are logged if listing mode or file is extracted
2217
            if ($v_listing || $v_extract_file || $v_extraction_stopped) {
2218
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2219
2220
                // ----- Log extracted files
2221
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2222
                    $v_file_dir = '';
2223
                }
2224 View Code Duplication
                if (('/' === substr($v_header['filename'], 0, 1)) && ('' == $v_file_dir)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2225
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
$v_file_dir is not used, you could remove the assignment.

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

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

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

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

Loading history...
2226
                }
2227
2228
                // ----- Add the array describing the file into the list
2229
                $p_list_detail[$v_nb] = $v_header;
2230
2231
                // ----- Increment
2232
                ++$v_nb;
2233
            }
2234
        }
2235
2236
        // ----- Close the tarfile
2237
        if ('tar' === $p_tar_mode) {
2238
            fclose($v_tar);
2239
        } else {
2240
            gzclose($v_tar);
2241
        }
2242
2243
        // ----- Return
2244
        TrFctEnd(__FILE__, __LINE__, $v_result);
2245
2246
        return $v_result;
2247
    }
2248
2249
    // --------------------------------------------------------------------------------
2250
2251
    // --------------------------------------------------------------------------------
2252
    // 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...
2253
    // Description :
2254
    //   Extract the files which are at the indexes specified. If the 'file' at the
2255
    //   index is a directory, the directory only is created, not all the files stored
2256
    //   for that directory.
2257
    // Parameters :
2258
    //   $p_index_string : String of indexes of files to extract. The form of the
2259
    //                     string is "0,4-6,8-12" with only numbers and '-' for
2260
    //                     for range, and ',' to separate ranges. No spaces or ';'
2261
    //                     are allowed.
2262
    // Return Values :
2263
    // --------------------------------------------------------------------------------
2264
    /**
2265
     * @param $p_tarname
2266
     * @param $p_index_string
2267
     * @param $p_list_detail
2268
     * @param $p_path
2269
     * @param $p_remove_path
2270
     * @param $p_tar_mode
2271
     *
2272
     * @return int
2273
     */
2274
    function PclTarHandleExtractByIndexList(
2275
        $p_tarname,
2276
        $p_index_string,
2277
        &$p_list_detail,
2278
        $p_path,
2279
        $p_remove_path,
2280
        $p_tar_mode
2281
    ) {
2282
        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");
2283
        $v_result = 1;
2284
        $v_nb     = 0;
0 ignored issues
show
Unused Code introduced by
$v_nb is not used, you could remove the assignment.

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

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

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

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

Loading history...
2285
2286
        // ----- TBC : I should check the string by a regexp
2287
2288
        // ----- Check the path
2289
        if (('' == $p_path)
2290
            || (('/' !== substr($p_path, 0, 1)) && ('../' !== substr($p_path, 0, 3))
2291
                && ('./' !== substr($p_path, 0, 2)))) {
2292
            $p_path = './' . $p_path;
2293
        }
2294
2295
        // ----- Look for path to remove format (should end by /)
2296
        if (('' != $p_remove_path) && ('/' !== substr($p_remove_path, -1))) {
2297
            $p_remove_path .= '/';
2298
        }
2299
        $p_remove_path_size = strlen($p_remove_path);
0 ignored issues
show
Unused Code introduced by
$p_remove_path_size is not used, you could remove the assignment.

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

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

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

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

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

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

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

Loading history...
2303
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2304
            $v_tar = @fopen($p_tarname, 'rb');
2305
        } else {
2306
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2307
            $v_tar = @gzopen($p_tarname, 'rb');
2308
        }
2309
2310
        // ----- Check that the archive is open
2311
        if (0 == $v_tar) {
2312
            // ----- Error log
2313
            PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
2314
2315
            // ----- Return
2316
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2317
2318
            return PclErrorCode();
2319
        }
2320
2321
        // ----- Manipulate the index list
2322
        $v_list = explode(',', $p_index_string);
2323
        sort($v_list);
2324
2325
        // ----- Loop on the index list
2326
        $v_index = 0;
2327
        for ($i = 0; ($i < count($v_list)) && $v_result; ++$i) {
2328
            TrFctMessage(__FILE__, __LINE__, 3, "Looking for index part '$v_list[$i]'");
2329
2330
            // ----- Extract range
2331
            $v_index_list      = explode('-', $v_list[$i]);
2332
            $v_size_index_list = count($v_index_list);
2333
            if (1 == $v_size_index_list) {
2334
                TrFctMessage(__FILE__, __LINE__, 3, "Only one index '$v_index_list[0]'");
2335
2336
                // ----- Do the extraction
2337
                $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);
2338
            } elseif (2 == $v_size_index_list) {
2339
                TrFctMessage(__FILE__, __LINE__, 3, "Two indexes '$v_index_list[0]' and '$v_index_list[1]'");
2340
2341
                // ----- Do the extraction
2342
                $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);
2343
            }
2344
        }
2345
2346
        // ----- Close the tarfile
2347
        if ('tar' === $p_tar_mode) {
2348
            fclose($v_tar);
2349
        } else {
2350
            gzclose($v_tar);
2351
        }
2352
2353
        // ----- Return
2354
        TrFctEnd(__FILE__, __LINE__, $v_result);
2355
2356
        return $v_result;
2357
    }
2358
2359
    // --------------------------------------------------------------------------------
2360
2361
    // --------------------------------------------------------------------------------
2362
    // 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...
2363
    // Description :
2364
    // Parameters :
2365
    // Return Values :
2366
    // --------------------------------------------------------------------------------
2367
    /**
2368
     * @param $p_tar
2369
     * @param $p_index_current
2370
     * @param $p_index_start
2371
     * @param $p_index_stop
2372
     * @param $p_list_detail
2373
     * @param $p_path
2374
     * @param $p_remove_path
2375
     * @param $p_tar_mode
2376
     *
2377
     * @return int
2378
     */
2379
    function PclTarHandleExtractByIndex(
2380
        $p_tar,
2381
        &$p_index_current,
2382
        $p_index_start,
2383
        $p_index_stop,
2384
        &$p_list_detail,
2385
        $p_path,
2386
        $p_remove_path,
2387
        $p_tar_mode
2388
    ) {
2389
        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");
2390
        $v_result = 1;
2391
        $v_nb     = 0;
0 ignored issues
show
Unused Code introduced by
$v_nb is not used, you could remove the assignment.

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

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

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

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

Loading history...
2392
2393
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2394
        $v_tar = $p_tar;
2395
2396
        // ----- Look the number of elements already in $p_list_detail
2397
        $v_nb = count($p_list_detail);
2398
2399
        // ----- Read the blocks
2400
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
2401
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next file ...');
2402
            TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
2403
2404
            if ($p_index_current > $p_index_stop) {
2405
                TrFctMessage(__FILE__, __LINE__, 2, 'Stop extraction, past stop index');
2406
                break;
2407
            }
2408
2409
            // ----- Clear cache of file infos
2410
            clearstatcache();
2411
2412
            // ----- Reset extract tag
2413
            $v_extract_file       = false;
0 ignored issues
show
Unused Code introduced by
$v_extract_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
2415
2416
            // ----- Read the 512 bytes header
2417
            if ('tar' === $p_tar_mode) {
2418
                $v_binary_data = fread($v_tar, 512);
2419
            } else {
2420
                $v_binary_data = gzread($v_tar, 512);
2421
            }
2422
2423
            // ----- Read the header properties
2424
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
2425
                // ----- Return
2426
                TrFctEnd(__FILE__, __LINE__, $v_result);
2427
2428
                return $v_result;
2429
            }
2430
2431
            // ----- Look for empty blocks to skip
2432
            if ('' == $v_header['filename']) {
2433
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2434
                continue;
2435
            }
2436
2437
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2438
2439
            // ----- Look if file is in the range to be extracted
2440
            if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop)) {
2441
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is in the range to be extracted');
2442
                $v_extract_file = true;
2443
            } else {
2444
                TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' is out of the range');
2445
                $v_extract_file = false;
2446
            }
2447
2448
            // ----- Look if this file need to be extracted
2449
            if ($v_extract_file) {
2450
                if (1 != ($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode))) {
2451
                    // ----- Return
2452
                    TrFctEnd(__FILE__, __LINE__, $v_result);
2453
2454
                    return $v_result;
2455
                }
2456
            } // ----- Look for file that is not to be extracted
2457 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2458
                // ----- Trace
2459
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump file ' . $v_header['filename'] . '');
2460
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2461
2462
                // ----- Jump to next file
2463
                if ('tar' === $p_tar_mode) {
2464
                    fseek($v_tar, ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) + (ceil($v_header['size'] / 512) * 512));
2465
                } else {
2466
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2467
                }
2468
2469
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2470
            }
2471
2472
            if ('tar' === $p_tar_mode) {
2473
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
2476
            }
2477
2478
            // ----- File name and properties are logged if listing mode or file is extracted
2479
            if ($v_extract_file) {
2480
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2481
2482
                // ----- Log extracted files
2483
                if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
2484
                    $v_file_dir = '';
2485
                }
2486 View Code Duplication
                if (('/' === substr($v_header['filename'], 0, 1)) && ('' === $v_file_dir)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2487
                    $v_file_dir = '/';
0 ignored issues
show
Unused Code introduced by
$v_file_dir is not used, you could remove the assignment.

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

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

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

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

Loading history...
2488
                }
2489
2490
                // ----- Add the array describing the file into the list
2491
                $p_list_detail[$v_nb] = $v_header;
2492
2493
                // ----- Increment
2494
                ++$v_nb;
2495
            }
2496
2497
            // ----- Increment the current file index
2498
            ++$p_index_current;
2499
        }
2500
2501
        // ----- Return
2502
        TrFctEnd(__FILE__, __LINE__, $v_result);
2503
2504
        return $v_result;
2505
    }
2506
2507
    // --------------------------------------------------------------------------------
2508
2509
    // --------------------------------------------------------------------------------
2510
    // 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...
2511
    // Description :
2512
    // Parameters :
2513
    // Return Values :
2514
    // --------------------------------------------------------------------------------
2515
    /**
2516
     * @param $p_tar
2517
     * @param $v_header
2518
     * @param $p_path
2519
     * @param $p_remove_path
2520
     * @param $p_tar_mode
2521
     *
2522
     * @return int
2523
     */
2524
    function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
2525
    {
2526
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtractFile', "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
2527
        $v_result = 1;
2528
2529
        // TBC : I should replace all $v_tar by $p_tar in this function ....
2530
        $v_tar          = $p_tar;
2531
        $v_extract_file = 1;
2532
2533
        $p_remove_path_size = strlen($p_remove_path);
2534
2535
        // ----- Look for path to remove
2536 View Code Duplication
        if (('' != $p_remove_path) && (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2537
            TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file " . $v_header['filename'] . '');
2538
            // ----- Remove the path
2539
            $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
2540
            TrFctMessage(__FILE__, __LINE__, 3, 'Resulting file is ' . $v_header['filename'] . '');
2541
        }
2542
2543
        // ----- Add the path to the file
2544 View Code Duplication
        if (('./' !== $p_path) && ('/' !== $p_path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2545
            // ----- Look for the path end '/'
2546
            while ('/' === substr($p_path, -1)) {
2547
                TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2548
                $p_path = substr($p_path, 0, -1);
2549
                TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2550
            }
2551
2552
            // ----- Add the path
2553
            if ('/' === substr($v_header['filename'], 0, 1)) {
2554
                $v_header['filename'] = $p_path . $v_header['filename'];
2555
            } else {
2556
                $v_header['filename'] = $p_path . '/' . $v_header['filename'];
2557
            }
2558
        }
2559
2560
        // ----- Trace
2561
        TrFctMessage(__FILE__, __LINE__, 2, 'Extracting file (with path) ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2562
2563
        // ----- Check that the file does not exists
2564
        if (file_exists($v_header['filename'])) {
2565
            TrFctMessage(__FILE__, __LINE__, 2, 'File ' . $v_header['filename'] . ' already exists');
2566
2567
            // ----- Look if file is a directory
2568
            if (is_dir($v_header['filename'])) {
2569
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is a directory');
2570
2571
                // ----- Change the file status
2572
                $v_header['status'] = 'already_a_directory';
2573
2574
                // ----- Skip the extract
2575
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

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

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

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

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

Loading history...
2576
                $v_extract_file       = 0;
2577
            } // ----- Look if file is write protected
2578
            elseif (!is_writable($v_header['filename'])) {
2579
                TrFctMessage(__FILE__, __LINE__, 2, 'Existing file ' . $v_header['filename'] . ' is write protected');
2580
2581
                // ----- Change the file status
2582
                $v_header['status'] = 'write_protected';
2583
2584
                // ----- Skip the extract
2585
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

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

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

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

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

Loading history...
2586
                $v_extract_file       = 0;
2587
            } // ----- Look if the extracted file is older
2588
            elseif (filemtime($v_header['filename']) > $v_header['mtime']) {
2589
                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']) . ')');
2590
2591
                // ----- Change the file status
2592
                $v_header['status'] = 'newer_exist';
2593
2594
                // ----- Skip the extract
2595
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

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

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

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

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

Loading history...
2596
                $v_extract_file       = 0;
2597
            }
2598
        } // ----- Check the directory availability and create it if necessary
2599 View Code Duplication
        else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2600
            if ('5' == $v_header['typeflag']) {
2601
                $v_dir_to_check = $v_header['filename'];
2602
            } elseif (false === strpos($v_header['filename'], '/')) {
2603
                $v_dir_to_check = '';
2604
            } else {
2605
                $v_dir_to_check = dirname($v_header['filename']);
2606
            }
2607
2608
            if (1 != ($v_result = PclTarHandlerDirCheck($v_dir_to_check))) {
2609
                TrFctMessage(__FILE__, __LINE__, 2, 'Unable to create path for ' . $v_header['filename'] . '');
2610
2611
                // ----- Change the file status
2612
                $v_header['status'] = 'path_creation_fail';
2613
2614
                // ----- Skip the extract
2615
                $v_extraction_stopped = 1;
0 ignored issues
show
Unused Code introduced by
$v_extraction_stopped is not used, you could remove the assignment.

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

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

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

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

Loading history...
2616
                $v_extract_file       = 0;
2617
            }
2618
        }
2619
2620
        // ----- Do the real bytes extraction (if not a directory)
2621
        if ($v_extract_file && ('5' != $v_header['typeflag'])) {
2622
            // ----- Open the destination file in write mode
2623 View Code Duplication
            if (0 == ($v_dest_file = @fopen($v_header['filename'], 'wb'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2624
                TrFctMessage(__FILE__, __LINE__, 2, 'Error while opening ' . $v_header['filename'] . ' in write binary mode');
2625
2626
                // ----- Change the file status
2627
                $v_header['status'] = 'write_error';
2628
2629
                // ----- Jump to next file
2630
                TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2631
                if ('tar' === $p_tar_mode) {
2632
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2633
                } else {
2634
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2635
                }
2636
            } else {
2637
                TrFctMessage(__FILE__, __LINE__, 2, 'Start extraction of ' . $v_header['filename'] . '');
2638
2639
                // ----- Read data
2640
                $n = floor($v_header['size'] / 512);
2641
                for ($i = 0; $i < $n; ++$i) {
2642
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2643
                    if ('tar' === $p_tar_mode) {
2644
                        $v_content = fread($v_tar, 512);
2645
                    } else {
2646
                        $v_content = gzread($v_tar, 512);
2647
                    }
2648
                    fwrite($v_dest_file, $v_content, 512);
2649
                }
2650
                if (0 != ($v_header['size'] % 512)) {
2651
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read last ' . ($v_header['size'] % 512) . ' bytes in a 512 block');
2652
                    if ('tar' === $p_tar_mode) {
2653
                        $v_content = fread($v_tar, 512);
2654
                    } else {
2655
                        $v_content = gzread($v_tar, 512);
2656
                    }
2657
                    fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
2658
                }
2659
2660
                // ----- Close the destination file
2661
                fclose($v_dest_file);
2662
2663
                // ----- Change the file mode, mtime
2664
                touch($v_header['filename'], $v_header['mtime']);
2665
                //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...
2666
            }
2667
2668
            // ----- Check the file size
2669
            clearstatcache();
2670
            if (filesize($v_header['filename']) != $v_header['size']) {
2671
                // ----- Error log
2672
                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
Bug introduced by
The variable $v_filename does not exist. Did you forget to declare it?

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

Loading history...
2673
2674
                // ----- Return
2675
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2676
2677
                return PclErrorCode();
2678
            }
2679
2680
            // ----- Trace
2681
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction done');
2682 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2683
            TrFctMessage(__FILE__, __LINE__, 2, 'Extraction of file ' . $v_header['filename'] . ' skipped.');
2684
2685
            // ----- Jump to next file
2686
            TrFctMessage(__FILE__, __LINE__, 2, 'Jump to next file');
2687
            if ('tar' === $p_tar_mode) {
2688
                fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2689
            } else {
2690
                gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2691
            }
2692
        }
2693
2694
        // ----- Return
2695
        TrFctEnd(__FILE__, __LINE__, $v_result);
2696
2697
        return $v_result;
2698
    }
2699
2700
    // --------------------------------------------------------------------------------
2701
2702
    // --------------------------------------------------------------------------------
2703
    // 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...
2704
    // Description :
2705
    // Parameters :
2706
    // Return Values :
2707
    // --------------------------------------------------------------------------------
2708
    /**
2709
     * @param $p_tarname
2710
     * @param $p_file_list
2711
     * @param $p_list_detail
2712
     * @param $p_tar_mode
2713
     *
2714
     * @return int
2715
     */
2716
    function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
2717
    {
2718
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleDelete', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2719
        $v_result = 1;
2720
        $v_nb     = 0;
2721
2722
        // ----- Look for regular tar file
2723 View Code Duplication
        if ('tar' === $p_tar_mode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2724
            // ----- Open file
2725
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2726
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2727
                // ----- Error log
2728
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2729
2730
                // ----- Return
2731
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2732
2733
                return PclErrorCode();
2734
            }
2735
2736
            // ----- Open a temporary file in write mode
2737
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2738
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2739
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
2740
                // ----- Close tar file
2741
                fclose($v_tar);
2742
2743
                // ----- Error log
2744
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2745
2746
                // ----- Return
2747
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2748
2749
                return PclErrorCode();
2750
            }
2751
        } // ----- Look for compressed tar file
2752
        else {
2753
            // ----- Open the file in read mode
2754
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2755
            if (0 == ($v_tar = @gzopen($p_tarname, 'rb'))) {
2756
                // ----- Error log
2757
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2758
2759
                // ----- Return
2760
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2761
2762
                return PclErrorCode();
2763
            }
2764
2765
            // ----- Open a temporary file in write mode
2766
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2767
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2768
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
2769
                // ----- Close tar file
2770
                gzclose($v_tar);
2771
2772
                // ----- Error log
2773
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2774
2775
                // ----- Return
2776
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2777
2778
                return PclErrorCode();
2779
            }
2780
        }
2781
2782
        // ----- Read the blocks
2783
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
2784
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
2785
2786
            // ----- Clear cache of file infos
2787
            clearstatcache();
2788
2789
            // ----- Reset delete tag
2790
            $v_delete_file = false;
0 ignored issues
show
Unused Code introduced by
$v_delete_file is not used, you could remove the assignment.

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

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

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

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

Loading history...
2791
2792
            // ----- Read the first 512 block header
2793
            if ('tar' === $p_tar_mode) {
2794
                $v_binary_data = fread($v_tar, 512);
2795
            } else {
2796
                $v_binary_data = gzread($v_tar, 512);
2797
            }
2798
2799
            // ----- Read the header properties
2800 View Code Duplication
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2801
                // ----- Close the archive file
2802
                if ('tar' === $p_tar_mode) {
2803
                    fclose($v_tar);
2804
                    fclose($v_temp_tar);
2805
                } else {
2806
                    gzclose($v_tar);
2807
                    gzclose($v_temp_tar);
2808
                }
2809
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
2810
2811
                // ----- Return
2812
                TrFctEnd(__FILE__, __LINE__, $v_result);
2813
2814
                return $v_result;
2815
            }
2816
2817
            // ----- Look for empty blocks to skip
2818
            if ('' == $v_header['filename']) {
2819
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
2820
                continue;
2821
            }
2822
2823
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
2824
2825
            // ----- Look for filenames to delete
2826
            for ($i = 0, $v_delete_file = false; ($i < count($p_file_list)) && (!$v_delete_file); ++$i) {
2827
                // ----- Compare the file names
2828
                //        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...
2829
                if (($v_len = strcmp($p_file_list[$i], $v_header['filename'])) <= 0) {
2830
                    if (0 == $v_len) {
2831
                        TrFctMessage(__FILE__, __LINE__, 3, 'Found that ' . $v_header['filename'] . ' need to be deleted');
2832
                        $v_delete_file = true;
2833
                    } else {
2834
                        TrFctMessage(__FILE__, __LINE__, 3, 'Look if ' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2835
                        if ('/' === substr($v_header['filename'], strlen($p_file_list[$i]), 1)) {
2836
                            TrFctMessage(__FILE__, __LINE__, 3, '' . $v_header['filename'] . " is a file in $p_file_list[$i]");
2837
                            $v_delete_file = true;
2838
                        }
2839
                    }
2840
                }
2841
            }
2842
2843
            // ----- Copy files that do not need to be deleted
2844
            if (!$v_delete_file) {
2845
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
2846
2847
                // ----- Write the file header
2848
                if ('tar' === $p_tar_mode) {
2849
                    fwrite($v_temp_tar, $v_binary_data, 512);
2850
                } else {
2851
                    gzputs($v_temp_tar, $v_binary_data, 512);
2852
                }
2853
2854
                // ----- Write the file data
2855
                $n = ceil($v_header['size'] / 512);
2856
                for ($i = 0; $i < $n; ++$i) {
2857
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($i + 1));
2858
                    if ('tar' === $p_tar_mode) {
2859
                        $v_content = fread($v_tar, 512);
2860
                        fwrite($v_temp_tar, $v_content, 512);
2861
                    } else {
2862
                        $v_content = gzread($v_tar, 512);
2863
                        gzwrite($v_temp_tar, $v_content, 512);
2864
                    }
2865
                }
2866
2867
                // ----- File name and properties are logged if listing mode or file is extracted
2868
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
2869
2870
                // ----- Add the array describing the file into the list
2871
                $p_list_detail[$v_nb]           = $v_header;
2872
                $p_list_detail[$v_nb]['status'] = 'ok';
2873
2874
                // ----- Increment
2875
                ++$v_nb;
2876
            } // ----- Look for file that is to be deleted
2877 View Code Duplication
            else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2878
                // ----- Trace
2879
                TrFctMessage(__FILE__, __LINE__, 2, 'Start deletion of ' . $v_header['filename'] . '');
2880
                TrFctMessage(__FILE__, __LINE__, 4, 'Position avant jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2881
2882
                // ----- Jump to next file
2883
                if ('tar' === $p_tar_mode) {
2884
                    fseek($v_tar, ftell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2885
                } else {
2886
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_header['size'] / 512) * 512));
2887
                }
2888
2889
                TrFctMessage(__FILE__, __LINE__, 4, 'Position après jump [' . ('tar' === $p_tar_mode ? ftell($v_tar) : gztell($v_tar)) . ']');
2890
            }
2891
2892
            // ----- Look for end of file
2893
            if ('tar' === $p_tar_mode) {
2894
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
2897
            }
2898
        }
2899
2900
        // ----- Write the last empty buffer
2901
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
2902
2903
        // ----- Close the tarfile
2904
        if ('tar' === $p_tar_mode) {
2905
            fclose($v_tar);
2906
            fclose($v_temp_tar);
2907
        } else {
2908
            gzclose($v_tar);
2909
            gzclose($v_temp_tar);
2910
        }
2911
2912
        // ----- Unlink tar file
2913
        if (!@unlink($p_tarname)) {
2914
            // ----- Error log
2915
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
2916
        }
2917
2918
        // ----- Rename tar file
2919
        if (!@rename($v_temp_tarname, $p_tarname)) {
2920
            // ----- Error log
2921
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
2922
2923
            // ----- Return
2924
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2925
2926
            return PclErrorCode();
2927
        }
2928
2929
        // ----- Return
2930
        TrFctEnd(__FILE__, __LINE__, $v_result);
2931
2932
        return $v_result;
2933
    }
2934
2935
    // --------------------------------------------------------------------------------
2936
2937
    // --------------------------------------------------------------------------------
2938
    // 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...
2939
    // Description :
2940
    // Parameters :
2941
    // Return Values :
2942
    // --------------------------------------------------------------------------------
2943
    /**
2944
     * @param $p_tarname
2945
     * @param $p_file_list
2946
     * @param $p_list_detail
2947
     * @param $p_tar_mode
2948
     * @param $p_add_dir
2949
     * @param $p_remove_dir
2950
     *
2951
     * @return int
2952
     */
2953
    function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
2954
    {
2955
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleUpdate', "archive='$p_tarname', list, tar_mode=$p_tar_mode");
2956
        $v_result     = 1;
2957
        $v_nb         = 0;
2958
        $v_found_list = [];
2959
2960
        // ----- Look for regular tar file
2961 View Code Duplication
        if ('tar' === $p_tar_mode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2962
            // ----- Open file
2963
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in binary read mode');
2964
            if (0 == ($v_tar = @fopen($p_tarname, 'rb'))) {
2965
                // ----- Error log
2966
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2967
2968
                // ----- Return
2969
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2970
2971
                return PclErrorCode();
2972
            }
2973
2974
            // ----- Open a temporary file in write mode
2975
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
2976
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
2977
            if (0 == ($v_temp_tar = @fopen($v_temp_tarname, 'wb'))) {
2978
                // ----- Close tar file
2979
                fclose($v_tar);
2980
2981
                // ----- Error log
2982
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
2983
2984
                // ----- Return
2985
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2986
2987
                return PclErrorCode();
2988
            }
2989
        } // ----- Look for compressed tar file
2990
        else {
2991
            // ----- Open the file in read mode
2992
            TrFctMessage(__FILE__, __LINE__, 3, 'Open file in gzip binary read mode');
2993
            if (0 == ($v_tar = @gzopen($p_tarname, 'rb'))) {
2994
                // ----- Error log
2995
                PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
2996
2997
                // ----- Return
2998
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
2999
3000
                return PclErrorCode();
3001
            }
3002
3003
            // ----- Open a temporary file in write mode
3004
            $v_temp_tarname = uniqid('pcltar-', true) . '.tmp';
3005
            TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
3006
            if (0 == ($v_temp_tar = @gzopen($v_temp_tarname, 'wb'))) {
3007
                // ----- Close tar file
3008
                gzclose($v_tar);
3009
3010
                // ----- Error log
3011
                PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
3012
3013
                // ----- Return
3014
                TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3015
3016
                return PclErrorCode();
3017
            }
3018
        }
3019
3020
        // ----- Prepare the list of files
3021
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3022
            // ----- Reset the found list
3023
            $v_found_list[$i] = 0;
3024
3025
            // ----- Calculate the stored filename
3026
            $v_stored_list[$i] = $p_file_list[$i];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$v_stored_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $v_stored_list = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
3027
            if ('' != $p_remove_dir) {
3028
                if ('/' !== substr($p_file_list[$i], -1)) {
3029
                    $p_remove_dir .= '/';
3030
                }
3031
3032
                if (substr($p_file_list[$i], 0, strlen($p_remove_dir)) == $p_remove_dir) {
3033
                    $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
0 ignored issues
show
Bug introduced by
The variable $v_stored_list does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
3034
                    TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3035
                }
3036
            }
3037
            if ('' != $p_add_dir) {
3038
                if ('/' === substr($p_add_dir, -1)) {
3039
                    $v_stored_list[$i] = $p_add_dir . $v_stored_list[$i];
3040
                } else {
3041
                    $v_stored_list[$i] = $p_add_dir . '/' . $v_stored_list[$i];
3042
                }
3043
                TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
3044
            }
3045
            $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
3046
            TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
3047
        }
3048
3049
        // ----- Update file cache
3050
        clearstatcache();
3051
3052
        // ----- Read the blocks
3053
        while (!($v_end_of_file = ('tar' === $p_tar_mode ? feof($v_tar) : gzeof($v_tar)))) {
3054
            TrFctMessage(__FILE__, __LINE__, 3, 'Looking for next header ...');
3055
3056
            // ----- Clear cache of file infos
3057
            clearstatcache();
3058
3059
            // ----- Reset current found filename
3060
            $v_current_filename = '';
3061
3062
            // ----- Reset delete tag
3063
            $v_delete_file = false;
0 ignored issues
show
Unused Code introduced by
$v_delete_file is not used, you could remove the assignment.

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

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

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

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

Loading history...
3064
3065
            // ----- Read the first 512 block header
3066
            if ('tar' === $p_tar_mode) {
3067
                $v_binary_data = fread($v_tar, 512);
3068
            } else {
3069
                $v_binary_data = gzread($v_tar, 512);
3070
            }
3071
3072
            // ----- Read the header properties
3073 View Code Duplication
            if (1 != ($v_result = PclTarHandleReadHeader($v_binary_data, $v_header))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3074
                // ----- Close the archive file
3075
                if ('tar' === $p_tar_mode) {
3076
                    fclose($v_tar);
3077
                    fclose($v_temp_tar);
3078
                } else {
3079
                    gzclose($v_tar);
3080
                    gzclose($v_temp_tar);
3081
                }
3082
                @unlink($v_temp_tarname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3083
3084
                // ----- Return
3085
                TrFctEnd(__FILE__, __LINE__, $v_result);
3086
3087
                return $v_result;
3088
            }
3089
3090
            // ----- Look for empty blocks to skip
3091
            if ('' == $v_header['filename']) {
3092
                TrFctMessage(__FILE__, __LINE__, 2, 'Empty block found. End of archive?');
3093
                continue;
3094
            }
3095
3096
            TrFctMessage(__FILE__, __LINE__, 2, 'Found file ' . $v_header['filename'] . ", size '" . $v_header['size'] . "'");
3097
3098
            // ----- Look for filenames to update
3099
            for ($i = 0, $v_update_file = false, $v_found_file = false; ($i < count($v_stored_list)) && (!$v_update_file); ++$i) {
3100
                TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
3101
3102
                // ----- Compare the file names
3103
                if ($v_stored_list[$i] == $v_header['filename']) {
3104
                    TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
3105
                    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])));
3106
                    TrFctMessage(__FILE__, __LINE__, 3, 'Archived mtime=' . $v_header['mtime'] . ' ' . date('l dS of F Y h:i:s A', $v_header['mtime']));
3107
3108
                    // ----- Store found informations
3109
                    $v_found_file       = true;
3110
                    $v_current_filename = $p_file_list[$i];
3111
3112
                    // ----- Look if the file need to be updated
3113
                    if (filemtime($p_file_list[$i]) > $v_header['mtime']) {
3114
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
3115
                        $v_update_file = true;
3116
                    } else {
3117
                        TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
3118
                        $v_update_file = false;
3119
                    }
3120
3121
                    // ----- Flag the name in order not to add the file at the end
3122
                    $v_found_list[$i] = 1;
3123
                } else {
3124
                    TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not " . $v_header['filename'] . '');
3125
                }
3126
            }
3127
3128
            // ----- Copy files that do not need to be updated
3129
            if (!$v_update_file) {
3130
                TrFctMessage(__FILE__, __LINE__, 2, 'Keep file ' . $v_header['filename'] . '');
3131
3132
                // ----- Write the file header
3133
                if ('tar' === $p_tar_mode) {
3134
                    fwrite($v_temp_tar, $v_binary_data, 512);
3135
                } else {
3136
                    gzputs($v_temp_tar, $v_binary_data, 512);
3137
                }
3138
3139
                // ----- Write the file data
3140
                $n = ceil($v_header['size'] / 512);
3141
                for ($j = 0; $j < $n; ++$j) {
3142
                    TrFctMessage(__FILE__, __LINE__, 3, 'Read complete 512 bytes block number ' . ($j + 1));
3143
                    if ('tar' === $p_tar_mode) {
3144
                        $v_content = fread($v_tar, 512);
3145
                        fwrite($v_temp_tar, $v_content, 512);
3146
                    } else {
3147
                        $v_content = gzread($v_tar, 512);
3148
                        gzwrite($v_temp_tar, $v_content, 512);
3149
                    }
3150
                }
3151
3152
                // ----- File name and properties are logged if listing mode or file is extracted
3153
                TrFctMessage(__FILE__, __LINE__, 2, 'Memorize info about file ' . $v_header['filename'] . '');
3154
3155
                // ----- Add the array describing the file into the list
3156
                $p_list_detail[$v_nb]           = $v_header;
3157
                $p_list_detail[$v_nb]['status'] = ($v_found_file ? 'not_updated' : 'ok');
3158
3159
                // ----- Increment
3160
                ++$v_nb;
3161
            } // ----- Look for file that need to be updated
3162
            else {
3163
                // ----- Trace
3164
                TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
3165
3166
                // ----- Store the old file size
3167
                $v_old_size = $v_header['size'];
3168
3169
                // ----- Add the file
3170 View Code Duplication
                if (1 != ($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3171
                    // ----- Close the tarfile
3172
                    if ('tar' === $p_tar_mode) {
3173
                        fclose($v_tar);
3174
                        fclose($v_temp_tar);
3175
                    } else {
3176
                        gzclose($v_tar);
3177
                        gzclose($v_temp_tar);
3178
                    }
3179
                    @unlink($p_temp_tarname);
0 ignored issues
show
Bug introduced by
The variable $p_temp_tarname does not exist. Did you mean $p_tarname?

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

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

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

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3180
3181
                    // ----- Return status
3182
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3183
3184
                    return $v_result;
3185
                }
3186
3187
                // ----- Trace
3188
                TrFctMessage(__FILE__, __LINE__, 2, 'Skip old file ' . $v_header['filename'] . '');
3189
3190
                // ----- Jump to next file
3191
                if ('tar' === $p_tar_mode) {
3192
                    fseek($v_tar, ftell($v_tar) + (ceil($v_old_size / 512) * 512));
3193
                } else {
3194
                    gzseek($v_tar, gztell($v_tar) + (ceil($v_old_size / 512) * 512));
3195
                }
3196
3197
                // ----- Add the array describing the file into the list
3198
                $p_list_detail[$v_nb]           = $v_header;
3199
                $p_list_detail[$v_nb]['status'] = 'updated';
3200
3201
                // ----- Increment
3202
                ++$v_nb;
3203
            }
3204
3205
            // ----- Look for end of file
3206
            if ('tar' === $p_tar_mode) {
3207
                $v_end_of_file = feof($v_tar);
0 ignored issues
show
Unused Code introduced by
$v_end_of_file is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
3210
            }
3211
        }
3212
3213
        // ----- Look for files that does not exists in the archive and need to be added
3214
        for ($i = 0, $iMax = count($p_file_list); $i < $iMax; ++$i) {
3215
            // ----- Look if file not found in the archive
3216
            if (!$v_found_list[$i]) {
3217
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be added");
3218
3219
                // ----- Add the file
3220 View Code Duplication
                if (1 != ($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3221
                    // ----- Close the tarfile
3222
                    if ('tar' === $p_tar_mode) {
3223
                        fclose($v_tar);
3224
                        fclose($v_temp_tar);
3225
                    } else {
3226
                        gzclose($v_tar);
3227
                        gzclose($v_temp_tar);
3228
                    }
3229
                    @unlink($p_temp_tarname);
0 ignored issues
show
Bug introduced by
The variable $p_temp_tarname does not exist. Did you mean $p_tarname?

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

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

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

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

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

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3230
3231
                    // ----- Return status
3232
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3233
3234
                    return $v_result;
3235
                }
3236
3237
                // ----- Add the array describing the file into the list
3238
                $p_list_detail[$v_nb]           = $v_header;
3239
                $p_list_detail[$v_nb]['status'] = 'added';
3240
3241
                // ----- Increment
3242
                ++$v_nb;
3243
            } else {
3244
                TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' was already updated if needed");
3245
            }
3246
        }
3247
3248
        // ----- Write the last empty buffer
3249
        PclTarHandleFooter($v_temp_tar, $p_tar_mode);
3250
3251
        // ----- Close the tarfile
3252
        if ('tar' === $p_tar_mode) {
3253
            fclose($v_tar);
3254
            fclose($v_temp_tar);
3255
        } else {
3256
            gzclose($v_tar);
3257
            gzclose($v_temp_tar);
3258
        }
3259
3260
        // ----- Unlink tar file
3261
        if (!@unlink($p_tarname)) {
3262
            // ----- Error log
3263
            PclErrorLog(-11, "Error while deleting archive name $p_tarname");
3264
        }
3265
3266
        // ----- Rename tar file
3267
        if (!@rename($v_temp_tarname, $p_tarname)) {
3268
            // ----- Error log
3269
            PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
3270
3271
            // ----- Return
3272
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3273
3274
            return PclErrorCode();
3275
        }
3276
3277
        // ----- Return
3278
        TrFctEnd(__FILE__, __LINE__, $v_result);
3279
3280
        return $v_result;
3281
    }
3282
3283
    // --------------------------------------------------------------------------------
3284
3285
    // --------------------------------------------------------------------------------
3286
    // 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...
3287
    // Description :
3288
    // Parameters :
3289
    // Return Values :
3290
    // --------------------------------------------------------------------------------
3291
    /**
3292
     * @param $v_binary_data
3293
     * @param $v_header
3294
     *
3295
     * @return int
3296
     */
3297
    function PclTarHandleReadHeader($v_binary_data, &$v_header)
3298
    {
3299
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleReadHeader', '');
3300
        $v_result = 1;
3301
3302
        // ----- Read the 512 bytes header
3303
        /*
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...
3304
    if ($p_tar_mode == "tar")
3305
      $v_binary_data = fread($p_tar, 512);
3306
    else
3307
      $v_binary_data = gzread($p_tar, 512);
3308
    */
3309
3310
        // ----- Look for no more block
3311
        if (0 == strlen($v_binary_data)) {
3312
            $v_header['filename'] = '';
3313
            $v_header['status']   = 'empty';
3314
3315
            // ----- Return
3316
            TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3317
3318
            return $v_result;
3319
        }
3320
3321
        // ----- Look for invalid block size
3322
        if (512 != strlen($v_binary_data)) {
3323
            $v_header['filename'] = '';
3324
            $v_header['status']   = 'invalid_header';
3325
            TrFctMessage(__FILE__, __LINE__, 2, 'Invalid block size : ' . strlen($v_binary_data));
3326
3327
            // ----- Error log
3328
            PclErrorLog(-10, 'Invalid block size : ' . strlen($v_binary_data));
3329
3330
            // ----- Return
3331
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3332
3333
            return PclErrorCode();
3334
        }
3335
3336
        // ----- Calculate the checksum
3337
        $v_checksum = 0;
3338
        // ..... First part of the header
3339 View Code Duplication
        for ($i = 0; $i < 148; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3340
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3341
        }
3342
        // ..... Ignore the checksum value and replace it by ' ' (space)
3343
        for ($i = 148; $i < 156; ++$i) {
3344
            $v_checksum += ord(' ');
3345
        }
3346
        // ..... Last part of the header
3347 View Code Duplication
        for ($i = 156; $i < 512; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3348
            $v_checksum += ord(substr($v_binary_data, $i, 1));
3349
        }
3350
        TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
3351
3352
        // ----- Extract the values
3353
        TrFctMessage(__FILE__, __LINE__, 2, "Header : '$v_binary_data'");
3354
        $v_data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $v_binary_data);
3355
3356
        // ----- Extract the checksum for check
3357
        $v_header['checksum'] = octdec(trim($v_data['checksum']));
3358
        TrFctMessage(__FILE__, __LINE__, 3, 'File checksum : ' . $v_header['checksum'] . '');
3359
        if ($v_header['checksum'] != $v_checksum) {
3360
            TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : $v_checksum calculated, " . $v_header['checksum'] . ' expected');
3361
3362
            $v_header['filename'] = '';
3363
            $v_header['status']   = 'invalid_header';
3364
3365
            // ----- Look for last block (empty block)
3366
            if ((256 == $v_checksum) && (0 == $v_header['checksum'])) {
3367
                $v_header['status'] = 'empty';
3368
                // ----- Return
3369
                TrFctEnd(__FILE__, __LINE__, $v_result, 'End of archive found');
3370
3371
                return $v_result;
3372
            }
3373
3374
            // ----- Error log
3375
            PclErrorLog(-13, "Invalid checksum : $v_checksum calculated, " . $v_header['checksum'] . ' expected');
3376
3377
            // ----- Return
3378
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3379
3380
            return PclErrorCode();
3381
        }
3382
        TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ($v_checksum)");
3383
3384
        // ----- Extract the properties
3385
        $v_header['filename'] = trim($v_data['filename']);
3386
        TrFctMessage(__FILE__, __LINE__, 2, 'Name : ' . $v_header['filename'] . '');
3387
        $v_header['mode'] = octdec(trim($v_data['mode']));
3388
        TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . decoct($v_header['mode']) . "'");
3389
        $v_header['uid'] = octdec(trim($v_data['uid']));
3390
        TrFctMessage(__FILE__, __LINE__, 2, "Uid : '" . $v_header['uid'] . "'");
3391
        $v_header['gid'] = octdec(trim($v_data['gid']));
3392
        TrFctMessage(__FILE__, __LINE__, 2, "Gid : '" . $v_header['gid'] . "'");
3393
        $v_header['size'] = octdec(trim($v_data['size']));
3394
        TrFctMessage(__FILE__, __LINE__, 2, "Size : '" . $v_header['size'] . "'");
3395
        $v_header['mtime'] = octdec(trim($v_data['mtime']));
3396
        TrFctMessage(__FILE__, __LINE__, 2, 'Date : ' . date('l dS of F Y h:i:s A', $v_header['mtime']));
3397
        if ('5' == ($v_header['typeflag'] = $v_data['typeflag'])) {
3398
            $v_header['size'] = 0;
3399
            TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '" . $v_header['size'] . "'");
3400
        }
3401
        TrFctMessage(__FILE__, __LINE__, 2, 'File typeflag : ' . $v_header['typeflag'] . '');
3402
        /* ----- 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...
3403
    $v_header[link] = trim($v_data[link]);
3404
    TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]");
3405
    $v_header[magic] = trim($v_data[magic]);
3406
    TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]");
3407
    $v_header[version] = trim($v_data[version]);
3408
    TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]");
3409
    $v_header[uname] = trim($v_data[uname]);
3410
    TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]");
3411
    $v_header[gname] = trim($v_data[gname]);
3412
    TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]");
3413
    $v_header[devmajor] = trim($v_data[devmajor]);
3414
    TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]");
3415
    $v_header[devminor] = trim($v_data[devminor]);
3416
    TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]");
3417
    */
3418
3419
        // ----- Set the status field
3420
        $v_header['status'] = 'ok';
3421
3422
        // ----- Return
3423
        TrFctEnd(__FILE__, __LINE__, $v_result);
3424
3425
        return $v_result;
3426
    }
3427
3428
    // --------------------------------------------------------------------------------
3429
3430
    // --------------------------------------------------------------------------------
3431
    // 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...
3432
    // Description :
3433
    //   Check if a directory exists, if not it creates it and all the parents directory
3434
    //   which may be useful.
3435
    // Parameters :
3436
    //   $p_dir : Directory path to check (without / at the end).
3437
    // Return Values :
3438
    //    1 : OK
3439
    //   -1 : Unable to create directory
3440
    // --------------------------------------------------------------------------------
3441
    /**
3442
     * @param $p_dir
3443
     *
3444
     * @return int
3445
     */
3446
    function PclTarHandlerDirCheck($p_dir)
3447
    {
3448
        $v_result = 1;
3449
3450
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlerDirCheck', "$p_dir");
3451
3452
        // ----- Check the directory availability
3453
        if (is_dir($p_dir) || ('' == $p_dir)) {
3454
            TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
3455
3456
            return 1;
3457
        }
3458
3459
        // ----- Look for file alone
3460
        /*
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...
3461
    if (false === strpos("$p_dir", "/")) {
3462
      TrFctEnd(__FILE__, __LINE__,  "'$p_dir' is a file with no directory");
3463
3464
      return 1;
3465
    }
3466
    */
3467
3468
        // ----- Extract parent directory
3469
        $p_parent_dir = dirname($p_dir);
3470
        TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
3471
3472
        // ----- Just a check
3473
        if ($p_parent_dir != $p_dir) {
3474
            // ----- Look for parent directory
3475
            if ('' != $p_parent_dir) {
3476
                if (1 != ($v_result = PclTarHandlerDirCheck($p_parent_dir))) {
3477
                    TrFctEnd(__FILE__, __LINE__, $v_result);
3478
3479
                    return $v_result;
3480
                }
3481
            }
3482
        }
3483
3484
        // ----- Create the directory
3485
        TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
3486
        if (!@mkdir($p_dir, 0777)) {
3487
            // ----- Error log
3488
            PclErrorLog(-8, "Unable to create directory '$p_dir'");
3489
3490
            // ----- Return
3491
            TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3492
3493
            return PclErrorCode();
3494
        }
3495
3496
        // ----- Return
3497
        TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
3498
3499
        return $v_result;
3500
    }
3501
3502
    // --------------------------------------------------------------------------------
3503
3504
    // --------------------------------------------------------------------------------
3505
    // 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...
3506
    // Description :
3507
    // Parameters :
3508
    // Return Values :
3509
    // --------------------------------------------------------------------------------
3510
    /**
3511
     * @param $p_tarname
3512
     *
3513
     * @return string
3514
     */
3515
    function PclTarHandleExtension($p_tarname)
3516
    {
3517
        TrFctStart(__FILE__, __LINE__, 'PclTarHandleExtension', "tar=$p_tarname");
3518
3519
        // ----- Look for file extension
3520
        if (('.tar.gz' === substr($p_tarname, -7)) || ('.tgz' === substr($p_tarname, -4))) {
3521
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a gzip tar');
3522
            $v_tar_mode = 'tgz';
3523
        } elseif ('.tar' === substr($p_tarname, -4)) {
3524
            TrFctMessage(__FILE__, __LINE__, 2, 'Archive is a tar');
3525
            $v_tar_mode = 'tar';
3526
        } else {
3527
            // ----- Error log
3528
            PclErrorLog(-9, 'Invalid archive extension');
3529
3530
            TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
3531
3532
            $v_tar_mode = '';
3533
        }
3534
3535
        // ----- Return
3536
        TrFctEnd(__FILE__, __LINE__, $v_tar_mode);
3537
3538
        return $v_tar_mode;
3539
    }
3540
3541
    // --------------------------------------------------------------------------------
3542
3543
    // --------------------------------------------------------------------------------
3544
    // 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...
3545
    // Description :
3546
    // Parameters :
3547
    // Return Values :
3548
    // --------------------------------------------------------------------------------
3549
    /**
3550
     * @param $p_dir
3551
     *
3552
     * @return string
3553
     */
3554
    function PclTarHandlePathReduction($p_dir)
3555
    {
3556
        TrFctStart(__FILE__, __LINE__, 'PclTarHandlePathReduction', "dir='$p_dir'");
3557
        $v_result = '';
3558
3559
        // ----- Look for not empty path
3560
        if ('' != $p_dir) {
3561
            // ----- Explode path by directory names
3562
            $v_list = explode('/', $p_dir);
3563
3564
            // ----- Study directories from last to first
3565
            for ($i = count($v_list) - 1; $i >= 0; $i--) {
3566
                // ----- Look for current path
3567
                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...
3568
                    // ----- Ignore this directory
3569
                    // Should be the first $i=0, but no check is done
3570
                } elseif ('..' === $v_list[$i]) {
3571
                    // ----- Ignore it and ignore the $i-1
3572
                    $i--;
3573
                } 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...
3574
                    // ----- Ignore only the double '//' in path,
3575
                    // but not the first and last '/'
3576
                } else {
3577
                    $v_result = $v_list[$i] . ($i != (count($v_list) - 1) ? '/' . $v_result : '');
3578
                }
3579
            }
3580
        }
3581
3582
        // ----- Return
3583
        TrFctEnd(__FILE__, __LINE__, $v_result);
3584
3585
        return $v_result;
3586
    }
3587
3588
    // --------------------------------------------------------------------------------
3589
3590
    // ----- End of double include look
3591
}
3592