Completed
Push — develop ( 2922a1...cfa1fe )
by Adrien
24:40
created

PclZip::listContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 23
rs 9.0856
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 31 and the first side effect is on line 87.

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
namespace PhpOffice\PhpSpreadsheet\Shared\PCLZip;
4
5
// --------------------------------------------------------------------------------
6
// PhpConcept Library - Zip Module 2.8.2
7
// --------------------------------------------------------------------------------
8
// License GNU/LGPL - Vincent Blavet - August 2009
9
// http://www.phpconcept.net
10
// --------------------------------------------------------------------------------
11
//
12
// Presentation :
13
//     PclZip is a PHP library that manage ZIP archives.
14
//     So far tests show that archives generated by PclZip are readable by
15
//     WinZip application and other tools.
16
//
17
// Description :
18
//     See readme.txt and http://www.phpconcept.net
19
//
20
// Warning :
21
//     This library and the associated files are non commercial, non professional
22
//     work.
23
//     It should not have unexpected results. However if any damage is caused by
24
//     this software the author can not be responsible.
25
//     The use of this software is at the risk of the user.
26
//
27
// --------------------------------------------------------------------------------
28
29
// ----- Constants
30
if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
31
    define('PCLZIP_READ_BLOCK_SIZE', 2048);
32
}
33
34
// ----- File list separator
35
// In version 1.x of PclZip, the separator for file list is a space
36
// (which is not a very smart choice, specifically for windows paths !).
37
// A better separator should be a comma (,). This constant gives you the
38
// abilty to change that.
39
// However notice that changing this value, may have impact on existing
40
// scripts, using space separated filenames.
41
// Recommanded values for compatibility with older versions :
42
//define('PCLZIP_SEPARATOR', ' ');
43
// Recommanded values for smart separation of filenames.
44
if (!defined('PCLZIP_SEPARATOR')) {
45
    define('PCLZIP_SEPARATOR', ',');
46
}
47
48
// ----- Error configuration
49
// 0 : PclZip Class integrated error handling
50
// 1 : PclError external library error handling. By enabling this
51
//         you must ensure that you have included PclError library.
52
// [2,...] : reserved for futur use
53
if (!defined('PCLZIP_ERROR_EXTERNAL')) {
54
    define('PCLZIP_ERROR_EXTERNAL', 0);
55
}
56
57
// ----- Optional static temporary directory
58
//             By default temporary files are generated in the script current
59
//             path.
60
//             If defined :
61
//             - MUST BE terminated by a '/'.
62
//             - MUST be a valid, already created directory
63
//             Samples :
64
// define('PCLZIP_TEMPORARY_DIR', '/temp/');
65
// define('PCLZIP_TEMPORARY_DIR', 'C:/Temp/');
66
if (!defined('PCLZIP_TEMPORARY_DIR')) {
67
    define('PCLZIP_TEMPORARY_DIR', '');
68
}
69
70
// ----- Optional threshold ratio for use of temporary files
71
//             Pclzip sense the size of the file to add/extract and decide to
72
//             use or not temporary file. The algorythm is looking for
73
//             memory_limit of PHP and apply a ratio.
74
//             threshold = memory_limit * ratio.
75
//             Recommended values are under 0.5. Default 0.47.
76
//             Samples :
77
// define('PCLZIP_TEMPORARY_FILE_RATIO', 0.5);
78
if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
79
    define('PCLZIP_TEMPORARY_FILE_RATIO', 0.47);
80
}
81
82
// --------------------------------------------------------------------------------
83
// ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
84
// --------------------------------------------------------------------------------
85
86
// ----- Global variables
87
$g_pclzip_version = '2.8.2';
88
89
// ----- Error codes
90
//     -1 : Unable to open file in binary write mode
91
//     -2 : Unable to open file in binary read mode
92
//     -3 : Invalid parameters
93
//     -4 : File does not exist
94
//     -5 : Filename is too long (max. 255)
95
//     -6 : Not a valid zip file
96
//     -7 : Invalid extracted file size
97
//     -8 : Unable to create directory
98
//     -9 : Invalid archive extension
99
//    -10 : Invalid archive format
100
//    -11 : Unable to delete file (unlink)
101
//    -12 : Unable to rename file (rename)
102
//    -13 : Invalid header checksum
103
//    -14 : Invalid archive size
104
define('PCLZIP_ERR_USER_ABORTED', 2);
105
define('PCLZIP_ERR_NO_ERROR', 0);
106
define('PCLZIP_ERR_WRITE_OPEN_FAIL', -1);
107
define('PCLZIP_ERR_READ_OPEN_FAIL', -2);
108
define('PCLZIP_ERR_INVALID_PARAMETER', -3);
109
define('PCLZIP_ERR_MISSING_FILE', -4);
110
define('PCLZIP_ERR_FILENAME_TOO_LONG', -5);
111
define('PCLZIP_ERR_INVALID_ZIP', -6);
112
define('PCLZIP_ERR_BAD_EXTRACTED_FILE', -7);
113
define('PCLZIP_ERR_DIR_CREATE_FAIL', -8);
114
define('PCLZIP_ERR_BAD_EXTENSION', -9);
115
define('PCLZIP_ERR_BAD_FORMAT', -10);
116
define('PCLZIP_ERR_DELETE_FILE_FAIL', -11);
117
define('PCLZIP_ERR_RENAME_FILE_FAIL', -12);
118
define('PCLZIP_ERR_BAD_CHECKSUM', -13);
119
define('PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
120
define('PCLZIP_ERR_MISSING_OPTION_VALUE', -15);
121
define('PCLZIP_ERR_INVALID_OPTION_VALUE', -16);
122
define('PCLZIP_ERR_ALREADY_A_DIRECTORY', -17);
123
define('PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18);
124
define('PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19);
125
define('PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20);
126
define('PCLZIP_ERR_DIRECTORY_RESTRICTION', -21);
127
128
// ----- Options values
129
define('PCLZIP_OPT_PATH', 77001);
130
define('PCLZIP_OPT_ADD_PATH', 77002);
131
define('PCLZIP_OPT_REMOVE_PATH', 77003);
132
define('PCLZIP_OPT_REMOVE_ALL_PATH', 77004);
133
define('PCLZIP_OPT_SET_CHMOD', 77005);
134
define('PCLZIP_OPT_EXTRACT_AS_STRING', 77006);
135
define('PCLZIP_OPT_NO_COMPRESSION', 77007);
136
define('PCLZIP_OPT_BY_NAME', 77008);
137
define('PCLZIP_OPT_BY_INDEX', 77009);
138
define('PCLZIP_OPT_BY_EREG', 77010);
139
define('PCLZIP_OPT_BY_PREG', 77011);
140
define('PCLZIP_OPT_COMMENT', 77012);
141
define('PCLZIP_OPT_ADD_COMMENT', 77013);
142
define('PCLZIP_OPT_PREPEND_COMMENT', 77014);
143
define('PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015);
144
define('PCLZIP_OPT_REPLACE_NEWER', 77016);
145
define('PCLZIP_OPT_STOP_ON_ERROR', 77017);
146
// Having big trouble with crypt. Need to multiply 2 long int
147
// which is not correctly supported by PHP ...
148
//define('PCLZIP_OPT_CRYPT', 77018);
149
define('PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019);
150
define('PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020);
151
define('PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020); // alias
152
define('PCLZIP_OPT_TEMP_FILE_ON', 77021);
153
define('PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021); // alias
154
define('PCLZIP_OPT_TEMP_FILE_OFF', 77022);
155
define('PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022); // alias
156
157
// ----- File description attributes
158
define('PCLZIP_ATT_FILE_NAME', 79001);
159
define('PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002);
160
define('PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003);
161
define('PCLZIP_ATT_FILE_MTIME', 79004);
162
define('PCLZIP_ATT_FILE_CONTENT', 79005);
163
define('PCLZIP_ATT_FILE_COMMENT', 79006);
164
165
// ----- Call backs values
166
define('PCLZIP_CB_PRE_EXTRACT', 78001);
167
define('PCLZIP_CB_POST_EXTRACT', 78002);
168
define('PCLZIP_CB_PRE_ADD', 78003);
169
define('PCLZIP_CB_POST_ADD', 78004);
170
/* For futur use
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
171
define('PCLZIP_CB_PRE_LIST', 78005);
172
define('PCLZIP_CB_POST_LIST', 78006);
173
define('PCLZIP_CB_PRE_DELETE', 78007);
174
define('PCLZIP_CB_POST_DELETE', 78008);
175
*/
176
177
// --------------------------------------------------------------------------------
178
// Class : PclZip
179
// Description :
180
//     PclZip is the class that represent a Zip archive.
181
//     The public methods allow the manipulation of the archive.
182
// Attributes :
183
//     Attributes must not be accessed directly.
184
// Methods :
185
//     PclZip() : Object creator
186
//     create() : Creates the Zip archive
187
//     listContent() : List the content of the Zip archive
188
//     extract() : Extract the content of the archive
189
//     properties() : List the properties of the archive
190
// --------------------------------------------------------------------------------
191
class PclZip
192
{
193
    // ----- Filename of the zip file
194
    public $zipname = '';
195
196
    // ----- File descriptor of the zip file
197
    public $zip_fd = 0;
198
199
    // ----- Internal error handling
200
    public $error_code = 1;
201
    public $error_string = '';
202
203
    // ----- Current status of the magic_quotes_runtime
204
    // This value store the php configuration for magic_quotes
205
    // The class can then disable the magic_quotes and reset it after
206
    public $magic_quotes_status;
207
208
    // --------------------------------------------------------------------------------
209
    // Function : PclZip()
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...
210
    // Description :
211
    //     Creates a PclZip object and set the name of the associated Zip archive
212
    //     filename.
213
    //     Note that no real action is taken, if the archive does not exist it is not
214
    //     created. Use create() for that.
215
    // --------------------------------------------------------------------------------
216
217
    /**
218
     * @param string $p_zipname
219
     */
220
    public function __construct($p_zipname)
221
    {
222
223
        // ----- Tests the zlib
224
        if (!function_exists('gzopen')) {
225
            die('Abort ' . basename(__FILE__) . ' : Missing zlib extensions');
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
226
        }
227
228
        // ----- Set the attributes
229
        $this->zipname = $p_zipname;
230
        $this->zip_fd = 0;
231
        $this->magic_quotes_status = -1;
232
233
        // ----- Return
234
        return;
235
    }
236
    // --------------------------------------------------------------------------------
237
238
    // --------------------------------------------------------------------------------
239
    // Function :
240
    //     create($p_filelist, $p_add_dir="", $p_remove_dir="")
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
241
    //     create($p_filelist, $p_option, $p_option_value, ...)
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...
242
    // Description :
243
    //     This method supports two different synopsis. The first one is historical.
244
    //     This method creates a Zip Archive. The Zip file is created in the
245
    //     filesystem. The files and directories indicated in $p_filelist
246
    //     are added in the archive. See the parameters description for the
247
    //     supported format of $p_filelist.
248
    //     When a directory is in the list, the directory and its content is added
249
    //     in the archive.
250
    //     In this synopsis, the function takes an optional variable list of
251
    //     options. See bellow the supported options.
252
    // Parameters :
253
    //     $p_filelist : An array containing file or directory names, or
254
    //                                 a string containing one filename or one directory name, or
255
    //                                 a string containing a list of filenames and/or directory
256
    //                                 names separated by spaces.
257
    //     $p_add_dir : A path to add before the real path of the archived file,
258
    //                                in order to have it memorized in the archive.
259
    //     $p_remove_dir : A path to remove from the real path of the file to archive,
260
    //                                     in order to have a shorter path memorized in the archive.
261
    //                                     When $p_add_dir and $p_remove_dir are set, $p_remove_dir
262
    //                                     is removed first, before $p_add_dir is added.
263
    // Options :
264
    //     PCLZIP_OPT_ADD_PATH :
265
    //     PCLZIP_OPT_REMOVE_PATH :
266
    //     PCLZIP_OPT_REMOVE_ALL_PATH :
267
    //     PCLZIP_OPT_COMMENT :
268
    //     PCLZIP_CB_PRE_ADD :
269
    //     PCLZIP_CB_POST_ADD :
270
    // Return Values :
271
    //     0 on failure,
272
    //     The list of the added files, with a status of the add action.
273
    //     (see PclZip::listContent() for list entry format)
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...
274
    // --------------------------------------------------------------------------------
275
    public function create($p_filelist)
276
    {
277
        $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...
278
279
        // ----- Reset the error handler
280
        $this->privErrorReset();
281
282
        // ----- Set default values
283
        $v_options = [];
284
        $v_options[PCLZIP_OPT_NO_COMPRESSION] = false;
285
286
        // ----- Look for variable options arguments
287
        $v_size = func_num_args();
288
289
        // ----- Look for arguments
290
        if ($v_size > 1) {
291
            // ----- Get the arguments
292
            $v_arg_list = func_get_args();
293
294
            // ----- Remove from the options list the first argument
295
            array_shift($v_arg_list);
296
            --$v_size;
297
298
            // ----- Look for first arg
299
            if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
300
                // ----- Parse the options
301
                $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, [
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_REMOVE_...FILE_OFF => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
302
                    PCLZIP_OPT_REMOVE_PATH => 'optional',
303
                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
304
                    PCLZIP_OPT_ADD_PATH => 'optional',
305
                    PCLZIP_CB_PRE_ADD => 'optional',
306
                    PCLZIP_CB_POST_ADD => 'optional',
307
                    PCLZIP_OPT_NO_COMPRESSION => 'optional',
308
                    PCLZIP_OPT_COMMENT => 'optional',
309
                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
310
                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
311
                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional',
312
                    //, PCLZIP_OPT_CRYPT => 'optional'
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
313
                ]);
314
                if ($v_result != 1) {
315
                    return 0;
316
                }
317 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...
318
                // ----- Look for 2 args
319
                // Here we need to support the first historic synopsis of the
320
                // method.
321
                // ----- Get the first argument
322
                $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
323
324
                // ----- Look for the optional second argument
325
                if ($v_size == 2) {
326
                    $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
327
                } elseif ($v_size > 2) {
328
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid number / type of arguments');
329
330
                    return 0;
331
                }
332
            }
333
        }
334
335
        // ----- Look for default option values
336
        $this->privOptionDefaultThreshold($v_options);
337
338
        // ----- Init
339
        $v_string_list = [];
340
        $v_att_list = [];
341
        $v_filedescr_list = [];
342
        $p_result_list = [];
343
344
        // ----- Look if the $p_filelist is really an array
345 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...
346
            // ----- Look if the first element is also an array
347
            //             This will mean that this is a file description entry
348
            if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
349
                $v_att_list = $p_filelist;
350
            } else {
351
                // ----- The list is a list of string names
352
                $v_string_list = $p_filelist;
353
            }
354
        } elseif (is_string($p_filelist)) {
355
            // ----- Look if the $p_filelist is a string
356
            // ----- Create a list from the string
357
            $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
358
        } else {
359
            // ----- Invalid variable type for $p_filelist
360
            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid variable type p_filelist');
361
362
            return 0;
363
        }
364
365
        // ----- Reformat the string list
366 View Code Duplication
        if (sizeof($v_string_list) != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
367
            foreach ($v_string_list as $v_string) {
368
                if ($v_string != '') {
369
                    $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
370
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
371
                }
372
            }
373
        }
374
375
        // ----- For each file in the list check the attributes
376
        $v_supported_attributes = [
377
            PCLZIP_ATT_FILE_NAME => 'mandatory',
378
            PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
379
            PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
380
            PCLZIP_ATT_FILE_MTIME => 'optional',
381
            PCLZIP_ATT_FILE_CONTENT => 'optional',
382
            PCLZIP_ATT_FILE_COMMENT => 'optional',
383
        ];
384 View Code Duplication
        foreach ($v_att_list as $v_entry) {
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...
385
            $v_result = $this->privFileDescrParseAtt($v_entry, $v_filedescr_list[], $v_options, $v_supported_attributes);
0 ignored issues
show
Documentation introduced by
$v_supported_attributes is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
386
            if ($v_result != 1) {
387
                return 0;
388
            }
389
        }
390
391
        // ----- Expand the filelist (expand directories)
392
        $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
393
        if ($v_result != 1) {
394
            return 0;
395
        }
396
397
        // ----- Call the create fct
398
        $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
399
        if ($v_result != 1) {
400
            return 0;
401
        }
402
403
        // ----- Return
404
        return $p_result_list;
405
    }
406
    // --------------------------------------------------------------------------------
407
408
    // --------------------------------------------------------------------------------
409
    // Function :
410
    //     add($p_filelist, $p_add_dir="", $p_remove_dir="")
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
411
    //     add($p_filelist, $p_option, $p_option_value, ...)
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...
412
    // Description :
413
    //     This method supports two synopsis. The first one is historical.
414
    //     This methods add the list of files in an existing archive.
415
    //     If a file with the same name already exists, it is added at the end of the
416
    //     archive, the first one is still present.
417
    //     If the archive does not exist, it is created.
418
    // Parameters :
419
    //     $p_filelist : An array containing file or directory names, or
420
    //                                 a string containing one filename or one directory name, or
421
    //                                 a string containing a list of filenames and/or directory
422
    //                                 names separated by spaces.
423
    //     $p_add_dir : A path to add before the real path of the archived file,
424
    //                                in order to have it memorized in the archive.
425
    //     $p_remove_dir : A path to remove from the real path of the file to archive,
426
    //                                     in order to have a shorter path memorized in the archive.
427
    //                                     When $p_add_dir and $p_remove_dir are set, $p_remove_dir
428
    //                                     is removed first, before $p_add_dir is added.
429
    // Options :
430
    //     PCLZIP_OPT_ADD_PATH :
431
    //     PCLZIP_OPT_REMOVE_PATH :
432
    //     PCLZIP_OPT_REMOVE_ALL_PATH :
433
    //     PCLZIP_OPT_COMMENT :
434
    //     PCLZIP_OPT_ADD_COMMENT :
435
    //     PCLZIP_OPT_PREPEND_COMMENT :
436
    //     PCLZIP_CB_PRE_ADD :
437
    //     PCLZIP_CB_POST_ADD :
438
    // Return Values :
439
    //     0 on failure,
440
    //     The list of the added files, with a status of the add action.
441
    //     (see PclZip::listContent() for list entry format)
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...
442
    // --------------------------------------------------------------------------------
443
    public function add($p_filelist)
444
    {
445
        $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...
446
447
        // ----- Reset the error handler
448
        $this->privErrorReset();
449
450
        // ----- Set default values
451
        $v_options = [];
452
        $v_options[PCLZIP_OPT_NO_COMPRESSION] = false;
453
454
        // ----- Look for variable options arguments
455
        $v_size = func_num_args();
456
457
        // ----- Look for arguments
458
        if ($v_size > 1) {
459
            // ----- Get the arguments
460
            $v_arg_list = func_get_args();
461
462
            // ----- Remove form the options list the first argument
463
            array_shift($v_arg_list);
464
            --$v_size;
465
466
            // ----- Look for first arg
467
            if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
468
                // ----- Parse the options
469
                $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, [
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_REMOVE_...FILE_OFF => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
470
                    PCLZIP_OPT_REMOVE_PATH => 'optional',
471
                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
472
                    PCLZIP_OPT_ADD_PATH => 'optional',
473
                    PCLZIP_CB_PRE_ADD => 'optional',
474
                    PCLZIP_CB_POST_ADD => 'optional',
475
                    PCLZIP_OPT_NO_COMPRESSION => 'optional',
476
                    PCLZIP_OPT_COMMENT => 'optional',
477
                    PCLZIP_OPT_ADD_COMMENT => 'optional',
478
                    PCLZIP_OPT_PREPEND_COMMENT => 'optional',
479
                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
480
                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
481
                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional',
482
                    //, PCLZIP_OPT_CRYPT => 'optional'
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...
483
                ]);
484
                if ($v_result != 1) {
485
                    return 0;
486
                }
487 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...
488
                // ----- Look for 2 args
489
                // Here we need to support the first historic synopsis of the
490
                // method.
491
                // ----- Get the first argument
492
                $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
0 ignored issues
show
Unused Code introduced by
$v_add_path 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...
493
494
                // ----- Look for the optional second argument
495
                if ($v_size == 2) {
496
                    $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
497
                } elseif ($v_size > 2) {
498
                    // ----- Error log
499
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid number / type of arguments');
500
501
                    // ----- Return
502
                    return 0;
503
                }
504
            }
505
        }
506
507
        // ----- Look for default option values
508
        $this->privOptionDefaultThreshold($v_options);
509
510
        // ----- Init
511
        $v_string_list = [];
512
        $v_att_list = [];
513
        $v_filedescr_list = [];
514
        $p_result_list = [];
515
516
        // ----- Look if the $p_filelist is really an array
517 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...
518
            // ----- Look if the first element is also an array
519
            //             This will mean that this is a file description entry
520
            if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
521
                $v_att_list = $p_filelist;
522
            } else {
523
                // ----- The list is a list of string names
524
                $v_string_list = $p_filelist;
525
            }
526
        } elseif (is_string($p_filelist)) {
527
            // ----- Look if the $p_filelist is a string
528
            // ----- Create a list from the string
529
            $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
530
        } else {
531
            // ----- Invalid variable type for $p_filelist
532
            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '" . gettype($p_filelist) . "' for p_filelist");
533
534
            return 0;
535
        }
536
537
        // ----- Reformat the string list
538 View Code Duplication
        if (sizeof($v_string_list) != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
539
            foreach ($v_string_list as $v_string) {
540
                $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
541
            }
542
        }
543
544
        // ----- For each file in the list check the attributes
545
        $v_supported_attributes = [
546
            PCLZIP_ATT_FILE_NAME => 'mandatory',
547
            PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
548
            PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
549
            PCLZIP_ATT_FILE_MTIME => 'optional',
550
            PCLZIP_ATT_FILE_CONTENT => 'optional',
551
            PCLZIP_ATT_FILE_COMMENT => 'optional',
552
        ];
553 View Code Duplication
        foreach ($v_att_list as $v_entry) {
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...
554
            $v_result = $this->privFileDescrParseAtt($v_entry, $v_filedescr_list[], $v_options, $v_supported_attributes);
0 ignored issues
show
Documentation introduced by
$v_supported_attributes is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
555
            if ($v_result != 1) {
556
                return 0;
557
            }
558
        }
559
560
        // ----- Expand the filelist (expand directories)
561
        $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
562
        if ($v_result != 1) {
563
            return 0;
564
        }
565
566
        // ----- Call the create fct
567
        $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
568
        if ($v_result != 1) {
569
            return 0;
570
        }
571
572
        // ----- Return
573
        return $p_result_list;
574
    }
575
    // --------------------------------------------------------------------------------
576
577
    // --------------------------------------------------------------------------------
578
    // Function : listContent()
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...
579
    // Description :
580
    //     This public method, gives the list of the files and directories, with their
581
    //     properties.
582
    //     The properties of each entries in the list are (used also in other functions) :
583
    //         filename : Name of the file. For a create or add action it is the filename
584
    //                                given by the user. For an extract function it is the filename
585
    //                                of the extracted file.
586
    //         stored_filename : Name of the file / directory stored in the archive.
587
    //         size : Size of the stored file.
588
    //         compressed_size : Size of the file's data compressed in the archive
589
    //                                             (without the headers overhead)
590
    //         mtime : Last known modification date of the file (UNIX timestamp)
591
    //         comment : Comment associated with the file
592
    //         folder : true | false
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
593
    //         index : index of the file in the archive
594
    //         status : status of the action (depending of the action) :
595
    //                            Values are :
596
    //                                ok : OK !
597
    //                                filtered : the file / dir is not extracted (filtered by user)
598
    //                                already_a_directory : the file can not be extracted because a
599
    //                                                                            directory with the same name already exists
600
    //                                write_protected : the file can not be extracted because a file
601
    //                                                                    with the same name already exists and is
602
    //                                                                    write protected
603
    //                                newer_exist : the file was not extracted because a newer file exists
604
    //                                path_creation_fail : the file is not extracted because the folder
605
    //                                                                         does not exist and can not be created
606
    //                                write_error : the file was not extracted because there was a
607
    //                                                            error while writing the file
608
    //                                read_error : the file was not extracted because there was a error
609
    //                                                         while reading the file
610
    //                                invalid_header : the file was not extracted because of an archive
611
    //                                                                 format error (bad file header)
612
    //     Note that each time a method can continue operating when there
613
    //     is an action error on a file, the error is only logged in the file status.
614
    // Return Values :
615
    //     0 on an unrecoverable failure,
616
    //     The list of the files in the archive.
617
    // --------------------------------------------------------------------------------
618
    public function listContent()
619
    {
620
        $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...
621
622
        // ----- Reset the error handler
623
        $this->privErrorReset();
624
625
        // ----- Check archive
626
        if (!$this->privCheckFormat()) {
627
            return 0;
628
        }
629
630
        // ----- Call the extracting fct
631
        $p_list = [];
632
        if (($v_result = $this->privList($p_list)) != 1) {
633
            unset($p_list);
634
635
            return 0;
636
        }
637
638
        // ----- Return
639
        return $p_list;
640
    }
641
    // --------------------------------------------------------------------------------
642
643
    // --------------------------------------------------------------------------------
644
    // Function :
645
    //     extract($p_path="./", $p_remove_path="")
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
646
    //     extract([$p_option, $p_option_value, ...])
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
647
    // Description :
648
    //     This method supports two synopsis. The first one is historical.
649
    //     This method extract all the files / directories from the archive to the
650
    //     folder indicated in $p_path.
651
    //     If you want to ignore the 'root' part of path of the memorized files
652
    //     you can indicate this in the optional $p_remove_path parameter.
653
    //     By default, if a newer file with the same name already exists, the
654
    //     file is not extracted.
655
    //
656
    //     If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
657
    //     are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
658
    //     at the end of the path value of PCLZIP_OPT_PATH.
659
    // Parameters :
660
    //     $p_path : Path where the files and directories are to be extracted
661
    //     $p_remove_path : First part ('root' part) of the memorized path
662
    //                                        (if any similar) to remove while extracting.
663
    // Options :
664
    //     PCLZIP_OPT_PATH :
665
    //     PCLZIP_OPT_ADD_PATH :
666
    //     PCLZIP_OPT_REMOVE_PATH :
667
    //     PCLZIP_OPT_REMOVE_ALL_PATH :
668
    //     PCLZIP_CB_PRE_EXTRACT :
669
    //     PCLZIP_CB_POST_EXTRACT :
670
    // Return Values :
671
    //     0 or a negative value on failure,
672
    //     The list of the extracted files, with a status of the action.
673
    //     (see PclZip::listContent() for list entry format)
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...
674
    // --------------------------------------------------------------------------------
675
    public function extract()
676
    {
677
        $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...
678
679
        // ----- Reset the error handler
680
        $this->privErrorReset();
681
682
        // ----- Check archive
683
        if (!$this->privCheckFormat()) {
684
            return 0;
685
        }
686
687
        // ----- Set default values
688
        $v_options = [];
689
        $v_path = '';
690
        $v_remove_path = '';
691
        $v_remove_all_path = false;
692
693
        // ----- Look for variable options arguments
694
        $v_size = func_num_args();
695
696
        // ----- Default values for option
697
        $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
698
699
        // ----- Look for arguments
700
        if ($v_size > 0) {
701
            // ----- Get the arguments
702
            $v_arg_list = func_get_args();
703
704
            // ----- Look for first arg
705
            if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
706
                // ----- Parse the options
707
                $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, [
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_PATH =>...FILE_OFF => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
708
                    PCLZIP_OPT_PATH => 'optional',
709
                    PCLZIP_OPT_REMOVE_PATH => 'optional',
710
                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
711
                    PCLZIP_OPT_ADD_PATH => 'optional',
712
                    PCLZIP_CB_PRE_EXTRACT => 'optional',
713
                    PCLZIP_CB_POST_EXTRACT => 'optional',
714
                    PCLZIP_OPT_SET_CHMOD => 'optional',
715
                    PCLZIP_OPT_BY_NAME => 'optional',
716
                    PCLZIP_OPT_BY_EREG => 'optional',
717
                    PCLZIP_OPT_BY_PREG => 'optional',
718
                    PCLZIP_OPT_BY_INDEX => 'optional',
719
                    PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
720
                    PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
721
                    PCLZIP_OPT_REPLACE_NEWER => 'optional',
722
                    PCLZIP_OPT_STOP_ON_ERROR => 'optional',
723
                    PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
724
                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
725
                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
726
                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional',
727
                ]);
728
                if ($v_result != 1) {
729
                    return 0;
730
                }
731
732
                // ----- Set the arguments
733
                if (isset($v_options[PCLZIP_OPT_PATH])) {
734
                    $v_path = $v_options[PCLZIP_OPT_PATH];
735
                }
736
                if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
737
                    $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
738
                }
739
                if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
740
                    $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
741
                }
742 View Code Duplication
                if (isset($v_options[PCLZIP_OPT_ADD_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...
743
                    // ----- Check for '/' in last path char
744
                    if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
745
                        $v_path .= '/';
746
                    }
747
                    $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
748
                }
749 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...
750
                // ----- Look for 2 args
751
                // Here we need to support the first historic synopsis of the
752
                // method.
753
                // ----- Get the first argument
754
                $v_path = $v_arg_list[0];
755
756
                // ----- Look for the optional second argument
757
                if ($v_size == 2) {
758
                    $v_remove_path = $v_arg_list[1];
759
                } elseif ($v_size > 2) {
760
                    // ----- Error log
761
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid number / type of arguments');
762
763
                    // ----- Return
764
                    return 0;
765
                }
766
            }
767
        }
768
769
        // ----- Look for default option values
770
        $this->privOptionDefaultThreshold($v_options);
771
772
        // ----- Trace
773
774
        // ----- Call the extracting fct
775
        $p_list = [];
776
        $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options);
777
        if ($v_result < 1) {
778
            unset($p_list);
779
780
            return 0;
781
        }
782
783
        // ----- Return
784
        return $p_list;
785
    }
786
    // --------------------------------------------------------------------------------
787
788
    // --------------------------------------------------------------------------------
789
    // Function :
790
    //     extractByIndex($p_index, $p_path="./", $p_remove_path="")
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
791
    //     extractByIndex($p_index, [$p_option, $p_option_value, ...])
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...
792
    // Description :
793
    //     This method supports two synopsis. The first one is historical.
794
    //     This method is doing a partial extract of the archive.
795
    //     The extracted files or folders are identified by their index in the
796
    //     archive (from 0 to n).
797
    //     Note that if the index identify a folder, only the folder entry is
798
    //     extracted, not all the files included in the archive.
799
    // Parameters :
800
    //     $p_index : A single index (integer) or a string of indexes of files to
801
    //                            extract. The form of the string is "0,4-6,8-12" with only numbers
802
    //                            and '-' for range or ',' to separate ranges. No spaces or ';'
803
    //                            are allowed.
804
    //     $p_path : Path where the files and directories are to be extracted
805
    //     $p_remove_path : First part ('root' part) of the memorized path
806
    //                                        (if any similar) to remove while extracting.
807
    // Options :
808
    //     PCLZIP_OPT_PATH :
809
    //     PCLZIP_OPT_ADD_PATH :
810
    //     PCLZIP_OPT_REMOVE_PATH :
811
    //     PCLZIP_OPT_REMOVE_ALL_PATH :
812
    //     PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
813
    //         not as files.
814
    //         The resulting content is in a new field 'content' in the file
815
    //         structure.
816
    //         This option must be used alone (any other options are ignored).
817
    //     PCLZIP_CB_PRE_EXTRACT :
818
    //     PCLZIP_CB_POST_EXTRACT :
819
    // Return Values :
820
    //     0 on failure,
821
    //     The list of the extracted files, with a status of the action.
822
    //     (see PclZip::listContent() for list entry format)
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...
823
    // --------------------------------------------------------------------------------
824
    //function extractByIndex($p_index, options...)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
825
    public function extractByIndex($p_index)
826
    {
827
        $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...
828
829
        // ----- Reset the error handler
830
        $this->privErrorReset();
831
832
        // ----- Check archive
833
        if (!$this->privCheckFormat()) {
834
            return 0;
835
        }
836
837
        // ----- Set default values
838
        $v_options = [];
839
        $v_path = '';
840
        $v_remove_path = '';
841
        $v_remove_all_path = false;
842
843
        // ----- Look for variable options arguments
844
        $v_size = func_num_args();
845
846
        // ----- Default values for option
847
        $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
848
849
        // ----- Look for arguments
850
        if ($v_size > 1) {
851
            // ----- Get the arguments
852
            $v_arg_list = func_get_args();
853
854
            // ----- Remove form the options list the first argument
855
            array_shift($v_arg_list);
856
            --$v_size;
857
858
            // ----- Look for first arg
859
            if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
860
                // ----- Parse the options
861
                $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, [
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_PATH =>...FILE_OFF => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
862
                    PCLZIP_OPT_PATH => 'optional',
863
                    PCLZIP_OPT_REMOVE_PATH => 'optional',
864
                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
865
                    PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
866
                    PCLZIP_OPT_ADD_PATH => 'optional',
867
                    PCLZIP_CB_PRE_EXTRACT => 'optional',
868
                    PCLZIP_CB_POST_EXTRACT => 'optional',
869
                    PCLZIP_OPT_SET_CHMOD => 'optional',
870
                    PCLZIP_OPT_REPLACE_NEWER => 'optional',
871
                    PCLZIP_OPT_STOP_ON_ERROR => 'optional',
872
                    PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
873
                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
874
                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
875
                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional',
876
                ]);
877
                if ($v_result != 1) {
878
                    return 0;
879
                }
880
881
                // ----- Set the arguments
882
                if (isset($v_options[PCLZIP_OPT_PATH])) {
883
                    $v_path = $v_options[PCLZIP_OPT_PATH];
884
                }
885
                if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
886
                    $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
887
                }
888
                if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
889
                    $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
890
                }
891 View Code Duplication
                if (isset($v_options[PCLZIP_OPT_ADD_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...
892
                    // ----- Check for '/' in last path char
893
                    if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
894
                        $v_path .= '/';
895
                    }
896
                    $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
897
                }
898
                if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
899
                    $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
900
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
901
                }
902 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...
903
                // ----- Look for 2 args
904
                // Here we need to support the first historic synopsis of the
905
                // method.
906
907
                // ----- Get the first argument
908
                $v_path = $v_arg_list[0];
909
910
                // ----- Look for the optional second argument
911
                if ($v_size == 2) {
912
                    $v_remove_path = $v_arg_list[1];
913
                } elseif ($v_size > 2) {
914
                    // ----- Error log
915
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid number / type of arguments');
916
917
                    // ----- Return
918
                    return 0;
919
                }
920
            }
921
        }
922
923
        // ----- Trace
924
925
        // ----- Trick
926
        // Here I want to reuse extractByRule(), so I need to parse the $p_index
927
        // with privParseOptions()
928
        $v_arg_trick = [PCLZIP_OPT_BY_INDEX, $p_index];
929
        $v_options_trick = [];
930
        $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick, [PCLZIP_OPT_BY_INDEX => 'optional']);
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_BY_INDEX => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
931
        if ($v_result != 1) {
932
            return 0;
933
        }
934
        $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
935
936
        // ----- Look for default option values
937
        $this->privOptionDefaultThreshold($v_options);
938
939
        // ----- Call the extracting fct
940
        if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
941
            return 0;
942
        }
943
944
        // ----- Return
945
        return $p_list;
946
    }
947
    // --------------------------------------------------------------------------------
948
949
    // --------------------------------------------------------------------------------
950
    // Function :
951
    //     delete([$p_option, $p_option_value, ...])
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
952
    // Description :
953
    //     This method removes files from the archive.
954
    //     If no parameters are given, then all the archive is emptied.
955
    // Parameters :
956
    //     None or optional arguments.
957
    // Options :
958
    //     PCLZIP_OPT_BY_INDEX :
959
    //     PCLZIP_OPT_BY_NAME :
960
    //     PCLZIP_OPT_BY_EREG :
961
    //     PCLZIP_OPT_BY_PREG :
962
    // Return Values :
963
    //     0 on failure,
964
    //     The list of the files which are still present in the archive.
965
    //     (see PclZip::listContent() for list entry format)
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...
966
    // --------------------------------------------------------------------------------
967
    public function delete()
968
    {
969
        $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...
970
971
        // ----- Reset the error handler
972
        $this->privErrorReset();
973
974
        // ----- Check archive
975
        if (!$this->privCheckFormat()) {
976
            return 0;
977
        }
978
979
        // ----- Set default values
980
        $v_options = [];
981
982
        // ----- Look for variable options arguments
983
        $v_size = func_num_args();
984
985
        // ----- Look for arguments
986
        if ($v_size > 0) {
987
            // ----- Get the arguments
988
            $v_arg_list = func_get_args();
989
990
            // ----- Parse the options
991
            $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, [
0 ignored issues
show
Documentation introduced by
array(PCLZIP_OPT_BY_NAME...BY_INDEX => 'optional') is of type array<integer,string>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
992
                PCLZIP_OPT_BY_NAME => 'optional',
993
                PCLZIP_OPT_BY_EREG => 'optional',
994
                PCLZIP_OPT_BY_PREG => 'optional',
995
                PCLZIP_OPT_BY_INDEX => 'optional',
996
            ]);
997
            if ($v_result != 1) {
998
                return 0;
999
            }
1000
        }
1001
1002
        // ----- Magic quotes trick
1003
        $this->privDisableMagicQuotes();
1004
1005
        // ----- Call the delete fct
1006
        $v_list = [];
1007
        if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1008
            $this->privSwapBackMagicQuotes();
1009
            unset($v_list);
1010
1011
            return 0;
1012
        }
1013
1014
        // ----- Magic quotes trick
1015
        $this->privSwapBackMagicQuotes();
1016
1017
        // ----- Return
1018
        return $v_list;
1019
    }
1020
    // --------------------------------------------------------------------------------
1021
1022
    // --------------------------------------------------------------------------------
1023
    // Function : deleteByIndex()
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...
1024
    // Description :
1025
    //     ***** Deprecated *****
1026
    //     delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1027
    // --------------------------------------------------------------------------------
1028
    public function deleteByIndex($p_index)
1029
    {
1030
        $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1031
1032
        // ----- Return
1033
        return $p_list;
1034
    }
1035
    // --------------------------------------------------------------------------------
1036
1037
    // --------------------------------------------------------------------------------
1038
    // Function : properties()
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...
1039
    // Description :
1040
    //     This method gives the properties of the archive.
1041
    //     The properties are :
1042
    //         nb : Number of files in the archive
1043
    //         comment : Comment associated with the archive file
1044
    //         status : not_exist, ok
1045
    // Parameters :
1046
    //     None
1047
    // Return Values :
1048
    //     0 on failure,
1049
    //     An array with the archive properties.
1050
    // --------------------------------------------------------------------------------
1051
    public function properties()
1052
    {
1053
1054
        // ----- Reset the error handler
1055
        $this->privErrorReset();
1056
1057
        // ----- Magic quotes trick
1058
        $this->privDisableMagicQuotes();
1059
1060
        // ----- Check archive
1061
        if (!$this->privCheckFormat()) {
1062
            $this->privSwapBackMagicQuotes();
1063
1064
            return 0;
1065
        }
1066
1067
        // ----- Default properties
1068
        $v_prop = [];
1069
        $v_prop['comment'] = '';
1070
        $v_prop['nb'] = 0;
1071
        $v_prop['status'] = 'not_exist';
1072
1073
        // ----- Look if file exists
1074
        if (@is_file($this->zipname)) {
1075
            // ----- Open the zip file
1076 View Code Duplication
            if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1077
                $this->privSwapBackMagicQuotes();
1078
1079
                // ----- Error log
1080
                self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
1081
1082
                // ----- Return
1083
                return 0;
1084
            }
1085
1086
            // ----- Read the central directory informations
1087
            $v_central_dir = [];
1088
            if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
1089
                $this->privSwapBackMagicQuotes();
1090
1091
                return 0;
1092
            }
1093
1094
            // ----- Close the zip file
1095
            $this->privCloseFd();
1096
1097
            // ----- Set the user attributes
1098
            $v_prop['comment'] = $v_central_dir['comment'];
1099
            $v_prop['nb'] = $v_central_dir['entries'];
1100
            $v_prop['status'] = 'ok';
1101
        }
1102
1103
        // ----- Magic quotes trick
1104
        $this->privSwapBackMagicQuotes();
1105
1106
        // ----- Return
1107
        return $v_prop;
1108
    }
1109
    // --------------------------------------------------------------------------------
1110
1111
    // --------------------------------------------------------------------------------
1112
    // Function : duplicate()
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...
1113
    // Description :
1114
    //     This method creates an archive by copying the content of an other one. If
1115
    //     the archive already exist, it is replaced by the new one without any warning.
1116
    // Parameters :
1117
    //     $p_archive : The filename of a valid archive, or
1118
    //                                a valid PclZip object.
1119
    // Return Values :
1120
    //     1 on success.
1121
    //     0 or a negative value on error (error code).
1122
    // --------------------------------------------------------------------------------
1123
    public function duplicate($p_archive)
1124
    {
1125
        $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...
1126
1127
        // ----- Reset the error handler
1128
        $this->privErrorReset();
1129
1130
        // ----- Look if the $p_archive is a PclZip object
1131
        if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip')) {
1132
            // ----- Duplicate the archive
1133
            $v_result = $this->privDuplicate($p_archive->zipname);
1134
        } elseif (is_string($p_archive)) {
1135
            // ----- Look if the $p_archive is a string (so a filename)
1136
            // ----- Check that $p_archive is a valid zip file
1137
            // TBC : Should also check the archive format
1138
            if (!is_file($p_archive)) {
1139
                // ----- Error log
1140
                self::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '" . $p_archive . "'");
1141
                $v_result = PCLZIP_ERR_MISSING_FILE;
1142
            } else {
1143
                // ----- Duplicate the archive
1144
                $v_result = $this->privDuplicate($p_archive);
1145
            }
1146
        } else {
1147
            // ----- Invalid variable
1148
            // ----- Error log
1149
            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid variable type p_archive_to_add');
1150
            $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1151
        }
1152
1153
        // ----- Return
1154
        return $v_result;
1155
    }
1156
    // --------------------------------------------------------------------------------
1157
1158
    // --------------------------------------------------------------------------------
1159
    // Function : merge()
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...
1160
    // Description :
1161
    //     This method merge the $p_archive_to_add archive at the end of the current
1162
    //     one ($this).
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...
1163
    //     If the archive ($this) does not exist, the merge becomes a duplicate.
1164
    //     If the $p_archive_to_add archive does not exist, the merge is a success.
1165
    // Parameters :
1166
    //     $p_archive_to_add : It can be directly the filename of a valid zip archive,
1167
    //                                             or a PclZip object archive.
1168
    // Return Values :
1169
    //     1 on success,
1170
    //     0 or negative values on error (see below).
1171
    // --------------------------------------------------------------------------------
1172
    public function merge($p_archive_to_add)
1173
    {
1174
        $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...
1175
1176
        // ----- Reset the error handler
1177
        $this->privErrorReset();
1178
1179
        // ----- Check archive
1180
        if (!$this->privCheckFormat()) {
1181
            return 0;
1182
        }
1183
1184
        // ----- Look if the $p_archive_to_add is a PclZip object
1185
        if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip')) {
1186
            // ----- Merge the archive
1187
            $v_result = $this->privMerge($p_archive_to_add);
1188
        } elseif (is_string($p_archive_to_add)) {
1189
            // ----- Look if the $p_archive_to_add is a string (so a filename)
1190
            // ----- Create a temporary archive
1191
            $v_object_archive = new self($p_archive_to_add);
1192
1193
            // ----- Merge the archive
1194
            $v_result = $this->privMerge($v_object_archive);
1195
        } else {
1196
            // ----- Invalid variable
1197
            // ----- Error log
1198
            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid variable type p_archive_to_add');
1199
            $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1200
        }
1201
1202
        // ----- Return
1203
        return $v_result;
1204
    }
1205
    // --------------------------------------------------------------------------------
1206
1207
    // --------------------------------------------------------------------------------
1208
    // Function : errorCode()
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...
1209
    // Description :
1210
    // Parameters :
1211
    // --------------------------------------------------------------------------------
1212
    public function errorCode()
1213
    {
1214
        if (PCLZIP_ERROR_EXTERNAL == 1) {
1215
            return PclErrorCode();
1216
        } else {
1217
            return $this->error_code;
1218
        }
1219
    }
1220
    // --------------------------------------------------------------------------------
1221
1222
    // --------------------------------------------------------------------------------
1223
    // Function : errorName()
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...
1224
    // Description :
1225
    // Parameters :
1226
    // --------------------------------------------------------------------------------
1227
    public function errorName($p_with_code = false)
1228
    {
1229
        $v_name = [
1230
            PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1231
            PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1232
            PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1233
            PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1234
            PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1235
            PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1236
            PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1237
            PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1238
            PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1239
            PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1240
            PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1241
            PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1242
            PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1243
            PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1244
            PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1245
            PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1246
            PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1247
            PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1248
            PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION',
1249
            PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE',
1250
            PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION',
1251
        ];
1252
1253
        if (isset($v_name[$this->error_code])) {
1254
            $v_value = $v_name[$this->error_code];
1255
        } else {
1256
            $v_value = 'NoName';
1257
        }
1258
1259
        if ($p_with_code) {
1260
            return $v_value . ' (' . $this->error_code . ')';
1261
        } else {
1262
            return $v_value;
1263
        }
1264
    }
1265
    // --------------------------------------------------------------------------------
1266
1267
    // --------------------------------------------------------------------------------
1268
    // Function : errorInfo()
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...
1269
    // Description :
1270
    // Parameters :
1271
    // --------------------------------------------------------------------------------
1272
    public function errorInfo($p_full = false)
1273
    {
1274
        if (PCLZIP_ERROR_EXTERNAL == 1) {
1275
            return PclErrorString();
1276
        } else {
1277
            if ($p_full) {
1278
                return $this->errorName(true) . ' : ' . $this->error_string;
1279
            } else {
1280
                return $this->error_string . ' [code ' . $this->error_code . ']';
1281
            }
1282
        }
1283
    }
1284
    // --------------------------------------------------------------------------------
1285
1286
    // --------------------------------------------------------------------------------
1287
    // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1288
    // *****                                                                                                                *****
1289
    // *****             THESES FUNCTIONS MUST NOT BE USED DIRECTLY             *****
1290
    // --------------------------------------------------------------------------------
1291
1292
    // --------------------------------------------------------------------------------
1293
    // Function : privCheckFormat()
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...
1294
    // Description :
1295
    //     This method check that the archive exists and is a valid zip archive.
1296
    //     Several level of check exists. (futur)
1297
    // Parameters :
1298
    //     $p_level : Level of check. Default 0.
1299
    //                            0 : Check the first bytes (magic codes) (default value))
1300
    //                            1 : 0 + Check the central directory (futur)
1301
    //                            2 : 1 + Check each file header (futur)
1302
    // Return Values :
1303
    //     true on success,
1304
    //     false on error, the error code is set.
1305
    // --------------------------------------------------------------------------------
1306
    public function privCheckFormat($p_level = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $p_level is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1307
    {
1308
        $v_result = true;
1309
1310
        // ----- Reset the file system cache
1311
        clearstatcache();
1312
1313
        // ----- Reset the error handler
1314
        $this->privErrorReset();
1315
1316
        // ----- Look if the file exits
1317
        if (!is_file($this->zipname)) {
1318
            // ----- Error log
1319
            self::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '" . $this->zipname . "'");
1320
1321
            return false;
1322
        }
1323
1324
        // ----- Check that the file is readeable
1325
        if (!is_readable($this->zipname)) {
1326
            // ----- Error log
1327
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '" . $this->zipname . "'");
1328
1329
            return false;
1330
        }
1331
1332
        // ----- Check the magic code
1333
        // TBC
1334
1335
        // ----- Check the central header
1336
        // TBC
1337
1338
        // ----- Check each file header
1339
        // TBC
1340
1341
        // ----- Return
1342
        return $v_result;
1343
    }
1344
    // --------------------------------------------------------------------------------
1345
1346
    // --------------------------------------------------------------------------------
1347
    // Function : privParseOptions()
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...
1348
    // Description :
1349
    //     This internal methods reads the variable list of arguments ($p_options_list,
1350
    //     $p_size) and generate an array with the options and values ($v_result_list).
1351
    //     $v_requested_options contains the options that can be present and those that
1352
    //     must be present.
1353
    //     $v_requested_options is an array, with the option value as key, and 'optional',
1354
    //     or 'mandatory' as value.
1355
    // Parameters :
1356
    //     See above.
1357
    // Return Values :
1358
    //     1 on success.
1359
    //     0 on failure.
1360
    // --------------------------------------------------------------------------------
1361
    public function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options = false)
1362
    {
1363
        $v_result = 1;
1364
1365
        // ----- Read the options
1366
        $i = 0;
1367
        while ($i < $p_size) {
1368
            // ----- Check if the option is supported
1369
            if (!isset($v_requested_options[$p_options_list[$i]])) {
1370
                // ----- Error log
1371
                self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '" . $p_options_list[$i] . "' for this method");
1372
1373
                // ----- Return
1374
                return self::errorCode();
1375
            }
1376
1377
            // ----- Look for next option
1378
            switch ($p_options_list[$i]) {
1379
                // ----- Look for options that request a path value
1380
                case PCLZIP_OPT_PATH:
1381
                case PCLZIP_OPT_REMOVE_PATH:
1382 View Code Duplication
                case PCLZIP_OPT_ADD_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...
1383
                    // ----- Check the number of parameters
1384
                    if (($i + 1) >= $p_size) {
1385
                        // ----- Error log
1386
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1387
1388
                        // ----- Return
1389
                        return self::errorCode();
1390
                    }
1391
1392
                    // ----- Get the value
1393
                    $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], false);
1394
                    ++$i;
1395
                    break;
1396
1397
                case PCLZIP_OPT_TEMP_FILE_THRESHOLD:
1398
                    // ----- Check the number of parameters
1399
                    if (($i + 1) >= $p_size) {
1400
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1401
1402
                        return self::errorCode();
1403
                    }
1404
1405
                    // ----- Check for incompatible options
1406 View Code Duplication
                    if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
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...
1407
                        self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1408
1409
                        return self::errorCode();
1410
                    }
1411
1412
                    // ----- Check the value
1413
                    $v_value = $p_options_list[$i + 1];
1414
                    if ((!is_integer($v_value)) || ($v_value < 0)) {
1415
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1416
1417
                        return self::errorCode();
1418
                    }
1419
1420
                    // ----- Get the value (and convert it in bytes)
1421
                    $v_result_list[$p_options_list[$i]] = $v_value * 1048576;
1422
                    ++$i;
1423
                    break;
1424
1425
                case PCLZIP_OPT_TEMP_FILE_ON:
1426
                    // ----- Check for incompatible options
1427 View Code Duplication
                    if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
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...
1428
                        self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1429
1430
                        return self::errorCode();
1431
                    }
1432
1433
                    $v_result_list[$p_options_list[$i]] = true;
1434
                    break;
1435
1436
                case PCLZIP_OPT_TEMP_FILE_OFF:
1437
                    // ----- Check for incompatible options
1438 View Code Duplication
                    if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
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...
1439
                        self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
1440
1441
                        return self::errorCode();
1442
                    }
1443
                    // ----- Check for incompatible options
1444 View Code Duplication
                    if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
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...
1445
                        self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
1446
1447
                        return self::errorCode();
1448
                    }
1449
                    $v_result_list[$p_options_list[$i]] = true;
1450
                    break;
1451
1452
                case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION:
1453
                    // ----- Check the number of parameters
1454
                    if (($i + 1) >= $p_size) {
1455
                        // ----- Error log
1456
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1457
                        // ----- Return
1458
                        return self::errorCode();
1459
                    }
1460
1461
                    // ----- Get the value
1462
                    if (is_string($p_options_list[$i + 1]) && ($p_options_list[$i + 1] != '')) {
1463
                        $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], false);
1464
                        ++$i;
1465
                    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
1466
                    }
1467
                    break;
1468
                // ----- Look for options that request an array of string for value
1469
                case PCLZIP_OPT_BY_NAME:
1470
                    // ----- Check the number of parameters
1471
                    if (($i + 1) >= $p_size) {
1472
                        // ----- Error log
1473
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1474
                        // ----- Return
1475
                        return self::errorCode();
1476
                    }
1477
1478
                    // ----- Get the value
1479
                    if (is_string($p_options_list[$i + 1])) {
1480
                        $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i + 1];
1481
                    } elseif (is_array($p_options_list[$i + 1])) {
1482
                        $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
1483
                    } else {
1484
                        // ----- Error log
1485
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1486
                        // ----- Return
1487
                        return self::errorCode();
1488
                    }
1489
                    ++$i;
1490
                    break;
1491
                // ----- Look for options that request an EREG or PREG expression
1492
                case PCLZIP_OPT_BY_EREG:
1493
                    // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
1494
                    // to PCLZIP_OPT_BY_PREG
1495
                    $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
1496
                    // no break;
1497 View Code Duplication
                case PCLZIP_OPT_BY_PREG:
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...
1498
                //case PCLZIP_OPT_CRYPT :
1499
                    // ----- Check the number of parameters
1500
                    if (($i + 1) >= $p_size) {
1501
                        // ----- Error log
1502
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1503
                        // ----- Return
1504
                        return self::errorCode();
1505
                    }
1506
1507
                    // ----- Get the value
1508
                    if (is_string($p_options_list[$i + 1])) {
1509
                        $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
1510
                    } else {
1511
                        // ----- Error log
1512
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1513
                        // ----- Return
1514
                        return self::errorCode();
1515
                    }
1516
                    ++$i;
1517
                    break;
1518
1519
                // ----- Look for options that takes a string
1520
                case PCLZIP_OPT_COMMENT:
1521
                case PCLZIP_OPT_ADD_COMMENT:
1522 View Code Duplication
                case PCLZIP_OPT_PREPEND_COMMENT:
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...
1523
                    // ----- Check the number of parameters
1524
                    if (($i + 1) >= $p_size) {
1525
                        // ----- Error log
1526
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1527
1528
                        // ----- Return
1529
                        return self::errorCode();
1530
                    }
1531
1532
                    // ----- Get the value
1533
                    if (is_string($p_options_list[$i + 1])) {
1534
                        $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
1535
                    } else {
1536
                        // ----- Error log
1537
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1538
1539
                        // ----- Return
1540
                        return self::errorCode();
1541
                    }
1542
                    ++$i;
1543
                    break;
1544
1545
                // ----- Look for options that request an array of index
1546
                case PCLZIP_OPT_BY_INDEX:
1547
                    // ----- Check the number of parameters
1548
                    if (($i + 1) >= $p_size) {
1549
                        // ----- Error log
1550
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1551
1552
                        // ----- Return
1553
                        return self::errorCode();
1554
                    }
1555
1556
                    // ----- Get the value
1557
                    $v_work_list = [];
1558
                    if (is_string($p_options_list[$i + 1])) {
1559
                        // ----- Remove spaces
1560
                        $p_options_list[$i + 1] = strtr($p_options_list[$i + 1], ' ', '');
1561
1562
                        // ----- Parse items
1563
                        $v_work_list = explode(',', $p_options_list[$i + 1]);
1564
                    } elseif (is_integer($p_options_list[$i + 1])) {
1565
                        $v_work_list[0] = $p_options_list[$i + 1] . '-' . $p_options_list[$i + 1];
1566
                    } elseif (is_array($p_options_list[$i + 1])) {
1567
                        $v_work_list = $p_options_list[$i + 1];
1568
                    } else {
1569
                        // ----- Error log
1570
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1571
1572
                        // ----- Return
1573
                        return self::errorCode();
1574
                    }
1575
1576
                    // ----- Reduce the index list
1577
                    // each index item in the list must be a couple with a start and
1578
                    // an end value : [0,3], [5-5], [8-10], ...
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
1579
                    // ----- Check the format of each item
1580
                    $v_sort_flag = false;
1581
                    $v_sort_value = 0;
1582
                    for ($j = 0; $j < sizeof($v_work_list); ++$j) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
1583
                        // ----- Explode the item
1584
                        $v_item_list = explode('-', $v_work_list[$j]);
1585
                        $v_size_item_list = sizeof($v_item_list);
1586
1587
                        // ----- TBC : Here we might check that each item is a
1588
                        // real integer ...
1589
1590
                        // ----- Look for single value
1591
                        if ($v_size_item_list == 1) {
1592
                            // ----- Set the option value
1593
                            $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1594
                            $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1595
                        } elseif ($v_size_item_list == 2) {
1596
                            // ----- Set the option value
1597
                            $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1598
                            $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1599
                        } else {
1600
                            // ----- Error log
1601
                            self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1602
1603
                            // ----- Return
1604
                            return self::errorCode();
1605
                        }
1606
1607
                        // ----- Look for list sort
1608
                        if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1609
                            $v_sort_flag = true;
0 ignored issues
show
Unused Code introduced by
$v_sort_flag 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...
1610
1611
                            // ----- TBC : An automatic sort should be writen ...
1612
                            // ----- Error log
1613
                            self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1614
1615
                            // ----- Return
1616
                            return self::errorCode();
1617
                        }
1618
                        $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1619
                    }
1620
1621
                    // ----- Sort the items
1622
                    if ($v_sort_flag) {
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...
1623
                        // TBC : To Be Completed
1624
                    }
1625
                    // ----- Next option
1626
                    ++$i;
1627
                    break;
1628
                // ----- Look for options that request no value
1629
                case PCLZIP_OPT_REMOVE_ALL_PATH:
1630
                case PCLZIP_OPT_EXTRACT_AS_STRING:
1631
                case PCLZIP_OPT_NO_COMPRESSION:
1632
                case PCLZIP_OPT_EXTRACT_IN_OUTPUT:
1633
                case PCLZIP_OPT_REPLACE_NEWER:
1634
                case PCLZIP_OPT_STOP_ON_ERROR:
1635
                    $v_result_list[$p_options_list[$i]] = true;
1636
                    break;
1637
                // ----- Look for options that request an octal value
1638 View Code Duplication
                case PCLZIP_OPT_SET_CHMOD:
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...
1639
                    // ----- Check the number of parameters
1640
                    if (($i + 1) >= $p_size) {
1641
                        // ----- Error log
1642
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1643
                        // ----- Return
1644
                        return self::errorCode();
1645
                    }
1646
                    // ----- Get the value
1647
                    $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
1648
                    ++$i;
1649
                    break;
1650
1651
                // ----- Look for options that request a call-back
1652
                case PCLZIP_CB_PRE_EXTRACT:
1653
                case PCLZIP_CB_POST_EXTRACT:
1654
                case PCLZIP_CB_PRE_ADD:
1655
                case PCLZIP_CB_POST_ADD:
1656
                /* for futur use
1657
                case PCLZIP_CB_PRE_DELETE :
1658
                case PCLZIP_CB_POST_DELETE :
1659
                case PCLZIP_CB_PRE_LIST :
1660
                case PCLZIP_CB_POST_LIST :
1661
                */
1662
                    // ----- Check the number of parameters
1663
                    if (($i + 1) >= $p_size) {
1664
                        // ----- Error log
1665
                        self::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1666
                        // ----- Return
1667
                        return self::errorCode();
1668
                    }
1669
1670
                    // ----- Get the value
1671
                    $v_function_name = $p_options_list[$i + 1];
1672
1673
                    // ----- Check that the value is a valid existing function
1674
                    if (!function_exists($v_function_name)) {
1675
                        // ----- Error log
1676
                        self::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '" . $v_function_name . "()' is not an existing function for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
1677
                        // ----- Return
1678
                        return self::errorCode();
1679
                    }
1680
1681
                    // ----- Set the attribute
1682
                    $v_result_list[$p_options_list[$i]] = $v_function_name;
1683
                    ++$i;
1684
                    break;
1685
                default:
1686
                    // ----- Error log
1687
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" . $p_options_list[$i] . "'");
1688
1689
                    // ----- Return
1690
                    return self::errorCode();
1691
            }
1692
1693
            // ----- Next options
1694
            ++$i;
1695
        }
1696
1697
        // ----- Look for mandatory options
1698 View Code Duplication
        if ($v_requested_options !== false) {
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...
1699
            for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
0 ignored issues
show
Unused Code introduced by
$key 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...
1700
                // ----- Look for mandatory option
1701
                if ($v_requested_options[$key] == 'mandatory') {
1702
                    // ----- Look if present
1703
                    if (!isset($v_result_list[$key])) {
1704
                        // ----- Error log
1705
                        self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Missing mandatory parameter ' . PclZipUtilOptionText($key) . '(' . $key . ')');
1706
1707
                        // ----- Return
1708
                        return self::errorCode();
1709
                    }
1710
                }
1711
            }
1712
        }
1713
1714
        // ----- Look for default values
1715
        if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
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...
1716
        }
1717
1718
        // ----- Return
1719
        return $v_result;
1720
    }
1721
    // --------------------------------------------------------------------------------
1722
1723
    // --------------------------------------------------------------------------------
1724
    // Function : privOptionDefaultThreshold()
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...
1725
    // Description :
1726
    // Parameters :
1727
    // Return Values :
1728
    // --------------------------------------------------------------------------------
1729
    public function privOptionDefaultThreshold(&$p_options)
1730
    {
1731
        $v_result = 1;
1732
1733
        if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
1734
            return $v_result;
1735
        }
1736
1737
        // ----- Get 'memory_limit' configuration value
1738
        $v_memory_limit = ini_get('memory_limit');
1739
        $v_memory_limit = trim($v_memory_limit);
1740
        $last = strtolower(substr($v_memory_limit, -1));
1741
1742
        if ($last == 'g') {
1743
            //$v_memory_limit = $v_memory_limit*1024*1024*1024;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
1744
            $v_memory_limit = $v_memory_limit * 1073741824;
1745
        }
1746
        if ($last == 'm') {
1747
            //$v_memory_limit = $v_memory_limit*1024*1024;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
1748
            $v_memory_limit = $v_memory_limit * 1048576;
1749
        }
1750
        if ($last == 'k') {
1751
            $v_memory_limit = $v_memory_limit * 1024;
1752
        }
1753
1754
        $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit * PCLZIP_TEMPORARY_FILE_RATIO);
1755
1756
        // ----- Sanity check : No threshold if value lower than 1M
1757
        if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
1758
            unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
1759
        }
1760
1761
        // ----- Return
1762
        return $v_result;
1763
    }
1764
    // --------------------------------------------------------------------------------
1765
1766
    // --------------------------------------------------------------------------------
1767
    // Function : privFileDescrParseAtt()
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...
1768
    // Description :
1769
    // Parameters :
1770
    // Return Values :
1771
    //     1 on success.
1772
    //     0 on failure.
1773
    // --------------------------------------------------------------------------------
1774
    public function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options = false)
0 ignored issues
show
Unused Code introduced by
The parameter $v_options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1775
    {
1776
        $v_result = 1;
1777
1778
        // ----- For each file in the list check the attributes
1779
        foreach ($p_file_list as $v_key => $v_value) {
1780
            // ----- Check if the option is supported
1781
            if (!isset($v_requested_options[$v_key])) {
1782
                // ----- Error log
1783
                self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '" . $v_key . "' for this file");
1784
1785
                // ----- Return
1786
                return self::errorCode();
1787
            }
1788
1789
            // ----- Look for attribute
1790
            switch ($v_key) {
1791 View Code Duplication
                case PCLZIP_ATT_FILE_NAME:
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...
1792
                    if (!is_string($v_value)) {
1793
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, 'Invalid type ' . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
1794
1795
                        return self::errorCode();
1796
                    }
1797
1798
                    $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
1799
1800
                    if ($p_filedescr['filename'] == '') {
1801
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
1802
1803
                        return self::errorCode();
1804
                    }
1805
                    break;
1806 View Code Duplication
                case PCLZIP_ATT_FILE_NEW_SHORT_NAME:
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...
1807
                    if (!is_string($v_value)) {
1808
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, 'Invalid type ' . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
1809
1810
                        return self::errorCode();
1811
                    }
1812
1813
                    $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
1814
1815
                    if ($p_filedescr['new_short_name'] == '') {
1816
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
1817
1818
                        return self::errorCode();
1819
                    }
1820
                    break;
1821 View Code Duplication
                case PCLZIP_ATT_FILE_NEW_FULL_NAME:
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...
1822
                    if (!is_string($v_value)) {
1823
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, 'Invalid type ' . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
1824
1825
                        return self::errorCode();
1826
                    }
1827
1828
                    $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
1829
1830
                    if ($p_filedescr['new_full_name'] == '') {
1831
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
1832
1833
                        return self::errorCode();
1834
                    }
1835
                    break;
1836
                // ----- Look for options that takes a string
1837 View Code Duplication
                case PCLZIP_ATT_FILE_COMMENT:
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...
1838
                    if (!is_string($v_value)) {
1839
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, 'Invalid type ' . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
1840
1841
                        return self::errorCode();
1842
                    }
1843
                    $p_filedescr['comment'] = $v_value;
1844
                    break;
1845 View Code Duplication
                case PCLZIP_ATT_FILE_MTIME:
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...
1846
                    if (!is_integer($v_value)) {
1847
                        self::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, 'Invalid type ' . gettype($v_value) . ". Integer expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
1848
1849
                        return self::errorCode();
1850
                    }
1851
                    $p_filedescr['mtime'] = $v_value;
1852
                    break;
1853
                case PCLZIP_ATT_FILE_CONTENT:
1854
                    $p_filedescr['content'] = $v_value;
1855
                    break;
1856
                default:
1857
                    // ----- Error log
1858
                    self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" . $v_key . "'");
1859
1860
                    // ----- Return
1861
                    return self::errorCode();
1862
            }
1863
1864
            // ----- Look for mandatory options
1865 View Code Duplication
            if ($v_requested_options !== false) {
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...
1866
                for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
0 ignored issues
show
Unused Code introduced by
$key 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...
1867
                    // ----- Look for mandatory option
1868
                    if ($v_requested_options[$key] == 'mandatory') {
1869
                        // ----- Look if present
1870
                        if (!isset($p_file_list[$key])) {
1871
                            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Missing mandatory parameter ' . PclZipUtilOptionText($key) . '(' . $key . ')');
1872
1873
                            return self::errorCode();
1874
                        }
1875
                    }
1876
                }
1877
            }
1878
        }
1879
1880
        // ----- Return
1881
        return $v_result;
1882
    }
1883
    // --------------------------------------------------------------------------------
1884
1885
    // --------------------------------------------------------------------------------
1886
    // Function : privFileDescrExpand()
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...
1887
    // Description :
1888
    //     This method look for each item of the list to see if its a file, a folder
1889
    //     or a string to be added as file. For any other type of files (link, other)
1890
    //     just ignore the item.
1891
    //     Then prepare the information that will be stored for that file.
1892
    //     When its a folder, expand the folder with all the files that are in that
1893
    //     folder (recursively).
1894
    // Parameters :
1895
    // Return Values :
1896
    //     1 on success.
1897
    //     0 on failure.
1898
    // --------------------------------------------------------------------------------
1899
    public function privFileDescrExpand(&$p_filedescr_list, &$p_options)
1900
    {
1901
        $v_result = 1;
1902
1903
        // ----- Create a result list
1904
        $v_result_list = [];
1905
1906
        // ----- Look each entry
1907
        for ($i = 0; $i < sizeof($p_filedescr_list); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
1908
            // ----- Get filedescr
1909
            $v_descr = $p_filedescr_list[$i];
1910
1911
            // ----- Reduce the filename
1912
            $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
1913
            $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
1914
1915
            // ----- Look for real file or folder
1916
            if (file_exists($v_descr['filename'])) {
1917
                if (@is_file($v_descr['filename'])) {
1918
                    $v_descr['type'] = 'file';
1919
                } elseif (@is_dir($v_descr['filename'])) {
1920
                    $v_descr['type'] = 'folder';
1921
                } elseif (@is_link($v_descr['filename'])) {
1922
                    // skip
1923
                    continue;
1924
                } else {
1925
                    // skip
1926
                    continue;
1927
                }
1928
            } elseif (isset($v_descr['content'])) {
1929
                // ----- Look for string added as file
1930
                $v_descr['type'] = 'virtual_file';
1931
            } else {
1932
                // ----- Missing file
1933
                // ----- Error log
1934
                self::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $v_descr['filename'] . "' does not exist");
1935
1936
                // ----- Return
1937
                return self::errorCode();
1938
            }
1939
1940
            // ----- Calculate the stored filename
1941
            $this->privCalculateStoredFilename($v_descr, $p_options);
1942
1943
            // ----- Add the descriptor in result list
1944
            $v_result_list[sizeof($v_result_list)] = $v_descr;
1945
1946
            // ----- Look for folder
1947
            if ($v_descr['type'] == 'folder') {
1948
                // ----- List of items in folder
1949
                $v_dirlist_descr = [];
1950
                $v_dirlist_nb = 0;
1951
                if ($v_folder_handler = @opendir($v_descr['filename'])) {
1952
                    while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
1953
                        // ----- Skip '.' and '..'
1954
                        if (($v_item_handler == '.') || ($v_item_handler == '..')) {
1955
                            continue;
1956
                        }
1957
1958
                        // ----- Compose the full filename
1959
                        $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'] . '/' . $v_item_handler;
1960
1961
                        // ----- Look for different stored filename
1962
                        // Because the name of the folder was changed, the name of the
1963
                        // files/sub-folders also change
1964
                        if (($v_descr['stored_filename'] != $v_descr['filename'])
1965
                                 && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
1966
                            if ($v_descr['stored_filename'] != '') {
1967
                                $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'] . '/' . $v_item_handler;
1968
                            } else {
1969
                                $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
1970
                            }
1971
                        }
1972
                        ++$v_dirlist_nb;
1973
                    }
1974
1975
                    @closedir($v_folder_handler);
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...
1976
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
1977
                    // TBC : unable to open folder in read mode
1978
                }
1979
1980
                // ----- Expand each element of the list
1981
                if ($v_dirlist_nb != 0) {
1982
                    // ----- Expand
1983
                    if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
1984
                        return $v_result;
1985
                    }
1986
1987
                    // ----- Concat the resulting list
1988
                    $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
1989
                }
1990
1991
                // ----- Free local array
1992
                unset($v_dirlist_descr);
1993
            }
1994
        }
1995
1996
        // ----- Get the result list
1997
        $p_filedescr_list = $v_result_list;
1998
1999
        // ----- Return
2000
        return $v_result;
2001
    }
2002
    // --------------------------------------------------------------------------------
2003
2004
    // --------------------------------------------------------------------------------
2005
    // Function : privCreate()
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...
2006
    // Description :
2007
    // Parameters :
2008
    // Return Values :
2009
    // --------------------------------------------------------------------------------
2010
    public function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2011
    {
2012
        $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...
2013
        $v_list_detail = [];
0 ignored issues
show
Unused Code introduced by
$v_list_detail 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...
2014
2015
        // ----- Magic quotes trick
2016
        $this->privDisableMagicQuotes();
2017
2018
        // ----- Open the file in write mode
2019
        if (($v_result = $this->privOpenFd('wb')) != 1) {
2020
            // ----- Return
2021
            return $v_result;
2022
        }
2023
2024
        // ----- Add the list of files
2025
        $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2026
2027
        // ----- Close
2028
        $this->privCloseFd();
2029
2030
        // ----- Magic quotes trick
2031
        $this->privSwapBackMagicQuotes();
2032
2033
        // ----- Return
2034
        return $v_result;
2035
    }
2036
    // --------------------------------------------------------------------------------
2037
2038
    // --------------------------------------------------------------------------------
2039
    // Function : privAdd()
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...
2040
    // Description :
2041
    // Parameters :
2042
    // Return Values :
2043
    // --------------------------------------------------------------------------------
2044
    public function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2045
    {
2046
        $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...
2047
        $v_list_detail = [];
0 ignored issues
show
Unused Code introduced by
$v_list_detail 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...
2048
2049
        // ----- Look if the archive exists or is empty
2050
        if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0)) {
2051
            // ----- Do a create
2052
            $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2053
2054
            // ----- Return
2055
            return $v_result;
2056
        }
2057
        // ----- Magic quotes trick
2058
        $this->privDisableMagicQuotes();
2059
2060
        // ----- Open the zip file
2061
        if (($v_result = $this->privOpenFd('rb')) != 1) {
2062
            // ----- Magic quotes trick
2063
            $this->privSwapBackMagicQuotes();
2064
2065
            // ----- Return
2066
            return $v_result;
2067
        }
2068
2069
        // ----- Read the central directory informations
2070
        $v_central_dir = [];
2071
        if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
2072
            $this->privCloseFd();
2073
            $this->privSwapBackMagicQuotes();
2074
2075
            return $v_result;
2076
        }
2077
2078
        // ----- Go to beginning of File
2079
        @rewind($this->zip_fd);
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...
2080
2081
        // ----- Creates a temporay file
2082
        $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
2083
2084
        // ----- Open the temporary file in write mode
2085
        if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
2086
            $this->privCloseFd();
2087
            $this->privSwapBackMagicQuotes();
2088
2089
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
2090
2091
            // ----- Return
2092
            return self::errorCode();
2093
        }
2094
2095
        // ----- Copy the files from the archive to the temporary file
2096
        // TBC : Here I should better append the file and go back to erase the central dir
2097
        $v_size = $v_central_dir['offset'];
2098
        while ($v_size != 0) {
2099
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2100
            $v_buffer = fread($this->zip_fd, $v_read_size);
2101
            @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
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...
2102
            $v_size -= $v_read_size;
2103
        }
2104
2105
        // ----- Swap the file descriptor
2106
        // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2107
        // the following methods on the temporary fil and not the real archive
2108
        $v_swap = $this->zip_fd;
2109
        $this->zip_fd = $v_zip_temp_fd;
2110
        $v_zip_temp_fd = $v_swap;
2111
2112
        // ----- Add the files
2113
        $v_header_list = [];
2114 View Code Duplication
        if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2115
            fclose($v_zip_temp_fd);
2116
            $this->privCloseFd();
2117
            @unlink($v_zip_temp_name);
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...
2118
            $this->privSwapBackMagicQuotes();
2119
2120
            // ----- Return
2121
            return $v_result;
2122
        }
2123
2124
        // ----- Store the offset of the central dir
2125
        $v_offset = @ftell($this->zip_fd);
2126
2127
        // ----- Copy the block of file headers from the old archive
2128
        $v_size = $v_central_dir['size'];
2129 View Code Duplication
        while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2130
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2131
            $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
2132
            @fwrite($this->zip_fd, $v_buffer, $v_read_size);
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...
2133
            $v_size -= $v_read_size;
2134
        }
2135
2136
        // ----- Create the Central Dir files header
2137
        for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
2138
            // ----- Create the file header
2139
            if ($v_header_list[$i]['status'] == 'ok') {
2140 View Code Duplication
                if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2141
                    fclose($v_zip_temp_fd);
2142
                    $this->privCloseFd();
2143
                    @unlink($v_zip_temp_name);
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...
2144
                    $this->privSwapBackMagicQuotes();
2145
2146
                    // ----- Return
2147
                    return $v_result;
2148
                }
2149
                ++$v_count;
2150
            }
2151
2152
            // ----- Transform the header to a 'usable' info
2153
            $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2154
        }
2155
2156
        // ----- Zip file comment
2157
        $v_comment = $v_central_dir['comment'];
2158
        if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2159
            $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2160
        }
2161
        if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
2162
            $v_comment = $v_comment . $p_options[PCLZIP_OPT_ADD_COMMENT];
2163
        }
2164
        if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
2165
            $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT] . $v_comment;
2166
        }
2167
2168
        // ----- Calculate the size of the central header
2169
        $v_size = @ftell($this->zip_fd) - $v_offset;
2170
2171
        // ----- Create the central dir footer
2172
        if (($v_result = $this->privWriteCentralHeader($v_count + $v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
2173
            // ----- Reset the file list
2174
            unset($v_header_list);
2175
            $this->privSwapBackMagicQuotes();
2176
2177
            // ----- Return
2178
            return $v_result;
2179
        }
2180
2181
        // ----- Swap back the file descriptor
2182
        $v_swap = $this->zip_fd;
2183
        $this->zip_fd = $v_zip_temp_fd;
2184
        $v_zip_temp_fd = $v_swap;
2185
2186
        // ----- Close
2187
        $this->privCloseFd();
2188
2189
        // ----- Close the temporary file
2190
        @fclose($v_zip_temp_fd);
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...
2191
2192
        // ----- Magic quotes trick
2193
        $this->privSwapBackMagicQuotes();
2194
2195
        // ----- Delete the zip file
2196
        // TBC : I should test the result ...
2197
        @unlink($this->zipname);
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...
2198
2199
        // ----- Rename the temporary file
2200
        // TBC : I should test the result ...
2201
        //@rename($v_zip_temp_name, $this->zipname);
2202
        PclZipUtilRename($v_zip_temp_name, $this->zipname);
2203
2204
        // ----- Return
2205
        return $v_result;
2206
    }
2207
    // --------------------------------------------------------------------------------
2208
2209
    // --------------------------------------------------------------------------------
2210
    // Function : privOpenFd()
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...
2211
    // Description :
2212
    // Parameters :
2213
    // --------------------------------------------------------------------------------
2214
    public function privOpenFd($p_mode)
2215
    {
2216
        $v_result = 1;
2217
2218
        // ----- Look if already open
2219
        if ($this->zip_fd != 0) {
2220
            // ----- Error log
2221
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \'' . $this->zipname . '\' already open');
2222
2223
            // ----- Return
2224
            return self::errorCode();
2225
        }
2226
2227
        // ----- Open the zip file
2228
        if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) {
2229
            // ----- Error log
2230
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in ' . $p_mode . ' mode');
2231
2232
            // ----- Return
2233
            return self::errorCode();
2234
        }
2235
2236
        // ----- Return
2237
        return $v_result;
2238
    }
2239
    // --------------------------------------------------------------------------------
2240
2241
    // --------------------------------------------------------------------------------
2242
    // Function : privCloseFd()
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...
2243
    // Description :
2244
    // Parameters :
2245
    // --------------------------------------------------------------------------------
2246
    public function privCloseFd()
2247
    {
2248
        $v_result = 1;
2249
2250
        if ($this->zip_fd != 0) {
2251
            @fclose($this->zip_fd);
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...
2252
        }
2253
        $this->zip_fd = 0;
2254
2255
        // ----- Return
2256
        return $v_result;
2257
    }
2258
    // --------------------------------------------------------------------------------
2259
2260
    // --------------------------------------------------------------------------------
2261
    // Function : privAddList()
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...
2262
    // Description :
2263
    //     $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2264
    //     different from the real path of the file. This is usefull if you want to have PclTar
2265
    //     running in any directory, and memorize relative path from an other directory.
2266
    // Parameters :
2267
    //     $p_list : An array containing the file or directory names to add in the tar
2268
    //     $p_result_list : list of added files with their properties (specially the status field)
2269
    //     $p_add_dir : Path to add in the filename path archived
2270
    //     $p_remove_dir : Path to remove in the filename path archived
2271
    // Return Values :
2272
    // --------------------------------------------------------------------------------
2273
    //    public function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
2274
    public function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2275
    {
2276
        $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...
2277
2278
        // ----- Add the files
2279
        $v_header_list = [];
2280
        if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
2281
            // ----- Return
2282
            return $v_result;
2283
        }
2284
2285
        // ----- Store the offset of the central dir
2286
        $v_offset = @ftell($this->zip_fd);
2287
2288
        // ----- Create the Central Dir files header
2289
        for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
2290
            // ----- Create the file header
2291
            if ($v_header_list[$i]['status'] == 'ok') {
2292
                if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2293
                    // ----- Return
2294
                    return $v_result;
2295
                }
2296
                ++$v_count;
2297
            }
2298
2299
            // ----- Transform the header to a 'usable' info
2300
            $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2301
        }
2302
2303
        // ----- Zip file comment
2304
        $v_comment = '';
2305
        if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2306
            $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2307
        }
2308
2309
        // ----- Calculate the size of the central header
2310
        $v_size = @ftell($this->zip_fd) - $v_offset;
2311
2312
        // ----- Create the central dir footer
2313
        if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
2314
            // ----- Reset the file list
2315
            unset($v_header_list);
2316
2317
            // ----- Return
2318
            return $v_result;
2319
        }
2320
2321
        // ----- Return
2322
        return $v_result;
2323
    }
2324
    // --------------------------------------------------------------------------------
2325
2326
    // --------------------------------------------------------------------------------
2327
    // Function : privAddFileList()
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...
2328
    // Description :
2329
    // Parameters :
2330
    //     $p_filedescr_list : An array containing the file description
2331
    //                                            or directory names to add in the zip
2332
    //     $p_result_list : list of added files with their properties (specially the status field)
2333
    // Return Values :
2334
    // --------------------------------------------------------------------------------
2335
    public function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2336
    {
2337
        $v_result = 1;
2338
        $v_header = [];
2339
2340
        // ----- Recuperate the current number of elt in list
2341
        $v_nb = sizeof($p_result_list);
2342
2343
        // ----- Loop on the files
2344
        for ($j = 0; ($j < sizeof($p_filedescr_list)) && ($v_result == 1); ++$j) {
2345
            // ----- Format the filename
2346
            $p_filedescr_list[$j]['filename'] = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2347
2348
            // ----- Skip empty file names
2349
            // TBC : Can this be possible ? not checked in DescrParseAtt ?
2350
            if ($p_filedescr_list[$j]['filename'] == '') {
2351
                continue;
2352
            }
2353
2354
            // ----- Check the filename
2355
            if (($p_filedescr_list[$j]['type'] != 'virtual_file') && (!file_exists($p_filedescr_list[$j]['filename']))) {
2356
                self::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $p_filedescr_list[$j]['filename'] . "' does not exist");
2357
2358
                return self::errorCode();
2359
            }
2360
2361
            // ----- Look if it is a file or a dir with no all path remove option
2362
            // or a dir with all its path removed
2363
    //            if (    (is_file($p_filedescr_list[$j]['filename']))
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
2364
    //                    || (    is_dir($p_filedescr_list[$j]['filename'])
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...
2365
            if (($p_filedescr_list[$j]['type'] == 'file') || ($p_filedescr_list[$j]['type'] == 'virtual_file') || (($p_filedescr_list[$j]['type'] == 'folder') && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]) || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
2366
                // ----- Add the file
2367
                $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header, $p_options);
2368
                if ($v_result != 1) {
2369
                    return $v_result;
2370
                }
2371
2372
                // ----- Store the file infos
2373
                $p_result_list[$v_nb++] = $v_header;
2374
            }
2375
        }
2376
2377
        // ----- Return
2378
        return $v_result;
2379
    }
2380
    // --------------------------------------------------------------------------------
2381
2382
    // --------------------------------------------------------------------------------
2383
    // Function : privAddFile()
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...
2384
    // Description :
2385
    // Parameters :
2386
    // Return Values :
2387
    // --------------------------------------------------------------------------------
2388
    public function privAddFile($p_filedescr, &$p_header, &$p_options)
2389
    {
2390
        $v_result = 1;
2391
2392
        // ----- Working variable
2393
        $p_filename = $p_filedescr['filename'];
2394
2395
        // TBC : Already done in the fileAtt check ... ?
2396
        if ($p_filename == '') {
2397
            // ----- Error log
2398
            self::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, 'Invalid file list parameter (invalid or empty list)');
2399
2400
            // ----- Return
2401
            return self::errorCode();
2402
        }
2403
2404
        // ----- Look for a stored different filename
2405
        /* TBC : Removed
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
2406
        if (isset($p_filedescr['stored_filename'])) {
2407
            $v_stored_filename = $p_filedescr['stored_filename'];
2408
        }
2409
        else {
2410
            $v_stored_filename = $p_filedescr['stored_filename'];
2411
        }
2412
        */
2413
2414
        // ----- Set the file properties
2415
        clearstatcache();
2416
        $p_header['version'] = 20;
2417
        $p_header['version_extracted'] = 10;
2418
        $p_header['flag'] = 0;
2419
        $p_header['compression'] = 0;
2420
        $p_header['crc'] = 0;
2421
        $p_header['compressed_size'] = 0;
2422
        $p_header['filename_len'] = strlen($p_filename);
2423
        $p_header['extra_len'] = 0;
2424
        $p_header['disk'] = 0;
2425
        $p_header['internal'] = 0;
2426
        $p_header['offset'] = 0;
2427
        $p_header['filename'] = $p_filename;
2428
    // TBC : Removed        $p_header['stored_filename'] = $v_stored_filename;
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% 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...
2429
        $p_header['stored_filename'] = $p_filedescr['stored_filename'];
2430
        $p_header['extra'] = '';
2431
        $p_header['status'] = 'ok';
2432
        $p_header['index'] = -1;
2433
2434
        // ----- Look for regular file
2435
        if ($p_filedescr['type'] == 'file') {
2436
            $p_header['external'] = 0x00000000;
2437
            $p_header['size'] = filesize($p_filename);
2438
        } elseif ($p_filedescr['type'] == 'folder') {
2439
            // ----- Look for regular folder
2440
            $p_header['external'] = 0x00000010;
2441
            $p_header['mtime'] = filemtime($p_filename);
2442
            $p_header['size'] = filesize($p_filename);
2443
        } elseif ($p_filedescr['type'] == 'virtual_file') {
2444
            // ----- Look for virtual file
2445
            $p_header['external'] = 0x00000000;
2446
            $p_header['size'] = strlen($p_filedescr['content']);
2447
        }
2448
2449
        // ----- Look for filetime
2450
        if (isset($p_filedescr['mtime'])) {
2451
            $p_header['mtime'] = $p_filedescr['mtime'];
2452
        } elseif ($p_filedescr['type'] == 'virtual_file') {
2453
            $p_header['mtime'] = time();
2454
        } else {
2455
            $p_header['mtime'] = filemtime($p_filename);
2456
        }
2457
2458
        // ------ Look for file comment
2459
        if (isset($p_filedescr['comment'])) {
2460
            $p_header['comment_len'] = strlen($p_filedescr['comment']);
2461
            $p_header['comment'] = $p_filedescr['comment'];
2462
        } else {
2463
            $p_header['comment_len'] = 0;
2464
            $p_header['comment'] = '';
2465
        }
2466
2467
        // ----- Look for pre-add callback
2468
        if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2469
            // ----- Generate a local information
2470
            $v_local_header = [];
2471
            $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2472
2473
            // ----- Call the callback
2474
            // Here I do not use call_user_func() because I need to send a reference to the
2475
            // header.
2476
    //            eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
2477
            $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
2478
            if ($v_result == 0) {
2479
                // ----- Change the file status
2480
                $p_header['status'] = 'skipped';
2481
                $v_result = 1;
2482
            }
2483
2484
            // ----- Update the informations
2485
            // Only some fields can be modified
2486
            if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2487
                $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2488
            }
2489
        }
2490
2491
        // ----- Look for empty stored filename
2492
        if ($p_header['stored_filename'] == '') {
2493
            $p_header['status'] = 'filtered';
2494
        }
2495
2496
        // ----- Check the path length
2497
        if (strlen($p_header['stored_filename']) > 0xFF) {
2498
            $p_header['status'] = 'filename_too_long';
2499
        }
2500
2501
        // ----- Look if no error, or file not skipped
2502
        if ($p_header['status'] == 'ok') {
2503
            // ----- Look for a file
2504
            if ($p_filedescr['type'] == 'file') {
2505
                // ----- Look for using temporary file to zip
2506
                if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON]) || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])))) {
2507
                    $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
2508
                    if ($v_result < PCLZIP_ERR_NO_ERROR) {
2509
                        return $v_result;
2510
                    }
2511
                } else {
2512
                    // ----- Use "in memory" zip algo
2513
                    // ----- Open the source file
2514 View Code Duplication
                    if (($v_file = @fopen($p_filename, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2515
                        self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2516
2517
                        return self::errorCode();
2518
                    }
2519
2520
                    // ----- Read the file content
2521
                    $v_content = @fread($v_file, $p_header['size']);
2522
2523
                    // ----- Close the file
2524
                    @fclose($v_file);
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...
2525
2526
                    // ----- Calculate the CRC
2527
                    $p_header['crc'] = @crc32($v_content);
2528
2529
                    // ----- Look for no compression
2530 View Code Duplication
                    if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
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...
2531
                        // ----- Set header parameters
2532
                        $p_header['compressed_size'] = $p_header['size'];
2533
                        $p_header['compression'] = 0;
2534
                    } else {
2535
                        // ----- Look for normal compression
2536
                        // ----- Compress the content
2537
                        $v_content = @gzdeflate($v_content);
2538
2539
                        // ----- Set header parameters
2540
                        $p_header['compressed_size'] = strlen($v_content);
2541
                        $p_header['compression'] = 8;
2542
                    }
2543
2544
                    // ----- Call the header generation
2545
                    if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2546
                        @fclose($v_file);
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...
2547
2548
                        return $v_result;
2549
                    }
2550
2551
                    // ----- Write the compressed (or not) content
2552
                    @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
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...
2553
                }
2554
            } elseif ($p_filedescr['type'] == 'virtual_file') {
2555
                // ----- Look for a virtual file (a file from string)
2556
                $v_content = $p_filedescr['content'];
2557
2558
                // ----- Calculate the CRC
2559
                $p_header['crc'] = @crc32($v_content);
2560
2561
                // ----- Look for no compression
2562 View Code Duplication
                if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
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...
2563
                    // ----- Set header parameters
2564
                    $p_header['compressed_size'] = $p_header['size'];
2565
                    $p_header['compression'] = 0;
2566
                } else {
2567
                    // ----- Look for normal compression
2568
                    // ----- Compress the content
2569
                    $v_content = @gzdeflate($v_content);
2570
2571
                    // ----- Set header parameters
2572
                    $p_header['compressed_size'] = strlen($v_content);
2573
                    $p_header['compression'] = 8;
2574
                }
2575
2576
                // ----- Call the header generation
2577
                if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2578
                    @fclose($v_file);
0 ignored issues
show
Bug introduced by
The variable $v_file seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

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...
2579
2580
                    return $v_result;
2581
                }
2582
2583
                // ----- Write the compressed (or not) content
2584
                @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
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...
2585
            } elseif ($p_filedescr['type'] == 'folder') {
2586
                // ----- Look for a directory
2587
                // ----- Look for directory last '/'
2588
                if (@substr($p_header['stored_filename'], -1) != '/') {
2589
                    $p_header['stored_filename'] .= '/';
2590
                }
2591
2592
                // ----- Set the file properties
2593
                $p_header['size'] = 0;
2594
                //$p_header['external'] = 0x41FF0010;     // Value for a folder : to be checked
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
2595
                $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2596
2597
                // ----- Call the header generation
2598
                if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2599
                    return $v_result;
2600
                }
2601
            }
2602
        }
2603
2604
        // ----- Look for post-add callback
2605
        if (isset($p_options[PCLZIP_CB_POST_ADD])) {
2606
            // ----- Generate a local information
2607
            $v_local_header = [];
2608
            $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2609
2610
            // ----- Call the callback
2611
            // Here I do not use call_user_func() because I need to send a reference to the
2612
            // header.
2613
    //            eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
2614
            $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
2615
            if ($v_result == 0) {
2616
                // ----- Ignored
2617
                $v_result = 1;
2618
            }
2619
2620
            // ----- Update the informations
2621
            // Nothing can be modified
2622
        }
2623
2624
        // ----- Return
2625
        return $v_result;
2626
    }
2627
    // --------------------------------------------------------------------------------
2628
2629
    // --------------------------------------------------------------------------------
2630
    // Function : privAddFileUsingTempFile()
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...
2631
    // Description :
2632
    // Parameters :
2633
    // Return Values :
2634
    // --------------------------------------------------------------------------------
2635
    public function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
0 ignored issues
show
Unused Code introduced by
The parameter $p_options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2636
    {
2637
        $v_result = PCLZIP_ERR_NO_ERROR;
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...
2638
2639
        // ----- Working variable
2640
        $p_filename = $p_filedescr['filename'];
2641
2642
        // ----- Open the source file
2643 View Code Duplication
        if (($v_file = @fopen($p_filename, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2644
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2645
2646
            return self::errorCode();
2647
        }
2648
2649
        // ----- Creates a compressed temporary file
2650
        $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
2651 View Code Duplication
        if (($v_file_compressed = @gzopen($v_gzip_temp_name, 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2652
            fclose($v_file);
2653
            self::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
2654
2655
            return self::errorCode();
2656
        }
2657
2658
        // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2659
        $v_size = filesize($p_filename);
2660
        while ($v_size != 0) {
2661
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2662
            $v_buffer = @fread($v_file, $v_read_size);
2663
            //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
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...
2664
            @gzputs($v_file_compressed, $v_buffer, $v_read_size);
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...
2665
            $v_size -= $v_read_size;
2666
        }
2667
2668
        // ----- Close the file
2669
        @fclose($v_file);
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...
2670
        @gzclose($v_file_compressed);
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...
2671
2672
        // ----- Check the minimum file size
2673
        if (filesize($v_gzip_temp_name) < 18) {
2674
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \'' . $v_gzip_temp_name . '\' has invalid filesize - should be minimum 18 bytes');
2675
2676
            return self::errorCode();
2677
        }
2678
2679
        // ----- Extract the compressed attributes
2680 View Code Duplication
        if (($v_file_compressed = @fopen($v_gzip_temp_name, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2681
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
2682
2683
            return self::errorCode();
2684
        }
2685
2686
        // ----- Read the gzip file header
2687
        $v_binary_data = @fread($v_file_compressed, 10);
2688
        $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
2689
2690
        // ----- Check some parameters
2691
        $v_data_header['os'] = bin2hex($v_data_header['os']);
2692
2693
        // ----- Read the gzip file footer
2694
        @fseek($v_file_compressed, filesize($v_gzip_temp_name) - 8);
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...
2695
        $v_binary_data = @fread($v_file_compressed, 8);
2696
        $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
2697
2698
        // ----- Set the attributes
2699
        $p_header['compression'] = ord($v_data_header['cm']);
2700
        //$p_header['mtime'] = $v_data_header['mtime'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
2701
        $p_header['crc'] = $v_data_footer['crc'];
2702
        $p_header['compressed_size'] = filesize($v_gzip_temp_name) - 18;
2703
2704
        // ----- Close the file
2705
        @fclose($v_file_compressed);
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...
2706
2707
        // ----- Call the header generation
2708
        if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2709
            return $v_result;
2710
        }
2711
2712
        // ----- Add the compressed data
2713 View Code Duplication
        if (($v_file_compressed = @fopen($v_gzip_temp_name, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2714
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
2715
2716
            return self::errorCode();
2717
        }
2718
2719
        // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2720
        fseek($v_file_compressed, 10);
2721
        $v_size = $p_header['compressed_size'];
2722 View Code Duplication
        while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2723
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2724
            $v_buffer = @fread($v_file_compressed, $v_read_size);
2725
            @fwrite($this->zip_fd, $v_buffer, $v_read_size);
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...
2726
            $v_size -= $v_read_size;
2727
        }
2728
2729
        // ----- Close the file
2730
        @fclose($v_file_compressed);
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...
2731
2732
        // ----- Unlink the temporary file
2733
        @unlink($v_gzip_temp_name);
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...
2734
2735
        // ----- Return
2736
        return $v_result;
2737
    }
2738
    // --------------------------------------------------------------------------------
2739
2740
    // --------------------------------------------------------------------------------
2741
    // Function : privCalculateStoredFilename()
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...
2742
    // Description :
2743
    //     Based on file descriptor properties and global options, this method
2744
    //     calculate the filename that will be stored in the archive.
2745
    // Parameters :
2746
    // Return Values :
2747
    // --------------------------------------------------------------------------------
2748
    public function privCalculateStoredFilename(&$p_filedescr, &$p_options)
2749
    {
2750
        $v_result = 1;
2751
2752
        // ----- Working variables
2753
        $p_filename = $p_filedescr['filename'];
2754
        if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
2755
            $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
2756
        } else {
2757
            $p_add_dir = '';
2758
        }
2759
        if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
2760
            $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
2761
        } else {
2762
            $p_remove_dir = '';
2763
        }
2764
        if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
2765
            $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
2766
        } else {
2767
            $p_remove_all_dir = 0;
2768
        }
2769
2770
        // ----- Look for full name change
2771
        if (isset($p_filedescr['new_full_name'])) {
2772
            // ----- Remove drive letter if any
2773
            $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
2774
        } else {
2775
            // ----- Look for path and/or short name change
2776
            // ----- Look for short name change
2777
            // Its when we cahnge just the filename but not the path
2778
            if (isset($p_filedescr['new_short_name'])) {
2779
                $v_path_info = pathinfo($p_filename);
2780
                $v_dir = '';
2781
                if ($v_path_info['dirname'] != '') {
2782
                    $v_dir = $v_path_info['dirname'] . '/';
2783
                }
2784
                $v_stored_filename = $v_dir . $p_filedescr['new_short_name'];
2785
            } else {
2786
                // ----- Calculate the stored filename
2787
                $v_stored_filename = $p_filename;
2788
            }
2789
2790
            // ----- Look for all path to remove
2791
            if ($p_remove_all_dir) {
2792
                $v_stored_filename = basename($p_filename);
2793
            } elseif ($p_remove_dir != '') {
2794
                // ----- Look for partial path remove
2795
                if (substr($p_remove_dir, -1) != '/') {
2796
                    $p_remove_dir .= '/';
2797
                }
2798
2799
                if ((substr($p_filename, 0, 2) == './') || (substr($p_remove_dir, 0, 2) == './')) {
2800 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...
2801
                        $p_remove_dir = './' . $p_remove_dir;
2802
                    }
2803 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...
2804
                        $p_remove_dir = substr($p_remove_dir, 2);
2805
                    }
2806
                }
2807
2808
                $v_compare = PclZipUtilPathInclusion($p_remove_dir, $v_stored_filename);
2809
                if ($v_compare > 0) {
2810
                    if ($v_compare == 2) {
2811
                        $v_stored_filename = '';
2812
                    } else {
2813
                        $v_stored_filename = substr($v_stored_filename, strlen($p_remove_dir));
2814
                    }
2815
                }
2816
            }
2817
2818
            // ----- Remove drive letter if any
2819
            $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
2820
2821
            // ----- Look for path to add
2822
            if ($p_add_dir != '') {
2823
                if (substr($p_add_dir, -1) == '/') {
2824
                    $v_stored_filename = $p_add_dir . $v_stored_filename;
2825
                } else {
2826
                    $v_stored_filename = $p_add_dir . '/' . $v_stored_filename;
2827
                }
2828
            }
2829
        }
2830
2831
        // ----- Filename (reduce the path of stored name)
2832
        $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
2833
        $p_filedescr['stored_filename'] = $v_stored_filename;
2834
2835
        // ----- Return
2836
        return $v_result;
2837
    }
2838
    // --------------------------------------------------------------------------------
2839
2840
    // --------------------------------------------------------------------------------
2841
    // Function : privWriteFileHeader()
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...
2842
    // Description :
2843
    // Parameters :
2844
    // Return Values :
2845
    // --------------------------------------------------------------------------------
2846
    public function privWriteFileHeader(&$p_header)
2847
    {
2848
        $v_result = 1;
2849
2850
        // ----- Store the offset position of the file
2851
        $p_header['offset'] = ftell($this->zip_fd);
2852
2853
        // ----- Transform UNIX mtime to DOS format mdate/mtime
2854
        $v_date = getdate($p_header['mtime']);
2855
        $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
2856
        $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
2857
2858
        // ----- Packed data
2859
        $v_binary_data = pack('VvvvvvVVVvv', 0x04034b50, $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']), $p_header['extra_len']);
2860
2861
        // ----- Write the first 148 bytes of the header in the archive
2862
        fputs($this->zip_fd, $v_binary_data, 30);
2863
2864
        // ----- Write the variable fields
2865 View Code Duplication
        if (strlen($p_header['stored_filename']) != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2866
            fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2867
        }
2868 View Code Duplication
        if ($p_header['extra_len'] != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2869
            fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2870
        }
2871
2872
        // ----- Return
2873
        return $v_result;
2874
    }
2875
    // --------------------------------------------------------------------------------
2876
2877
    // --------------------------------------------------------------------------------
2878
    // Function : privWriteCentralFileHeader()
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...
2879
    // Description :
2880
    // Parameters :
2881
    // Return Values :
2882
    // --------------------------------------------------------------------------------
2883
    public function privWriteCentralFileHeader(&$p_header)
2884
    {
2885
        $v_result = 1;
2886
2887
        // TBC
2888
        //for(reset($p_header); $key = key($p_header); next($p_header)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
2889
        //}
2890
2891
        // ----- Transform UNIX mtime to DOS format mdate/mtime
2892
        $v_date = getdate($p_header['mtime']);
2893
        $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
2894
        $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
2895
2896
        // ----- Packed data
2897
        $v_binary_data = pack('VvvvvvvVVVvvvvvVV', 0x02014b50, $p_header['version'], $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'], $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
2898
2899
        // ----- Write the 42 bytes of the header in the zip file
2900
        fputs($this->zip_fd, $v_binary_data, 46);
2901
2902
        // ----- Write the variable fields
2903 View Code Duplication
        if (strlen($p_header['stored_filename']) != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2904
            fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2905
        }
2906 View Code Duplication
        if ($p_header['extra_len'] != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2907
            fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2908
        }
2909
        if ($p_header['comment_len'] != 0) {
2910
            fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
2911
        }
2912
2913
        // ----- Return
2914
        return $v_result;
2915
    }
2916
    // --------------------------------------------------------------------------------
2917
2918
    // --------------------------------------------------------------------------------
2919
    // Function : privWriteCentralHeader()
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...
2920
    // Description :
2921
    // Parameters :
2922
    // Return Values :
2923
    // --------------------------------------------------------------------------------
2924
    public function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
2925
    {
2926
        $v_result = 1;
2927
2928
        // ----- Packed data
2929
        $v_binary_data = pack('VvvvvVVv', 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
2930
2931
        // ----- Write the 22 bytes of the header in the zip file
2932
        fputs($this->zip_fd, $v_binary_data, 22);
2933
2934
        // ----- Write the variable fields
2935
        if (strlen($p_comment) != 0) {
2936
            fputs($this->zip_fd, $p_comment, strlen($p_comment));
2937
        }
2938
2939
        // ----- Return
2940
        return $v_result;
2941
    }
2942
    // --------------------------------------------------------------------------------
2943
2944
    // --------------------------------------------------------------------------------
2945
    // Function : privList()
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...
2946
    // Description :
2947
    // Parameters :
2948
    // Return Values :
2949
    // --------------------------------------------------------------------------------
2950
    public function privList(&$p_list)
2951
    {
2952
        $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...
2953
2954
        // ----- Magic quotes trick
2955
        $this->privDisableMagicQuotes();
2956
2957
        // ----- Open the zip file
2958 View Code Duplication
        if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2959
            // ----- Magic quotes trick
2960
            $this->privSwapBackMagicQuotes();
2961
2962
            // ----- Error log
2963
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
2964
2965
            // ----- Return
2966
            return self::errorCode();
2967
        }
2968
2969
        // ----- Read the central directory informations
2970
        $v_central_dir = [];
2971
        if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
2972
            $this->privSwapBackMagicQuotes();
2973
2974
            return $v_result;
2975
        }
2976
2977
        // ----- Go to beginning of Central Dir
2978
        @rewind($this->zip_fd);
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...
2979 View Code Duplication
        if (@fseek($this->zip_fd, $v_central_dir['offset'])) {
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...
2980
            $this->privSwapBackMagicQuotes();
2981
2982
            // ----- Error log
2983
            self::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
2984
2985
            // ----- Return
2986
            return self::errorCode();
2987
        }
2988
2989
        // ----- Read each entry
2990
        for ($i = 0; $i < $v_central_dir['entries']; ++$i) {
2991
            // ----- Read the file header
2992
            if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
2993
                $this->privSwapBackMagicQuotes();
2994
2995
                return $v_result;
2996
            }
2997
            $v_header['index'] = $i;
2998
2999
            // ----- Get the only interesting attributes
3000
            $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3001
            unset($v_header);
3002
        }
3003
3004
        // ----- Close the zip file
3005
        $this->privCloseFd();
3006
3007
        // ----- Magic quotes trick
3008
        $this->privSwapBackMagicQuotes();
3009
3010
        // ----- Return
3011
        return $v_result;
3012
    }
3013
    // --------------------------------------------------------------------------------
3014
3015
    // --------------------------------------------------------------------------------
3016
    // Function : privConvertHeader2FileInfo()
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...
3017
    // Description :
3018
    //     This function takes the file informations from the central directory
3019
    //     entries and extract the interesting parameters that will be given back.
3020
    //     The resulting file infos are set in the array $p_info
3021
    //         $p_info['filename'] : Filename with full path. Given by user (add),
3022
    //                                                     extracted in the filesystem (extract).
3023
    //         $p_info['stored_filename'] : Stored filename in the archive.
3024
    //         $p_info['size'] = Size of the file.
3025
    //         $p_info['compressed_size'] = Compressed size of the file.
3026
    //         $p_info['mtime'] = Last modification date of the file.
3027
    //         $p_info['comment'] = Comment associated with the file.
3028
    //         $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3029
    //         $p_info['status'] = status of the action on the file.
3030
    //         $p_info['crc'] = CRC of the file content.
3031
    // Parameters :
3032
    // Return Values :
3033
    // --------------------------------------------------------------------------------
3034
    public function privConvertHeader2FileInfo($p_header, &$p_info)
3035
    {
3036
        $v_result = 1;
3037
3038
        // ----- Get the interesting attributes
3039
        $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
3040
        $p_info['filename'] = $v_temp_path;
3041
        $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
3042
        $p_info['stored_filename'] = $v_temp_path;
3043
        $p_info['size'] = $p_header['size'];
3044
        $p_info['compressed_size'] = $p_header['compressed_size'];
3045
        $p_info['mtime'] = $p_header['mtime'];
3046
        $p_info['comment'] = $p_header['comment'];
3047
        $p_info['folder'] = (($p_header['external'] & 0x00000010) == 0x00000010);
3048
        $p_info['index'] = $p_header['index'];
3049
        $p_info['status'] = $p_header['status'];
3050
        $p_info['crc'] = $p_header['crc'];
3051
3052
        // ----- Return
3053
        return $v_result;
3054
    }
3055
    // --------------------------------------------------------------------------------
3056
3057
    // --------------------------------------------------------------------------------
3058
    // Function : privExtractByRule()
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...
3059
    // Description :
3060
    //     Extract a file or directory depending of rules (by index, by name, ...)
3061
    // Parameters :
3062
    //     $p_file_list : An array where will be placed the properties of each
3063
    //                                    extracted file
3064
    //     $p_path : Path to add while writing the extracted files
3065
    //     $p_remove_path : Path to remove (from the file memorized path) while writing the
3066
    //                                        extracted files. If the path does not match the file path,
3067
    //                                        the file is extracted with its memorized path.
3068
    //                                        $p_remove_path does not apply to 'list' mode.
3069
    //                                        $p_path and $p_remove_path are commulative.
3070
    // Return Values :
3071
    //     1 on success,0 or less on error (see error code list)
3072
    // --------------------------------------------------------------------------------
3073
    public function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3074
    {
3075
        $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...
3076
3077
        // ----- Magic quotes trick
3078
        $this->privDisableMagicQuotes();
3079
3080
        // ----- Check the path
3081
        if (($p_path == '') || ((substr($p_path, 0, 1) != '/') && (substr($p_path, 0, 3) != '../') && (substr($p_path, 1, 2) != ':/'))) {
3082
            $p_path = './' . $p_path;
3083
        }
3084
3085
        // ----- Reduce the path last (and duplicated) '/'
3086
        if (($p_path != './') && ($p_path != '/')) {
3087
            // ----- Look for the path end '/'
3088
            while (substr($p_path, -1) == '/') {
3089
                $p_path = substr($p_path, 0, strlen($p_path) - 1);
3090
            }
3091
        }
3092
3093
        // ----- Look for path to remove format (should end by /)
3094
        if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) {
3095
            $p_remove_path .= '/';
3096
        }
3097
        $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...
3098
3099
        // ----- Open the zip file
3100
        if (($v_result = $this->privOpenFd('rb')) != 1) {
3101
            $this->privSwapBackMagicQuotes();
3102
3103
            return $v_result;
3104
        }
3105
3106
        // ----- Read the central directory informations
3107
        $v_central_dir = [];
3108
        if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
3109
            // ----- Close the zip file
3110
            $this->privCloseFd();
3111
            $this->privSwapBackMagicQuotes();
3112
3113
            return $v_result;
3114
        }
3115
3116
        // ----- Start at beginning of Central Dir
3117
        $v_pos_entry = $v_central_dir['offset'];
3118
3119
        // ----- Read each entry
3120
        $j_start = 0;
3121
        for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; ++$i) {
3122
            // ----- Read next Central dir entry
3123
            @rewind($this->zip_fd);
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...
3124 View Code Duplication
            if (@fseek($this->zip_fd, $v_pos_entry)) {
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...
3125
                // ----- Close the zip file
3126
                $this->privCloseFd();
3127
                $this->privSwapBackMagicQuotes();
3128
3129
                // ----- Error log
3130
                self::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3131
3132
                // ----- Return
3133
                return self::errorCode();
3134
            }
3135
3136
            // ----- Read the file header
3137
            $v_header = [];
3138
            if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
3139
                // ----- Close the zip file
3140
                $this->privCloseFd();
3141
                $this->privSwapBackMagicQuotes();
3142
3143
                return $v_result;
3144
            }
3145
3146
            // ----- Store the index
3147
            $v_header['index'] = $i;
3148
3149
            // ----- Store the file position
3150
            $v_pos_entry = ftell($this->zip_fd);
3151
3152
            // ----- Look for the specific extract rules
3153
            $v_extract = false;
3154
3155
            // ----- Look for extract by name rule
3156
            if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
3157
                // ----- Look if the filename is in the list
3158
                for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); ++$j) {
3159
                    // ----- Look for a directory
3160
                    if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == '/') {
3161
                        // ----- Look if the directory is in the filename path
3162
                        if ((strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
3163
                            $v_extract = true;
3164
                        }
3165
                    } elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
3166
                        // ----- Look for a filename
3167
                        $v_extract = true;
3168
                    }
3169
                }
3170
            } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != '')) {
3171
                // ----- Look for extract by preg rule
3172
                if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
3173
                    $v_extract = true;
3174
                }
3175 View Code Duplication
            } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3176
                // ----- Look for extract by index rule
3177
                // ----- Look if the index is in the list
3178
                for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); ++$j) {
3179
                    if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
3180
                        $v_extract = true;
3181
                    }
3182
                    if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
3183
                        $j_start = $j + 1;
3184
                    }
3185
3186
                    if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
3187
                        break;
3188
                    }
3189
                }
3190
            } else {
3191
                // ----- Look for no rule, which means extract all the archive
3192
                $v_extract = true;
3193
            }
3194
3195
            // ----- Check compression method
3196
            if (($v_extract) && (($v_header['compression'] != 8) && ($v_header['compression'] != 0))) {
3197
                $v_header['status'] = 'unsupported_compression';
3198
3199
                // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3200
                if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
3201
                    $this->privSwapBackMagicQuotes();
3202
3203
                    self::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION, "Filename '" . $v_header['stored_filename'] . "' is compressed by an unsupported compression method (" . $v_header['compression'] . ') ');
3204
3205
                    return self::errorCode();
3206
                }
3207
            }
3208
3209
            // ----- Check encrypted files
3210
            if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
3211
                $v_header['status'] = 'unsupported_encryption';
3212
                // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3213
                if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
3214
                    $this->privSwapBackMagicQuotes();
3215
3216
                    self::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, "Unsupported encryption for  filename '" . $v_header['stored_filename'] . "'");
3217
3218
                    return self::errorCode();
3219
                }
3220
            }
3221
3222
            // ----- Look for real extraction
3223
            if (($v_extract) && ($v_header['status'] != 'ok')) {
3224
                $v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++]);
3225
                if ($v_result != 1) {
3226
                    $this->privCloseFd();
3227
                    $this->privSwapBackMagicQuotes();
3228
3229
                    return $v_result;
3230
                }
3231
3232
                $v_extract = false;
3233
            }
3234
3235
            // ----- Look for real extraction
3236
            if ($v_extract) {
3237
                // ----- Go to the file position
3238
                @rewind($this->zip_fd);
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...
3239 View Code Duplication
                if (@fseek($this->zip_fd, $v_header['offset'])) {
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...
3240
                    // ----- Close the zip file
3241
                    $this->privCloseFd();
3242
3243
                    $this->privSwapBackMagicQuotes();
3244
3245
                    // ----- Error log
3246
                    self::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3247
3248
                    // ----- Return
3249
                    return self::errorCode();
3250
                }
3251
3252
                // ----- Look for extraction as string
3253
                if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
3254
                    $v_string = '';
3255
3256
                    // ----- Extracting the file
3257
                    $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
3258
                    if ($v_result1 < 1) {
3259
                        $this->privCloseFd();
3260
                        $this->privSwapBackMagicQuotes();
3261
3262
                        return $v_result1;
3263
                    }
3264
3265
                    // ----- Get the only interesting attributes
3266 View Code Duplication
                    if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3267
                        // ----- Close the zip file
3268
                        $this->privCloseFd();
3269
                        $this->privSwapBackMagicQuotes();
3270
3271
                        return $v_result;
3272
                    }
3273
3274
                    // ----- Set the file content
3275
                    $p_file_list[$v_nb_extracted]['content'] = $v_string;
3276
3277
                    // ----- Next extracted file
3278
                    ++$v_nb_extracted;
3279
3280
                    // ----- Look for user callback abort
3281
                    if ($v_result1 == 2) {
3282
                        break;
3283
                    }
3284
                } elseif ((isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
3285
                    // ----- Look for extraction in standard output
3286
                    // ----- Extracting the file in standard output
3287
                    $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3288
                    if ($v_result1 < 1) {
3289
                        $this->privCloseFd();
3290
                        $this->privSwapBackMagicQuotes();
3291
3292
                        return $v_result1;
3293
                    }
3294
3295
                    // ----- Get the only interesting attributes
3296 View Code Duplication
                    if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3297
                        $this->privCloseFd();
3298
                        $this->privSwapBackMagicQuotes();
3299
3300
                        return $v_result;
3301
                    }
3302
3303
                    // ----- Look for user callback abort
3304
                    if ($v_result1 == 2) {
3305
                        break;
3306
                    }
3307
                } else {
3308
                    // ----- Look for normal extraction
3309
                    // ----- Extracting the file
3310
                    $v_result1 = $this->privExtractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_options);
3311
                    if ($v_result1 < 1) {
3312
                        $this->privCloseFd();
3313
                        $this->privSwapBackMagicQuotes();
3314
3315
                        return $v_result1;
3316
                    }
3317
3318
                    // ----- Get the only interesting attributes
3319 View Code Duplication
                    if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3320
                        // ----- Close the zip file
3321
                        $this->privCloseFd();
3322
                        $this->privSwapBackMagicQuotes();
3323
3324
                        return $v_result;
3325
                    }
3326
3327
                    // ----- Look for user callback abort
3328
                    if ($v_result1 == 2) {
3329
                        break;
3330
                    }
3331
                }
3332
            }
3333
        }
3334
3335
        // ----- Close the zip file
3336
        $this->privCloseFd();
3337
        $this->privSwapBackMagicQuotes();
3338
3339
        // ----- Return
3340
        return $v_result;
3341
    }
3342
    // --------------------------------------------------------------------------------
3343
3344
    // --------------------------------------------------------------------------------
3345
    // Function : privExtractFile()
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...
3346
    // Description :
3347
    // Parameters :
3348
    // Return Values :
3349
    //
3350
    // 1 : ... ?
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
3351
    // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3352
    // --------------------------------------------------------------------------------
3353
    public function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3354
    {
3355
        $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...
3356
3357
        // ----- Read the file header
3358
        if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
3359
            // ----- Return
3360
            return $v_result;
3361
        }
3362
3363
        // ----- Check that the file header is coherent with $p_entry info
3364
        if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

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

could be turned into

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

This is much more concise to read.

Loading history...
3365
            // TBC
3366
        }
3367
3368
        // ----- Look for all path to remove
3369
        if ($p_remove_all_path == true) {
3370
            // ----- Look for folder entry that not need to be extracted
3371
            if (($p_entry['external'] & 0x00000010) == 0x00000010) {
3372
                $p_entry['status'] = 'filtered';
3373
3374
                return $v_result;
3375
            }
3376
3377
            // ----- Get the basename of the path
3378
            $p_entry['filename'] = basename($p_entry['filename']);
3379
        } elseif ($p_remove_path != '') {
3380
            // ----- Look for path to remove
3381
            if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
3382
                // ----- Change the file status
3383
                $p_entry['status'] = 'filtered';
3384
3385
                // ----- Return
3386
                return $v_result;
3387
            }
3388
3389
            $p_remove_path_size = strlen($p_remove_path);
3390
            if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
3391
                // ----- Remove the path
3392
                $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
3393
            }
3394
        }
3395
3396
        // ----- Add the path
3397
        if ($p_path != '') {
3398
            $p_entry['filename'] = $p_path . '/' . $p_entry['filename'];
3399
        }
3400
3401
        // ----- Check a base_dir_restriction
3402
        if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
3403
            $v_inclusion = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION], $p_entry['filename']);
3404
            if ($v_inclusion == 0) {
3405
                self::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION, "Filename '" . $p_entry['filename'] . "' is outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
3406
3407
                return self::errorCode();
3408
            }
3409
        }
3410
3411
        // ----- Look for pre-extract callback
3412 View Code Duplication
        if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
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...
3413
            // ----- Generate a local information
3414
            $v_local_header = [];
3415
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3416
3417
            // ----- Call the callback
3418
            // Here I do not use call_user_func() because I need to send a reference to the
3419
            // header.
3420
    //            eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3421
            $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
3422
            if ($v_result == 0) {
3423
                // ----- Change the file status
3424
                $p_entry['status'] = 'skipped';
3425
                $v_result = 1;
3426
            }
3427
3428
            // ----- Look for abort result
3429
            if ($v_result == 2) {
3430
                // ----- This status is internal and will be changed in 'skipped'
3431
                $p_entry['status'] = 'aborted';
3432
                $v_result = PCLZIP_ERR_USER_ABORTED;
3433
            }
3434
3435
            // ----- Update the informations
3436
            // Only some fields can be modified
3437
            $p_entry['filename'] = $v_local_header['filename'];
3438
        }
3439
3440
        // ----- Look if extraction should be done
3441
        if ($p_entry['status'] == 'ok') {
3442
            // ----- Look for specific actions while the file exist
3443
            if (file_exists($p_entry['filename'])) {
3444
                // ----- Look if file is a directory
3445
                if (is_dir($p_entry['filename'])) {
3446
                    // ----- Change the file status
3447
                    $p_entry['status'] = 'already_a_directory';
3448
3449
                    // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3450
                    // For historical reason first PclZip implementation does not stop
3451
                    // when this kind of error occurs.
3452
                    if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
3453
                        self::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY, "Filename '" . $p_entry['filename'] . "' is already used by an existing directory");
3454
3455
                        return self::errorCode();
3456
                    }
3457 View Code Duplication
                } elseif (!is_writeable($p_entry['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...
3458
                    // ----- Look if file is write protected
3459
                    // ----- Change the file status
3460
                    $p_entry['status'] = 'write_protected';
3461
3462
                    // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3463
                    // For historical reason first PclZip implementation does not stop
3464
                    // when this kind of error occurs.
3465
                    if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
3466
                        self::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Filename '" . $p_entry['filename'] . "' exists and is write protected");
3467
3468
                        return self::errorCode();
3469
                    }
3470
                } elseif (filemtime($p_entry['filename']) > $p_entry['mtime']) {
3471
                    // ----- Look if the extracted file is older
3472
                    // ----- Change the file status
3473
                    if ((isset($p_options[PCLZIP_OPT_REPLACE_NEWER])) && ($p_options[PCLZIP_OPT_REPLACE_NEWER] === true)) {
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...
3474 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...
3475
                        $p_entry['status'] = 'newer_exist';
3476
3477
                        // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3478
                        // For historical reason first PclZip implementation does not stop
3479
                        // when this kind of error occurs.
3480
                        if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
3481
                            self::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Newer version of '" . $p_entry['filename'] . "' exists and option PCLZIP_OPT_REPLACE_NEWER is not selected");
3482
3483
                            return self::errorCode();
3484
                        }
3485
                    }
3486
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
3487
                }
3488
            } else {
3489
                // ----- Check the directory availability and create it if necessary
3490
                if ((($p_entry['external'] & 0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/')) {
3491
                    $v_dir_to_check = $p_entry['filename'];
3492
                } elseif (!strstr($p_entry['filename'], '/')) {
3493
                    $v_dir_to_check = '';
3494
                } else {
3495
                    $v_dir_to_check = dirname($p_entry['filename']);
3496
                }
3497
3498
                if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external'] & 0x00000010) == 0x00000010))) != 1) {
3499
                    // ----- Change the file status
3500
                    $p_entry['status'] = 'path_creation_fail';
3501
3502
                    // ----- Return
3503
                    //return $v_result;
3504
                    $v_result = 1;
3505
                }
3506
            }
3507
        }
3508
3509
        // ----- Look if extraction should be done
3510
        if ($p_entry['status'] == 'ok') {
3511
            // ----- Do the extraction (if not a folder)
3512
            if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
3513
                // ----- Look for not compressed file
3514
                if ($p_entry['compression'] == 0) {
3515
                    // ----- Opening destination file
3516 View Code Duplication
                    if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3517
                        // ----- Change the file status
3518
                        $p_entry['status'] = 'write_error';
3519
3520
                        // ----- Return
3521
                        return $v_result;
3522
                    }
3523
3524
                    // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3525
                    $v_size = $p_entry['compressed_size'];
3526 View Code Duplication
                    while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3527
                        $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3528
                        $v_buffer = @fread($this->zip_fd, $v_read_size);
3529
                        /* Try to speed up the code
3530
                        $v_binary_data = pack('a'.$v_read_size, $v_buffer);
3531
                        @fwrite($v_dest_file, $v_binary_data, $v_read_size);
3532
                        */
3533
                        @fwrite($v_dest_file, $v_buffer, $v_read_size);
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...
3534
                        $v_size -= $v_read_size;
3535
                    }
3536
3537
                    // ----- Closing the destination file
3538
                    fclose($v_dest_file);
3539
3540
                    // ----- Change the file mtime
3541
                    touch($p_entry['filename'], $p_entry['mtime']);
3542
                } else {
3543
                    // ----- TBC
3544
                    // Need to be finished
3545
                    if (($p_entry['flag'] & 1) == 1) {
3546
                        self::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \'' . $p_entry['filename'] . '\' is encrypted. Encrypted files are not supported.');
3547
3548
                        return self::errorCode();
3549
                    }
3550
3551
                    // ----- Look for using temporary file to unzip
3552
                    if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON]) || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])))) {
3553
                        $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
3554
                        if ($v_result < PCLZIP_ERR_NO_ERROR) {
3555
                            return $v_result;
3556
                        }
3557
                    } else {
3558
                        // ----- Look for extract in memory
3559
                        // ----- Read the compressed file in a buffer (one shot)
3560
                        $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3561
3562
                        // ----- Decompress the file
3563
                        $v_file_content = @gzinflate($v_buffer);
3564
                        unset($v_buffer);
3565
                        if ($v_file_content === false) {
3566
                            // ----- Change the file status
3567
                            // TBC
3568
                            $p_entry['status'] = 'error';
3569
3570
                            return $v_result;
3571
                        }
3572
3573
                        // ----- Opening destination file
3574 View Code Duplication
                        if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3575
                            // ----- Change the file status
3576
                            $p_entry['status'] = 'write_error';
3577
3578
                            return $v_result;
3579
                        }
3580
3581
                        // ----- Write the uncompressed data
3582
                        @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
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...
3583
                        unset($v_file_content);
3584
3585
                        // ----- Closing the destination file
3586
                        @fclose($v_dest_file);
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...
3587
                    }
3588
3589
                    // ----- Change the file mtime
3590
                    @touch($p_entry['filename'], $p_entry['mtime']);
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...
3591
                }
3592
3593
                // ----- Look for chmod option
3594
                if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
3595
                    // ----- Change the mode of the file
3596
                    @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
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...
3597
                }
3598
            }
3599
        }
3600
3601
        // ----- Change abort status
3602 View Code Duplication
        if ($p_entry['status'] == 'aborted') {
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...
3603
            $p_entry['status'] = 'skipped';
3604
        } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3605
            // ----- Look for post-extract callback
3606
            // ----- Generate a local information
3607
            $v_local_header = [];
3608
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3609
3610
            // ----- Call the callback
3611
            // Here I do not use call_user_func() because I need to send a reference to the
3612
            // header.
3613
    //            eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3614
            $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
3615
3616
            // ----- Look for abort result
3617
            if ($v_result == 2) {
3618
                $v_result = PCLZIP_ERR_USER_ABORTED;
3619
            }
3620
        }
3621
3622
        // ----- Return
3623
        return $v_result;
3624
    }
3625
    // --------------------------------------------------------------------------------
3626
3627
    // --------------------------------------------------------------------------------
3628
    // Function : privExtractFileUsingTempFile()
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...
3629
    // Description :
3630
    // Parameters :
3631
    // Return Values :
3632
    // --------------------------------------------------------------------------------
3633
    public function privExtractFileUsingTempFile(&$p_entry, &$p_options)
0 ignored issues
show
Unused Code introduced by
The parameter $p_options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
3634
    {
3635
        $v_result = 1;
3636
3637
        // ----- Creates a temporary file
3638
        $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
3639 View Code Duplication
        if (($v_dest_file = @fopen($v_gzip_temp_name, 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3640
            fclose($v_file);
0 ignored issues
show
Bug introduced by
The variable $v_file 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...
3641
            self::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
3642
3643
            return self::errorCode();
3644
        }
3645
3646
        // ----- Write gz file format header
3647
        $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
3648
        @fwrite($v_dest_file, $v_binary_data, 10);
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...
3649
3650
        // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3651
        $v_size = $p_entry['compressed_size'];
3652 View Code Duplication
        while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3653
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3654
            $v_buffer = @fread($this->zip_fd, $v_read_size);
3655
            //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
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...
3656
            @fwrite($v_dest_file, $v_buffer, $v_read_size);
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...
3657
            $v_size -= $v_read_size;
3658
        }
3659
3660
        // ----- Write gz file format footer
3661
        $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
3662
        @fwrite($v_dest_file, $v_binary_data, 8);
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...
3663
3664
        // ----- Close the temporary file
3665
        @fclose($v_dest_file);
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...
3666
3667
        // ----- Opening destination file
3668 View Code Duplication
        if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3669
            $p_entry['status'] = 'write_error';
3670
3671
            return $v_result;
3672
        }
3673
3674
        // ----- Open the temporary gz file
3675 View Code Duplication
        if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
3676
            @fclose($v_dest_file);
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...
3677
            $p_entry['status'] = 'read_error';
3678
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
3679
3680
            return self::errorCode();
3681
        }
3682
3683
        // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3684
        $v_size = $p_entry['size'];
3685
        while ($v_size != 0) {
3686
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3687
            $v_buffer = @gzread($v_src_file, $v_read_size);
3688
            @fwrite($v_dest_file, $v_buffer, $v_read_size);
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...
3689
            $v_size -= $v_read_size;
3690
        }
3691
        @fclose($v_dest_file);
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...
3692
        @gzclose($v_src_file);
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...
3693
3694
        // ----- Delete the temporary file
3695
        @unlink($v_gzip_temp_name);
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...
3696
3697
        // ----- Return
3698
        return $v_result;
3699
    }
3700
    // --------------------------------------------------------------------------------
3701
3702
    // --------------------------------------------------------------------------------
3703
    // Function : privExtractFileInOutput()
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...
3704
    // Description :
3705
    // Parameters :
3706
    // Return Values :
3707
    // --------------------------------------------------------------------------------
3708
    public function privExtractFileInOutput(&$p_entry, &$p_options)
3709
    {
3710
        $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...
3711
3712
        // ----- Read the file header
3713
        if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
3714
            return $v_result;
3715
        }
3716
3717
        // ----- Check that the file header is coherent with $p_entry info
3718
        if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

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

could be turned into

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

This is much more concise to read.

Loading history...
3719
            // TBC
3720
        }
3721
3722
        // ----- Look for pre-extract callback
3723 View Code Duplication
        if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
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...
3724
            // ----- Generate a local information
3725
            $v_local_header = [];
3726
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3727
3728
            // ----- Call the callback
3729
            // Here I do not use call_user_func() because I need to send a reference to the
3730
            // header.
3731
    //            eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3732
            $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
3733
            if ($v_result == 0) {
3734
                // ----- Change the file status
3735
                $p_entry['status'] = 'skipped';
3736
                $v_result = 1;
3737
            }
3738
3739
            // ----- Look for abort result
3740
            if ($v_result == 2) {
3741
                // ----- This status is internal and will be changed in 'skipped'
3742
                $p_entry['status'] = 'aborted';
3743
                $v_result = PCLZIP_ERR_USER_ABORTED;
3744
            }
3745
3746
            // ----- Update the informations
3747
            // Only some fields can be modified
3748
            $p_entry['filename'] = $v_local_header['filename'];
3749
        }
3750
3751
        // ----- Trace
3752
3753
        // ----- Look if extraction should be done
3754
        if ($p_entry['status'] == 'ok') {
3755
            // ----- Do the extraction (if not a folder)
3756
            if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
3757
                // ----- Look for not compressed file
3758
                if ($p_entry['compressed_size'] == $p_entry['size']) {
3759
                    // ----- Read the file in a buffer (one shot)
3760
                    $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3761
3762
                    // ----- Send the file to the output
3763
                    echo $v_buffer;
3764
                    unset($v_buffer);
3765
                } else {
3766
                    // ----- Read the compressed file in a buffer (one shot)
3767
                    $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3768
3769
                    // ----- Decompress the file
3770
                    $v_file_content = gzinflate($v_buffer);
3771
                    unset($v_buffer);
3772
3773
                    // ----- Send the file to the output
3774
                    echo $v_file_content;
3775
                    unset($v_file_content);
3776
                }
3777
            }
3778
        }
3779
3780
        // ----- Change abort status
3781 View Code Duplication
        if ($p_entry['status'] == 'aborted') {
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...
3782
            $p_entry['status'] = 'skipped';
3783
        } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3784
            // ----- Look for post-extract callback
3785
3786
            // ----- Generate a local information
3787
            $v_local_header = [];
3788
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3789
3790
            // ----- Call the callback
3791
            // Here I do not use call_user_func() because I need to send a reference to the
3792
            // header.
3793
    //            eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3794
            $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
3795
3796
            // ----- Look for abort result
3797
            if ($v_result == 2) {
3798
                $v_result = PCLZIP_ERR_USER_ABORTED;
3799
            }
3800
        }
3801
3802
        return $v_result;
3803
    }
3804
    // --------------------------------------------------------------------------------
3805
3806
    // --------------------------------------------------------------------------------
3807
    // Function : privExtractFileAsString()
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...
3808
    // Description :
3809
    // Parameters :
3810
    // Return Values :
3811
    // --------------------------------------------------------------------------------
3812
    public function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
3813
    {
3814
        $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...
3815
3816
        // ----- Read the file header
3817
        $v_header = [];
3818
        if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
3819
            // ----- Return
3820
            return $v_result;
3821
        }
3822
3823
        // ----- Check that the file header is coherent with $p_entry info
3824
        if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

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

could be turned into

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

This is much more concise to read.

Loading history...
3825
            // TBC
3826
        }
3827
3828
        // ----- Look for pre-extract callback
3829 View Code Duplication
        if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
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...
3830
            // ----- Generate a local information
3831
            $v_local_header = [];
3832
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3833
3834
            // ----- Call the callback
3835
            // Here I do not use call_user_func() because I need to send a reference to the
3836
            // header.
3837
    //            eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3838
            $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
3839
            if ($v_result == 0) {
3840
                // ----- Change the file status
3841
                $p_entry['status'] = 'skipped';
3842
                $v_result = 1;
3843
            }
3844
3845
            // ----- Look for abort result
3846
            if ($v_result == 2) {
3847
                // ----- This status is internal and will be changed in 'skipped'
3848
                $p_entry['status'] = 'aborted';
3849
                $v_result = PCLZIP_ERR_USER_ABORTED;
3850
            }
3851
3852
            // ----- Update the informations
3853
            // Only some fields can be modified
3854
            $p_entry['filename'] = $v_local_header['filename'];
3855
        }
3856
3857
        // ----- Look if extraction should be done
3858
        if ($p_entry['status'] == 'ok') {
3859
            // ----- Do the extraction (if not a folder)
3860
            if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
3861
                // ----- Look for not compressed file
3862
    //            if ($p_entry['compressed_size'] == $p_entry['size'])
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...
3863
                if ($p_entry['compression'] == 0) {
3864
                    // ----- Reading the file
3865
                    $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
3866
                } else {
3867
                    // ----- Reading the file
3868
                    $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
3869
3870
                    // ----- Decompress the file
3871
                    if (($p_string = @gzinflate($v_data)) === false) {
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...
3872
                        // TBC
3873
                    }
3874
                }
3875
                // ----- Trace
3876
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
3877
                // TBC : error : can not extract a folder in a string
3878
            }
3879
        }
3880
3881
        // ----- Change abort status
3882 View Code Duplication
        if ($p_entry['status'] == 'aborted') {
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...
3883
            $p_entry['status'] = 'skipped';
3884
        } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3885
            // ----- Look for post-extract callback
3886
            // ----- Generate a local information
3887
            $v_local_header = [];
3888
            $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3889
3890
            // ----- Swap the content to header
3891
            $v_local_header['content'] = $p_string;
3892
            $p_string = '';
3893
3894
            // ----- Call the callback
3895
            // Here I do not use call_user_func() because I need to send a reference to the
3896
            // header.
3897
    //            eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
3898
            $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
3899
3900
            // ----- Swap back the content to header
3901
            $p_string = $v_local_header['content'];
3902
            unset($v_local_header['content']);
3903
3904
            // ----- Look for abort result
3905
            if ($v_result == 2) {
3906
                $v_result = PCLZIP_ERR_USER_ABORTED;
3907
            }
3908
        }
3909
3910
        // ----- Return
3911
        return $v_result;
3912
    }
3913
    // --------------------------------------------------------------------------------
3914
3915
    // --------------------------------------------------------------------------------
3916
    // Function : privReadFileHeader()
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...
3917
    // Description :
3918
    // Parameters :
3919
    // Return Values :
3920
    // --------------------------------------------------------------------------------
3921
    public function privReadFileHeader(&$p_header)
3922
    {
3923
        $v_result = 1;
3924
3925
        // ----- Read the 4 bytes signature
3926
        $v_binary_data = @fread($this->zip_fd, 4);
3927
        $v_data = unpack('Vid', $v_binary_data);
3928
3929
        // ----- Check signature
3930
        if ($v_data['id'] != 0x04034b50) {
3931
            // ----- Error log
3932
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
3933
3934
            // ----- Return
3935
            return self::errorCode();
3936
        }
3937
3938
        // ----- Read the first 42 bytes of the header
3939
        $v_binary_data = fread($this->zip_fd, 26);
3940
3941
        // ----- Look for invalid block size
3942 View Code Duplication
        if (strlen($v_binary_data) != 26) {
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...
3943
            $p_header['filename'] = '';
3944
            $p_header['status'] = 'invalid_header';
3945
3946
            // ----- Error log
3947
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid block size : ' . strlen($v_binary_data));
3948
3949
            // ----- Return
3950
            return self::errorCode();
3951
        }
3952
3953
        // ----- Extract the values
3954
        $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
3955
3956
        // ----- Get filename
3957
        $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
3958
3959
        // ----- Get extra_fields
3960
        if ($v_data['extra_len'] != 0) {
3961
            $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
3962
        } else {
3963
            $p_header['extra'] = '';
3964
        }
3965
3966
        // ----- Extract properties
3967
        $p_header['version_extracted'] = $v_data['version'];
3968
        $p_header['compression'] = $v_data['compression'];
3969
        $p_header['size'] = $v_data['size'];
3970
        $p_header['compressed_size'] = $v_data['compressed_size'];
3971
        $p_header['crc'] = $v_data['crc'];
3972
        $p_header['flag'] = $v_data['flag'];
3973
        $p_header['filename_len'] = $v_data['filename_len'];
3974
3975
        // ----- Recuperate date in UNIX format
3976
        $p_header['mdate'] = $v_data['mdate'];
3977
        $p_header['mtime'] = $v_data['mtime'];
3978
        if ($p_header['mdate'] && $p_header['mtime']) {
3979
            // ----- Extract time
3980
            $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
3981
            $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
3982
            $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
3983
3984
            // ----- Extract date
3985
            $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
3986
            $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
3987
            $v_day = $p_header['mdate'] & 0x001F;
3988
3989
            // ----- Get UNIX date format
3990
            $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
3991
        } else {
3992
            $p_header['mtime'] = time();
3993
        }
3994
3995
        // TBC
3996
        //for(reset($v_data); $key = key($v_data); next($v_data)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
3997
        //}
3998
3999
        // ----- Set the stored filename
4000
        $p_header['stored_filename'] = $p_header['filename'];
4001
4002
        // ----- Set the status field
4003
        $p_header['status'] = 'ok';
4004
4005
        // ----- Return
4006
        return $v_result;
4007
    }
4008
    // --------------------------------------------------------------------------------
4009
4010
    // --------------------------------------------------------------------------------
4011
    // Function : privReadCentralFileHeader()
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...
4012
    // Description :
4013
    // Parameters :
4014
    // Return Values :
4015
    // --------------------------------------------------------------------------------
4016
    public function privReadCentralFileHeader(&$p_header)
4017
    {
4018
        $v_result = 1;
4019
4020
        // ----- Read the 4 bytes signature
4021
        $v_binary_data = @fread($this->zip_fd, 4);
4022
        $v_data = unpack('Vid', $v_binary_data);
4023
4024
        // ----- Check signature
4025
        if ($v_data['id'] != 0x02014b50) {
4026
            // ----- Error log
4027
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4028
4029
            // ----- Return
4030
            return self::errorCode();
4031
        }
4032
4033
        // ----- Read the first 42 bytes of the header
4034
        $v_binary_data = fread($this->zip_fd, 42);
4035
4036
        // ----- Look for invalid block size
4037 View Code Duplication
        if (strlen($v_binary_data) != 42) {
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...
4038
            $p_header['filename'] = '';
4039
            $p_header['status'] = 'invalid_header';
4040
4041
            // ----- Error log
4042
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid block size : ' . strlen($v_binary_data));
4043
4044
            // ----- Return
4045
            return self::errorCode();
4046
        }
4047
4048
        // ----- Extract the values
4049
        $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
4050
4051
        // ----- Get filename
4052 View Code Duplication
        if ($p_header['filename_len'] != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4053
            $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
4054
        } else {
4055
            $p_header['filename'] = '';
4056
        }
4057
4058
        // ----- Get extra
4059 View Code Duplication
        if ($p_header['extra_len'] != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4060
            $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
4061
        } else {
4062
            $p_header['extra'] = '';
4063
        }
4064
4065
        // ----- Get comment
4066 View Code Duplication
        if ($p_header['comment_len'] != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4067
            $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
4068
        } else {
4069
            $p_header['comment'] = '';
4070
        }
4071
4072
        // ----- Extract properties
4073
4074
        // ----- Recuperate date in UNIX format
4075
        //if ($p_header['mdate'] && $p_header['mtime'])
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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...
4076
        // TBC : bug : this was ignoring time with 0/0/0
4077
        if (1) {
4078
            // ----- Extract time
4079
            $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4080
            $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4081
            $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
4082
4083
            // ----- Extract date
4084
            $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4085
            $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4086
            $v_day = $p_header['mdate'] & 0x001F;
4087
4088
            // ----- Get UNIX date format
4089
            $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4090
        } else {
4091
            $p_header['mtime'] = time();
4092
        }
4093
4094
        // ----- Set the stored filename
4095
        $p_header['stored_filename'] = $p_header['filename'];
4096
4097
        // ----- Set default status to ok
4098
        $p_header['status'] = 'ok';
4099
4100
        // ----- Look if it is a directory
4101
        if (substr($p_header['filename'], -1) == '/') {
4102
            //$p_header['external'] = 0x41FF0010;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
4103
            $p_header['external'] = 0x00000010;
4104
        }
4105
4106
        // ----- Return
4107
        return $v_result;
4108
    }
4109
    // --------------------------------------------------------------------------------
4110
4111
    // --------------------------------------------------------------------------------
4112
    // Function : privCheckFileHeaders()
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...
4113
    // Description :
4114
    // Parameters :
4115
    // Return Values :
4116
    //     1 on success,
4117
    //     0 on error;
4118
    // --------------------------------------------------------------------------------
4119
    public function privCheckFileHeaders(&$p_local_header, &$p_central_header)
4120
    {
4121
        $v_result = 1;
4122
4123
        // ----- Check the static values
4124
        // TBC
4125
        if ($p_local_header['filename'] != $p_central_header['filename']) {
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...
4126
        }
4127
        if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
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...
4128
        }
4129
        if ($p_local_header['flag'] != $p_central_header['flag']) {
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...
4130
        }
4131
        if ($p_local_header['compression'] != $p_central_header['compression']) {
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...
4132
        }
4133
        if ($p_local_header['mtime'] != $p_central_header['mtime']) {
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...
4134
        }
4135
        if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
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...
4136
        }
4137
4138
        // ----- Look for flag bit 3
4139
        if (($p_local_header['flag'] & 8) == 8) {
4140
            $p_local_header['size'] = $p_central_header['size'];
4141
            $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
4142
            $p_local_header['crc'] = $p_central_header['crc'];
4143
        }
4144
4145
        // ----- Return
4146
        return $v_result;
4147
    }
4148
    // --------------------------------------------------------------------------------
4149
4150
    // --------------------------------------------------------------------------------
4151
    // Function : privReadEndCentralDir()
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...
4152
    // Description :
4153
    // Parameters :
4154
    // Return Values :
4155
    // --------------------------------------------------------------------------------
4156
    public function privReadEndCentralDir(&$p_central_dir)
4157
    {
4158
        $v_result = 1;
4159
4160
        // ----- Go to the end of the zip file
4161
        $v_size = filesize($this->zipname);
4162
        @fseek($this->zip_fd, $v_size);
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...
4163 View Code Duplication
        if (@ftell($this->zip_fd) != $v_size) {
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...
4164
            // ----- Error log
4165
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
4166
4167
            // ----- Return
4168
            return self::errorCode();
4169
        }
4170
4171
        // ----- First try : look if this is an archive with no commentaries (most of the time)
4172
        // in this case the end of central dir is at 22 bytes of the file end
4173
        $v_found = 0;
4174
        if ($v_size > 26) {
4175
            @fseek($this->zip_fd, $v_size - 22);
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...
4176 View Code Duplication
            if (($v_pos = @ftell($this->zip_fd)) != ($v_size - 22)) {
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...
4177
                // ----- Error log
4178
                self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
4179
4180
                // ----- Return
4181
                return self::errorCode();
4182
            }
4183
4184
            // ----- Read for bytes
4185
            $v_binary_data = @fread($this->zip_fd, 4);
4186
            $v_data = @unpack('Vid', $v_binary_data);
4187
4188
            // ----- Check signature
4189
            if ($v_data['id'] == 0x06054b50) {
4190
                $v_found = 1;
4191
            }
4192
4193
            $v_pos = ftell($this->zip_fd);
4194
        }
4195
4196
        // ----- Go back to the maximum possible size of the Central Dir End Record
4197
        if (!$v_found) {
4198
            $v_maximum_size = 65557; // 0xFFFF + 22;
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...
4199
            if ($v_maximum_size > $v_size) {
4200
                $v_maximum_size = $v_size;
4201
            }
4202
            @fseek($this->zip_fd, $v_size - $v_maximum_size);
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...
4203 View Code Duplication
            if (@ftell($this->zip_fd) != ($v_size - $v_maximum_size)) {
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...
4204
                // ----- Error log
4205
                self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
4206
4207
                // ----- Return
4208
                return self::errorCode();
4209
            }
4210
4211
            // ----- Read byte per byte in order to find the signature
4212
            $v_pos = ftell($this->zip_fd);
4213
            $v_bytes = 0x00000000;
4214
            while ($v_pos < $v_size) {
4215
                // ----- Read a byte
4216
                $v_byte = @fread($this->zip_fd, 1);
4217
4218
                // -----    Add the byte
4219
                //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
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...
4220
                // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
4221
                // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
4222
                $v_bytes = (($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
4223
4224
                // ----- Compare the bytes
4225
                if ($v_bytes == 0x504b0506) {
4226
                    ++$v_pos;
4227
                    break;
4228
                }
4229
4230
                ++$v_pos;
4231
            }
4232
4233
            // ----- Look if not found end of central dir
4234
            if ($v_pos == $v_size) {
4235
                // ----- Error log
4236
                self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to find End of Central Dir Record signature');
4237
4238
                // ----- Return
4239
                return self::errorCode();
4240
            }
4241
        }
4242
4243
        // ----- Read the first 18 bytes of the header
4244
        $v_binary_data = fread($this->zip_fd, 18);
4245
4246
        // ----- Look for invalid block size
4247
        if (strlen($v_binary_data) != 18) {
4248
            // ----- Error log
4249
            self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid End of Central Dir Record size : ' . strlen($v_binary_data));
4250
4251
            // ----- Return
4252
            return self::errorCode();
4253
        }
4254
4255
        // ----- Extract the values
4256
        $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
4257
4258
        // ----- Check the global size
4259
        if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
0 ignored issues
show
Bug introduced by
The variable $v_pos 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...
4260
            // ----- Removed in release 2.2 see readme file
4261
            // The check of the file size is a little too strict.
4262
            // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
4263
            // While decrypted, zip has training 0 bytes
4264
            if (0) {
4265
                // ----- Error log
4266
                self::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive. Some trailing bytes exists after the archive.');
4267
4268
                // ----- Return
4269
                return self::errorCode();
4270
            }
4271
        }
4272
4273
        // ----- Get comment
4274
        if ($v_data['comment_size'] != 0) {
4275
            $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
4276
        } else {
4277
            $p_central_dir['comment'] = '';
4278
        }
4279
4280
        $p_central_dir['entries'] = $v_data['entries'];
4281
        $p_central_dir['disk_entries'] = $v_data['disk_entries'];
4282
        $p_central_dir['offset'] = $v_data['offset'];
4283
        $p_central_dir['size'] = $v_data['size'];
4284
        $p_central_dir['disk'] = $v_data['disk'];
4285
        $p_central_dir['disk_start'] = $v_data['disk_start'];
4286
4287
        // TBC
4288
        //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
4289
        //}
4290
4291
        // ----- Return
4292
        return $v_result;
4293
    }
4294
    // --------------------------------------------------------------------------------
4295
4296
    // --------------------------------------------------------------------------------
4297
    // Function : privDeleteByRule()
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...
4298
    // Description :
4299
    // Parameters :
4300
    // Return Values :
4301
    // --------------------------------------------------------------------------------
4302
    public function privDeleteByRule(&$p_result_list, &$p_options)
4303
    {
4304
        $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...
4305
        $v_list_detail = [];
0 ignored issues
show
Unused Code introduced by
$v_list_detail 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...
4306
4307
        // ----- Open the zip file
4308
        if (($v_result = $this->privOpenFd('rb')) != 1) {
4309
            // ----- Return
4310
            return $v_result;
4311
        }
4312
4313
        // ----- Read the central directory informations
4314
        $v_central_dir = [];
4315
        if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
4316
            $this->privCloseFd();
4317
4318
            return $v_result;
4319
        }
4320
4321
        // ----- Go to beginning of File
4322
        @rewind($this->zip_fd);
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...
4323
4324
        // ----- Scan all the files
4325
        // ----- Start at beginning of Central Dir
4326
        $v_pos_entry = $v_central_dir['offset'];
4327
        @rewind($this->zip_fd);
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...
4328 View Code Duplication
        if (@fseek($this->zip_fd, $v_pos_entry)) {
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...
4329
            // ----- Close the zip file
4330
            $this->privCloseFd();
4331
4332
            // ----- Error log
4333
            self::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4334
4335
            // ----- Return
4336
            return self::errorCode();
4337
        }
4338
4339
        // ----- Read each entry
4340
        $v_header_list = [];
4341
        $j_start = 0;
4342
        for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; ++$i) {
4343
            // ----- Read the file header
4344
            $v_header_list[$v_nb_extracted] = [];
4345
            if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
4346
                // ----- Close the zip file
4347
                $this->privCloseFd();
4348
4349
                return $v_result;
4350
            }
4351
4352
            // ----- Store the index
4353
            $v_header_list[$v_nb_extracted]['index'] = $i;
4354
4355
            // ----- Look for the specific extract rules
4356
            $v_found = false;
4357
4358
            // ----- Look for extract by name rule
4359
            if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
4360
                // ----- Look if the filename is in the list
4361
                for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); ++$j) {
4362
                    // ----- Look for a directory
4363
                    if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == '/') {
4364
                        // ----- Look if the directory is in the filename path
4365
                        if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4366
                            $v_found = true;
4367
                        } elseif ((($v_header_list[$v_nb_extracted]['external'] & 0x00000010) == 0x00000010) /* Indicates a folder */ && ($v_header_list[$v_nb_extracted]['stored_filename'] . '/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4368
                            $v_found = true;
4369
                        }
4370
                    } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
4371
                        // ----- Look for a filename
4372
                        $v_found = true;
4373
                    }
4374
                }
4375
            } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != '')) {
4376
                // ----- Look for extract by preg rule
4377
                if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4378
                    $v_found = true;
4379
                }
4380 View Code Duplication
            } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4381
                // ----- Look for extract by index rule
4382
                // ----- Look if the index is in the list
4383
                for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); ++$j) {
4384
                    if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
4385
                        $v_found = true;
4386
                    }
4387
                    if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
4388
                        $j_start = $j + 1;
4389
                    }
4390
                    if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
4391
                        break;
4392
                    }
4393
                }
4394
            } else {
4395
                $v_found = true;
4396
            }
4397
4398
            // ----- Look for deletion
4399
            if ($v_found) {
4400
                unset($v_header_list[$v_nb_extracted]);
4401
            } else {
4402
                ++$v_nb_extracted;
4403
            }
4404
        }
4405
4406
        // ----- Look if something need to be deleted
4407
        if ($v_nb_extracted > 0) {
4408
            // ----- Creates a temporay file
4409
            $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
4410
4411
            // ----- Creates a temporary zip archive
4412
            $v_temp_zip = new self($v_zip_temp_name);
4413
4414
            // ----- Open the temporary zip file in write mode
4415
            if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
4416
                $this->privCloseFd();
4417
4418
                // ----- Return
4419
                return $v_result;
4420
            }
4421
4422
            // ----- Look which file need to be kept
4423
            for ($i = 0; $i < sizeof($v_header_list); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
4424
                // ----- Calculate the position of the header
4425
                @rewind($this->zip_fd);
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...
4426
                if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
4427
                    // ----- Close the zip file
4428
                    $this->privCloseFd();
4429
                    $v_temp_zip->privCloseFd();
4430
                    @unlink($v_zip_temp_name);
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...
4431
4432
                    // ----- Error log
4433
                    self::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4434
4435
                    // ----- Return
4436
                    return self::errorCode();
4437
                }
4438
4439
                // ----- Read the file header
4440
                $v_local_header = [];
4441
                if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
4442
                    // ----- Close the zip file
4443
                    $this->privCloseFd();
4444
                    $v_temp_zip->privCloseFd();
4445
                    @unlink($v_zip_temp_name);
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...
4446
4447
                    // ----- Return
4448
                    return $v_result;
4449
                }
4450
4451
                // ----- Check that local file header is same as central file header
4452
                if ($this->privCheckFileHeaders($v_local_header, $v_header_list[$i]) != 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

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

could be turned into

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

This is much more concise to read.

Loading history...
4453
                    // TBC
4454
                }
4455
                unset($v_local_header);
4456
4457
                // ----- Write the file header
4458 View Code Duplication
                if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4459
                    // ----- Close the zip file
4460
                    $this->privCloseFd();
4461
                    $v_temp_zip->privCloseFd();
4462
                    @unlink($v_zip_temp_name);
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...
4463
4464
                    // ----- Return
4465
                    return $v_result;
4466
                }
4467
4468
                // ----- Read/write the data block
4469
                if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
4470
                    // ----- Close the zip file
4471
                    $this->privCloseFd();
4472
                    $v_temp_zip->privCloseFd();
4473
                    @unlink($v_zip_temp_name);
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...
4474
4475
                    // ----- Return
4476
                    return $v_result;
4477
                }
4478
            }
4479
4480
            // ----- Store the offset of the central dir
4481
            $v_offset = @ftell($v_temp_zip->zip_fd);
4482
4483
            // ----- Re-Create the Central Dir files header
4484
            for ($i = 0; $i < sizeof($v_header_list); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
4485
                // ----- Create the file header
4486 View Code Duplication
                if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4487
                    $v_temp_zip->privCloseFd();
4488
                    $this->privCloseFd();
4489
                    @unlink($v_zip_temp_name);
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...
4490
4491
                    // ----- Return
4492
                    return $v_result;
4493
                }
4494
4495
                // ----- Transform the header to a 'usable' info
4496
                $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
4497
            }
4498
4499
            // ----- Zip file comment
4500
            $v_comment = '';
4501
            if (isset($p_options[PCLZIP_OPT_COMMENT])) {
4502
                $v_comment = $p_options[PCLZIP_OPT_COMMENT];
4503
            }
4504
4505
            // ----- Calculate the size of the central header
4506
            $v_size = @ftell($v_temp_zip->zip_fd) - $v_offset;
4507
4508
            // ----- Create the central dir footer
4509
            if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
4510
                // ----- Reset the file list
4511
                    unset($v_header_list);
4512
                $v_temp_zip->privCloseFd();
4513
                $this->privCloseFd();
4514
                @unlink($v_zip_temp_name);
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...
4515
4516
                    // ----- Return
4517
                    return $v_result;
4518
            }
4519
4520
            // ----- Close
4521
            $v_temp_zip->privCloseFd();
4522
            $this->privCloseFd();
4523
4524
            // ----- Delete the zip file
4525
            // TBC : I should test the result ...
4526
            @unlink($this->zipname);
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...
4527
4528
            // ----- Rename the temporary file
4529
            // TBC : I should test the result ...
4530
            //@rename($v_zip_temp_name, $this->zipname);
4531
            PclZipUtilRename($v_zip_temp_name, $this->zipname);
4532
4533
            // ----- Destroy the temporary archive
4534
            unset($v_temp_zip);
4535
        } elseif ($v_central_dir['entries'] != 0) {
4536
            // ----- Remove every files : reset the file
4537
            $this->privCloseFd();
4538
4539
            if (($v_result = $this->privOpenFd('wb')) != 1) {
4540
                return $v_result;
4541
            }
4542
4543
            if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
4544
                return $v_result;
4545
            }
4546
4547
            $this->privCloseFd();
4548
        }
4549
4550
        // ----- Return
4551
        return $v_result;
4552
    }
4553
    // --------------------------------------------------------------------------------
4554
4555
    // --------------------------------------------------------------------------------
4556
    // Function : privDirCheck()
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...
4557
    // Description :
4558
    //     Check if a directory exists, if not it creates it and all the parents directory
4559
    //     which may be useful.
4560
    // Parameters :
4561
    //     $p_dir : Directory path to check.
4562
    // Return Values :
4563
    //        1 : OK
4564
    //     -1 : Unable to create directory
4565
    // --------------------------------------------------------------------------------
4566
    public function privDirCheck($p_dir, $p_is_dir = false)
4567
    {
4568
        $v_result = 1;
4569
4570
        // ----- Remove the final '/'
4571
        if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
4572
            $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
4573
        }
4574
4575
        // ----- Check the directory availability
4576
        if ((is_dir($p_dir)) || ($p_dir == '')) {
4577
            return 1;
4578
        }
4579
4580
        // ----- Extract parent directory
4581
        $p_parent_dir = dirname($p_dir);
4582
4583
        // ----- Just a check
4584
        if ($p_parent_dir != $p_dir) {
4585
            // ----- Look for parent directory
4586
            if ($p_parent_dir != '') {
4587
                if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) {
4588
                    return $v_result;
4589
                }
4590
            }
4591
        }
4592
4593
        // ----- Create the directory
4594
        if (!@mkdir($p_dir, 0777)) {
4595
            // ----- Error log
4596
            self::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
4597
4598
            // ----- Return
4599
            return self::errorCode();
4600
        }
4601
4602
        // ----- Return
4603
        return $v_result;
4604
    }
4605
    // --------------------------------------------------------------------------------
4606
4607
    // --------------------------------------------------------------------------------
4608
    // Function : privMerge()
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...
4609
    // Description :
4610
    //     If $p_archive_to_add does not exist, the function exit with a success result.
4611
    // Parameters :
4612
    // Return Values :
4613
    // --------------------------------------------------------------------------------
4614
    public function privMerge(&$p_archive_to_add)
4615
    {
4616
        $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...
4617
4618
        // ----- Look if the archive_to_add exists
4619
        if (!is_file($p_archive_to_add->zipname)) {
4620
            // ----- Nothing to merge, so merge is a success
4621
            $v_result = 1;
4622
4623
            // ----- Return
4624
            return $v_result;
4625
        }
4626
4627
        // ----- Look if the archive exists
4628
        if (!is_file($this->zipname)) {
4629
            // ----- Do a duplicate
4630
            $v_result = $this->privDuplicate($p_archive_to_add->zipname);
4631
4632
            // ----- Return
4633
            return $v_result;
4634
        }
4635
4636
        // ----- Open the zip file
4637
        if (($v_result = $this->privOpenFd('rb')) != 1) {
4638
            // ----- Return
4639
            return $v_result;
4640
        }
4641
4642
        // ----- Read the central directory informations
4643
        $v_central_dir = [];
4644
        if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
4645
            $this->privCloseFd();
4646
4647
            return $v_result;
4648
        }
4649
4650
        // ----- Go to beginning of File
4651
        @rewind($this->zip_fd);
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...
4652
4653
        // ----- Open the archive_to_add file
4654
        if (($v_result = $p_archive_to_add->privOpenFd('rb')) != 1) {
4655
            $this->privCloseFd();
4656
4657
            // ----- Return
4658
            return $v_result;
4659
        }
4660
4661
        // ----- Read the central directory informations
4662
        $v_central_dir_to_add = [];
4663
        if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1) {
4664
            $this->privCloseFd();
4665
            $p_archive_to_add->privCloseFd();
4666
4667
            return $v_result;
4668
        }
4669
4670
        // ----- Go to beginning of File
4671
        @rewind($p_archive_to_add->zip_fd);
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...
4672
4673
        // ----- Creates a temporay file
4674
        $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
4675
4676
        // ----- Open the temporary file in write mode
4677
        if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
4678
            $this->privCloseFd();
4679
            $p_archive_to_add->privCloseFd();
4680
4681
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
4682
4683
            // ----- Return
4684
            return self::errorCode();
4685
        }
4686
4687
        // ----- Copy the files from the archive to the temporary file
4688
        // TBC : Here I should better append the file and go back to erase the central dir
4689
        $v_size = $v_central_dir['offset'];
4690
        while ($v_size != 0) {
4691
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4692
            $v_buffer = fread($this->zip_fd, $v_read_size);
4693
            @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
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...
4694
            $v_size -= $v_read_size;
4695
        }
4696
4697
        // ----- Copy the files from the archive_to_add into the temporary file
4698
        $v_size = $v_central_dir_to_add['offset'];
4699
        while ($v_size != 0) {
4700
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4701
            $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
4702
            @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
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...
4703
            $v_size -= $v_read_size;
4704
        }
4705
4706
        // ----- Store the offset of the central dir
4707
        $v_offset = @ftell($v_zip_temp_fd);
4708
4709
        // ----- Copy the block of file headers from the old archive
4710
        $v_size = $v_central_dir['size'];
4711 View Code Duplication
        while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4712
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4713
            $v_buffer = @fread($this->zip_fd, $v_read_size);
4714
            @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
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...
4715
            $v_size -= $v_read_size;
4716
        }
4717
4718
        // ----- Copy the block of file headers from the archive_to_add
4719
        $v_size = $v_central_dir_to_add['size'];
4720 View Code Duplication
        while ($v_size != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4721
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4722
            $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
4723
            @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
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...
4724
            $v_size -= $v_read_size;
4725
        }
4726
4727
        // ----- Merge the file comments
4728
        $v_comment = $v_central_dir['comment'] . ' ' . $v_central_dir_to_add['comment'];
4729
4730
        // ----- Calculate the size of the (new) central header
4731
        $v_size = @ftell($v_zip_temp_fd) - $v_offset;
4732
4733
        // ----- Swap the file descriptor
4734
        // Here is a trick : I swap the temporary fd with the zip fd, in order to use
4735
        // the following methods on the temporary fil and not the real archive fd
4736
        $v_swap = $this->zip_fd;
4737
        $this->zip_fd = $v_zip_temp_fd;
4738
        $v_zip_temp_fd = $v_swap;
4739
4740
        // ----- Create the central dir footer
4741
        if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries'] + $v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
4742
            $this->privCloseFd();
4743
            $p_archive_to_add->privCloseFd();
4744
            @fclose($v_zip_temp_fd);
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...
4745
            $this->zip_fd = null;
4746
4747
            // ----- Reset the file list
4748
            unset($v_header_list);
4749
4750
            // ----- Return
4751
            return $v_result;
4752
        }
4753
4754
        // ----- Swap back the file descriptor
4755
        $v_swap = $this->zip_fd;
4756
        $this->zip_fd = $v_zip_temp_fd;
4757
        $v_zip_temp_fd = $v_swap;
4758
4759
        // ----- Close
4760
        $this->privCloseFd();
4761
        $p_archive_to_add->privCloseFd();
4762
4763
        // ----- Close the temporary file
4764
        @fclose($v_zip_temp_fd);
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...
4765
4766
        // ----- Delete the zip file
4767
        // TBC : I should test the result ...
4768
        @unlink($this->zipname);
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...
4769
4770
        // ----- Rename the temporary file
4771
        // TBC : I should test the result ...
4772
        //@rename($v_zip_temp_name, $this->zipname);
4773
        PclZipUtilRename($v_zip_temp_name, $this->zipname);
4774
4775
        // ----- Return
4776
        return $v_result;
4777
    }
4778
    // --------------------------------------------------------------------------------
4779
4780
    // --------------------------------------------------------------------------------
4781
    // Function : privDuplicate()
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...
4782
    // Description :
4783
    // Parameters :
4784
    // Return Values :
4785
    // --------------------------------------------------------------------------------
4786
    public function privDuplicate($p_archive_filename)
4787
    {
4788
        $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...
4789
4790
        // ----- Look if the $p_archive_filename exists
4791
        if (!is_file($p_archive_filename)) {
4792
            // ----- Nothing to duplicate, so duplicate is a success.
4793
            $v_result = 1;
4794
4795
            // ----- Return
4796
            return $v_result;
4797
        }
4798
4799
        // ----- Open the zip file
4800
        if (($v_result = $this->privOpenFd('wb')) != 1) {
4801
            // ----- Return
4802
            return $v_result;
4803
        }
4804
4805
        // ----- Open the temporary file in write mode
4806 View Code Duplication
        if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
4807
            $this->privCloseFd();
4808
4809
            self::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \'' . $p_archive_filename . '\' in binary write mode');
4810
4811
            // ----- Return
4812
            return self::errorCode();
4813
        }
4814
4815
        // ----- Copy the files from the archive to the temporary file
4816
        // TBC : Here I should better append the file and go back to erase the central dir
4817
        $v_size = filesize($p_archive_filename);
4818
        while ($v_size != 0) {
4819
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4820
            $v_buffer = fread($v_zip_temp_fd, $v_read_size);
4821
            @fwrite($this->zip_fd, $v_buffer, $v_read_size);
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...
4822
            $v_size -= $v_read_size;
4823
        }
4824
4825
        // ----- Close
4826
        $this->privCloseFd();
4827
4828
        // ----- Close the temporary file
4829
        @fclose($v_zip_temp_fd);
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...
4830
4831
        // ----- Return
4832
        return $v_result;
4833
    }
4834
    // --------------------------------------------------------------------------------
4835
4836
    // --------------------------------------------------------------------------------
4837
    // Function : privErrorLog()
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...
4838
    // Description :
4839
    // Parameters :
4840
    // --------------------------------------------------------------------------------
4841
    public function privErrorLog($p_error_code = 0, $p_error_string = '')
4842
    {
4843
        if (PCLZIP_ERROR_EXTERNAL == 1) {
4844
            PclError($p_error_code, $p_error_string);
4845
        } else {
4846
            $this->error_code = $p_error_code;
4847
            $this->error_string = $p_error_string;
4848
        }
4849
    }
4850
    // --------------------------------------------------------------------------------
4851
4852
    // --------------------------------------------------------------------------------
4853
    // Function : privErrorReset()
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...
4854
    // Description :
4855
    // Parameters :
4856
    // --------------------------------------------------------------------------------
4857
    public function privErrorReset()
4858
    {
4859
        if (PCLZIP_ERROR_EXTERNAL == 1) {
4860
            PclErrorReset();
4861
        } else {
4862
            $this->error_code = 0;
4863
            $this->error_string = '';
4864
        }
4865
    }
4866
    // --------------------------------------------------------------------------------
4867
4868
    // --------------------------------------------------------------------------------
4869
    // Function : privDisableMagicQuotes()
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...
4870
    // Description :
4871
    // Parameters :
4872
    // Return Values :
4873
    // --------------------------------------------------------------------------------
4874 View Code Duplication
    public function privDisableMagicQuotes()
0 ignored issues
show
Duplication introduced by
This method 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...
4875
    {
4876
        $v_result = 1;
4877
4878
        // ----- Look if function exists
4879
        if ((!function_exists('get_magic_quotes_runtime')) || (!function_exists('set_magic_quotes_runtime'))) {
4880
            return $v_result;
4881
        }
4882
4883
        // ----- Look if already done
4884
        if ($this->magic_quotes_status != -1) {
4885
            return $v_result;
4886
        }
4887
4888
        // ----- Get and memorize the magic_quote value
4889
        $this->magic_quotes_status = @get_magic_quotes_runtime();
4890
4891
        // ----- Disable magic_quotes
4892
        if ($this->magic_quotes_status == 1) {
4893
            @set_magic_quotes_runtime(0);
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...
4894
        }
4895
4896
        // ----- Return
4897
        return $v_result;
4898
    }
4899
    // --------------------------------------------------------------------------------
4900
4901
    // --------------------------------------------------------------------------------
4902
    // Function : privSwapBackMagicQuotes()
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...
4903
    // Description :
4904
    // Parameters :
4905
    // Return Values :
4906
    // --------------------------------------------------------------------------------
4907 View Code Duplication
    public function privSwapBackMagicQuotes()
0 ignored issues
show
Duplication introduced by
This method 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...
4908
    {
4909
        $v_result = 1;
4910
4911
        // ----- Look if function exists
4912
        if ((!function_exists('get_magic_quotes_runtime')) || (!function_exists('set_magic_quotes_runtime'))) {
4913
            return $v_result;
4914
        }
4915
4916
        // ----- Look if something to do
4917
        if ($this->magic_quotes_status != -1) {
4918
            return $v_result;
4919
        }
4920
4921
        // ----- Swap back magic_quotes
4922
        if ($this->magic_quotes_status == 1) {
4923
            @set_magic_quotes_runtime($this->magic_quotes_status);
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...
4924
        }
4925
4926
        // ----- Return
4927
        return $v_result;
4928
    }
4929
    // --------------------------------------------------------------------------------
4930
}
4931
// End of class
4932
// --------------------------------------------------------------------------------
4933
4934
// --------------------------------------------------------------------------------
4935
// Function : PclZipUtilPathReduction()
4936
// Description :
4937
// Parameters :
4938
// Return Values :
4939
// --------------------------------------------------------------------------------
4940
function PclZipUtilPathReduction($p_dir)
4941
{
4942
    $v_result = '';
4943
4944
    // ----- Look for not empty path
4945
    if ($p_dir != '') {
4946
        // ----- Explode path by directory names
4947
        $v_list = explode('/', $p_dir);
4948
4949
        // ----- Study directories from last to first
4950
        $v_skip = 0;
4951
        for ($i = sizeof($v_list) - 1; $i >= 0; --$i) {
4952
            // ----- Look for current path
4953
            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...
4954
                // ----- Ignore this directory
4955
                // Should be the first $i=0, but no check is done
4956
            } elseif ($v_list[$i] == '..') {
4957
                ++$v_skip;
4958
            } elseif ($v_list[$i] == '') {
4959
                // ----- First '/' i.e. root slash
4960
                if ($i == 0) {
4961
                    $v_result = '/' . $v_result;
4962
                    if ($v_skip > 0) {
4963
                        // ----- It is an invalid path, so the path is not modified
4964
                        // TBC
4965
                        $v_result = $p_dir;
4966
                        $v_skip = 0;
4967
                    }
4968
                } elseif ($i == (sizeof($v_list) - 1)) {
4969
                    // ----- Last '/' i.e. indicates a directory
4970
                    $v_result = $v_list[$i];
4971
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
4972
                    // ----- Double '/' inside the path
4973
                    // ----- Ignore only the double '//' in path,
4974
                    // but not the first and last '/'
4975
                }
4976
            } else {
4977
                // ----- Look for item to skip
4978
                if ($v_skip > 0) {
4979
                    --$v_skip;
4980
                } else {
4981
                    $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? '/' . $v_result : '');
4982
                }
4983
            }
4984
        }
4985
4986
        // ----- Look for skip
4987
        if ($v_skip > 0) {
4988
            while ($v_skip > 0) {
4989
                $v_result = '../' . $v_result;
4990
                --$v_skip;
4991
            }
4992
        }
4993
    }
4994
4995
    // ----- Return
4996
    return $v_result;
4997
}
4998
// --------------------------------------------------------------------------------
4999
5000
// --------------------------------------------------------------------------------
5001
// Function : PclZipUtilPathInclusion()
5002
// Description :
5003
//     This function indicates if the path $p_path is under the $p_dir tree. Or,
5004
//     said in an other way, if the file or sub-dir $p_path is inside the dir
5005
//     $p_dir.
5006
//     The function indicates also if the path is exactly the same as the dir.
5007
//     This function supports path with duplicated '/' like '//', but does not
5008
//     support '.' or '..' statements.
5009
// Parameters :
5010
// Return Values :
5011
//     0 if $p_path is not inside directory $p_dir
5012
//     1 if $p_path is inside directory $p_dir
5013
//     2 if $p_path is exactly the same as $p_dir
5014
// --------------------------------------------------------------------------------
5015
function PclZipUtilPathInclusion($p_dir, $p_path)
5016
{
5017
    $v_result = 1;
5018
5019
    // ----- Look for path beginning by ./
5020 View Code Duplication
    if (($p_dir == '.') || ((strlen($p_dir) >= 2) && (substr($p_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...
5021
        $p_dir = PclZipUtilTranslateWinPath(getcwd(), false) . '/' . substr($p_dir, 1);
5022
    }
5023 View Code Duplication
    if (($p_path == '.') || ((strlen($p_path) >= 2) && (substr($p_path, 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...
5024
        $p_path = PclZipUtilTranslateWinPath(getcwd(), false) . '/' . substr($p_path, 1);
5025
    }
5026
5027
    // ----- Explode dir and path by directory separator
5028
    $v_list_dir = explode('/', $p_dir);
5029
    $v_list_dir_size = sizeof($v_list_dir);
5030
    $v_list_path = explode('/', $p_path);
5031
    $v_list_path_size = sizeof($v_list_path);
5032
5033
    // ----- Study directories paths
5034
    $i = 0;
5035
    $j = 0;
5036
    while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
5037
        // ----- Look for empty dir (path reduction)
5038
        if ($v_list_dir[$i] == '') {
5039
            ++$i;
5040
            continue;
5041
        }
5042
        if ($v_list_path[$j] == '') {
5043
            ++$j;
5044
            continue;
5045
        }
5046
5047
        // ----- Compare the items
5048
        if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
5049
            $v_result = 0;
5050
        }
5051
5052
        // ----- Next items
5053
        ++$i;
5054
        ++$j;
5055
    }
5056
5057
    // ----- Look if everything seems to be the same
5058
    if ($v_result) {
5059
        // ----- Skip all the empty items
5060
        while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
5061
            ++$j;
5062
        }
5063
        while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
5064
            ++$i;
5065
        }
5066
5067
        if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
5068
            // ----- There are exactly the same
5069
            $v_result = 2;
5070
        } elseif ($i < $v_list_dir_size) {
5071
            // ----- The path is shorter than the dir
5072
            $v_result = 0;
5073
        }
5074
    }
5075
5076
    // ----- Return
5077
    return $v_result;
5078
}
5079
// --------------------------------------------------------------------------------
5080
5081
// --------------------------------------------------------------------------------
5082
// Function : PclZipUtilCopyBlock()
5083
// Description :
5084
// Parameters :
5085
//     $p_mode : read/write compression mode
5086
//                         0 : src & dest normal
5087
//                         1 : src gzip, dest normal
5088
//                         2 : src normal, dest gzip
5089
//                         3 : src & dest gzip
5090
// Return Values :
5091
// --------------------------------------------------------------------------------
5092
function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
5093
{
5094
    $v_result = 1;
5095
5096
    if ($p_mode == 0) {
5097
        while ($p_size != 0) {
5098
            $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5099
            $v_buffer = @fread($p_src, $v_read_size);
5100
            @fwrite($p_dest, $v_buffer, $v_read_size);
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...
5101
            $p_size -= $v_read_size;
5102
        }
5103 View Code Duplication
    } elseif ($p_mode == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
5104
        while ($p_size != 0) {
5105
            $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5106
            $v_buffer = @gzread($p_src, $v_read_size);
5107
            @fwrite($p_dest, $v_buffer, $v_read_size);
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...
5108
            $p_size -= $v_read_size;
5109
        }
5110
    } elseif ($p_mode == 2) {
5111
        while ($p_size != 0) {
5112
            $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5113
            $v_buffer = @fread($p_src, $v_read_size);
5114
            @gzwrite($p_dest, $v_buffer, $v_read_size);
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...
5115
            $p_size -= $v_read_size;
5116
        }
5117 View Code Duplication
    } elseif ($p_mode == 3) {
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...
5118
        while ($p_size != 0) {
5119
            $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5120
            $v_buffer = @gzread($p_src, $v_read_size);
5121
            @gzwrite($p_dest, $v_buffer, $v_read_size);
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...
5122
            $p_size -= $v_read_size;
5123
        }
5124
    }
5125
5126
    // ----- Return
5127
    return $v_result;
5128
}
5129
// --------------------------------------------------------------------------------
5130
5131
// --------------------------------------------------------------------------------
5132
// Function : PclZipUtilRename()
5133
// Description :
5134
//     This function tries to do a simple rename() function. If it fails, it
5135
//     tries to copy the $p_src file in a new $p_dest file and then unlink the
5136
//     first one.
5137
// Parameters :
5138
//     $p_src : Old filename
5139
//     $p_dest : New filename
5140
// Return Values :
5141
//     1 on success, 0 on failure.
5142
// --------------------------------------------------------------------------------
5143
function PclZipUtilRename($p_src, $p_dest)
5144
{
5145
    $v_result = 1;
5146
5147
    // ----- Try to rename the files
5148
    if (!@rename($p_src, $p_dest)) {
5149
        // ----- Try to copy & unlink the src
5150
        if (!@copy($p_src, $p_dest)) {
5151
            $v_result = 0;
5152
        } elseif (!@unlink($p_src)) {
5153
            $v_result = 0;
5154
        }
5155
    }
5156
5157
    // ----- Return
5158
    return $v_result;
5159
}
5160
// --------------------------------------------------------------------------------
5161
5162
// --------------------------------------------------------------------------------
5163
// Function : PclZipUtilOptionText()
5164
// Description :
5165
//     Translate option value in text. Mainly for debug purpose.
5166
// Parameters :
5167
//     $p_option : the option value.
5168
// Return Values :
5169
//     The option text value.
5170
// --------------------------------------------------------------------------------
5171
function PclZipUtilOptionText($p_option)
5172
{
5173
    $v_list = get_defined_constants();
5174
    for (reset($v_list); $v_key = key($v_list); next($v_list)) {
5175
        $v_prefix = substr($v_key, 0, 10);
5176
        if ((($v_prefix == 'PCLZIP_OPT') || ($v_prefix == 'PCLZIP_CB_') || ($v_prefix == 'PCLZIP_ATT')) && ($v_list[$v_key] == $p_option)) {
5177
            return $v_key;
5178
        }
5179
    }
5180
5181
    $v_result = 'Unknown';
5182
5183
    return $v_result;
5184
}
5185
// --------------------------------------------------------------------------------
5186
5187
// --------------------------------------------------------------------------------
5188
// Function : PclZipUtilTranslateWinPath()
5189
// Description :
5190
//     Translate windows path by replacing '\' by '/' and optionally removing
5191
//     drive letter.
5192
// Parameters :
5193
//     $p_path : path to translate.
5194
//     $p_remove_disk_letter : true | false
5195
// Return Values :
5196
//     The path translated.
5197
// --------------------------------------------------------------------------------
5198
function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter = true)
5199
{
5200
    if (stristr(php_uname(), 'windows')) {
5201
        // ----- Look for potential disk letter
5202
        if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $v_position = strpos($p_path, ':') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
5203
            $p_path = substr($p_path, $v_position + 1);
5204
        }
5205
        // ----- Change potential windows directory separator
5206
        if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
5207
            $p_path = strtr($p_path, '\\', '/');
5208
        }
5209
    }
5210
5211
    return $p_path;
5212
}
5213